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