REST API: Remove rest_get_post filter and `get_post` abstraction.

This filter was originally introduced in https://github.com/WP-API/WP-API/pull/2535 to support Customizer Changesets (née Transactions). This is a super broad filter and doesn't really fit with the design of the API, nor is it (arguably) the right level to do this.

Props rmccue.
Fixes #38701.


git-svn-id: https://develop.svn.wordpress.org/trunk@39161 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe Hoyle 2016-11-08 13:08:49 +00:00
parent 2eca6870b9
commit 6fd4d6e0ea
6 changed files with 26 additions and 87 deletions

View File

@ -72,7 +72,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
// Attaching media to a post requires ability to edit said post.
if ( ! empty( $request['post'] ) ) {
$parent = $this->get_post( (int) $request['post'] );
$parent = get_post( (int) $request['post'] );
$post_parent_type = get_post_type_object( $parent->post_type );
if ( ! current_user_can( $post_parent_type->cap->edit_post, $request['post'] ) ) {
@ -153,7 +153,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
return $id;
}
$attachment = $this->get_post( $id );
$attachment = get_post( $id );
// Include admin functions to get access to wp_generate_attachment_metadata().
require_once ABSPATH . 'wp-admin/includes/admin.php';
@ -217,7 +217,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] );
}
$attachment = $this->get_post( $request['id'] );
$attachment = get_post( $request['id'] );
$fields_update = $this->update_additional_fields_for_object( $attachment, $request );

View File

