diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php --- a/wp/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php Tue Dec 15 13:49:49 2020 +0100 @@ -12,6 +12,7 @@ * * @since 5.0.0 * + * @see WP_REST_Revisions_Controller * @see WP_REST_Controller */ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { @@ -58,11 +59,13 @@ public function __construct( $parent_post_type ) { $this->parent_post_type = $parent_post_type; $post_type_object = get_post_type_object( $parent_post_type ); + $parent_controller = $post_type_object->get_rest_controller(); - // Ensure that post type-specific controller logic is available. - $parent_controller_class = ! empty( $post_type_object->rest_controller_class ) ? $post_type_object->rest_controller_class : 'WP_REST_Posts_Controller'; + if ( ! $parent_controller ) { + $parent_controller = new WP_REST_Posts_Controller( $parent_post_type ); + } - $this->parent_controller = new $parent_controller_class( $post_type_object->name ); + $this->parent_controller = $parent_controller; $this->revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type ); $this->rest_namespace = 'wp/v2'; $this->rest_base = 'autosaves'; @@ -70,7 +73,7 @@ } /** - * Registers routes for autosaves. + * Registers the routes for autosaves. * * @since 5.0.0 * @@ -148,7 +151,7 @@ * * @since 5.0.0 * - * @param WP_REST_Request $request Full data about the request. + * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access, WP_Error object otherwise. */ public function get_items_permissions_check( $request ) { @@ -157,9 +160,12 @@ return $parent; } - $parent_post_type_obj = get_post_type_object( $parent->post_type ); - if ( ! current_user_can( $parent_post_type_obj->cap->edit_post, $parent->ID ) ) { - return new WP_Error( 'rest_cannot_read', __( 'Sorry, you are not allowed to view autosaves of this post.' ), array( 'status' => rest_authorization_required_code() ) ); + if ( ! current_user_can( 'edit_post', $parent->ID ) ) { + return new WP_Error( + 'rest_cannot_read', + __( 'Sorry, you are not allowed to view autosaves of this post.' ), + array( 'status' => rest_authorization_required_code() ) + ); } return true; @@ -178,8 +184,13 @@ */ public function create_item_permissions_check( $request ) { $id = $request->get_param( 'id' ); + if ( empty( $id ) ) { - return new WP_Error( 'rest_post_invalid_id', __( 'Invalid item ID.' ), array( 'status' => 404 ) ); + return new WP_Error( + 'rest_post_invalid_id', + __( 'Invalid item ID.' ), + array( 'status' => 404 ) + ); } return $this->parent_controller->update_item_permissions_check( $request ); @@ -211,7 +222,7 @@ if ( ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) && $post->post_author == $user_id ) { // Draft posts for the same author: autosaving updates the post and does not create a revision. - // Convert the post object to an array and add slashes, wp_update_post expects escaped array. + // Convert the post object to an array and add slashes, wp_update_post() expects escaped array. $autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true ); } else { // Non-draft posts: create or update the post autosave. @@ -236,20 +247,28 @@ * * @since 5.0.0 * - * @param WP_REST_Request $request Full data about the request. + * @param WP_REST_Request $request Full details about the request. * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. */ public function get_item( $request ) { $parent_id = (int) $request->get_param( 'parent' ); if ( $parent_id <= 0 ) { - return new WP_Error( 'rest_post_invalid_id', __( 'Invalid parent post ID.' ), array( 'status' => 404 ) ); + return new WP_Error( + 'rest_post_invalid_id', + __( 'Invalid post parent ID.' ), + array( 'status' => 404 ) + ); } $autosave = wp_get_post_autosave( $parent_id ); if ( ! $autosave ) { - return new WP_Error( 'rest_post_no_autosave', __( 'There is no autosave revision for this post.' ), array( 'status' => 404 ) ); + return new WP_Error( + 'rest_post_no_autosave', + __( 'There is no autosave revision for this post.' ), + array( 'status' => 404 ) + ); } $response = $this->prepare_item_for_response( $autosave, $request ); @@ -263,7 +282,7 @@ * * @since 5.0.0 * - * @param WP_REST_Request $request Full data about the request. + * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_items( $request ) { @@ -295,6 +314,10 @@ * @return array Item schema data. */ public function get_item_schema() { + if ( $this->schema ) { + return $this->add_additional_fields_schema( $this->schema ); + } + $schema = $this->revisions_controller->get_item_schema(); $schema['properties']['preview_link'] = array( @@ -305,7 +328,9 @@ 'readonly' => true, ); - return $schema; + $this->schema = $schema; + + return $this->add_additional_fields_schema( $this->schema ); } /** @@ -315,7 +340,7 @@ * * @since 5.0.0 * - * @param mixed $post_data Associative array containing the post data. + * @param array $post_data Associative array containing the post data. * @return mixed The autosave revision ID or WP_Error. */ public function create_post_autosave( $post_data ) { @@ -341,7 +366,7 @@ $autosave_is_different = false; foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { - if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) { + if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) { $autosave_is_different = true; break; } @@ -349,15 +374,17 @@ if ( ! $autosave_is_different ) { wp_delete_post_revision( $old_autosave->ID ); - return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.' ), array( 'status' => 400 ) ); + return new WP_Error( + 'rest_autosave_no_changes', + __( 'There is nothing to save. The autosave and the post content are the same.' ), + array( 'status' => 400 ) + ); } - /** - * This filter is documented in wp-admin/post.php. - */ + /** This filter is documented in wp-admin/post.php */ do_action( 'wp_creating_autosave', $new_autosave ); - // wp_update_post expects escaped array. + // wp_update_post() expects escaped array. return wp_update_post( wp_slash( $new_autosave ) ); } @@ -372,7 +399,6 @@ * * @param WP_Post $post Post revision object. * @param WP_REST_Request $request Request object. - * * @return WP_REST_Response Response object. */ public function prepare_item_for_response( $post, $request ) {