<!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>[53072] trunk: Block Editor: Backport the Global Styles Variations endpoint.</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/53072">53072</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/53072","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>youknowriad</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-04-05 09:50:13 +0000 (Tue, 05 Apr 2022)</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'>Block Editor: Backport the Global Styles Variations endpoint.

This include the /global-styles/themes/{theme}/variations rest endpoint into core.
The endpoint will be used by the site editor to display alternative theme styles to the user.

Props gziolo, oandregal.
See <a href="https://core.trac.wordpress.org/ticket/55505">#55505</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkpackagelockjson">trunk/package-lock.json</a></li>
<li><a href="#trunksrcwpincludesclasswpthemejsonresolverphp">trunk/src/wp-includes/class-wp-theme-json-resolver.php</a></li>
<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>
<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>trunk/tests/phpunit/data/themedir1/block-theme/styles/</li>
<li><a href="#trunktestsphpunitdatathemedir1blockthemestylesvariationjson">trunk/tests/phpunit/data/themedir1/block-theme/styles/variation.json</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkpackagelockjson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/package-lock.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/package-lock.json   2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/package-lock.json     2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7750,7 +7750,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                },
</span><span class="cx" style="display: block; padding: 0 10px">                "browserify-aes": {
</span><span class="cx" style="display: block; padding: 0 10px">                        "version": "1.2.0",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 "resolved": "http://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
</ins><span class="cx" style="display: block; padding: 0 10px">                         "integrity": "sha512-+7CHXqGuspUn/Sl5aO7Ea0xWGAtETPXNSAjHo48JfLdPWcMng33Xe4znFvQweqc/uzk5zSOI3H52CYnjCfb5hA==",
</span><span class="cx" style="display: block; padding: 0 10px">                        "dev": true,
</span><span class="cx" style="display: block; padding: 0 10px">                        "requires": {
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpthemejsonresolverphp"></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/class-wp-theme-json-resolver.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme-json-resolver.php    2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/src/wp-includes/class-wp-theme-json-resolver.php      2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -449,4 +449,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">                static::$i18n_schema              = null;
</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">+        /**
+        * Returns the style variations defined by the theme.
+        *
+        * @since 6.0.0
+        *
+        * @return array
+        */
+       public static function get_style_variations() {
+               $variations     = array();
+               $base_directory = get_stylesheet_directory() . '/styles';
+               if ( is_dir( $base_directory ) ) {
+                       $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
+                       $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
+                       ksort( $nested_html_files );
+                       foreach ( $nested_html_files as $path => $file ) {
+                               $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
+                               if ( is_array( $decoded_file ) ) {
+                                       $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
+                                       $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
+                                       if ( empty( $variation['title'] ) ) {
+                                               $variation['title'] = basename( $path, '.json' );
+                                       }
+                                       $variations[] = $variation;
+                               }
+                       }
+               }
+               return $variations;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestglobalstylescontrollerphp"></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-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       2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php 2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,6 +38,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @return void
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function register_routes() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                register_rest_route(
+                       $this->namespace,
+                       '/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
+                       array(
+                               array(
+                                       'methods'             => WP_REST_Server::READABLE,
+                                       'callback'            => array( $this, 'get_theme_items' ),
+                                       'permission_callback' => array( $this, 'get_theme_items_permissions_check' ),
+                                       'args'                => array(
+                                               'stylesheet' => array(
+                                                       'description' => __( 'The theme identifier' ),
+                                                       'type'        => 'string',
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // List themes global styles.
</span><span class="cx" style="display: block; padding: 0 10px">                register_rest_route(
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->namespace,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -585,4 +603,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $response;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Checks if a given request has access to read a single theme global styles config.
+        *
+        * @since 6.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, WP_Error object otherwise.
+        */
+       public function get_theme_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
+               // 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;
+       }
+
+       /**
+        * Returns the given theme global styles variations.
+        *
+        * @since 6.0.0
+        *
+        * @param WP_REST_Request $request The request instance.
+        *
+        * @return WP_REST_Response|WP_Error
+        */
+       public function get_theme_items( $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 )
+                       );
+               }
+
+               $variations = WP_Theme_JSON_Resolver::get_style_variations();
+               $response   = rest_ensure_response( $variations );
+
+               return $response;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunitdatathemedir1blockthemestylesvariationjson"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/data/themedir1/block-theme/styles/variation.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/themedir1/block-theme/styles/variation.json                              (rev 0)
+++ trunk/tests/phpunit/data/themedir1/block-theme/styles/variation.json        2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,23 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+{
+    "version": 2,
+    "settings": {
+        "color": {
+            "palette": [
+                {
+                    "slug": "foreground",
+                    "color": "#3F67C6",
+                    "name": "Foreground"
+                }
+            ]
+        }
+    },
+    "styles": {
+        "blocks": {
+            "core/post-title": {
+                "typography": {
+                    "fontWeight": "700"
+                }
+            }
+        }
+    }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/data/themedir1/block-theme/styles/variation.json
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapirestglobalstylescontrollerphp"></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-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      2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php        2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -117,6 +117,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $routes['/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)'],
</span><span class="cx" style="display: block; padding: 0 10px">                        'Theme global styles route does not have exactly one element'
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertArrayHasKey(
+                       '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
+                       $routes,
+                       'Theme global styles variations route does not exist'
+               );
</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">        public function test_context_param() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -455,4 +460,42 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'settings', $properties, 'Schema properties array does not have "settings" key' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+
+       public function test_get_theme_items() {
+               wp_set_current_user( self::$admin_id );
+               switch_theme( 'block-theme' );
+               $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/block-theme/variations' );
+               $response = rest_get_server()->dispatch( $request );
+               $data     = $response->get_data();
+               $expected = array(
+                       array(
+                               'version'  => 2,
+                               'settings' => array(
+                                       'color' => array(
+                                               'palette' => array(
+                                                       'theme' => array(
+                                                               array(
+                                                                       'slug'  => 'foreground',
+                                                                       'color' => '#3F67C6',
+                                                                       'name'  => 'Foreground',
+                                                               ),
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                               'styles'   => array(
+                                       'blocks' => array(
+                                               'core/post-title' => array(
+                                                       'typography' => array(
+                                                               'fontWeight' => '700',
+                                                       ),
+                                               ),
+                                       ),
+                               ),
+                               'title'    => 'variation',
+                       ),
+               );
+               $this->assertSameSetsWithIndex( $data, $expected );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</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  2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php    2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -136,6 +136,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/global-styles/(?P<id>[\/\w-]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
</ins><span class="cx" style="display: block; padding: 0 10px">                         '/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
</span><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></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    2022-04-05 09:31:06 UTC (rev 53071)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js      2022-04-05 09:50:13 UTC (rev 53072)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9424,6 +9424,26 @@
</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>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+)/variations": {
+            "namespace": "wp/v2",
+            "methods": [
+                "GET"
+            ],
+            "endpoints": [
+                {
+                    "methods": [
+                        "GET"
+                    ],
+                    "args": {
+                        "stylesheet": {
+                            "description": "The theme identifier",
+                            "type": "string",
+                            "required": false
+                        }
+                    }
+                }
+            ]
+        },
</ins><span class="cx" style="display: block; padding: 0 10px">         "/wp/v2/global-styles/themes/(?P<stylesheet>[^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)": {
</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>