Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion 3rd-party/class.jetpack-amp-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Jetpack_AMP_Support {

/**
* Apply custom AMP changes onthe frontend.
* Apply custom AMP changes on the front-end.
*/
public static function init() {

Expand Down
163 changes: 105 additions & 58 deletions modules/shortcodes/vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,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 -->';
}

// 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 );

$width = intval( $attr['width'] );
$height = intval( $attr['height'] );
$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 );

// 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 );
}

Expand All @@ -130,6 +126,46 @@ 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.
Expand All @@ -149,12 +185,24 @@ function vimeo_shortcode( $atts ) {
$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.
Expand All @@ -169,7 +217,6 @@ function vimeo_shortcode( $atts ) {

return $html;
}

add_shortcode( 'vimeo', 'vimeo_shortcode' );

/**
Expand Down
150 changes: 150 additions & 0 deletions tests/php/modules/shortcodes/test-class.vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

class WP_Test_Jetpack_Shortcodes_Vimeo extends WP_UnitTestCase {

/**
* Tear down each test.
*
* @inheritDoc
*/
public function tearDown() {
unset( $GLOBALS['content_width'] );
parent::tearDown();
}

/**
* @author scotchfield
* @covers ::vimeo_shortcode
Expand Down Expand Up @@ -95,6 +105,11 @@ public function test_shortcodes_vimeo_width_height() {
* @since 3.9
*/
public function test_replace_url_with_iframe_in_the_content() {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'Embeds are handled by core on WordPress.com. See D27860-code' );
return;
}

global $post;

$video_id = '141358';
Expand Down Expand Up @@ -223,4 +238,139 @@ function test_vimeo_embed_to_shortcode_5() {

$this->assertEquals( $expected_shortcode, $shortcode );
}

/**
* Gets the test data for the Vimeo shortcodes.
*
* @return array An associative array of test data.
*/
public function get_vimeo_shortcode_data() {
return array(
'empty_attr_array' => array(
array(),
'<!-- vimeo error: not a vimeo video -->',
),
'no_width_or_height_in_attr' => array(
array( 'id' => '24246' ),
'<amp-vimeo data-videoid="24246" layout="responsive" width="600" height="338"></amp-vimeo>'
),
'normal_attributes_present' => array(
array(
'id' => '623422',
'width' => '900',
'height' => '1200',
),
'<amp-vimeo data-videoid="623422" layout="responsive" width="900" height="1200"></amp-vimeo>'
),
);
}

/**
* Tests that the Vimeo shortcode filter produces the right HTML.
*
* @dataProvider get_vimeo_shortcode_data
*
* @param array $attr The shortcode attributes.
* @param string $expected The expected return value.
*/
public function test_jetpack_amp_vimeo_shortcode( $attr, $expected ) {
// Test AMP version. On AMP views, we only show a link.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
return;
}

unset( $GLOBALS['content_width'] );
add_filter( 'jetpack_is_amp_request', '__return_true' );

$this->assertEquals( $expected, vimeo_shortcode( $attr ) );
}

/**
* Tests the Vimeo shortcode filter in an AMP view when there is a global $content_width value.
*/
public function test_jetpack_amp_vimeo_shortcode_global_content_width() {
// Test AMP version. On AMP views, we only show a link.
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
self::markTestSkipped( 'WordPress.com does not run the latest version of the AMP plugin yet.' );
return;
}

add_filter( 'jetpack_is_amp_request', '__return_true' );

$video_id = '624432';
$content_width = 650;
$expected_height = 366;
$GLOBALS['content_width'] = $content_width;

$this->assertEquals(
'<amp-vimeo data-videoid="' . $video_id .'" layout="responsive" width="' . $content_width . '" height="' . $expected_height .'"></amp-vimeo>',
vimeo_shortcode( array( 'id' => $video_id ) )
);
}

/**
* Gets the testing data for jetpack_shortcode_get_vimeo_dimensions().
*
* @return array The testing data.
*/
public function get_vimeo_dimensions_data() {
return array(
'no_width_or_height' => array(
array(),
array( 600, 338 ),
),
'only_width' => array(
array( 'width' => 800 ),
array( 800, 450 ),
),
'only_height' => array(
array( 'height' => 400 ),
array( 600, 400 ),
),
'width_and_height' => array(
array(
'width' => 600,
'height' => 400,
),
array( 600, 400 ),
),
'width_and_height_as_strings' => array(
array(
'width' => '600',
'height' => '400',
),
array( 600, 400 ),
),
);
}

/**
* Tests jetpack_shortcode_get_vimeo_dimensions, when there is no global $content_width.
*
* @dataProvider get_vimeo_dimensions_data
* @covers ::jetpack_shortcode_get_vimeo_dimensions()
*
* @param array $attr The shortcode attributes.
* @param array $expected The expected dimensions.
*/
function test_jetpack_shortcode_get_vimeo_dimensions_no_global_content_width( $attr, $expected ) {
unset( $GLOBALS['content_width'] );
$this->assertEquals( $expected, jetpack_shortcode_get_vimeo_dimensions( $attr ) );
}

/**
* Tests jetpack_shortcode_get_vimeo_dimensions, when there is a global $content_width.
*
* @covers ::jetpack_shortcode_get_vimeo_dimensions()
*/
function test_jetpack_shortcode_get_vimeo_dimensions_with_global_content_width() {
$width = 500;
$height = 281;
$GLOBALS['content_width'] = $width;
$this->assertEquals(
array( $width, $height ),
jetpack_shortcode_get_vimeo_dimensions( array() )
);
}
}