Skip to content

Commit

Permalink
Only run and set styles and/or settings through WP_Theme_JSON if one …
Browse files Browse the repository at this point in the history
…or both of them are set.
  • Loading branch information
ramonjd committed Jun 26, 2023
1 parent c5cc8c5 commit f9bb652
Showing 1 changed file with 19 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit f9bb652

Please sign in to comment.