| 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 |
$src = str_replace( 'http://', 'https://', $src ); |
| 139 |
|
| 140 |
if ( 'video' === $showing ) { |
| 141 |
|
| 142 |
if ( ! is_numeric( $src ) && ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) { |
| 143 |
return ''; |
| 144 |
} |
| 145 |
|
| 146 |
if ( preg_match( '!photos/(([0-9a-zA-Z-_]+)|([0-9]+@N[0-9]+))/([0-9]+)/?$!', $src, $m ) ) { |
| 147 |
$atts['photo_id'] = $m[4]; |
| 148 |
} else { |
| 149 |
$atts['photo_id'] = $atts['video']; |
| 150 |
} |
| 151 |
|
| 152 |
if ( |
| 153 |
! isset( $atts['show_info'] ) |
| 154 |
|| in_array( $atts['show_info'], array( 'yes', 'true' ), true ) |
| 155 |
) { |
| 156 |
$atts['show_info'] = 'true'; |
| 157 |
} elseif ( in_array( $atts['show_info'], array( 'false', 'no' ), true ) ) { |
| 158 |
$atts['show_info'] = 'false'; |
| 159 |
} |
| 160 |
|
| 161 |
if ( isset( $atts['secret'] ) ) { |
| 162 |
$atts['secret'] = preg_replace( '![^\w]+!i', '', $atts['secret'] ); |
| 163 |
} |
| 164 |
|
| 165 |
return flickr_shortcode_video_markup( $atts ); |
| 166 |
} elseif ( 'photo' === $showing ) { |
| 167 |
|
| 168 |
if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) { |
| 169 |
return ''; |
| 170 |
} |
| 171 |
|
| 172 |
$src = sprintf( '%s/player/', untrailingslashit( $src ) ); |
| 173 |
|
| 174 |
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'] ) ); |
| 175 |
} |
| 176 |
|
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Return HTML markup for a Flickr embed. |
| 182 |
* |
| 183 |
* @param array $atts Shortcode attributes. |
| 184 |
*/ |
| 185 |
function flickr_shortcode_video_markup( $atts ) { |
| 186 |
$atts = array_map( 'esc_attr', $atts ); |
| 187 |
|
| 188 |
$photo_vars = "photo_id=$atts[photo_id]"; |
| 189 |
if ( isset( $atts['secret'] ) ) { |
| 190 |
$photo_vars .= "&photo_secret=$atts[secret]"; |
| 191 |
} |
| 192 |
|
| 193 |
return <<<EOD |
| 194 |
<object type="application/x-shockwave-flash" width="$atts[w]" height="$atts[h]" data="https://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="https://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="https://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> |
| 195 |
EOD; |
| 196 |
} |
| 197 |
|
| 198 |
add_shortcode( 'flickr', 'flickr_shortcode_handler' ); |
| 199 |
|
| 200 |
// Override core's Flickr support because Flickr oEmbed doesn't support web embeds. |
| 201 |
wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' ); |
| 202 |
|
| 203 |
/** |
| 204 |
* Callback to modify output of embedded Vimeo video using Jetpack's shortcode. |
| 205 |
* |
| 206 |
* @since 3.9 |
| 207 |
* |
| 208 |
* @param array $matches Regex partial matches against the URL passed. |
| 209 |
* @param array $attr Attributes received in embed response. |
| 210 |
* @param array $url Requested URL to be embedded. |
| 211 |
* |
| 212 |
* @return string Return output of Vimeo shortcode with the proper markup. |
| 213 |
*/ |
| 214 |
function jetpack_flickr_oembed_handler( $matches, $attr, $url ) { |
| 215 |
/* |
| 216 |
* Legacy slideshow embeds end with /show/ |
| 217 |
* e.g. http://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/ |
| 218 |
*/ |
| 219 |
if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) { |
| 220 |
// These lookups need cached, as they don't use WP_Embed (which caches). |
| 221 |
$cache_key = md5( $url . wp_json_encode( $attr ) ); |
| 222 |
$cache_group = 'oembed_flickr'; |
| 223 |
|
| 224 |
$html = wp_cache_get( $cache_key, $cache_group ); |
| 225 |
|
| 226 |
if ( false === $html ) { |
| 227 |
$html = _wp_oembed_get_object()->get_html( $url, $attr ); |
| 228 |
|
| 229 |
wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS ); |
| 230 |
} |
| 231 |
|
| 232 |
return $html; |
| 233 |
} |
| 234 |
|
| 235 |
return flickr_shortcode_handler( array( 'photo' => $url ) ); |
| 236 |
} |
| 237 |
|