Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ public function register_routes() {
'schema' => array( $this, 'get_public_item_schema' ),
)
);

register_rest_route(
$this->namespace,
'/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)/restore',
array(
'args' => array(
'parent' => array(
'description' => __( 'The ID for the parent of the revision.' ),
'type' => 'integer',
),
'id' => array(
'description' => __( 'Unique identifier for the revision.' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'restore_item' ),
'permission_callback' => array( $this, 'restore_item_permissions_check' ),
),
)
);
}

/**
Expand Down Expand Up @@ -553,6 +575,120 @@ public function delete_item( $request ) {
return $response;
}

/**
* Checks if a given request has access to restore a revision.
*
* @since 7.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has access to restore the revision, WP_Error object otherwise.
*/
public function restore_item_permissions_check( $request ) {
$parent = $this->get_parent_for_restore( $request );
if ( is_wp_error( $parent ) ) {
return $parent;
}

if ( ! current_user_can( 'edit_post', $parent->ID ) ) {
return new WP_Error(
'rest_cannot_restore_revision',
__( 'Sorry, you are not allowed to restore revisions of this post.' ),
array( 'status' => rest_authorization_required_code() )
);
}

return true;
}

/**
* Restores a revision and returns the updated parent.
*
* The restore runs through wp_restore_post_revision(), as it does on the
* revisions screen, so the `wp_restore_post_revision` action fires and all
* revisioned meta is restored, including meta that is not shown in REST.
*
* @since 7.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error The parent as its own endpoint returns it for editing, or WP_Error object on failure.
*/
public function restore_item( $request ) {
$parent = $this->get_parent_for_restore( $request );
if ( is_wp_error( $parent ) ) {
return $parent;
}

$revision_id = $request['id'];
$revision = $this->get_revision( is_numeric( $revision_id ) ? (int) $revision_id : 0 );
if ( is_wp_error( $revision ) ) {
return $revision;
}

if ( (int) $parent->ID !== (int) $revision->post_parent ) {
return new WP_Error(
'rest_revision_parent_id_mismatch',
/* translators: %d: A post id. */
sprintf( __( 'The revision does not belong to the specified parent with id of "%d"' ), $parent->ID ),
array( 'status' => 404 )
);
}

// The revisions screen only restores autosaves while revisions are disabled.
if ( ! wp_revisions_enabled( $parent ) && ! wp_is_post_autosave( $revision ) ) {
return new WP_Error(
'rest_revisions_disabled',
__( 'Revisions are disabled for this post.' ),
array( 'status' => 400 )
);
}

if ( ! wp_restore_post_revision( $revision->ID ) ) {
return new WP_Error(
'rest_cannot_restore_revision',
__( 'The revision could not be restored.' ),
array( 'status' => 500 )
);
}

$parent_request = new WP_REST_Request( WP_REST_Server::READABLE );
$parent_request->set_param( 'context', 'edit' );

return $this->prepare_restored_parent_for_response( $parent->ID, $request, $parent_request );
}

/**
* Gets the parent post of the revision that a request restores.
*
* @since 7.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Post|WP_Error Post object if the parent ID is valid, WP_Error otherwise.
*/
protected function get_parent_for_restore( $request ) {
$parent_id = $request['parent'];

return $this->get_parent( is_numeric( $parent_id ) ? (int) $parent_id : 0 );
}

/**
* Prepares the parent of a restored revision as its own endpoint returns it.
*
* @since 7.3.0
*
* @param int $parent_id ID of the restored post.
* @param WP_REST_Request $request The restore request.
* @param WP_REST_Request $parent_request Request to prepare the parent with.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
protected function prepare_restored_parent_for_response( $parent_id, $request, $parent_request ) {
$parent = $this->get_parent( $parent_id );
if ( is_wp_error( $parent ) ) {
return $parent;
}

return rest_ensure_response( $this->parent_controller->prepare_item_for_response( $parent, $parent_request ) );
}

/**
* Determines the allowed query_vars for a get_items() response and prepares
* them for WP_Query.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,41 @@ public function register_routes() {
'schema' => array( $this, 'get_public_item_schema' ),
)
);

register_rest_route(
$this->namespace,
sprintf(
'/%s/(?P<parent>%s%s)/%s/%s/restore',
$this->parent_base,
/*
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
* Excludes invalid directory name characters: `/:<>*?"|`.
*/
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
// Matches the template name.
'[\/\w%-]+',
$this->rest_base,
'(?P<id>[\d]+)'
),
array(
'args' => array(
'parent' => array(
'description' => __( 'The id of a template' ),
'type' => 'string',
'sanitize_callback' => array( $this->parent_controller, '_sanitize_template_id' ),
),
'id' => array(
'description' => __( 'Unique identifier for the revision.' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'restore_item' ),
'permission_callback' => array( $this, 'restore_item_permissions_check' ),
),
)
);
}

