<!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>[58262] trunk: Block Themes: Add support for relative URLs in top-level theme.json styles</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/58262">58262</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/58262","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>noisysocks</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-05-31 01:17:50 +0000 (Fri, 31 May 2024)</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 Themes: Add support for relative URLs in top-level theme.json styles

Allow using relative `file:` URLs in top-level theme.json properties such as
`styles.background`, and modify the REST API to provide clients with the
absolute URLs via a 'https://api.w.org/theme-file' attribute in the `_links`
array.

Props ramonopoly.
Fixes <a href="https://core.trac.wordpress.org/ticket/61273">#61273</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpthemejsonresolverphp">trunk/src/wp-includes/class-wp-theme-json-resolver.php</a></li>
<li><a href="#trunksrcwpincludesglobalstylesandsettingsphp">trunk/src/wp-includes/global-styles-and-settings.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="#trunksrcwpincludesrestapiendpointsclasswprestglobalstylesrevisionscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php</a></li>
<li><a href="#trunktestsphpunitdatathemedir1blockthemestylesvariationbjson">trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json</a></li>
<li><a href="#trunktestsphpunittestsrestapirestglobalstylescontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpThemeJsonResolverphp">trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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    2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/src/wp-includes/class-wp-theme-json-resolver.php      2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -744,4 +744,82 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">                return $variations;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Resolves relative paths in theme.json styles to theme absolute paths
+        * and returns them in an array that can be embedded
+        * as the value of `_link` object in REST API responses.
+        *
+        * @since 6.6.0
+        *
+        * @param WP_Theme_JSON  $theme_json A theme json instance.
+        * @return array An array of resolved paths.
+        */
+       public static function get_resolved_theme_uris( $theme_json ) {
+               $resolved_theme_uris = array();
+
+               if ( ! $theme_json instanceof WP_Theme_JSON ) {
+                       return $resolved_theme_uris;
+               }
+
+               $theme_json_data = $theme_json->get_raw_data();
+
+               // Top level styles.
+               $background_image_url = isset( $theme_json_data['styles']['background']['backgroundImage']['url'] ) ? $theme_json_data['styles']['background']['backgroundImage']['url'] : null;
+
+               /*
+                * The same file convention when registering web fonts.
+                * See: WP_Font_Face_Resolver:: to_theme_file_uri.
+                */
+               $placeholder = 'file:./';
+               if (
+                       isset( $background_image_url ) &&
+                       is_string( $background_image_url ) &&
+                       // Skip if the src doesn't start with the placeholder, as there's nothing to replace.
+                       str_starts_with( $background_image_url, $placeholder )
+               ) {
+                       $file_type          = wp_check_filetype( $background_image_url );
+                       $src_url            = str_replace( $placeholder, '', $background_image_url );
+                       $resolved_theme_uri = array(
+                               'name'   => $background_image_url,
+                               'href'   => sanitize_url( get_theme_file_uri( $src_url ) ),
+                               'target' => 'styles.background.backgroundImage.url',
+                       );
+                       if ( isset( $file_type['type'] ) ) {
+                               $resolved_theme_uri['type'] = $file_type['type'];
+                       }
+                       $resolved_theme_uris[] = $resolved_theme_uri;
+               }
+
+               return $resolved_theme_uris;
+       }
+
+       /**
+        * Resolves relative paths in theme.json styles to theme absolute paths
+        * and merges them with incoming theme JSON.
+        *
+        * @since 6.6.0
+        *
+        * @param WP_Theme_JSON  $theme_json A theme json instance.
+        * @return WP_Theme_JSON Theme merged with resolved paths, if any found.
+        */
+       public static function resolve_theme_file_uris( $theme_json ) {
+               $resolved_urls = static::get_resolved_theme_uris( $theme_json );
+               if ( empty( $resolved_urls ) ) {
+                       return $theme_json;
+               }
+
+               $resolved_theme_json_data = array(
+                       'version' => WP_Theme_JSON::LATEST_SCHEMA,
+               );
+
+               foreach ( $resolved_urls as $resolved_url ) {
+                       $path = explode( '.', $resolved_url['target'] );
+                       _wp_array_set( $resolved_theme_json_data, $path, $resolved_url['href'] );
+               }
+
+               $theme_json->merge( new WP_Theme_JSON( $resolved_theme_json_data ) );
+
+               return $theme_json;
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludesglobalstylesandsettingsphp"></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/global-styles-and-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/global-styles-and-settings.php      2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/src/wp-includes/global-styles-and-settings.php        2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -139,6 +139,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.9.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 6.1.0 Added 'base-layout-styles' support.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.6.0 Resolves relative paths in theme.json styles to theme absolute paths.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array $types Optional. Types of styles to load.
</span><span class="cx" style="display: block; padding: 0 10px">  *                     It accepts as values 'variables', 'presets', 'styles', 'base-layout-styles'.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -179,9 +180,9 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $tree = WP_Theme_JSON_Resolver::get_merged_data();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $tree                = WP_Theme_JSON_Resolver::resolve_theme_file_uris( WP_Theme_JSON_Resolver::get_merged_data() );
+       $supports_theme_json = wp_theme_has_theme_json();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $supports_theme_json = wp_theme_has_theme_json();
</del><span class="cx" style="display: block; padding: 0 10px">         if ( empty( $types ) && ! $supports_theme_json ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $types = array( 'variables', 'presets', 'base-layout-styles' );
</span><span class="cx" style="display: block; padding: 0 10px">        } elseif ( empty( $types ) ) {
</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       2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php 2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -289,6 +289,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Prepare a global styles config output for response.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added custom relative theme file URIs to `_links`.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Post         $post    Global Styles post object.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_REST_Request $request Request object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -298,8 +299,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $raw_config                       = json_decode( $post->post_content, true );
</span><span class="cx" style="display: block; padding: 0 10px">                $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
</span><span class="cx" style="display: block; padding: 0 10px">                $config                           = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $theme_json                       = null;
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( $is_global_styles_user_theme_json ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $theme_json = new WP_Theme_JSON( $raw_config, 'custom' );
+                       $config     = $theme_json->get_raw_data();
</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">                // Base fields for every post.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -341,6 +344,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $links = $this->prepare_links( $post->ID );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // Only return resolved URIs for get requests to user theme JSON.
+                       if ( $theme_json ) {
+                               $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
+                               if ( ! empty( $resolved_theme_uris ) ) {
+                                       $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $response->add_links( $links );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $links['self']['href'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $actions = $this->get_available_actions( $post, $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -515,6 +527,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Returns the given theme global styles config.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added custom relative theme file URIs to `_links`.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_REST_Request $request The request instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @return WP_REST_Response|WP_Error
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -549,11 +562,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $response = rest_ensure_response( $data );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $links = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $links               = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'self' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
</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">+                        $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme );
+                       if ( ! empty( $resolved_theme_uris ) ) {
+                               $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         $response->add_links( $links );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -591,6 +608,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.0.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.2.0 Returns parent theme variations, if they exist.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added custom relative theme file URIs to `_links` for each item.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_REST_Request $request The request instance.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -606,9 +624,24 @@
</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">+                $response   = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                 $variations = WP_Theme_JSON_Resolver::get_style_variations();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return rest_ensure_response( $variations );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $variations as $variation ) {
+                       $variation_theme_json = new WP_Theme_JSON( $variation );
+                       $resolved_theme_uris  = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $variation_theme_json );
+                       $data                 = rest_ensure_response( $variation );
+                       if ( ! empty( $resolved_theme_uris ) ) {
+                               $data->add_links(
+                                       array(
+                                               'https://api.w.org/theme-file' => $resolved_theme_uris,
+                                       )
+                               );
+                       }
+                       $response[] = $this->prepare_response_for_collection( $data );
+               }
+
+               return rest_ensure_response( $response );
</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="trunksrcwpincludesrestapiendpointsclasswprestglobalstylesrevisionscontrollerphp"></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-revisions-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-revisions-controller.php     2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php       2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -268,6 +268,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Prepares the revision for the REST response.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added resolved URI links to the response.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Post         $post    Post revision object.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_REST_Request $request Request object.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -281,11 +282,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $global_styles_config;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $fields = $this->get_fields_for_response( $request );
-               $data   = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $fields     = $this->get_fields_for_response( $request );
+               $data       = array();
+               $theme_json = null;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $theme_json           = new WP_Theme_JSON( $global_styles_config, 'custom' );
+                       $global_styles_config = $theme_json->get_raw_data();
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( rest_is_field_included( 'settings', $fields ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -322,11 +325,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $data['parent'] = (int) $parent->ID;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
-               $data    = $this->add_additional_fields_to_object( $data, $request );
-               $data    = $this->filter_response_by_context( $data, $context );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $context             = ! empty( $request['context'] ) ? $request['context'] : 'view';
+               $data                = $this->add_additional_fields_to_object( $data, $request );
+               $data                = $this->filter_response_by_context( $data, $context );
+               $response            = rest_ensure_response( $data );
+               $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                return rest_ensure_response( $data );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $resolved_theme_uris ) ) {
+                       $response->add_links(
+                               array(
+                                       'https://api.w.org/theme-file' => $resolved_theme_uris,
+                               )
+                       );
+               }
+
+               return $response;
</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="trunktestsphpunitdatathemedir1blockthemestylesvariationbjson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.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-b.json    2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/tests/phpunit/data/themedir1/block-theme/styles/variation-b.json      2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,5 +14,12 @@
</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">+        },
+       "styles": {
+               "background": {
+                       "backgroundImage": {
+                               "url": "file:./assets/sugarloaf-mountain.jpg"
+                       }
+               }
</ins><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="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      2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php        2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -34,8 +34,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function set_up() {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::set_up();
</span><span class="cx" style="display: block; padding: 0 10px">                switch_theme( 'tt1-blocks' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
</ins><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">+        public function tear_down() {
+               remove_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
+               parent::tear_down();
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Create fake data before our tests run.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -79,6 +85,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                self::delete_user( self::$subscriber_id );
</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">+        /*
+        * This filter callback normalizes the return value from `get_theme_file_uri`
+        * to guard against changes in test environments.
+        * The test suite otherwise returns full system dir path, e.g.,
+        * /var/www/tests/phpunit/includes/../data/themedir1/block-theme/assets/sugarloaf-mountain.jpg
+        */
+       public function filter_theme_file_uri( $file ) {
+               $file_name = substr( strrchr( $file, '/' ), 1 );
+               return 'https://example.org/wp-content/themes/example-theme/assets/' . $file_name;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers WP_REST_Global_Styles_Controller::register_routes
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 54596
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,6 +136,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Controller does not use get_context_param().
</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">+        /**
+        * Tests a GET request to the global styles variations endpoint.
+        *
+        * @covers WP_REST_Global_Styles_Controller::get_theme_items
+        * @ticket 61273
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_get_theme_items() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( self::$admin_id );
</span><span class="cx" style="display: block; padding: 0 10px">                switch_theme( 'block-theme' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -128,7 +151,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'version'  => 2,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'title'    => 'variation-a',
</del><span class="cx" style="display: block; padding: 0 10px">                                 'settings' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'blocks' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'core/paragraph' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -146,10 +168,10 @@
</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">+                                'title'    => 'variation-a',
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'version'  => 2,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'title'    => 'variation-b',
</del><span class="cx" style="display: block; padding: 0 10px">                                 'settings' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'blocks' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                                'core/post-title' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,6 +189,31 @@
</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">+                                'styles'   => array(
+                                       'background' => array(
+                                               'backgroundImage' => array(
+                                                       'url' => 'file:./assets/sugarloaf-mountain.jpg',
+                                               ),
+                                       ),
+                               ),
+                               'title'    => 'variation-b',
+                               '_links'   => array(
+                                       'curies'        => array(
+                                               array(
+                                                       'name'      => 'wp',
+                                                       'href'      => 'https://api.w.org/{rel}',
+                                                       'templated' => true,
+                                               ),
+                                       ),
+                                       'wp:theme-file' => array(
+                                               array(
+                                                       'href'   => 'https://example.org/wp-content/themes/example-theme/assets/sugarloaf-mountain.jpg',
+                                                       'name'   => 'file:./assets/sugarloaf-mountain.jpg',
+                                                       'target' => 'styles.background.backgroundImage.url',
+                                                       'type'   => 'image/jpeg',
+                                               ),
+                                       ),
+                               ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'version'  => 2,
</span></span></pre></div>
<a id="trunktestsphpunitteststhemewpThemeJsonResolverphp"></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/theme/wpThemeJsonResolver.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php   2024-05-30 21:17:58 UTC (rev 58261)
+++ trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php     2024-05-31 01:17:50 UTC (rev 58262)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,6 +103,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->queries = array();
</span><span class="cx" style="display: block; padding: 0 10px">                // Clear caches.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_clean_themes_cache();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -113,6 +114,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
</span><span class="cx" style="display: block; padding: 0 10px">                wp_clean_themes_cache();
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $GLOBALS['wp_themes'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                remove_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Reset data between tests.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_clean_theme_json_cache();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,6 +121,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                parent::tear_down();
</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">+        /*
+        * This filter callback normalizes the return value from `get_theme_file_uri`
+        * to guard against changes in test environments.
+        * The test suite otherwise returns full system dir path, e.g.,
+        * /var/www/tests/phpunit/includes/../data/themedir1/block-theme/assets/sugarloaf-mountain.jpg
+        */
+       public function filter_theme_file_uri( $file ) {
+               $file_name = substr( strrchr( $file, '/' ), 1 );
+               return 'https://example.org/wp-content/themes/example-theme/assets/' . $file_name;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function filter_set_theme_root() {
</span><span class="cx" style="display: block; padding: 0 10px">                return $this->theme_root;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1176,4 +1189,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $default_presets_for_block = $theme_json->get_settings()['shadow']['defaultPresets'];
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( $default_presets_for_block );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Tests that relative paths are resolved and merged into the theme.json data.
+        *
+        * @covers WP_Theme_JSON_Resolver::resolve_theme_file_uris
+        * @ticket 61273
+        */
+       public function test_resolve_theme_file_uris() {
+               $theme_json = new WP_Theme_JSON(
+                       array(
+                               'version' => WP_Theme_JSON::LATEST_SCHEMA,
+                               'styles'  => array(
+                                       'background' => array(
+                                               'backgroundImage' => array(
+                                                       'url' => 'file:./assets/image.png',
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+
+               $expected_data = array(
+                       'version' => WP_Theme_JSON::LATEST_SCHEMA,
+                       'styles'  => array(
+                               'background' => array(
+                                       'backgroundImage' => array(
+                                               'url' => 'https://example.org/wp-content/themes/example-theme/assets/image.png',
+                                       ),
+                               ),
+                       ),
+               );
+
+               $actual = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $theme_json );
+
+               $this->assertSame( $expected_data, $actual->get_raw_data() );
+       }
+
+       /**
+        * Tests that them uris are resolved and bundled with other metadata in an array.
+        *
+        * @covers WP_Theme_JSON_Resolver::get_resolved_theme_uris
+        * @ticket 61273
+        */
+       public function test_get_resolved_theme_uris() {
+               $theme_json = new WP_Theme_JSON(
+                       array(
+                               'version' => WP_Theme_JSON::LATEST_SCHEMA,
+                               'styles'  => array(
+                                       'background' => array(
+                                               'backgroundImage' => array(
+                                                       'url' => 'file:./assets/image.png',
+                                               ),
+                                       ),
+                               ),
+                       )
+               );
+
+               $expected_data = array(
+                       array(
+                               'name'   => 'file:./assets/image.png',
+                               'href'   => 'https://example.org/wp-content/themes/example-theme/assets/image.png',
+                               'target' => 'styles.background.backgroundImage.url',
+                               'type'   => 'image/png',
+                       ),
+               );
+
+               $actual = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
+
+               $this->assertSame( $expected_data, $actual );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>