Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created September 6, 2018 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/88cd6df6fc378317a8266c9659c04996 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/88cd6df6fc378317a8266c9659c04996 to your computer and use it in GitHub Desktop.
[CoursePress] - Alter [course_media] shortcode to enable thumbnail.
<?php
/**
*
* Plugin Name: CoursePress - Course Thumbnail
* Description: Alter [course_media] shortcode to enable thumbnail.
* Version: 1.0.0
* Author: Konstantinos Xenos @ WPMU DEV
* Author URI: https://premium.wpmudev.org/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
*/
add_action( 'coursepress_course_updated', 'cp_update_thumbnail_id', 10, 2 );
add_action( 'coursepress_course_created', 'cp_update_thumbnail_id', 10, 2 );
function cp_update_thumbnail_id( $course_id, $settings ) {
global $wpdb;
$course_image = $settings['listing_image'];
if ( ! empty( $course_image ) ) {
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s;", $course_image ) );
update_post_meta( $course_id, '_thumbnail_id', $attachment[0] );
} else {
update_post_meta( $course_id, '_thumbnail_id', null );
}
}
add_action( 'init', 'replace_cp_cm_shortcode', 20 );
function replace_cp_cm_shortcode() {
remove_shortcode( 'course_media' );
add_shortcode( 'course_media', 'cp_thumbnail_media_shortcode' );
}
function cp_thumbnail_media_shortcode( $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';
$width = $height = '';
}
// 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 . '">' : '';
$content .= get_the_post_thumbnail( $course_id );
$content .= ! empty( $wrapper ) ? '</' . $wrapper . '>' : '';
$content .= '</div>';
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment