Make WordPress Core


Ignore:
Timestamp:
05/31/2024 01:17:50 AM (9 months 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/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r57662 r58262  
    3535        parent::set_up();
    3636        switch_theme( 'tt1-blocks' );
     37        add_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
     38    }
     39
     40    public function tear_down() {
     41        remove_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
     42        parent::tear_down();
    3743    }
    3844
     
    8086    }
    8187
     88    /*
     89     * This filter callback normalizes the return value from `get_theme_file_uri`
     90     * to guard against changes in test environments.
     91     * The test suite otherwise returns full system dir path, e.g.,
     92     * /var/www/tests/phpunit/includes/../data/themedir1/block-theme/assets/sugarloaf-mountain.jpg
     93     */
     94    public function filter_theme_file_uri( $file ) {
     95        $file_name = substr( strrchr( $file, '/' ), 1 );
     96        return 'https://example.org/wp-content/themes/example-theme/assets/' . $file_name;
     97    }
     98
    8299    /**
    83100     * @covers WP_REST_Global_Styles_Controller::register_routes
     
    120137    }
    121138
     139    /**
     140     * Tests a GET request to the global styles variations endpoint.
     141     *
     142     * @covers WP_REST_Global_Styles_Controller::get_theme_items
     143     * @ticket 61273
     144     */
    122145    public function test_get_theme_items() {
    123146        wp_set_current_user( self::$admin_id );
     
    129152            array(
    130153                'version'  => 2,
    131                 'title'    => 'variation-a',
    132154                'settings' => array(
    133155                    'blocks' => array(
     
    147169                    ),
    148170                ),
     171                'title'    => 'variation-a',
    149172            ),
    150173            array(
    151174                'version'  => 2,
    152                 'title'    => 'variation-b',
    153175                'settings' => array(
    154176                    'blocks' => array(
     
    165187                                ),
    166188                            ),
     189                        ),
     190                    ),
     191                ),
     192                'styles'   => array(
     193                    'background' => array(
     194                        'backgroundImage' => array(
     195                            'url' => 'file:./assets/sugarloaf-mountain.jpg',
     196                        ),
     197                    ),
     198                ),
     199                'title'    => 'variation-b',
     200                '_links'   => array(
     201                    'curies'        => array(
     202                        array(
     203                            'name'      => 'wp',
     204                            'href'      => 'https://api.w.org/{rel}',
     205                            'templated' => true,
     206                        ),
     207                    ),
     208                    'wp:theme-file' => array(
     209                        array(
     210                            'href'   => 'https://example.org/wp-content/themes/example-theme/assets/sugarloaf-mountain.jpg',
     211                            'name'   => 'file:./assets/sugarloaf-mountain.jpg',
     212                            'target' => 'styles.background.backgroundImage.url',
     213                            'type'   => 'image/jpeg',
    167214                        ),
    168215                    ),
Note: See TracChangeset for help on using the changeset viewer.