responsive-videos.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  21. add_action( 'after_setup_theme', 'jetpack_responsive_videos_init', 99 );
  22. /**
  23. * Adds a wrapper to videos and enqueue script
  24. *
  25. * @return string
  26. */
  27. function jetpack_responsive_videos_embed_html( $html ) {
  28. if ( empty( $html ) || ! is_string( $html ) ) {
  29. return $html;
  30. }
  31. // The customizer video widget wraps videos with a class of wp-video
  32. // mejs as of 4.9 apparently resizes videos too which causes issues
  33. // skip the video if it is wrapped in wp-video.
  34. $video_widget_wrapper = 'class="wp-video"';
  35. $mejs_wrapped = strpos( $html, $video_widget_wrapper );
  36. // If this is a video widget wrapped by mejs, return the html.
  37. if ( false !== $mejs_wrapped ) {
  38. return $html;
  39. }
  40. if ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) {
  41. wp_enqueue_script( 'jetpack-responsive-videos-script', plugins_url( 'responsive-videos/responsive-videos.js', __FILE__ ), array( 'jquery' ), '1.3', true );
  42. } else {
  43. wp_enqueue_script( 'jetpack-responsive-videos-min-script', plugins_url( 'responsive-videos/responsive-videos.min.js', __FILE__ ), array( 'jquery' ), '1.3', true );
  44. }
  45. // Enqueue CSS to ensure compatibility with all themes
  46. wp_enqueue_style( 'jetpack-responsive-videos-style', plugins_url( 'responsive-videos/responsive-videos.css', __FILE__ ) );
  47. return '<div class="jetpack-video-wrapper">' . $html . '</div>';
  48. }
  49. /**
  50. * Check if oEmbed is YouTube or Vimeo before wrapping.
  51. *
  52. * @return string
  53. */
  54. function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url ) {
  55. if ( empty( $html ) || ! is_string( $html ) || ! $url ) {
  56. return $html;
  57. }
  58. $jetpack_video_wrapper = '<div class="jetpack-video-wrapper">';
  59. $already_wrapped = strpos( $html, $jetpack_video_wrapper );
  60. // If the oEmbed has already been wrapped, return the html.
  61. if ( false !== $already_wrapped ) {
  62. return $html;
  63. }
  64. /**
  65. * oEmbed Video Providers.
  66. *
  67. * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output.
  68. *
  69. * @module theme-tools
  70. *
  71. * @since 3.8.0
  72. *
  73. * @param array $video_patterns oEmbed video provider Regex patterns.
  74. */
  75. $video_patterns = apply_filters( 'jetpack_responsive_videos_oembed_videos', array(
  76. 'https?://((m|www)\.)?youtube\.com/watch',
  77. 'https?://((m|www)\.)?youtube\.com/playlist',
  78. 'https?://youtu\.be/',
  79. 'https?://(.+\.)?vimeo\.com/',
  80. 'https?://(www\.)?dailymotion\.com/',
  81. 'https?://dai.ly/',
  82. 'https?://(www\.)?hulu\.com/watch/',
  83. 'https?://wordpress.tv/',
  84. 'https?://(www\.)?funnyordie\.com/videos/',
  85. 'https?://vine.co/v/',
  86. 'https?://(www\.)?collegehumor\.com/video/',
  87. 'https?://(www\.|embed\.)?ted\.com/talks/'
  88. ) );
  89. // Merge patterns to run in a single preg_match call.
  90. $video_patterns = '(' . implode( '|', $video_patterns ) . ')';
  91. $is_video = preg_match( $video_patterns, $url );
  92. // If the oEmbed is a video, wrap it in the responsive wrapper.
  93. if ( false === $already_wrapped && 1 === $is_video ) {
  94. return jetpack_responsive_videos_embed_html( $html );
  95. }
  96. return $html;
  97. }