/modules/shortcodes/dailymotion.php

  1. <?php 
  2.  
  3. /** 
  4. * Dailymotion code 
  5. * */ 
  6.  
  7. /** 
  8. * Original codes: 
  9. * 
  10. * <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" /> 
  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/xaose5_sexy-surprise_na">Sexy Surprise</a></b><br /><i>Uploaded by <a href="http://www.dailymotion.com/GilLavie">GilLavie</a>. - <a target="_self" href="http://www.dailymotion.com/channel/sexy/featured/1">Find more steamy, sexy videos.</a></i> 
  18. * movie param enforces anti-xss protection 
  19. * 
  20. * Scroll down for the new <iframe> embed code handler. 
  21. */ 
  22.  
  23. function dailymotion_embed_to_shortcode( $content ) { 
  24. if ( false === stripos( $content, 'www.dailymotion.com/swf/' ) ) { 
  25. return $content; 
  26.  
  27. $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>!'; 
  28. $regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); 
  29.  
  30. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { 
  31. if ( ! preg_match_all( $$reg, $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.  
  38. if ( 'regexp_ent' == $reg ) { 
  39. $src = html_entity_decode( $src ); 
  40. $params = html_entity_decode( $params ); 
  41.  
  42. $params = wp_kses_hair( $params, array( 'http' ) ); 
  43.  
  44. if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' != $params['type']['value'] ) { 
  45. continue; 
  46.  
  47. $id = basename( substr( $src, strlen( 'www.dailymotion.com/swf' ) ) ); 
  48. $id = preg_replace( '/[^a-z0-9].*$/i', '', $id ); 
  49.  
  50. $content = str_replace( $match[0], "[dailymotion id=$id]", $content ); 
  51. /** This action is documented in modules/shortcodes/youtube.php */ 
  52. do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $id ); 
  53. return $content; 
  54. add_filter( 'pre_kses', 'dailymotion_embed_to_shortcode' ); 
  55.  
  56. /** 
  57. * DailyMotion shortcode 
  58. * 
  59. * The documented shortcode is: 
  60. * [dailymotion id=x8oma9] 
  61. * 
  62. * Possibilities, according to the old parsing regexp: 
  63. * [dailymotion x8oma9] 
  64. * [dailymotion=x8oma9] 
  65. * 
  66. * Hypothetical option, according to the old shortcode function is 
  67. * [dailymotion id=1&title=2&user=3&video=4] 
  68. * 
  69. * The new style is now: 
  70. * [dailymotion id=x8oma9 title=2 user=3 video=4] 
  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.  
  78. function dailymotion_shortcode( $atts ) { 
  79. global $content_width; 
  80.  
  81. if ( isset( $atts[0] ) ) { 
  82. $id = ltrim( $atts[0], '=' ); 
  83. $atts['id'] = $id; 
  84.  
  85. } else { 
  86. $params = shortcode_new_to_old_params( $atts ); 
  87. parse_str( $params, $atts_new ); 
  88.  
  89. foreach( $atts_new as $k => $v ) { 
  90. $atts[ $k ] = $v; 
  91.  
  92. if ( isset( $atts['id'] ) ) { 
  93. $id = $atts['id']; 
  94. } else { 
  95. return '<!--Dailymotion error: bad or missing ID-->'; 
  96.  
  97. if ( ! empty( $content_width ) ) { 
  98. $width = min( 425, intval( $content_width ) ); 
  99. } else { 
  100. $width = 425; 
  101.  
  102. $height = ( 425 == $width ) ? 334 : ( $width / 425 ) * 334; 
  103. $id = urlencode( $id ); 
  104.  
  105. if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) { 
  106. $output = '<iframe width="' . $width . '" height="' . $height . '" src="' . esc_url( '//www.dailymotion.com/embed/video/' . $id ) . '" frameborder="0"></iframe>'; 
  107. $after = ''; 
  108.  
  109. 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() ) ) { 
  110. $after .= '<br /><strong><a href="' . esc_url( 'http://www.dailymotion.com/video/' . $video ) . '">' . esc_html( $title ) . '</a></strong>'; 
  111.  
  112. if ( array_key_exists( 'user', $atts ) && $user = preg_replace( '/[^-a-z0-9_]/i', '', $atts['user'] ) ) { 
  113. $after .= '<br /><em>Uploaded by <a href="' . esc_url( 'http://www.dailymotion.com/' . $user ) . '">' . esc_html( $user ) . '</a></em>'; 
  114.  
  115. return $output . $after; 
  116.  
  117. add_shortcode( 'dailymotion', 'dailymotion_shortcode' ); 
  118.  
  119. /** 
  120. * DailyMotion Channel Shortcode 
  121. * 
  122. * Examples: 
  123. * [dailymotion-channel user=MatthewDominick] 
  124. * [dailymotion-channel user=MatthewDominick type=grid] (supports grid, carousel, badge/default) 
  125. */ 
  126. function dailymotion_channel_shortcode( $atts ) { 
  127. $username = $atts['user']; 
  128.  
  129. switch( $atts['type'] ) { 
  130. case 'grid': 
  131. return '<iframe width="300px" height="264px" scrolling="no" frameborder="0" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=grid' ) . '"></iframe>'; 
  132. break; 
  133. case 'carousel': 
  134. return '<iframe width="300px" height="360px" scrolling="no" frameborder="0" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username . '?type=carousel' ) . '"></iframe>'; 
  135. break; 
  136. default: 
  137. return '<iframe width="300px" height="78px" scrolling="no" frameborder="0" src="' . esc_url( '//www.dailymotion.com/badge/user/' . $username ) . '"></iframe>'; 
  138.  
  139. add_shortcode( 'dailymotion-channel', 'dailymotion_channel_shortcode' ); 
  140.  
  141. /** 
  142. * Embed Reversal for Badge/Channel 
  143. */ 
  144. function dailymotion_channel_reversal( $content ) { 
  145. if ( false === stripos( $content, 'dailymotion.com/badge/' ) ) { 
  146. return $content; 
  147.  
  148. /** Sample embed code: 
  149. <iframe width="300px" height="360px" scrolling="no" frameborder="0" src="http://www.dailymotion.com/badge/user/Dailymotion?type=carousel"></iframe> 
  150. */ 
  151.  
  152. $regexes = array(); 
  153.  
  154. $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "[^>]*+></iframe>#ix'; 
  155.  
  156. // Let's play nice with the visual editor too. 
  157. $regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/badge/user/([^"\'/]++) "(?:[^&]|&(?!gt;))*+></iframe>#ix'; 
  158.  
  159. foreach ( $regexes as $regex ) { 
  160. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { 
  161. continue; 
  162.  
  163. foreach ( $matches as $match ) { 
  164. $url_pieces = parse_url( $match[1] ); 
  165.  
  166. if ( 'type=carousel' === $url_pieces['query'] ) { 
  167. $type = 'carousel'; 
  168. } else if ( 'type=grid' === $url_pieces['query'] ) { 
  169. $type = 'grid'; 
  170. } else { 
  171. $type = 'badge'; 
  172.  
  173. $shortcode = '[dailymotion-channel user=' . esc_attr( $url_pieces['path'] ) . ' type=' . esc_attr( $type ) . ']'; 
  174. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); 
  175. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $shortcode ), $content ); 
  176.  
  177. return $content; 
  178.  
  179. add_filter( 'pre_kses', 'dailymotion_channel_reversal' ); 
  180.  
  181. /** 
  182. * Dailymotion Embed Reversal (with new iframe code as of 17.09.2014) 
  183. * 
  184. * Converts a generic HTML embed code from Dailymotion into an 
  185. * oEmbeddable URL. 
  186. */ 
  187.  
  188. function jetpack_dailymotion_embed_reversal( $content ) { 
  189. if ( false === stripos( $content, 'dailymotion.com/embed' ) ) { 
  190. return $content; 
  191.  
  192. /** Sample embed code as of Sep 17th 2014: 
  193.   
  194. <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> 
  195. */ 
  196. $regexes = array(); 
  197.  
  198. // I'm Konstantin and I love regex. 
  199. $regexes[] = '#<iframe[^>]+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "[^>]*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+ 
  200. (?: <a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+ )? 
  201. (?: <i>.*?<a[^>]+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "[^>]*+>.+?</a>\s*+</i> )?#ix'; 
  202.  
  203. $regexes[] = '#<iframe(?:[^&]|&(?!gt;))+?src=" (?:https?:)?//(?:www\.)?dailymotion\.com/embed/video/([^"\'/]++) "(?:[^&]|&(?!gt;))*+>\s*+</iframe>\s*+(?:<br\s*+/>)?\s*+ 
  204. (?: <a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+>.+?</a>\s*+ )? 
  205. (?: <i>.*?<a(?:[^&]|&(?!gt;))+?href=" (?:https?:)?//(?:www\.)?dailymotion\.com/[^"\']++ "(?:[^&]|&(?!gt;))*+>.+?</a>\s*+</i> )?#ix'; 
  206.  
  207. foreach ( $regexes as $regex ) { 
  208. if ( ! preg_match_all( $regex, $content, $matches, PREG_SET_ORDER ) ) { 
  209. continue; 
  210.  
  211. foreach ( $matches as $match ) { 
  212. $url = esc_url( sprintf( 'https://dailymotion.com/video/%s', $match[1] ) ); 
  213. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $match[0], '#' ) ); 
  214. $content = preg_replace( $replace_regex, sprintf( "\n\n%s\n\n", $url ), $content ); 
  215.  
  216. /** This action is documented in modules/shortcodes/youtube.php */ 
  217. do_action( 'jetpack_embed_to_shortcode', 'dailymotion', $url ); 
  218.  
  219. return $content; 
  220.  
  221. add_filter( 'pre_kses', 'jetpack_dailymotion_embed_reversal' ); 
.