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