From f9bb6529ea1aeb6b6a8bcc33e7d270458f3ae3f7 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 27 Jun 2023 09:37:04 +1000 Subject: [PATCH] Only run and set styles and/or settings through WP_Theme_JSON if one or both of them are set. --- ...est-global-styles-revisions-controller.php | 53 +++++++------------ 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php index 43e7c28f8360c..98ee37ff657ef 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php @@ -70,36 +70,23 @@ public function register_routes() { /** * Retrieves the query params for collections. - * Taken mostly from WP_REST_Controller->get_collection_params(). + * Inherits from WP_REST_Controller::get_collection_params(), + * also reflects changes to return value WP_REST_Revisions_Controller::get_collection_params(). * * @since 6.3.0 * * @return array Collection parameters. */ public function get_collection_params() { - return array( - 'context' => array( 'default' => 'view' ), - 'page' => array( - 'description' => __( 'Current page of the collection.' ), - 'type' => 'integer', - 'default' => 1, - 'sanitize_callback' => 'absint', - 'validate_callback' => 'rest_validate_request_arg', - 'minimum' => 1, - ), - 'per_page' => array( - 'description' => __( 'Maximum number of items to be returned in result set.' ), - 'type' => 'integer', - 'minimum' => 1, - 'maximum' => 100, - 'sanitize_callback' => 'absint', - 'validate_callback' => 'rest_validate_request_arg', - ), - 'offset' => array( - 'description' => __( 'Offset the result set by a specific number of items.' ), - 'type' => 'integer', - ), + $collection_params = parent::get_collection_params(); + $collection_params['offset'] = array( + 'description' => __( 'Offset the result set by a specific number of items.' ), + 'type' => 'integer', ); + unset( $collection_params['search'] ); + unset( $collection_params['per_page']['default'] ); + + return $collection_params; } /** @@ -290,13 +277,19 @@ public function prepare_item_for_response( $post, $request ) { return $global_styles_config; } + $fields = $this->get_fields_for_response( $request ); + $data = array(); + if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) { $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data(); + if ( rest_is_field_included( 'settings', $fields ) ) { + $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass(); + } + if ( rest_is_field_included( 'styles', $fields ) ) { + $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass(); + } } - $fields = $this->get_fields_for_response( $request ); - $data = array(); - if ( rest_is_field_included( 'author', $fields ) ) { $data['author'] = (int) $post->post_author; } @@ -325,14 +318,6 @@ public function prepare_item_for_response( $post, $request ) { $data['parent'] = (int) $parent->ID; } - if ( rest_is_field_included( 'settings', $fields ) ) { - $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass(); - } - - if ( rest_is_field_included( 'styles', $fields ) ) { - $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass(); - } - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object( $data, $request ); $data = $this->filter_response_by_context( $data, $context );