wppaste
WordPress

WP_REST_Autosaves_Controller::create_post_autosave( array $post_data, array $meta = array() ): mixed

Since
5.0.0, 6.4.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php:378
Creates autosave for the specified post.

Description

From wp-admin/post.php.

Compatibility

WordPress
since 6.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$post_dataarray
Associative array containing the post data.
$metaarrayoptional
Associative array containing the post meta data.Default: array()

Return value

mixed
The autosave revision ID or WP_Error.

Performance profile

How much work a call to WP_REST_Autosaves_Controller::create_post_autosave() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
13–112

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 136.

Plugin surface
1 hook

Third-party callbacks on 'wp_creating_autosave' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_get()one call below WP_REST_Autosaves_Controller::create_post_autosave()
  • serializeserialisationmaybe_unserialize()one call below WP_REST_Autosaves_Controller::create_post_autosave()

Further down the call graph this can also reach option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 19 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Autosaves_Controller::create_post_autosave() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_wp_error()13get_post(), is_wp_error()
!is_wp_error() && empty($meta)47–48get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && !empty($meta)55–57get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && empty($meta)55–63get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && !array_intersect() && empty($meta)61get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && !empty($meta)63–72get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && empty($meta)68–76get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()
!is_wp_error() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false69–71get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && !array_intersect() && !empty($meta)69–70get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && !array_intersect() && empty($meta)69–76get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && !empty($meta)76–85get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()
!is_wp_error() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false77–86get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
7 further outcomes, up to 112 instructions
!is_wp_error() && !array_intersect() && !empty($meta)77–85get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && !array_intersect() && empty($meta)82–89get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()
!is_wp_error() && !array_intersect() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false83–84get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave()
!is_wp_error() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false90–99get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()
!is_wp_error() && !array_intersect() && !empty($meta)90–98get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()
!is_wp_error() && !array_intersect() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false91–99get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave(), _wp_put_post_revision(), is_wp_error()
!is_wp_error() && !array_intersect() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false104–112get_post(), is_wp_error(), _wp_post_revision_data(), array_keys(), _wp_post_revision_fields(), array_keys(), array_intersect(), normalize_whitespace(), normalize_whitespace(), wp_post_revision_meta_keys(), get_metadata_raw(), get_current_user_id(), wp_get_post_autosave(), do_action(), wp_slash(), wp_update_post(), is_wp_error()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 136 instructions, 13–112 executed per call, 18 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_REST_Autosaves_Controller::create_post_autosave() runs, in this order:

  1. do_action( wp_creating_autosave )actionline 428 (+50 into the body)

    Fires before an autosave is stored.

Uses · 14

Show all 14
  • _wp_put_post_revision()Inserts post data into the posts table as a post revision.
  • update_metadata()Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added.

Used by · 1

Source code

	public function create_post_autosave( $post_data, array $meta = array() ) { 		$post_id = (int) $post_data['ID'];		$post    = get_post( $post_id ); 		if ( is_wp_error( $post ) ) {			return $post;		} 		// Only create an autosave when it is different from the saved post.		$autosave_is_different = false;		$new_autosave          = _wp_post_revision_data( $post_data, true ); 		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;			}		} 		// Check if meta values have changed.		if ( ! empty( $meta ) ) {			$revisioned_meta_keys = wp_post_revision_meta_keys( $post->post_type );			foreach ( $revisioned_meta_keys as $meta_key ) {				// get_metadata_raw is used to avoid retrieving the default value.				$old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );				$new_meta = $meta[ $meta_key ] ?? ''; 				if ( $new_meta !== $old_meta ) {					$autosave_is_different = true;					break;				}			}		} 		$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 ( ! $autosave_is_different && $old_autosave ) {			// Nothing to save, return the existing autosave.			return $old_autosave->ID;		} 		if ( $old_autosave ) {			$new_autosave['ID']          = $old_autosave->ID;			$new_autosave['post_author'] = $user_id; 			/** This action is documented in wp-admin/includes/post.php */			do_action( 'wp_creating_autosave', $new_autosave, true ); 			// wp_update_post() expects escaped array.			$revision_id = wp_update_post( wp_slash( $new_autosave ) );		} else {			// Create the new autosave as a special post revision.			$revision_id = _wp_put_post_revision( $post_data, true );		} 		if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {			return $revision_id;		} 		// Attached any passed meta values that have revisions enabled.		if ( ! empty( $meta ) ) {			foreach ( $revisioned_meta_keys as $meta_key ) {				if ( isset( $meta[ $meta_key ] ) ) {					update_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta[ $meta_key ] ) );				}			}		} 		return $revision_id;	}

Changelog

Introduced in 5.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.4.0
The $meta parameter was added.from the docblock
5.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.