Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix errors with excerpt #226

Merged
merged 3 commits into from

3 participants

@rmccue
Owner

Fixes #222.

rmccue added some commits
@rmccue rmccue Set up and restore post data
Turns out subtle errors turn up if we don't do this. *sigh*
5d55204
@rmccue rmccue Provide raw excerpt with edit context a12065d
@rmccue
Owner

Needs tests and docs.

@rmccue rmccue added this to the 1.1 milestone
@rmccue rmccue added the Bug label
@rmccue rmccue self-assigned this
@rmccue
Owner

Tests blocked on #99.

@rmccue
Owner

Turns out this is already documented, we must have just removed the docs accidentally.

@rachelbaker
Owner

@rmccue is this ready for a #reviewmerge?

@rmccue
Owner

@rachelbaker Oh, yes! #reviewmerge :)

@rmccue rmccue added the Review label
@kadamwhite
Collaborator

Feels slightly WET, but abstracting the repeated code out may be overkill

@rmccue
Owner

Feels slightly WET, but abstracting the repeated code out may be overkill

Indeed, I'm not sure on this one. Surprised there isn't an existing way to do it in WP. (wp_reset_postdata doesn't work here)

@rachelbaker
Owner

@rmccue @kadamwhite This does resolve the issue, so I am merging for now. The code can be refactored later in another issue.

@rachelbaker rachelbaker merged commit 0b31094 into master
@rachelbaker rachelbaker deleted the fix-excerpt-errors branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 27, 2014
  1. @rmccue

    Set up and restore post data

    rmccue authored
    Turns out subtle errors turn up if we don't do this. *sigh*
  2. @rmccue
  3. @rmccue
This page is out of date. Refresh to see the latest.
Showing with 25 additions and 0 deletions.
  1. +25 −0 lib/class-wp-json-posts.php
View
25 lib/class-wp-json-posts.php
@@ -612,6 +612,14 @@ protected function prepare_post( $post, $context = 'view' ) {
if ( ! $this->check_read_permission( $post ) )
return new WP_Error( 'json_user_cannot_read', __( 'Sorry, you cannot read this post.' ), array( 'status' => 401 ) );
+ $previous_post = null;
+ if ( ! empty( $GLOBALS['post'] ) ) {
+ $previous_post = $GLOBALS['post'];
+ }
+ $post_obj = get_post( $post['ID'] );
+ $GLOBALS['post'] = $post_obj;
+ setup_postdata( $post_obj );
+
// prepare common post fields
$post_fields = array(
'title' => get_the_title( $post['ID'] ), // $post['post_title'],
@@ -635,6 +643,7 @@ protected function prepare_post( $post, $context = 'view' ) {
$post_fields_raw = array(
'title_raw' => $post['post_title'],
'content_raw' => $post['post_content'],
+ 'excerpt_raw' => $post['post_excerpt'],
'guid_raw' => $post['guid'],
'post_meta' => $this->get_all_meta( $post['ID'] ),
);
@@ -685,17 +694,29 @@ protected function prepare_post( $post, $context = 'view' ) {
if ( 'edit' === $context ) {
if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) {
if ( is_wp_error( $post_fields_raw['post_meta'] ) ) {
+ $GLOBALS['post'] = $previous_post;
+ if ( $previous_post ) {
+ setup_postdata( $previous_post );
+ }
return $post_fields_raw['post_meta'];
}
$_post = array_merge( $_post, $post_fields_raw );
} else {
+ $GLOBALS['post'] = $previous_post;
+ if ( $previous_post ) {
+ setup_postdata( $previous_post );
+ }
return new WP_Error( 'json_cannot_edit', __( 'Sorry, you cannot edit this post' ), array( 'status' => 403 ) );
}
} elseif ( 'view-revision' == $context ) {
if ( current_user_can( $post_type->cap->edit_post, $post['ID'] ) ) {
$_post = array_merge( $_post, $post_fields_raw );
} else {
+ $GLOBALS['post'] = $previous_post;
+ if ( $previous_post ) {
+ setup_postdata( $previous_post );
+ }
return new WP_Error( 'json_cannot_view', __( 'Sorry, you cannot view this revision' ), array( 'status' => 403 ) );
}
}
@@ -717,6 +738,10 @@ protected function prepare_post( $post, $context = 'view' ) {
if ( ! empty( $post['post_parent'] ) )
$_post['meta']['links']['up'] = json_url( '/posts/' . (int) $post['post_parent'] );
+ $GLOBALS['post'] = $previous_post;
+ if ( $previous_post ) {
+ setup_postdata( $previous_post );
+ }
return apply_filters( 'json_prepare_post', $_post, $post, $context );
}
Something went wrong with that request. Please try again.