' . __( '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 = '