Skip to content

Commit

Permalink
Themes: Introduce responsive embeds support.
Browse files Browse the repository at this point in the history
Responsive embeds is a way for a theme to opt in to WordPress dynamically scaling the width/height of an embed. When a theme supports responsive embeds, a `wp-embed-responsive` class is added to the `<body>` tag. This information is also presented through the REST API for clients to respect.

Props desrosj.
Fixes #45125.


git-svn-id: https://develop.svn.wordpress.org/branches/5.0@43790 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
danielbachhuber committed Oct 22, 2018
1 parent d45455e commit 443771e
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,10 @@ function get_body_class( $class = '' ) {
$classes[] = 'wp-custom-logo';
}

if ( current_theme_supports( 'responsive-embeds' ) ) {
$classes[] = 'wp-embed-responsive';
}

$page = $wp_query->get( 'page' );

if ( ! $page || $page < 2 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ public function prepare_item_for_response( $theme, $request ) {
$formats = array_merge( array( 'standard' ), $formats );
$data['theme_supports']['formats'] = $formats;

$data['theme_supports']['post-thumbnails'] = false;
$post_thumbnails = get_theme_support( 'post-thumbnails' );
$data['theme_supports']['post-thumbnails'] = false;
$data['theme_supports']['responsive-embeds'] = (bool) get_theme_support( 'responsive-embeds' );
$post_thumbnails = get_theme_support( 'post-thumbnails' );

if ( $post_thumbnails ) {
// $post_thumbnails can contain a nested array of post types.
Expand Down Expand Up @@ -156,16 +157,21 @@ public function get_item_schema() {
'type' => 'array',
'readonly' => true,
'properties' => array(
'formats' => array(
'formats' => array(
'description' => __( 'Post formats supported.' ),
'type' => 'array',
'readonly' => true,
),
'post-thumbnails' => array(
'post-thumbnails' => array(
'description' => __( 'Whether the theme supports post thumbnails.' ),
'type' => array( 'array', 'bool' ),
'readonly' => true,
),
'responsive-embeds' => array(
'description' => __( 'Whether the theme supports responsive embedded content.', 'gutenberg' ),
'type' => 'bool',
'readonly' => true,
),
),
),
),
Expand Down
4 changes: 3 additions & 1 deletion src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -2210,12 +2210,14 @@ function get_theme_starter_content() {
* @since 4.1.0 The `title-tag` feature was added
* @since 4.5.0 The `customize-selective-refresh-widgets` feature was added
* @since 4.7.0 The `starter-content` feature was added
* @since 5.0.0 The `responsive-embeds` feature was added.
*
* @global array $_wp_theme_features
*
* @param string $feature The feature being added. Likely core values include 'post-formats',
* 'post-thumbnails', 'html5', 'custom-logo', 'custom-header-uploads',
* 'custom-header', 'custom-background', 'title-tag', 'starter-content', etc.
* 'custom-header', 'custom-background', 'title-tag', 'starter-content',
* 'responsive-embeds/', etc.
* @param mixed $args,... Optional extra arguments to pass along with certain features.
* @return void|bool False on failure, void otherwise.
*/
Expand Down
32 changes: 31 additions & 1 deletion tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ public function test_get_item_schema() {
$this->assertEquals( 1, count( $properties ) );
$this->assertArrayHasKey( 'theme_supports', $properties );

$this->assertEquals( 2, count( $properties['theme_supports']['properties'] ) );
$this->assertEquals( 3, count( $properties['theme_supports']['properties'] ) );
$this->assertArrayHasKey( 'formats', $properties['theme_supports']['properties'] );
$this->assertArrayHasKey( 'post-thumbnails', $properties['theme_supports']['properties'] );
$this->assertArrayHasKey( 'responsive-embeds', $properties['theme_supports']['properties'] );
}

/**
Expand Down Expand Up @@ -222,6 +223,35 @@ public function test_theme_supports_formats_non_default() {
$this->assertSame( array( 'standard', 'aside', 'video' ), $result[0]['theme_supports']['formats'] );
}

/**
* Test when a theme does not support responsive embeds.
*
* @ticket 45016
*/
public function test_theme_supports_responsive_embeds_false() {
remove_theme_support( 'responsive-embeds' );
$response = self::perform_active_theme_request();

$result = $response->get_data();
$this->assertTrue( isset( $result[0]['theme_supports'] ) );
$this->assertTrue( isset( $result[0]['theme_supports']['responsive-embeds'] ) );
$this->assertFalse( $result[0]['theme_supports']['responsive-embeds'] );
}

/**
* Test when a theme supports responsive embeds.
*
* @ticket 45016
*/
public function test_theme_supports_responsive_embeds_true() {
remove_theme_support( 'responsive-embeds' );
add_theme_support( 'responsive-embeds' );
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertTrue( isset( $result[0]['theme_supports'] ) );
$this->assertTrue( $result[0]['theme_supports']['responsive-embeds'] );
}

/**
* Test when a theme does not support post thumbnails.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/tests/theme/support.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,16 @@ function test_supports_menus() {
$this->assertEmpty( get_registered_nav_menus() );
$this->assertFalse( current_theme_supports( 'menus' ) );
}

/**
* @ticket 45125
*/
function test_responsive_embeds() {
add_theme_support( 'responsive-embeds' );
$this->assertTrue( current_theme_supports( 'responsive-embeds' ) );
remove_theme_support( 'responsive-embeds' );
$this->assertFalse( current_theme_supports( 'responsive-embeds' ) );
add_theme_support( 'responsive-embeds' );
$this->assertTrue( current_theme_supports( 'responsive-embeds' ) );
}
}

0 comments on commit 443771e

Please sign in to comment.