PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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