Created
June 1, 2017 20:36
-
-
Save panoslyrakis/5f23ec0df0bb6b70895e752c64d6b3f1 to your computer and use it in GitHub Desktop.
Custom archive virtual page for 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 | |
| /* | |
| Plugin Name: Custom archive virtual page for CoursePress | |
| Plugin URI: https://premium.wpmudev.org/ | |
| Description: Requires CoursePress 2.0 Pro | |
| Author: Panos Lyrakis @ WPMUDEV | |
| Author URI: https://premium.wpmudev.org/ | |
| License: GPLv2 or later | |
| */ | |
| if( ! class_exists( 'WPMUDEV_CP_Custom_Archive' ) ){ | |
| class WPMUDEV_CP_Custom_Archive{ | |
| private static $_instance = null; | |
| public static function get_instance() { | |
| if ( is_null( self::$_instance ) ) { | |
| self::$_instance = new WPMUDEV_CP_Custom_Archive(); | |
| } | |
| return self::$_instance; | |
| } | |
| private function __construct(){ | |
| add_action( 'coursepress_template_course_archive', array( $this, 'coursepress_template_course_archive' ), 10, 2 ); | |
| add_shortcode( 'custom_course_list_box', array( $this, 'course_list_box' ) ); | |
| add_shortcode( 'custom_course_thumbnail', array( $this, 'custom_course_thumbnail' ) ); | |
| add_shortcode( 'custom_course_media', array( $this, 'custom_course_media' ) ); | |
| } | |
| public function coursepress_template_course_archive( $template, $atts ){ | |
| if( $template == '[course_list_box]' ){ | |
| $template = '[custom_course_list_box]'; | |
| } | |
| return $template; | |
| } | |
| public static function course_list_box( $a ) { | |
| $a = shortcode_atts( array( | |
| 'course_id' => CoursePress_Helper_Utility::the_course( true ), | |
| 'clickable' => false, | |
| 'clickable_label' => __( 'Course Details', 'cp' ), | |
| 'override_button_text' => '', | |
| 'override_button_link' => '', | |
| 'button_label' => __( 'Details', 'cp' ), | |
| 'echo' => false, | |
| 'show_withdraw_link' => false, | |
| ), $a, 'course_list_box' ); | |
| $course_id = (int) $a['course_id']; | |
| $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 ); | |
| $content = '<div class="course course_list_box_item course_' . $course_id . ' ' . $clickable_class . ' ' . $completion_class . ' ' . $thumbnail_class . '" ' . $clickable_link . ' ' . $schema .'> | |
| <div class="course-information"> | |
| ' . $course_title . ' | |
| [custom_course_thumbnail course_id="' . $course_id . '"] | |
| [course_summary course_id="' . $course_id . '"] | |
| [course_instructors style="list-flat" link="' . $instructor_link . '" course_id="' . $course_id . '"] | |
| <div class="course-meta-information"> | |
| [course_start label="" course_id="' . $course_id . '"] | |
| [course_language label="" course_id="' . $course_id . '"] | |
| [course_cost label="" course_id="' . $course_id . '"] | |
| [course_categories course_id="' . $course_id . '"] | |
| '.$withdraw_from_course.' | |
| </div>' . | |
| $button_text . $clickable_text . ' | |
| </div> | |
| </div> | |
| '; | |
| if ( $echo ) { | |
| echo $content; | |
| } | |
| return $content; | |
| } | |
| public static function custom_course_thumbnail( $atts ) { | |
| extract( shortcode_atts( array( | |
| 'course_id' => CoursePress_Helper_Utility::the_course( true ), | |
| 'wrapper' => 'figure', | |
| 'class' => '', | |
| ), $atts, 'course_thumbnail' ) ); | |
| $course_id = (int) $course_id; | |
| if ( empty( $course_id ) ) { return ''; } | |
| $wrapper = sanitize_html_class( $wrapper ); | |
| $class = sanitize_html_class( $class ); | |
| return do_shortcode( '[custom_course_media course_id="' . $course_id . '" wrapper="' . $wrapper . '" class="' . $class . '" type="thumbnail"]' ); | |
| } | |
| public static function custom_course_media( $atts ) { | |
| extract( shortcode_atts( array( | |
| 'course_id' => CoursePress_Helper_Utility::the_course( true ), | |
| 'class' => '', | |
| 'height' => CoursePress_Core::get_setting( 'course/image_height' ), | |
| 'list_page' => 'no', | |
| 'priority' => '', // Gives priority to video (or image). | |
| 'type' => '', // Default, video, image. | |
| 'width' => CoursePress_Core::get_setting( 'course/image_width' ), | |
| 'wrapper' => '', | |
| ), $atts, 'course_media' ) ); | |
| $course_id = (int) $course_id; | |
| if ( empty( $course_id ) ) { return ''; } | |
| $type = sanitize_text_field( $type ); | |
| $priority = sanitize_text_field( $priority ); | |
| $list_page = cp_is_true( sanitize_html_class( $list_page ) ); | |
| $class = sanitize_html_class( $class ); | |
| $wrapper = sanitize_html_class( $wrapper ); | |
| $height = sanitize_text_field( $height ); | |
| $width = sanitize_text_field( $width ); | |
| // We'll use pixel if none is set | |
| if ( ! empty( $width ) && (int) $width == $width ) { | |
| $width .= 'px'; | |
| } | |
| if ( ! empty( $height ) && (int) $height == $height ) { | |
| $height .= 'px'; | |
| } | |
| if ( ! $list_page ) { | |
| $type = empty( $type ) ? CoursePress_Core::get_setting( 'course/details_media_type', 'default' ) : $type; | |
| $priority = empty( $priority ) ? CoursePress_Core::get_setting( 'course/details_media_priority', 'video' ) : $priority; | |
| } else { | |
| $type = empty( $type ) ? CoursePress_Core::get_setting( 'course/listing_media_type', 'default' ) : $type; | |
| $priority = empty( $priority ) ? CoursePress_Core::get_setting( 'course/listing_media_priority', 'image' ) : $priority; | |
| } | |
| $priority = 'default' != $type ? false : $priority; | |
| // Saves some overhead by not loading the post again if we don't need to. | |
| $class = sanitize_html_class( $class ); | |
| $course_video = CoursePress_Data_Course::get_setting( $course_id, 'featured_video' ); | |
| $course_image = CoursePress_Data_Course::get_setting( $course_id, 'listing_image' ); | |
| $content = ''; | |
| if ( 'thumbnail' == $type ) { | |
| $type = 'image'; | |
| $priority = 'image'; | |
| } | |
| // If no wrapper and we're specifying a width and height, we need one, so will use div. | |
| if ( empty( $wrapper ) && ( ! empty( $width ) || ! empty( $height ) ) ) { | |
| $wrapper = 'div'; | |
| } | |
| $wrapper_style = ''; | |
| $wrapper_style .= ! empty( $width ) ? 'width:' . $width . ';' : ''; | |
| $wrapper_style .= ! empty( $width ) ? 'height:' . $height . ';' : ''; | |
| if ( is_singular( 'course' ) ) { | |
| $wrapper_style = ''; | |
| } | |
| if ( ( ( 'default' == $type && 'video' == $priority ) || 'video' == $type || ( 'default' == $type && 'image' == $priority && empty( $course_image ) ) ) && ! empty( $course_video ) ) { | |
| $content = '<div class="video_player course-featured-media course-featured-media-' . $course_id . ' ' . $class . '">'; | |
| $content .= ! empty( $wrapper ) ? '<' . $wrapper . ' style="' . $wrapper_style . '">' : ''; | |
| $video_extension = pathinfo( $course_video, PATHINFO_EXTENSION ); | |
| if ( ! empty( $video_extension ) ) { | |
| $attr = array( | |
| 'src' => $course_video, | |
| ); | |
| $content .= wp_video_shortcode( $attr ); | |
| } else { | |
| $embed_args = array(); | |
| // Add YouTube filter. | |
| if ( preg_match( '/youtube.com|youtu.be/', $course_video ) ) { | |
| add_filter( 'oembed_result', array( | |
| 'CoursePress_Helper_Utility', | |
| 'remove_related_videos', | |
| ), 10, 3 ); | |
| } | |
| $content .= wp_oembed_get( $course_video, $embed_args ); | |
| } | |
| $content .= ! empty( $wrapper ) ? '</' . $wrapper . '>' : ''; | |
| $content .= '</div>'; | |
| } | |
| if ( ( ( 'default' == $type && 'image' == $priority ) || 'image' == $type || ( 'default' == $type && 'video' == $priority && empty( $course_video ) ) ) && ! empty( $course_image ) ) { | |
| $content .= '<div class="course-thumbnail course-featured-media course-featured-media-' . $course_id . ' ' . $class . '">'; | |
| //$content .= ! empty( $wrapper ) ? '<' . $wrapper . ' style="' . $wrapper_style . '">' : ''; | |
| $attachment_id = $this->_get_image_id_by_url( $course_image ); | |
| $src = wp_get_attachment_image_src( $attachment_id, 'full' ); | |
| $srcset = wp_get_attachment_image_srcset( $attachment_id, 'full' ); | |
| $sizes = wp_get_attachment_image_sizes( $attachment_id, 'full' ); | |
| $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true); | |
| ob_start(); | |
| ?> | |
| <img src="<?php echo esc_attr( $src );?>" | |
| srcset="<?php echo esc_attr( $srcset ); ?>" | |
| sizes="<?php echo esc_attr( $sizes );?>" | |
| alt="<?php echo esc_attr( $alt );?>" /> | |
| <?php | |
| $content .= ob_get_clean(); | |
| //$content .= ! empty( $wrapper ) ? '</' . $wrapper . '>' : ''; | |
| $content .= '</div>'; | |
| } | |
| return $content; | |
| } | |
| private function _get_attachment_by_url( $image_url ) { | |
| global $wpdb; | |
| $attachment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE guid='%s';", $image_url )); | |
| return $attachment; | |
| } | |
| private function _get_image_id_by_url( $image_url ) { | |
| global $wpdb; | |
| $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); | |
| return $attachment[0]; | |
| } | |
| } | |
| add_action( 'plugins_loaded', function(){ | |
| $GLOBALS['WPMUDEV_CP_Custom_Archive'] = WPMUDEV_CP_Custom_Archive::get_instance(); | |
| }, 10 ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment