<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[43985] trunk: REST API: Introduce themes endpoint to expose theme-supports values for the active theme.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/43985">43985</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/43985","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jeremyfelt</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-12 03:32:21 +0000 (Wed, 12 Dec 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: Introduce themes endpoint to expose theme-supports values for the active theme.

In order to correctly render parts of its UI, the new editor needs to be aware of the active theme's post-formats and post-thumbnails support. This data is exposed by querying for the active theme on a new /wp/v2/themes endpoint for sufficiently privileged users.

Merges <a href="https://core.trac.wordpress.org/changeset/43734">[43734]</a>, <a href="https://core.trac.wordpress.org/changeset/43735">[43735]</a> to trunk.

props desrosj.
Fixes <a href="https://core.trac.wordpress.org/ticket/45016">#45016</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestthemescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestthemescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#trunk">trunk/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: trunk
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- trunk        2018-12-12 03:14:47 UTC (rev 43984)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ trunk 2018-12-12 03:32:21 UTC (rev 43985)
</ins><a id="trunk"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: trunk</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/4.9:43557
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/branches/5.0:43681-43682,43684-43687,43720,43726-43727,43729-43731
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/branches/5.0:43681-43682,43684-43687,43720,43726-43727,43729-43731,43734-43735
</ins><span class="cx" style="display: block; padding: 0 10px"> /trunk:18512
</span><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiendpointsclasswprestthemescontrollerphpfromrev43735branches50srcwpincludesrestapiendpointsclasswprestthemescontrollerphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php (from rev 43735, branches/5.0/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php                              (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php        2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,231 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Themes_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 5.0.0
+ */
+
+/**
+ * Core class used to manage themes via the REST API.
+ *
+ * @since 5.0.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Themes_Controller extends WP_REST_Controller {
+
+       /**
+        * Constructor.
+        *
+        * @since 5.0.0
+        */
+       public function __construct() {
+               $this->namespace = 'wp/v2';
+               $this->rest_base = 'themes';
+       }
+
+       /**
+        * Registers the routes for the objects of the controller.
+        *
+        * @since 5.0.0
+        *
+        * @see register_rest_route()
+        */
+       public function register_routes() {
+               register_rest_route(
+                       $this->namespace,
+                       '/' . $this->rest_base,
+                       array(
+                               array(
+                                       'methods'             => WP_REST_Server::READABLE,
+                                       'callback'            => array( $this, 'get_items' ),
+                                       'permission_callback' => array( $this, 'get_items_permissions_check' ),
+                                       'args'                => $this->get_collection_params(),
+                               ),
+                               'schema' => array( $this, 'get_item_schema' ),
+                       )
+               );
+       }
+
+       /**
+        * Checks if a given request has access to read the theme.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
+        */
+       public function get_items_permissions_check( $request ) {
+               if ( ! is_user_logged_in() || ! current_user_can( 'edit_posts' ) ) {
+                       return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to view themes.' ), array( 'status' => rest_authorization_required_code() ) );
+               }
+
+               return true;
+       }
+
+       /**
+        * Retrieves a collection of themes.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
+        */
+       public function get_items( $request ) {
+               // Retrieve the list of registered collection query parameters.
+               $registered = $this->get_collection_params();
+               $themes     = array();
+
+               if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) {
+                       $active_theme = wp_get_theme();
+                       $active_theme = $this->prepare_item_for_response( $active_theme, $request );
+                       $themes[]     = $this->prepare_response_for_collection( $active_theme );
+               }
+
+               $response = rest_ensure_response( $themes );
+
+               $response->header( 'X-WP-Total', count( $themes ) );
+               $response->header( 'X-WP-TotalPages', count( $themes ) );
+
+               return $response;
+       }
+
+       /**
+        * Prepares a single theme output for response.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_Theme        $theme   Theme object.
+        * @param WP_REST_Request $request Request object.
+        * @return WP_REST_Response Response object.
+        */
+       public function prepare_item_for_response( $theme, $request ) {
+               $data   = array();
+               $fields = $this->get_fields_for_response( $request );
+
+               if ( in_array( 'theme_supports', $fields, true ) ) {
+                       $formats                           = get_theme_support( 'post-formats' );
+                       $formats                           = is_array( $formats ) ? array_values( $formats[0] ) : array();
+                       $formats                           = array_merge( array( 'standard' ), $formats );
+                       $data['theme_supports']['formats'] = $formats;
+
+                       $data['theme_supports']['post-thumbnails'] = false;
+                       $post_thumbnails                           = get_theme_support( 'post-thumbnails' );
+
+                       if ( $post_thumbnails ) {
+                               // $post_thumbnails can contain a nested array of post types.
+                               // e.g. array( array( 'post', 'page' ) ).
+                               $data['theme_supports']['post-thumbnails'] = is_array( $post_thumbnails ) ? $post_thumbnails[0] : true;
+                       }
+               }
+
+               $data = $this->add_additional_fields_to_object( $data, $request );
+
+               // Wrap the data in a response object.
+               $response = rest_ensure_response( $data );
+
+               /**
+                * Filters theme data returned from the REST API.
+                *
+                * @since 5.0.0
+                *
+                * @param WP_REST_Response $response The response object.
+                * @param WP_Theme         $theme    Theme object used to create response.
+                * @param WP_REST_Request  $request  Request object.
+                */
+               return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
+       }
+
+       /**
+        * Retrieves the theme's schema, conforming to JSON Schema.
+        *
+        * @since 5.0.0
+        *
+        * @return array Item schema data.
+        */
+       public function get_item_schema() {
+               $schema = array(
+                       '$schema'    => 'http://json-schema.org/draft-04/schema#',
+                       'title'      => 'theme',
+                       'type'       => 'object',
+                       'properties' => array(
+                               'theme_supports' => array(
+                                       'description' => __( 'Features supported by this theme.' ),
+                                       'type'        => 'array',
+                                       'readonly'    => true,
+                                       'properties'  => array(
+                                               'formats'         => array(
+                                                       'description' => __( 'Post formats supported.' ),
+                                                       'type'        => 'array',
+                                                       'readonly'    => true,
+                                               ),
+                                               'post-thumbnails' => array(
+                                                       'description' => __( 'Whether the theme supports post thumbnails.' ),
+                                                       'type'        => array( 'array', 'bool' ),
+                                                       'readonly'    => true,
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+
+               return $this->add_additional_fields_schema( $schema );
+       }
+
+       /**
+        * Retrieves the search params for the themes collection.
+        *
+        * @since 5.0.0
+        *
+        * @return array Collection parameters.
+        */
+       public function get_collection_params() {
+               $query_params = parent::get_collection_params();
+
+               $query_params['status'] = array(
+                       'description'       => __( 'Limit result set to themes assigned one or more statuses.' ),
+                       'type'              => 'array',
+                       'items'             => array(
+                               'enum' => array( 'active' ),
+                               'type' => 'string',
+                       ),
+                       'required'          => true,
+                       'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
+               );
+
+               /**
+                * Filter collection parameters for the themes controller.
+                *
+                * @since 5.0.0
+                *
+                * @param array        $query_params JSON Schema-formatted collection parameters.
+                */
+               return apply_filters( 'rest_themes_collection_params', $query_params );
+       }
+
+       /**
+        * Sanitizes and validates the list of theme status.
+        *
+        * @since 5.0.0
+        *
+        * @param  string|array    $statuses  One or more theme statuses.
+        * @param  WP_REST_Request $request   Full details about the request.
+        * @param  string          $parameter Additional parameter to pass to validation.
+        * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
+        */
+       public function sanitize_theme_status( $statuses, $request, $parameter ) {
+               $statuses = wp_parse_slug_list( $statuses );
+
+               foreach ( $statuses as $status ) {
+                       $result = rest_validate_request_arg( $status, $request, $parameter );
+
+                       if ( is_wp_error( $result ) ) {
+                               return $result;
+                       }
+               }
+
+               return $statuses;
+       }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php        2018-12-12 03:14:47 UTC (rev 43984)
+++ trunk/src/wp-includes/rest-api.php  2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -233,6 +233,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Settings.
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Settings_Controller;
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       // Themes.
+       $controller = new WP_REST_Themes_Controller;
+       $controller->register_routes();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2018-12-12 03:14:47 UTC (rev 43984)
+++ trunk/src/wp-settings.php   2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -235,6 +235,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-users-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-comments-controller.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-settings-controller.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-themes-controller.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-meta-fields.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-comment-meta-fields.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php' );
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestschemasetupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php  2018-12-12 03:14:47 UTC (rev 43984)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php    2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -111,6 +111,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/comments',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/comments/(?P<id>[\\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/settings',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        '/wp/v2/themes',
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $expected_routes, $routes );
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestthemescontrollerphpfromrev43735branches50testsphpunittestsrestapirestthemescontrollerphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php (from rev 43735, branches/5.0/tests/phpunit/tests/rest-api/rest-themes-controller.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php                             (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/rest-themes-controller.php       2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,340 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Themes_Controller functionality.
+ *
+ * @package WordPress
+ * @subpackage REST API
+ */
+
+/**
+ * @group restapi-themes
+ * @group restapi
+ */
+class WP_Test_REST_Themes_Controller extends WP_Test_REST_Controller_Testcase {
+       /**
+        * Subscriber user ID.
+        *
+        * @since 5.0.0
+        *
+        * @var int $subscriber_id
+        */
+       protected static $subscriber_id;
+
+       /**
+        * Contributor user ID.
+        *
+        * @since 5.0.0
+        *
+        * @var int $contributor_id
+        */
+       protected static $contributor_id;
+
+       /**
+        * The current theme object.
+        *
+        * @since 5.0.0
+        *
+        * @var WP_Theme $current_theme
+        */
+       protected static $current_theme;
+
+       /**
+        * The REST API route for themes.
+        *
+        * @since 5.0.0
+        *
+        * @var string $themes_route
+        */
+       protected static $themes_route = '/wp/v2/themes';
+
+       /**
+        * Performs a REST API request for the active theme.
+        *
+        * @since 5.0.0
+        *
+        * @param string $method Optional. Request method. Default GET.
+        * @return WP_REST_Response The request's response.
+        */
+       protected function perform_active_theme_request( $method = 'GET' ) {
+               $request = new WP_REST_Request( $method, self::$themes_route );
+               $request->set_param( 'status', 'active' );
+
+               return rest_get_server()->dispatch( $request );
+       }
+
+       /**
+        * Check that common properties are included in a response.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_REST_Response $response Current REST API response.
+        */
+       protected function check_get_theme_response( $response ) {
+               if ( $response instanceof WP_REST_Response ) {
+                       $headers  = $response->get_headers();
+                       $response = $response->get_data();
+               } else {
+                       $headers = array();
+               }
+
+               $this->assertArrayHasKey( 'X-WP-Total', $headers );
+               $this->assertEquals( 1, $headers['X-WP-Total'] );
+               $this->assertArrayHasKey( 'X-WP-TotalPages', $headers );
+               $this->assertEquals( 1, $headers['X-WP-TotalPages'] );
+       }
+
+       /**
+        * Set up class test fixtures.
+        *
+        * @since 5.0.0
+        *
+        * @param WP_UnitTest_Factory $factory WordPress unit test factory.
+        */
+       public static function wpSetUpBeforeClass( $factory ) {
+               self::$subscriber_id  = $factory->user->create(
+                       array(
+                               'role' => 'subscriber',
+                       )
+               );
+               self::$contributor_id = $factory->user->create(
+                       array(
+                               'role' => 'contributor',
+                       )
+               );
+               self::$current_theme  = wp_get_theme();
+
+               wp_set_current_user( self::$contributor_id );
+       }
+
+       /**
+        * Clean up test fixtures.
+        *
+        * @since 5.0.0
+        */
+       public static function wpTearDownAfterClass() {
+               self::delete_user( self::$subscriber_id );
+               self::delete_user( self::$contributor_id );
+       }
+
+       /**
+        * Set up each test method.
+        *
+        * @since 5.0.0
+        */
+       public function setUp() {
+               parent::setUp();
+
+               wp_set_current_user( self::$contributor_id );
+       }
+
+       /**
+        * Theme routes should be registered correctly.
+        *
+        * @ticket 45016
+        */
+       public function test_register_routes() {
+               $routes = rest_get_server()->get_routes();
+               $this->assertArrayHasKey( self::$themes_route, $routes );
+       }
+
+       /**
+        * Test retrieving a collection of themes.
+        *
+        * @ticket 45016
+        */
+       public function test_get_items() {
+               $response = self::perform_active_theme_request();
+
+               $this->assertEquals( 200, $response->get_status() );
+               $data = $response->get_data();
+
+               $this->check_get_theme_response( $response );
+               $fields = array(
+                       'theme_supports',
+               );
+               $this->assertEqualSets( $fields, array_keys( $data[0] ) );
+       }
+
+       /**
+        * An error should be returned when the user does not have the edit_posts capability.
+        *
+        * @ticket 45016
+        */
+       public function test_get_items_no_permission() {
+               wp_set_current_user( self::$subscriber_id );
+               $response = self::perform_active_theme_request();
+               $this->assertErrorResponse( 'rest_user_cannot_view', $response, 403 );
+       }
+
+       /**
+        * Test an item is prepared for the response.
+        *
+        * @ticket 45016
+        */
+       public function test_prepare_item() {
+               $response = self::perform_active_theme_request();
+               $this->assertEquals( 200, $response->get_status() );
+               $this->check_get_theme_response( $response );
+       }
+
+       /**
+        * Verify the theme schema.
+        *
+        * @ticket 45016
+        */
+       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->assertArrayHasKey( 'theme_supports', $properties );
+
+               $this->assertEquals( 2, count( $properties['theme_supports']['properties'] ) );
+               $this->assertArrayHasKey( 'formats', $properties['theme_supports']['properties'] );
+               $this->assertArrayHasKey( 'post-thumbnails', $properties['theme_supports']['properties'] );
+       }
+
+       /**
+        * Should include relevant data in the 'theme_supports' key.
+        *
+        * @ticket 45016
+        */
+       public function test_theme_supports_formats() {
+               remove_theme_support( 'post-formats' );
+               $response = self::perform_active_theme_request();
+               $result   = $response->get_data();
+               $this->assertTrue( isset( $result[0]['theme_supports'] ) );
+               $this->assertTrue( isset( $result[0]['theme_supports']['formats'] ) );
+               $this->assertSame( array( 'standard' ), $result[0]['theme_supports']['formats'] );
+       }
+
+       /**
+        * Test when a theme only supports some post formats.
+        *
+        * @ticket 45016
+        */
+       public function test_theme_supports_formats_non_default() {
+               add_theme_support( 'post-formats', array( 'aside', 'video' ) );
+               $response = self::perform_active_theme_request();
+               $result   = $response->get_data();
+               $this->assertTrue( isset( $result[0]['theme_supports'] ) );
+               $this->assertTrue( isset( $result[0]['theme_supports']['formats'] ) );
+               $this->assertSame( array( 'standard', 'aside', 'video' ), $result[0]['theme_supports']['formats'] );
+       }
+
+       /**
+        * Test when a theme does not support post thumbnails.
+        *
+        * @ticket 45016
+        */
+       public function test_theme_supports_post_thumbnails_false() {
+               remove_theme_support( 'post-thumbnails' );
+               $response = self::perform_active_theme_request();
+
+               $result = $response->get_data();
+               $this->assertTrue( isset( $result[0]['theme_supports'] ) );
+               $this->assertTrue( isset( $result[0]['theme_supports']['post-thumbnails'] ) );
+               $this->assertFalse( $result[0]['theme_supports']['post-thumbnails'] );
+       }
+
+       /**
+        * Test when a theme supports all post thumbnails.
+        *
+        * @ticket 45016
+        */
+       public function test_theme_supports_post_thumbnails_true() {
+               remove_theme_support( 'post-thumbnails' );
+               add_theme_support( 'post-thumbnails' );
+               $response = self::perform_active_theme_request();
+               $result   = $response->get_data();
+               $this->assertTrue( isset( $result[0]['theme_supports'] ) );
+               $this->assertTrue( $result[0]['theme_supports']['post-thumbnails'] );
+       }
+
+       /**
+        * Test when a theme only supports post thumbnails for certain post types.
+        *
+        * @ticket 45016
+        */
+       public function test_theme_supports_post_thumbnails_array() {
+               remove_theme_support( 'post-thumbnails' );
+               add_theme_support( 'post-thumbnails', array( 'post' ) );
+               $response = self::perform_active_theme_request();
+               $result   = $response->get_data();
+               $this->assertTrue( isset( $result[0]['theme_supports'] ) );
+               $this->assertEquals( array( 'post' ), $result[0]['theme_supports']['post-thumbnails'] );
+       }
+
+       /**
+        * It should be possible to register custom fields to the endpoint.
+        *
+        * @ticket 45016
+        */
+       public function test_get_additional_field_registration() {
+               $schema = array(
+                       'type'        => 'integer',
+                       'description' => 'Some integer of mine',
+                       'enum'        => array( 1, 2, 3, 4 ),
+               );
+
+               register_rest_field(
+                       'theme',
+                       'my_custom_int',
+                       array(
+                               'schema'       => $schema,
+                               'get_callback' => array( $this, 'additional_field_get_callback' ),
+                       )
+               );
+
+               $response = self::perform_active_theme_request( 'OPTIONS' );
+               $data     = $response->get_data();
+
+               $this->assertArrayHasKey( 'my_custom_int', $data['schema']['properties'] );
+               $this->assertEquals( $schema, $data['schema']['properties']['my_custom_int'] );
+
+               $response = self::perform_active_theme_request( 'GET' );
+               $data     = $response->get_data();
+               $this->assertArrayHasKey( 'my_custom_int', $data[0] );
+               $this->assertSame( 2, $data[0]['my_custom_int'] );
+
+               global $wp_rest_additional_fields;
+               $wp_rest_additional_fields = array();
+       }
+
+       /**
+        * Return a value for the custom field.
+        *
+        * @since 5.0.0
+        *
+        * @param array $theme Theme data array.
+        * @return int Additional field value.
+        */
+       public function additional_field_get_callback( $theme ) {
+               return 2;
+       }
+
+       /**
+        * The create_item() method does not exist for themes.
+        */
+       public function test_create_item() {}
+
+       /**
+        * The update_item() method does not exist for themes.
+        */
+       public function test_update_item() {}
+
+       /**
+        * The get_item() method does not exist for themes.
+        */
+       public function test_get_item() {}
+
+       /**
+        * The delete_item() method does not exist for themes.
+        */
+       public function test_delete_item() {}
+
+       /**
+        * Context is not supported for themes.
+        */
+       public function test_context_param() {}
+}
</ins></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js    2018-12-12 03:14:47 UTC (rev 43984)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2018-12-12 03:32:21 UTC (rev 43985)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3525,6 +3525,57 @@
</span><span class="cx" style="display: block; padding: 0 10px">             "_links": {
</span><span class="cx" style="display: block; padding: 0 10px">                 "self": "http://example.org/index.php?rest_route=/wp/v2/settings"
</span><span class="cx" style="display: block; padding: 0 10px">             }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        },
+        "/wp/v2/themes": {
+            "namespace": "wp/v2",
+            "methods": [
+                "GET"
+            ],
+            "endpoints": [
+                {
+                    "methods": [
+                        "GET"
+                    ],
+                    "args": {
+                        "context": {
+                            "required": false,
+                            "description": "Scope under which the request is made; determines fields present in response.",
+                            "type": "string"
+                        },
+                        "page": {
+                            "required": false,
+                            "default": 1,
+                            "description": "Current page of the collection.",
+                            "type": "integer"
+                        },
+                        "per_page": {
+                            "required": false,
+                            "default": 10,
+                            "description": "Maximum number of items to be returned in result set.",
+                            "type": "integer"
+                        },
+                        "search": {
+                            "required": false,
+                            "description": "Limit results to those matching a string.",
+                            "type": "string"
+                        },
+                        "status": {
+                            "required": true,
+                            "description": "Limit result set to themes assigned one or more statuses.",
+                            "type": "array",
+                            "items": {
+                                "enum": [
+                                    "active"
+                                ],
+                                "type": "string"
+                            }
+                        }
+                    }
+                }
+            ],
+            "_links": {
+                "self": "http://example.org/index.php?rest_route=/wp/v2/themes"
+            }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px">     }
</span><span class="cx" style="display: block; padding: 0 10px"> };
</span></span></pre>
</div>
</div>

</body>
</html>