Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vimeo: add new shortcode format #14418

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 30 additions & 3 deletions modules/shortcodes/vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [vimeo 141358]
* [vimeo http://vimeo.com/141358]
* [vimeo 141358 h=500&w=350]
* [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>
Expand Down Expand Up @@ -52,11 +53,35 @@ function jetpack_shortcode_get_vimeo_dimensions( $attr, $old_attr = array() ) {
$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 fallbacks.
* For width and height, we want to support both formats
* that can be provided in the new shortcode format:
* - for width: width or w
* - for height: height or h
*
* For each variation, the full word takes priority.
*
* If no variation is set, we default to the default width and height values set above.
*/
if ( ! empty( $attr['width'] ) ) {
$width = absint( $attr['width'] );
} elseif ( ! empty( $attr['w'] ) ) {
$width = absint( $attr['w'] );
} else {
$width = $default_width;
}

if ( ! empty( $attr['height'] ) ) {
$height = absint( $attr['height'] );
} elseif ( ! empty( $attr['h'] ) ) {
$height = absint( $attr['h'] );
} else {
$height = $default_height;
}

/*
* Support w and h argument as fallbacks in old shortcode format.
*/
if (
$default_width === $width
Expand Down Expand Up @@ -146,6 +171,8 @@ function vimeo_shortcode( $atts ) {
'height' => 0,
'autoplay' => 0,
'loop' => 0,
'w' => 0,
'h' => 0,
),
$atts
)
Expand Down
19 changes: 18 additions & 1 deletion tests/php/modules/shortcodes/test-class.vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function test_shortcodes_vimeo_url() {
* @covers ::vimeo_shortcode
* @since 3.2
*/
public function test_shortcodes_vimeo_w_h() {
public function test_shortcodes_vimeo_w_h_old_format() {
$video_id = '141358';
$width = '350';
$height = '500';
Expand All @@ -81,6 +81,23 @@ public function test_shortcodes_vimeo_w_h() {
$this->assertContains( 'height="' . $height . '"', $shortcode_content );
}

/**
* @covers ::vimeo_shortcode
* @since 8.2
*/
public function test_shortcodes_vimeo_w_h_new_format() {
$video_id = '141358';
$width = '350';
$height = '500';
$content = '[vimeo ' . $video_id . ' w=' . $width . ' h=' . $height . ']';

$shortcode_content = do_shortcode( $content );

$this->assertContains( 'vimeo.com/video/' . $video_id, $shortcode_content );
$this->assertContains( 'width="' . $width . '"', $shortcode_content );
$this->assertContains( 'height="' . $height . '"', $shortcode_content );
}

/**
* @author scotchfield
* @covers ::vimeo_shortcode
Expand Down