/** * Embed a YouTube video * * Write out XHTML to embed a YouTube video * * @since 2.0 * * @uses vye_get_parameters Extract parameters from input * @uses vye_get_embed_type Work out the correct embed type to use * @uses vye_set_autohide Set correct autohide parameter * @uses vye_generate_youtube_code Generate the YouTube code * * @param string $content YouTube video ID * @param string $paras List of parameters * @param string $style Optional CSS */ function youtube_video_embed($content, $paras = '', $style = '') { $width = vye_get_parameters($paras, 'width'); $height = vye_get_parameters($paras, 'height'); $fullscreen = vye_get_parameters($paras, 'fullscreen'); $related = vye_get_parameters($paras, 'related'); $autoplay = vye_get_parameters($paras, 'autoplay'); $loop = vye_get_parameters($paras, 'loop'); $start = vye_get_parameters($paras, 'start'); $info = vye_get_parameters($paras, 'info'); $annotation = vye_get_parameters($paras, 'annotation'); $cc = vye_get_parameters($paras, 'cc'); $link = vye_get_parameters($paras, 'link'); $react = vye_get_parameters($paras, 'react'); $stop = vye_get_parameters($paras, 'stop'); $sweetspot = vye_get_parameters($paras, 'sweetspot'); $embedplus = vye_get_parameters($paras, 'embedplus'); $disablekb = vye_get_parameters($paras, 'disablekb'); $ratio = vye_get_parameters($paras, 'ratio'); $autohide = vye_get_parameters($paras, 'autohide'); $controls = vye_get_parameters($paras, 'controls'); $type = vye_get_parameters($paras, 'type'); $profile = vye_get_parameters($paras, 'profile'); $list = vye_get_parameters($paras, 'list'); $audio = vye_get_parameters($paras, 'audio'); $template = vye_get_parameters($paras, 'template'); $hd = vye_get_parameters($paras, 'hd'); $color = vye_get_parameters($paras, 'color'); $theme = vye_get_parameters($paras, 'theme'); $https = vye_get_parameters($paras, 'ssl'); $dynamic = vye_get_parameters($paras, 'dynamic'); $search = vye_get_parameters($paras, 'search'); $user = vye_get_parameters($paras, 'user'); $vq = vye_get_parameters($paras, 'vq'); // Get Embed type $type = vye_get_embed_type($type, $embedplus); // Set up Autohide parameter $autohide = vye_set_autohide($autohide); echo vye_generate_youtube_code($content, $type, $width, $height, vye_convert($fullscreen), vye_convert($related), vye_convert($autoplay), vye_convert($loop), $start, vye_convert($info), vye_convert_3($annotation), vye_convert($cc), $style, vye_convert($link), vye_convert($react), $stop, vye_convert($sweetspot), vye_convert($disablekb), $ratio, $autohide, $controls, $profile, $list, vye_convert($audio), $template, vye_convert($hd), $color, $theme, vye_convert($https), vye_convert($dynamic), vye_convert($search), vye_convert($user), $vq); return; }
/** * Video shortcode * * Use shortcode parameters to embed a YouTube video or playlist * * @since 2.0 * * @uses vye_get_embed_type Get the embed type * @uses vye_set_autohide Get the autohide parameter * @uses vye_set_general_defaults Set default options * @uses vye_generate_youtube_code Generate the embed code * * @param string $paras Shortcode parameters * @param string $content Shortcode content * @param string $alt_shortcode The number of the alternative shortcode used * @return string YouTube embed code */ function vye_video_shortcode($paras = '', $content = '', $callback = '', $alt_shortcode = false) { extract(shortcode_atts(array('width' => '', 'height' => '', 'fullscreen' => '', 'related' => '', 'autoplay' => '', 'loop' => '', 'start' => '', 'info' => '', 'annotation' => '', 'cc' => '', 'style' => '', 'stop' => '', 'disablekb' => '', 'ratio' => '', 'autohide' => '', 'controls' => '', 'profile' => '', 'id' => '', 'url' => '', 'rel' => '', 'fs' => '', 'cc_load_policy' => '', 'iv_load_policy' => '', 'showinfo' => '', 'youtubeurl' => '', 'template' => '', 'list' => '', 'color' => '', 'theme' => '', 'ssl' => '', 'height' => '', 'width' => '', 'dynamic' => '', 'h' => '', 'w' => '', 'search' => '', 'user' => '', 'modest' => '', 'playsinline' => '', 'html5' => ''), $paras)); // If no profile specified and an alternative shortcode used, get that shortcodes default profile if ($profile == '' && $alt_shortcode) { // Get general options $options = vye_set_general_defaults(); $profile = $options['alt_profile']; } // If an alternative field is set, use it if ($id != '' && $content == '') { $content = $id; } if ($url != '' && $content == '') { $content = $url; } if ($youtubeurl != '' && $content == '') { $content = $youtubeurl; } if ($h != '' && $height == '') { $height = $h; } if ($w != '' && $width == '') { $width = $w; } if ($rel != '' && $related == '') { $related = $rel; } if ($fs != '' && $fullscreen == '') { $fullscreen = $fs; } if ($cc_load_policy != '' && $cc == '') { $cc = $cc_load_policy; } if ($iv_load_policy != '' && $annotation == '') { $annotation = $iv_load_policy; } if ($showinfo != '' && $info == '') { $info = $showinfo; } // If ID was not passed in the content and the first parameter is set, assume that to be the ID if ($content == '' && $paras[0] != '') { $content = $paras[0]; if (substr($content, 0, 1) == ":" or substr($content, 0, 1) == "=") { $content = substr($content, 1); } if (array_key_exists(1, $paras)) { if ($paras[1] != '') { $width = $paras[1]; } } if (array_key_exists(2, $paras)) { if ($paras[2] != '') { $height = $paras[2]; } } } // Set up Autohide parameter $autohide = vye_set_autohide($autohide); // Create YouTube code $youtube_code = vye_generate_youtube_code($content, $width, $height, vye_convert($fullscreen), vye_convert($related), vye_convert($autoplay), vye_convert($loop), $start, vye_convert($info), vye_convert_3($annotation), vye_convert($cc), $style, $stop, vye_convert($disablekb), $ratio, $autohide, $controls, $profile, $list, $template, $color, $theme, vye_convert($ssl), vye_convert($dynamic), vye_convert($search), vye_convert($user), vye_convert($modest), vye_convert($playsinline), vye_convert($html5)); return apply_filters('a3_lazy_load_html', do_shortcode($youtube_code)); }
/** * Generate embed code * * Generate XHTML compatible YouTube embed code * * @since 2.0 * * @uses vye_add_links Add links under video * @uses vye_error Display an error * @uses vye_extract_id Get the video ID * @uses vye_validate_list Get the requested listr * @uses vye_validate_id Validate the video ID * @uses vye_validate_profile Get the requested profile * @uses vye_set_general_defaults Get general options * @uses vye_set_profile_defaults Set default profile options * * @param string $id Video ID * @param string $width Video width * @param string $height Video height * @param string $fullscreen Fullscreen button * @param string $related Show related info. * @param string $autoplay Start video automatically * @param string $loop Loop video to start * @param string $start Start in seconds * @param string $info Show video info. * @param string $annotation Annotations * @param string $cc Closed captions * @param string $style Stylesheet information * @param string $stop Stop in seconds * @param string $disablekb Disable keyboard controls * @param string $ratio Video size ratio * @param string $autohide Autohide controls * @param string $controls Display controls * @param string $profile Which profile to use * @param string $list_style How to use a list, if used * @param string $template Display template * @param string $color Progress bar color * @param string $theme Use dark or light theme * @param string $https Use HTTPS for links * @param string $dynamic Show dynamic output * @param string $search Perform a search * @param string $user Look up user videos * @param string $modest Modest browsing * @param string $playsinline Playsinline on iOS * @param string $html5 Force HTML5 * @return string Code output */ function vye_generate_youtube_code($id = '', $width = '', $height = '', $fullscreen = '', $related = '', $autoplay = '', $loop = '', $start = '', $info = '', $annotation = '', $cc = '', $style = '', $stop = '', $disablekb = '', $ratio = '', $autohide = '', $controls = '', $profile = '', $list_style = '', $template = '', $color = '', $theme = '', $https = '', $dynamic = '', $search = '', $user = '', $modest = '', $playsinline = '', $html5 = '') { $start_time = microtime(true); // Ensure an ID is passed if ($id == '') { return vye_error(__('No video/playlist ID has been supplied', 'youtube-embed')); } $newline = "\n"; $tab = "\t"; // Get general options $general = vye_set_general_defaults(); // Find the profile, if one is specified $profile = vye_validate_profile($profile, $general['profile_no']); // Get default values if no values are supplied $options = vye_set_profile_defaults($profile); // If a user look-up or search has been requested, miss out looking up list details and // simple assign it as an IFRAME video $playlist_ids = ''; $embed_type = ''; if ($user == 0 && $search == 0) { // Check it's not a list $list = vye_validate_list($id, $general['list_no']); if (!is_array($list)) { // Check if certain parameters are included in the URL $width = vye_get_url_para($id, 'w', $width); $height = vye_get_url_para($id, 'h', $height); // Extract the ID if a full URL has been specified $id = vye_extract_id($id); // Check what type of video it is and whether it's valid $embed_type = vye_validate_id($id); // If the video is invalid, output an error if ($embed_type == '' or strlen($embed_type) != 1) { $result = $newline . '<!-- YouTube Embed v' . youtube_embed_version . ' -->' . $newline; $result .= sprintf(__('The YouTube ID of %s is invalid.', 'youtube-embed'), $id) . $newline . '<!-- End of YouTube Embed code -->' . $newline; return $result; } } else { $return = ''; $embed_type = 'v'; // Randomize the video if ($list_style == 'random') { shuffle($list); } // Extract one video randomly if ($list_style == 'single') { $id = $list[array_rand($list, 1)]; // Build the playlist } else { $id = $list[0]; // Build the playlist if (count($list) > 1) { $loop = 1; while ($loop < count($list)) { if ($playlist_ids != '') { $playlist_ids .= ','; } $list_id = vye_extract_id($list[$loop]); $playlist_ids .= $list_id; $loop++; } } } } } $metadata = $general['metadata']; // Correct the ID if a playlist if (strtolower(substr($id, 0, 2)) == 'pl') { $id = substr($id, 2); } // Work out correct protocol to use - HTTP or HTTPS if ($https == '') { $https = $options['https']; } if ($https == 1) { $https = 's'; } else { $https = ''; } // If this is a feed then display a thumbnail and/or text link to the original video if (is_feed()) { $result = ''; if ($playlist_ids != '') { $result .= '<p>' . __('A video list cannot be viewed within this feed - please view the original content', 'youtube-embed') . '.</p>' . $newline; } else { $youtube_url = 'http' . $https . '://www.youtube.com/watch?' . $embed_type . '=' . $id; if ($embed_type == 'v' && $general['feed'] != 't') { $result .= '<p><a href="' . $youtube_url . '"><img src="http://img.youtube.com/vi/' . $id . '/' . $general['thumbnail'] . '.jpg"></a></p>' . $newline; } if ($general['feed'] != 'v' or $embed_type != 'v') { $result .= '<p><a href="' . $youtube_url . '">' . __('Click here to view the video on YouTube', 'youtube-embed') . '</a>.</p>' . $newline; } } return $result; } // If a dynamic size has been requested, check whether the width should be fixed $fixed = 0; if ($dynamic == '') { $dynamic = $options['dynamic']; $fixed = $options['fixed']; } else { if ($width != '') { $fixed = 1; } } // Only set width and height from defaults if both are missing if ($width == '' && $height == '') { $width = $options['width']; $height = $options['height']; } // If controls parameter is not numeric then convert to 0 or 1 // This is to maintain backwards compatibility after version 2.6 if (!is_numeric($controls) && $controls != '') { $controls = vye_convert($controls); } // If values have not been pressed, use the default values if ($fullscreen == '') { $fullscreen = $options['fullscreen']; } if ($related == '') { $related = $options['related']; } if ($autoplay == '') { $autoplay = $options['autoplay']; } if ($loop == '') { $loop = $options['loop']; } if ($info == '') { $info = $options['info']; } if ($annotation == '') { $annotation = $options['annotation']; } if ($cc == '') { $cc = $options['cc']; } if ($disablekb == '') { $disablekb = $options['disablekb']; } if ($autohide == '') { $autohide = $options['autohide']; } if ($controls == '') { $controls = $options['controls']; } if ($style == '') { $style = $options['style']; } if ($color == '') { $color = $options['color']; } if ($theme == '') { $theme = $options['theme']; } if ($modest == '') { $modest = $options['modest']; } if ($playsinline == '') { $playsinline = $options['playsinline']; } if ($html5 == '') { $html5 = $options['html5']; } $language = $general['language']; $debug = $general['debug']; $wmode = $options['wmode']; if ($theme == '') { $theme = $options['theme']; } // Build the required template if ($template == '') { $template = $options['template']; } else { $template = vye_decode($template); } if (strpos($template, '%video%') === false) { $template = '%video%'; } // If looping and no playlist has been generated, add the current ID // This is a workaround for the AS3 player which won't otherwise loop if ($loop == 1 && $embed_type != 'p' && $playlist_ids == '') { $playlist_ids = $id; } // Set parameters without default values if ($start == '') { $start = '0'; } if ($stop == '') { $stop = '0'; } // If height or width is missing, calculate missing parameter using ratio if (($width == '' or $height == '') && ($width != '' or $height != '')) { $ratio_to_use = ''; if ($ratio != '') { // Extract the ratio from the provided string $pos = strpos($ratio, ':', 0); if ($pos !== false) { $ratio_l = substr($ratio, 0, $pos); $ratio_r = substr($ratio, $pos + 1); if (is_numeric($ratio_l) && is_numeric($ratio_r)) { $ratio_to_use = $ratio_l / $ratio_r; } } } // If no, or invalid, ratio supplied, calculate from the default video dimensions if ($ratio_to_use == '') { $ratio_to_use = $options['width'] / $options['height']; } // Complete the missing width or height using the ratio if ($width == '') { $width = round($height * $ratio_to_use, 0); } if ($height == '') { $height = round($width / $ratio_to_use, 0); } } // Set Frameborder output $frameborder = ''; $amp = '&'; if (isset($general['frameborder'])) { if ($general['frameborder'] != 1) { $frameborder = 'frameborder="0" '; } else { $amp = '&'; } } // Set up embed types $class = 'youtube-player'; $paras = ''; // Generate parameters to add to URL if ($modest == 1) { $paras .= $amp . 'modestbranding=1'; } if ($fullscreen != 1) { $paras .= $amp . 'fs=0'; } if ($related != 1) { $paras .= $amp . 'rel=0'; } if ($autoplay == 1) { $paras .= $amp . 'autoplay=1'; } if ($loop == 1) { $paras .= $amp . 'loop=1'; } if ($info != 1) { $paras .= $amp . 'showinfo=0'; } if ($annotation != 1) { $paras .= $amp . 'iv_load_policy=3'; } if ($cc != '') { $paras .= $amp . 'cc_load_policy=' . $cc; } if ($disablekb == 1) { $paras .= $amp . 'disablekb=1'; } if ($autohide != 2) { $paras .= $amp . 'autohide=' . $autohide; } if ($controls != 1) { $paras .= $amp . 'controls=' . $controls; } if (strtolower($color) != 'red') { $paras .= $amp . 'color=' . strtolower($color); } if (strtolower($theme) != 'dark') { $paras .= $amp . 'theme=' . strtolower($theme); } if ($wmode != 'window') { $paras .= $amp . 'wmode=' . $wmode; } if ($playsinline == 1) { $paras .= $amp . 'playsinline=1'; } if ($html5 == 1) { $paras .= $amp . 'html5=1'; } if ($language != '') { $paras .= $amp . 'hl=' . $language; } // If not a playlist, add the playlist parameter if ($playlist_ids != '' && $playlist_ids != $id) { $paras .= $amp . 'playlist=' . $playlist_ids; } // Add start & stop parameters if ($start != 0) { $paras .= $amp . 'start=' . $start; } if ($stop != 0) { $paras .= $amp . 'end=' . $stop; } // Generate DIVs to wrap around video $ttab = $tab; $result = '<div class="youtube-embed'; if ($dynamic == 1) { $result .= ' ye-container'; } $result .= '"'; if ($metadata != 0) { $result .= ' itemprop="video" itemscope itemtype="http://schema.org/VideoObject"'; } $result .= '>' . $newline; if ($dynamic == 1 && $fixed == 1) { $result = '<div style="width: ' . $width . 'px; max-width: 100%">' . $newline . $tab . $result; $ttab .= $tab; } // Add Metadata if ($metadata != 0) { $title = get_the_title(); $result .= $ttab . '<meta itemprop="url" content="http' . $https . '://www.youtube.com/' . $embed_type . '/' . $id . '" />' . $newline; $result .= $ttab . '<meta itemprop="name" content="' . $title . '" />' . $newline; $result .= $ttab . '<meta itemprop="description" content="' . $title . '" />' . $newline; $result .= $ttab . '<meta itemprop="uploadDate" content="' . get_the_date('c') . '" />' . $newline; $result .= $ttab . '<meta itemprop="thumbnailUrl" content="http://i.ytimg.com/vi/' . $id . '/hqdefault.jpg" />' . $newline; $result .= $ttab . '<meta itemprop="embedUrl" content="http' . $https . '://www.youtube.com/embed/' . $id . '" />' . $newline; $result .= $ttab . '<meta itemprop="height" content="' . $height . '" />' . $newline; $result .= $ttab . '<meta itemprop="width" content="' . $width . '" />' . $newline; } // Work out, depending on privacy settings, the main address to use $privacy = $general['privacy']; if ($privacy == 2) { $do_not_track = vye_do_not_track(); if ($do_not_track) { $privacy = 1; } else { $privacy = 0; } } if ($privacy == 1) { $url_privacy = 'youtube-nocookie.com'; } else { $url_privacy = 'youtube.com'; } // Generate the first part of the embed URL along with the ID section $embed_url = 'http' . $https . '://www.' . $url_privacy . '/embed'; $id_paras = '/' . $id; // If a playlist, user or download build the ID appropriately if ($embed_type == 'p' or $user != 0 or $search != 0) { $list_type = ''; if ($embed_type == 'p') { $list_type = 'playlist'; } if ($user != 0) { $list_type = 'user_uploads'; } if ($search != 0) { $list_type = 'search'; $id = urlencode($id); } $id_paras = '?listType=' . $list_type . '&list='; if ($embed_type == 'p' && strtolower(substr($id, 0, 2)) != 'pl') { $id_paras .= 'PL'; } $id_paras .= $id; } // Combine URL parts together $embed_url .= $id_paras; if (!strpos($embed_url, '?') && $paras != '') { $paras = '?' . substr($paras, 1); } $embed_url .= $paras; // Check length of URL to ensure it doesn't exceed 2000 characters if (strlen($embed_url) > 2000) { return vye_error(__('The maximum URL length has been exceeded. Please reduce your parameter and/or playlist.', 'youtube-embed')); } // Add IFRAME embed code if ($embed_type == "p") { $playlist_para = "p/"; } else { $playlist_para = ''; } $result .= $ttab . '<iframe ' . $frameborder . 'style="border: 0;' . $style . '" class="' . $class . '" width="' . $width . '" height="' . $height . '" src="' . $embed_url . '"'; if ($fullscreen == 1) { $result .= ' allowfullscreen'; } $result .= ' ></iframe>' . $newline; // Now apply the template to the result $end_tag = ''; if ($dynamic == 1 && $fixed == 1) { $end_tag .= $tab . '</div>' . $newline . '</div>' . $newline; } else { $end_tag .= '</div>' . $newline; } $result = str_replace('%video%', $result . $end_tag, $template); // Add the download link, if required if ($options['download'] == 1 && $embed_type == 'v') { $result .= '<div style="' . $options['download_style'] . '" class="aye_download">' . $newline . $tab . '<a href="' . vye_generate_download_code($id) . "\">" . $options['download_text'] . '</a>' . $newline . '</div>' . $newline; } // Now add a commented header and trailer if ($debug == 1) { $result = '<!-- YouTube Embed v' . youtube_embed_version . ' -->' . $newline . $result; $runtime = round(microtime(true) - $start_time, 5); $result .= '<!-- End of YouTube Embed code. Generated in ' . $runtime . ' seconds -->' . $newline; } return $newline . $result; }