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