PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/shortcodes/youtube.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 284 lines | 155 code | 56 blank | 73 comment | 56 complexity | 2767768387b12c7a4666a7cae868c358 MD5 | raw file
  1. <?php
  2. /**
  3. * youtube shortcode
  4. *
  5. * Contains shortcode + some improvements over the Embeds syntax @
  6. * http://codex.wordpress.org/Embeds
  7. *
  8. * @example [youtube=http://www.youtube.com/watch?v=wq0rXGLs0YM&amp;fs=1&amp;hl=bg_BG]
  9. */
  10. /**
  11. * Replaces YouTube embeds with YouTube shortcodes.
  12. *
  13. * @param string $content HTML content.
  14. * @return string The content with YouTube embeds replaced with YouTube shortcodes.
  15. */
  16. // 2008-07-15:
  17. //<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>
  18. // around 2008-06-06 youtube changed their old embed code to this:
  19. //<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>
  20. // old style was:
  21. // <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>
  22. // 12-2010:
  23. // <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>
  24. // 01-2011:
  25. // <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>
  26. // <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/VIDEO_ID" frameborder="0"></iframe>
  27. function youtube_embed_to_short_code( $content ) {
  28. if ( false === strpos( $content, 'youtube.com' ) )
  29. return $content;
  30. //older codes
  31. $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';
  32. $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES );
  33. $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|&#0*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!';
  34. $old_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) );
  35. //new code
  36. $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="https?://(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i';
  37. $ifr_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) );
  38. foreach ( array( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg ) {
  39. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) )
  40. continue;
  41. foreach ( $matches as $match ) {
  42. // Hack, but '?' should only ever appear once, and
  43. // it should be for the 1st field-value pair in query string,
  44. // if it is present
  45. // YouTube changed their embed code.
  46. // Example of how it is now:
  47. // <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>
  48. // As shown at the start of function, previous YouTube didn't '?'
  49. // the 1st field-value pair.
  50. if ( in_array ( $reg, array( 'ifr_regexp', 'ifr_regexp_ent' ) ) ) {
  51. $params = $match[1];
  52. if ( 'ifr_regexp_ent' == $reg )
  53. $params = html_entity_decode( $params );
  54. $params = wp_kses_hair( $params, array( 'http' ) );
  55. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  56. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  57. $wh = '';
  58. if ( $width && $height )
  59. $wh = "&w=$width&h=$height";
  60. $url = esc_url_raw( "http://www.youtube.com/watch?v={$match[2]}{$wh}" );
  61. } else {
  62. $match[1] = str_replace( '?', '&', $match[1] );
  63. $url = esc_url_raw( 'http://www.youtube.com/watch?v=' . html_entity_decode( $match[1] ) );
  64. }
  65. $content = str_replace( $match[0], "[youtube $url]", $content );
  66. do_action( 'jetpack_embed_to_shortcode', 'youtube', $url );
  67. }
  68. }
  69. return $content;
  70. }
  71. add_filter('pre_kses', 'youtube_embed_to_short_code');
  72. /**
  73. * Replaces plain-text links to YouTube videos with YouTube embeds.
  74. *
  75. * @param string $content HTML content
  76. * @return string The content with embeds instead of URLs
  77. */
  78. function youtube_link( $content ) {
  79. return preg_replace_callback( '!(?:\n|\A)http://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/)[^\s]+?(?:\n|\Z)!i', 'youtube_link_callback', $content );
  80. }
  81. /**
  82. * Callback function for the regex that replaces YouTube URLs with
  83. * YouTube embeds.
  84. */
  85. function youtube_link_callback( $matches ) {
  86. return "\n" . youtube_id( $matches[0] ) . "\n";
  87. }
  88. /**
  89. * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
  90. *
  91. * @param string $url
  92. * @return string The normalized URL
  93. */
  94. function youtube_sanitize_url( $url ) {
  95. $url = trim( $url, ' "' );
  96. $url = trim( $url );
  97. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
  98. // 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.
  99. $query_string_start = strpos( $url, "?" );
  100. if ( false !== $query_string_start ) {
  101. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) );
  102. }
  103. return $url;
  104. }
  105. /*
  106. * url can be:
  107. * http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
  108. * http://www.youtube.com/watch#!v=H2Ncxw1xfck
  109. * http://www.youtube.com/watch?v=H2Ncxw1xfck
  110. * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
  111. * http://www.youtube.com/v/jF-kELmmvgA
  112. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  113. * http://youtu.be/Rrohlqeir5E
  114. */
  115. function get_youtube_id( $url ) {
  116. $url = youtube_sanitize_url( $url );
  117. $url = parse_url( $url );
  118. $id = false;
  119. if ( ! isset( $url['query'] ) )
  120. return false;
  121. parse_str( $url['query'], $qargs );
  122. if ( ! isset( $qargs['v'] ) && ! isset( $qargs['list'] ) )
  123. return false;
  124. if ( isset( $qargs['list'] ) )
  125. $id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['list'] );
  126. if ( empty( $id ) )
  127. $id = preg_replace( '|[^_a-z0-9-]|i', '', $qargs['v'] );
  128. return $id;
  129. }
  130. /**
  131. * Converts a YouTube URL into an embedded YouTube video.
  132. */
  133. function youtube_id( $url ) {
  134. if ( apply_filters( 'jetpack_bail_on_shortcode', false, 'youtube' ) )
  135. return '';
  136. if ( ! $id = get_youtube_id( $url ) )
  137. return '<!--YouTube Error: bad URL entered-->';
  138. $url = youtube_sanitize_url( $url );
  139. $url = parse_url( $url );
  140. if ( ! isset( $url['query'] ) )
  141. return false;
  142. parse_str( $url['query'], $qargs );
  143. $agent = $_SERVER['HTTP_USER_AGENT'];
  144. // Bloglines & Google Reader handle YouTube well now, instead of
  145. // big blank space of yester year, so they can skip this treatment
  146. if ( is_feed() && ! preg_match( '#' . apply_filters( 'jetpack_shortcode_youtube_whitelist_user_agents', 'Bloglines|FeedFetcher-Google|feedburner' ) . '#i', $agent ) )
  147. 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>';
  148. // calculate the width and height, taking content_width into consideration
  149. global $content_width;
  150. $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0;
  151. $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0;
  152. $default_width = get_option('embed_size_w');
  153. if ( empty( $default_width ) ) {
  154. if ( ! empty( $content_width ) ) {
  155. $default_width = $content_width;
  156. } else {
  157. $default_width = 640;
  158. }
  159. }
  160. if ( $input_w > 0 && $input_h > 0 ) {
  161. $w = $input_w;
  162. $h = $input_h;
  163. } elseif ( 0 == $input_w && 0 == $input_h ) {
  164. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
  165. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
  166. } else {
  167. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
  168. $h = ceil( ( $w / 16 ) * 9 ) + 30;
  169. }
  170. } elseif ( $input_w > 0 ) {
  171. $w = $input_w;
  172. $h = ceil( ( $w / 16 ) * 9 ) + 30;
  173. } else {
  174. if ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) {
  175. $w = ( ! empty( $content_width ) ? min( $content_width, 480 ) : 480 );
  176. } else {
  177. $w = ( ! empty( $content_width ) ? min( $content_width, $default_width ) : $default_width );
  178. $h = $input_h;
  179. }
  180. }
  181. $w = (int) apply_filters( 'youtube_width', $w );
  182. $h = (int) apply_filters( 'youtube_height', $h );
  183. $rel = ( isset( $qargs['rel'] ) && 0 == $qargs['rel'] ) ? 0 : 1;
  184. $search = ( isset( $qargs['showsearch'] ) && 1 == $qargs['showsearch'] ) ? 1 : 0;
  185. $info = ( isset( $qargs['showinfo'] ) && 0 == $qargs['showinfo'] ) ? 0 : 1;
  186. $iv = ( isset( $qargs['iv_load_policy'] ) && 3 == $qargs['iv_load_policy'] ) ? 3 : 1;
  187. $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : '';
  188. $start = ( isset( $qargs['start'] ) && intval( $qargs['start'] ) ) ? '&start=' . (int) $qargs['start'] : '';
  189. $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : '';
  190. $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ) ) ) ? $qargs['wmode'] : 'transparent';
  191. $alignmentcss = 'text-align:center;';
  192. if ( isset( $qargs['align'] ) ) {
  193. switch ( $qargs['align'] ) {
  194. case 'left':
  195. $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
  196. break;
  197. case 'right':
  198. $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
  199. break;
  200. }
  201. }
  202. if ( ( isset( $url['path'] ) && '/videoseries' == $url['path'] ) || isset( $qargs['list'] ) ) {
  203. $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>";
  204. } else {
  205. $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>";
  206. }
  207. $html = apply_filters( 'video_embed_html', $html );
  208. return $html;
  209. }
  210. function youtube_shortcode( $atts ) {
  211. return youtube_id( ( isset ( $atts[0] ) ) ? ltrim( $atts[0] , '=' ) : shortcode_new_to_old_params( $atts ) );
  212. }
  213. add_shortcode( 'youtube', 'youtube_shortcode' );
  214. /**
  215. * For bare URLs on their own line of the form
  216. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  217. */
  218. function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
  219. return youtube_id( $url );
  220. }
  221. function wpcom_youtube_embed_crazy_url_init() {
  222. wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#http://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
  223. }
  224. add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
  225. // higher priority because we need it before auto-link and autop get to it
  226. if ( get_option('embed_autourls') ) {
  227. add_filter( 'comment_text', 'youtube_link', 1 );
  228. }