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-controller.php

    r58225 r58262  
    290290         *
    291291         * @since 5.9.0
     292         * @since 6.6.0 Added custom relative theme file URIs to `_links`.
    292293         *
    293294         * @param WP_Post         $post    Global Styles post object.
    … …  
    299300                $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
    300301                $config                           = array();
     302                $theme_json                       = null;
    301303                if ( $is_global_styles_user_theme_json ) {
    302                         $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data();
     304                        $theme_json = new WP_Theme_JSON( $raw_config, 'custom' );
     305                        $config     = $theme_json->get_raw_data();
    303306                }
    304307
    … …  
    342345                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
    343346                        $links = $this->prepare_links( $post->ID );
     347
     348                        // Only return resolved URIs for get requests to user theme JSON.
     349                        if ( $theme_json ) {
     350                                $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
     351                                if ( ! empty( $resolved_theme_uris ) ) {
     352                                        $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
     353                                }
     354                        }
     355
    344356                        $response->add_links( $links );
    345357                        if ( ! empty( $links['self']['href'] ) ) {
    … …  
    516528         *
    517529         * @since 5.9.0
     530         * @since 6.6.0 Added custom relative theme file URIs to `_links`.
    518531         *
    519532         * @param WP_REST_Request $request The request instance.
    … …  
    550563
    551564                if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
    552                         $links = array(
     565                        $links               = array(
    553566                                'self' => array(
    554567                                        'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
    555568                                ),
    556569                        );
     570                        $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme );
     571                        if ( ! empty( $resolved_theme_uris ) ) {
     572                                $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
     573                        }
    557574                        $response->add_links( $links );
    558575                }
    … …  
    592609         * @since 6.0.0
    593610         * @since 6.2.0 Returns parent theme variations, if they exist.
     611         * @since 6.6.0 Added custom relative theme file URIs to `_links` for each item.
    594612         *
    595613         * @param WP_REST_Request $request The request instance.
    … …  
    607625                }
    608626
     627                $response   = array();
    609628                $variations = WP_Theme_JSON_Resolver::get_style_variations();
    610629
    611                 return rest_ensure_response( $variations );
     630                foreach ( $variations as $variation ) {
     631                        $variation_theme_json = new WP_Theme_JSON( $variation );
     632                        $resolved_theme_uris  = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $variation_theme_json );
     633                        $data                 = rest_ensure_response( $variation );
     634                        if ( ! empty( $resolved_theme_uris ) ) {
     635                                $data->add_links(
     636                                        array(
     637                                                'https://api.w.org/theme-file' => $resolved_theme_uris,
     638                                        )
     639                                );
     640                        }
     641                        $response[] = $this->prepare_response_for_collection( $data );
     642                }
     643
     644                return rest_ensure_response( $response );
    612645        }
    613646
Note: See TracChangeset for help on using the changeset viewer.