Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.12% |
84 / 129 |
|
0.00% |
0 / 7 |
CRAP | n/a |
0 / 0 |
|
| flickr_embed_to_shortcode | |
42.86% |
3 / 7 |
|
0.00% |
0 / 1 |
9.66 | |||
| jetpack_flickr_photo_to_shortcode | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| jetpack_flickr_video_to_shortcode | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
6.02 | |||
| flickr_shortcode_handler | |
88.89% |
32 / 36 |
|
0.00% |
0 / 1 |
10.14 | |||
| flickr_shortcode_video_markup | |
88.37% |
38 / 43 |
|
0.00% |
0 / 1 |
15.35 | |||
| flick_shortcode_video_id | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
30 | |||
| jetpack_flickr_oembed_handler | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Flickr Short Code |
| 4 | * Author: kellan |
| 5 | * License: BSD/GPL/public domain (take your pick) |
| 6 | * |
| 7 | * [flickr video=www.flickr.com/photos/kalakeli/49931239842] |
| 8 | * [flickr video=49931239842] |
| 9 | * [flickr video=49931239842 w=200 h=150] |
| 10 | * [flickr video=49931239842 autoplay="yes" controls="no"] |
| 11 | * [flickr video=49931239842 autoplay="no" controls="yes" w=200 h=150] |
| 12 | * |
| 13 | * <div class="flick_video" style="max-width: 100%;width: 500px;height: 300px;"><video src="https://www.flickr.com/photos/kalakeli/49931239842/play/360p/183f75d545/" controls autoplay ></video></div> |
| 14 | * |
| 15 | * @package automattic/jetpack |
| 16 | */ |
| 17 | |
| 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | exit( 0 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Transform embed to shortcode on save. |
| 24 | * |
| 25 | * @param string $content Post content. |
| 26 | * |
| 27 | * @return string Shortcode or the embed content itself. |
| 28 | */ |
| 29 | function flickr_embed_to_shortcode( $content ) { |
| 30 | if ( ! is_string( $content ) ) { |
| 31 | return $content; |
| 32 | } |
| 33 | |
| 34 | if ( str_contains( $content, '<div class="flickr_video"' ) && str_contains( $content, '<video' ) ) { |
| 35 | return jetpack_flickr_video_to_shortcode( $content ); |
| 36 | } elseif ( preg_match( '/<iframe src="(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/[^\"]+\"/', $content ) ) { |
| 37 | return jetpack_flickr_photo_to_shortcode( $content ); |
| 38 | } |
| 39 | |
| 40 | return $content; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Transforms embed to shortcode on save when the photo param is used. |
| 45 | * If embed content cannot be transformed to a valid shortcode, |
| 46 | * the embed content itself is returned. |
| 47 | * |
| 48 | * @param string $content Embed output. |
| 49 | * |
| 50 | * @return string Shortcode or the embed content. |
| 51 | */ |
| 52 | function jetpack_flickr_photo_to_shortcode( $content ) { |
| 53 | preg_match( '/<iframe src=\"([^\"]+)\"(\s+height=\"([^\"]*)\")?(\s+width=\"([^\"]*)\")?/', $content, $matches ); |
| 54 | |
| 55 | if ( empty( $matches[1] ) ) { |
| 56 | return $content; |
| 57 | } |
| 58 | |
| 59 | $src = esc_attr( str_replace( 'player/', '', $matches[1] ) ); |
| 60 | $height = empty( $matches[3] ) ? '' : esc_attr( $matches[3] ); |
| 61 | $width = empty( $matches[5] ) ? '' : esc_attr( $matches[5] ); |
| 62 | |
| 63 | /** This action is documented in modules/shortcodes/youtube.php */ |
| 64 | do_action( 'jetpack_embed_to_shortcode', 'flickr_photo', $src ); |
| 65 | |
| 66 | return '[flickr photo="' . $src . '" w=' . $width . ' h=' . $height . ']'; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Transforms embed to shortcode on save when the video param is used. |
| 71 | * If embed content cannot be transformed to a valid shortcode, |
| 72 | * the embed content itself is returned. |
| 73 | * |
| 74 | * @param string $content Embed output. |
| 75 | * |
| 76 | * @return string Shortcode or the embed content. |
| 77 | */ |
| 78 | function jetpack_flickr_video_to_shortcode( $content ) { |
| 79 | // Get video src. |
| 80 | preg_match( '/<video src=\"([^\"]+)\"/', $content, $matches ); |
| 81 | |
| 82 | if ( empty( $matches[1] ) ) { |
| 83 | return $content; |
| 84 | } |
| 85 | |
| 86 | preg_match( '/(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/photos\/([^\/]+)\/\d+\//', $matches[1], $matches ); |
| 87 | |
| 88 | $video_src = esc_attr( $matches[0] ); |
| 89 | |
| 90 | // Get width and height. |
| 91 | |
| 92 | preg_match( '/style=\"max-width: 100%;(width:\s(\d+)px;)?(height:\s(\d+)px;)?/', $content, $matches ); |
| 93 | |
| 94 | $width = empty( $matches[2] ) ? '' : 'w=' . esc_attr( $matches[2] ); |
| 95 | |
| 96 | $height = empty( $matches[4] ) ? '' : 'h=' . esc_attr( $matches[4] ); |
| 97 | |
| 98 | $controls = str_contains( $content, 'controls' ) ? 'yes' : 'no'; |
| 99 | |
| 100 | $autoplay = str_contains( $content, 'autoplay' ) ? 'yes' : 'no'; |
| 101 | |
| 102 | /** This action is documented in modules/shortcodes/youtube.php */ |
| 103 | do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $video_src ); |
| 104 | |
| 105 | return '[flickr video="' . $video_src . '" ' . $width . ' ' . $height . ' controls="' . $controls . '" autoplay="' . $autoplay . '"]'; |
| 106 | } |
| 107 | |
| 108 | if ( jetpack_shortcodes_should_hook_pre_kses() ) { |
| 109 | add_filter( 'pre_kses', 'flickr_embed_to_shortcode' ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Flickr Shortcode handler. |
| 114 | * |
| 115 | * @param array $atts Shortcode attributes. |
| 116 | * |
| 117 | * @return string Shortcode Output. |
| 118 | */ |
| 119 | function flickr_shortcode_handler( $atts ) { |
| 120 | $atts = shortcode_atts( |
| 121 | array( |
| 122 | 'video' => 0, |
| 123 | 'photo' => 0, |
| 124 | 'w' => '', |
| 125 | 'h' => '', |
| 126 | 'controls' => 'yes', |
| 127 | 'autoplay' => '', |
| 128 | ), |
| 129 | $atts, |
| 130 | 'flickr' |
| 131 | ); |
| 132 | |
| 133 | if ( ! empty( $atts['video'] ) ) { |
| 134 | $showing = 'video'; |
| 135 | $src = $atts['video']; |
| 136 | } elseif ( ! empty( $atts['photo'] ) ) { |
| 137 | $showing = 'photo'; |
| 138 | $src = $atts['photo']; |
| 139 | } else { |
| 140 | return ''; |
| 141 | } |
| 142 | |
| 143 | $src = str_replace( 'http://', 'https://', $src ); |
| 144 | |
| 145 | if ( 'video' === $showing ) { |
| 146 | |
| 147 | $video_id = flick_shortcode_video_id( $src ); |
| 148 | |
| 149 | if ( empty( $video_id ) ) { |
| 150 | return ''; |
| 151 | } |
| 152 | |
| 153 | $atts = array_map( 'esc_attr', $atts ); |
| 154 | return flickr_shortcode_video_markup( $atts, $video_id, $src ); |
| 155 | } elseif ( 'photo' === $showing ) { |
| 156 | |
| 157 | if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) { |
| 158 | return ''; |
| 159 | } |
| 160 | |
| 161 | $height = empty( $atts['h'] ) ? 'auto' : esc_attr( $atts['h'] ); |
| 162 | |
| 163 | $src = sprintf( '%s/player/', untrailingslashit( $src ) ); |
| 164 | |
| 165 | $allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen'; |
| 166 | |
| 167 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 168 | $allow_full_screen = str_replace( ' oallowfullscreen msallowfullscreen', '', $allow_full_screen ); |
| 169 | } |
| 170 | |
| 171 | return sprintf( '<iframe src="%s" height="%s" width="%s" frameborder="0" %s></iframe>', esc_url( $src ), $height, esc_attr( $atts['w'] ), $allow_full_screen ); |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Return HTML markup for a Flickr embed. |
| 179 | * |
| 180 | * @param array $atts Shortcode attributes. |
| 181 | * @param string $id Video ID. |
| 182 | * @param string $video_param video param of the shortcode. |
| 183 | * |
| 184 | * @return string Shortcode ouput for video. |
| 185 | */ |
| 186 | function flickr_shortcode_video_markup( $atts, $id, $video_param ) { |
| 187 | |
| 188 | $transient_name = "flickr_video_$id"; |
| 189 | $video_src = get_transient( $transient_name ); |
| 190 | |
| 191 | if ( empty( $video_src ) ) { |
| 192 | $video_url = ''; |
| 193 | if ( ! is_numeric( $video_param ) ) { |
| 194 | $video_url = $video_param; |
| 195 | } else { |
| 196 | // Get the URL of the video from the page of the video. |
| 197 | $video_page_content = wp_remote_get( "https://flickr.com/photo.gne?id=$video_param" ); |
| 198 | // Bail if we do not get any info from Flickr. |
| 199 | if ( is_wp_error( $video_page_content ) ) { |
| 200 | return ''; |
| 201 | } |
| 202 | |
| 203 | // Extract the URL from the og:url meta tag. |
| 204 | preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches ); |
| 205 | if ( empty( $matches[1] ) ) { |
| 206 | return ''; |
| 207 | } |
| 208 | $video_url = $matches[1]; |
| 209 | } |
| 210 | |
| 211 | $provider = 'https://www.flickr.com/services/oembed/'; |
| 212 | $oembed = _wp_oembed_get_object(); |
| 213 | $data = (array) $oembed->fetch( $provider, $video_url ); |
| 214 | if ( empty( $data['html'] ) ) { |
| 215 | return ''; |
| 216 | } |
| 217 | |
| 218 | // Get the embed url. |
| 219 | preg_match( '/src=\"([^\"]+)\"/', $data['html'], $matches ); |
| 220 | |
| 221 | if ( empty( $matches[1] ) ) { |
| 222 | return ''; |
| 223 | } |
| 224 | $embed_url = $matches[1]; |
| 225 | |
| 226 | $embed_page = wp_remote_get( $embed_url ); |
| 227 | |
| 228 | // Bail if the request returns an error. |
| 229 | if ( ! is_array( $embed_page ) ) { |
| 230 | return ''; |
| 231 | } |
| 232 | |
| 233 | // Get the video url from embed html markup. |
| 234 | |
| 235 | preg_match( '/video.+src=\"([^\"]+)\"/', $embed_page['body'], $matches ); |
| 236 | if ( ! empty( $matches[1] ) ) { |
| 237 | $video_src = $matches[1]; |
| 238 | set_transient( $transient_name, $video_src, 2592000 ); // 30 days transient. |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | $style = 'max-width: 100%;'; |
| 243 | |
| 244 | if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) { |
| 245 | $style .= sprintf( 'width: %dpx;', $atts['w'] ); |
| 246 | } |
| 247 | |
| 248 | if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) { |
| 249 | $style .= sprintf( 'height: %dpx;', $atts['h'] ); |
| 250 | } |
| 251 | |
| 252 | $controls = 'yes' === $atts['controls'] ? 'controls' : ''; |
| 253 | $autoplay = 'yes' === $atts['autoplay'] ? 'autoplay' : ''; |
| 254 | |
| 255 | return sprintf( |
| 256 | '<div class="flick_video" style="%s"><video src="%s" %s %s /></div>', |
| 257 | esc_attr( $style ), |
| 258 | esc_attr( $video_src ), |
| 259 | $controls, |
| 260 | $autoplay |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Extract the id of the flickr video from the video param. |
| 266 | * |
| 267 | * @param string $video_param Video parameter of the shortcode. |
| 268 | * |
| 269 | * @return string|boolean ID of the video or false in case the ID can not be extracted. |
| 270 | */ |
| 271 | function flick_shortcode_video_id( $video_param ) { |
| 272 | if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) { |
| 273 | |
| 274 | // Extract the video id from the url. |
| 275 | preg_match( '/\d+/', $video_param, $matches ); |
| 276 | |
| 277 | if ( empty( $matches ) ) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | return $matches[0]; |
| 282 | |
| 283 | } elseif ( is_numeric( $video_param ) ) { |
| 284 | return $video_param; |
| 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | add_shortcode( 'flickr', 'flickr_shortcode_handler' ); |
| 291 | |
| 292 | // Override core's Flickr support because Flickr oEmbed doesn't support web embeds. |
| 293 | wp_embed_register_handler( 'flickr', '#https?://(www\.)?flickr\.com/.*#i', 'jetpack_flickr_oembed_handler' ); |
| 294 | |
| 295 | /** |
| 296 | * Callback to modify output of embedded Vimeo video using Jetpack's shortcode. |
| 297 | * |
| 298 | * @since 3.9 |
| 299 | * |
| 300 | * @param array $matches Regex partial matches against the URL passed. |
| 301 | * @param array $attr Attributes received in embed response. |
| 302 | * @param string $url Requested URL to be embedded. |
| 303 | * |
| 304 | * @return string Return output of Vimeo shortcode with the proper markup. |
| 305 | */ |
| 306 | function jetpack_flickr_oembed_handler( $matches, $attr, $url ) { |
| 307 | /* |
| 308 | * Legacy slideshow embeds end with /show/ |
| 309 | * e.g. https://www.flickr.com/photos/yarnaholic/sets/72157615194738969/show/ |
| 310 | */ |
| 311 | if ( '/show/' !== substr( $url, -strlen( '/show/' ) ) ) { |
| 312 | // These lookups need cached, as they don't use WP_Embed (which caches). |
| 313 | $cache_key = md5( $url . wp_json_encode( $attr ) ); |
| 314 | $cache_group = 'oembed_flickr'; |
| 315 | |
| 316 | $html = wp_cache_get( $cache_key, $cache_group ); |
| 317 | |
| 318 | if ( false === $html ) { |
| 319 | $html = _wp_oembed_get_object()->get_html( $url, $attr ); |
| 320 | |
| 321 | wp_cache_set( $cache_key, $html, $cache_group, 60 * MINUTE_IN_SECONDS ); |
| 322 | } |
| 323 | |
| 324 | return $html; |
| 325 | } |
| 326 | |
| 327 | return flickr_shortcode_handler( array( 'photo' => $url ) ); |
| 328 | } |