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:366
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
- Scaling
- Scales with input
- Instructions
- 13–112
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 137.
Third-party callbacks on 'wp_creating_autosave' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_cache_get()one call below WP_REST_Autosaves_Controller::create_post_autosave() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 13 | get_post(), is_wp_error() |
!is_wp_error() && empty($meta) | 47–48 | get_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–57 | get_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–63 | get_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) | 61 | get_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–72 | get_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) | 67–75 | get_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() && !array_intersect() && !empty($meta) | 69–70 | get_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–76 | get_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) && !$revisioned_meta_keys && $new_meta !== false | 70–72 | get_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() && !empty($meta) | 75–84 | get_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() && !array_intersect() && !empty($meta) | 77–85 | get_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() |
7 further outcomes, up to 112 instructions
!is_wp_error() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false | 78–87 | get_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() |
!is_wp_error() && !array_intersect() && empty($meta) | 81–88 | get_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 !== false | 84–85 | get_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() && !array_intersect() && !empty($meta) | 89–97 | get_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() && !empty($meta) && !$revisioned_meta_keys && $new_meta !== false | 90–99 | get_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) && !$revisioned_meta_keys && $new_meta !== false | 92–100 | get_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 !== false | 104–112 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 137 | 13–112 | 18 | |
| 8.5 | 137 | 13–112 | 18 | |
| 8.4 | 137 | 13–112 | 18 | |
| 8.3 | 137 | 13–112 | 18 | |
| 8.2 | 137 | 13–112 | 18 | |
| 8.1 | 137 | 13–112 | 18 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 138 | 13–113 | 18 |
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:
- do_action( wp_creating_autosave )actionline 416 (+50 into the body)
Fires before an autosave is stored.
Uses · 14
- get_post()Retrieves post data given a post ID or post object.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _wp_post_revision_data()Returns a post array ready to be inserted into the posts table as a post revision.
- _wp_post_revision_fields()Determines which fields of posts are to be saved in revisions.
- normalize_whitespace()Normalizes EOL characters and strips duplicate whitespace.
- wp_post_revision_meta_keys()Determine which post meta fields should be revisioned.
- get_metadata_raw()Retrieves raw metadata value for the specified object.
- get_current_user_id()Gets the current user's ID.
- wp_get_post_autosave()Retrieves the autosaved data of the specified post.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_update_post()Updates a post with new post data.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
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
- WP_REST_Autosaves_Controller::create_item()Creates, updates or deletes an autosave revision.
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 = isset( $meta[ $meta_key ] ) ? $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 filter is documented in wp-admin/post.php */ do_action( 'wp_creating_autosave', $new_autosave ); // 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.
Signature, return type and hooks compared across 5 parsed releases.
$meta parameter was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.