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

REST API: Themes: Expose some additional basic fields #222

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e4740bb
REST API: Themes: Expose some additional basic field
ockham Apr 14, 2020
61b89e9
phpcbf
ockham Apr 15, 2020
e3a03de
Fix unit tests
ockham Apr 15, 2020
4378fd9
Blindly try adding a unit test for the new stylesheet field
ockham Apr 15, 2020
eb662ca
Blindly add unit test for template field
ockham Apr 16, 2020
dcf4047
Fix template unit test
ockham Apr 16, 2020
db0378b
Add a few more fields
ockham Apr 16, 2020
d45471e
Add conditional for screenshot
ockham Apr 16, 2020
dfdb0b2
Use default theme for testing
ockham Apr 16, 2020
d9dfddd
More flexible implementation
ockham Apr 16, 2020
1acdfb7
Add more unit tests, change author to author_name
ockham Apr 16, 2020
f80dbdd
Add author, author_uri, and theme_uri fields
ockham Apr 16, 2020
9b9bace
Have screenshot fall back to empty string
ockham Apr 27, 2020
9282f21
Add format: 'uri' to author_uri and theme_uri
ockham Apr 27, 2020
09e285b
raw/rendered
ockham Apr 27, 2020
364f975
Drop author_name, user raw/rendered for author
ockham Apr 27, 2020
f724571
Moar consistent
ockham Apr 27, 2020
8b16987
Use ->display for all teh things
ockham Apr 27, 2020
2e3a487
Change wording to 'as found in the theme header'
ockham Apr 28, 2020
d917474
Change `type` to `object` for `author_uri`
ockham May 18, 2020
375d25b
Change `type` to `object` for `description`
ockham May 18, 2020
505d96d
Change `type` to `object` for `name`
ockham May 18, 2020
1e3d010
Change `type` to `object` for `author`
ockham May 18, 2020
a0c31d6
Change `type` to `object` for `theme_uri`
ockham May 18, 2020
769a614
Change `type` to `uri` for `raw` `author_uri` and `theme_uri`
ockham May 18, 2020
6c0bcfd
Change `type` to `uri` for `rendered` `author_uri` and `theme_uri`
ockham May 18, 2020
34a179c
Add tags
ockham Jun 2, 2020
d00cfb4
Whitespace/linebreak
ockham Jun 2, 2020
f828777
Add 'items' declaration for 'raw' tags
ockham Jun 3, 2020
d14da40
Use (preferred) $theme->get rather than (backward-compat) ArrayAccess
ockham Jun 3, 2020
4a959ae
Add textdomain field
ockham Jun 3, 2020
fac13e5
Add requires_php and requires_wp fields
ockham Jun 3, 2020
7f5db79
format should be uri, not type
ockham Jun 4, 2020
019eb4b
Switch to "rest_is_field_included"
TimothyBJacobs Jun 7, 2020
fd52d6f
Clean up the schema definition a bit.
TimothyBJacobs Jun 7, 2020
0ec4581
Cleanup tests. Fix stylesheet handling.
TimothyBJacobs Jun 7, 2020
2b0e263
Include fixture in theme list test.
TimothyBJacobs Jun 7, 2020
29ae2dc
We of course have to let the theme work on 5.6 :D
TimothyBJacobs Jun 7, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ public function prepare_item_for_response( $theme, $request ) {
$data = array();
$fields = $this->get_fields_for_response( $request );

$field_mappings = array(
'author' => 'Author',
'author_name' => 'Author Name',
'author_uri' => 'Author URI',
'description' => 'Description',
'name' => 'Name',
'stylesheet' => 'Stylesheet',
'template' => 'Template',
'theme_uri' => 'Theme URI',
'version' => 'Version',
);
TimothyBJacobs marked this conversation as resolved.
Show resolved Hide resolved
$fields_to_include = array_intersect( array_keys( $field_mappings ), $fields );

foreach ( $fields_to_include as $field ) {
$data[ $field ] = $theme[ $field_mappings[ $field ] ];
ockham marked this conversation as resolved.
Show resolved Hide resolved
}

if ( in_array( 'screenshot', $fields, true ) ) {
// Using $theme->get_screenshot() with no args to get absolute URL.
$data['screenshot'] = $theme->get_screenshot();
ockham marked this conversation as resolved.
Show resolved Hide resolved
}

if ( in_array( 'theme_supports', $fields, true ) ) {
$item_schemas = $this->get_item_schema();
$theme_supports = $item_schemas['properties']['theme_supports']['properties'];
Expand Down Expand Up @@ -192,6 +214,46 @@ public function get_item_schema() {
'title' => 'theme',
'type' => 'object',
'properties' => array(
'author' => array(
'description' => __( 'The theme author.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'readonly' => true,
),
'author_name' => array(
'description' => __( 'The theme author\'s name.' ),
'type' => 'string',
'readonly' => true,
),
'author_uri' => array(
'description' => __( 'The website of the theme author.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'readonly' => true,
ockham marked this conversation as resolved.
Show resolved Hide resolved
),
'description' => array(
'description' => __( 'A description of the theme.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'readonly' => true,
),
'name' => array(
'description' => __( 'The name of the theme.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'readonly' => true,
),
'screenshot' => array(
'description' => __( 'A theme screenshot URL.' ),
'type' => 'string',
'readonly' => true,
),
'stylesheet' => array(
'description' => __( 'The theme\'s stylesheet.' ),
'type' => 'string',
'readonly' => true,
),
'template' => array(
'description' => __( 'The theme\'s template name.' ),
'type' => 'string',
'readonly' => true,
),
'theme_supports' => array(
'description' => __( 'Features supported by this theme.' ),
'type' => 'object',
Expand Down Expand Up @@ -458,6 +520,16 @@ public function get_item_schema() {
),
),
),
'theme_uri' => array(
'description' => __( 'The URI of the theme\'s webpage.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'readonly' => true,
ockham marked this conversation as resolved.
Show resolved Hide resolved
),
'version' => array(
'description' => __( 'The theme\'s current version.' ),
'type' => 'string',
'readonly' => true,
),
),
);

Expand Down
99 changes: 98 additions & 1 deletion tests/phpunit/tests/rest-api/rest-themes-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,17 @@ public function test_get_items() {

$this->check_get_theme_response( $response );
$fields = array(
'author',
'author_name',
'author_uri',
'description',
'name',
'screenshot',
'stylesheet',
'template',
'theme_supports',
'theme_uri',
'version',
);
$this->assertEqualSets( $fields, array_keys( $data[0] ) );
}
Expand Down Expand Up @@ -207,8 +217,19 @@ public function test_get_item_schema() {
$response = self::perform_active_theme_request( 'OPTIONS' );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 1, count( $properties ) );
$this->assertEquals( 11, count( $properties ) );
$this->assertArrayHasKey( 'author', $properties );
$this->assertArrayHasKey( 'author_name', $properties );
$this->assertArrayHasKey( 'author_uri', $properties );
$this->assertArrayHasKey( 'description', $properties );
$this->assertArrayHasKey( 'name', $properties );
$this->assertArrayHasKey( 'screenshot', $properties );
$this->assertArrayHasKey( 'stylesheet', $properties );
$this->assertArrayHasKey( 'template', $properties );
$this->assertArrayHasKey( 'theme_supports', $properties );
$this->assertArrayHasKey( 'theme_uri', $properties );
$this->assertArrayHasKey( 'version', $properties );

$theme_supports = $properties['theme_supports']['properties'];
$this->assertEquals( 20, count( $theme_supports ) );
$this->assertArrayHasKey( 'align-wide', $theme_supports );
Expand All @@ -233,6 +254,82 @@ public function test_get_item_schema() {
$this->assertArrayHasKey( 'wp-block-styles', $theme_supports );
}

public function test_theme_author() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'author', $result[0] );
$this->assertSame(
'<a href="http://binarybonsai.com/">Michael Heilemann</a>',
$result[0]['author']
);
}

public function test_theme_author_name() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'author_name', $result[0] );
$this->assertSame( 'Michael Heilemann', $result[0]['author_name'] );
}

public function test_theme_author_uri() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'author_uri', $result[0] );
$this->assertSame( 'http://binarybonsai.com/', $result[0]['author_uri'] );
}

public function test_theme_description() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'description', $result[0] );
$this->assertSame(
'The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>.',
$result[0]['description']
);
}

public function test_theme_name() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'name', $result[0] );
$this->assertSame( 'WordPress Default', $result[0]['name'] );
}

public function test_theme_screenshot() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'screenshot', $result[0] );
$this->assertFalse( $result[0]['screenshot'] ); // No screenshot for default theme
}

public function test_theme_stylesheet() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'stylesheet', $result[0] );
$this->assertSame( 'default', $result[0]['stylesheet'] );
}

public function test_theme_template() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'template', $result[0] );
$this->assertSame( 'default', $result[0]['template'] );
}

public function test_theme_theme_uri() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'theme_uri', $result[0] );
$this->assertNull( $result[0]['theme_uri'] ); // Theme URI is null for default theme.
}

public function test_theme_version() {
$response = self::perform_active_theme_request();
$result = $response->get_data();
$this->assertArrayHasKey( 'version', $result[0] );
$this->assertSame( '1.6', $result[0]['version'] );
}

/**
* @ticket 49037
*/
Expand Down