Нема описа

responsive-videos.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Load the Responsive videos plugin
  4. */
  5. function jetpack_responsive_videos_init() {
  6. /* If the doesn't theme support 'jetpack-responsive-videos', don't continue */
  7. if ( ! current_theme_supports( 'jetpack-responsive-videos' ) ) {
  8. return;
  9. }
  10. /* If the theme does support 'jetpack-responsive-videos', wrap the videos */
  11. add_filter( 'wp_video_shortcode', 'jetpack_responsive_videos_embed_html' );
  12. add_filter( 'video_embed_html', 'jetpack_responsive_videos_embed_html' );
  13. /* Only wrap oEmbeds if video */
  14. add_filter( 'embed_oembed_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 );
  15. add_filter( 'embed_handler_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 );
  16. /* Wrap videos in Buddypress */
  17. add_filter( 'bp_embed_oembed_html', 'jetpack_responsive_videos_embed_html' );
  18. /* Wrap Slideshare shortcodes */
  19. add_filter( 'jetpack_slideshare_shortcode', 'jetpack_responsive_videos_embed_html' );
  20. // Remove the Jetpack Responsive video wrapper in embed blocks on sites that support core Responsive embeds.
  21. if ( current_theme_supports( 'responsive-embeds' ) ) {
  22. add_filter( 'render_block', 'jetpack_responsive_videos_remove_wrap_oembed', 10, 2 );
  23. }
  24. }
  25. add_action( 'after_setup_theme', 'jetpack_responsive_videos_init', 99 );
  26. /**
  27. * Adds a wrapper to videos and enqueue script
  28. *
  29. * @return string
  30. */
  31. function jetpack_responsive_videos_embed_html( $html ) {
  32. if ( empty( $html ) || ! is_string( $html ) ) {
  33. return $html;
  34. }
  35. // Short-circuit for AMP responses, since custom scripts are not allowed in AMP and videos are naturally responsive.
  36. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  37. return $html;
  38. }
  39. // The customizer video widget wraps videos with a class of wp-video
  40. // mejs as of 4.9 apparently resizes videos too which causes issues
  41. // skip the video if it is wrapped in wp-video.
  42. $video_widget_wrapper = 'class="wp-video"';
  43. $mejs_wrapped = strpos( $html, $video_widget_wrapper );
  44. // If this is a video widget wrapped by mejs, return the html.
  45. if ( false !== $mejs_wrapped ) {
  46. return $html;
  47. }
  48. if ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) {
  49. wp_enqueue_script( 'jetpack-responsive-videos-script', plugins_url( 'responsive-videos/responsive-videos.js', __FILE__ ), array( 'jquery' ), '1.3', true );
  50. } else {
  51. wp_enqueue_script( 'jetpack-responsive-videos-min-script', plugins_url( 'responsive-videos/responsive-videos.min.js', __FILE__ ), array( 'jquery' ), '1.3', true );
  52. }
  53. // Enqueue CSS to ensure compatibility with all themes
  54. wp_enqueue_style( 'jetpack-responsive-videos-style', plugins_url( 'responsive-videos/responsive-videos.css', __FILE__ ) );
  55. return '<div class="jetpack-video-wrapper">' . $html . '</div>';
  56. }
  57. /**
  58. * Check if oEmbed is a `$video_patterns` provider video before wrapping.
  59. *
  60. * @param mixed $html The cached HTML result, stored in post meta.
  61. * @param string $url he attempted embed URL.
  62. *
  63. * @return string
  64. */
  65. function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null ) {
  66. if ( empty( $html ) || ! is_string( $html ) || ! $url ) {
  67. return $html;
  68. }
  69. // Short-circuit for AMP responses, since custom scripts are not allowed in AMP and videos are naturally responsive.
  70. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  71. return $html;
  72. }
  73. $jetpack_video_wrapper = '<div class="jetpack-video-wrapper">';
  74. $already_wrapped = strpos( $html, $jetpack_video_wrapper );
  75. // If the oEmbed has already been wrapped, return the html.
  76. if ( false !== $already_wrapped ) {
  77. return $html;
  78. }
  79. /**
  80. * oEmbed Video Providers.
  81. *
  82. * An allowed list of oEmbed video provider Regex patterns to check against before wrapping the output.
  83. *
  84. * @module theme-tools
  85. *
  86. * @since 3.8.0
  87. *
  88. * @param array $video_patterns oEmbed video provider Regex patterns.
  89. */
  90. $video_patterns = apply_filters(
  91. 'jetpack_responsive_videos_oembed_videos',
  92. array(
  93. 'https?://((m|www)\.)?youtube\.com/watch',
  94. 'https?://((m|www)\.)?youtube\.com/playlist',
  95. 'https?://youtu\.be/',
  96. 'https?://(.+\.)?vimeo\.com/',
  97. 'https?://(www\.)?dailymotion\.com/',
  98. 'https?://dai.ly/',
  99. 'https?://(www\.)?hulu\.com/watch/',
  100. 'https?://wordpress.tv/',
  101. 'https?://(www\.)?funnyordie\.com/videos/',
  102. 'https?://vine.co/v/',
  103. 'https?://(www\.)?collegehumor\.com/video/',
  104. 'https?://(www\.|embed\.)?ted\.com/talks/',
  105. )
  106. );
  107. // Merge patterns to run in a single preg_match call.
  108. $video_patterns = '(' . implode( '|', $video_patterns ) . ')';
  109. $is_video = preg_match( $video_patterns, $url );
  110. // If the oEmbed is a video, wrap it in the responsive wrapper.
  111. if ( false === $already_wrapped && 1 === $is_video ) {
  112. return jetpack_responsive_videos_embed_html( $html );
  113. }
  114. return $html;
  115. }
  116. /**
  117. * Remove the Jetpack Responsive video wrapper in embed blocks.
  118. *
  119. * @since 7.0.0
  120. *
  121. * @param string $block_content The block content about to be appended.
  122. * @param array $block The full block, including name and attributes.
  123. *
  124. * @return string $block_content String of rendered HTML.
  125. */
  126. function jetpack_responsive_videos_remove_wrap_oembed( $block_content, $block ) {
  127. if (
  128. isset( $block['blockName'] )
  129. && (
  130. false !== strpos( $block['blockName'], 'core-embed' ) // pre-WP 5.6 embeds (multiple embed blocks starting with 'core-embed').
  131. || 'core/embed' === $block['blockName'] // WP 5.6 embed block format (single embed block w/ block variations).
  132. )
  133. ) {
  134. $block_content = preg_replace( '#<div class="jetpack-video-wrapper">(.*?)</div>#', '${1}', $block_content );
  135. }
  136. return $block_content;
  137. }