PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / theme-tools / responsive-videos.php

responsive-videos.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.2, at modules/theme-tools/responsive-videos.php

153 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // The customizer video widget wraps videos with a class of wp-video
46 // mejs as of 4.9 apparently resizes videos too which causes issues
47 // skip the video if it is wrapped in wp-video.
48 $video_widget_wrapper = 'class="wp-video"';
49
50 $mejs_wrapped = strpos( $html, $video_widget_wrapper );
51
52 // If this is a video widget wrapped by mejs, return the html.
53 if ( false !== $mejs_wrapped ) {
54 return $html;
55 }
56
57 if ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) {
58 wp_enqueue_script( 'jetpack-responsive-videos-script', plugins_url( 'responsive-videos/responsive-videos.js', __FILE__ ), array( 'jquery' ), '1.3', true );
59 } else {
60 wp_enqueue_script( 'jetpack-responsive-videos-min-script', plugins_url( 'responsive-videos/responsive-videos.min.js', __FILE__ ), array( 'jquery' ), '1.3', true );
61 }
62
63 // Enqueue CSS to ensure compatibility with all themes
64 wp_enqueue_style( 'jetpack-responsive-videos-style', plugins_url( 'responsive-videos/responsive-videos.css', __FILE__ ) );
65
66 return '<div class="jetpack-video-wrapper">' . $html . '</div>';
67 }
68
69 /**
70 * Check if oEmbed is a `$video_patterns` provider video before wrapping.
71 *
72 * @param mixed $html The cached HTML result, stored in post meta.
73 * @param string $url he attempted embed URL.
74 *
75 * @return string
76 */
77 function jetpack_responsive_videos_maybe_wrap_oembed( $html, $url = null ) {
78 if ( empty( $html ) || ! is_string( $html ) || ! $url ) {
79 return $html;
80 }
81
82 $jetpack_video_wrapper = '<div class="jetpack-video-wrapper">';
83
84 $already_wrapped = strpos( $html, $jetpack_video_wrapper );
85
86 // If the oEmbed has already been wrapped, return the html.
87 if ( false !== $already_wrapped ) {
88 return $html;
89 }
90
91 /**
92 * oEmbed Video Providers.
93 *
94 * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output.
95 *
96 * @module theme-tools
97 *
98 * @since 3.8.0
99 *
100 * @param array $video_patterns oEmbed video provider Regex patterns.
101 */
102 $video_patterns = apply_filters(
103 'jetpack_responsive_videos_oembed_videos',
104 array(
105 'https?://((m|www)\.)?youtube\.com/watch',
106 'https?://((m|www)\.)?youtube\.com/playlist',
107 'https?://youtu\.be/',
108 'https?://(.+\.)?vimeo\.com/',
109 'https?://(www\.)?dailymotion\.com/',
110 'https?://dai.ly/',
111 'https?://(www\.)?hulu\.com/watch/',
112 'https?://wordpress.tv/',
113 'https?://(www\.)?funnyordie\.com/videos/',
114 'https?://vine.co/v/',
115 'https?://(www\.)?collegehumor\.com/video/',
116 'https?://(www\.|embed\.)?ted\.com/talks/',
117 )
118 );
119
120 // Merge patterns to run in a single preg_match call.
121 $video_patterns = '(' . implode( '|', $video_patterns ) . ')';
122
123 $is_video = preg_match( $video_patterns, $url );
124
125 // If the oEmbed is a video, wrap it in the responsive wrapper.
126 if ( false === $already_wrapped && 1 === $is_video ) {
127 return jetpack_responsive_videos_embed_html( $html );
128 }
129
130 return $html;
131 }
132
133 /**
134 * Remove the Jetpack Responsive video wrapper in embed blocks.
135 *
136 * @since 7.0.0
137 *
138 * @param string $block_content The block content about to be appended.
139 * @param array $block The full block, including name and attributes.
140 *
141 * @return string $block_content String of rendered HTML.
142 */
143 function jetpack_responsive_videos_remove_wrap_oembed( $block_content, $block ) {
144 if (
145 isset( $block['blockName'] )
146 && false !== strpos( $block['blockName'], 'core-embed' )
147 ) {
148 $block_content = preg_replace( '#<div class="jetpack-video-wrapper">(.*?)</div>#', '${1}', $block_content );
149 }
150
151 return $block_content;
152 }
153