WP_REST_Autosaves_Controller::create_post_autosave() public WP 5.0.0
Creates autosave for the specified post.
From wp-admin/post.php.
{} It's a method of the class: WP_REST_Autosaves_Controller{}
Hooks from the method
Return
Mixed. The autosave revision ID or WP_Error.
Usage
$WP_REST_Autosaves_Controller = new WP_REST_Autosaves_Controller(); $WP_REST_Autosaves_Controller->create_post_autosave( $post_data );
- $post_data(array) (required)
- Associative array containing the post data.
Changelog
Since 5.0.0 | Introduced. |
Code of WP_REST_Autosaves_Controller::create_post_autosave() WP REST Autosaves Controller::create post autosave WP 5.6
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 );
}