| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dailymotion code |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Original codes: |
| 10 |
* |
| 11 |
* <embed height="270" type="application/x-shockwave-flash" width="480" src="http://www.dailymotion.com/swf/video/xekmrq?additionalInfos=0" wmode="opaque" pluginspage="http://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(?:\:|�*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( '&#0*58;', '&#0*58;|�*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], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 40 |
$params = $match[2] . $match[4]; |
| 41 |
|
| 42 |
if ( 'regexp_ent' === $reg ) { |
| 43 |
$src = html_entity_decode( $src, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 44 |
$params = html_entity_decode( $params, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ); |
| 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'] ) ? (int) $atts['width'] : 0; |
| 139 |
$height = isset( $atts['height'] ) ? (int) $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 |
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 155 |
return sprintf( |
| 156 |
'<amp-dailymotion data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-dailymotion>', |
| 157 |
esc_attr( $id ), |
| 158 |
absint( $width ), |
| 159 |
absint( $height ) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Let's add parameters if needed. |
| 165 |
* |
| 166 |
* @see https://developer.dailymotion.com/player |
| 167 |
*/ |
| 168 |
$player_params = array(); |
| 169 |
|
| 170 |
if ( isset( $atts['autoplay'] ) && '1' === $atts['autoplay'] ) { |
| 171 |
$player_params['autoplay'] = '1'; |
| 172 |
} |
| 173 |
if ( isset( $atts['endscreen-enable'] ) && '0' === $atts['endscreen-enable'] ) { |
| 174 |
$player_params['endscreen-enable'] = '0'; |
| 175 |
} |
| 176 |
if ( isset( $atts['mute'] ) && '1' === $atts['mute'] ) { |
| 177 |
$player_params['mute'] = '1'; |
| 178 |
} |
| 179 |
if ( isset( $atts['sharing-enable'] ) && '0' === $atts['sharing-enable'] ) { |
| 180 |
$player_params['sharing-enable'] = '0'; |
| 181 |
} |
| 182 |
if ( isset( $atts['start'] ) && ! empty( $atts['start'] ) ) { |
| 183 |
$player_params['start'] = abs( (int) $atts['start'] ); |
| 184 |
} |
| 185 |
if ( isset( $atts['subtitles-default'] ) && ! empty( $atts['subtitles-default'] ) ) { |
| 186 |
$player_params['subtitles-default'] = esc_attr( $atts['subtitles-default'] ); |
| 187 |
} |
| 188 |
if ( isset( $atts['ui-highlight'] ) && ! empty( $atts['ui-highlight'] ) ) { |
| 189 |
$player_params['ui-highlight'] = esc_attr( $atts['ui-highlight'] ); |
| 190 |
} |
| 191 |
if ( isset( $atts['ui-logo'] ) && '0' === $atts['ui-logo'] ) { |
| 192 |
$player_params['ui-logo'] = '0'; |
| 193 |
} |
| 194 |
if ( isset( $atts['ui-start-screen-info'] ) && '0' === $atts['ui-start-screen-info'] ) { |
| 195 |
$player_params['ui-start-screen-info'] = '0'; |
| 196 |
} |
| 197 |
if ( isset( $atts['ui-theme'] ) && in_array( strtolower( $atts['ui-theme'] ), array( 'dark', 'light' ), true ) ) { |
| 198 |
$player_params['ui-theme'] = esc_attr( $atts['ui-theme'] ); |
| 199 |
} |
| 200 |
|
| 201 |
// Add those parameters to the Video URL. |
| 202 |
$video_url = add_query_arg( |
| 203 |
$player_params, |
| 204 |
'https://www.dailymotion.com/embed/video/' . $id |
| 205 |
); |
| 206 |
|
| 207 |
$output = ''; |
| 208 |
|
| 209 |
if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) { |
| 210 |
$output .= '<iframe width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" src="' . esc_url( $video_url ) . '" style="border:0;" allowfullscreen></iframe>'; |
| 211 |
|
| 212 |
$video = preg_replace( '/[^-a-z0-9_]/i', '', $atts['video'] ); |
| 213 |
$title = wp_kses( $atts['title'], array() ); |
| 214 |
if ( |
| 215 |
array_key_exists( 'video', $atts ) |
| 216 |
&& $video |
| 217 |
&& array_key_exists( 'title', $atts ) |
| 218 |
&& $title |
| 219 |
) { |
| 220 |
$output .= '<br /><strong><a href="' . esc_url( 'https://www.dailymotion.com/video/' . $video ) . '" target="_blank">' . esc_html( $title ) . '</a></strong>'; |
| 221 |
} |
| 222 |
|
| 223 |
$user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] ); |
| 224 |
if ( array_key_exists( 'user', $atts ) && $user ) { |
| 225 |
/* translators: %s is a Dailymotion user name */ |
| 226 |
$output .= '<br /><em>' . wp_kses( |
| 227 |
sprintf( |
| 228 |
/* Translators: placeholder is a Dailymotion username, linking to a Dailymotion profile page. */ |
| 229 |
__( 'Uploaded by %s', 'jetpack' ), |
| 230 |
'<a href="' . esc_url( 'https://www.dailymotion.com/' . $user ) . '" target="_blank">' . esc_html( $user ) . '</a>' |
| 231 |
), |
| 232 |
array( |
| 233 |
'a' => array( |
| 234 |
'href' => true, |
| 235 |
'target' => true, |
| 236 |
), |
| 237 |
) |
| 238 |
) . '</em>'; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Calypso Helper |
| 244 |
* |
| 245 |
* Makes shortcode output responsive to the location it is loaded: |
| 246 |
* Notifications, Reader, Email |
| 247 |
*/ |
| 248 |
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 249 |
require_once WP_CONTENT_DIR . '/lib/display-context.php'; |
| 250 |
$context = A8C\Display_Context\get_current_context(); |
| 251 |
|
| 252 |
// Notifications. |
| 253 |
if ( A8C\Display_Context\NOTIFICATIONS === $context ) { |
| 254 |
return sprintf( |
| 255 |
'<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>', |
| 256 |
esc_url( 'https://www.dailymotion.com/video/' . $id ) |
| 257 |
); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return $output; |
| 262 |
} |
| 263 |
add_shortcode( 'dailymotion', 'dailymotion_shortcode' ); |
| 264 |
|
| 265 |
/** |
| 266 |
* DailyMotion Channel Shortcode |
| 267 |
* |
| 268 |
* Examples: |
| 269 |
* [dailymotion-channel user=MatthewDominick] |
| 270 |
* [dailymotion-channel user=MatthewDominick type=grid] (supports grid, carousel, badge/default) |
| 271 |
* |
| 272 |
* @param array $atts Shortcode attributes. |
| 273 |
*/ |
| 274 |
function dailymotion_channel_shortcode( $atts ) { |
| 275 |
$username = $atts['user']; |
| 276 |
|
| 277 |
switch ( $atts['type'] ) { |
| 278 |
case 'grid': |
| 279 |
$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>'; |
| 280 |
break; |
| 281 |
case 'carousel': |
| 282 |
$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>'; |
| 283 |
break; |
| 284 |
default: |
| 285 |
$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>'; |
| 286 |
} |
| 287 |
|
| 288 |
return $channel_iframe; |
| 289 |
} |
| 290 |
add_shortcode( 'dailymotion-channel', 'dailymotion_channel_shortcode' ); |
| 291 |
|
| 292 |
/** |
| 293 |
* Embed Reversal for Badge/Channel |
| 294 |
* |
| 295 |
* @param string $content Post content. |
| 296 |
*/ |
| 297 |
function dailymotion_channel_reversal( $content ) { |
| 298 |
if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/badge/' ) ) { |
| 299 |
return $content; |
| 300 |
} |
| 301 |
|
| 302 |
/* |
| 303 |
* Sample embed code: |
| 304 |
* <iframe width="300px" height="360px" scrolling="no" frameborder="0" src="http://www.dailymotion.com/badge/user/Dailymotion?type=carousel"></iframe> |
| 305 |
*/ |
| 306 |
|
| 307 |
$regexes = array(); |
| 308 |
|
| 309 |
$regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "[^>]*+></iframe>#ix'; |
| 310 |
|
| 311 |
// Let's play nice with the visual editor too. |
| 312 |
$regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "(?:[^&]|&(?!gt;))*+></iframe>#ix'; |
| 313 |
|
| 314 |
foreach ( $regexes as $regex ) { |
| 315 |
if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
foreach ( $matches as $match ) { |
| 320 |
$url_pieces = wp_parse_url( $match[1] ); |
| 321 |
|
| 322 |
if ( 'type=carousel' === $url_pieces['query'] ) { |
| 323 |
$type = 'carousel'; |
| 324 |
} elseif ( 'type=grid' === $url_pieces['query'] ) { |
| 325 |
$type = 'grid'; |
| 326 |
} else { |
| 327 |
$type = 'badge'; |
| 328 |
} |
| 329 |
|
| 330 |
$shortcode = '[dailymotion-channel user=' . esc_attr( $url_pieces['path'] ) . ' type=' . esc_attr( $type ) . ']'; |
| 331 |
$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); |
| 332 |
$content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content ); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return $content; |
| 337 |
} |
| 338 |
add_filter( 'pre_kses', 'dailymotion_channel_reversal' ); |
| 339 |
|
| 340 |
/** |
| 341 |
* Dailymotion Embed Reversal (with new iframe code as of 17.09.2014) |
| 342 |
* |
| 343 |
* Converts a generic HTML embed code from Dailymotion into an |
| 344 |
* oEmbeddable URL. |
| 345 |
* |
| 346 |
* @param string $content Post content. |
| 347 |
*/ |
| 348 |
function jetpack_dailymotion_embed_reversal( $content ) { |
| 349 |
if ( ! is_string( $content ) || false === stripos( $content, 'dailymotion.com/embed' ) ) { |
| 350 |
return $content; |
| 351 |
} |
| 352 |
|
| 353 |
/* |
| 354 |
* Sample embed code as of Sep 17th 2014: |
| 355 |
* <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> |
| 356 |
*/ |
| 357 |
$regexes = array(); |
| 358 |
|
| 359 |
// I'm Konstantin and I love regex. |
| 360 |
$regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "[^>]*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+ |
| 361 |
(?: <a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+ )? |
| 362 |
(?: <i>.*?<a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+</i> )?#ix'; |
| 363 |
|
| 364 |
$regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "(?:[^&]|&(?!gt;))*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+ |
| 365 |
(?: <a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+>.+?</a>\s*+ )? |
| 366 |
(?: <i>.*?<a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+>.+?</a>\s*+</i> )?#ix'; |
| 367 |
|
| 368 |
foreach ( $regexes as $regex ) { |
| 369 |
if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { |
| 370 |
continue; |
| 371 |
} |
| 372 |
|
| 373 |
foreach ( $matches as $match ) { |
| 374 |
$url = esc_url( sprintf( 'https://dailymotion.com/video/%s', $match[1] ) ); |
| 375 |
$replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); |
| 376 |
$content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); |
| 377 |
|
| 378 |
/** This action is documented in modules/shortcodes/youtube.php */ |
| 379 |
do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $url ); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
return $content; |
| 384 |
} |
| 385 |
add_filter( 'pre_kses', 'jetpack_dailymotion_embed_reversal' ); |
| 386 |
|