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/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r57885 r58262  
    104104                add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
    105105                add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
     106                add_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
    106107                $this->queries = array();
    107108                // Clear caches.
    … …  
    114115                wp_clean_themes_cache();
    115116                unset( $GLOBALS['wp_themes'] );
     117                remove_filter( 'theme_file_uri', array( $this, 'filter_theme_file_uri' ) );
    116118
    117119                // Reset data between tests.
    118120                wp_clean_theme_json_cache();
    119121                parent::tear_down();
     122        }
     123
     124        /*
     125         * This filter callback normalizes the return value from `get_theme_file_uri`
     126         * to guard against changes in test environments.
     127         * The test suite otherwise returns full system dir path, e.g.,
     128         * /var/www/tests/phpunit/includes/../data/themedir1/block-theme/assets/sugarloaf-mountain.jpg
     129         */
     130        public function filter_theme_file_uri( $file ) {
     131                $file_name = substr( strrchr( $file, '/' ), 1 );
     132                return 'https://example.org/wp-content/themes/example-theme/assets/' . $file_name;
    120133        }
    121134
    … …  
    11771190                $this->assertTrue( $default_presets_for_block );
    11781191        }
     1192
     1193        /**
     1194         * Tests that relative paths are resolved and merged into the theme.json data.
     1195         *
     1196         * @covers WP_Theme_JSON_Resolver::resolve_theme_file_uris
     1197         * @ticket 61273
     1198         */
     1199        public function test_resolve_theme_file_uris() {
     1200                $theme_json = new WP_Theme_JSON(
     1201                        array(
     1202                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     1203                                'styles'  => array(
     1204                                        'background' => array(
     1205                                                'backgroundImage' => array(
     1206                                                        'url' => 'file:./assets/image.png',
     1207                                                ),
     1208                                        ),
     1209                                ),
     1210                        )
     1211                );
     1212
     1213                $expected_data = array(
     1214                        'version' => WP_Theme_JSON::LATEST_SCHEMA,
     1215                        'styles'  => array(
     1216                                'background' => array(
     1217                                        'backgroundImage' => array(
     1218                                                'url' => 'https://example.org/wp-content/themes/example-theme/assets/image.png',
     1219                                        ),
     1220                                ),
     1221                        ),
     1222                );
     1223
     1224                $actual = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $theme_json );
     1225
     1226                $this->assertSame( $expected_data, $actual->get_raw_data() );
     1227        }
     1228
     1229        /**
     1230         * Tests that them uris are resolved and bundled with other metadata in an array.
     1231         *
     1232         * @covers WP_Theme_JSON_Resolver::get_resolved_theme_uris
     1233         * @ticket 61273
     1234         */
     1235        public function test_get_resolved_theme_uris() {
     1236                $theme_json = new WP_Theme_JSON(
     1237                        array(
     1238                                'version' => WP_Theme_JSON::LATEST_SCHEMA,
     1239                                'styles'  => array(
     1240                                        'background' => array(
     1241                                                'backgroundImage' => array(
     1242                                                        'url' => 'file:./assets/image.png',
     1243                                                ),
     1244                                        ),
     1245                                ),
     1246                        )
     1247                );
     1248
     1249                $expected_data = array(
     1250                        array(
     1251                                'name'   => 'file:./assets/image.png',
     1252                                'href'   => 'https://example.org/wp-content/themes/example-theme/assets/image.png',
     1253                                'target' => 'styles.background.backgroundImage.url',
     1254                                'type'   => 'image/png',
     1255                        ),
     1256                );
     1257
     1258                $actual = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
     1259
     1260                $this->assertSame( $expected_data, $actual );
     1261        }
    11791262}
Note: See TracChangeset for help on using the changeset viewer.