Completed
Push — master-stable ( 31b82b...bc7a45 )
by
unknown
49:20 queued 40:50
created

modules/shortcodes/vimeo.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
		return $id;
23
	}
24
	return 0;
25
}
26
27
/**
28
 * Convert a Vimeo shortcode into an embed code.
29
 *
30
 * @param array $atts An array of shortcode attributes.
31
 * @return string The embed code for the Vimeo video.
32
 */
33
function vimeo_shortcode( $atts ) {
34
	global $content_width;
35
36
	extract( array_map( 'intval', shortcode_atts( array(
37
		'id'       => 0,
38
		'width'    => 400,
39
		'height'   => 300,
40
		'autoplay' => 0,
41
		'loop'     => 0,
42
	), $atts, 'vimeo' ) ) );
43
44
	if ( isset( $atts[0] ) ) {
45
		$id = jetpack_shortcode_get_vimeo_id( $atts );
46
	}
47
48
	if ( ! $id ) return "<!-- vimeo error: not a vimeo video -->";
49
50
	// [vimeo 141358 h=500&w=350]
51
	$params = shortcode_new_to_old_params( $atts ); // h=500&w=350
52
	$params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
53
	parse_str( $params, $args );
54
55 View Code Duplication
	if ( isset( $args['w'] ) ) {
56
		$width = (int) $args['w'];
57
58
		if ( ! isset( $args['h'] ) ) {
59
			// The case where w=300 is specified without h=200, otherwise $height
60
			// will always equal the default of 300, no matter what w was set to.
61
			$height = round( ( $width / 640 ) * 360 );
62
		}
63
	}
64
65 View Code Duplication
	if ( isset( $args['h'] ) ) {
66
		$height = (int) $args['h'];
67
68
		if ( ! isset( $args['w'] ) ) {
69
			$width = round( ( $height / 360 ) * 640 );
70
		}
71
	}
72
73
	if ( ! $width ) {
74
		$width = absint( $content_width );
75
	}
76
77
	if ( ! $height ) {
78
		$height = round( ( $width / 640 ) * 360 );
79
	}
80
81
	/**
82
	 * Filter the Vimeo player width.
83
	 *
84
	 * @module shortcodes
85
	 *
86
	 * @since 3.4.0
87
	 *
88
	 * @param int $width Width of the Vimeo player in pixels.
89
	 */
90
	$width = (int) apply_filters( 'vimeo_width', $width );
91
92
	/**
93
	 * Filter the Vimeo player height.
94
	 *
95
	 * @module shortcodes
96
	 *
97
	 * @since 3.4.0
98
	 *
99
	 * @param int $height Height of the Vimeo player in pixels.
100
	 */
101
	$height = (int) apply_filters( 'vimeo_height', $height );
102
103
	$url = esc_url( "https://player.vimeo.com/video/$id" );
104
105
	// $args['autoplay'] is parsed from the embedded url.
106
	// $autoplay is parsed from shortcode arguments.
107
	// in_array( 'autoplay', $atts ) catches the argument passed without a value.
108 View Code Duplication
	if ( ! empty( $args['autoplay'] ) || ! empty( $autoplay ) || in_array( 'autoplay', $atts ) ) {
109
		$url = add_query_arg( 'autoplay', 1, $url );
110
	}
111
112 View Code Duplication
	if ( ! empty( $args['loop'] ) || ! empty( $loop ) || in_array( 'loop', $atts ) ) {
113
		$url = add_query_arg( 'loop', 1, $url );
114
	}
115
116
	$html = sprintf( '<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>', esc_url( $url ), $width, $height );
117
118
	/**
119
	 * Filter the Vimeo player HTML.
120
	 *
121
	 * @module shortcodes
122
	 *
123
	 * @since 1.2.3
124
	 *
125
	 * @param string $html Embedded Vimeo player HTML.
126
	 */
127
	$html = apply_filters( 'video_embed_html', $html );
128
129
	return $html;
130
}
131
132
add_shortcode( 'vimeo', 'vimeo_shortcode' );
133
134
/**
135
 * Callback to modify output of embedded Vimeo video using Jetpack's shortcode.
136
 *
137
 * @since 3.9
138
 *
139
 * @param array $matches Regex partial matches against the URL passed.
140
 * @param array $attr Attributes received in embed response
141
 * @param array $url Requested URL to be embedded
142
 *
143
 * @return string Return output of Vimeo shortcode with the proper markup.
144
 */
