dailymotion.php 14 KB

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