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 #2693 from websupporter/revisions-rendered-contents
Browse files Browse the repository at this point in the history
Add raw and rendered to schema
  • Loading branch information
kadamwhite committed Sep 13, 2016
2 parents bb69048 + a7525c5 commit 427564a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 52 deletions.
133 changes: 85 additions & 48 deletions lib/endpoints/class-wp-rest-revisions-controller.php
Expand Up @@ -180,31 +180,71 @@ public function delete_item( $request ) {
*/
public function prepare_item_for_response( $post, $request ) {

// Base fields for every post
$data = array(
'author' => $post->post_author,
'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ),
'guid' => $post->guid,
'id' => $post->ID,
'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
'parent' => (int) $post->post_parent,
'slug' => $post->post_name,
);

$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(
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'get_the_guid', $post->guid ),
'raw' => $post->guid,
);
}

if ( ! empty( $schema['properties']['title'] ) ) {
$data['title'] = $post->post_title;
$data['title'] = array(
'raw' => $post->post_title,
'rendered' => get_the_title( $post->ID ),
);
}

if ( ! empty( $schema['properties']['content'] ) ) {
$data['content'] = $post->post_content;

$data['content'] = array(
'raw' => $post->post_content,
/** This filter is documented in wp-includes/post-template.php */
'rendered' => apply_filters( 'the_content', $post->post_content ),
);
}

if ( ! empty( $schema['properties']['excerpt'] ) ) {
$data['excerpt'] = $post->post_excerpt;
$data['excerpt'] = array(
'raw' => $post->post_excerpt,
'rendered' => $this->prepare_excerpt_response( $post->post_excerpt, $post ),
);
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
Expand Down Expand Up @@ -316,38 +356,17 @@ public function get_item_schema() {

$parent_schema = $this->parent_controller->get_item_schema();

foreach ( array( 'title', 'content', 'excerpt' ) as $property ) {
if ( empty( $parent_schema['properties'][ $property ] ) ) {
continue;
}

switch ( $property ) {

case 'title':
$schema['properties']['title'] = array(
'description' => __( 'Title for the object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
);
break;

case 'content':
$schema['properties']['content'] = array(
'description' => __( 'Content for the object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
);
break;

case 'excerpt':
$schema['properties']['excerpt'] = array(
'description' => __( 'Excerpt for the object, as it exists in the database.' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
);
break;

}
if ( ! empty( $parent_schema['properties']['title'] ) ) {
$schema['properties']['title'] = $parent_schema['properties']['title'];
}
if ( ! empty( $parent_schema['properties']['content'] ) ) {
$schema['properties']['content'] = $parent_schema['properties']['content'];
}
if ( ! empty( $parent_schema['properties']['excerpt'] ) ) {
$schema['properties']['excerpt'] = $parent_schema['properties']['excerpt'];
}
if ( ! empty( $parent_schema['properties']['guid'] ) ) {
$schema['properties']['guid'] = $parent_schema['properties']['guid'];
}

return $this->add_additional_fields_schema( $schema );
Expand All @@ -364,4 +383,22 @@ public function get_collection_params() {
);
}

/**
* Check the post excerpt and prepare it for single post output.
*
* @param string $excerpt
* @return string|null $excerpt
*/
protected function prepare_excerpt_response( $excerpt, $post ) {

/** This filter is documented in wp-includes/post-template.php */
$excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt, $post ) );

if ( empty( $excerpt ) ) {
return '';
}

return $excerpt;
}

}
18 changes: 14 additions & 4 deletions tests/test-rest-revisions-controller.php
Expand Up @@ -267,16 +267,26 @@ protected function check_get_revision_response( $response, $revision ) {
}

$this->assertEquals( $revision->post_author, $response['author'] );
$this->assertEquals( $revision->post_content, $response['content'] );

$rendered_content = apply_filters( 'the_content', $revision->post_content );
$this->assertEquals( $rendered_content, $response['content']['rendered'] );

$this->assertEquals( mysql_to_rfc3339( $revision->post_date ), $response['date'] );
$this->assertEquals( mysql_to_rfc3339( $revision->post_date_gmt ), $response['date_gmt'] );
$this->assertEquals( $revision->post_excerpt, $response['excerpt'] );
$this->assertEquals( $revision->guid, $response['guid'] );

$rendered_excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $revision->post_excerpt, $revision ) );
$this->assertEquals( $rendered_excerpt, $response['excerpt']['rendered'] );

$rendered_guid = apply_filters( 'get_the_guid', $revision->guid );
$this->assertEquals( $rendered_guid, $response['guid']['rendered'] );

$this->assertEquals( $revision->ID, $response['id'] );
$this->assertEquals( mysql_to_rfc3339( $revision->post_modified ), $response['modified'] );
$this->assertEquals( mysql_to_rfc3339( $revision->post_modified_gmt ), $response['modified_gmt'] );
$this->assertEquals( $revision->post_name, $response['slug'] );
$this->assertEquals( $revision->post_title, $response['title'] );

$rendered_title = get_the_title( $revision->ID );
$this->assertEquals( $rendered_title, $response['title']['rendered'] );

$parent = get_post( $revision->post_parent );
$parent_controller = new WP_REST_Posts_Controller( $parent->post_type );
Expand Down

0 comments on commit 427564a

Please sign in to comment.