Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.89% covered (warning)
53.89%
97 / 180
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
dailymotion_embed_to_shortcode
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
dailymotion_shortcode
91.51% covered (success)
91.51%
97 / 106
0.00% covered (danger)
0.00%
0 / 1
45.18
dailymotion_channel_shortcode
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
dailymotion_channel_reversal
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
72
jetpack_dailymotion_embed_reversal
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Dailymotion code
4 *
5 * @package automattic/jetpack
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Original codes:
14 *
15 * <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" />
16 *
17 * <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>
18 *  <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>
19 * </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>
20 *
21 * Code as of 01.01.11:
22 * <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>
23 * movie param enforces anti-xss protection
24 *
25 * Scroll down for the new <iframe> embed code handler.
26 *
27 * @param string $content Post content.
28 */
29function dailymotion_embed_to_shortcode( $content ) {
30    if ( ! is_string( $content ) || false === stripos( $content, 'www.dailymotion.com/swf/' ) ) {
31        return $content;
32    }
33
34    $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>!';
35    $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
36
37    foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
38        if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
39            continue;
40        }
41
42        foreach ( $matches as $match ) {
43            $src    = html_entity_decode( $match[3], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
44            $params = $match[2] . $match[4];
45
46            if ( 'regexp_ent' === $reg ) {
47                $src    = html_entity_decode( $src, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
48                $params = html_entity_decode( $params, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 );
49            }
50
51            $params = wp_kses_hair( $params, array( 'http' ) );
52
53            if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' !== $params['type']['value'] ) {
54                continue;
55            }
56
57            $id = basename( substr( $src, strlen( 'www.dailymotion.com/swf' ) ) );
58            $id = preg_replace( '/[^a-z0-9].*$/is', '', $id );
59
60            $content = str_replace( $match[0], "[dailymotion id=$id]", $content );
61            /** This action is documented in modules/shortcodes/youtube.php */
62            do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $id );
63        }
64    }
65    return $content;
66}
67
68/**
69 * DailyMotion shortcode
70 *
71 * The documented shortcode is:
72 * [dailymotion id=x8oma9]
73 *
74 * Possibilities, according to the old parsing regexp:
75 * [dailymotion x8oma9]
76 * [dailymotion=x8oma9]
77 *
78 * Hypothetical option, according to the old shortcode function is
79 * [dailymotion id=1&title=2&user=3&video=4]
80 *
81 * The new style is now:
82 * [dailymotion id=x8oma9 title=2 user=3 video=4]
83 *
84 * Supported parameters for player customization: width, height,
85 * autoplay, endscreen-enable, mute, sharing-enabled, start, subtitles-default,
86 * ui-highlight, ui-logo, ui-start-screen-info, ui-theme
87 * see https://developer.dailymotion.com/player#player-parameters
88 *
89 * @todo: Update code to sniff for iframe embeds and convert those to shortcodes.
90 *
91 * @param array $atts Shortcode attributes.
92 *
93 * @return string html
94 */
95function dailymotion_shortcode( $atts ) {
96    global $content_width;
97
98    if ( isset( $atts[0] ) ) {
99        $id         = ltrim( $atts[0], '=' );
100        $atts['id'] = $id;
101
102    } else {
103        $params = shortcode_new_to_old_params( $atts );
104        parse_str( $params, $atts_new );
105
106        foreach ( $atts_new as $k => $v ) {
107            $atts[ $k ] = $v;
108        }
109    }
110
111    $atts = shortcode_atts(
112        array(
113            'id'                   => '', // string.
114            'width'                => '', // int.
115            'height'               => '', // int.
116            'title'                => '', // string.
117            'user'                 => '', // string.
118            'video'                => '', // string.
119            'autoplay'             => 0,  // int.
120            'endscreen-enable'     => 1,  // int.
121            'mute'                 => 0,  // int.
122            'sharing-enable'       => 1,  // int.
123            'start'                => '', // int.
124            'subtitles-default'    => '', // string.
125            'ui-highlight'         => '', // string.
126            'ui-logo'              => 1,  // int.
127            'ui-start-screen-info' => 0,  // int.
128            'ui-theme'             => '', // string.
129        ),
130        $atts,
131        'dailymotion'
132    );
133
134    if ( isset( $atts['id'] ) && ! empty( $atts['id'] ) ) {
135        $id = rawurlencode( $atts['id'] );
136    } else {
137        return '<!--Dailymotion error: bad or missing ID-->';
138    }
139
140    /*set width and height using provided parameters if any */
141    $width  = isset( $atts['width'] ) ? (int) $atts['width'] : 0;
142    $height = isset( $atts['height'] ) ? (int) $atts['height'] : 0;
143
144    if ( ! $width && ! $height ) {
145        if ( ! empty( $content_width ) ) {
146            $width = absint( $content_width );
147        } else {
148            $width = 425;
149        }
150        $height = $width / 425 * 334;
151    } elseif ( ! $height ) {
152        $height = $width / 425 * 334;
153    } elseif ( ! $width ) {
154        $width = $height / 334 * 425;
155    }
156
157    if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
158        return sprintf(
159            '<amp-dailymotion data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-dailymotion>',
160            esc_attr( $id ),
161            absint( $width ),
162            absint( $height )
163        );
164    }
165
166    /**
167     * Let's add parameters if needed.
168     *
169     * @see https://developer.dailymotion.com/player
170     */
171    $player_params = array();
172
173    if ( isset( $atts['autoplay'] ) && '1' === $atts['autoplay'] ) {
174        $player_params['autoplay'] = '1';
175    }
176    if ( isset( $atts['endscreen-enable'] ) && '0' === $atts['endscreen-enable'] ) {
177        $player_params['endscreen-enable'] = '0';
178    }
179    if ( isset( $atts['mute'] ) && '1' === $atts['mute'] ) {
180        $player_params['mute'] = '1';
181    }
182    if ( isset( $atts['sharing-enable'] ) && '0' === $atts['sharing-enable'] ) {
183        $player_params['sharing-enable'] = '0';
184    }
185    if ( isset( $atts['start'] ) && ! empty( $atts['start'] ) ) {
186        $player_params['start'] = abs( (int) $atts['start'] );
187    }
188    if ( isset( $atts['subtitles-default'] ) && ! empty( $atts['subtitles-default'] ) ) {
189        $player_params['subtitles-default'] = esc_attr( $atts['subtitles-default'] );
190    }
191    if ( isset( $atts['ui-highlight'] ) && ! empty( $atts['ui-highlight'] ) ) {
192        $player_params['ui-highlight'] = esc_attr( $atts['ui-highlight'] );
193    }
194    if ( isset( $atts['ui-logo'] ) && '0' === $atts['ui-logo'] ) {
195        $player_params['ui-logo'] = '0';
196    }
197    if ( isset( $atts['ui-start-screen-info'] ) && '0' === $atts['ui-start-screen-info'] ) {
198        $player_params['ui-start-screen-info'] = '0';
199    }
200    if ( isset( $atts['ui-theme'] ) && in_array( strtolower( $atts['ui-theme'] ), array( 'dark', 'light' ), true ) ) {
201        $player_params['ui-theme'] = esc_attr( $atts['ui-theme'] );
202    }
203
204    // Add those parameters to the Video URL.
205    $video_url = add_query_arg(
206        $player_params,
207        'https://www.dailymotion.com/embed/video/' . $id
208    );
209
210    $output = '';
211
212    if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) {
213        $output .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $video_url ) . '" style="border:0;" allowfullscreen></iframe>';
214
215        $video = preg_replace( '/[^-a-z0-9_]/i', '', $atts['video'] );
216        $title = wp_kses( $atts['title'], array() );
217        if (
218            array_key_exists( 'video', $atts )
219            && $video
220            && array_key_exists( 'title', $atts )
221            && $title
222        ) {
223            $output .= '<br /><strong><a href="' . esc_url( 'https://www.dailymotion.com/video/' . $video ) . '" target="_blank">' . esc_html( $title ) . '</a></strong>';
224        }
225
226        $user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] );
227        if ( array_key_exists( 'user', $atts ) && $user ) {
228            /* translators: %s is a Dailymotion user name */
229            $output .= '<br /><em>' . wp_kses(
230                sprintf(
231                    /* Translators: placeholder is a Dailymotion username, linking to a Dailymotion profile page. */
232                    __( 'Uploaded by %s', 'jetpack' ),
233                    '<a href="' . esc_url( 'https://www.dailymotion.com/' . $user ) . '" target="_blank">' . esc_html( $user ) . '</a>'
234                ),
235                array(
236                    'a' => array(
237                        'href'   => true,
238                        'target' => true,
239                    ),
240                )
241            ) . '</em>';
242        }
243    }
244
245    /**
246     * Calypso Helper
247     *
248     * Makes shortcode output responsive to the location it is loaded:
249     * Notifications, Reader, Email
250     */
251    if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
252        require_once WP_CONTENT_DIR . '/lib/display-context.php';
253        $context = A8C\Display_Context\get_current_context();
254
255        // Notifications.
256        if ( A8C\Display_Context\NOTIFICATIONS === $context ) {
257            return sprintf(
258                '<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>',
259                esc_url( 'https://www.dailymotion.com/video/' . $id )
260            );
261        }
262    }
263
264    return $output;
265}
266add_shortcode( 'dailymotion', 'dailymotion_shortcode' );
267
268/**
269 * DailyMotion Channel Shortcode
270 *
271 * Examples:
272 * [dailymotion-channel user=MatthewDominick]
273 * [dailymotion-channel user=MatthewDominick type=grid] (supports grid, carousel, badge/default)
274 *
275 * @param array $atts Shortcode attributes.
276 */
277function dailymotion_channel_shortcode( $atts ) {
278    $username = $atts['user'];
279
280    switch ( $atts['type'] ) {
281        case 'grid':
282            $channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="264px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=grid' ) . '"></iframe>';
283            break;
284        case 'carousel':
285            $channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="360px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=carousel' ) . '"></iframe>';
286            break;
287        default:
288            $channel_iframe = '<iframe sandbox="allow-popups allow-scripts allow-same-origin allow-presentation" width="300px" height="78px" scrolling="no" style="border:0;" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username ) . '"></iframe>';
289    }
290
291    return $channel_iframe;
292}
293add_shortcode( 'dailymotion-channel', 'dailymotion_channel_shortcode' );
294
295/**
296 * Embed Reversal for Badge/Channel
297 *
298 * @param string $content Post content.
299 */
300function dailymotion_channel_reversal( $content ) {
301    if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/badge/' ) ) {
302        return $content;
303    }
304
305    /*
306     * Sample embed code:
307     * <iframe width="300px" height="360px" scrolling="no" frameborder="0" src="http://www.dailymotion.com/badge/user/Dailymotion?type=carousel"></iframe>
308    */
309
310    $regexes = array();
311
312    $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "[^>]*+></iframe>#ix';
313
314    // Let's play nice with the visual editor too.
315    $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;&lt;/iframe&gt;#ix';
316
317    foreach ( $regexes as $regex ) {
318        if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
319            continue;
320        }
321
322        foreach ( $matches as $match ) {
323            $url_pieces = wp_parse_url( $match[1] );
324
325            if ( 'type=carousel' === $url_pieces['query'] ) {
326                $type = 'carousel';
327            } elseif ( 'type=grid' === $url_pieces['query'] ) {
328                $type = 'grid';
329            } else {
330                $type = 'badge';
331            }
332
333            $shortcode     = '[dailymotion-channel user=' . esc_attr( $url_pieces['path'] ) . ' type=' . esc_attr( $type ) . ']';
334            $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
335            $content       = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content );
336        }
337    }
338
339    return $content;
340}
341
342/**
343 * Dailymotion Embed Reversal (with new iframe code as of 17.09.2014)
344 *
345 * Converts a generic HTML embed code from Dailymotion into an
346 * oEmbeddable URL.
347 *
348 * @param string $content Post content.
349 */
350function jetpack_dailymotion_embed_reversal( $content ) {
351    if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/embed' ) ) {
352        return $content;
353    }
354
355    /*
356     * Sample embed code as of Sep 17th 2014:
357     * <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>
358    */
359    $regexes = array();
360
361    // I'm Konstantin and I love regex.
362    $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "[^>]*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+
363    (?: <a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+ )?
364    (?: <i>.*?<a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+</i> )?#ix';
365
366    $regexes[] = '#&lt;iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "(?:[^&]|&(?!gt;))*+&gt;\s*+&lt;/iframe&gt;\s*+(?:&lt;br\s*+/&gt;)?\s*+
367    (?: &lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+ )?
368    (?: &lt;i&gt;.*?&lt;a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+&gt;.+?&lt;/a&gt;\s*+&lt;/i&gt; )?#ix';
369
370    foreach ( $regexes as $regex ) {
371        if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) {
372            continue;
373        }
374
375        foreach ( $matches as $match ) {
376            $url           = esc_url( sprintf( 'https://dailymotion.com/video/%s', $match[1] ) );
377            $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) );
378            $content       = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content );
379
380            /** This action is documented in modules/shortcodes/youtube.php */
381            do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $url );
382        }
383    }
384
385    return $content;
386}
387
388if ( jetpack_shortcodes_should_hook_pre_kses() ) {
389    add_filter( 'pre_kses', 'dailymotion_embed_to_shortcode' );
390    add_filter( 'pre_kses', 'dailymotion_channel_reversal' );
391    add_filter( 'pre_kses', 'jetpack_dailymotion_embed_reversal' );
392}