Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e72a626
Migrate the vimeo shortcode AMP logic from AMP plugin, add tests
kienstra Oct 31, 2019
c7b39e5
In response to Code Climate, shorten 2 functions
kienstra Oct 31, 2019
5b0c942
Abstract part of filter_vimeo_shortcode() into another function
kienstra Oct 31, 2019
59fbb51
Move the shortcode logic into a new class
kienstra Nov 1, 2019
20a701d
Add new file to the files to be required
kienstra Nov 1, 2019
e24d28e
Update the PHP DocBlocks, correct typos
kienstra Nov 1, 2019
03d11b7
Handle a shortcode of [vimeo 12345]
kienstra Nov 1, 2019
4792db5
Rename Jetpack_AMP_Shortcodes to Jetpack_AMP_Vimeo_Shortcode
kienstra Nov 1, 2019
7751f6e
In response to Code Climate, remove build_attributes_string()
kienstra Nov 1, 2019
18a9db0
Reduce lines of code in response to Code Climate
kienstra Nov 1, 2019
1e877c5
Rename $string to $collected_attributes
kienstra Nov 1, 2019
18863c1
Empty commit to trigger another Travis build
kienstra Nov 1, 2019
3e601b7
Merge branch 'master' into add/vimeo-shortcode-amp
kienstra Nov 1, 2019
db5ac8f
In response to failed Travis build, change file in whitelist
kienstra Nov 1, 2019
949de5c
Rename filter_vimeo_shortcode() to filter_shortcode()
kienstra Nov 1, 2019
cf4342d
Move test_init() above a dataProvider for a lower function
kienstra Nov 1, 2019
86ca9c1
Replace [] syntax for arrays with array()
kienstra Nov 1, 2019
8afd006
Check that the class exists before calling is_amp_request()
kienstra Nov 4, 2019
c66aff4
Move logic from Jetpack_AMP_Vimeo_Shortcode into shortcodes/vimeo.php
kienstra Nov 4, 2019
f92de98
Address 2 Code Climate issues
kienstra Nov 5, 2019
1bf1f8a
Merge branch 'master' into add/vimeo-shortcode-amp
kienstra Nov 5, 2019
35225b8
Code Climate: Attempt to reduce 'Cognitive Complexity'
kienstra Nov 5, 2019
625dc93
Remove references to removed file
kienstra Nov 5, 2019
b1a4cf5
Add parent::tearDown() call, correct @covers tags
kienstra Nov 5, 2019
3f938bf
Give $video_id an initial value of null
kienstra Nov 5, 2019
e8c9449
Stop using build_tag(), and instead simply construct the HTML
kienstra Nov 5, 2019
66e25ac
Remove needless extra line, correct name of tests
kienstra Nov 5, 2019
cb09ba4
Remove jetpack_render_amp_vimeo(), move it into amp_vimeo_shortcode()
kienstra Nov 5, 2019
ce21662
Make $video_id 0 instead of null
kienstra Nov 5, 2019
4e3a047
Also exit if Jetpack_AMP_Support doesn't exist
kienstra Nov 5, 2019
0a6732a
Use absint() for the width and height
kienstra Nov 5, 2019
abe56a0
Add a unit test for jetpack_get_amp_vimeo_dimensions()
kienstra Nov 5, 2019
2ee69b2
In sprintf(), change %d to %s
kienstra Nov 5, 2019
d874efe
Commit Jeremy's diff
kienstra Nov 6, 2019
9330915
Update unit tests for the previous commit
kienstra Nov 6, 2019
451ef9f
Update the name of the function in the test files
kienstra Nov 6, 2019
ee38ebc
Remove @covers tag for jetpack_shortcode_get_vimeo_dimensions()
kienstra Nov 6, 2019
a485777
Empty commit to trigger Travis build
kienstra Nov 6, 2019
1bfc3e6
Move AMP rendering into main shortcode function
jeherve Nov 7, 2019
d0fac0c
Update tests to match 1bfc3e698fc386bd273c7033a3468f38847daac7
jeherve Nov 7, 2019
2344248
Skip some tests on WordPress.com.
jeherve Nov 7, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move the shortcode logic into a new class
In response to a Code Climate
report of the class being too long.
  • Loading branch information
kienstra committed Nov 1, 2019
commit 59fbb51d78e692cbdcd2f8ca019943385611a31a
158 changes: 158 additions & 0 deletions 3rd-party/class.jetpack-amp-shortcodes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

