<!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>[52051] trunk: Add: Global Styles Rest endpoints.</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/52051">52051</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/52051","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jorgefilipecosta</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-08 20:58:56 +0000 (Mon, 08 Nov 2021)</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'>Add: Global Styles Rest endpoints.

This commit ports the global styles rest endpoints from the Gutenberg plugin into the core.

See <a href="https://core.trac.wordpress.org/ticket/54336">#54336</a>.
Props oandregal, aristath, timothyblynjacobs, spacedmonkey, youknowriad.</pre>

<h3>Modified 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="#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="#trunksrcwpincludesrestapiendpointsclasswprestglobalstylescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestglobalstylescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestglobalstylescontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-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-global-styles-controller.php                               (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php 2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,416 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Global_Styles_Controller class
+ *
+ * @package    WordPress
+ * @subpackage REST_API
+ * @since 5.9.0
+ */
+
+/**
+ * Base Global Styles REST API Controller.
+ */
+class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
+       /**
+        * Constructor.
+        * @since 5.9.0
+        */
+       public function __construct() {
+               $this->namespace = 'wp/v2';
+               $this->rest_base = 'global-styles';
+       }
+
+       /**
+        * Registers the controllers routes.
+        *
+        * @since 5.9.0
+        *
+        * @return void
+        */
+       public function register_routes() {
+               // List themes global styles.
+               register_rest_route(
+                       $this->namespace,
+                       '/' . $this->rest_base . '/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
+                       array(
+                               array(
+                                       'methods'             => WP_REST_Server::READABLE,
+                                       'callback'            => array( $this, 'get_theme_item' ),
+                                       'permission_callback' => array( $this, 'get_theme_item_permissions_check' ),
+                                       'args'                => array(
+                                               'stylesheet' => array(
+                                                       'description' => __( 'The theme identifier' ),
+                                                       'type'        => 'string',
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+
+               // Lists/updates a single gloval style variation based on the given id.
+               register_rest_route(
+                       $this->namespace,
+                       '/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
+                       array(
+                               array(
+                                       'methods'             => WP_REST_Server::READABLE,
+                                       'callback'            => array( $this, 'get_item' ),
+                                       'permission_callback' => array( $this, 'get_item_permissions_check' ),
+                                       'args'                => array(
+                                               'id' => array(
+                                                       'description' => __( 'The id of a template' ),
+                                                       'type'        => 'string',
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'methods'             => WP_REST_Server::EDITABLE,
+                                       'callback'            => array( $this, 'update_item' ),
+                                       'permission_callback' => array( $this, 'update_item_permissions_check' ),
+                                       'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
+                               ),
+                               'schema' => array( $this, 'get_public_item_schema' ),
+                       )
+               );
+       }
+
+       /**
+        * Checks if the user has permissions to make the request.
+        *
+        * @since 5.9.0
+        *
+        * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
+        */
+       protected function permissions_check() {
+               // Verify if the current user has edit_theme_options capability.
+               // This capability is required to edit/view/delete templates.
+               if ( ! current_user_can( 'edit_theme_options' ) ) {
+                       return new WP_Error(
+                               'rest_cannot_manage_global_styles',
+                               __( 'Sorry, you are not allowed to access the global styles on this site.' ),
+                               array(
+                                       'status' => rest_authorization_required_code(),
+                               )
+                       );
+               }
+
+               return true;
+       }
+
+       /**
+        * Checks if a given request has access to read a single global styles config.
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
+        */
+       public function get_item_permissions_check( $request ) {
+               return $this->permissions_check( $request );
+       }
+
+       /**
+        * Returns the given global styles config.
+        *
+        * @since 5.9.0
+        *
+        * @param WP_REST_Request $request The request instance.
+        *
+        * @return WP_REST_Response|WP_Error
+        */
+       public function get_item( $request ) {
+               $post = get_post( $request['id'] );
+               if ( ! $post || 'wp_global_styles' !== $post->post_type ) {
+                       return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.' ), array( 'status' => 404 ) );
+               }
+
+               return $this->prepare_item_for_response( $post, $request );
+       }
+
+       /**
+        * Checks if a given request has access to write a single global styles config.
+        *
+        * @since 5.9.0
+        *
+        * @param WP_REST_Request $request Full details about the request.
+        * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
+        */
+       public function update_item_permissions_check( $request ) {
+               return $this->permissions_check( $request );
+       }
+
+       /**
+        * Updates a single global style config.
+        *
+        * @since 5.9.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 update_item( $request ) {
+               $post = get_post( $request['id'] );
+               if ( ! $post || 'wp_global_styles' !== $post->post_type ) {
+                       return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.' ), array( 'status' => 404 ) );
+               }
+
+               $changes = $this->prepare_item_for_database( $request );
+               $result  = wp_update_post( wp_slash( (array) $changes ), true );
+               if ( is_wp_error( $result ) ) {
+                       return $result;
+               }
+
+               $post          = get_post( $request['id'] );
+               $fields_update = $this->update_additional_fields_for_object( $post, $request );
+               if ( is_wp_error( $fields_update ) ) {
+                       return $fields_update;
+               }
+
+               return $this->prepare_item_for_response(
+                       get_post( $request['id'] ),
+                       $request
+               );
+       }
+
+       /**
+        * Prepares a single global styles config for update.
+        *
+        * @since 5.9.0
+        *
+        * @param WP_REST_Request $request Request object.
+        * @return stdClass Changes to pass to wp_update_post.
+        */
+       protected function prepare_item_for_database( $request ) {
+               $changes     = new stdClass();
+               $changes->ID = $request['id'];
+
+               $post            = get_post( $request['id'] );
+               $existing_config = array();
+               if ( $post ) {
+                       $existing_config     = json_decode( $post->post_content, true );
+                       $json_decoding_error = json_last_error();
+                       if ( JSON_ERROR_NONE !== $json_decoding_error || ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) ||
+                               ! $existing_config['isGlobalStylesUserThemeJSON'] ) {
+                               $existing_config = array();
+                       }
+               }
+
+               if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) {
+                       $config = array();
+                       if ( isset( $request['styles'] ) ) {
+                               $config['styles'] = $request['styles'];
+                       } elseif ( isset( $existing_config['styles'] ) ) {
+                               $config['styles'] = $existing_config['styles'];
+                       }
+                       if ( isset( $request['settings'] ) ) {
+                               $config['settings'] = $request['settings'];
+                       } elseif ( isset( $existing_config['settings'] ) ) {
+                               $config['settings'] = $existing_config['settings'];
+                       }
+                       $config['isGlobalStylesUserThemeJSON'] = true;
+                       $config['version']                     = WP_Theme_JSON::LATEST_SCHEMA;
+                       $changes->post_content                 = wp_json_encode( $config );
+               }
+
+               // Post title.
+               if ( isset( $request['title'] ) ) {
+                       if ( is_string( $request['title'] ) ) {
+                               $changes->post_title = $request['title'];
+                       } elseif ( ! empty( $request['title']['raw'] ) ) {
+                               $changes->post_title = $request['title']['raw'];
+                       }
+               }
+
+               return $changes;
+       }
+
+       /**
+        * Prepare a global styles config output for response.
+        *
+        * @since 5.9.0
+        *
+        * @param WP_Post         $post    Global Styles post object.
+        * @param WP_REST_Request $request Request object.
+        *
+        * @return WP_REST_Response $data
+        */
+       public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
+               $config                           = json_decode( $post->post_content, true );
+               $is_global_styles_user_theme_json = isset( $config['isGlobalStylesUserThemeJSON'] ) && true === $config['isGlobalStylesUserThemeJSON'];
+               $result                           = array(
+                       'id'       => $post->ID,
+                       'settings' => ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(),
+                       'styles'   => ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(),
+                       'title'    => array(
+                               'raw'      => $post->post_title,
+                               'rendered' => get_the_title( $post ),
+                       ),
+               );
+               $result                           = $this->add_additional_fields_to_object( $result, $request );
+               $response                         = rest_ensure_response( $result );
+               $links                            = $this->prepare_links( $post->id );
+               $response->add_links( $links );
+               if ( ! empty( $links['self']['href'] ) ) {
+                       $actions = $this->get_available_actions();
+                       $self    = $links['self']['href'];
+                       foreach ( $actions as $rel ) {
+                               $response->add_link( $rel, $self );
+                       }
+               }
+
+               return $response;
+       }
+
+
+       /**
+        * Prepares links for the request.
+        *
+        * @since 5.9.0
+        *
+        * @param integer $id ID.
+        * @return array Links for the given post.
+        */
+       protected function prepare_links( $id ) {
+               $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
+
+               $links = array(
+                       'self'       => array(
+                               'href' => rest_url( trailingslashit( $base ) . $id ),
+                       ),
+                       'collection' => array(
+                               'href' => rest_url( $base ),
+                       ),
+               );
+
+               return $links;
+       }
+
+       /**
+        * Get the link relations available for the post and current user.
+        *
+        * @since 5.9.0
+        *
+        * @return array List of link relations.
+        */
+       protected function get_available_actions() {
+               $rels = array();
+
+               $post_type = get_post_type_object( 'wp_global_styles' );
+               if ( current_user_can( $post_type->cap->publish_posts ) ) {
+                       $rels[] = 'https://api.w.org/action-publish';
+               }
+
+               return $rels;
+       }
+
+       /**
+        * Retrieves the query params for the global styles collection.
+        *
+        * @since 5.9.0
+        *
+        * @return array Collection parameters.
+        */
+       public function get_collection_params() {
+               return array();
+       }
+
+       /**
+        * Retrieves the global styles type' schema, conforming to JSON Schema.
+        *
+        * @since 5.9.0
+        *
+        * @return array Item schema data.
+        */
+       public function get_item_schema() {
+               if ( $this->schema ) {
+                       return $this->add_additional_fields_schema( $this->schema );
+               }
+
+               $schema = array(
+                       '$schema'    => 'http://json-schema.org/draft-04/schema#',
+                       'title'      => 'wp_global_styles',
+                       'type'       => 'object',
+                       'properties' => array(
+                               'id'       => array(
+                                       'description' => __( 'ID of global styles config.' ),
+                                       'type'        => 'string',
+                                       'context'     => array( 'embed', 'view', 'edit' ),
+                                       'readonly'    => true,
+                               ),
+                               'styles'   => array(
+                                       'description' => __( 'Global styles.' ),
+                                       'type'        => array( 'object' ),
+                                       'context'     => array( 'view', 'edit' ),
+                               ),
+                               'settings' => array(
+                                       'description' => __( 'Global settings.' ),
+                                       'type'        => array( 'object' ),
+                                       'context'     => array( 'view', 'edit' ),
+                               ),
+                               'title'    => array(
+                                       'description' => __( 'Title of the global styles variation.' ),
+                                       'type'        => array( 'object', 'string' ),
+                                       'default'     => '',
+                                       'context'     => array( 'embed', 'view', 'edit' ),
+                                       'properties'  => array(
+                                               'raw'      => array(
+                                                       'description' => __( 'Title for the global styles variation, as it exists in the database.' ),
+                                                       'type'        => 'string',
+                                                       'context'     => array( 'view', 'edit', 'embed' ),
+                                               ),
+                                               'rendered' => array(
+                                                       'description' => __( 'HTML title for the post, transformed for display.' ),
+                                                       'type'        => 'string',
+                                                       'context'     => array( 'view', 'edit', 'embed' ),
+                                                       'readonly'    => true,
+                                               ),
+                                       ),
+                               ),
+                       ),
+               );
+
+               $this->schema = $schema;
+
+               return $this->add_additional_fields_schema( $this->schema );
+       }
+
+       /**
+        * Checks if a given request has access to read a single theme global styles config.
+        *
+        * @since 5.9.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, WP_Error object otherwise.
+        */
+       public function get_theme_item_permissions_check( $request ) {
+               return $this->permissions_check( $request );
+       }
+
+       /**
+        * Returns the given theme global styles config.
+        *
+        * @since 5.9.0
+        *
+        * @param WP_REST_Request $request The request instance.
+        *
+        * @return WP_REST_Response|WP_Error
+        */
+       public function get_theme_item( $request ) {
+               if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) {
+                       // This endpoint only supports the active theme for now.
+                       return new WP_Error(
+                               'rest_theme_not_found',
+                               __( 'Theme not found.' ),
+                               array( 'status' => 404 )
+                       );
+               }
+
+               $theme    = WP_Theme_JSON_Resolver::get_merged_data( 'theme' );
+               $styles   = $theme->get_raw_data()['styles'];
+               $settings = $theme->get_settings();
+               $result   = array(
+                       'settings' => $settings,
+                       'styles'   => $styles,
+               );
+               $response = rest_ensure_response( $result );
+
+               return $response;
+       }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestthemescontrollerphp"></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/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      2021-11-08 20:32:39 UTC (rev 52050)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php        2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -316,6 +316,35 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $response->add_links( $this->prepare_links( $theme ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) {
+                       // This creates a record for the current theme if not existent.
+                       $id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id();
+               } else {
+                       $wp_query_args       = array(
+                               'post_status'    => 'publish',
+                               'post_type'      => 'wp_global_styles',
+                               'posts_per_page' => 1,
+                               'no_found_rows'  => true,
+                               'fields'         => 'ids',
+                               'tax_query'      => array(
+                                       array(
+                                               'taxonomy' => 'wp_theme',
+                                               'field'    => 'name',
+                                               'terms'    => $theme->get_stylesheet(),
+                                       ),
+                               ),
+                       );
+                       $global_styles_query = new WP_Query( $wp_query_args );
+                       $id                  = ! empty( $global_styles_query->posts ) ? array_shift( $global_styles_query->posts ) : null;
+               }
+
+               if ( $id ) {
+                       $response->add_link(
+                               'https://api.w.org/user-global-styles',
+                               rest_url( 'wp/v2/global-styles/' . $id )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /**
</span><span class="cx" style="display: block; padding: 0 10px">                 * Filters theme data returned from the REST API.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span></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        2021-11-08 20:32:39 UTC (rev 52050)
+++ trunk/src/wp-includes/rest-api.php  2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,6 +301,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $controller = new WP_REST_Block_Types_Controller();
</span><span class="cx" style="display: block; padding: 0 10px">        $controller->register_routes();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Global Styles.
+       $controller = new WP_REST_Global_Styles_Controller;
+       $controller->register_routes();
+
</ins><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></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 2021-11-08 20:32:39 UTC (rev 52050)
+++ trunk/src/wp-settings.php   2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,6 +254,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-posts-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-attachments-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-global-styles-controller.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-types-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-statuses-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-revisions-controller.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestglobalstylescontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/rest-global-styles-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-global-styles-controller.php                              (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php        2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,132 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Global_Styles_Controller functionality.
+ *
+ * @package WordPress
+ * @subpackage REST API
+ */
+
+/**
+ * @group restapi-global-styles
+ * @group restapi
+ */
+class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Testcase {
+       /**
+        * @var int
+        */
+       protected static $admin_id;
+
+       /**
+        * @var int
+        */
+       protected static $global_styles_id;
+
+       private function find_and_normalize_global_styles_by_id( $global_styles, $id ) {
+               foreach ( $global_styles as $style ) {
+                       if ( $style['id'] === $id ) {
+                               unset( $style['_links'] );
+                               return $style;
+                       }
+               }
+
+               return null;
+       }
+
+       public function set_up() {
+               parent::set_up();
+               switch_theme( 'tt1-blocks' );
+       }
+
+       /**
+        * Create fake data before our tests run.
+        *
+        * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
+        */
+       public static function wpSetupBeforeClass( $factory ) {
+               self::$admin_id = $factory->user->create(
+                       array(
+                               'role' => 'administrator',
+                       )
+               );
+               // This creates the global styles for the current theme.
+               self::$global_styles_id = wp_insert_post(
+                       array(
+                               'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
+                               'post_status'  => 'publish',
+                               'post_title'   => __( 'Custom Styles', 'default' ),
+                               'post_type'    => 'wp_global_styles',
+                               'post_name'    => 'wp-global-styles-tt1-blocks',
+                               'tax_input'    => array(
+                                       'wp_theme' => 'tt1-blocks',
+                               ),
+                       ),
+                       true
+               );
+       }
+
+       public function test_register_routes() {
+               $routes = rest_get_server()->get_routes();
+               $this->assertArrayHasKey( '/wp/v2/global-styles/(?P<id>[\/\w-]+)', $routes );
+       }
+
+       public function test_context_param() {
+               // TODO: Implement test_context_param() method.
+               $this->markTestIncomplete();
+       }
+
+       public function test_get_items() {
+               $this->markTestIncomplete();
+       }
+
+       public function test_get_item() {
+               wp_set_current_user( self::$admin_id );
+               $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+               unset( $data['_links'] );
+
+               $this->assertEquals(
+                       array(
+                               'id'       => self::$global_styles_id,
+                               'title'    => array(
+                                       'raw'      => 'Custom Styles',
+                                       'rendered' => 'Custom Styles',
+                               ),
+                               'settings' => new stdClass(),
+                               'styles'   => new stdClass(),
+                       ),
+                       $data
+               );
+       }
+
+       public function test_create_item() {
+               $this->markTestIncomplete();
+       }
+
+       public function test_update_item() {
+               wp_set_current_user( self::$admin_id );
+               $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
+               $request->set_body_params(
+                       array(
+                               'title' => 'My new global styles title',
+                       )
+               );
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+               $this->assertEquals( 'My new global styles title', $data['title']['raw'] );
+       }
+
+       public function test_delete_item() {
+               $this->markTestIncomplete();
+       }
+
+       public function test_prepare_item() {
+               // TODO: Implement test_prepare_item() method.
+               $this->markTestIncomplete();
+       }
+
+       public function test_get_item_schema() {
+               // TODO: Implement test_get_item_schema() method.
+               $this->markTestIncomplete();
+       }
+}
</ins></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  2021-11-08 20:32:39 UTC (rev 52050)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php    2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -124,6 +124,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/users/(?P<user_id>(?:[\\d]+|me))/application-passwords/(?P<uuid>[\\w\\-]+)',
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
+                       '/wp/v2/global-styles/themes/(?P<stylesheet>[^.\/]+(?:\/[^.\/]+)?)',
</ins><span class="cx" style="display: block; padding: 0 10px">                         '/wp/v2/search',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/block-renderer/(?P<name>[a-z0-9-]+/[a-z0-9-]+)',
</span><span class="cx" style="display: block; padding: 0 10px">                        '/wp/v2/block-types',
</span></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    2021-11-08 20:32:39 UTC (rev 52050)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2021-11-08 20:58:56 UTC (rev 52051)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6560,6 +6560,101 @@
</span><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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        "/wp/v2/global-styles/themes/(?P<stylesheet>[^.\\/]+(?:\\/[^.\\/]+)?)": {
+            "namespace": "wp/v2",
+            "methods": [
+                "GET"
+            ],
+            "endpoints": [
+                {
+                    "methods": [
+                        "GET"
+                    ],
+                    "args": {
+                        "stylesheet": {
+                            "description": "The theme identifier",
+                            "type": "string",
+                            "required": false
+                        }
+                    }
+                }
+            ]
+        },
+        "/wp/v2/global-styles/(?P<id>[\\/\\w-]+)": {
+            "namespace": "wp/v2",
+            "methods": [
+                "GET",
+                "POST",
+                "PUT",
+                "PATCH"
+            ],
+            "endpoints": [
+                {
+                    "methods": [
+                        "GET"
+                    ],
+                    "args": {
+                        "id": {
+                            "description": "The id of a template",
+                            "type": "string",
+                            "required": false
+                        }
+                    }
+                },
+                {
+                    "methods": [
+                        "POST",
+                        "PUT",
+                        "PATCH"
+                    ],
+                    "args": {
+                        "styles": {
+                            "description": "Global styles.",
+                            "type": [
+                                "object"
+                            ],
+                            "required": false
+                        },
+                        "settings": {
+                            "description": "Global settings.",
+                            "type": [
+                                "object"
+                            ],
+                            "required": false
+                        },
+                        "title": {
+                            "description": "Title of the global styles variation.",
+                            "type": [
+                                "object",
+                                "string"
+                            ],
+                            "properties": {
+                                "raw": {
+                                    "description": "Title for the global styles variation, as it exists in the database.",
+                                    "type": "string",
+                                    "context": [
+                                        "view",
+                                        "edit",
+                                        "embed"
+                                    ]
+                                },
+                                "rendered": {
+                                    "description": "HTML title for the post, transformed for display.",
+                                    "type": "string",
+                                    "context": [
+                                        "view",
+                                        "edit",
+                                        "embed"
+                                    ],
+                                    "readonly": true
+                                }
+                            },
+                            "required": false
+                        }
+                    }
+                }
+            ]
+        },
</ins><span class="cx" style="display: block; padding: 0 10px">         "/wp/v2/settings": {
</span><span class="cx" style="display: block; padding: 0 10px">             "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px">             "methods": [
</span></span></pre>
</div>
</div>

</body>
</html>