WP_REST_Revisions_Controller::prepare_item_for_response PHP Method

prepare_item_for_response() public method

Prepares the revision for the REST response.
Since: 4.7.0
public prepare_item_for_response ( WP_Post $post, WP_REST_Request $request ) : WP_REST_Response
$post WP_Post Post revision object.
$request WP_REST_Request Request object.
return WP_REST_Response Response object.
    public function prepare_item_for_response($post, $request)
    {
        $schema = $this->get_item_schema();
        $data = array();
        if (!empty($schema['properties']['author'])) {
            $data['author'] = $post->post_author;
        }
        if (!empty($schema['properties']['date'])) {
            $data['date'] = $this->prepare_date_response($post->post_date_gmt, $post->post_date);
        }
        if (!empty($schema['properties']['date_gmt'])) {
            $data['date_gmt'] = $this->prepare_date_response($post->post_date_gmt);
        }
        if (!empty($schema['properties']['id'])) {
            $data['id'] = $post->ID;
        }
        if (!empty($schema['properties']['modified'])) {
            $data['modified'] = $this->prepare_date_response($post->post_modified_gmt, $post->post_modified);
        }
        if (!empty($schema['properties']['modified_gmt'])) {
            $data['modified_gmt'] = $this->prepare_date_response($post->post_modified_gmt);
        }
        if (!empty($schema['properties']['parent'])) {
            $data['parent'] = (int) $post->post_parent;
        }
        if (!empty($schema['properties']['slug'])) {
            $data['slug'] = $post->post_name;
        }
        if (!empty($schema['properties']['guid'])) {
            $data['guid'] = array('rendered' => apply_filters('get_the_guid', $post->guid), 'raw' => $post->guid);
        }
        if (!empty($schema['properties']['title'])) {
            $data['title'] = array('raw' => $post->post_title, 'rendered' => get_the_title($post->ID));
        }
        if (!empty($schema['properties']['content'])) {
            $data['content'] = array('raw' => $post->post_content, 'rendered' => apply_filters('the_content', $post->post_content));
        }
        if (!empty($schema['properties']['excerpt'])) {
            $data['excerpt'] = array('raw' => $post->post_excerpt, 'rendered' => $this->prepare_excerpt_response($post->post_excerpt, $post));
        }
        $context = !empty($request['context']) ? $request['context'] : 'view';
        $data = $this->add_additional_fields_to_object($data, $request);
        $data = $this->filter_response_by_context($data, $context);
        $response = rest_ensure_response($data);
        if (!empty($data['parent'])) {
            $response->add_link('parent', rest_url(sprintf('%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'])));
        }
        /**
         * Filters a revision returned from the API.
         *
         * Allows modification of the revision right before it is returned.
         *
         * @since 4.7.0
         *
         * @param WP_REST_Response $response The response object.
         * @param WP_Post          $post     The original revision object.
         * @param WP_REST_Request  $request  Request used to generate the response.
         */
        return apply_filters('rest_prepare_revision', $response, $post, $request);
    }