/** * @param $url Can be just the $url or the whole $atts array * @return bool|mixed The Youtube video ID */ function jetpack_get_youtube_id($url) { // Do we have an $atts array? Get first att if (is_array($url)) { $url = $url[0]; } $url = youtube_sanitize_url($url); $url = parse_url($url); $id = false; if (!isset($url['query'])) { return false; } parse_str($url['query'], $qargs); if (!isset($qargs['v']) && !isset($qargs['list'])) { return false; } if (isset($qargs['list'])) { $id = preg_replace('|[^_a-z0-9-]|i', '', $qargs['list']); } if (empty($id)) { $id = preg_replace('|[^_a-z0-9-]|i', '', $qargs['v']); } return $id; }
/** * Converts a YouTube URL into an embedded YouTube video. */ function youtube_id($url) { if (!($id = jetpack_get_youtube_id($url))) { return '<!--YouTube Error: bad URL entered-->'; } $url = youtube_sanitize_url($url); $url = parse_url($url); if (!isset($url['query'])) { return false; } if (isset($url['fragment'])) { wp_parse_str($url['fragment'], $fargs); } else { $fargs = array(); } wp_parse_str($url['query'], $qargs); $qargs = array_merge($fargs, $qargs); // calculate the width and height, taking content_width into consideration global $content_width; $input_w = isset($qargs['w']) && intval($qargs['w']) ? intval($qargs['w']) : 0; $input_h = isset($qargs['h']) && intval($qargs['h']) ? intval($qargs['h']) : 0; $default_width = get_option('embed_size_w'); if (empty($default_width)) { if (!empty($content_width)) { $default_width = $content_width; } else { $default_width = 640; } } if ($input_w > 0 && $input_h > 0) { $w = $input_w; $h = $input_h; } elseif (0 == $input_w && 0 == $input_h) { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; $h = ceil($w / 16 * 9) + 30; } } elseif ($input_w > 0) { $w = $input_w; $h = ceil($w / 16 * 9) + 30; } else { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; $h = $input_h; } } /** * Filter the YouTube player width. * * @module shortcodes * * @since 1.1.0 * * @param int $w Width of the YouTube player in pixels. */ $w = (int) apply_filters('youtube_width', $w); /** * Filter the YouTube player height. * * @module shortcodes * * @since 1.1.0 * * @param int $h Height of the YouTube player in pixels. */ $h = (int) apply_filters('youtube_height', $h); $rel = isset($qargs['rel']) && 0 == $qargs['rel'] ? 0 : 1; $search = isset($qargs['showsearch']) && 1 == $qargs['showsearch'] ? 1 : 0; $info = isset($qargs['showinfo']) && 0 == $qargs['showinfo'] ? 0 : 1; $iv = isset($qargs['iv_load_policy']) && 3 == $qargs['iv_load_policy'] ? 3 : 1; $fmt = isset($qargs['fmt']) && intval($qargs['fmt']) ? '&fmt=' . (int) $qargs['fmt'] : ''; if (!isset($qargs['autohide']) || ($qargs['autohide'] < 0 || 2 < $qargs['autohide'])) { $autohide = '&autohide=2'; } else { $autohide = '&autohide=' . absint($qargs['autohide']); } $start = 0; if (isset($qargs['start'])) { $start = intval($qargs['start']); } else { if (isset($qargs['t'])) { $time_pieces = preg_split('/(?<=\\D)(?=\\d+)/', $qargs['t']); foreach ($time_pieces as $time_piece) { $int = (int) $time_piece; switch (substr($time_piece, -1)) { case 'h': $start += $int * 3600; break; case 'm': $start += $int * 60; break; case 's': $start += $int; break; } } } } $start = $start ? '&start=' . $start : ''; $end = isset($qargs['end']) && intval($qargs['end']) ? '&end=' . (int) $qargs['end'] : ''; $hd = isset($qargs['hd']) && intval($qargs['hd']) ? '&hd=' . (int) $qargs['hd'] : ''; $vq = isset($qargs['vq']) && in_array($qargs['vq'], array('hd720', 'hd1080')) ? '&vq=' . $qargs['vq'] : ''; $cc = isset($qargs['cc_load_policy']) ? '&cc_load_policy=1' : ''; $cc_lang = isset($qargs['cc_lang_pref']) ? '&cc_lang_pref=' . preg_replace('/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref']) : ''; $wmode = isset($qargs['wmode']) && in_array(strtolower($qargs['wmode']), array('opaque', 'window', 'transparent')) ? $qargs['wmode'] : 'transparent'; $theme = isset($qargs['theme']) && in_array(strtolower($qargs['theme']), array('dark', 'light')) ? '&theme=' . $qargs['theme'] : ''; $autoplay = ''; /** * Allow YouTube videos to start playing automatically. * * @module shortcodes * * @since 2.2.2 * * @param bool false Enable autoplay for YouTube videos. */ if (apply_filters('jetpack_youtube_allow_autoplay', false) && isset($qargs['autoplay'])) { $autoplay = '&autoplay=' . (int) $qargs['autoplay']; } $alignmentcss = 'text-align:center;'; if (isset($qargs['align'])) { switch ($qargs['align']) { case 'left': $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;"; break; case 'right': $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;"; break; } } if (isset($url['path']) && '/videoseries' == $url['path'] || isset($qargs['list'])) { $html = "<span class='embed-youtube' style='{$alignmentcss} display: block;'><iframe class='youtube-player' type='text/html' width='{$w}' height='{$h}' src='" . esc_url(set_url_scheme("http://www.youtube.com/embed/videoseries?list={$id}&hl=en_US")) . "' frameborder='0' allowfullscreen='true'></iframe></span>"; } else { $html = "<span class='embed-youtube' style='{$alignmentcss} display: block;'><iframe class='youtube-player' type='text/html' width='{$w}' height='{$h}' src='" . esc_url(set_url_scheme("http://www.youtube.com/embed/{$id}?version=3&rel={$rel}&fs=1{$fmt}{$autohide}&showsearch={$search}&showinfo={$info}&iv_load_policy={$iv}{$start}{$end}{$hd}&wmode={$wmode}{$theme}{$autoplay}{$cc}{$cc_lang}")) . "' frameborder='0' allowfullscreen='true'></iframe></span>"; } /** * Filter the YouTube video HTML output. * * @module shortcodes * * @since 1.2.3 * * @param string $html YouTube video HTML output. */ $html = apply_filters('video_embed_html', $html); return $html; }
/** * Converts a YouTube URL into an embedded YouTube video. */ function youtube_id($url) { if (apply_filters('jetpack_bail_on_shortcode', false, 'youtube')) { return ''; } if (!($id = get_youtube_id($url))) { return '<!--YouTube Error: bad URL entered-->'; } $url = youtube_sanitize_url($url); $url = parse_url($url); if (!isset($url['query'])) { return false; } parse_str($url['query'], $qargs); $agent = $_SERVER['HTTP_USER_AGENT']; // Bloglines & Google Reader handle YouTube well now, instead of // big blank space of yester year, so they can skip this treatment if (is_feed() && !preg_match('#' . apply_filters('jetpack_shortcode_youtube_whitelist_user_agents', 'Bloglines|FeedFetcher-Google|feedburner') . '#i', $agent)) { return '<span style="text-align:center; display: block;"><a href="' . get_permalink() . '"><img src="http://img.youtube.com/vi/' . $id . '/2.jpg" alt="" /></a></span>'; } // calculate the width and height, taken content_width into consideration global $content_width; $input_w = isset($qargs['w']) && intval($qargs['w']) ? intval($qargs['w']) : 0; $input_h = isset($qargs['h']) && intval($qargs['h']) ? intval($qargs['h']) : 0; $default_width = 640; if ($input_w > 0 && $input_h > 0) { $w = $input_w; $h = $input_h; } elseif (0 == $input_w && 0 == $input_h) { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; } $h = ceil($w / 16 * 9) + 30; } elseif ($input_w > 0) { $w = $input_w; $h = ceil($w / 16 * 9) + 30; } else { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; } $h = $input_h; } $w = (int) apply_filters('youtube_width', $w); $h = (int) apply_filters('youtube_height', $h); $fmt = ''; if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $fmt = '&fmt=' . (int) $qargs['fmt']; } if (isset($qargs['rel']) && 0 == $qargs['rel']) { $rel = 0; } else { $rel = 1; } if (isset($qargs['showsearch']) && 1 == $qargs['showsearch']) { $search = 1; } else { $search = 0; } if (isset($qargs['showinfo']) && 0 == $qargs['showinfo']) { $info = 0; } else { $info = 1; } if (isset($qargs['iv_load_policy']) && 3 == $qargs['iv_load_policy']) { $iv = 3; } else { $iv = 1; } $start = ''; if (isset($qargs['start']) && intval($qargs['start'])) { $start = '&start=' . (int) $qargs['start']; } $hd = ''; if (isset($qargs['hd']) && intval($qargs['hd'])) { $hd = '&hd=' . (int) $qargs['hd']; } $alignmentcss = 'text-align:center;'; if (isset($qargs['align'])) { switch ($qargs['align']) { case 'left': $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;"; break; case 'right': $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;"; break; } } if (isset($qargs['wmode']) && in_array(strtolower($qargs['wmode']), array('opaque', 'window', 'transparent'))) { $wmode = $qargs['wmode']; } else { $wmode = 'transparent'; } $html = "<span class='embed-youtube' style='{$alignmentcss} display: block;'><iframe class='youtube-player' type='text/html' width='{$w}' height='{$h}' src='" . esc_attr("http://www.youtube.com/embed/{$id}?version=3&rel={$rel}&fs=1{$fmt}&showsearch={$search}&showinfo={$info}&iv_load_policy={$iv}{$start}{$hd}&wmode={$wmode}") . "' frameborder='0'></iframe></span>"; $html = apply_filters('video_embed_html', $html); return $html; }
/** * Converts a YouTube URL into an embedded YouTube video. */ function youtube_id($url) { if (apply_filters('jetpack_bail_on_shortcode', false, 'youtube')) { return ''; } if (!($id = get_youtube_id($url))) { return '<!--YouTube Error: bad URL entered-->'; } $url = youtube_sanitize_url($url); $url = parse_url($url); if (!isset($url['query'])) { return false; } parse_str($url['query'], $qargs); // calculate the width and height, taking content_width into consideration global $content_width; $input_w = isset($qargs['w']) && intval($qargs['w']) ? intval($qargs['w']) : 0; $input_h = isset($qargs['h']) && intval($qargs['h']) ? intval($qargs['h']) : 0; $default_width = get_option('embed_size_w'); if (empty($default_width)) { if (!empty($content_width)) { $default_width = $content_width; } else { $default_width = 640; } } if ($input_w > 0 && $input_h > 0) { $w = $input_w; $h = $input_h; } elseif (0 == $input_w && 0 == $input_h) { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; $h = ceil($w / 16 * 9) + 30; } } elseif ($input_w > 0) { $w = $input_w; $h = ceil($w / 16 * 9) + 30; } else { if (isset($qargs['fmt']) && intval($qargs['fmt'])) { $w = !empty($content_width) ? min($content_width, 480) : 480; } else { $w = !empty($content_width) ? min($content_width, $default_width) : $default_width; $h = $input_h; } } $w = (int) apply_filters('youtube_width', $w); $h = (int) apply_filters('youtube_height', $h); $rel = isset($qargs['rel']) && 0 == $qargs['rel'] ? 0 : 1; $search = isset($qargs['showsearch']) && 1 == $qargs['showsearch'] ? 1 : 0; $info = isset($qargs['showinfo']) && 0 == $qargs['showinfo'] ? 0 : 1; $iv = isset($qargs['iv_load_policy']) && 3 == $qargs['iv_load_policy'] ? 3 : 1; $fmt = isset($qargs['fmt']) && intval($qargs['fmt']) ? '&fmt=' . (int) $qargs['fmt'] : ''; $start = isset($qargs['start']) && intval($qargs['start']) ? '&start=' . (int) $qargs['start'] : ''; $hd = isset($qargs['hd']) && intval($qargs['hd']) ? '&hd=' . (int) $qargs['hd'] : ''; $wmode = isset($qargs['wmode']) && in_array(strtolower($qargs['wmode']), array('opaque', 'window', 'transparent')) ? $qargs['wmode'] : 'transparent'; $alignmentcss = 'text-align:center;'; if (isset($qargs['align'])) { switch ($qargs['align']) { case 'left': $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;"; break; case 'right': $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;"; break; } } if (isset($url['path']) && '/videoseries' == $url['path'] || isset($qargs['list'])) { $html = "<span class='embed-youtube' style='{$alignmentcss} display: block;'><iframe class='youtube-player' type='text/html' width='{$w}' height='{$h}' src='" . esc_url("http://www.youtube.com/embed/videoseries?list={$id}&hl=en_US") . "' frameborder='0'></iframe></span>"; } else { $html = "<span class='embed-youtube' style='{$alignmentcss} display: block;'><iframe class='youtube-player' type='text/html' width='{$w}' height='{$h}' src='" . esc_url("http://www.youtube.com/embed/{$id}?version=3&rel={$rel}&fs=1{$fmt}&showsearch={$search}&showinfo={$info}&iv_load_policy={$iv}{$start}{$hd}&wmode={$wmode}") . "' frameborder='0'></iframe></span>"; } $html = apply_filters('video_embed_html', $html); return $html; }
/** * @author enkrates * @covers ::youtube_sanitize_url * @since 3.2 */ public function test_youtube_sanitize_url_with_extra_question_mark() { $extra_question_mark_url = 'http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US'; $expected_sanitized_url = 'http://www.youtube.com/?v=9FhMMmqzbD8&fs=1&hl=en_US'; $sanitized_url = youtube_sanitize_url($extra_question_mark_url); $this->assertEquals($expected_sanitized_url, $sanitized_url); }