Skip to content

Commit

Permalink
Respect fields param for global styles REST API requests. (#36206)
Browse files Browse the repository at this point in the history
* Respect fields param for REST API requests.

* Fix lint
  • Loading branch information
spacedmonkey authored Nov 8, 2021
1 parent 23647e7 commit 0a0e0ca
Showing 1 changed file with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,46 @@ protected function prepare_item_for_database( $request ) {
public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$config = json_decode( $post->post_content, true );
$is_global_styles_user_theme_json = isset( $config['isGlobalStylesUserThemeJSON'] ) && true === $config['isGlobalStylesUserThemeJSON'];
$result = array(
'id' => $post->ID,
'settings' => ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(),
'styles' => ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(),
'title' => array(
'raw' => $post->post_title,
'rendered' => get_the_title( $post ),
),
);
$result = $this->add_additional_fields_to_object( $result, $request );
$response = rest_ensure_response( $result );
$links = $this->prepare_links( $post->id );

$fields = $this->get_fields_for_response( $request );

// Base fields for every post.
$data = array();

if ( rest_is_field_included( 'id', $fields ) ) {
$data['id'] = $post->ID;
}

if ( rest_is_field_included( 'title', $fields ) ) {
$data['title'] = array();
}
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $post->post_title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );

$data['title']['rendered'] = get_the_title( $post->ID );

remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
}

if ( rest_is_field_included( 'settings', $fields ) ) {
$data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass();
}

if ( rest_is_field_included( 'styles', $fields ) ) {
$data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $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 );

// Wrap the data in a response object.
$response = rest_ensure_response( $data );

$links = $this->prepare_links( $post->ID );
$response->add_links( $links );
if ( ! empty( $links['self']['href'] ) ) {
$actions = $this->get_available_actions();
Expand Down Expand Up @@ -280,6 +308,19 @@ protected function get_available_actions() {
return $rels;
}

/**
* Overwrites the default protected title format.
*
* By default, WordPress will show password protected posts with a title of
* "Protected: %s", as the REST API communicates the protected status of a post
* in a machine readable format, we remove the "Protected: " prefix.
*
* @return string Protected title format.
*/
public function protected_title_format() {
return '%s';
}

/**
* Retrieves the query params for the global styles collection.
*
Expand Down

0 comments on commit 0a0e0ca

Please sign in to comment.