PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/hunt9310/ras
PHP | 391 lines | 191 code | 61 blank | 139 comment | 67 complexity | 8d648496a4f153a0110ffe762dedaf20 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 ( ! is_string( $content ) || false === strpos( $content, 'youtube.com' ) ) {
  29. return $content;
  30. }
  31. //older codes
  32. $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';
  33. $regexp_ent = htmlspecialchars( $regexp, ENT_NOQUOTES );
  34. $old_regexp = '!<embed(?:\s+\w+="[^"]*")*\s+src="https?(?:\:|&#0*58;)//www\.youtube\.com/v/([^"]+)"(?:\s+\w+="[^"]*")*\s*(?:/>|>\s*</embed>)!';
  35. $old_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $old_regexp, ENT_NOQUOTES ) );
  36. //new code
  37. $ifr_regexp = '!<iframe((?:\s+\w+="[^"]*")*?)\s+src="(https?:)?//(?:www\.)*youtube.com/embed/([^"]+)".*?</iframe>!i';
  38. $ifr_regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $ifr_regexp, ENT_NOQUOTES ) );
  39. foreach ( array( 'regexp', 'regexp_ent', 'old_regexp', 'old_regexp_ent', 'ifr_regexp', 'ifr_regexp_ent' ) as $reg ) {
  40. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) )
  41. continue;
  42. foreach ( $matches as $match ) {
  43. // Hack, but '?' should only ever appear once, and
  44. // it should be for the 1st field-value pair in query string,
  45. // if it is present
  46. // YouTube changed their embed code.
  47. // Example of how it is now:
  48. // <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>
  49. // As shown at the start of function, previous YouTube didn't '?'
  50. // the 1st field-value pair.
  51. if ( in_array ( $reg, array( 'ifr_regexp', 'ifr_regexp_ent' ) ) ) {
  52. $params = $match[1];
  53. if ( 'ifr_regexp_ent' == $reg )
  54. $params = html_entity_decode( $params );
  55. $params = wp_kses_hair( $params, array( 'http' ) );
  56. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  57. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  58. $wh = '';
  59. if ( $width && $height )
  60. $wh = "&w=$width&h=$height";
  61. $url = esc_url_raw( "https://www.youtube.com/watch?v={$match[3]}{$wh}" );
  62. } else {
  63. $match[1] = str_replace( '?', '&', $match[1] );
  64. $url = esc_url_raw( "https://www.youtube.com/watch?v=" . html_entity_decode( $match[1] ) );
  65. }
  66. $content = str_replace( $match[0], "[youtube $url]", $content );
  67. /**
  68. * Fires before the YouTube embed is transformed into a shortcode.
  69. *
  70. * @module shortcodes
  71. *
  72. * @since 1.2.0
  73. *
  74. * @param string youtube Shortcode name.
  75. * @param string $url YouTube video URL.
  76. */
  77. do_action( 'jetpack_embed_to_shortcode', 'youtube', $url );
  78. }
  79. }
  80. return $content;
  81. }
  82. add_filter( 'pre_kses', 'youtube_embed_to_short_code' );
  83. /**
  84. * Replaces plain-text links to YouTube videos with YouTube embeds.
  85. *
  86. * @param string $content HTML content
  87. * @return string The content with embeds instead of URLs
  88. */
  89. function youtube_link( $content ) {
  90. 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/' );
  91. }
  92. /**
  93. * Callback function for the regex that replaces YouTube URLs with
  94. * YouTube embeds.
  95. */
  96. function youtube_link_callback( $matches ) {
  97. return "\n" . youtube_id( $matches[0] ) . "\n";
  98. }
  99. /**
  100. * Normalizes a YouTube URL to include a v= parameter and a query string free of encoded ampersands.
  101. *
  102. * @param string $url
  103. * @return string The normalized URL
  104. */
  105. if ( ! function_exists( 'youtube_sanitize_url' ) ) :
  106. function youtube_sanitize_url( $url ) {
  107. $url = trim( $url, ' "' );
  108. $url = trim( $url );
  109. $url = str_replace( array( 'youtu.be/', '/v/', '#!v=', '&amp;', '&#038;', 'playlist' ), array( 'youtu.be/?v=', '/?v=', '?v=', '&', '&', 'videoseries' ), $url );
  110. // 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.
  111. $query_string_start = strpos( $url, "?" );
  112. if ( false !== $query_string_start ) {
  113. $url = substr( $url, 0, $query_string_start + 1 ) . str_replace( "?", "&", substr( $url, $query_string_start + 1 ) );
  114. }
  115. return $url;
  116. }
  117. endif;
  118. /*
  119. * url can be:
  120. * http://www.youtube.com/embed/videoseries?list=PL94269DA08231042B&amp;hl=en_US
  121. * http://www.youtube.com/watch#!v=H2Ncxw1xfck
  122. * http://www.youtube.com/watch?v=H2Ncxw1xfck
  123. * http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0
  124. * http://www.youtube.com/v/jF-kELmmvgA
  125. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  126. * http://youtu.be/Rrohlqeir5E
  127. */
  128. /**
  129. * Converts a YouTube URL into an embedded YouTube video.
  130. */
  131. function youtube_id( $url ) {
  132. if ( ! $id = jetpack_get_youtube_id( $url ) )
  133. return '<!--YouTube Error: bad URL entered-->';
  134. $url = youtube_sanitize_url( $url );
  135. $url = parse_url( $url );
  136. if ( ! isset( $url['query'] ) )
  137. return false;
  138. if ( isset( $url['fragment'] ) ) {
  139. wp_parse_str( $url['fragment'], $fargs );
  140. } else {
  141. $fargs = array();
  142. }
  143. wp_parse_str( $url['query'], $qargs );
  144. $qargs = array_merge( $fargs, $qargs );
  145. // calculate the width and height, taking content_width into consideration
  146. global $content_width;
  147. $input_w = ( isset( $qargs['w'] ) && intval( $qargs['w'] ) ) ? intval( $qargs['w'] ) : 0;
  148. $input_h = ( isset( $qargs['h'] ) && intval( $qargs['h'] ) ) ? intval( $qargs['h'] ) : 0;
  149. // If we have $content_width, use it.
  150. if ( ! empty( $content_width ) ) {
  151. $default_width = $content_width;
  152. } else {
  153. // Otherwise get default width from the old, now deprecated embed_size_w option.
  154. $default_width = get_option('embed_size_w');
  155. }
  156. // If we don't know those 2 values use a hardcoded width.h
  157. if ( empty( $default_width ) ) {
  158. $default_width = 640;
  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. /**
  182. * Filter the YouTube player width.
  183. *
  184. * @module shortcodes
  185. *
  186. * @since 1.1.0
  187. *
  188. * @param int $w Width of the YouTube player in pixels.
  189. */
  190. $w = (int) apply_filters( 'youtube_width', $w );
  191. /**
  192. * Filter the YouTube player height.
  193. *
  194. * @module shortcodes
  195. *
  196. * @since 1.1.0
  197. *
  198. * @param int $h Height of the YouTube player in pixels.
  199. */
  200. $h = (int) apply_filters( 'youtube_height', $h );
  201. $rel = ( isset( $qargs['rel'] ) && 0 == $qargs['rel'] ) ? 0 : 1;
  202. $search = ( isset( $qargs['showsearch'] ) && 1 == $qargs['showsearch'] ) ? 1 : 0;
  203. $info = ( isset( $qargs['showinfo'] ) && 0 == $qargs['showinfo'] ) ? 0 : 1;
  204. $iv = ( isset( $qargs['iv_load_policy'] ) && 3 == $qargs['iv_load_policy'] ) ? 3 : 1;
  205. $fmt = ( isset( $qargs['fmt'] ) && intval( $qargs['fmt'] ) ) ? '&fmt=' . (int) $qargs['fmt'] : '';
  206. if ( ! isset( $qargs['autohide'] ) || ( $qargs['autohide'] < 0 || 2 < $qargs['autohide'] ) ) {
  207. $autohide = '&autohide=2';
  208. } else {
  209. $autohide = '&autohide=' . absint( $qargs['autohide'] );
  210. }
  211. $start = 0;
  212. if ( isset( $qargs['start'] ) ) {
  213. $start = intval( $qargs['start'] );
  214. } else if ( isset( $qargs['t'] ) ) {
  215. $time_pieces = preg_split( '/(?<=\D)(?=\d+)/', $qargs['t'] );
  216. foreach ( $time_pieces as $time_piece ) {
  217. $int = (int) $time_piece;
  218. switch ( substr( $time_piece, -1 ) ) {
  219. case 'h' :
  220. $start += $int * 3600;
  221. break;
  222. case 'm' :
  223. $start += $int * 60;
  224. break;
  225. case 's' :
  226. $start += $int;
  227. break;
  228. }
  229. }
  230. }
  231. $start = $start ? '&start=' . $start : '';
  232. $end = ( isset( $qargs['end'] ) && intval( $qargs['end'] ) ) ? '&end=' . (int) $qargs['end'] : '';
  233. $hd = ( isset( $qargs['hd'] ) && intval( $qargs['hd'] ) ) ? '&hd=' . (int) $qargs['hd'] : '';
  234. $vq = ( isset( $qargs['vq'] ) && in_array( $qargs['vq'], array('hd720','hd1080') ) ) ? '&vq=' . $qargs['vq'] : '';
  235. $cc = ( isset( $qargs['cc_load_policy'] ) ) ? '&cc_load_policy=1' : '';
  236. $cc_lang = ( isset( $qargs['cc_lang_pref'] ) ) ? '&cc_lang_pref=' . preg_replace( '/[^_a-z0-9-]/i', '', $qargs['cc_lang_pref'] ) : '';
  237. $wmode = ( isset( $qargs['wmode'] ) && in_array( strtolower( $qargs['wmode'] ), array( 'opaque', 'window', 'transparent' ) ) ) ? $qargs['wmode'] : 'transparent';
  238. $theme = ( isset( $qargs['theme'] ) && in_array( strtolower( $qargs['theme'] ), array( 'dark', 'light' ) ) ) ? '&theme=' . $qargs['theme'] : '';
  239. $autoplay = '';
  240. /**
  241. * Allow YouTube videos to start playing automatically.
  242. *
  243. * @module shortcodes
  244. *
  245. * @since 2.2.2
  246. *
  247. * @param bool false Enable autoplay for YouTube videos.
  248. */
  249. if ( apply_filters( 'jetpack_youtube_allow_autoplay', false ) && isset( $qargs['autoplay'] ) )
  250. $autoplay = '&autoplay=' . (int)$qargs['autoplay'];
  251. if ( ( isset( $url['path'] ) && '/videoseries' == $url['path'] ) || isset( $qargs['list'] ) ) {
  252. $html = "<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" ) ) . "' allowfullscreen='true' style='border:0;'></iframe>";
  253. } else {
  254. $html = "<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}" ) ) . "' allowfullscreen='true' style='border:0;'></iframe>";
  255. }
  256. // Let's do some alignment wonder in a span, unless we're producing a feed
  257. if ( ! is_feed() ) {
  258. $alignmentcss = 'text-align:center;';
  259. if ( isset( $qargs['align'] ) ) {
  260. switch ( $qargs['align'] ) {
  261. case 'left':
  262. $alignmentcss = "float:left; width:{$w}px; height:{$h}px; margin-right:10px; margin-bottom: 10px;";
  263. break;
  264. case 'right':
  265. $alignmentcss = "float:right; width:{$w}px; height:{$h}px; margin-left:10px; margin-bottom: 10px;";
  266. break;
  267. }
  268. }
  269. $html = sprintf(
  270. '<span class="embed-youtube" style="%s display: block;">%s</span>',
  271. esc_attr( $alignmentcss ),
  272. $html
  273. );
  274. }
  275. /**
  276. * Filter the YouTube video HTML output.
  277. *
  278. * @module shortcodes
  279. *
  280. * @since 1.2.3
  281. *
  282. * @param string $html YouTube video HTML output.
  283. */
  284. $html = apply_filters( 'video_embed_html', $html );
  285. return $html;
  286. }
  287. function youtube_shortcode( $atts ) {
  288. return youtube_id( ( isset ( $atts[0] ) ) ? ltrim( $atts[0] , '=' ) : shortcode_new_to_old_params( $atts ) );
  289. }
  290. add_shortcode( 'youtube', 'youtube_shortcode' );
  291. /**
  292. * For bare URLs on their own line of the form
  293. * http://www.youtube.com/v/9FhMMmqzbD8?fs=1&hl=en_US
  294. */
  295. function wpcom_youtube_embed_crazy_url( $matches, $attr, $url ) {
  296. return youtube_id( $url );
  297. }
  298. function wpcom_youtube_embed_crazy_url_init() {
  299. wp_embed_register_handler( 'wpcom_youtube_embed_crazy_url', '#https?://(?:www\.)?(?:youtube.com/(?:v/|playlist|watch[/\#?])|youtu\.be/).*#i', 'wpcom_youtube_embed_crazy_url' );
  300. }
  301. add_action( 'init', 'wpcom_youtube_embed_crazy_url_init' );
  302. /**
  303. * Allow oEmbeds in Jetpack's Comment form.
  304. *
  305. * @module shortcodes
  306. *
  307. * @since 2.8.0
  308. *
  309. * @param int get_option('embed_autourls') Option to automatically embed all plain text URLs.
  310. */
  311. if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
  312. // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out.
  313. // Higher priority because we need it before auto-link and autop get to it
  314. add_filter( 'comment_text', 'youtube_link', 1 );
  315. }
  316. /**
  317. * Core changes to do_shortcode (https://core.trac.wordpress.org/changeset/34747) broke "improper" shortcodes
  318. * with the format [shortcode=http://url.com].
  319. *
  320. * This removes the "=" from the shortcode so it can be parsed.
  321. *
  322. * @see https://github.com/Automattic/jetpack/issues/3121
  323. */
  324. function jetpack_fix_youtube_shortcode_display_filter( $content ) {
  325. if ( strpos( $content, '[youtube=' ) !== false ) {
  326. $content = preg_replace( '@\[youtube=(.*?)\]@', '[youtube $1]', $content );
  327. }
  328. return $content;
  329. }
  330. add_filter( 'the_content', 'jetpack_fix_youtube_shortcode_display_filter', 7 );