ID, 'ct_tracks_video_key', true ); // post or post and blog? $display_value = get_post_meta( $post->ID, 'ct_tracks_video_display_key', true ); // youtube parameters $youtube_title = get_post_meta( $post->ID, 'ct_tracks_video_youtube_title', true ); $youtube_related = get_post_meta( $post->ID, 'ct_tracks_video_youtube_related', true ); $youtube_logo = get_post_meta( $post->ID, 'ct_tracks_video_youtube_logo', true ); $youtube_captions = get_post_meta( $post->ID, 'ct_tracks_video_youtube_captions', true ); // sets video to display on posts only by default if( empty( $display_value ) ) { $display_value = "post"; } // video preview echo '
'; echo ' '; if( $value ) { // output video embed echo wp_oembed_get( esc_url( $value ) ); } // add loading indicator echo '' . ct_tracks_loading_indicator_svg() . ''; echo '
'; // video URL input echo '
'; echo ' '; echo '
'; echo ''; echo ct_tracks_green_checkmark_svg(); echo '
'; echo '
'; // Display option if( $post->post_type == 'post' ) { echo '
'; echo '

' . __( 'Choose where to display the video:', 'tracks' ) . '

'; echo ' '; echo ' '; echo ' '; echo '
'; } // Youtube options // hide class for initially hiding youtube options $class = 'hide'; // if it's a youtube video, don't add the class if( strpos($value, 'youtube.com' ) || strpos($value, 'youtu.be' ) ) { $class = ''; } echo '
'; echo '

' . __( 'Youtube controls', 'tracks' ) . '