/**
* Makes certain Jetpack shortcodes AMP-compatible.
*
* @see https://github.com/ampproject/amp-wp/blob/ea9e6fb9d262e699ea64978794840ba9868715f6/includes/embeds/class-amp-vimeo-embed.php#L71
*/
class Jetpack_AMP_Shortcodes {

/**
* Apply custom AMP changes onthe frontend.
*/
public static function init() {
// Filter [vimeo] shortcode markup.
add_filter( 'do_shortcode_tag', array( 'Jetpack_AMP_Support', 'filter_vimeo_shortcode' ), 10, 3 );
}

/**
* Filters the Vimeo shortcode to be AMP-compatible.
*
* @param string $html The video player HTML.
* @param string $shortcode_tag The shortcode's tag (name).
* @param array $attr The attributes of the shortcode.
* @return string The filtered HTML.
*/
public static function filter_vimeo_shortcode( $html, $shortcode_tag, $attr ) {
if ( ! Jetpack_AMP_Support::is_amp_request() || 'vimeo' !== $shortcode_tag ) {
return $html;
}

$video_id = self::get_vimeo_id_from_attr( $attr );
if ( empty( $video_id ) ) {
return '';
}

$aspect_ratio = 0.5625;
$default_width = 600;
$default_height = 338;

if ( ! empty( $GLOBALS['content_width'] ) ) {
$width = $GLOBALS['content_width'];
$height = round( $GLOBALS['content_width'] * $aspect_ratio );
} else {
$width = isset( $attr['width'] ) ? $attr['width'] : $default_width;
$height = isset( $attr['height'] ) ? $attr['height'] : $default_height;
}

return self::render_vimeo( compact( 'video_id', 'width', 'height' ) );
}

/**
* Gets the Vimeo ID from the shortcode attributes.
*
* @param array $attr The shortcode attributes to get the ID from.
* @return string|null The ID, as a numeric string, or null.
*/
public static function get_vimeo_id_from_attr( $attr ) {
if ( isset( $attr['id'] ) ) {
return $attr['id'];
} elseif ( isset( $attr['url'] ) ) {
return self::get_vimeo_id_from_url( $attr['url'] );
} elseif ( isset( $attr[0] ) ) {
return self::get_vimeo_id_from_url( $attr[0] );
} elseif ( function_exists( 'shortcode_new_to_old_params' ) ) {
return shortcode_new_to_old_params( $attr );
}
}

/**
* Determines the video ID from the URL.
*
* @param string $url URL.
* @return string The video ID, or an empty string if it's not found.
*/
public static function get_vimeo_id_from_url( $url ) {
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );

if (
in_array( $host, [ 'vimeo.com', 'www.vimeo.com' ], true )
&&
preg_match( ':^/(\d+):', $path, $matches )
) {
// @todo This will not get the private key for unlisted videos (which look like https://vimeo.com/123456789/abcdef0123), but amp-vimeo doesn't support them currently anyway.
return $matches[1];
}

return '';
}

/**
* Renders the Vimeo shortcode as AMP.
*
* @param array $args The arguments.
* @return string The rendered HTML.
*/
public static function render_vimeo( $args ) {
$args = wp_parse_args(
$args,
array( 'video_id' => false )
);

if ( empty( $args['video_id'] ) ) {
return self::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
);
}

return self::build_tag(
'amp-vimeo',
array(
'data-videoid' => $args['video_id'],
'layout' => 'responsive',
'width' => $args['width'],
'height' => $args['height'],
)
);
}

/**
* Generates HTML markup for the given tag, attributes and content.
*
* @param string $tag_name Tag name.
* @param array $attributes Associative array of $attribute => $value pairs.
* @param string $content Inner content for the generated node.
* @return string HTML markup.
*/
public static function build_tag( $tag_name, $attributes = [], $content = '' ) {
$attr_string = self::build_attributes_string( $attributes );
return sprintf( '<%1$s %2$s>%3$s</%1$s>', sanitize_key( $tag_name ), $attr_string, $content );
}

/**
* Generates a string of HTML attributes.
*
* @param array $attributes An associative array of $attribute => $value pairs.
* @return string The HTML attributes.
*/
public static function build_attributes_string( $attributes ) {
$string = [];
foreach ( $attributes as $name => $value ) {
if ( '' === $value ) {
$string[] = sprintf( '%s', sanitize_key( $name ) );
} else {
$string[] = sprintf( '%s="%s"', sanitize_key( $name ), esc_attr( $value ) );
}
}

return implode( ' ', $string );
}
}

