Created
March 3, 2018 09:01
-
-
Save wpmudev-sls/352398e87cdc11e09cc5d2a7f703b33f to your computer and use it in GitHub Desktop.
[CursePress] - Custom Dashboard page
This file contains 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
function cp_custom_dashboard_page_sc( $atts ){ | |
CoursePress_Core::$is_cp_page = true; | |
$atts = CoursePress_Helper_Utility::sanitize_recursive( | |
shortcode_atts( | |
array( | |
'completed_label' => __( 'Completed courses', 'cp' ), | |
'context' => 'all', // <blank>, enrolled, completed | |
'current_label' => __( 'Current Courses', 'cp' ), | |
'dashboard' => false, | |
'facilitator_label' => __( 'Facilitated Courses', 'cp' ), | |
'facilitator' => '', | |
'future_label' => __( 'Starting soon', 'cp' ), | |
'incomplete_label' => __( 'Incomplete courses', 'cp' ), | |
'instructor_msg' => __( 'The Instructor does not have any courses assigned yet.', 'cp' ), | |
'instructor' => '', // Note, one or the other | |
'limit' => - 1, | |
'manage_label' => __( 'Manage Courses', 'cp' ), | |
'order' => 'ASC', | |
'orderby' => 'meta', /// possible values: meta, title | |
'past_label' => __( 'Past courses', 'cp' ), | |
'show_labels' => false, | |
'status' => 'publish', | |
'student_msg' => sprintf( __( 'You are not enrolled in any courses. <a href="%s">See available courses.</a>', 'cp' ), CoursePress_Core::get_slug( 'course/', true ) ), | |
'student' => '', // If both student and instructor is specified only student will be used | |
'suggested_label' => __( 'Suggested courses', 'cp' ), | |
'suggested_msg' => __( 'You are not enrolled in any courses.<br />Here are a few you might like, or <a href="%s">see all available courses.</a>', 'cp' ), | |
'show_withdraw_link' => false, | |
'categories' => '', | |
), | |
$atts, | |
'course_page' | |
) | |
); | |
$instructor_list = false; | |
$student_list = false; | |
$atts['dashboard'] = cp_is_true( $atts['dashboard'] ); | |
$courses = array(); | |
$content = ''; | |
$student = 0; | |
$include_ids = array(); | |
/** | |
* Sanitize show_withdraw_link | |
*/ | |
if ( empty( $atts['student'] ) || 'incomplete' != $atts['status'] ) { | |
$atts['show_withdraw_link'] = false; | |
} | |
if ( ! empty( $atts['instructor'] ) ) { | |
$include_ids = array(); | |
$instructors = explode( ',', $atts['instructor'] ); | |
if ( ! empty( $instructors ) ) { | |
foreach ( $instructors as $ins ) { | |
$ins = (int) $ins; | |
if ( $ins ) { | |
$course_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $ins, $atts['status'] ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} else { | |
$instructor = (int) $atts['instructor']; | |
if ( $instructor ) { | |
$course_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $instructor, $atts['status'] ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
$instructor_list = true; | |
if ( empty( $include_ids ) ) { return ''; } | |
} | |
if ( ! empty( $atts['facilitator'] ) ) { | |
$facilitator = $atts['facilitator']; | |
$atts['context'] = 'facilitator'; | |
$include_ids = CoursePress_Data_Facilitator::get_facilitated_courses( $facilitator, 'publish', true ); | |
if ( empty( $include_ids ) ) { | |
return ''; | |
} | |
} | |
if ( ! empty( $atts['student'] ) ) { | |
$include_ids = array(); | |
$students = explode( ',', $atts['student'] ); | |
foreach ( $students as $student ) { | |
$student = (int) $student; | |
if ( $student ) { | |
$courses_ids = array(); | |
$courses_to_add = CoursePress_Data_Student::get_enrolled_courses_ids( $student ); | |
if ( isset( $atts['status'] ) ) { | |
foreach ( $courses_to_add as $course_id ) { | |
$status = get_post_status( $course_id ); | |
if ( 'publish' != $status ) { | |
continue; | |
} | |
$add = true; | |
if ( 'publish' != $atts['status'] ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $student, false ); | |
if ( 'completed' == $atts['status'] ) { | |
$add = false; | |
if ( 'certified' == $status ) { | |
$add = true; | |
} | |
} else { | |
if ( 'certified' == $status ) { | |
$add = false; | |
} | |
} | |
} | |
if ( $add ) { | |
$courses_ids[] = $course_id; | |
} | |
} | |
} else { | |
$courses_ids = $courses_to_add; | |
} | |
if ( $courses_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $courses_ids ) ); | |
} | |
} | |
} | |
$student_list = true; | |
} | |
$post_args = array( | |
'order' => $atts['order'], | |
'post_type' => CoursePress_Data_Course::get_post_type_name(), | |
'post_status' => $atts['status'], | |
'posts_per_page' => (int) $atts['limit'], | |
'suppress_filters' => true, | |
'meta_key' => 'cp_course_start_date', | |
'orderby' => 'meta_value_num', | |
); | |
/** | |
* categories | |
*/ | |
if ( ! empty( $atts['categories'] ) ) { | |
$post_args['tax_query'] = array( | |
array( | |
'taxonomy' => CoursePress_Data_Course::get_post_category_name(), | |
'field' => 'slug', | |
'terms' => preg_split( '/[, ]+/', $atts['categories'] ), | |
), | |
); | |
} | |
$test_empty_courses_ids = false; | |
switch ( $atts['context'] ) { | |
case 'enrolled': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$include_ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
break; | |
case 'incomplete': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
foreach ( $ids as $course_id ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $user_id, false ); | |
if ( 'certified' != $status ) { | |
$include_ids[] = $course_id; | |
} | |
} | |
break; | |
case 'completed': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
foreach ( $ids as $course_id ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $user_id, false ); | |
if ( 'certified' == $status ) { | |
$include_ids[] = $course_id; | |
} | |
} | |
break; | |
case 'future': | |
unset( $post_args['meta_key'] ); | |
$post_args['meta_query'] = array( | |
array( | |
'key' => 'cp_course_start_date', | |
'value' => time(), | |
'type' => 'NUMERIC', | |
'compare' => '>', | |
), | |
); | |
break; | |
case 'past': | |
unset( $post_args['meta_key'] ); | |
$post_args['meta_query'] = array( | |
'relation' => 'AND', | |
array( | |
'key' => 'cp_course_end_date', | |
'compare' => 'EXISTS', | |
), | |
array( | |
'key' => 'cp_course_end_date', | |
'value' => 0, | |
'type' => 'NUMERIC', | |
'compare' => '>', | |
), | |
array( | |
'key' => 'cp_course_end_date', | |
'value' => time(), | |
'type' => 'NUMERIC', | |
'compare' => '<', | |
), | |
); | |
break; | |
case 'manage': | |
$user_id = get_current_user_id(); | |
$test_empty_courses_ids = true; | |
if ( CoursePress_Data_Capabilities::can_manage_courses( $user_id ) ) { | |
$local_args = array( | |
'post_type' => CoursePress_Data_Course::get_post_type_name(), | |
'nopaging' => true, | |
'fields' => 'ids', | |
); | |
$include_ids = get_posts( $local_args ); | |
} else { | |
$include_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $user_id ); | |
if ( empty( $include_ids ) ) { | |
$include_ids = CoursePress_Data_Facilitator::get_facilitated_courses( $user_id, array( 'all' ), true, 0, -1 ); | |
} | |
} | |
break; | |
case 'all': | |
$atts['orderby'] = strtolower( $atts['orderby'] ); | |
switch ( $atts['orderby'] ) { | |
case 'title': | |
case 'post_title': | |
$post_args['orderby'] = 'title'; | |
break; | |
default: | |
$post_args['orderby'] = 'meta_value_num'; | |
break; | |
} | |
break; | |
} | |
if ( $test_empty_courses_ids && empty( $include_ids ) ) { | |
/** | |
* do nothing if we have empty list | |
*/ | |
$courses = array(); | |
} else if ( ( ( $student_list || $instructor_list ) && ! empty( $include_ids ) ) || ( ! $student_list && ! $instructor_list ) ) { | |
if ( ! empty( $include_ids ) ) { | |
$post_args = wp_parse_args( array( 'post__in' => $include_ids ), $post_args ); | |
} | |
$courses = get_posts( $post_args ); | |
} | |
$counter = 0; | |
if ( ! $atts['dashboard'] ) { | |
foreach ( $courses as $course ) { | |
$shortcode_attributes = array( | |
'course_id' => $course->ID, | |
'show_withdraw_link' => $atts['show_withdraw_link'], | |
); | |
$shortcode_attributes = CoursePress_Helper_Utility::convert_array_to_params( $shortcode_attributes ); | |
$content .= do_shortcode( '[course_list_box ' . $shortcode_attributes . ']' ); | |
$counter += 1; | |
} | |
} else { | |
if ( $student_list ) { | |
$my_courses = CoursePress_Data_Student::my_courses( $student, $courses ); | |
$context = $atts['context']; | |
if ( isset( $my_courses[ $context ] ) ) { | |
$courses = $my_courses[ $context ]; | |
} | |
$courses = array_filter( $courses ); | |
if ( empty( $courses ) ) { | |
if ( $atts['dashboard'] ) { | |
$content .= sprintf( '<p class="message">%s</p>', esc_html__( 'You are not enrolled to any course.', 'cp' ) ); | |
} | |
} else { | |
$counter += count( $courses ); | |
$content .= CoursePress_Template_Course::course_list_table( $courses ); | |
} | |
} else { | |
foreach ( $courses as $course ) { | |
$course_url = get_edit_post_link( $course->ID ); | |
$content .= do_shortcode( '[course_list_box course_id="' . $course->ID . '" override_button_text="' . esc_attr__( 'Manage Course', 'cp' ) . '" override_button_link="' . esc_url( $course_url ) . '"]' ); | |
$counter += 1; | |
} | |
} | |
} | |
$context = $atts['dashboard'] && $instructor_list ? 'manage' : $atts['context']; | |
if ( ( $atts['dashboard'] && ! empty( $counter ) ) || ! empty( $atts['show_labels'] ) ) { | |
$label = ''; | |
$show_empty = false; | |
switch ( $context ) { | |
case 'enrolled': | |
case 'current': | |
case 'all': | |
$label = $atts['current_label']; | |
if ( 0 == $counter ) { | |
$show_empty = true; | |
$content = sprintf( | |
'<p class="message">%s</p>', | |
sprintf( | |
$atts['student_msg'], | |
esc_attr( '/'.CoursePress_Core::get_setting( 'slugs/course', 'courses' ) ) | |
) | |
); | |
} | |
break; | |
case 'future': | |
$label = $atts['future_label']; | |
break; | |
case 'incomplete': | |
$label = $atts['incomplete_label']; | |
break; | |
case 'completed': | |
$label = $atts['completed_label']; | |
break; | |
case 'past': | |
$label = $atts['past_label']; | |
break; | |
case 'manage': | |
$label = $atts['manage_label']; | |
break; | |
case 'facilitator': | |
$label = $atts['facilitator_label']; | |
break; | |
} | |
if ( $counter || ( 0 === $counter && $show_empty ) ) { | |
$content = '<div class="dashboard-course-list ' . esc_attr( $context ) . '">' . | |
'<h3 class="section-title">' . esc_html( $label ) . '</h3>' . | |
$content . | |
'</div>'; | |
} | |
} elseif ( $atts['dashboard'] && 'enrolled' === $context ) { | |
$label = $atts['suggested_label']; | |
$message = sprintf( $atts['suggested_msg'], esc_url( CoursePress_Core::get_slug( 'courses', true ) ) ); | |
$content = '<div class="dashboard-course-list suggested">' . | |
'<h3 class="section-title">' . esc_html( $label ) . '</h3>' . | |
'<p>' . $message . '</p>' . | |
do_shortcode( '[course_random featured_title="" media_type="image" media_priority="image"]' ) . | |
'</div>'; | |
} | |
return $content; | |
} | |
function cp_custom_dashboard_page( $template, $user_id, $a ){ | |
$template = '<div class="coursepress-dashboard-wrapper"> | |
[course_list instructor="%1$s" dashboard="true"] | |
[course_list facilitator="%1$s" dashboard="true"] | |
[cp_custom_dashboard_page student="%1$s" dashboard="true" current_label="%2$s" show_labels="true" orderby="post_title"] | |
</div>'; | |
return sprintf( $template, $user_id, __( 'Enrolled Courses', 'cp' ) ); | |
} | |
add_shortcode( 'cp_custom_dashboard_page', 'cp_custom_dashboard_page_sc' ); | |
add_filter( 'coursepress_template_dashboard_page', 'cp_custom_dashboard_page', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment