Created
June 1, 2015 23:09
-
-
Save bappi-d-great/20c17d08304c72eceb50 to your computer and use it in GitHub Desktop.
Modified course_list shortcode to enable pagination in coursepress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function course_list( $atts ) { | |
extract( shortcode_atts( array( | |
'course_id' => '', | |
'status' => 'publish', | |
'instructor' => '', // Note, one or the other | |
'instructor_msg' => __( 'The Instructor does not have any courses assigned yet.', 'cp' ), | |
'student' => '', // If both student and instructor is specified only student will be used | |
'student_msg' => __( 'You have not yet enrolled in a course. Browse courses %s', 'cp' ), | |
'two_column' => 'yes', | |
'title_column' => 'none', | |
'left_class' => '', | |
'right_class' => '', | |
'course_class' => '', | |
'title_link' => 'yes', | |
'title_class' => 'course-title', | |
'title_tag' => 'h3', | |
'course_status' => 'all', | |
'list_wrapper_before' => 'div', | |
'list_wrapper_before_class' => 'course-list %s', | |
'list_wrapper_after' => 'div', | |
'show' => 'dates,enrollment_dates,class_size,cost', | |
'show_button' => 'yes', | |
'show_divider' => 'yes', | |
'show_media' => 'false', | |
'show_title' => 'yes', | |
'media_type' => get_option( 'listings_media_type', 'image' ), // default, image, video | |
'media_priority' => get_option( 'listings_media_priority', 'image' ), // image, video | |
'admin_links' => false, | |
'manage_link_title' => __( 'Manage Course', 'cp' ), | |
'finished_link_title' => __( 'View Course', 'cp' ), | |
'limit' => -1, | |
'order' => 'ASC', | |
'class' => '', | |
'pagination' => false | |
), $atts, 'course_list' ) ); | |
if ( !empty( $course_id ) ) { | |
$course_id = (int) $course_id; | |
} | |
$status = sanitize_html_class( $status ); | |
$instructor = sanitize_text_field( $instructor ); | |
$instructor_msg = sanitize_text_field( $instructor_msg ); | |
$student = sanitize_text_field( $student ); | |
$student_msg = sanitize_text_field( $student_msg ); | |
$two_column = sanitize_html_class( $two_column ); | |
$title_column = sanitize_text_field( $title_column ); | |
$left_class = sanitize_html_class( $left_class ); | |
$right_class = sanitize_html_class( $right_class ); | |
$course_class = sanitize_html_class( $course_class ); | |
$title_link = sanitize_html_class( $title_link ); | |
$title_class = sanitize_html_class( $title_class ); | |
$title_tag = sanitize_html_class( $title_tag ); | |
$course_status = sanitize_text_field( $course_status ); | |
$list_wrapper_before = sanitize_html_class( $list_wrapper_before ); | |
$list_wrapper_after = sanitize_html_class( $list_wrapper_after ); | |
$show = sanitize_text_field( $show ); | |
$show_button = sanitize_html_class( $show_button ); | |
$show_divider = sanitize_html_class( $show_divider ); | |
$show_title = sanitize_html_class( $show_title ); | |
$show_media = sanitize_html_class( $show_media ); | |
$media_type = !empty( $media_type ) ? sanitize_text_field( $media_type ) : 'image'; | |
$media_priority = !empty( $media_priority ) ? sanitize_text_field( $media_priority ) : 'image'; | |
$admin_links = (bool) $admin_links; | |
$manage_link_title = sanitize_text_field( $manage_link_title ); | |
$finished_link_title = sanitize_text_field( $finished_link_title ); | |
$limit = (int) $limit; | |
$order = sanitize_html_class( $order ); | |
$class = sanitize_html_class( $class ); | |
$status = 'published' == $status ? 'publish' : $status; | |
// student or instructor ids provided | |
$user_provided = false; | |
$user_provided = empty( $student ) ? empty( $instructor ) ? false : true : true; | |
$content = ''; | |
$courses = array(); | |
if ( !empty( $instructor ) ) { | |
$include_ids = array(); | |
$instructors = explode( ',', $instructor ); | |
if ( !empty( $instructors ) ) { | |
foreach ( $instructors as $ins ) { | |
$ins = (int) $ins; | |
if ( $ins ) { | |
$ins = new Instructor( $ins ); | |
$course_ids = $ins->get_assigned_courses_ids( $status ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} else { | |
$instructor = (int) $instructor; | |
if ( $instructor ) { | |
$instructor = new Instructor( $ins ); | |
$course_ids = $instructor->get_assigned_courses_ids( $status ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} | |
if ( !empty( $student ) ) { | |
$include_ids = array(); | |
$students = explode( ',', $student ); | |
if ( !empty( $students ) ) { | |
foreach ( $students as $stud ) { | |
$stud = (int) $stud; | |
if ( $stud ) { | |
$stud = new Student( $stud ); | |
$course_ids = $stud->get_assigned_courses_ids( $status ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} else { | |
$student = (int) $student; | |
if ( $student ) { | |
$student = new Student( $student ); | |
$course_ids = $student->get_assigned_courses_ids( $status ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} | |
$post_args = array( | |
'order' => $order, | |
'post_type' => 'course', | |
'meta_key' => 'enroll_type', | |
'post_status' => $status, | |
'posts_per_page' => $limit | |
); | |
if( $pagination ){ | |
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; | |
$post_args['paged'] = $paged; | |
} | |
if ( !empty( $include_ids ) ) { | |
$post_args = wp_parse_args( array( 'include' => $include_ids ), $post_args ); | |
} | |
if ( $user_provided && !empty( $include_ids ) || !$user_provided ) { | |
$courses = get_posts( $post_args ); | |
} | |
//<div class="course-list %s"> | |
$content .= 0 < count( $courses ) && !empty( $list_wrapper_before ) ? '<' . $list_wrapper_before . ' class=' . $list_wrapper_before_class . '>' : ''; | |
foreach ( $courses as $course ) { | |
if ( !empty( $student ) && 'all' != strtolower( $course_status ) && !is_array( $student ) ) { | |
$completion = new Course_Completion( $course->ID ); | |
$completion->init_student_status( $student ); | |
$course->completed = $completion->is_course_complete(); | |
// Skip if we wanted a completed course but got an incomplete | |
if ( 'completed' == strtolower( $course_status ) && !$course->completed ) { | |
continue; | |
} | |
// Skip if we wanted an incompleted course but got a completed | |
if ( 'incomplete' == strtolower( $course_status ) && $course->completed ) { | |
continue; | |
} | |
} | |
$content .= '<div class="course-list-item ' . $course_class . '">'; | |
if ( 'yes' == $show_media ) { | |
$content .= do_shortcode( '[course_media course_id="' . $course->ID . '" type="' . $media_type . '" priority="' . $media_priority . '"]' ); | |
} | |
if ( 'none' == $title_column ) { | |
$content .= do_shortcode( '[course_title course_id="' . $course->ID . '" link="' . $title_link . '" class="' . $title_class . '" title_tag="' . $title_tag . '"]' ); | |
} | |
if ( 'yes' == $two_column ) { | |
$content .= '<div class="course-list-box-left ' . $left_class . '">'; | |
} | |
if ( 'left' == $title_column ) { | |
$content .= do_shortcode( '[course_title course_id="' . $course->ID . '" link="' . $title_link . '" class="' . $title_class . '" title_tag="' . $title_tag . '"]' ); | |
} | |
// One liner.. | |
$content .= do_shortcode( '[course show="' . $show . '" show_title="yes" course_id="' . $course->ID . '"]' ); | |
if ( 'yes' == $two_column ) { | |
$content .= '</div>'; | |
$content .= '<div class="course-list-box-right ' . $right_class . '">'; | |
} | |
if ( 'right' == $title_column ) { | |
$content .= do_shortcode( '[course_title course_id="' . $course->ID . '" link="' . $title_link . '" class="' . $title_class . '" title_tag="' . $title_tag . '"]' ); | |
} | |
if ( 'yes' == $show_button ) { | |
if ( !empty( $course->completed ) ) { | |
$content .= do_shortcode( '[course_join_button course_id="' . $course->ID . '" continue_learning_text="' . $finished_link_title . '"]' ); | |
} else { | |
$content .= do_shortcode( '[course_join_button course_id="' . $course->ID . '"]' ); | |
} | |
} | |
if ( $admin_links ) { | |
$content .= '<button class="manage-course" data-link="' . admin_url( 'admin.php?page=course_details&course_id=' . $course->ID ) . '">' . $manage_link_title . '</button>'; | |
} | |
// Add action links if student | |
if ( !empty( $student ) ) { | |
$content .= do_shortcode( '[course_action_links course_id="' . $course->ID . '"]' ); | |
} | |
if ( 'yes' == $two_column ) { | |
$content .= '</div>'; | |
} | |
if ( 'yes' == $show_divider ) { | |
$content .= '<div class="divider" ></div>'; | |
} | |
$content .= '</div>'; //course-list-item | |
} // foreach | |
if ( (!$courses || 0 == count( $courses ) ) && !empty( $instructor ) ) { | |
$content .= $instructor_msg; | |
} | |
if ( (!$courses || 0 == count( $courses ) ) && !empty( $student ) ) { | |
$content .= sprintf( $student_msg, '<a href="' . trailingslashit( site_url() . '/' . CoursePress::instance()->get_course_slug() ) . '">' . __( 'here', 'cp' ) . '</a>' ); | |
} | |
// </div> course-list | |
$content .= 0 < count( $courses ) && !empty( $list_wrapper_before ) ? '</' . $list_wrapper_after . '>' : ''; | |
if( $pagination ){ | |
global $wp_query; | |
$total_course = wp_count_posts( 'course' ); | |
$big = 999999999; // need an unlikely integer | |
$content .= paginate_links( array( | |
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), | |
'format' => '?paged=%#%', | |
'current' => max( 1, get_query_var('paged') ), | |
'total' => $total_course->publish | |
) ); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment