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 all 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,10 +115,65 @@ public function prepare_item_for_response( $theme, $request ) {
$data = array();
$fields = $this->get_fields_for_response( $request );

if ( in_array( 'theme_supports', $fields, true ) ) {
if ( rest_is_field_included( 'stylesheet', $fields ) ) {
$data['stylesheet'] = $theme->get_stylesheet();
}

if ( rest_is_field_included( 'template', $fields ) ) {
/**
* Use the get_template() method, not the 'Template' header, for finding the template.
* The 'Template' header is only good for what was written in the style.css, while
* get_template() takes into account where WordPress actually located the theme and
* whether it is actually valid.
*/
$data['template'] = $theme->get_template();
}

$plain_field_mappings = array(
'requires_php' => 'RequiresPHP',
'requires_wp' => 'RequiresWP',
'textdomain' => 'TextDomain',
'version' => 'Version',
);

foreach ( $plain_field_mappings as $field => $header ) {
if ( rest_is_field_included( $field, $fields ) ) {
$data[ $field ] = $theme->get( $header );
}
}

if ( rest_is_field_included( 'screenshot', $fields ) ) {
// Using $theme->get_screenshot() with no args to get absolute URL.
$data['screenshot'] = $theme->get_screenshot() ?: '';
}

$rich_field_mappings = array(
'author' => 'Author',
'author_uri' => 'AuthorURI',
'description' => 'Description',
'name' => 'Name',
'tags' => 'Tags',
'theme_uri' => 'ThemeURI',
);
TimothyBJacobs marked this conversation as resolved.
Show resolved Hide resolved

foreach ( $rich_field_mappings as $field => $header ) {
if ( rest_is_field_included( "{$field}.raw", $fields ) ) {
$data[ $field ]['raw'] = $theme->display( $header, false, true );
}

if ( rest_is_field_included( "{$field}.rendered", $fields ) ) {
$data[ $field ]['rendered'] = $theme->display( $header );
}
}

if ( rest_is_field_included( 'theme_supports', $fields ) ) {
$item_schemas = $this->get_item_schema();
$theme_supports = $item_schemas['properties']['theme_supports']['properties'];
foreach ( $theme_supports as $name => $schema ) {
if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) {
continue;
}

if ( 'formats' === $name ) {
continue;
}
Expand Down Expand Up @@ -192,6 +247,117 @@ public function get_item_schema() {
'title' => 'theme',
'type' => 'object',
'properties' => array(
'stylesheet' => array(
'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
'type' => 'string',
'readonly' => true,
),
'template' => array(
'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ),
'type' => 'string',
'readonly' => true,
),
'author' => array(
'description' => __( 'The theme author.' ),
'type' => 'object',
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'The theme author\'s name, as found in the theme header.' ),
'type' => 'string',
),
'rendered' => array(
'description' => __( 'HTML for the theme author, transformed for display.' ),
'type' => 'string',
),
),
),
'author_uri' => array(
'description' => __( 'The website of the theme author.' ),
'type' => 'object',
'readonly' => true,
ockham marked this conversation as resolved.
Show resolved Hide resolved
'properties' => array(
'raw' => array(
'description' => __( 'The website of the theme author, as found in the theme header.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'The website of the theme author, transformed for display.' ),
'type' => 'string',
ockham marked this conversation as resolved.
Show resolved Hide resolved
'format' => 'uri',
),
),
),
'description' => array(
'description' => __( 'A description of the theme.' ),
'type' => 'object',
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'The theme description, as found in the theme header.' ),
'type' => 'string',
),
'rendered' => array(
'description' => __( 'The theme description, transformed for display.' ),
'type' => 'string',
),
),
),
'name' => array(
'description' => __( 'The name of the theme.' ),
'type' => 'object',
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'The theme name, as found in the theme header.' ),
'type' => 'string',
),
'rendered' => array(
'description' => __( 'The theme name, transformed for display.' ),
'type' => 'string',
),
),
),
'requires_php' => array(
'description' => __( 'The minimum PHP version required for the theme to work.' ),
'type' => 'string',
'readonly' => true,
),
'requires_wp' => array(
'description' => __( 'The minimum WordPress version required for the theme to work.' ),
'type' => 'string',
'readonly' => true,
),
'screenshot' => array(
'description' => __( 'The theme\'s screenshot URL.' ),
'type' => 'string',
'format' => 'uri',
'readonly' => true,
),
'tags' => array(
'description' => __( 'Tags indicating styles and features of the theme.' ),
'type' => 'object',
'readonly' => true,
'properties' => array(
'raw' => array(
'description' => __( 'The theme tags, as found in the theme header.' ),
'type' => 'array',
'items' => array(
'type' => 'string',
),
),
'rendered' => array(
'description' => __( 'The theme tags, transformed for display.' ),
'type' => 'string',
),
),
),
'textdomain' => array(
'description' => __( 'The theme\'s textdomain.' ),
'type' => 'string',
'readonly' => true,
),
'theme_supports' => array(
'description' => __( 'Features supported by this theme.' ),
'type' => 'object',
Expand Down Expand Up @@ -458,6 +624,28 @@ public function get_item_schema() {
),
),
),
'theme_uri' => array(
'description' => __( 'The URI of the theme\'s webpage.' ),
'type' => 'object',
'readonly' => true,
ockham marked this conversation as resolved.
Show resolved Hide resolved
'properties' => array(
'raw' => array(
'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ),
'type' => 'string',
'format' => 'uri',
),
'rendered' => array(
'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ),
'type' => 'string',
'format' => 'uri',
),
),
),
'version' => array(
'description' => __( 'The theme\'s current version.' ),
'type' => 'string',
'readonly' => true,
),
),
);

Expand Down
15 changes: 15 additions & 0 deletions tests/phpunit/data/themedir1/rest-api/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
Theme Name: REST Theme
Theme URI: http://wordpress.org/?search=1&term=2
Description: The 9' foot tall theme.
Version: 1.6
Author: Michael Heilemann
Author URI: http://binarybonsai.com/?search=1&term=2
Tags: holiday, custom-menu
Template: default
Requires at least: 5.3
Requires PHP: 5.6
Text Domain: rest-api
*/


Loading