/modules/shortcodes/youtube.php

  1. <?php 
  2.  
  3. /** 
  4. * youtube shortcode 
  5. * 
  6. * Contains shortcode + some improvements over the Embeds syntax @ 
  7. * http://codex.wordpress.org/Embeds 
  8. * 
  9. * @example [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&fs=1&hl=bg_BG] 
  10. */ 
  11.  
  12. /** 
  13. * Replaces YouTube embeds with YouTube shortcodes. 
  14. * 
  15. * @param string $content HTML content. 
  16. * @return string The content with YouTube embeds replaced with YouTube shortcodes. 
  17. */ 
  18. // 2008-07-15: 
  19. //<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> 
  20. // around 2008-06-06 youtube changed their old embed code to this: 
  21. //<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> 
  22. // old style was: 
  23. // <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> 
  24. // 12-2010: 
  25. // <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/3H8bnKdf654?fs=1&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&hl=en_GB" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object> 
  26. // 01-2011: 
  27. // <iframe title="YouTube video player" class="youtube-player" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/Qq9El3ki0_g" frameborder="0" allowFullScreen></iframe> 
  28. // <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe> 
  29.  
  30. function youtube_embed_to_short_code( $content ) { 
  31. if ( false === strpos( $content, 'youtube.com' ) ) 
  32. return $content; 
  33.  
  34. //older codes 
  35. $regexp = '!<object width="\d+" height="\d+"><param name="movie" value="https?://www\.youtube\.com/v/([^"]+)"></param>(?:<param name="\w+" value="[^"]*"></param>)*<embed src="https?://www\.youtube\.com/v/(.+)" type="application/x-shockwave-flash"(?: \w+="[^"]*")* width="\d+" height="\d+"></embed></object>!i'; 
  36. $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES ); 
  37. $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|�*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!'; 
  38. $old_regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) ); 
  39.  
  40. //new code 
  41. $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i'; 
  42. $ifr_regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) ); 
  43.  
  44. foreach ( array( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg ) { 
  45. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) 
  46. continue; 
  47.  
  48. foreach ( $matches as $match ) { 
  49. // Hack, but '?' should only ever appear once, and 
  50. // it should be for the 1st field-value pair in query string,  
  51. // if it is present 
  52. // YouTube changed their embed code. 
  53. // Example of how it is now: 
  54. // <object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/aP9AaD4tgBY?fs=1&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&hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object> 
  55. // As shown at the start of function, previous YouTube didn't '?' 
  56. // the 1st field-value pair. 
  57. if ( in_array ( $reg, array( 'ifr_regexp', 'ifr_regexp_ent' ) ) ) { 
  58. $params = $match[1]; 
  59.  
  60. if ( 'ifr_regexp_ent' == $reg ) 
  61. $params = html_entity_decode( $params ); 
  62.  
  63. $params = wp_kses_hair( $params, array( 'http' ) ); 
  64.  
  65. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; 
  66. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0; 
  67. $wh = ''; 
  68.  
  69. if ( $width && $height ) 
  70. $wh = "&w=$width&h=$height"; 
  71.  
  72. $url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" ); 
  73. } else { 
  74. $match[1] = str_replace( '?', '&', $match[1] ); 
  75.  
  76. $url = esc_url_raw( "https://www.youtube.com/watch?v=" . html_entity_decode( $match[1] ) ); 
  77.  
  78. $content = str_replace( $match[0], "[youtube $url]", $content ); 
  79.  
  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. return $content; 
  93.  
  94. add_filter( 'pre_kses', 'youtube_embed_to_short_code' ); 
  95.  
  96. /** 
  97. * Replaces plain-text links to YouTube videos with YouTube embeds. 
  98. * 
  99. * @param string $content HTML content 
  100. * @return string The content with embeds instead of URLs 
  101. */ 
  102. function youtube_link( $content ) { 
  103. return preg_replace_callback( '!(?:\n|\A)https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/)[^\s]+?(?:\n|\Z)!i', 'youtube_link_callback', $content ); 
  104.  
  105. /** 
  106. * Callback function for the regex that replaces YouTube URLs with 
  107. * YouTube embeds. 
  108. */ 
  109. function youtube_link_callback( $matches ) { 
  110. return "\n" . youtube_id( $matches[0] ) . "\n"; 
  111.  
  112. /** 
  113. * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands. 
  114. * 
  115. * @param string $url 
  116. * @return string The normalized URL 
  117. */ 
  118. if ( ! function_exists( 'youtube_sanitize_url' ) ) : 
  119. function youtube_sanitize_url( $url ) { 
  120. $url = trim( $url, ' "' ); 
  121. $url = trim( $url ); 
  122. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&', '&', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url ); 
  123.  
  124. // 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. 
  125. $query_string_start = strpos( $url, "?" ); 
  126.  
  127. if ( false !== $query_string_start ) { 
  128. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) ); 
  129.  
  130. return $url; 
  131. endif; 
  132.  
  133. /** 
  134. * url can be: 
  135. * http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&hl=en_US 
  136. * http://www.youtube.com/watch#!v=H2Ncxw1xfck 
  137. * http://www.youtube.com/watch?v=H2Ncxw1xfck 
  138. * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0 
  139. * http://www.youtube.com/v/jF-kELmmvgA 
  140. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US 
  141. * http://youtu.be/Rrohlqeir5E 
  142. */ 
  143.  
  144. /** 
  145. * Converts a YouTube URL into an embedded YouTube video. 
  146. */ 
  147. function youtube_id( $url ) { 
  148. if ( ! $id = jetpack_get_youtube_id( $url ) ) 
  149. return '<!--YouTube Error: bad URL entered-->'; 
  150.  
  151. $url = youtube_sanitize_url( $url ); 
  152. $url = parse_url( $url ); 
  153.  
  154. if ( ! isset( $url['query'] ) ) 
  155. return false; 
  156.  
  157. if ( isset( $url['fragment'] ) ) { 
  158. wp_parse_str( $url['fragment'], $fargs ); 
  159. } else { 
  160. $fargs = array(); 
  161. wp_parse_str( $url['query'], $qargs ); 
  162.  
  163. $qargs = array_merge( $fargs, $qargs ); 
  164.  
  165. // calculate the width and height, taking content_width into consideration 
  166. global $content_width; 
  167.  
  168. $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0; 
  169. $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0; 
  170.  
  171. $default_width = get_option('embed_size_w'); 
  172.  
  173. if ( empty( $default_width ) ) { 
  174. if ( ! empty( $content_width ) ) { 
  175. $default_width = $content_width; 
  176. } else { 
  177. $default_width = 640; 
  178.  
  179. if ( $input_w > 0 && $input_h > 0 ) { 
  180. $w = $input_w; 
  181. $h = $input_h; 
  182. } elseif ( 0 == $input_w && 0 == $input_h ) { 
  183. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) { 
  184. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 ); 
  185. } else { 
  186. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width ); 
  187. $h = ceil( ( $w / 16 ) * 9 ) + 30; 
  188. } elseif ( $input_w > 0 ) { 
  189. $w = $input_w; 
  190. $h = ceil( ( $w / 16 ) * 9 ) + 30; 
  191. } else { 
  192. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) { 
  193. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 ); 
  194. } else { 
  195. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width ); 
  196. $h = $input_h; 
  197.  
  198. /** 
  199. * Filter the YouTube player width. 
  200. * 
  201. * @module shortcodes 
  202. * 
  203. * @since 1.1.0 
  204. * 
  205. * @param int $w Width of the YouTube player in pixels. 
  206. */ 
  207. $w = (int) apply_filters( 'youtube_width', $w ); 
  208.  
  209. /** 
  210. * Filter the YouTube player height. 
  211. * 
  212. * @module shortcodes 
  213. * 
  214. * @since 1.1.0 
  215. * 
  216. * @param int $h Height of the YouTube player in pixels. 
  217. */ 
  218. $h = (int) apply_filters( 'youtube_height', $h ); 
  219.  
  220. $rel = ( isset( $qargs['rel'] ) && 0 == $qargs['rel'] ) ? 0 : 1; 
  221. $search = ( isset( $qargs['showsearch'] ) && 1 == $qargs['showsearch'] ) ? 1 : 0; 
  222. $info = ( isset( $qargs['showinfo'] ) && 0 == $qargs['showinfo'] ) ? 0 : 1; 
  223. $iv = ( isset( $qargs['iv_load_policy'] ) && 3 == $qargs['iv_load_policy'] ) ? 3 : 1; 
  224.  
  225. $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : ''; 
  226.  
  227. if ( ! isset( $qargs['autohide'] ) || ( $qargs['autohide'] < 0 || 2 < $qargs['autohide'] ) ) { 
  228. $autohide = '&autohide=2'; 
  229. } else { 
  230. $autohide = '&autohide=' . absint( $qargs['autohide'] ); 
  231.  
  232. $start = 0; 
  233. if ( isset( $qargs['start'] ) ) { 
  234. $start = intval( $qargs['start'] ); 
  235. } else if ( isset( $qargs['t'] ) ) { 
  236. $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $qargs['t'] ); 
  237.  
  238. foreach ( $time_pieces as $time_piece ) { 
  239. $int = (int) $time_piece; 
  240. switch ( substr( $time_piece, -1 ) ) { 
  241. case 'h' : 
  242. $start += $int * 3600; 
  243. break; 
  244. case 'm' : 
  245. $start += $int * 60; 
  246. break; 
  247. case 's' : 
  248. $start += $int; 
  249. break; 
  250.  
  251. $start = $start ? '&start=' . $start : ''; 
  252. $end = ( isset( $qargs['end'] ) && intval( $qargs['end'] ) ) ? '&end=' . (int) $qargs['end'] : ''; 
  253. $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : ''; 
  254.  
  255. $vq = ( isset( $qargs['vq'] ) && in_array( $qargs['vq'], array('hd720', 'hd1080') ) ) ? '&vq=' . $qargs['vq'] : ''; 
  256.  
  257. $cc = ( isset( $qargs['cc_load_policy'] ) ) ? '&cc_load_policy=1' : ''; 
  258. $cc_lang = ( isset( $qargs['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref'] ) : ''; 
  259.  
  260. $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ) ) ) ? $qargs['wmode'] : 'transparent'; 
  261.  
  262. $theme = ( isset( $qargs['theme'] ) && in_array( strtolower( $qargs['theme'] ), array( 'dark', 'light' ) ) ) ? '&theme=' . $qargs['theme'] : ''; 
  263.  
  264. $autoplay = ''; 
  265. /** 
  266. * Allow YouTube videos to start playing automatically. 
  267. * 
  268. * @module shortcodes 
  269. * 
  270. * @since 2.2.2 
  271. * 
  272. * @param bool false Enable autoplay for YouTube videos. 
  273. */ 
  274. if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $qargs['autoplay'] ) ) 
  275. $autoplay = '&autoplay=' . (int)$qargs['autoplay']; 
  276.  
  277. $alignmentcss = 'text-align:center;'; 
  278. if ( isset( $qargs['align'] ) ) { 
  279. switch ( $qargs['align'] ) { 
  280. case 'left': 
  281. $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;"; 
  282. break; 
  283. case 'right': 
  284. $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;"; 
  285. break; 
  286.  
  287. if ( ( isset( $url['path'] ) && '/videoseries' == $url['path'] ) || isset( $qargs['list'] ) ) { 
  288. $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>"; 
  289. } else { 
  290. $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>"; 
  291.  
  292. /** 
  293. * Filter the YouTube video HTML output. 
  294. * 
  295. * @module shortcodes 
  296. * 
  297. * @since 1.2.3 
  298. * 
  299. * @param string $html YouTube video HTML output. 
  300. */ 
  301. $html = apply_filters( 'video_embed_html', $html ); 
  302.  
  303. return $html; 
  304.  
  305. function youtube_shortcode( $atts ) { 
  306. return youtube_id( ( isset ( $atts[0] ) ) ? ltrim( $atts[0] , '=' ) : shortcode_new_to_old_params( $atts ) ); 
  307.  
  308. add_shortcode( 'youtube', 'youtube_shortcode' ); 
  309.  
  310. /** 
  311. * For bare URLs on their own line of the form 
  312. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US 
  313. */ 
  314. function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) { 
  315. return youtube_id( $url ); 
  316.  
  317. function wpcom_youtube_embed_crazy_url_init() { 
  318. wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' ); 
  319.  
  320. add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' ); 
  321.  
  322. /** 
  323. * Allow oEmbeds in Jetpack's Comment form. 
  324. * 
  325. * @module shortcodes 
  326. * 
  327. * @since 2.8.0 
  328. * 
  329. * @param int get_option('embed_autourls') Option to automatically embed all plain text URLs. 
  330. */ 
  331. if ( apply_filters( 'jetpack_comments_allow_oembed', get_option('embed_autourls') ) ) { 
  332. // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out. 
  333. if ( ! is_admin() ) { 
  334. // Higher priority because we need it before auto-link and autop get to it 
  335. add_filter( 'comment_text', 'youtube_link', 1 ); 
.