Ei kuvausta

dailymotion.php 15KB

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