summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/shortcodes/flickr.php')
-rw-r--r--plugins/jetpack/modules/shortcodes/flickr.php306
1 files changed, 188 insertions, 118 deletions
diff --git a/plugins/jetpack/modules/shortcodes/flickr.php b/plugins/jetpack/modules/shortcodes/flickr.php
index 6ee80200..da725d8b 100644
--- a/plugins/jetpack/modules/shortcodes/flickr.php
+++ b/plugins/jetpack/modules/shortcodes/flickr.php
@@ -4,131 +4,121 @@
* Author: kellan
* License: BSD/GPL/public domain (take your pick)
*
- * [flickr video=http://www.flickr.com/photos/chaddles/2402990826]
- * [flickr video=2402990826]
- * [flickr video=2402990826 show_info=no]
- * [flickr video=2402990826 w=200 h=150]
- * [flickr video=2402990826 secret=846d9c1b39]
+ * [flickr video=www.flickr.com/photos/kalakeli/49931239842]
+ * [flickr video=49931239842]
+ * [flickr video=49931239842 w=200 h=150]
+ * [flickr video=49931239842 autoplay="yes" controls="no"]
+ * [flickr video=49931239842 autoplay="no" controls="yes" w=200 h=150]
*
- * @package Jetpack
- */
-
-/*
- * <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>
+ * <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>
+ *
+ * @package automattic/jetpack
*/
/**
* Transform embed to shortcode on save.
*
* @param string $content Post content.
+ *
+ * @return string Shortcode or the embed content itself.
*/
function flickr_embed_to_shortcode( $content ) {
- if ( ! is_string( $content ) || false === stripos( $content, '/www.flickr.com/apps/video/stewart.swf' ) ) {
+ if ( ! is_string( $content ) ) {
return $content;
}
- $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>)%';
- $regexp_ent = str_replace(
- array(
- '&amp;#0*58;',
- '[^&gt;]*',
- '[^&lt;]*',
- ),
- array(
- '&amp;#0*58;|&#0*58;',
- '[^&]*(?:&(?!gt;)[^&]*)*',
- '[^&]*(?:&(?!lt;)[^&]*)*',
- ),
- htmlspecialchars( $regexp, ENT_NOQUOTES )
- );
+ if ( false !== strpos( $content, '<div class="flickr_video"' ) && false !== strpos( $content, '<video' ) ) {
+ return jetpack_flickr_video_to_shortcode( $content );
+ } elseif ( preg_match( '/<iframe src="(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/[^\"]+\"/', $content ) ) {
+ return jetpack_flickr_photo_to_shortcode( $content );
+ }
- foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
- if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
- continue;
- }
- foreach ( $matches as $match ) {
- $params = $match[2] . $match[3];
+ return $content;
+}
- if ( 'regexp_ent' === $reg ) {
- $params = html_entity_decode( $params );
- }
+/**
+ * Transforms embed to shortcode on save when the photo param is used.
+ * If embed content can not be transformed to a valid shortcode,
+ * the embed content itself is returned.
+ *
+ * @param string $content Embed output.
+ *
+ * @return string Shortcode or the embed content.
+ */
+function jetpack_flickr_photo_to_shortcode( $content ) {
+ preg_match( '/<iframe src=\"([^\"]+)\"(\s+height=\"([^\"]*)\")?(\s+width=\"([^\"]*)\")?/', $content, $matches );
- $params = wp_kses_hair( $params, array( 'http' ) );
- if (
- ! isset( $params['type'] )
- || 'application/x-shockwave-flash' !== $params['type']['value']
- || ! isset( $params['flashvars'] )
- ) {
- continue;
- }
+ if ( empty( $matches[1] ) ) {
+ return $content;
+ }
- $flashvars = array();
- wp_parse_str( html_entity_decode( $params['flashvars']['value'] ), $flashvars );
+ $src = esc_attr( str_replace( 'player/', '', $matches[1] ) );
+ $height = empty( $matches[3] ) ? '' : esc_attr( $matches[3] );
+ $width = empty( $matches[5] ) ? '' : esc_attr( $matches[5] );
- if ( ! isset( $flashvars['photo_id'] ) ) {
- continue;
- }
+ /** This action is documented in modules/shortcodes/youtube.php */
+ do_action( 'jetpack_embed_to_shortcode', 'flickr_photo', $src );
- $photo_id = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_id'] );
+ return '[flickr photo="' . $src . '" w=' . $width . ' h=' . $height . ']';
+}
- if ( ! strlen( $photo_id ) ) {
- continue;
- }
+/**
+ * Transforms embed to shortcode on save when the video param is used.
+ * If embed content can not be transformed to a valid shortcode,
+ * the embed content itself is returned.
+ *
+ * @param string $content Embed output.
+ *
+ * @return string Shortcode or the embed content.
+ */
+function jetpack_flickr_video_to_shortcode( $content ) {
+ // Get video src.
+ preg_match( '/<video src=\"([^\"]+)\"/', $content, $matches );
- $code_atts = array( 'video' => $photo_id );
+ if ( empty( $matches[1] ) ) {
+ return $content;
+ }
- if (
- isset( $flashvars['flickr_show_info_box'] )
- && 'true' === $flashvars['flickr_show_info_box']
- ) {
- $code_atts['show_info'] = 'true';
- }
+ preg_match( '/(https?:)?\/\/([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)\/photos\/([^\/]+)\/\d+\//', $matches[1], $matches );
- if ( ! empty( $flashvars['photo_secret'] ) ) {
- $photo_secret = preg_replace( '#[^A-Za-z0-9_./@+-]+#', '', $flashvars['photo_secret'] );
- if ( strlen( $photo_secret ) ) {
- $code_atts['secret'] = $photo_secret;
- }
- }
+ $video_src = esc_attr( $matches[0] );
- if ( ! empty( $params['width']['value'] ) ) {
- $code_atts['w'] = (int) $params['width']['value'];
- }
+ // Get width and height.
- if ( ! empty( $params['height']['value'] ) ) {
- $code_atts['h'] = (int) $params['height']['value'];
- }
+ preg_match( '/style=\"max-width: 100%;(width:\s(\d+)px;)?(height:\s(\d+)px;)?/', $content, $matches );
- $code = '[flickr';
- foreach ( $code_atts as $k => $v ) {
- $code .= " $k=$v";
- }
- $code .= ']';
+ $width = empty( $matches[2] ) ? '' : 'w=' . esc_attr( $matches[2] );
- $content = str_replace( $match[0], $code, $content );
- /** This action is documented in modules/shortcodes/youtube.php */
- do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $flashvars['photo_id'] );
- }
- }
+ $height = empty( $matches[4] ) ? '' : 'h=' . esc_attr( $matches[4] );
- return $content;
+ $controls = false !== strpos( $content, 'controls' ) ? 'yes' : 'no';
+
+ $autoplay = false !== strpos( $content, 'autoplay' ) ? 'yes' : 'no';
+
+ /** This action is documented in modules/shortcodes/youtube.php */
+ do_action( 'jetpack_embed_to_shortcode', 'flickr_video', $video_src );
+
+ return '[flickr video="' . $video_src . '" ' . $width . ' ' . $height . ' controls="' . $controls . '" autoplay="' . $autoplay . '"]';
}
+
add_filter( 'pre_kses', 'flickr_embed_to_shortcode' );
/**
* Flickr Shortcode handler.
*
* @param array $atts Shortcode attributes.
+ *
+ * @return string Shortcode Output.
*/
function flickr_shortcode_handler( $atts ) {
$atts = shortcode_atts(
array(
- 'video' => 0,
- 'photo' => 0,
- 'show_info' => 0,
- 'w' => 400,
- 'h' => 300,
- 'secret' => 0,
+ 'video' => 0,
+ 'photo' => 0,
+ 'w' => '',
+ 'h' => '',
+ 'controls' => 'yes',
+ 'autoplay' => '',
),
$atts,
'flickr'
@@ -148,39 +138,31 @@ function flickr_shortcode_handler( $atts ) {
if ( 'video' === $showing ) {
- if ( ! is_numeric( $src ) && ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
- return '';
- }
-
- if ( preg_match( '!photos/(([0-9a-zA-Z-_]+)|([0-9]+@N[0-9]+))/([0-9]+)/?$!', $src, $m ) ) {
- $atts['photo_id'] = $m[4];
- } else {
- $atts['photo_id'] = $atts['video'];
- }
+ $video_id = flick_shortcode_video_id( $src );
- if (
- ! isset( $atts['show_info'] )
- || in_array( $atts['show_info'], array( 'yes', 'true' ), true )
- ) {
- $atts['show_info'] = 'true';
- } elseif ( in_array( $atts['show_info'], array( 'false', 'no' ), true ) ) {
- $atts['show_info'] = 'false';
- }
-
- if ( isset( $atts['secret'] ) ) {
- $atts['secret'] = preg_replace( '![^\w]+!i', '', $atts['secret'] );
+ if ( empty( $video_id ) ) {
+ return '';
}
- return flickr_shortcode_video_markup( $atts );
+ $atts = array_map( 'esc_attr', $atts );
+ return flickr_shortcode_video_markup( $atts, $video_id, $src );
} elseif ( 'photo' === $showing ) {
if ( ! preg_match( '~^(https?:)?//([\da-z\-]+\.)*?((static)?flickr\.com|flic\.kr)/.*~i', $src ) ) {
return '';
}
+ $height = empty( $atts['h'] ) ? 'auto' : esc_attr( $atts['h'] );
+
$src = sprintf( '%s/player/', untrailingslashit( $src ) );
- 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'] ) );
+ $allow_full_screen = 'allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen';
+
+ if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
+ $allow_full_screen = str_replace( ' oallowfullscreen msallowfullscreen', '', $allow_full_screen );
+ }
+
+ return sprintf( '<iframe src="%s" height="%s" width="%s" frameborder="0" %s></iframe>', esc_url( $src ), $height, esc_attr( $atts['w'] ), $allow_full_screen );
}
return false;
@@ -189,19 +171,107 @@ function flickr_shortcode_handler( $atts ) {
/**
* Return HTML markup for a Flickr embed.
*
- * @param array $atts Shortcode attributes.
+ * @param array $atts Shortcode attributes.
+ * @param string $id Video ID.
+ * @param string $video_param video param of the shortcode.
+ *
+ * @return string Shortcode ouput for video.
*/
-function flickr_shortcode_video_markup( $atts ) {
- $atts = array_map( 'esc_attr', $atts );
+function flickr_shortcode_video_markup( $atts, $id, $video_param ) {
+
+ $transient_name = "flickr_video_$id";
+ $video_src = get_transient( $transient_name );
+
+ if ( empty( $video_src ) ) {
+ $video_url = '';
+ if ( ! is_numeric( $video_param ) ) {
+ $video_url = $video_param;
+ } else {
+ // Get the URL of the video from the page of the video.
+ $video_page_content = wp_remote_get( "http://flickr.com/photo.gne?id=$video_param" );
+
+ // Bail if we do not get any info from Flickr.
+ if ( is_wp_error( $video_page_content ) ) {
+ return '';
+ }
+
+ // Extract the URL from the og:url meta tag.
+ preg_match( '/property=\"og:url\"\scontent=\"([^\"]+)\"/', $video_page_content['body'], $matches );
+ if ( empty( $matches[1] ) ) {
+ return '';
+ }
+ $video_url = $matches[1];
+ }
+
+ $provider = 'https://www.flickr.com/services/oembed/';
+ $oembed = _wp_oembed_get_object();
+ $data = (array) $oembed->fetch( $provider, $video_url );
+ if ( empty( $data['html'] ) ) {
+ return '';
+ }
+
+ // Get the embed url.
+ preg_match( '/src=\"([^\"]+)\"/', $data['html'], $matches );
+
+ $embed_url = $matches[1];
+
+ $embed_page = wp_remote_get( $embed_url );
+
+ // Get the video url from embed html markup.
- $photo_vars = "photo_id=$atts[photo_id]";
- if ( isset( $atts['secret'] ) ) {
- $photo_vars .= "&amp;photo_secret=$atts[secret]";
+ preg_match( '/video.+src=\"([^\"]+)\"/', $embed_page['body'], $matches );
+
+ $video_src = $matches[1];
+
+ set_transient( $transient_name, $video_src, 2592000 ); // 30 days transient.
+ }
+
+ $style = 'max-width: 100%;';
+
+ if ( ! empty( $atts['w'] ) && is_numeric( $atts['w'] ) ) {
+ $style .= sprintf( 'width: %dpx;', $atts['w'] );
+ }
+
+ if ( ! empty( $atts['h'] ) && is_numeric( $atts['h'] ) ) {
+ $style .= sprintf( 'height: %dpx;', $atts['h'] );
}
- return <<<EOD
-<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&amp;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&amp;flickr_show_info_box=$atts[show_info]" wmode="opaque" height="$atts[h]" width="$atts[w]"></embed></object>
-EOD;
+ $controls = 'yes' === $atts['controls'] ? 'controls' : '';
+ $autoplay = 'yes' === $atts['autoplay'] ? 'autoplay' : '';
+
+ return sprintf(
+ '<div class="flick_video" style="%s"><video src="%s" %s %s /></div>',
+ esc_attr( $style ),
+ esc_attr( $video_src ),
+ $controls,
+ $autoplay
+ );
+}
+
+/**
+ * Extract the id of the flickr video from the video param.
+ *
+ * @param string $video_param Video parameter of the shortcode.
+ *
+ * @return string|boolean ID of the video or false in case the ID can not be extracted.
+ */
+function flick_shortcode_video_id( $video_param ) {
+ if ( preg_match( '/^https?:\/\/(www\.)?flickr\.com\/.+/', $video_param ) || preg_match( '/^https?:\/\/flic\.kr\/.+/', $video_param ) ) {
+
+ // Extract the video id from the url.
+ preg_match( '/\d+/', $video_param, $matches );
+
+ if ( empty( $matches ) ) {
+ return false;
+ }
+
+ return $matches[0];
+
+ } elseif ( is_numeric( $video_param ) ) {
+ return $video_param;
+ }
+
+ return false;
}
add_shortcode( 'flickr', 'flickr_shortcode_handler' );