Make WordPress Core


Ignore:
Timestamp:
04/05/2022 09:50:13 AM (3 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.