Make WordPress Core

Changeset 53072


Ignore:
Timestamp:
04/05/2022 09:50:13 AM (5 years ago)
Author:
youknowriad
Message:

Block Editor: Backport the Global Styles Variations endpoint.

This include the /global-styles/themes/{theme}/variations rest endpoint into core.
The endpoint will be used by the site editor to display alternative theme styles to the user.

Props gziolo, oandregal.
See #55505.

Location:
trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/package-lock.json

    r53069 r53072  
    77517751                "browserify-aes": {
    77527752                        "version": "1.2.0",
    7753                         "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
     7753                        "resolved": "http://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz",
    77547754                        "integrity": "sha512-+7CHXqGuspUn/Sl5aO7Ea0xWGAtETPXNSAjHo48JfLdPWcMng33Xe4znFvQweqc/uzk5zSOI3H52CYnjCfb5hA==",
    77557755                        "dev": true,
  • trunk/src/wp-includes/class-wp-theme-json-resolver.php

    r52744 r53072  
    450450        }
    451451
     452        /**
     453         * Returns the style variations defined by the theme.
     454         *
     455         * @since 6.0.0
     456         *
     457         * @return array
     458         */
     459        public static function get_style_variations() {
     460                $variations     = array();
     461                $base_directory = get_stylesheet_directory() . '/styles';
     462                if ( is_dir( $base_directory ) ) {
     463                        $nested_files      = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
     464                        $nested_html_files = iterator_to_array( new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ) );
     465                        ksort( $nested_html_files );
     466                        foreach ( $nested_html_files as $path => $file ) {
     467                                $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) );
     468                                if ( is_array( $decoded_file ) ) {
     469                                        $translated = static::translate( $decoded_file, wp_get_theme()->get( 'TextDomain' ) );
     470                                        $variation  = ( new WP_Theme_JSON( $translated ) )->get_raw_data();
     471                                        if ( empty( $variation['title'] ) ) {
     472                                                $variation['title'] = basename( $path, '.json' );
     473                                        }
     474                                        $variations[] = $variation;
     475                                }
     476                        }
     477                }
     478                return $variations;
     479        }
    452480}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r52750 r53072  
    3939         */
    4040        public function register_routes() {
     41                register_rest_route(
     42                        $this->namespace,
     43                        '/' . $this->rest_base . '/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
     44                        array(
     45                                array(
     46                                        'methods'             => WP_REST_Server::READABLE,
     47                                        'callback'            => array( $this, 'get_theme_items' ),
     48                                        'permission_callback' => array( $this, 'get_theme_items_permissions_check' ),
     49                                        'args'                => array(
     50                                                'stylesheet' => array(
     51                                                        'description' => __( 'The theme identifier' ),
     52                                                        'type'        => 'string',
     53                                                ),
     54                                        ),
     55                                ),
     56                        )
     57                );
     58
    4159                // List themes global styles.
    4260                register_rest_route(
    … …  
    586604                return $response;
    587605        }
     606
     607        /**
     608         * Checks if a given request has access to read a single theme global styles config.
     609         *
     610         * @since 6.0.0
     611         *
     612         * @param WP_REST_Request $request Full details about the request.
     613         * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
     614         */
     615        public function get_theme_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
     616                // Verify if the current user has edit_theme_options capability.
     617                // This capability is required to edit/view/delete templates.
     618                if ( ! current_user_can( 'edit_theme_options' ) ) {
     619                        return new WP_Error(
     620                                'rest_cannot_manage_global_styles',
     621                                __( 'Sorry, you are not allowed to access the global styles on this site.' ),
     622                                array(
     623                                        'status' => rest_authorization_required_code(),
     624                                )
     625                        );
     626                }
     627
     628                return true;
     629        }
     630
     631        /**
     632         * Returns the given theme global styles variations.
     633         *
     634         * @since 6.0.0
     635         *
     636         * @param WP_REST_Request $request The request instance.
     637         *
     638         * @return WP_REST_Response|WP_Error
     639         */
     640        public function get_theme_items( $request ) {
     641                if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) {
     642                        // This endpoint only supports the active theme for now.
     643                        return new WP_Error(
     644                                'rest_theme_not_found',
     645                                __( 'Theme not found.' ),
     646                                array( 'status' => 404 )
     647                        );
     648                }
     649
     650                $variations = WP_Theme_JSON_Resolver::get_style_variations();
     651                $response   = rest_ensure_response( $variations );
     652
     653                return $response;
     654        }
    588655}
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r52399 r53072  
    117117                        $routes['/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)'],
    118118                        'Theme global styles route does not have exactly one element'
     119                );
     120                $this->assertArrayHasKey(
     121                        '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
     122                        $routes,
     123                        'Theme global styles variations route does not exist'
    119124                );
    120125        }
    … …  
    456461                $this->assertArrayHasKey( 'title', $properties, 'Schema properties array does not have "title" key' );
    457462        }
     463
     464
     465        public function test_get_theme_items() {
     466                wp_set_current_user( self::$admin_id );
     467                switch_theme( 'block-theme' );
     468                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/block-theme/variations' );
     469                $response = rest_get_server()->dispatch( $request );
     470                $data     = $response->get_data();
     471                $expected = array(
     472                        array(
     473                                'version'  => 2,
     474                                'settings' => array(
     475                                        'color' => array(
     476                                                'palette' => array(
     477                                                        'theme' => array(
     478                                                                array(
     479                                                                        'slug'  => 'foreground',
     480                                                                        'color' => '#3F67C6',
     481                                                                        'name'  => 'Foreground',
     482                                                                ),
     483                                                        ),
     484                                                ),
     485                                        ),
     486                                ),
     487                                'styles'   => array(
     488                                        'blocks' => array(
     489                                                'core/post-title' => array(
     490                                                        'typography' => array(
     491                                                                'fontWeight' => '700',
     492                                                        ),
     493                                                ),
     494                                        ),
     495                                ),
     496                                'title'    => 'variation',
     497                        ),
     498                );
     499                $this->assertSameSetsWithIndex( $data, $expected );
     500        }
    458501}
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r52399 r53072  
    137137                        '/wp/v2/comments/(?P<id>[\\d]+)',
    138138                        '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
     139                        '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
    139140                        '/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
    140141                        '/wp/v2/search',
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r52536 r53072  
    94259425            ]
    94269426        },
     9427        "/wp/v2/global-styles/themes/(?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+)/variations": {
     9428            "namespace": "wp/v2",
     9429            "methods": [
     9430                "GET"
     9431            ],
     9432            "endpoints": [
     9433                {
     9434                    "methods": [
     9435                        "GET"
     9436                    ],
     9437                    "args": {
     9438                        "stylesheet": {
     9439                            "description": "The theme identifier",
     9440                            "type": "string",
     9441                            "required": false
     9442                        }
     9443                    }
     9444                }
     9445            ]
     9446        },
    94279447        "/wp/v2/global-styles/themes/(?P<stylesheet>[^\\/:<>\\*\\?\"\\|]+(?:\\/[^\\/:<>\\*\\?\"\\|]+)?)": {
    94289448            "namespace": "wp/v2",
Note: See TracChangeset for help on using the changeset viewer.