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