@ -106,7 +106,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
if ( ! empty( $request['post'] ) ) {
foreach ( (array) $request['post'] as $post_id ) {
$post = $this->get_post( $post_id );
$post = get_post( $post_id );
if ( ! empty( $post_id ) && $post && ! $this->check_read_post_permission( $post ) ) {
return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
@ -314,7 +314,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_cannot_read', __( 'Sorry, you cannot read this comment.' ), array( 'status' => rest_authorization_required_code() ) );
}
$post = $this->get_post( $comment->comment_post_ID );
$post = get_post( $comment->comment_post_ID );
if ( $post && ! $this->check_read_post_permission( $post ) ) {
return new WP_Error( 'rest_cannot_read_post', __( 'Sorry, you cannot read the post for this comment.' ), array( 'status' => rest_authorization_required_code() ) );
@ -345,7 +345,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
if ( ! empty( $comment->comment_post_ID ) ) {
$post = $this->get_post( $comment->comment_post_ID );
$post = get_post( $comment->comment_post_ID );
if ( empty( $post ) ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
}
@ -389,7 +389,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_comment_invalid_post_id', __( 'Sorry, you cannot create this comment without a post.' ), array( 'status' => rest_authorization_required_code() ) );
}
if ( ! empty( $request['post'] ) && $post = $this->get_post( (int) $request['post'] ) ) {
if ( ! empty( $request['post'] ) && $post = get_post( (int) $request['post'] ) ) {
if ( 'draft' === $post->post_status ) {
return new WP_Error( 'rest_comment_draft_post', __( 'Sorry, you cannot create a comment on this post.' ), array( 'status' => 403 ) );
}
@ -871,7 +871,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller {
}
if ( 0 !== (int) $comment->comment_post_ID ) {
$post = $this->get_post( $comment->comment_post_ID );
$post = get_post( $comment->comment_post_ID );
if ( ! empty( $post->ID ) ) {
$obj = get_post_type_object( $post->post_type );

View File

@ -580,41 +580,6 @@ abstract class WP_REST_Controller {
return $endpoint_args;
}
/**
* Retrieves post data given a post ID or post object.
*
* This is a subset of the functionality of the `get_post()` function, with
* the additional functionality of having `the_post` action done on the
* resultant post object. This is done so that plugins may manipulate the
* post that is used in the REST API.
*
* @since 4.7.0
* @access public
*
* @see get_post()
* @global WP_Query $wp_query
*
* @param int|WP_Post $post Post ID or object. Defaults to global `$post` object.
* @return WP_Post|null A WP_Post object when successful, otherwise null.
*/
public function get_post( $post ) {
$post_obj = get_post( $post );
/**
* Filters the post in the context of a REST request.
*
* Allows plugins to filter the post object as returned by WP_REST_Controller::get_post().
*
* @since 4.7.0
*
* @param WP_Post|null $post_obj The post object as returned by get_post().
* @param int|WP_Post $post The original value used to obtain the post object.
*/
$post = apply_filters( 'rest_the_post', $post_obj, $post );
return $post;
}
/**
* Sanitizes the slug value.
*

View File

@ -350,7 +350,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
public function get_item_permissions_check( $request ) {
$post = $this->get_post( (int) $request['id'] );
$post = get_post( (int) $request['id'] );
if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
@ -419,7 +419,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$post = $this->get_post( $id );
$post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
@ -531,7 +531,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $terms_update;
}
$post = $this->get_post( $post_id );
$post = get_post( $post_id );
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], (int) $request['id'] );
@ -582,7 +582,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
public function update_item_permissions_check( $request ) {
$post = $this->get_post( $request['id'] );
$post = get_post( $request['id'] );
$post_type = get_post_type_object( $this->post_type );
if ( $post && ! $this->check_update_permission( $post ) ) {
@ -615,7 +615,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
public function update_item( $request ) {
$id = (int) $request['id'];
$post = $this->get_post( $id );
$post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Post id is invalid.' ), array( 'status' => 404 ) );
@ -667,7 +667,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
return $terms_update;
}
$post = $this->get_post( $post_id );
$post = get_post( $post_id );
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
$meta_update = $this->meta->update_value( $request['meta'], $post->ID );
@ -704,7 +704,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
public function delete_item_permissions_check( $request ) {
$post = $this->get_post( $request['id'] );
$post = get_post( $request['id'] );
if ( $post && ! $this->check_delete_permission( $post ) ) {
return new WP_Error( 'rest_cannot_delete', __( 'Sorry, you are not allowed to delete posts.' ), array( 'status' => rest_authorization_required_code() ) );
@ -726,7 +726,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$id = (int) $request['id'];
$force = (bool) $request['force'];
$post = $this->get_post( $id );
$post = get_post( $id );
if ( empty( $id ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post id.' ), array( 'status' => 404 ) );
@ -779,7 +779,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
// (Note that internally this falls through to `wp_delete_post` if
// the trash is disabled.)
$result = wp_trash_post( $id );
$post = $this->get_post( $id );
$post = get_post( $id );
$response = $this->prepare_item_for_response( $post, $request );
}
@ -1071,7 +1071,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
// Parent.
if ( ! empty( $schema['properties']['parent'] ) && ! empty( $request['parent'] ) ) {
$parent = $this->get_post( (int) $request['parent'] );
$parent = get_post( (int) $request['parent'] );
if ( empty( $parent ) ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post parent id.' ), array( 'status' => 400 ) );
@ -1183,7 +1183,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
* @param integer $post_id Post ID.
*/
public function handle_template( $template, $post_id ) {
if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( $this->get_post( $post_id ) ) ), true ) ) {
if ( in_array( $template, array_keys( wp_get_theme()->get_page_templates( get_post( $post_id ) ) ), true ) ) {
update_post_meta( $post_id, '_wp_page_template', $template );
} else {
update_post_meta( $post_id, '_wp_page_template', '' );
@ -1300,7 +1300,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
// Can we read the parent if we're inheriting?
if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
$parent = $this->get_post( $post->post_parent );
$parent = get_post( $post->post_parent );
return $this->check_read_permission( $parent );
}

View File

@ -117,7 +117,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
*/
public function get_items_permissions_check( $request ) {
$parent = $this->get_post( $request['parent'] );
$parent = get_post( $request['parent'] );
if ( ! $parent ) {
return true;
}
@ -139,8 +139,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$parent = $this->get_post( $request['parent'] );
$parent = get_post( $request['parent'] );
if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) );
}
@ -178,13 +177,12 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_item( $request ) {
$parent = $this->get_post( $request['parent'] );
$parent = get_post( $request['parent'] );
if ( ! $request['parent'] || ! $parent || $this->parent_post_type !== $parent->post_type ) {
return new WP_Error( 'rest_post_invalid_parent', __( 'Invalid post parent id.' ), array( 'status' => 404 ) );
}
$revision = $this->get_post( $request['id'] );
$revision = get_post( $request['id'] );
if ( ! $revision || 'revision' !== $revision->post_type ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) );
}
@ -209,7 +207,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
return $response;
}
$post = $this->get_post( $request['id'] );
$post = get_post( $request['id'] );
if ( ! $post ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid revision id.' ), array( 'status' => 404 ) );
}
@ -234,7 +232,7 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
return new WP_Error( 'rest_trash_not_supported', __( 'Revisions do not support trashing. Set force=true to delete.' ), array( 'status' => 501 ) );
}
$revision = $this->get_post( $request['id'] );
$revision = get_post( $request['id'] );
$previous = $this->prepare_item_for_response( $revision, $request );
$result = wp_delete_post( $request['id'], true );

View File

@ -190,28 +190,4 @@ class WP_Test_REST_Controller extends WP_Test_REST_TestCase {
$this->assertEquals( 'a', $args['somedefault']['default'] );
}
public $rest_the_post_filter_apply_count = 0;
public function test_get_post() {
$post_id = $this->factory()->post->create( array( 'post_title' => 'Original' ) );
$controller = new WP_REST_Test_Controller();
$post = $controller->get_post( $post_id );
$this->assertEquals( 'Original', $post->post_title );
$filter_apply_count = $this->rest_the_post_filter_apply_count;
add_filter( 'rest_the_post', array( $this, 'filter_rest_the_post_for_test_get_post' ), 10, 2 );
$post = $controller->get_post( $post_id );
$this->assertEquals( 'Overridden', $post->post_title );
$this->assertEquals( 1 + $filter_apply_count, $this->rest_the_post_filter_apply_count );
}
public function filter_rest_the_post_for_test_get_post( $post, $post_id ) {
$this->assertInstanceOf( 'WP_Post', $post );
$this->assertInternalType( 'int', $post_id );
$post->post_title = 'Overridden';
$this->rest_the_post_filter_apply_count += 1;
return $post;
}
}