WP_REST_Global_Styles_Revisions_Controller::prepare_item_for_response( WP_Post $post, WP_REST_Request $request ): WP_REST_Response|WP_Error

In this article

Prepares the revision for the REST response.

Parameters

$postWP_Postrequired
Post revision object.
$requestWP_REST_Requestrequired
Request object.

Return

WP_REST_Response|WP_Error Response object.

Source

public function prepare_item_for_response( $post, $request ) {
	$parent               = $this->get_parent( $request['parent'] );
	$global_styles_config = $this->get_decoded_global_styles_json( $post->post_content );

	if ( is_wp_error( $global_styles_config ) ) {
		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();
		}
	}

	if ( rest_is_field_included( 'author', $fields ) ) {
		$data['author'] = (int) $post->post_author;
	}

	if ( rest_is_field_included( 'date', $fields ) ) {
		$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
	}

	if ( rest_is_field_included( 'date_gmt', $fields ) ) {
		$data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
	}

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

	if ( rest_is_field_included( 'modified', $fields ) ) {
		$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
	}

	if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
		$data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
	}

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

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	return rest_ensure_response( $data );
}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.