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 / dailymotion.php

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

358 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Dailymotion code
4 *
5 * @package Jetpack
6 */
7
8 /**
9 * Original codes:
10 *
11 * <embed height="270" type="application/x-shockwave-flash" width="480" src="http&#58;//www.dailymotion.com/swf/video/xekmrq?additionalInfos=0" wmode="opaque" pluginspage="http&#58;//www.macromedia.com/go/getflashplayer" allowscriptaccess="never" allownetworking="internal" />
12 *
13 * <object width="480" height="240"><param name="movie" value="http://www.dailymotion.com/swf/video/xen4ms_ghinzu-cold-love-mirror-mirror_music?additionalInfos=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>
14 * <embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/xen4ms_ghinzu-cold-love-mirror-mirror_music?additionalInfos=0" width="480" height="240" allowfullscreen="true" allowscriptaccess="always"></embed>
15 * </object><br /><b><a href="http://www.dailymotion.com/video/xen4ms_ghinzu-cold-love-mirror-mirror_music">Ghinzu - Cold Love (Mirror Mirror)</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/GhinzuTV">GhinzuTV</a>. - <a href="http://www.dailymotion.com/us/channel/music">Watch more music videos, in HD!</a></i>
16 *
17 * Code as of 01.01.11:
18 * <object width="560" height="421"><param name="movie" value="http://www.dailymotion.com/swf/video/xaose5?width=560&theme=denim&foreground=%2392ADE0&highlight=%23A2ACBF&background=%23202226&start=&animatedTitle=&iframe=0&additionalInfos=0&autoPlay=0&hideInfos=0"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed type="application/x-shockwave-flash" src="http://www.dailymotion.com/swf/video/xaose5?width=560&theme=denim&foreground=%2392ADE0&highlight=%23A2ACBF&background=%23202226&start=&animatedTitle=&iframe=0&additionalInfos=0&autoPlay=0&hideInfos=0" width="560" height="421" allowfullscreen="true" allowscriptaccess="always"></embed></object><br /><b><a href="http://www.dailymotion.com/video/x29zm17_funny-videos-of-cats-and-babies-compilation-2015_fun">Funny cats and babies!</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/GilLavie">GilLavie</a>. - <a target="_self" href="http://www.dailymotion.com/channel/funny/featured/1">Find more funny videos.</a></i>
19 * movie param enforces anti-xss protection
20 *
21 * Scroll down for the new <iframe> embed code handler.
22 *
23 * @param string $content Post content.
24 */
25 function dailymotion_embed_to_shortcode( $content ) {
26 if ( ! is_string( $content ) || false === stripos( $content, 'www.dailymotion.com/swf/' ) ) {
27 return $content;
28 }
29
30 $regexp = '!<object.*>\s*(<param.*></param>\s*)*<embed((?:\s+\w+="[^"]*")*)\s+src="http(?:\:|&#0*58;)//(www\.dailymotion\.com/swf/[^"]*)"((?:\s+\w+="[^"]*")*)\s*(?:/>|>\s*</embed>)\s*</object><br /><b><a .*>.*</a></b><br /><i>.*</i>!';
31 $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
32
33 foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
34 if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
35 continue;
36 }
37
38 foreach ( $matches as $match ) {
39 $src = html_entity_decode( $match[3] );
40 $params = $match[2] . $match[4];
41
42 if ( 'regexp_ent' === $reg ) {
43 $src = html_entity_decode( $src );
44 $params = html_entity_decode( $params );
45 }
46
47 $params = wp_kses_hair( $params, array( 'http' ) );
48
49 if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' !== $params['type']['value'] ) {
50 continue;
51 }
52
53 $id = basename( substr( $src, strlen( 'www.dailymotion.com/swf' ) ) );
54 $id = preg_replace( '/[^a-z0-9].*$/is', '', $id );
55
56 $content = str_replace( $match[0], "[dailymotion id=$id]", $content );
57 /** This action is documented in modules/shortcodes/youtube.php */
58 do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $id );
59 }
60 }
61 return $content;
62 }
63 add_filter( 'pre_kses', 'dailymotion_embed_to_shortcode' );
64
65 /**
66 * DailyMotion shortcode
67 *
68 * The documented shortcode is:
69 * [dailymotion id=x8oma9]
70 *
71 * Possibilities, according to the old parsing regexp:
72 * [dailymotion x8oma9]
73 * [dailymotion=x8oma9]
74 *
75 * Hypothetical option, according to the old shortcode function is
76 * [dailymotion id=1&title=2&user=3&video=4]
77 *
78 * The new style is now:
79 * [dailymotion id=x8oma9 title=2 user=3 video=4]
80 *
81 * Supported parameters for player customization: width, height,
82 * autoplay, endscreen-enable, mute, sharing-enabled, start, subtitles-default,
83 * ui-highlight, ui-logo, ui-start-screen-info, ui-theme
84 * see https://developer.dailymotion.com/player#player-parameters
85 *
86 * @todo: Update code to sniff for iframe embeds and convert those to shortcodes.
87 *
88 * @param array $atts Shortcode attributes.
89 *
90 * @return string html
91 */
92 function dailymotion_shortcode( $atts ) {
93 global $content_width;
94
95 if ( isset( $atts[0] ) ) {
96 $id = ltrim( $atts[0], '=' );
97 $atts['id'] = $id;
98
99 } else {
100 $params = shortcode_new_to_old_params( $atts );
101 parse_str( $params, $atts_new );
102
103 foreach ( $atts_new as $k => $v ) {
104 $atts[ $k ] = $v;
105 }
106 }
107
108 $atts = shortcode_atts(
109 array(
110 'id' => '', // string.
111 'width' => '', // int.
112 'height' => '', // int.
113 'title' => '', // string.
114 'user' => '', // string.
115 'video' => '', // string.
116 'autoplay' => 0, // int.
117 'endscreen-enable' => 1, // int.
118 'mute' => 0, // int.
119 'sharing-enable' => 1, // int.
120 'start' => '', // int.
121 'subtitles-default' => '', // string.
122 'ui-highlight' => '', // string.
123 'ui-logo' => 1, // int.
124 'ui-start-screen-info' => 0, // int.
125 'ui-theme' => '', // string.
126 ),
127 $atts,
128 'dailymotion'
129 );
130
131 if ( isset( $atts['id'] ) && ! empty( $atts['id'] ) ) {
132 $id = rawurlencode( $atts['id'] );
133 } else {
134 return '<!--Dailymotion error: bad or missing ID-->';
135 }
136
137 /*set width and height using provided parameters if any */
138 $width = isset( $atts['width'] ) ? intval( $atts['width'] ) : 0;
139 $height = isset( $atts['height'] ) ? intval( $atts['height'] ) : 0;
140
141 if ( ! $width && ! $height ) {
142 if ( ! empty( $content_width ) ) {
143 $width = absint( $content_width );
144 } else {
145 $width = 425;
146 }
147 $height = $width / 425 * 334;
148 } elseif ( ! $height ) {
149 $height = $width / 425 * 334;
150 } elseif ( ! $width ) {
151 $width = $height / 334 * 425;
152 }
153
154 /**
155 * Let's add parameters if needed.
156 *
157 * @see https://developer.dailymotion.com/player
158 */
159 $player_params = array();
160
161 if ( isset( $atts['autoplay'] ) && '1' === $atts['autoplay'] ) {
162 $player_params['autoplay'] = '1';
163 }
164 if ( isset( $atts['endscreen-enable'] ) && '0' === $atts['endscreen-enable'] ) {
165 $player_params['endscreen-enable'] = '0';
166 }
167 if ( isset( $atts['mute'] ) && '1' === $atts['mute'] ) {
168 $player_params['mute'] = '1';
169 }
170 if ( isset( $atts['sharing-enable'] ) && '0' === $atts['sharing-enable'] ) {
171 $player_params['sharing-enable'] = '0';
172 }
173 if ( isset( $atts['start'] ) && ! empty( $atts['start'] ) ) {
174 $player_params['start'] = abs( intval( $atts['start'] ) );
175 }
176 if ( isset( $atts['subtitles-default'] ) && ! empty( $atts['subtitles-default'] ) ) {
177 $player_params['subtitles-default'] = esc_attr( $atts['subtitles-default'] );
178 }
179 if ( isset( $atts['ui-highlight'] ) && ! empty( $atts['ui-highlight'] ) ) {
180 $player_params['ui-highlight'] = esc_attr( $atts['ui-highlight'] );
181 }
182 if ( isset( $atts['ui-logo'] ) && '0' === $atts['ui-logo'] ) {
183 $player_params['ui-logo'] = '0';
184 }
185 if ( isset( $atts['ui-start-screen-info'] ) && '0' === $atts['ui-start-screen-info'] ) {
186 $player_params['ui-start-screen-info'] = '0';
187 }
188 if ( isset( $atts['ui-theme'] ) && in_array( strtolower( $atts['ui-theme'] ), array( 'dark', 'light' ), true ) ) {
189 $player_params['ui-theme'] = esc_attr( $atts['ui-theme'] );
190 }
191
192 // Add those parameters to the Video URL.
193 $video_url = add_query_arg(
194 $player_params,
195 'https://www.dailymotion.com/embed/video/' . $id
196 );
197
198 $output = '';
199
200 if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) {
201 $output .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $video_url ) . '" style="border:0;" allowfullscreen></iframe>';
202
203 $video = preg_replace( '/[^-a-z0-9_]/i', '', $atts['video'] );
204 $title = wp_kses( $atts['title'], array() );
205 if (
206 array_key_exists( 'video', $atts )
207 && $video
208 && array_key_exists( 'title', $atts )
209 && $title
210 ) {
211 $output .= '<br /><strong><a href="' . esc_url( 'http://www.dailymotion.com/video/' . $video ) . '" target="_blank">' . esc_html( $title ) . '</a></strong>';
212 }
213
214 $user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] );
215 if ( array_key_exists( 'user', $atts ) && $user ) {
216 /* translators: %s is a Dailymotion user name */
217 $output .= '<br /><em>' . wp_kses(
218 sprintf(
219 /* Translators: placeholder is a Dailymotion username, linking to a Dailymotion profile page. */
220 __( 'Uploaded by %s', 'jetpack' ),
221 '<a href="' . esc_url( 'http://www.dailymotion.com/' . $user ) . '" target="_blank">' . esc_html( $user ) . '</a>'
222 ),
223 array(
224 'a' => array(
225 'href' => true,
226 'target' => true,
227 ),
228 )
229 ) . '</em>';
230 }
231 }
232
233 return $output;
234 }
235 add_shortcode( 'dailymotion', 'dailymotion_shortcode' );
236
237 /**
238 * DailyMotion Channel Shortcode
239 *
240 * Examples:
241 * [dailymotion-channel user=MatthewDominick]
242 * [dailymotion-channel user=MatthewDominick type=grid] (supports grid, carousel, badge/default)
243 *
244 * @param array $atts Shortcode attributes.
245 */
246 function dailymotion_channel_shortcode( $atts ) {
247 $username = $atts['user'];
248
249 switch ( $atts['type'] ) {
250 case 'grid':
251 $channel_iframe = '<iframe width="300px" height="264px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=grid' ) . '"></iframe>';
252 break;
253 case 'carousel':
254 $channel_iframe = '<iframe width="300px" height="360px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=carousel' ) . '"></iframe>';
255 break;
256 default:
257 $channel_iframe = '<iframe width="300px" height="78px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username ) . '"></iframe>';
258 }
259
260 return $channel_iframe;
261 }
262 add_shortcode( 'dailymotion-channel', 'dailymotion_channel_shortcode' );
263
264 /**
265 * Embed Reversal for Badge/Channel
266 *
267 * @param string $content Post content.
268 */
269 function dailymotion_channel_reversal( $content ) {
270 if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/badge/' ) ) {
271 return $content;
272 }
273
274 /*
275 * Sample embed code:
276 * <iframe width="300px" height="360px" scrolling="no" frameborder="0" src="http://www.dailymotion.com/badge/user/Dailymotion?type=carousel"></iframe>
277 */
278
279 $regexes = array();
280
281 $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "[^>]*+></iframe>#ix';
282
283 // Let's play nice with the visual editor too.
284 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;&lt;/iframe&gt;#ix';
285
286 foreach ( $regexes as $regex ) {
287 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
288 continue;
289 }
290
291 foreach ( $matches as $match ) {
292 $url_pieces = wp_parse_url( $match[1] );
293
294 if ( 'type=carousel' === $url_pieces['query'] ) {
295 $type = 'carousel';
296 } elseif ( 'type=grid' === $url_pieces['query'] ) {
297 $type = 'grid';
298 } else {
299 $type = 'badge';
300 }
301
302 $shortcode = '[dailymotion-channel user=' . esc_attr( $url_pieces['path'] ) . ' type=' . esc_attr( $type ) . ']';
303 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
304 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
305 }
306 }
307
308 return $content;
309 }
310 add_filter( 'pre_kses', 'dailymotion_channel_reversal' );
311
312 /**
313 * Dailymotion Embed Reversal (with new iframe code as of 17.09.2014)
314 *
315 * Converts a generic HTML embed code from Dailymotion into an
316 * oEmbeddable URL.
317 *
318 * @param string $content Post content.
319 */
320 function jetpack_dailymotion_embed_reversal( $content ) {
321 if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/embed' ) ) {
322 return $content;
323 }
324
325 /*
326 * Sample embed code as of Sep 17th 2014:
327 * <iframe frameborder="0" width="480" height="270" src="//www.dailymotion.com/embed/video/x25x71x" allowfullscreen></iframe><br /><a href="http://www.dailymotion.com/video/x25x71x_dog-with-legs-in-casts-learns-how-to-enter-the-front-door_animals" target="_blank">Dog with legs in casts learns how to enter the...</a> <i>by <a href="http://www.dailymotion.com/videobash" target="_blank">videobash</a></i>
328 */
329 $regexes = array();
330
331 // I'm Konstantin and I love regex.
332 $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "[^>]*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+
333 (?: <a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+ )?
334 (?: <i>.*?<a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+</i> )?#ix';
335
336 $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;\s*+&lt;/iframe&gt;\s*+(?:&lt;br\s*+/&gt;)?\s*+
337 (?: &lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+ )?
338 (?: &lt;i&gt;.*?&lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+&lt;/i&gt; )?#ix';
339
340 foreach ( $regexes as $regex ) {
341 if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
342 continue;
343 }
344
345 foreach ( $matches as $match ) {
346 $url = esc_url( sprintf( 'https://dailymotion.com/video/%s', $match[1] ) );
347 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
348 $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
349
350 /** This action is documented in modules/shortcodes/youtube.php */
351 do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $url );
352 }
353 }
354
355 return $content;
356 }
357 add_filter( 'pre_kses', 'jetpack_dailymotion_embed_reversal' );
358