Nessuna descrizione

youtube.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?php
  2. /**
  3. * Youtube shortcode
  4. *
  5. * Contains shortcode + some improvements over the Core Embeds syntax (see http://codex.wordpress.org/Embeds )
  6. *
  7. * Examples:
  8. * [youtube https://www.youtube.com/watch?v=WVbQ-oro7FQ]
  9. * [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&fs=1&hl=bg_BG&autohide=1&rel=0]
  10. * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
  11. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  12. * https://www.youtube.com/playlist?list=PLP7HaNDU4Cifov7C2fQM8Ij6Ew_uPHEXW
  13. *
  14. * @package automattic/jetpack
  15. */
  16. /**
  17. * Replaces YouTube embeds with YouTube shortcodes.
  18. *
  19. * Covers the following formats:
  20. * 2008-07-15:
  21. * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/bZBHZT3a-FA&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/bZBHZT3a-FA&hl=en&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>
  22. * around 2008-06-06 youtube changed their old embed code to this:
  23. * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/M1D30gS7Z8U&hl=en"></param><embed src="http://www.youtube.com/v/M1D30gS7Z8U&hl=en" type="application/x-shockwave-flash" width="425" height="344"></embed></object>
  24. * old style was:
  25. * <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/dGY28Qbj76A&rel=0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/dGY28Qbj76A&rel=0" type="application/x-shockwave-flash" wmode="transparent" width="425" height="344"></embed></object>
  26. * 12-2010:
  27. * <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/3H8bnKdf654?fs=1&amp;hl=en_GB"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/3H8bnKdf654?fs=1&amp;hl=en_GB" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
  28. * 01-2011:
  29. * <iframe title="YouTube video player" class="youtube-player" width="640" height="390" src="http://www.youtube.com/embed/Qq9El3ki0_g" frameborder="0" allowFullScreen></iframe>
  30. * <iframe class="youtube-player" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe>
  31. *
  32. * @param string $content HTML content.
  33. * @return string The content with YouTube embeds replaced with YouTube shortcodes.
  34. */
  35. function youtube_embed_to_short_code( $content ) {
  36. if ( ! is_string( $content ) || false === strpos( $content, 'youtube.com' ) ) {
  37. return $content;
  38. }
  39. // older codes.
  40. $regexp = '!<object(.*?)>.*?<param\s+name=[\'"]movie[\'"]\s+value=[\'"](https?:)?//www\.youtube\.com/v/([^\'"]+)[\'"].*?>.*?</object>!i';
  41. $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES );
  42. $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|&#0*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!';
  43. $old_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) );
  44. // new code.
  45. $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i';
  46. $ifr_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) );
  47. foreach ( compact( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg => $regexp ) {
  48. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  49. continue;
  50. }
  51. foreach ( $matches as $match ) {
  52. /*
  53. * Hack, but '?' should only ever appear once, and
  54. * it should be for the 1st field-value pair in query string,
  55. * if it is present
  56. * YouTube changed their embed code.
  57. * Example of how it is now:
  58. * <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>
  59. * As shown at the start of function, previous YouTube didn't '?'
  60. * the 1st field-value pair.
  61. */
  62. if ( in_array( $reg, array( 'ifr_regexp', 'ifr_regexp_ent', 'regexp', 'regexp_ent' ), true ) ) {
  63. $params = $match[1];
  64. if ( in_array( $reg, array( 'ifr_regexp_ent', 'regexp_ent' ), true ) ) {
  65. $params = html_entity_decode( $params );
  66. }
  67. $params = wp_kses_hair( $params, array( 'http' ) );
  68. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  69. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  70. $wh = '';
  71. if ( $width && $height ) {
  72. $wh = "&w=$width&h=$height";
  73. }
  74. $url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" );
  75. } else {
  76. $match[1] = str_replace( '?', '&', $match[1] );
  77. $url = esc_url_raw( 'https://www.youtube.com/watch?v=' . html_entity_decode( $match[1] ) );
  78. }
  79. $content = str_replace( $match[0], "[youtube $url]", $content );
  80. /**
  81. * Fires before the YouTube embed is transformed into a shortcode.
  82. *
  83. * @module shortcodes
  84. *
  85. * @since 1.2.0
  86. *
  87. * @param string youtube Shortcode name.
  88. * @param string $url YouTube video URL.
  89. */
  90. do_action( 'jetpack_embed_to_shortcode', 'youtube', $url );
  91. }
  92. }
  93. return $content;
  94. }
  95. add_filter( 'pre_kses', 'youtube_embed_to_short_code' );
  96. /**
  97. * Replaces plain-text links to YouTube videos with YouTube embeds.
  98. *
  99. * @param string $content HTML content.
  100. *
  101. * @return string The content with embeds instead of URLs
  102. */
  103. function youtube_link( $content ) {
  104. return jetpack_preg_replace_callback_outside_tags( '!(?:\n|\A)https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/)[^\s]+?(?:\n|\Z)!i', 'youtube_link_callback', $content, 'youtube.com/' );
  105. }
  106. /**
  107. * Callback function for the regex that replaces YouTube URLs with
  108. * YouTube embeds.
  109. *
  110. * @param array $matches An array containing a YouTube URL.
  111. */
  112. function youtube_link_callback( $matches ) {
  113. return "\n" . youtube_id( $matches[0] ) . "\n";
  114. }
  115. /**
  116. * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
  117. *
  118. * @param string $url
  119. * @return string The normalized URL
  120. */
  121. if ( ! function_exists( 'youtube_sanitize_url' ) ) :
  122. /**
  123. * Clean up Youtube URL to match a single format.
  124. *
  125. * @param string $url Youtube URL.
  126. */
  127. function youtube_sanitize_url( $url ) {
  128. $url = trim( $url, ' "' );
  129. $url = trim( $url );
  130. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
  131. // Replace any extra question marks with ampersands - the result of a URL like "http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US" being passed in.
  132. $query_string_start = strpos( $url, '?' );
  133. if ( false !== $query_string_start ) {
  134. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( '?', '&', substr( $url, $query_string_start + 1 ) );
  135. }
  136. return $url;
  137. }
  138. endif;
  139. /**
  140. * Converts a YouTube URL into an embedded YouTube video.
  141. *
  142. * URL can be:
  143. * http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
  144. * http://www.youtube.com/watch#!v=H2Ncxw1xfck
  145. * http://www.youtube.com/watch?v=H2Ncxw1xfck
  146. * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
  147. * http://www.youtube.com/v/jF-kELmmvgA
  148. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  149. * http://youtu.be/Rrohlqeir5E
  150. * https://www.youtube.com/watch?v=GJNxoe-iSb4&list=PLAVZ4NFtZX0fE54mDSqNKym-o_rz-8xmk
  151. *
  152. * @param string $url Youtube URL.
  153. */
  154. function youtube_id( $url ) {
  155. $id = jetpack_get_youtube_id( $url );
  156. if ( ! $id ) {
  157. return sprintf( '<!--%s-->', esc_html__( 'YouTube Error: bad URL entered', 'jetpack' ) );
  158. }
  159. $url = youtube_sanitize_url( $url );
  160. $url = wp_parse_url( $url );
  161. $thumbnail = "https://i.ytimg.com/vi/$id/hqdefault.jpg";
  162. $video_url = add_query_arg( 'v', $id, 'https://www.youtube.com/watch' );
  163. $args = jetpack_shortcode_youtube_args( $url );
  164. if ( empty( $args ) ) {
  165. return sprintf( '<!--%s-->', esc_html__( 'YouTube Error: empty URL args', 'jetpack' ) );
  166. }
  167. // Account for URL having both v and list, where jetpack_get_youtube_id() only accounts for one or the other.
  168. if ( isset( $args['list'] ) && $args['list'] === $id ) {
  169. $id = null;
  170. }
  171. if ( isset( $args['v'] ) ) {
  172. $id = $args['v'];
  173. }
  174. if ( ! $id && empty( $args['list'] ) ) {
  175. return sprintf( '<!--%s-->', esc_html__( 'YouTube Error: missing id and/or list', 'jetpack' ) );
  176. }
  177. list( $w, $h ) = jetpack_shortcode_youtube_dimensions( $args );
  178. $params = array(
  179. 'rel' => ( isset( $args['rel'] ) && '0' === $args['rel'] ) ? 0 : 1,
  180. 'showsearch' => ( isset( $args['showsearch'] ) && '1' === $args['showsearch'] ) ? 1 : 0, // Now deprecated. See https://developers.google.com/youtube/player_parameters#march-29,-2012.
  181. 'showinfo' => ( isset( $args['showinfo'] ) && '0' === $args['showinfo'] ) ? 0 : 1, // Now obsolete. See https://developers.google.com/youtube/player_parameters#showinfo.
  182. 'iv_load_policy' => ( isset( $args['iv_load_policy'] ) && '3' === $args['iv_load_policy'] ) ? 3 : 1,
  183. 'fs' => 1,
  184. 'hl' => str_replace( '_', '-', get_locale() ),
  185. );
  186. if ( isset( $args['fmt'] ) && (int) $args['fmt'] ) {
  187. $params['fmt'] = (int) $args['fmt']; // Apparently an obsolete parameter. Not referenced on https://developers.google.com/youtube/player_parameters.
  188. }
  189. // The autohide parameter has been deprecated since 2015. See https://developers.google.com/youtube/player_parameters#august-19,-2015.
  190. if ( ! isset( $args['autohide'] ) || ( $args['autohide'] < 0 || 2 < $args['autohide'] ) ) {
  191. $params['autohide'] = 2;
  192. } else {
  193. $params['autohide'] = (int) $args['autohide'];
  194. }
  195. $start = 0;
  196. if ( isset( $args['start'] ) ) {
  197. $start = (int) $args['start'];
  198. } elseif ( isset( $args['t'] ) ) {
  199. if ( is_numeric( $args['t'] ) ) {
  200. $start = (int) $args['t'];
  201. } else {
  202. $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $args['t'] );
  203. foreach ( $time_pieces as $time_piece ) {
  204. $int = (int) $time_piece;
  205. switch ( substr( $time_piece, - 1 ) ) {
  206. case 'h':
  207. $start += $int * 3600;
  208. break;
  209. case 'm':
  210. $start += $int * 60;
  211. break;
  212. case 's':
  213. $start += $int;
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. if ( $start ) {
  220. $params['start'] = (int) $start;
  221. }
  222. if ( isset( $args['end'] ) && (int) $args['end'] ) {
  223. $params['end'] = (int) $args['end'];
  224. }
  225. if ( isset( $args['hd'] ) && (int) $args['hd'] ) {
  226. $params['hd'] = (int) $args['hd']; // Now obsolete per https://developers.google.com/youtube/player_parameters#march-29,-2012.
  227. }
  228. if ( isset( $args['vq'] ) && in_array( $args['vq'], array( 'hd720', 'hd1080' ), true ) ) {
  229. $params['vq'] = $args['vq']; // Note, this appears to be obsolete. Not referenced on https://developers.google.com/youtube/player_parameters.
  230. }
  231. if ( isset( $args['cc_load_policy'] ) ) {
  232. $params['cc_load_policy'] = 1;
  233. }
  234. if ( isset( $args['cc_lang_pref'] ) ) {
  235. $params['cc_lang_pref'] = preg_replace( '/[^_a-z0-9-]/i', '', $args['cc_lang_pref'] );
  236. }
  237. // The wmode parameter appears to be obsolete. Not referenced on https://developers.google.com/youtube/player_parameters.
  238. if ( isset( $args['wmode'] ) && in_array( strtolower( $args['wmode'] ), array( 'opaque', 'window', 'transparent' ), true ) ) {
  239. $params['wmode'] = $args['wmode'];
  240. } else {
  241. $params['wmode'] = 'transparent';
  242. }
  243. // The theme parameter is obsolete per https://developers.google.com/youtube/player_parameters#august-19,-2015.
  244. if ( isset( $args['theme'] ) && in_array( strtolower( $args['theme'] ), array( 'dark', 'light' ), true ) ) {
  245. $params['theme'] = $args['theme'];
  246. }
  247. if ( isset( $args['list'] ) ) {
  248. $params['listType'] = 'playlist';
  249. $params['list'] = preg_replace( '|[^_a-z0-9-]|i', '', $args['list'] );
  250. }
  251. /**
  252. * Allow YouTube videos to start playing automatically.
  253. *
  254. * @module shortcodes
  255. *
  256. * @since 2.2.2
  257. *
  258. * @param bool false Enable autoplay for YouTube videos.
  259. */
  260. if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $args['autoplay'] ) ) {
  261. $params['autoplay'] = (int) $args['autoplay'];
  262. }
  263. $is_amp = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
  264. if ( $is_amp && $id ) {
  265. // Note that <amp-youtube> currently is not well suited for playlists that don't have an individual video selected, hence the $id check above.
  266. $placeholder = sprintf(
  267. '<a href="%1$s" placeholder><amp-img src="%2$s" alt="%3$s" layout="fill" object-fit="cover"><noscript><img src="%2$s" loading="lazy" decoding="async" alt="%3$s"></noscript></amp-img></a>',
  268. esc_url( $video_url ),
  269. esc_url( $thumbnail ),
  270. esc_attr__( 'YouTube Poster', 'jetpack' ) // Would be preferable to provide YouTube video title, but not available in this non-oEmbed context.
  271. );
  272. $html_attributes = array();
  273. foreach ( $params as $param_name => $param_value ) {
  274. $html_attributes[] = sprintf(
  275. 'data-param-%s="%s"',
  276. sanitize_key( $param_name ),
  277. esc_attr( $param_value )
  278. );
  279. }
  280. $html = sprintf(
  281. '<amp-youtube data-videoid="%s" %s width="%d" height="%d" layout="responsive">%s</amp-youtube>',
  282. esc_attr( $id ),
  283. implode( ' ', $html_attributes ), // Note: Escaping done above.
  284. esc_attr( $w ),
  285. esc_attr( $h ),
  286. $placeholder
  287. );
  288. } else {
  289. // In AMP, the AMP_Iframe_Sanitizer will convert into <amp-iframe> as required.
  290. $src = 'https://www.youtube.com/embed';
  291. if ( $id ) {
  292. $src .= "/$id";
  293. }
  294. $src = add_query_arg(
  295. array_merge(
  296. array( 'version' => 3 ),
  297. $params
  298. ),
  299. $src
  300. );
  301. $layout = $is_amp ? 'layout="responsive" ' : '';
  302. $html = sprintf(
  303. '<iframe class="youtube-player" width="%s" height="%s" %ssrc="%s" allowfullscreen="true" style="border:0;" sandbox="allow-scripts allow-same-origin allow-popups allow-presentation"></iframe>',
  304. esc_attr( $w ),
  305. esc_attr( $h ),
  306. $layout,
  307. esc_url( $src )
  308. );
  309. }
  310. // Let's do some alignment wonder in a span, unless we're producing a feed.
  311. if ( ! is_feed() ) {
  312. $alignmentcss = 'text-align:center;';
  313. if ( isset( $args['align'] ) ) {
  314. switch ( $args['align'] ) {
  315. case 'left':
  316. $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
  317. break;
  318. case 'right':
  319. $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
  320. break;
  321. }
  322. }
  323. $html = sprintf(
  324. '<span class="embed-youtube" style="%s display: block;">%s</span>',
  325. esc_attr( $alignmentcss ),
  326. $html
  327. );
  328. }
  329. /**
  330. * Format output for Calypso Reader/Notifications/Comments
  331. */
  332. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  333. require_once WP_CONTENT_DIR . '/lib/display-context.php';
  334. $context = A8C\Display_Context\get_current_context();
  335. if ( A8C\Display_Context\NOTIFICATIONS === $context ) {
  336. return sprintf(
  337. '<a href="%1$s" target="_blank" rel="noopener noreferrer"><img src="%2$s" alt="%3$s" /></a>',
  338. esc_url( $video_url ),
  339. esc_url( $thumbnail ),
  340. esc_html__( 'YouTube video', 'jetpack' )
  341. );
  342. }
  343. }
  344. /**
  345. * Filter the YouTube video HTML output.
  346. *
  347. * @module shortcodes
  348. *
  349. * @since 1.2.3
  350. *
  351. * @param string $html YouTube video HTML output.
  352. */
  353. $html = apply_filters( 'video_embed_html', $html );
  354. return $html;
  355. }
  356. /**
  357. * Gets the args present in the YouTube shortcode URL.
  358. *
  359. * @since 8.0.0
  360. *
  361. * @param array $url The parsed URL of the shortcode.
  362. *
  363. * @return array|false The query args of the URL, or false.
  364. */
  365. function jetpack_shortcode_youtube_args( $url ) {
  366. $qargs = array();
  367. if ( ! empty( $url['query'] ) ) {
  368. wp_parse_str( $url['query'], $qargs );
  369. } else {
  370. return false;
  371. }
  372. $fargs = array();
  373. if ( ! empty( $url['fragment'] ) ) {
  374. wp_parse_str( $url['fragment'], $fargs );
  375. }
  376. return array_merge( $fargs, $qargs );
  377. }
  378. /**
  379. * Display the Youtube shortcode.
  380. *
  381. * @param array $atts Shortcode attributes.
  382. *
  383. * @return string The rendered shortcode.
  384. */
  385. function youtube_shortcode( $atts ) {
  386. $url = ( isset( $atts[0] ) ) ? ltrim( $atts[0], '=' ) : shortcode_new_to_old_params( $atts );
  387. return youtube_id( $url );
  388. }
  389. add_shortcode( 'youtube', 'youtube_shortcode' );
  390. /**
  391. * Renders the [youtube] shortcode as an AMP component.
  392. *
  393. * @since 8.0.0
  394. * @deprecated Use youtube_id() instead.
  395. *
  396. * @param string $url The YouTube URL.
  397. *
  398. * @return string The AMP-compatible rendered shortcode.
  399. */
  400. function jetpack_amp_youtube_shortcode( $url ) {
  401. _deprecated_function( __FUNCTION__, 'jetpack-9.1.0', 'youtube_id' );
  402. $video_id = jetpack_get_youtube_id( $url );
  403. if ( empty( $video_id ) ) {
  404. return sprintf(
  405. '<a href="%1$s" class="amp-wp-embed-fallback">%1$s</a>',
  406. esc_url( $url )
  407. );
  408. }
  409. $sanitized_url = youtube_sanitize_url( $url );
  410. $parsed_url = wp_parse_url( $sanitized_url );
  411. $args = jetpack_shortcode_youtube_args( $parsed_url );
  412. list( $width, $height ) = jetpack_shortcode_youtube_dimensions( $args );
  413. return sprintf(
  414. '<amp-youtube data-videoid="%s" layout="responsive" width="%d" height="%d"></amp-youtube>',
  415. esc_attr( $video_id ),
  416. absint( $width ),
  417. absint( $height )
  418. );
  419. }
  420. /**
  421. * Gets the dimensions of the [youtube] shortcode.
  422. *
  423. * Calculates the width and height, taking $content_width into consideration.
  424. *
  425. * @since 8.0.0
  426. *
  427. * @param array $query_args The query args of the URL.
  428. *
  429. * @return array The width and height of the shortcode.
  430. */
  431. function jetpack_shortcode_youtube_dimensions( $query_args ) {
  432. global $content_width;
  433. $input_w = ( isset( $query_args['w'] ) && (int) $query_args['w'] ) ? (int) $query_args['w'] : 0;
  434. $input_h = ( isset( $query_args['h'] ) && (int) $query_args['h'] ) ? (int) $query_args['h'] : 0;
  435. // If we have $content_width, use it.
  436. if ( ! empty( $content_width ) ) {
  437. $default_width = $content_width;
  438. } else {
  439. // Otherwise get default width from the old, now deprecated embed_size_w option.
  440. $default_width = get_option( 'embed_size_w' );
  441. }
  442. // If we don't know those 2 values use a hardcoded width.
  443. if ( empty( $default_width ) ) {
  444. $default_width = 640;
  445. }
  446. if ( $input_w > 0 && $input_h > 0 ) {
  447. $w = $input_w;
  448. $h = $input_h;
  449. } elseif ( 0 === $input_w && 0 === $input_h ) {
  450. if ( isset( $query_args['fmt'] ) && (int) $query_args['fmt'] ) {
  451. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
  452. } else {
  453. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
  454. $h = ceil( ( $w / 16 ) * 9 );
  455. }
  456. } elseif ( $input_w > 0 ) {
  457. $w = $input_w;
  458. $h = ceil( ( $w / 16 ) * 9 );
  459. } else {
  460. if ( isset( $query_args['fmt'] ) && (int) $query_args['fmt'] ) {
  461. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
  462. } else {
  463. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
  464. $h = $input_h;
  465. }
  466. }
  467. /**
  468. * Filter the YouTube player width.
  469. *
  470. * @module shortcodes
  471. *
  472. * @since 1.1.0
  473. *
  474. * @param int $w Width of the YouTube player in pixels.
  475. */
  476. $w = (int) apply_filters( 'youtube_width', $w );
  477. /**
  478. * Filter the YouTube player height.
  479. *
  480. * @module shortcodes
  481. *
  482. * @since 1.1.0
  483. *
  484. * @param int $h Height of the YouTube player in pixels.
  485. */
  486. $h = (int) apply_filters( 'youtube_height', $h );
  487. return array( $w, $h );
  488. }
  489. /**
  490. * For bare URLs on their own line of the form
  491. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  492. *
  493. * @param array $matches Regex partial matches against the URL passed.
  494. * @param array $attr Attributes received in embed response.
  495. * @param array $url Requested URL to be embedded.
  496. */
  497. function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
  498. return youtube_id( $url );
  499. }
  500. /**
  501. * Add a new handler to automatically transform custom Youtube URLs (like playlists) into embeds.
  502. */
  503. function wpcom_youtube_embed_crazy_url_init() {
  504. wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
  505. }
  506. add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
  507. if (
  508. ! is_admin()
  509. /**
  510. * Allow oEmbeds in Jetpack's Comment form.
  511. *
  512. * @module shortcodes
  513. *
  514. * @since 2.8.0
  515. *
  516. * @param int $allow_oembed Option to automatically embed all plain text URLs.
  517. */
  518. && apply_filters( 'jetpack_comments_allow_oembed', true )
  519. // No need for this on WordPress.com, this is done for multiple shortcodes at a time there.
  520. && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
  521. ) {
  522. /*
  523. * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
  524. * so the iframe gets filtered out.
  525. * Higher priority because we need it before auto-link and autop get to it.
  526. */
  527. add_filter( 'comment_text', 'youtube_link', 1 );
  528. }
  529. /**
  530. * Core changes to do_shortcode (https://core.trac.wordpress.org/changeset/34747) broke "improper" shortcodes
  531. * with the format [shortcode=http://url.com].
  532. *
  533. * This removes the "=" from the shortcode so it can be parsed.
  534. *
  535. * @see https://github.com/Automattic/jetpack/issues/3121
  536. *
  537. * @param string $content HTML content.
  538. */
  539. function jetpack_fix_youtube_shortcode_display_filter( $content ) {
  540. if ( strpos( $content, '[youtube=' ) !== false ) {
  541. $content = preg_replace( '@\[youtube=(.*?)\]@', '[youtube $1]', $content );
  542. }
  543. return $content;
  544. }
  545. add_filter( 'the_content', 'jetpack_fix_youtube_shortcode_display_filter', 7 );