PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.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 / shortcodes / vimeo.php

vimeo.php in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/shortcodes/vimeo.php

331 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Vimeo Shortcode.
4 *
5 * Examples:
6 * [vimeo 141358]
7 * [vimeo http://vimeo.com/141358]
8 * [vimeo 141358 h=500&w=350]
9 * [vimeo id=141358 width=350 height=500]
10 *
11 * <iframe src="http://player.vimeo.com/video/18427511" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/18427511">Eskmo 'We Got More' (Official Video)</a> from <a href="http://vimeo.com/ninjatune">Ninja Tune</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
12 *
13 * @package Jetpack
14 */
15
16 /**
17 * Extract Vimeo ID from shortcode.
18 *
19 * @param array $atts Shortcode attributes.
20 */
21 function jetpack_shortcode_get_vimeo_id( $atts ) {
22 if ( isset( $atts[0] ) ) {
23 $atts[0] = trim( $atts[0], '=' );
24 $id = false;
25 if ( is_numeric( $atts[0] ) ) {
26 $id = (int) $atts[0];
27 } elseif ( preg_match( '|vimeo\.com/(\d+)/?$|i', $atts[0], $match ) ) {
28 $id = (int) $match[1];
29 } elseif ( preg_match( '|player\.vimeo\.com/video/(\d+)/?$|i', $atts[0], $match ) ) {
30 $id = (int) $match[1];
31 }
32
33 return $id;
34 }
35
36 return 0;
37 }
38
39 /**
40 * Convert a Vimeo shortcode into an embed code.
41 *
42 * @param array $atts An array of shortcode attributes.
43 *
44 * @return string The embed code for the Vimeo video.
45 */
46 function vimeo_shortcode( $atts ) {
47 global $content_width;
48
49 $attr = array_map(
50 'intval',
51 shortcode_atts(
52 array(
53 'id' => 0,
54 'width' => 0,
55 'height' => 0,
56 'autoplay' => 0,
57 'loop' => 0,
58 ),
59 $atts
60 )
61 );
62
63 if ( isset( $atts[0] ) ) {
64 $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts );
65 }
66
67 if ( ! $attr['id'] ) {
68 return '<!-- vimeo error: not a vimeo video -->';
69 }
70
71 // Handle old shortcode params such as h=500&w=350.
72 $params = shortcode_new_to_old_params( $atts );
73 $params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
74 parse_str( $params, $args );
75
76 $width = intval( $attr['width'] );
77 $height = intval( $attr['height'] );
78
79 // Support w and h argument as fallback.
80 if ( empty( $width ) && isset( $args['w'] ) ) {
81 $width = intval( $args['w'] );
82
83 if ( empty( $height ) && ! isset( $args['h'] ) ) {
84 // The case where w=300 is specified without h=200, otherwise $height
85 // will always equal the default of 300, no matter what w was set to.
86 $height = round( ( $width / 640 ) * 360 );
87 }
88 }
89
90 if ( empty( $height ) && isset( $args['h'] ) ) {
91 $height = (int) $args['h'];
92
93 if ( ! isset( $args['w'] ) ) {
94 $width = round( ( $height / 360 ) * 640 );
95 }
96 }
97
98 if ( ! $width && ! empty( $content_width ) ) {
99 $width = absint( $content_width );
100 }
101
102 // If setting the width with content_width has failed, defaulting.
103 if ( ! $width ) {
104 $width = 640;
105 }
106
107 if ( ! $height ) {
108 $height = round( ( $width / 640 ) * 360 );
109 }
110
111 /**
112 * Filter the Vimeo player width.
113 *
114 * @module shortcodes
115 *
116 * @since 3.4.0
117 *
118 * @param int $width Width of the Vimeo player in pixels.
119 */
120 $width = (int) apply_filters( 'vimeo_width', $width );
121
122 /**
123 * Filter the Vimeo player height.
124 *
125 * @module shortcodes
126 *
127 * @since 3.4.0
128 *
129 * @param int $height Height of the Vimeo player in pixels.
130 */
131 $height = (int) apply_filters( 'vimeo_height', $height );
132
133 $url = esc_url( 'https://player.vimeo.com/video/' . $attr['id'] );
134
135 // Handle autoplay and loop arguments.
136 if (
137 isset( $args['autoplay'] ) && '1' === $args['autoplay'] // Parsed from the embedded URL.
138 || $attr['autoplay'] // Parsed from shortcode arguments.
139 || in_array( 'autoplay', $atts, true ) // Catch the argument passed without a value.
140 ) {
141 $url = add_query_arg( 'autoplay', 1, $url );
142 }
143
144 if (
145 isset( $args['loop'] ) && '1' === $args['loop'] // Parsed from the embedded URL.
146 || $attr['loop'] // Parsed from shortcode arguments.
147 || in_array( 'loop', $atts, true ) // Catch the argument passed without a value.
148 ) {
149 $url = add_query_arg( 'loop', 1, $url );
150 }
151
152 $html = sprintf(
153 '<div class="embed-vimeo" style="text-align: center;"><iframe src="%1$s" width="%2$u" height="%3$u" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>',
154 esc_url( $url ),
155 esc_attr( $width ),
156 esc_attr( $height )
157 );
158
159 /**
160 * Filter the Vimeo player HTML.
161 *
162 * @module shortcodes
163 *
164 * @since 1.2.3
165 *
166 * @param string $html Embedded Vimeo player HTML.
167 */
168 $html = apply_filters( 'video_embed_html', $html );
169
170 return $html;
171 }
172
173 add_shortcode( 'vimeo', 'vimeo_shortcode' );
174
175 /**
176 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
177 *
178 * @since 3.9
179 *
180 * @param array $matches Regex partial matches against the URL passed.
181 * @param array $attr Attributes received in embed response.
182 * @param array $url Requested URL to be embedded.
183 *
184 * @return string Return output of Vimeo shortcode with the proper markup.
185 */
186 function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
187 return vimeo_shortcode( array( $url ) );
188 }
189
190 /**
191 * For bare URLs on their own line of the form
192 * http://vimeo.com/12345
193 *
194 * @since 3.9
195 *
196 * @uses wpcom_vimeo_embed_url
197 */
198 function wpcom_vimeo_embed_url_init() {
199 wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(.+\.)?vimeo\.com/#i', 'wpcom_vimeo_embed_url' );
200 }
201
202 /*
203 * Register handler to modify Vimeo embeds using Jetpack's shortcode output.
204 * This does not happen on WordPress.com, since embeds are handled by core there.
205 */
206 if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
207 add_action( 'init', 'wpcom_vimeo_embed_url_init' );
208 }
209
210 /**
211 * Transform a Vimeo embed iFrame into a Vimeo shortcode.
212 *
213 * @param string $content Post content.
214 */
215 function vimeo_embed_to_shortcode( $content ) {
216 if ( ! is_string( $content ) || false === stripos( $content, 'player.vimeo.com/video/' ) ) {
217 return $content;
218 }
219
220 $regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
221 $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
222
223 foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
224 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
225 continue;
226 }
227
228 foreach ( $matches as $match ) {
229 $id = (int) $match[2];
230
231 $params = $match[3];
232
233 if ( 'regexp_ent' === $reg ) {
234 $params = html_entity_decode( $params );
235 }
236
237 $params = wp_kses_hair( $params, array( 'http' ) );
238
239 $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
240 $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
241
242 $wh = '';
243 if ( $width && $height ) {
244 $wh = ' w=' . $width . ' h=' . $height;
245 }
246
247 $shortcode = '[vimeo ' . $id . $wh . ']';
248 $content = str_replace( $match[0], $shortcode, $content );
249 }
250 }
251
252 return $content;
253 }
254 add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
255
256 /**
257 * Replaces shortcodes and plain-text URLs to Vimeo videos with Vimeo embeds.
258 * Covers shortcode usage [vimeo 1234] | [vimeo https://vimeo.com/1234] | [vimeo http://vimeo.com/1234]
259 * Or plain text URLs https://vimeo.com/1234 | vimeo.com/1234 | //vimeo.com/1234
260 * Links are left intact.
261 *
262 * @since 3.7.0
263 * @since 3.9.5 One regular expression matches shortcodes and plain URLs.
264 *
265 * @param string $content HTML content.
266 *
267 * @return string The content with embeds instead of URLs
268 */
269 function vimeo_link( $content ) {
270 /**
271 * [vimeo 12345]
272 * [vimeo http://vimeo.com/12345]
273 */
274 $shortcode = '(?:\[vimeo\s+[^0-9]*)([0-9]+)(?:\])';
275
276 /**
277 * Regex to look for a Vimeo link.
278 *
279 * - http://vimeo.com/12345
280 * - https://vimeo.com/12345
281 * - //vimeo.com/12345
282 * - vimeo.com/some/descender/12345
283 *
284 * Should not capture inside HTML attributes
285 * [Not] <a href="vimeo.com/12345">Cool Video</a>
286 * [Not] <a href="https://vimeo.com/12345">vimeo.com/12345</a>
287 *
288 * Could erroneously capture:
289 * <a href="some.link/maybe/even/vimeo">This video (vimeo.com/12345) is teh cat's meow!</a>
290 */
291 $plain_url = "(?:[^'\">]?\/?(?:https?:\/\/)?vimeo\.com[^0-9]+)([0-9]+)(?:[^'\"0-9<]|$)";
292
293 return jetpack_preg_replace_callback_outside_tags(
294 sprintf( '#%s|%s#i', $shortcode, $plain_url ),
295 'vimeo_link_callback',
296 $content,
297 'vimeo'
298 );
299 }
300
301 /**
302 * Callback function for the regex that replaces Vimeo URLs with Vimeo embeds.
303 *
304 * @since 3.7.0
305 *
306 * @param array $matches An array containing a Vimeo URL.
307 * @return string The Vimeo HTML embed code.
308 */
309 function vimeo_link_callback( $matches ) {
310 $id = isset( $matches[2] ) ? $matches[2] : $matches[1];
311 if ( isset( $id ) && ctype_digit( $id ) ) {
312 return "\n" . vimeo_shortcode( array( 'id' => $id ) ) . "\n";
313 }
314 return $matches[0];
315 }
316
317 if (
318 ! is_admin()
319 /** This filter is documented in modules/shortcodes/youtube.php */
320 && apply_filters( 'jetpack_comments_allow_oembed', true )
321 // No need for this on WordPress.com, this is done for multiple shortcodes at a time there.
322 && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
323 ) {
324 /*
325 * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
326 * so the iframe gets filtered out.
327 * Higher priority because we need it before auto-link and autop get to it
328 */
329 add_filter( 'comment_text', 'vimeo_link', 1 );
330 }
331