compat
2 years ago
content-options
2 years ago
js
6 years ago
responsive-videos
3 years ago
site-logo
2 years ago
social-menu
2 years ago
content-options.php
2 years ago
devicepx.php
2 years ago
featured-content.php
2 years ago
infinite-scroll.php
4 years ago
responsive-videos.php
2 years ago
site-breadcrumbs.php
3 years ago
site-logo.php
2 years ago
social-links.php
2 years ago
social-menu.php
2 years ago
responsive-videos.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Theme Tools: Responsive videos enhancements. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Assets; |
| 9 | |
| 10 | if ( ! class_exists( '\Automattic\Jetpack\Classic_Theme_Helper\Main' ) ) { |
| 11 | /** |
| 12 | * Load the Responsive videos plugin |
| 13 | * |
| 14 | * @deprecated 13.7 Moved to Classic Theme Helper package. |
| 15 | */ |
| 16 | function jetpack_responsive_videos_init() { |
| 17 | _deprecated_function( __FUNCTION__, 'jetpack-13.7' ); |
| 18 | |
| 19 | /* If the doesn't theme support 'jetpack-responsive-videos', don't continue */ |
| 20 | if ( ! current_theme_supports( 'jetpack-responsive-videos' ) ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | /* If the theme does support 'jetpack-responsive-videos', wrap the videos */ |
| 25 | add_filter( 'wp_video_shortcode', 'jetpack_responsive_videos_embed_html' ); |
| 26 | add_filter( 'video_embed_html', 'jetpack_responsive_videos_embed_html' ); |
| 27 | |
| 28 | /* Only wrap oEmbeds if video */ |
| 29 | add_filter( 'embed_oembed_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 ); |
| 30 | add_filter( 'embed_handler_html', 'jetpack_responsive_videos_maybe_wrap_oembed', 10, 2 ); |
| 31 | |
| 32 | /* Wrap videos in Buddypress */ |
| 33 | add_filter( 'bp_embed_oembed_html', 'jetpack_responsive_videos_embed_html' ); |
| 34 | |
| 35 | /* Wrap Slideshare shortcodes */ |
| 36 | add_filter( 'jetpack_slideshare_shortcode', 'jetpack_responsive_videos_embed_html' ); |
| 37 | |
| 38 | // Remove the Jetpack Responsive video wrapper in embed blocks on sites that support core Responsive embeds. |
| 39 | if ( current_theme_supports( 'responsive-embeds' ) ) { |
| 40 | add_filter( 'render_block', 'jetpack_responsive_videos_remove_wrap_oembed', 10, 2 ); |
| 41 | } |
| 42 | } |
| 43 | add_action( 'after_setup_theme', 'jetpack_responsive_videos_init', 99 ); |
| 44 | |
| 45 | /** |
| 46 | * Adds a wrapper to videos and enqueue script |
| 47 | * |
| 48 | * @deprecated 13.7 Moved to Classic Theme Helper package. |
| 49 | * |
| 50 | * @param string $html The video embed HTML. |
| 51 | * @return string |
| 52 | */ |
| 53 | function jetpack_responsive_videos_embed_html( $html ) { |
| 54 | _deprecated_function( __FUNCTION__, 'jetpack-13.7' ); |
| 55 | |
| 56 | if ( empty( $html ) || ! is_string( $html ) ) { |
| 57 | return $html; |
| 58 | } |
| 59 | |
| 60 | // Short-circuit for AMP responses, since custom scripts are not allowed in AMP and videos are naturally responsive. |
| 61 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 62 | return $html; |
| 63 | } |
| 64 | |
| 65 | // The customizer video widget wraps videos with a class of wp-video |
| 66 | // mejs as of 4.9 apparently resizes videos too which causes issues |
| 67 | // skip the video if it is wrapped in wp-video. |
| 68 | $video_widget_wrapper = 'class="wp-video"'; |
| 69 | |
| 70 | $mejs_wrapped = strpos( $html, $video_widget_wrapper ); |
| 71 | |
| 72 | // If this is a video widget wrapped by mejs, return the html. |
| 73 | if ( false !== $mejs_wrapped ) { |
| 74 | return $html; |
| 75 | } |
| 76 | |
| 77 | Assets::register_script( |
| 78 | 'jetpack-responsive-videos', |
| 79 | '_inc/build/theme-tools/responsive-videos/responsive-videos.min.js', |
| 80 | JETPACK__PLUGIN_FILE, |
| 81 | array( |
| 82 | 'in_footer' => true, |
| 83 | 'enqueue' => true, |
| 84 | 'textdomain' => 'jetpack', |
| 85 | 'css_path' => '_inc/build/theme-tools/responsive-videos/responsive-videos.css', |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | return '<div class="jetpack-video-wrapper">' . $html . '</div>'; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Check if oEmbed is a `$video_patterns` provider video before wrapping. |
| 94 | * |
| 95 | * @deprecated 13.7 Moved to Classic Theme Helper package. |
| 96 | * |
| 97 | * @param mixed $html The cached HTML result, stored in post meta. |
| 98 | * @param string $url he attempted embed URL. |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null ) { |
| 103 | _deprecated_function( __FUNCTION__, 'jetpack-13.7' ); |
| 104 | |
| 105 | if ( empty( $html ) || ! is_string( $html ) || ! $url ) { |
| 106 | return $html; |
| 107 | } |
| 108 | |
| 109 | // Short-circuit for AMP responses, since custom scripts are not allowed in AMP and videos are naturally responsive. |
| 110 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 111 | return $html; |
| 112 | } |
| 113 | |
| 114 | $jetpack_video_wrapper = '<div class="jetpack-video-wrapper">'; |
| 115 | |
| 116 | $already_wrapped = strpos( $html, $jetpack_video_wrapper ); |
| 117 | |
| 118 | // If the oEmbed has already been wrapped, return the html. |
| 119 | if ( false !== $already_wrapped ) { |
| 120 | return $html; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * The oEmbed video providers. |
| 125 | * |
| 126 | * An allowed list of oEmbed video provider Regex patterns to check against before wrapping the output. |
| 127 | * |
| 128 | * @module theme-tools |
| 129 | * |
| 130 | * @since 3.8.0 |
| 131 | * |
| 132 | * @param array $video_patterns oEmbed video provider Regex patterns. |
| 133 | */ |
| 134 | $video_patterns = apply_filters( |
| 135 | 'jetpack_responsive_videos_oembed_videos', |
| 136 | array( |
| 137 | 'https?://((m|www)\.)?youtube\.com/watch', |
| 138 | 'https?://((m|www)\.)?youtube\.com/playlist', |
| 139 | 'https?://youtu\.be/', |
| 140 | 'https?://(.+\.)?vimeo\.com/', |
| 141 | 'https?://(www\.)?dailymotion\.com/', |
| 142 | 'https?://dai.ly/', |
| 143 | 'https?://(www\.)?hulu\.com/watch/', |
| 144 | 'https?://wordpress.tv/', |
| 145 | 'https?://(www\.)?funnyordie\.com/videos/', |
| 146 | 'https?://vine.co/v/', |
| 147 | 'https?://(www\.)?collegehumor\.com/video/', |
| 148 | 'https?://(www\.|embed\.)?ted\.com/talks/', |
| 149 | ) |
| 150 | ); |
| 151 | |
| 152 | // Merge patterns to run in a single preg_match call. |
| 153 | $video_patterns = '(' . implode( '|', $video_patterns ) . ')'; |
| 154 | |
| 155 | $is_video = preg_match( $video_patterns, $url ); |
| 156 | |
| 157 | // If the oEmbed is a video, wrap it in the responsive wrapper. |
| 158 | if ( false === $already_wrapped && 1 === $is_video ) { |
| 159 | return jetpack_responsive_videos_embed_html( $html ); |
| 160 | } |
| 161 | |
| 162 | return $html; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Remove the Jetpack Responsive video wrapper in embed blocks. |
| 167 | * |
| 168 | * @since 7.0.0 |
| 169 | * |
| 170 | * @deprecated 13.7 Moved to Classic Theme Helper package. |
| 171 | * |
| 172 | * @param string $block_content The block content about to be appended. |
| 173 | * @param array $block The full block, including name and attributes. |
| 174 | * |
| 175 | * @return string $block_content String of rendered HTML. |
| 176 | */ |
| 177 | function jetpack_responsive_videos_remove_wrap_oembed( $block_content, $block ) { |
| 178 | _deprecated_function( __FUNCTION__, 'jetpack-13.7' ); |
| 179 | |
| 180 | if ( |
| 181 | isset( $block['blockName'] ) |
| 182 | && ( |
| 183 | str_contains( $block['blockName'], 'core-embed' ) // pre-WP 5.6 embeds (multiple embed blocks starting with 'core-embed'). |
| 184 | || 'core/embed' === $block['blockName'] // WP 5.6 embed block format (single embed block w/ block variations). |
| 185 | ) |
| 186 | ) { |
| 187 | $block_content = preg_replace( '#<div class="jetpack-video-wrapper">(.*?)</div>#', '${1}', $block_content ); |
| 188 | } |
| 189 | |
| 190 | return $block_content; |
| 191 | } |
| 192 | } |
| 193 |