Make WordPress Core


Ignore:
Timestamp:
05/28/2024 12:38:28 PM (14 months ago)
Author:
swissspidy
Message:

REST API: Refactor global styles endpoints in REST API to register with post type.

Updated the global styles endpoints in the REST API to extend from existing posts and revisions controllers. This reduces duplicated code and inconsistencies. The revisions controller is now a subclass of the WP_REST_Revisions_Controller. Related redundant methods were removed and schema generation and collection parameters were adjusted to suit the global styles context. Updated permission checks, constructor, and collection parameters accordingly. This change allows for easy override of these classes using the register_post_type_args filter.

This reintroduces [57624] (reverted in [57628]) with improved backward compatibility and further enhancements.

Props ramonopoly, spacedmonkey, mukesh27, swissspidy.
Fixes #60131.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r57628 r58225  
    1111 * Base Global Styles REST API Controller.
    1212 */
    13 class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
    14 
    15     /**
    16      * Post type.
    17      *
    18      * @since 5.9.0
    19      * @var string
    20      */
    21     protected $post_type;
     13class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
     14    /**
     15     * Whether the controller supports batching.
     16     *
     17     * @since 6.6.0
     18     * @var array
     19     */
     20    protected $allow_batch = array( 'v1' => false );
    2221
    2322    /**
    2423     * Constructor.
    25      * @since 5.9.0
    26      */
    27     public function __construct() {
    28         $this->namespace = 'wp/v2';
    29         $this->rest_base = 'global-styles';
    30         $this->post_type = 'wp_global_styles';
     24     *
     25     * @since 6.6.0
     26     *
     27     * @param string $post_type Post type.
     28     */
     29    public function __construct( $post_type = 'wp_global_styles' ) {
     30        parent::__construct( $post_type );
    3131    }
    3232
     
    5151                        ),
    5252                    ),
     53                    'allow_batch'         => $this->allow_batch,
    5354                ),
    5455            )
     
    8081                        ),
    8182                    ),
     83                    'allow_batch'         => $this->allow_batch,
    8284                ),
    8385            )
     
    107109                    'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
    108110                ),
    109                 'schema' => array( $this, 'get_public_item_schema' ),
     111                'schema'      => array( $this, 'get_public_item_schema' ),
     112                'allow_batch' => $this->allow_batch,
    110113            )
    111114        );
     
    195198     * @return bool Whether the post can be read.
    196199     */
    197     protected function check_read_permission( $post ) {
     200    public function check_read_permission( $post ) {
    198201        return current_user_can( 'read_post', $post->ID );
    199     }
    200 
    201     /**
    202      * Returns the given global styles config.
    203      *
    204      * @since 5.9.0
    205      *
    206      * @param WP_REST_Request $request The request instance.
    207      *
    208      * @return WP_REST_Response|WP_Error
    209      */
    210     public function get_item( $request ) {
    211         $post = $this->get_post( $request['id'] );
    212         if ( is_wp_error( $post ) ) {
    213             return $post;
    214         }
    215 
    216         return $this->prepare_item_for_response( $post, $request );
    217202    }
    218203
     
    240225
    241226        return true;
    242     }
    243 
    244     /**
    245      * Checks if a global style can be edited.
    246      *
    247      * @since 5.9.0
    248      *
    249      * @param WP_Post $post Post object.
    250      * @return bool Whether the post can be edited.
    251      */
    252     protected function check_update_permission( $post ) {
    253         return current_user_can( 'edit_post', $post->ID );
    254     }
    255 
    256     /**
    257      * Updates a single global style config.
    258      *
    259      * @since 5.9.0
    260      *
    261      * @param WP_REST_Request $request Full details about the request.
    262      * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
    263      */
    264     public function update_item( $request ) {
    265         $post_before = $this->get_post( $request['id'] );
    266         if ( is_wp_error( $post_before ) ) {
    267             return $post_before;
    268         }
    269 
    270         $changes = $this->prepare_item_for_database( $request );
    271         if ( is_wp_error( $changes ) ) {
    272             return $changes;
    273         }
    274 
    275         $result = wp_update_post( wp_slash( (array) $changes ), true, false );
    276         if ( is_wp_error( $result ) ) {
    277             return $result;
    278         }
    279 
    280         $post          = get_post( $request['id'] );
    281         $fields_update = $this->update_additional_fields_for_object( $post, $request );
    282         if ( is_wp_error( $fields_update ) ) {
    283             return $fields_update;
    284         }
    285 
    286         wp_after_insert_post( $post, true, $post_before );
    287 
    288         $response = $this->prepare_item_for_response( $post, $request );
    289 
    290         return rest_ensure_response( $response );
    291227    }
    292228
     
    408344            $response->add_links( $links );
    409345            if ( ! empty( $links['self']['href'] ) ) {
    410                 $actions = $this->get_available_actions();
     346                $actions = $this->get_available_actions( $post, $request );
    411347                $self    = $links['self']['href'];
    412348                foreach ( $actions as $rel ) {
     
    432368
    433369        $links = array(
    434             'self' => array(
     370            'self'  => array(
    435371                'href' => rest_url( trailingslashit( $base ) . $id ),
     372            ),
     373            'about' => array(
     374                'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
    436375            ),
    437376        );
     
    455394     * @since 5.9.0
    456395     * @since 6.2.0 Added 'edit-css' action.
    457      *
     396     * @since 6.6.0 Added $post and $request parameters.
     397     *
     398     * @param WP_Post         $post    Post object.
     399     * @param WP_REST_Request $request Request object.
    458400     * @return array List of link relations.
    459401     */
    460     protected function get_available_actions() {
     402    protected function get_available_actions( $post, $request ) {
    461403        $rels = array();
    462404
    463         $post_type = get_post_type_object( $this->post_type );
     405        $post_type = get_post_type_object( $post->post_type );
    464406        if ( current_user_can( $post_type->cap->publish_posts ) ) {
    465407            $rels[] = 'https://api.w.org/action-publish';
     
    471413
    472414        return $rels;
    473     }
    474 
    475     /**
    476      * Overwrites the default protected title format.
    477      *
    478      * By default, WordPress will show password protected posts with a title of
    479      * "Protected: %s", as the REST API communicates the protected status of a post
    480      * in a machine readable format, we remove the "Protected: " prefix.
    481      *
    482      * @since 5.9.0
    483      *
    484      * @return string Protected title format.
    485      */
    486     public function protected_title_format() {
    487         return '%s';
    488415    }
    489416
Note: See TracChangeset for help on using the changeset viewer.