'; echo ' '; echo ' '; echo ' '; echo ' '; echo '
'; } // ajax callback to return video embed content function add_oembed_callback() { global $wpdb, $post; // $wpdb - access to the database // get the video url passed from the JS (validate user input right away) $video_url = esc_url_raw( $_POST['videoURL'] ); // if got a URL if ( $video_url ) { // output video embed $response = wp_oembed_get( esc_url( $video_url ) ); // else return nothing } else { $response = ""; } // return response echo $response; die(); // this is required to return a proper result } add_action('wp_ajax_add_oembed', 'add_oembed_callback'); /** * When the post is saved, saves our custom data. * * @param int $post_id The ID of the post being saved. */ function ct_tracks_video_save_data( $post_id ) { /* * We need to verify this came from our screen and with proper authorization, * because the save_post action can be triggered at other times. */ global $post; // Check if our nonce is set. if ( ! isset( $_POST['ct_tracks_video_nonce'] ) ) { return; } // Verify that the nonce is valid. if ( ! wp_verify_nonce( $_POST['ct_tracks_video_nonce'], 'ct_tracks_video' ) ) { return; } // If this is an autosave, our form has not been submitted, so we don't want to do anything. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // Check the user's permissions. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } /* OK, it's safe for us to save the data now. */ // Make sure video URL is set if ( isset( $_POST['ct_tracks_video_url'] ) ) { // validate user input. $video_url = esc_url_raw( $_POST['ct_tracks_video_url'] ); // Update the meta field in the database. update_post_meta( $post_id, 'ct_tracks_video_key', $video_url ); // save display option for posts only if( $post->post_type == 'post' ) { // Make sure display setting is set if ( isset( $_POST['ct_tracks_video_display'] ) ) { // get user input $display_setting = esc_attr( $_POST['ct_tracks_video_display'] ); // validate user input if ( $display_setting == 'post' || $display_setting == 'blog' || $display_setting == 'both' ) { // Saves video display option update_post_meta( $post_id, 'ct_tracks_video_display_key', $display_setting ); } } } } // Youtube title // if not set, set to '0' to avoid undefined index error if ( !isset( $_POST['ct_tracks_video_youtube_title'] ) ) { $_POST['ct_tracks_video_youtube_title'] = '0'; } // get user input $youtube_title = $_POST[ 'ct_tracks_video_youtube_title' ]; // validate user input if( $youtube_title == '1' || $youtube_title == '0' ) { // Saves video display option update_post_meta( $post_id, 'ct_tracks_video_youtube_title', $youtube_title ); } // Youtube related vids // if not set, set to '0' to avoid undefined index error if ( !isset( $_POST['ct_tracks_video_youtube_related'] ) ) { $_POST['ct_tracks_video_youtube_related'] = '0'; } // get user input $youtube_related = $_POST[ 'ct_tracks_video_youtube_related' ]; // validate user input if( $youtube_related == '1' || $youtube_related == '0' ) { // Saves video display option update_post_meta( $post_id, 'ct_tracks_video_youtube_related', $youtube_related ); } // Youtube logo // if not set, set to '0' to avoid undefined index error if ( !isset( $_POST['ct_tracks_video_youtube_logo'] ) ) { $_POST['ct_tracks_video_youtube_logo'] = '0'; } // get user input $youtube_logo = $_POST[ 'ct_tracks_video_youtube_logo' ]; // validate user input if( $youtube_logo == '1' || $youtube_logo == '0' ) { // Saves video display option update_post_meta( $post_id, 'ct_tracks_video_youtube_logo', $youtube_logo ); } // Youtube captions // if not set, set to '0' to avoid undefined index error if ( !isset( $_POST['ct_tracks_video_youtube_captions'] ) ) { $_POST['ct_tracks_video_youtube_captions'] = '0'; } // get user input $youtube_captions = $_POST[ 'ct_tracks_video_youtube_captions' ]; // validate user input if( $youtube_captions == '1' || $youtube_captions == '0' ) { // Saves video display option update_post_meta( $post_id, 'ct_tracks_video_youtube_captions', $youtube_captions ); } } add_action( 'save_post', 'ct_tracks_video_save_data' ); // front-end output function ct_tracks_pro_output_featured_video( $featured_image ){ if ( trim( get_option( 'ct_tracks_featured_videos_license_key_status' ) ) != 'valid' ) return $featured_image; // get the post object global $post; // check for a featured video $featured_video = get_post_meta( $post->ID, 'ct_tracks_video_key', true ); if( $featured_video ) { // get the display setting (post or blog) $display_blog = get_post_meta( $post->ID, 'ct_tracks_video_display_key', true ); // post and setting is post or both, or if the blog and setting is blog or both, or if a page if( ( is_singular() && ( $display_blog == 'post' || $display_blog == 'both' ) ) || ( ( is_home() || is_archive() || is_search() ) && ( $display_blog == 'blog' || $display_blog == 'both' ) ) || is_singular('page') ) { $featured_image = ''; } } return $featured_image; } add_filter( 'ct_tracks_featured_image', 'ct_tracks_pro_output_featured_video', 20 ); // Filter video output function ct_tracks_add_youtube_parameters($html, $url, $args) { // access post object global $post; // get featured video if( ! empty( $post ) ) $featured_video = get_post_meta( $post->ID, 'ct_tracks_video_key', true ); // only run filter if there is a featured video if( ! empty( $featured_video ) ) { // only run filter on the featured video if( $url == $featured_video ) { // only add parameters if featured vid is a youtube vid if( strpos($featured_video, 'youtube.com' ) || strpos($featured_video, 'youtu.be' ) ) { // get user Youtube parameter settings // flip their value so 1 means, yes HIDE it, NOT yes SHOW it. $youtube_title = get_post_meta( $post->ID, 'ct_tracks_video_youtube_title', true ) ? 0 : 1; $youtube_related = get_post_meta( $post->ID, 'ct_tracks_video_youtube_related', true ) ? 0 : 1; $youtube_logo = get_post_meta( $post->ID, 'ct_tracks_video_youtube_logo', true ) ? 0 : 1; $youtube_captions = get_post_meta( $post->ID, 'ct_tracks_video_youtube_captions', true ); $youtube_parameters = array( 'showinfo' => $youtube_title, 'rel' => $youtube_related, 'modestbranding' => $youtube_logo, 'cc_load_policy' => $youtube_captions ); if ( is_array( $args ) ) { $args = array_merge( $args, $youtube_parameters ); } else { $args = $youtube_parameters; } $parameters = http_build_query( $args ); // Modify video parameters $html = str_replace( '?feature=oembed', '?feature=oembed&' . $parameters, $html ); } } } return $html; } add_filter('oembed_result','ct_tracks_add_youtube_parameters', 10, 3);