add_action( 'init', array( 'Jetpack_AMP_Shortcodes', 'init' ), 1 );
142 changes: 0 additions & 142 deletions 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public static function init() {
// enforce freedom mode for videopress.
add_filter( 'videopress_shortcode_options', array( 'Jetpack_AMP_Support', 'videopress_enable_freedom_mode' ) );

// Filter [vimeo] shortcode markup.
add_filter( 'do_shortcode_tag', array( 'Jetpack_AMP_Support', 'filter_vimeo_shortcode' ), 10, 3 );

// include Jetpack og tags when rendering native AMP head.
add_action( 'amp_post_template_head', array( 'Jetpack_AMP_Support', 'amp_post_jetpack_og_tags' ) );

Expand Down Expand Up @@ -439,145 +436,6 @@ public static function filter_photon_post_image_args_for_stories( $args, $detail

return $args;
}

/**
* Filters the Vimeo shortcode to be AMP-compatible.
*
* @param string $html The video player HTML.
* @param string $shortcode_tag The shortcode's tag (name).
* @param array $attr The attributes of the shortcode.
* @return string The filtered HTML.
*/
public static function filter_vimeo_shortcode( $html, $shortcode_tag, $attr ) {
if ( ! self::is_amp_request() || 'vimeo' !== $shortcode_tag ) {
return $html;
}

$video_id = self::get_vimeo_id_from_attr( $attr );
if ( empty( $video_id ) ) {
return '';
}

$aspect_ratio = 0.5625;
$default_width = 600;
$default_height = 338;

if ( ! empty( $GLOBALS['content_width'] ) ) {
$width = $GLOBALS['content_width'];
$height = round( $GLOBALS['content_width'] * $aspect_ratio );
} else {
$width = isset( $attr['width'] ) ? $attr['width'] : $default_width;
$height = isset( $attr['height'] ) ? $attr['height'] : $default_height;
}

return self::render_vimeo( compact( 'video_id', 'width', 'height' ) );
}

/**
* Gets the Vimeo ID from the shortcode attributes.
*
* @param array $attr The shortcode attributes to get the ID from.
* @return string|null The ID, as a numeric string, or null.
*/
public static function get_vimeo_id_from_attr( $attr ) {
if ( isset( $attr['id'] ) ) {
return $attr['id'];
} elseif ( isset( $attr['url'] ) ) {
return self::get_vimeo_id_from_url( $attr['url'] );
} elseif ( isset( $attr[0] ) ) {
return self::get_vimeo_id_from_url( $attr[0] );
} elseif ( function_exists( 'shortcode_new_to_old_params' ) ) {
return shortcode_new_to_old_params( $attr );
}
}

/**
* Determines the video ID from the URL.
*
* @param string $url URL.
* @return string The video ID, or an empty string if it's not found.
*/
public static function get_vimeo_id_from_url( $url ) {
$host = wp_parse_url( $url, PHP_URL_HOST );
$path = wp_parse_url( $url, PHP_URL_PATH );

if (
in_array( $host, [ 'vimeo.com', 'www.vimeo.com' ], true )
&&
preg_match( ':^/(\d+):', $path, $matches )
) {
// @todo This will not get the private key for unlisted videos (which look like https://vimeo.com/123456789/abcdef0123), but amp-vimeo doesn't support them currently anyway.
return $matches[1];
}

return '';
}

/**
* Renders the Vimeo shortcode as AMP.
*
* @param array $args The arguments.
* @return string The rendered HTML.
*/
public static function render_vimeo( $args ) {
$args = wp_parse_args(
$args,
array( 'video_id' => false )
);

if ( empty( $args['video_id'] ) ) {
return self::build_tag(
'a',
[
'href' => esc_url( $args['url'] ),
'class' => 'amp-wp-embed-fallback',
],
esc_html( $args['url'] )
);
}

return self::build_tag(
'amp-vimeo',
array(
'data-videoid' => $args['video_id'],
'layout' => 'responsive',
'width' => $args['width'],
'height' => $args['height'],
)
);
}

/**
* Generates HTML markup for the given tag, attributes and content.
*
* @param string $tag_name Tag name.
* @param array $attributes Associative array of $attribute => $value pairs.
* @param string $content Inner content for the generated node.
* @return string HTML markup.
*/
public static function build_tag( $tag_name, $attributes = [], $content = '' ) {
$attr_string = self::build_attributes_string( $attributes );
return sprintf( '<%1$s %2$s>%3$s</%1$s>', sanitize_key( $tag_name ), $attr_string, $content );
}

/**
* Generates a string of HTML attributes.
*
* @param array $attributes An associative array of $attribute => $value pairs.
* @return string The HTML attributes.
*/
public static function build_attributes_string( $attributes ) {
$string = [];
foreach ( $attributes as $name => $value ) {
if ( '' === $value ) {
$string[] = sprintf( '%s', sanitize_key( $name ) );
} else {
$string[] = sprintf( '%s="%s"', sanitize_key( $name ), esc_attr( $value ) );
}
}

return implode( ' ', $string );
}
}

add_action( 'init', array( 'Jetpack_AMP_Support', 'init' ), 1 );
Expand Down
Loading