/** * Prepare a post status object for serialization * * @param stdClass $status Post status data * @param WP_REST_Request $request * @return WP_REST_Response Post status data */ public function prepare_item_for_response($status, $request) { if (false === $status->public && !is_user_logged_in() || true === $status->internal && is_user_logged_in()) { return new WP_Error('rest_cannot_read_status', __('Cannot view status.'), array('status' => rest_authorization_required_code())); } $data = array('name' => $status->label, 'private' => (bool) $status->private, 'protected' => (bool) $status->protected, 'public' => (bool) $status->public, 'queryable' => (bool) $status->publicly_queryable, 'show_in_list' => (bool) $status->show_in_admin_all_list, 'slug' => $status->name); $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); $posts_controller = new WP_REST_Posts_Controller('post'); if ('publish' === $status->name) { $response->add_link('archives', rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post'))); } else { $response->add_link('archives', add_query_arg('status', $status->name, rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post')))); } /** * Filter a status returned from the API. * * Allows modification of the status data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $status The original status object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters('rest_prepare_status', $response, $status, $request); }
/** * Prepare a post status object for serialization * * @param stdClass $status Post status data * @param WP_REST_Request $request * @return WP_REST_Response Post status data */ public function prepare_item_for_response($status, $request) { if (false === $status->public && !is_user_logged_in() || true === $status->internal && is_user_logged_in()) { return new WP_Error('rest_cannot_read_status', __('Cannot view status.'), array('status' => 403)); } $data = array('name' => $status->label, 'private' => (bool) $status->private, 'protected' => (bool) $status->protected, 'public' => (bool) $status->public, 'queryable' => (bool) $status->publicly_queryable, 'show_in_list' => (bool) $status->show_in_admin_all_list, 'slug' => $status->name); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->filter_response_by_context($data, $context); $data = $this->add_additional_fields_to_object($data, $request); $data = rest_ensure_response($data); $posts_controller = new WP_REST_Posts_Controller('post'); if ('publish' === $status->name) { $data->add_link('archives', rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post'))); } else { $data->add_link('archives', add_query_arg('status', $status->name, rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post')))); } return $data; }
/** * Prepare links for the request. * * @param object $comment Comment object. * @return array Links for the given comment. */ protected function prepare_links($comment) { $links = array('self' => array('href' => rest_url('/wp/v2/comments/' . $comment->comment_ID)), 'collection' => array('href' => rest_url('/wp/v2/comments'))); if (0 !== (int) $comment->user_id) { $links['author'] = array('href' => rest_url('/wp/v2/users/' . $comment->user_id), 'embeddable' => true); } if (0 !== (int) $comment->comment_post_ID) { $post = get_post($comment->comment_post_ID); if (!empty($post->ID)) { $posts_controller = new WP_REST_Posts_Controller($post->post_type); $base = $posts_controller->get_post_type_base($post->post_type); $links['up'] = array('href' => rest_url('/wp/v2/' . $base . '/' . $comment->comment_post_ID), 'embeddable' => true, 'post_type' => $post->post_type); } } if (0 !== (int) $comment->comment_parent) { $links['in-reply-to'] = array('href' => rest_url(sprintf('/wp/v2/comments/%d', (int) $comment->comment_parent)), 'embeddable' => true); } return $links; }
protected function check_get_revision_response($response, $revision) { if ($response instanceof WP_REST_Response) { $links = $response->get_links(); $response = $response->get_data(); } else { $this->assertArrayHasKey('_links', $response); $links = $response['_links']; } $this->assertEquals($revision->post_author, $response['author']); $this->assertEquals($revision->post_content, $response['content']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_date), $response['date']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_date_gmt), $response['date_gmt']); $this->assertEquals($revision->post_excerpt, $response['excerpt']); $this->assertEquals($revision->guid, $response['guid']); $this->assertEquals($revision->ID, $response['id']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_modified), $response['modified']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_modified_gmt), $response['modified_gmt']); $this->assertEquals($revision->post_name, $response['slug']); $this->assertEquals($revision->post_title, $response['title']); $parent = get_post($revision->post_parent); $parent_controller = new WP_REST_Posts_Controller($parent->post_type); $parent_base = $parent_controller->get_post_type_base($parent->post_type); $this->assertEquals(rest_url('wp/' . $parent_base . '/' . $revision->post_parent), $links['parent'][0]['href']); }
public function __construct($parent_post_type) { $this->parent_post_type = $parent_post_type; $this->parent_controller = new WP_REST_Posts_Controller($this->parent_post_type); $this->parent_base = $this->parent_controller->get_post_type_base($this->parent_post_type); }