145
function wpcom_vimeo_embed_url( $matches, $attr, $url ) {
0 ignored issues
show
The parameter $matches is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $attr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
146
	return vimeo_shortcode( array( $url ) );
147
}
148
149
/**
150
 * For bare URLs on their own line of the form
151
 * http://vimeo.com/12345
152
 *
153
 * @since 3.9
154
 *
155
 * @uses wpcom_vimeo_embed_url
156
 */
157
function wpcom_vimeo_embed_url_init() {
158
	wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(.+\.)?vimeo\.com/#i', 'wpcom_vimeo_embed_url' );
159
}
160
161
// Register handler to modify Vimeo embeds using Jetpack's shortcode output.
162
add_action( 'init', 'wpcom_vimeo_embed_url_init' );
163
164
function vimeo_embed_to_shortcode( $content ) {
165
	if ( false === stripos( $content, 'player.vimeo.com/video/' ) )
166
		return $content;
167
168
	$regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
169
	$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
170
171
	foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
172
		if ( !preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) )
173
			continue;
174
175
		foreach ( $matches as $match ) {
176
			$id = (int) $match[2];
177
178
			$params = $match[3];
179
180
			if ( 'regexp_ent' == $reg )
181
				$params = html_entity_decode( $params );
182
183
			$params = wp_kses_hair( $params, array( 'http' ) );
184
185
			$width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
186
			$height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
187
188
			$wh = '';
189
			if ( $width && $height )
190
				$wh = ' w=' . $width . ' h=' . $height;
191
192
			$shortcode = '[vimeo ' . $id . $wh . ']';
193
			$content = str_replace( $match[0], $shortcode, $content );
194
		}
195
	}
196
197
	return $content;
198
}
199
200
add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
201
202
/**
203
 * Replaces plain-text links to Vimeo videos with Vimeo embeds.
204
 *
205
 * @since 3.7.0
206
 *
207
 * @param string $content HTML content
208
 * @return string The content with embeds instead of URLs
209
 */
210
function vimeo_link( $content ) {
211
	return preg_replace_callback( '#https://vimeo.com/\d*#', 'vimeo_link_callback', $content );
212
}
213
214
/**
215
 * Callback function for the regex that replaces Vimeo URLs with Vimeo embeds.
216
 *
217
 * @since 3.7.0
218
 *
219
 * @param array $matches An array containing a Vimeo URL.
220
 * @return string THe Vimeo HTML embed code.
221
 */
222
function vimeo_link_callback( $matches ) {
223
	// Grab the Vimeo ID from the URL
224
	if ( preg_match( '|vimeo\.com/(\d+)/?$|i', $matches[0], $match ) ) {
225
		$id = (int) $match[1];
226
	}
227
228
	// Pass that ID to the Vimeo shortcode function.
229
	if ( $id ) {
230
		$atts = array( 'id' => $id );
231
	}
232
	return "\n" . vimeo_shortcode( $atts ) . "\n";
233
}
234
235
/** This filter is documented in modules/shortcodes/youtube.php */
236 View Code Duplication
if ( apply_filters( 'jetpack_comments_allow_oembed', get_option('embed_autourls') ) ) {
237
	// We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out.
238
	if ( ! is_admin() ) {
239
		// Higher priority because we need it before auto-link and autop get to it
240
		add_filter( 'comment_text', 'vimeo_link', 1 );
241
	}
242
}
243