flickr.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /*
  3. Flickr Short Code
  4. Author: kellan
  5. License: BSD/GPL/public domain (take your pick)
  6. [flickr video=http://www.flickr.com/photos/chaddles/2402990826]
  7. [flickr video=2402990826]
  8. [flickr video=2402990826 show_info=no]
  9. [flickr video=2402990826 w=200 h=150]
  10. [flickr video=2402990826 secret=846d9c1b39]
  11. */
  12. /*
  13. * <object type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="intl_lang=en-us&photo_secret=846d9c1be9&photo_id=2345938910"></param> <param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param> <param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&photo_secret=846d9c1be9&photo_id=2345938910" height="300" width="400"></embed></object>
  14. */
  15. function flickr_embed_to_shortcode( $content ) {
  16. if ( ! is_string( $content ) || false === stripos( $content, '/www.flickr.com/apps/video/stewart.swf' ) ) {
  17. return $content;
  18. }
  19. $regexp = '%(<object.*?(?:<(?!/?(?:object|embed)\s+).*?)*?)?<embed((?:\s+\w+="[^"]*")*)\s+src="http(?:\:|&#0*58;)//www.flickr.com/apps/video/stewart.swf[^"]*"((?:\s+\w+="[^"]*")*)\s*(?:/>|>\s*</embed>)(?(1)\s*</object>)%';
  20. $regexp_ent = str_replace(
  21. array(
  22. '&amp;#0*58;',
  23. '[^&gt;]*',
  24. '[^&lt;]*',
  25. ),
  26. array(
  27. '&amp;#0*58;|&#0*58;',
  28. '[^&]*(?:&(?!gt;)[^&]*)*',
  29. '[^&]*(?:&(?!lt;)[^&]*)*',
  30. ),
  31. htmlspecialchars( $regexp, ENT_NOQUOTES )
  32. );
  33. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
  34. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
  35. continue;
  36. }
  37. foreach ( $matches as $match ) {
  38. $params = $match[2] . $match[3];
  39. if ( 'regexp_ent' == $reg ) {
  40. $params = html_entity_decode( $params );
  41. }
  42. $params = wp_kses_hair( $params, array( 'http' ) );
  43. if ( ! isset( $params['type'] ) || 'application/x-shockwave-flash' != $params['type']['value'] || ! isset( $params['flashvars'] ) ) {
  44. continue;
  45. }
  46. wp_parse_str( html_entity_decode( $params['flashvars']['value'] ), $flashvars );
  47. if ( ! isset( $flashvars['photo_id'] ) ) {
  48. continue;
  49. }
  50. $code_atts = array( 'video' => $flashvars['photo_id'], );
  51. if ( isset( $flashvars['flickr_show_info_box'] ) && 'true' == $flashvars['flickr_show_info_box'] ) {
  52. $code_atts['show_info'] = 'true';
  53. }
  54. if ( ! empty( $flashvars['photo_secret'] ) ) {
  55. $code_atts['secret'] = $flashvars['photo_secret'];
  56. }
  57. if ( ! empty( $params['width']['value'] ) ) {
  58. $code_atts['w'] = (int) $params['width']['value'];
  59. }
  60. if ( ! empty( $params['height']['value'] ) ) {
  61. $code_atts['h'] = (int) $params['height']['value'];
  62. }
  63. $code = '[flickr';
  64. foreach ( $code_atts as $k => $v ) {
  65. $code .= " $k=$v";
  66. }
  67. $code .= ']';
  68. $content = str_replace( $match[0], $code, $content );
  69. /** This action is documented in modules/shortcodes/youtube.php */
  70. do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $flashvars['photo_id'] );
  71. }
  72. }
  73. return $content;
  74. }
  75. add_filter( 'pre_kses', 'flickr_embed_to_shortcode' );
  76. function flickr_shortcode_handler( $atts ) {
  77. $atts = shortcode_atts(
  78. array(
  79. 'video' => 0,
  80. 'photo' => 0,
  81. 'show_info' => 0,
  82. 'w' => 400,
  83. 'h' => 300,
  84. 'secret' => 0,
  85. ), $atts, 'flickr'
  86. );
  87. if ( ! empty( $atts['video'] ) ) {
  88. $showing = 'video';
  89. $src = $atts['video'];
  90. } elseif ( ! empty( $atts['photo'] ) ) {
  91. $showing = 'photo';
  92. $src = $atts['photo'];
  93. } else {
  94. return '';
  95. }
  96. if ( is_ssl() ) {
  97. $src = str_replace( 'http://', 'https://', $src );
  98. }
  99. if ( 'video' === $showing ) {
  100. if ( ! is_numeric( $src ) && ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
  101. return '';
  102. }
  103. if ( preg_match( '!photos/(([0-9a-zA-Z-_]+)|([0-9]+@N[0-9]+))/([0-9]+)/?$!', $src, $m ) ) {
  104. $atts['photo_id'] = $m[4];
  105. } else {
  106. $atts['photo_id'] = $atts['video'];
  107. }
  108. if ( ! isset( $atts['show_info'] ) || in_array( $atts['show_info'], array( 'yes', 'true' ) ) ) {
  109. $atts['show_info'] = 'true';
  110. } elseif ( in_array( $atts['show_info'], array( 'false', 'no' ) ) ) {
  111. $atts['show_info'] = 'false';
  112. }
  113. if ( isset( $atts['secret'] ) ) {
  114. $atts['secret'] = preg_replace( '![^\w]+!i', '', $atts['secret'] );
  115. }
  116. return flickr_shortcode_video_markup( $atts );
  117. } elseif ( 'photo' == $showing ) {
  118. if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
  119. return '';
  120. }
  121. $src = sprintf( '%s/player/', untrailingslashit( $src ) );
  122. return sprintf( '<iframe src="%s" height="%s" width="%s" frameborder="0" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>', esc_url( $src ), esc_attr( $atts['h'] ), esc_attr( $atts['w'] ) );
  123. }
  124. return false;
  125. }
  126. function flickr_shortcode_video_markup( $atts ) {
  127. $atts = array_map( 'esc_attr', $atts );
  128. $http = ( is_ssl() ) ? 'https://' : 'http://';
  129. $photo_vars = "photo_id=$atts[photo_id]";
  130. if ( isset( $atts['secret'] ) ) {
  131. $photo_vars .= "&amp;photo_secret=$atts[secret]";
  132. }
  133. return <<<EOD
  134. <object type="application/x-shockwave-flash" width="$atts[w]" height="$atts[h]" data="{$http}www.flickr.com/apps/video/stewart.swf?v=1.161" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="$photo_vars&amp;flickr_show_info_box=$atts[show_info]"></param><param name="movie" value="{$http}www.flickr.com/apps/video/stewart.swf?v=1.161"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="opaque"></param><embed type="application/x-shockwave-flash" src="{$http}www.flickr.com/apps/video/stewart.swf?v=1.161" bgcolor="#000000" allowfullscreen="true" flashvars="$photo_vars&amp;flickr_show_info_box=$atts[show_info]" wmode="opaque" height="$atts[h]" width="$atts[w]"></embed></object>
  135. EOD;
  136. }
  137. add_shortcode( 'flickr', 'flickr_shortcode_handler' );
  138. // Override core's Flickr support because Flickr oEmbed doesn't support web embeds
  139. wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' );
  140. function jetpack_flickr_oembed_handler( $matches, $attr, $url ) {
  141. // Legacy slideshow embeds end with /show/
  142. // e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/
  143. if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) {
  144. // These lookups need cached, as they don't use WP_Embed (which caches)
  145. $cache_key = md5( $url . serialize( $attr ) );
  146. $cache_group = 'oembed_flickr';
  147. $html = wp_cache_get( $cache_key, $cache_group );
  148. if ( false === $html ) {
  149. $html = _wp_oembed_get_object()->get_html( $url, $attr );
  150. wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS );
  151. }
  152. return $html;
  153. }
  154. return flickr_shortcode_handler( array( 'photo' => $url ) );
  155. }