Created
February 24, 2017 12:25
-
-
Save panoslyrakis/09b6fb12f74ff9a3bb5e025fa94d6c14 to your computer and use it in GitHub Desktop.
Custom courses list for CoursePress Pro
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
<?php | |
/** | |
* Plugin Name: Custom courses list for CoursePress Pro | |
* Version: 1.0 | |
* Description: Provides a shortcode that outputs a custom list of courses | |
* Author: Panos Lyrakis (WPMU DEV) | |
* Author URI: http://premium.wpmudev.org | |
* Plugin URI: http://premium.wpmudev.org/project/coursepress/ | |
* License: GPL2 | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
* TextDomain: cp | |
*/ | |
if( ! class_exists( 'WPMUDEV_CP_Custom_List ') ){ | |
class WPMUDEV_CP_Custom_List{ | |
private static $_instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_CP_Custom_List(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
//Allow shortcodes in widgets: | |
//add_filter('widget_text','do_shortcode'); | |
//Override atts of [course_archive] shortcode | |
add_filter( 'shortcode_atts_course_archive', array( $this, 'shortcode_atts_course_archive' ), 10, 1 ); | |
//Override box $template | |
add_filter( 'coursepress_template_course_list_box', array( $this, 'template_course_list_box' ), 10, 3 ); | |
} | |
public function shortcode_atts_course_archive( $atts ){ | |
if( ! is_main_query() || ! in_the_loop() ){ | |
//The number of courses in a widget | |
$atts['posts_per_page'] = 4; | |
} | |
return $atts; | |
} | |
/* | |
* Return the custom shortcode | |
*/ | |
public function course_archive_template( $template, $a ){ | |
//It will return custom shortcode is in widget | |
if( ! is_main_query() || ! in_the_loop() ) { | |
$template = trim( '[wpmudev_course_list_box]' ); | |
} | |
return $template; | |
} | |
public function template_course_list_box( $template, $course_id, $a ){ | |
if( is_main_query() && in_the_loop() ) { | |
return $template; | |
} | |
$clickable_label = sanitize_text_field( $a['clickable_label'] ); | |
$echo = cp_is_true( $a['echo'] ); | |
$clickable = cp_is_true( $a['clickable'] ); | |
$url = CoursePress_Data_Course::get_course_url( $course_id ); | |
$course_image = CoursePress_Data_Course::get_setting( $course_id, 'listing_image' ); | |
$has_thumbnail = ! empty( $course_image ); | |
$clickable_link = $clickable ? 'data-link="' . esc_url( $url ) . '"' : ''; | |
$clickable_class = $clickable ? 'clickable' : ''; | |
$clickable_text = $clickable ? '<div class="clickable-label">' . $clickable_label . '</div>' : ''; | |
$button_label = $a['button_label']; | |
$button_link = $url; | |
$withdraw_from_course = ''; | |
if ( ! empty( $a['override_button_link'] ) ) { | |
$button_link = $a['override_button_link']; | |
} | |
$button_text = sprintf( '<a href="%s" rel="bookmark" class="button apply-button apply-button-details">%s</a>', esc_url( $button_link ), $button_label ); | |
$instructor_link = $clickable ? 'no' : 'yes'; | |
$thumbnail_class = $has_thumbnail ? 'has-thumbnail' : ''; | |
$completed = false; | |
$student_progress = false; | |
if ( is_user_logged_in() ) { | |
$student_progress = CoursePress_Data_Student::get_completion_data( get_current_user_id(), $course_id ); | |
$completed = isset( $student_progress['completion']['completed'] ) && ! empty( $student_progress['completion']['completed'] ); | |
// Withdraw from course | |
$show_withdraw_link = cp_is_true( $a['show_withdraw_link'] ); | |
if ( $show_withdraw_link && ! $completed ) { | |
$withdraw_link = add_query_arg( | |
array( | |
'_wpnonce' => wp_create_nonce( 'coursepress_student_withdraw' ), | |
'course_id' => $course_id, | |
'student_id' => get_current_user_id(), | |
) | |
); | |
$withdraw_from_course = sprintf( '<a href="%s" class="cp-withdraw-student">%s</a>', esc_url( $withdraw_link ), __( 'Withdraw', 'cp' ) ); | |
} | |
} | |
$completion_class = CoursePress_Data_Course::course_class( $course_id ); | |
$completion_class = implode( ' ', $completion_class ); | |
// Add filter to post classes | |
// Override button | |
if ( ! empty( $a['override_button_text'] ) && ! empty( $a['override_button_link'] ) ) { | |
$button_text = '<button class="coursepress-course-link" data-link="' . esc_url( $a['override_button_link'] ) . '">' . esc_attr( $a['override_button_text'] ) . '</button>'; | |
} | |
// schema.org | |
$schema = apply_filters( 'coursepress_schema', '', 'itemscope' ); | |
$course_title = do_shortcode( sprintf( '[course_title course_id="%s"]', $course_id ) ); | |
$course_title = sprintf( '<a href="%s" rel="bookmark">%s</a>', esc_url( $url ), $course_title ); | |
$template = '<div class="course course_list_box_item course_' . $course_id . ' ' . $clickable_class . ' ' . $completion_class . ' ' . $thumbnail_class . '" ' . $clickable_link . ' ' . $schema .'> | |
[course_thumbnail course_id="' . $course_id . '"] | |
<div class="course-information"> | |
' . $course_title . $button_text . $clickable_text . ' | |
</div> | |
</div> | |
'; | |
$template = apply_filters( 'wpmudev_coursepress_template_course_list_box', $template, $course_id, $a ); | |
$content = do_shortcode( $template ); | |
if ( $echo ) { | |
echo $content; | |
} | |
return $content; | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_CP_Custom_List'] = WPMUDEV_CP_Custom_List::get_instance(); | |
} ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment