summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/shortcodes/vimeo.php')
-rw-r--r--plugins/jetpack/modules/shortcodes/vimeo.php248
1 files changed, 162 insertions, 86 deletions
diff --git a/plugins/jetpack/modules/shortcodes/vimeo.php b/plugins/jetpack/modules/shortcodes/vimeo.php
index 91492d9c..cc0d3d77 100644
--- a/plugins/jetpack/modules/shortcodes/vimeo.php
+++ b/plugins/jetpack/modules/shortcodes/vimeo.php
@@ -1,14 +1,23 @@
<?php
+/**
+ * Vimeo Shortcode.
+ *
+ * Examples:
+ * [vimeo 141358]
+ * [vimeo http://vimeo.com/141358]
+ * [vimeo 141358 h=500&w=350]
+ * [vimeo id=141358 width=350 height=500]
+ *
+ * <iframe src="http://player.vimeo.com/video/18427511" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/18427511">Eskmo 'We Got More' (Official Video)</a> from <a href="http://vimeo.com/ninjatune">Ninja Tune</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
+ *
+ * @package Jetpack
+ */
-/*
-[vimeo 141358]
-[vimeo http://vimeo.com/141358]
-[vimeo 141358 h=500&w=350]
-[vimeo id=141358 width=350 height=500]
-
-<iframe src="http://player.vimeo.com/video/18427511" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/18427511">Eskmo 'We Got More' (Official Video)</a> from <a href="http://vimeo.com/ninjatune">Ninja Tune</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
-*/
-
+/**
+ * Extract Vimeo ID from shortcode.
+ *
+ * @param array $atts Shortcode attributes.
+ */
function jetpack_shortcode_get_vimeo_id( $atts ) {
if ( isset( $atts[0] ) ) {
$atts[0] = trim( $atts[0], '=' );
@@ -28,74 +37,70 @@ function jetpack_shortcode_get_vimeo_id( $atts ) {
}
/**
- * Convert a Vimeo shortcode into an embed code.
+ * Get video dimensions.
*
- * @param array $atts An array of shortcode attributes.
+ * @since 8.0.0
*
- * @return string The embed code for the Vimeo video.
+ * @param array $attr The attributes of the shortcode.
+ * @param array $old_attr Optional array of attributes from the old shortcode format.
+ *
+ * @return array Width and height.
*/
-function vimeo_shortcode( $atts ) {
+function jetpack_shortcode_get_vimeo_dimensions( $attr, $old_attr = array() ) {
global $content_width;
- $attr = array_map(
- 'intval',
- shortcode_atts(
- array(
- 'id' => 0,
- 'width' => 0,
- 'height' => 0,
- 'autoplay' => 0,
- 'loop' => 0,
- ),
- $atts
- )
- );
-
- if ( isset( $atts[0] ) ) {
- $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts );
- }
-
- if ( ! $attr['id'] ) {
- return '<!-- vimeo error: not a vimeo video -->';
- }
-
- // [vimeo 141358 h=500&w=350]
- $params = shortcode_new_to_old_params( $atts ); // h=500&w=350
- $params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
- parse_str( $params, $args );
+ $default_width = 600;
+ $default_height = 338;
+ $aspect_ratio = $default_height / $default_width;
+ $width = ( ! empty( $attr['width'] ) ? absint( $attr['width'] ) : $default_width );
+ $height = ( ! empty( $attr['height'] ) ? absint( $attr['height'] ) : $default_height );
- $width = intval( $attr['width'] );
- $height = intval( $attr['height'] );
-
- // Support w and h argument as fallback.
- if ( empty( $width ) && isset( $args['w'] ) ) {
- $width = intval( $args['w'] );
+ /*
+ * Support w and h argument as fallbacks.
+ */
+ if (
+ $default_width === $width
+ && ! empty( $old_attr['w'] )
+ ) {
+ $width = absint( $old_attr['w'] );
- if ( empty( $height ) && ! isset( $args['h'] ) ) {
- // The case where w=300 is specified without h=200, otherwise $height
- // will always equal the default of 300, no matter what w was set to.
- $height = round( ( $width / 640 ) * 360 );
+ if (
+ $default_width === $width
+ && empty( $old_attr['h'] )
+ ) {
+ $height = round( $width * $aspect_ratio );
}
}
- if ( empty( $height ) && isset( $args['h'] ) ) {
- $height = (int) $args['h'];
+ if (
+ $default_height === $height
+ && ! empty( $old_attr['h'] )
+ ) {
+ $height = absint( $old_attr['h'] );
- if ( ! isset( $args['w'] ) ) {
- $width = round( ( $height / 360 ) * 640 );
+ if ( empty( $old_attr['w'] ) ) {
+ $width = round( $height * $aspect_ratio );
}
}
- if ( ! $width && ! empty( $content_width ) ) {
+ /*
+ * If we have a content width defined, let it be the new default.
+ */
+ if (
+ $default_width === $width
+ && ! empty( $content_width )
+ ) {
$width = absint( $content_width );
}
- // If setting the width with content_width has failed, defaulting
- if ( ! $width ) {
- $width = 640;
- }
-
- if ( ! $height ) {
+ /*
+ * If we have a custom width, we need a custom height as well
+ * to maintain aspect ratio.
+ */
+ if (
+ $default_width !== $width
+ && $default_height === $height
+ ) {
$height = round( ( $width / 640 ) * 360 );
}
@@ -121,13 +126,53 @@ function vimeo_shortcode( $atts ) {
*/
$height = (int) apply_filters( 'vimeo_height', $height );
+ return array( $width, $height );
+}
+
+/**
+ * Convert a Vimeo shortcode into an embed code.
+ *
+ * @param array $atts An array of shortcode attributes.
+ *
+ * @return string The embed code for the Vimeo video.
+ */
+function vimeo_shortcode( $atts ) {
+ $attr = array_map(
+ 'intval',
+ shortcode_atts(
+ array(
+ 'id' => 0,
+ 'width' => 0,
+ 'height' => 0,
+ 'autoplay' => 0,
+ 'loop' => 0,
+ ),
+ $atts
+ )
+ );
+
+ if ( isset( $atts[0] ) ) {
+ $attr['id'] = jetpack_shortcode_get_vimeo_id( $atts );
+ }
+
+ if ( ! $attr['id'] ) {
+ return '<!-- vimeo error: not a vimeo video -->';
+ }
+
+ // Handle old shortcode params such as h=500&w=350.
+ $params = shortcode_new_to_old_params( $atts );
+ $params = str_replace( array( '&amp;', '&#038;' ), '&', $params );
+ parse_str( $params, $args );
+
+ list( $width, $height ) = jetpack_shortcode_get_vimeo_dimensions( $attr, $args );
+
$url = esc_url( 'https://player.vimeo.com/video/' . $attr['id'] );
// Handle autoplay and loop arguments.
if (
isset( $args['autoplay'] ) && '1' === $args['autoplay'] // Parsed from the embedded URL.
|| $attr['autoplay'] // Parsed from shortcode arguments.
- || in_array( 'autoplay', $atts ) // Catch the argument passed without a value.
+ || in_array( 'autoplay', $atts, true ) // Catch the argument passed without a value.
) {
$url = add_query_arg( 'autoplay', 1, $url );
}
@@ -135,17 +180,29 @@ function vimeo_shortcode( $atts ) {
if (
isset( $args['loop'] ) && '1' === $args['loop'] // Parsed from the embedded URL.
|| $attr['loop'] // Parsed from shortcode arguments.
- || in_array( 'loop', $atts ) // Catch the argument passed without a value.
+ || in_array( 'loop', $atts, true ) // Catch the argument passed without a value.
) {
$url = add_query_arg( 'loop', 1, $url );
}
- $html = sprintf(
- '<div class="embed-vimeo" style="text-align: center;"><iframe src="%1$s" width="%2$u" height="%3$u" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>',
- esc_url( $url ),
- esc_attr( $width ),
- esc_attr( $height )
- );
+ if (
+ class_exists( 'Jetpack_AMP_Support' )
+ && Jetpack_AMP_Support::is_amp_request()
+ ) {
+ $html = sprintf(
+ '<amp-vimeo data-videoid="%1$s" layout="responsive" width="%2$d" height="%3$d"></amp-vimeo>',
+ esc_attr( $attr['id'] ),
+ absint( $width ),
+ absint( $height )
+ );
+ } else {
+ $html = sprintf(
+ '<div class="embed-vimeo" style="text-align: center;"><iframe src="%1$s" width="%2$u" height="%3$u" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>',
+ esc_url( $url ),
+ esc_attr( $width ),
+ esc_attr( $height )
+ );
+ }
/**
* Filter the Vimeo player HTML.
@@ -160,7 +217,6 @@ function vimeo_shortcode( $atts ) {
return $html;
}
-
add_shortcode( 'vimeo', 'vimeo_shortcode' );
/**
@@ -169,8 +225,8 @@ add_shortcode( 'vimeo', 'vimeo_shortcode' );
* @since 3.9
*
* @param array $matches Regex partial matches against the URL passed.
- * @param array $attr Attributes received in embed response
- * @param array $url Requested URL to be embedded
+ * @param array $attr Attributes received in embed response.
+ * @param array $url Requested URL to be embedded.
*
* @return string Return output of Vimeo shortcode with the proper markup.
*/
@@ -190,9 +246,19 @@ function wpcom_vimeo_embed_url_init() {
wp_embed_register_handler( 'wpcom_vimeo_embed_url', '#https?://(.+\.)?vimeo\.com/#i', 'wpcom_vimeo_embed_url' );
}
-// Register handler to modify Vimeo embeds using Jetpack's shortcode output.
-add_action( 'init', 'wpcom_vimeo_embed_url_init' );
+/*
+ * Register handler to modify Vimeo embeds using Jetpack's shortcode output.
+ * This does not happen on WordPress.com, since embeds are handled by core there.
+ */
+if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) {
+ add_action( 'init', 'wpcom_vimeo_embed_url_init' );
+}
+/**
+ * Transform a Vimeo embed iFrame into a Vimeo shortcode.
+ *
+ * @param string $content Post content.
+ */
function vimeo_embed_to_shortcode( $content ) {
if ( ! is_string( $content ) || false === stripos( $content, 'player.vimeo.com/video/' ) ) {
return $content;
@@ -201,8 +267,8 @@ function vimeo_embed_to_shortcode( $content ) {
$regexp = '!<iframe\s+src=[\'"](https?:)?//player\.vimeo\.com/video/(\d+)[\w=&;?]*[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
$regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
- foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
- if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
+ foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
+ if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
continue;
}
@@ -211,7 +277,7 @@ function vimeo_embed_to_shortcode( $content ) {
$params = $match[3];
- if ( 'regexp_ent' == $reg ) {
+ if ( 'regexp_ent' === $reg ) {
$params = html_entity_decode( $params );
}
@@ -232,7 +298,6 @@ function vimeo_embed_to_shortcode( $content ) {
return $content;
}
-
add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
/**
@@ -244,7 +309,8 @@ add_filter( 'pre_kses', 'vimeo_embed_to_shortcode' );
* @since 3.7.0
* @since 3.9.5 One regular expression matches shortcodes and plain URLs.
*
- * @param string $content HTML content
+ * @param string $content HTML content.
+ *
* @return string The content with embeds instead of URLs
*/
function vimeo_link( $content ) {
@@ -255,10 +321,12 @@ function vimeo_link( $content ) {
$shortcode = '(?:\[vimeo\s+[^0-9]*)([0-9]+)(?:\])';
/**
- * http://vimeo.com/12345
- * https://vimeo.com/12345
- * //vimeo.com/12345
- * vimeo.com/some/descender/12345
+ * Regex to look for a Vimeo link.
+ *
+ * - http://vimeo.com/12345
+ * - https://vimeo.com/12345
+ * - //vimeo.com/12345
+ * - vimeo.com/some/descender/12345
*
* Should not capture inside HTML attributes
* [Not] <a href="vimeo.com/12345">Cool Video</a>
@@ -293,9 +361,17 @@ function vimeo_link_callback( $matches ) {
return $matches[0];
}
-/** This filter is documented in modules/shortcodes/youtube.php */
-if ( ! is_admin() && apply_filters( 'jetpack_comments_allow_oembed', true ) ) {
- // We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway, so the iframe gets filtered out.
- // Higher priority because we need it before auto-link and autop get to it
+if (
+ ! is_admin()
+ /** This filter is documented in modules/shortcodes/youtube.php */
+ && apply_filters( 'jetpack_comments_allow_oembed', true )
+ // No need for this on WordPress.com, this is done for multiple shortcodes at a time there.
+ && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
+) {
+ /*
+ * We attach wp_kses_post to comment_text in default-filters.php with priority of 10 anyway,
+ * so the iframe gets filtered out.
+ * Higher priority because we need it before auto-link and autop get to it
+ */
add_filter( 'comment_text', 'vimeo_link', 1 );
}