/**
Expand Down Expand Up @@ -187,6 +222,45 @@ protected function get_parent( $parent_template_id ) {
return get_post( $template->wp_id );
}

/**
* Gets the parent post of the template revision that a request restores.
*
* @since 7.3.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Post|WP_Error Post object if the template ID is valid, WP_Error otherwise.
*/
protected function get_parent_for_restore( $request ) {
$template_id = $request['parent'];

return $this->get_parent( is_string( $template_id ) ? $template_id : '' );
}

/**
* Prepares the template of a restored revision as its own endpoint returns it.
*
* @since 7.3.0
*
* @param int $parent_id ID of the restored post.
* @param WP_REST_Request $request The restore request.
* @param WP_REST_Request $parent_request Request to prepare the template with.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
protected function prepare_restored_parent_for_response( $parent_id, $request, $parent_request ) {
$template_id = $request['parent'];
$template = is_string( $template_id ) ? get_block_template( $template_id, $this->parent_post_type ) : null;

if ( ! $template ) {
return new WP_Error(
'rest_post_invalid_parent',
__( 'Invalid template parent ID.' ),
array( 'status' => WP_Http::NOT_FOUND )
);
}

return rest_ensure_response( $this->parent_controller->prepare_item_for_response( $template, $parent_request ) );
}

/**
* Prepares the item for the REST response.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/posts/(?P<id>[\\d]+)',
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions',
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
'/wp/v2/posts/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)/restore',
'/wp/v2/posts/(?P<id>[\\d]+)/autosaves',
'/wp/v2/posts/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)',
'/wp/v2/menu-items',
Expand All @@ -107,6 +108,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/pages/(?P<id>[\\d]+)',
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions',
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)',
'/wp/v2/pages/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)/restore',
'/wp/v2/pages/(?P<id>[\\d]+)/autosaves',
'/wp/v2/pages/(?P<parent>[\\d]+)/autosaves/(?P<id>[\\d]+)',
'/wp/v2/pattern-directory/patterns',
Expand All @@ -122,6 +124,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/blocks/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/blocks/(?P<parent>[\d]+)/revisions',
'/wp/v2/blocks/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/blocks/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)/restore',
'/wp/v2/types',
'/wp/v2/types/(?P<type>[\\w-]+)',
'/wp/v2/statuses',
Expand Down Expand Up @@ -157,13 +160,15 @@ public function test_expected_routes_in_schema() {
'/wp/v2/template-parts/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/template-parts/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions',
'/wp/v2/template-parts/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/template-parts/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)/restore',
'/wp/v2/template-parts/lookup',
'/wp/v2/templates',
'/wp/v2/templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)',
'/wp/v2/templates/(?P<id>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/templates/(?P<parent>([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)[\/\w%-]+)/revisions/(?P<id>[\d]+)/restore',
'/wp/v2/templates/lookup',
'/wp/v2/themes',
'/wp/v2/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
Expand All @@ -186,6 +191,7 @@ public function test_expected_routes_in_schema() {
'/wp/v2/navigation/(?P<parent>[\d]+)/autosaves/(?P<id>[\d]+)',
'/wp/v2/navigation/(?P<parent>[\d]+)/revisions',
'/wp/v2/navigation/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
'/wp/v2/navigation/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)/restore',
'/wp-site-health/v1',
'/wp-site-health/v1/tests/background-updates',
'/wp-site-health/v1/tests/loopback-requests',
Expand Down
Loading
Loading