Make WordPress Core


Ignore:
Timestamp:
05/31/2024 01:17:50 AM (2 years ago)
Author:
noisysocks
Message:

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 #61273.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

    r58225 r58262  
    269269     *
    270270     * @since 6.3.0
     271     * @since 6.6.0 Added resolved URI links to the response.
    271272     *
    272273     * @param WP_Post         $post    Post revision object.
     
    282283        }
    283284
    284         $fields = $this->get_fields_for_response( $request );
    285         $data   = array();
     285        $fields     = $this->get_fields_for_response( $request );
     286        $data       = array();
     287        $theme_json = null;
    286288
    287289        if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) {
    288             $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data();
     290            $theme_json           = new WP_Theme_JSON( $global_styles_config, 'custom' );
     291            $global_styles_config = $theme_json->get_raw_data();
    289292            if ( rest_is_field_included( 'settings', $fields ) ) {
    290293                $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass();
     
    323326        }
    324327
    325         $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    326         $data    = $this->add_additional_fields_to_object( $data, $request );
    327         $data    = $this->filter_response_by_context( $data, $context );
    328 
    329         return rest_ensure_response( $data );
     328        $context             = ! empty( $request['context'] ) ? $request['context'] : 'view';
     329        $data                = $this->add_additional_fields_to_object( $data, $request );
     330        $data                = $this->filter_response_by_context( $data, $context );
     331        $response            = rest_ensure_response( $data );
     332        $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
     333
     334        if ( ! empty( $resolved_theme_uris ) ) {
     335            $response->add_links(
     336                array(
     337                    'https://api.w.org/theme-file' => $resolved_theme_uris,
     338                )
     339            );
     340        }
     341
     342        return $response;
    330343    }
    331344
Note: See TracChangeset for help on using the changeset viewer.