Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2423 from websupporter/develop
Browse files Browse the repository at this point in the history
 Include Post data on the response object when declared  #2416
  • Loading branch information
danielbachhuber committed May 3, 2016
2 parents 25b4258 + d0c55b6 commit 6e04c96
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions lib/endpoints/class-wp-rest-posts-controller.php
Expand Up @@ -1071,26 +1071,58 @@ public function prepare_item_for_response( $post, $request ) {
$GLOBALS['post'] = $post;
setup_postdata( $post );

$schema = $this->get_item_schema();

// Base fields for every post.
$data = array(
'id' => $post->ID,
'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ),
'guid' => array(
$data = array();

if ( ! empty( $schema['properties']['id'] ) ) {
$data['id'] = $post->ID;
}

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']['guid'] ) ) {
$data['guid'] = array(
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'get_the_guid', $post->guid ),
'raw' => $post->guid,
),
'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
'password' => $post->post_password,
'slug' => $post->post_name,
'status' => $post->post_status,
'type' => $post->post_type,
'link' => get_permalink( $post->ID ),
);
);
}

$schema = $this->get_item_schema();
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']['password'] ) ) {
$data['password'] = $post->post_password;
}

if ( ! empty( $schema['properties']['slug'] ) ) {
$data['slug'] = $post->post_name;
}

if ( ! empty( $schema['properties']['status'] ) ) {
$data['status'] = $post->post_status;
}

if ( ! empty( $schema['properties']['type'] ) ) {
$data['type'] = $post->post_type;
}

if ( ! empty( $schema['properties']['link'] ) ) {
$data['link'] = get_permalink( $post->ID );
}

if ( ! empty( $schema['properties']['title'] ) ) {
$data['title'] = array(
Expand Down Expand Up @@ -1171,8 +1203,10 @@ public function prepare_item_for_response( $post, $request ) {
$taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$terms = get_the_terms( $post, $taxonomy->name );
$data[ $base ] = $terms ? wp_list_pluck( $terms, 'term_id' ) : array();
if ( ! empty( $schema['properties'][ $base ] ) ) {
$terms = get_the_terms( $post, $taxonomy->name );
$data[ $base ] = $terms ? wp_list_pluck( $terms, 'term_id' ) : array();
}
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down

0 comments on commit 6e04c96

Please sign in to comment.