On this page
WP_REST_Autosaves_Controller::create_post_autosave( array $post_data ): mixed
Creates autosave for the specified post.
Description
From wp-admin/post.php.
Parameters
$post_dataarray Required-
Associative array containing the post data.
Return
mixed The autosave revision ID or WP_Error.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php. View all references
public function create_post_autosave( $post_data ) {
$post_id = (int) $post_data['ID'];
$post = get_post( $post_id );
if ( is_wp_error( $post ) ) {
return $post;
}
$user_id = get_current_user_id();
// Store one autosave per author. If there is already an autosave, overwrite it.
$old_autosave = wp_get_post_autosave( $post_id, $user_id );
if ( $old_autosave ) {
$new_autosave = _wp_post_revision_data( $post_data, true );
$new_autosave['ID'] = $old_autosave->ID;
$new_autosave['post_author'] = $user_id;
// If the new autosave has the same content as the post, delete the autosave.
$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 ) ) {
$autosave_is_different = true;
break;
}
}
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 )
);
}
/** This filter is documented in wp-admin/post.php */
do_action( 'wp_creating_autosave', $new_autosave );
// wp_update_post() expects escaped array.
return wp_update_post( wp_slash( $new_autosave ) );
}
// Create the new autosave as a special post revision.
return _wp_put_post_revision( $post_data, true );
}
Hooks
- do_action( 'wp_creating_autosave',
array $new_autosave ) -
Fires before an autosave is stored.
Related
Uses
| Uses | Description |
|---|---|
| _wp_post_revision_data() wp-includes/revision.php | Returns a post array ready to be inserted into the posts table as a post revision. |
| normalize_whitespace() wp-includes/formatting.php | Normalizes EOL characters and strips duplicate whitespace. |
| wp_update_post() wp-includes/post.php | Updates a post with new post data. |
| wp_delete_post_revision() wp-includes/revision.php | Deletes a revision. |
| _wp_put_post_revision() wp-includes/revision.php | Inserts post data into the posts table as a post revision. |
| wp_get_post_autosave() wp-includes/revision.php | Retrieves the autosaved data of the specified post. |
| _wp_post_revision_fields() wp-includes/revision.php | Determines which fields of posts are to be saved in revisions. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| wp_slash() wp-includes/formatting.php | Adds slashes to a string or recursively adds slashes to strings within an array. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| get_current_user_id() wp-includes/user.php | Gets the current user’s ID. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Autosaves_Controller::create_item() wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php | Creates, updates or deletes an autosave revision. |
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_autosaves_controller/create_post_autosave