public function __construct() {
add_shortcode(‘course_list’, array($this, ‘display_course_list’));
}
/**
* Display course list in frontend.
*
* Shortcode to display course list in frontend.
*
* @access public
* @param
* @return
* @since 1.0.0
*/
public function display_course_list() {
// Fetch the results
$output = '';
$courses_per_page = 2;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$get_course_page_slug = get_course_page_slug();
// Fetch the results with pagination
$results = get_course_data( $paged, $courses_per_page );
// HTML table
$output = '<div class="table-responsive">';
$output .= '<a href="'.home_url().'/'.$get_course_page_slug.'?add=1" class="btn btn-primary mb-3" id="addCourseBtn">Add Course</a>';
$output .= '<table class="table table-bordered table-striped">';
$output .= '<thead class="thead-dark"><tr><th>Course Name</th><th>Description</th><th>Duration</th><th>Price</th><th colspan=4 class="text-center">Action</th></tr></thead>';
$output .= '<tbody>';
if( !empty( $results )){
// Loop through results to populate table rows
foreach ($results as $result) {
$output .= '<tr>';
$output .= '<td>' . $result->course_name . '</td>';
$output .= '<td>' . $result->description . '</td>';
$output .= '<td>' . $result->duration . '</td>';
$output .= '<td>' . $result->price . '</td>';
$output .= '<td><a href="'.home_url().'/'.$get_course_page_slug.'?edit=1&edit_id='.$result->id.'" class="btn btn-primary mb-3" id="editCourseBtn">Edit</a></td><td><a href="javascript:void(0)" data-course-id="'.$result->id.'" class="btn bg-white mb-3 deleteCourseBtn">Delete</a></td>';
$output .= '</tr>';
}
} else {
// Handle case when no data is found
$output .= '<tr><td colspan="6">No course data found.</td></tr>';
}
// Close the HTML table
$output .= '</table>';
$count = get_course_count();
// Add pagination links
$output .= '<div class="pagination">';
$output .= '<nav aria-label="Page navigation">';
$output .= '<ul class="pagination">';
$big = 999999999;
$pagination_args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'total' => ceil( $count / $courses_per_page ),
'current' => $paged,
'show_all' => false,
'next_text' => '<span class="custom_next_link">→</span>',
'prev_next' => true,
'type' => 'list',
'mid_size' => 3,
'prev_text' => '<span class="custom_prev_link">←</span>',
);
$output .= paginate_links( $pagination_args );
$output .= '</ul>';
$output .= ”;
$output .= '</div>';
return $output;
}
}
function get_course_data( $paged, $courses_per_page ){
global $wpdb;
$table_name = $wpdb->prefix . ‘course_detail’;
$offset = ( $paged – 1 ) * $courses_per_page;
$query = $wpdb->prepare( “SELECT * FROM $table_name LIMIT %d, %d”, $offset, $courses_per_page );
$results = $wpdb->get_results($query);
return $results;
}
if(!function_exists(‘get_course_count’)) {
/**
* Get course count.
*
* @access public
* @param
* @return string
* @since 1.0.0
*/
function get_course_count(){
global $wpdb;
$table_name = $wpdb->prefix . 'course_detail';
$count_query = "SELECT COUNT(*) FROM $table_name";
return $wpdb->get_var($count_query);
}
}
if(!function_exists(‘get_course_page_slug’)) {
/**
* Get course page slug.
*
* @access public
* @param
* @return string
* @since 1.0.0
*/
function get_course_page_slug(){
$page_id = get_option('add_course_page_id');
if( $page_id ){
$page_slug = get_post_field( 'post_name', $page_id );
return $page_slug;
}
}
}
if(!function_exists(‘get_course_by_ID’)) {
/**
* Get course by id.
*
* @access public
* @param int
* @return array
* @since 1.0.0
*/
function get_course_by_ID($course_id){
global $wpdb;
$course = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}course_detail WHERE id = %d", $course_id) );
return $course;
}
}