diff -r 7b1b88e27a20 -r 48c4eec2b7e6 wp/wp-includes/revision.php --- a/wp/wp-includes/revision.php Thu Sep 29 08:06:27 2022 +0200 +++ b/wp/wp-includes/revision.php Fri Sep 05 18:40:08 2025 +0200 @@ -17,7 +17,7 @@ * @param array|WP_Post $post Optional. A post array or a WP_Post object being processed * for insertion as a post revision. Default empty array. * @param bool $deprecated Not used. - * @return array Array of fields that can be versioned. + * @return string[] Array of fields that can be versioned. */ function _wp_post_revision_fields( $post = array(), $deprecated = false ) { static $fields = null; @@ -47,9 +47,9 @@ * @since 2.6.0 * @since 4.5.0 The `$post` parameter was added. * - * @param array $fields List of fields to revision. Contains 'post_title', - * 'post_content', and 'post_excerpt' by default. - * @param array $post A post array being processed for insertion as a post revision. + * @param string[] $fields List of fields to revision. Contains 'post_title', + * 'post_content', and 'post_excerpt' by default. + * @param array $post A post array being processed for insertion as a post revision. */ $fields = apply_filters( '_wp_post_revision_fields', $fields, $post ); @@ -96,6 +96,27 @@ } /** + * Saves revisions for a post after all changes have been made. + * + * @since 6.4.0 + * + * @param int $post_id The post id that was inserted. + * @param WP_Post $post The post object that was inserted. + * @param bool $update Whether this insert is updating an existing post. + */ +function wp_save_post_revision_on_insert( $post_id, $post, $update ) { + if ( ! $update ) { + return; + } + + if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) { + return; + } + + wp_save_post_revision( $post_id ); +} + +/** * Creates a revision for the current version of a post. * * Typically used immediately after a post update, as every update is a revision, @@ -111,7 +132,13 @@ return; } + // Prevent saving post revisions if revisions should be saved on wp_after_insert_post. + if ( doing_action( 'post_updated' ) && has_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert' ) ) { + return; + } + $post = get_post( $post_id ); + if ( ! $post ) { return; } @@ -135,16 +162,16 @@ */ $revisions = wp_get_post_revisions( $post_id ); if ( $revisions ) { - // Grab the last revision, but not an autosave. + // Grab the latest revision, but not an autosave. foreach ( $revisions as $revision ) { - if ( false !== strpos( $revision->post_name, "{$revision->post_parent}-revision" ) ) { - $last_revision = $revision; + if ( str_contains( $revision->post_name, "{$revision->post_parent}-revision" ) ) { + $latest_revision = $revision; break; } } /** - * Filters whether the post has changed since the last revision. + * Filters whether the post has changed since the latest revision. * * By default a revision is saved only if one of the revisioned fields has changed. * This filter can override that so a revision is saved even if nothing has changed. @@ -153,14 +180,14 @@ * * @param bool $check_for_changes Whether to check for changes before saving a new revision. * Default true. - * @param WP_Post $last_revision The last revision post object. + * @param WP_Post $latest_revision The latest revision post object. * @param WP_Post $post The post object. */ - if ( isset( $last_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $last_revision, $post ) ) { + if ( isset( $latest_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $latest_revision, $post ) ) { $post_has_changed = false; foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { - if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $last_revision->$field ) ) { + if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $latest_revision->$field ) ) { $post_has_changed = true; break; } @@ -175,10 +202,10 @@ * @since 4.1.0 * * @param bool $post_has_changed Whether the post has changed. - * @param WP_Post $last_revision The last revision post object. + * @param WP_Post $latest_revision The latest revision post object. * @param WP_Post $post The post object. */ - $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post ); + $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $latest_revision, $post ); // Don't save revision if post unchanged. if ( ! $post_has_changed ) { @@ -189,8 +216,10 @@ $return = _wp_put_post_revision( $post ); - // If a limit for the number of revisions to keep has been set, - // delete the oldest ones. + /* + * If a limit for the number of revisions to keep has been set, + * delete the oldest ones. + */ $revisions_to_keep = wp_revisions_to_keep( $post ); if ( $revisions_to_keep < 0 ) { @@ -199,6 +228,20 @@ $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); + /** + * Filters the revisions to be considered for deletion. + * + * @since 6.2.0 + * + * @param WP_Post[] $revisions Array of revisions, or an empty array if none. + * @param int $post_id The ID of the post to save as a revision. + */ + $revisions = apply_filters( + 'wp_save_post_revision_revisions_before_deletion', + $revisions, + $post_id + ); + $delete = count( $revisions ) - $revisions_to_keep; if ( $delete < 1 ) { @@ -208,7 +251,7 @@ $revisions = array_slice( $revisions, 0, $delete ); for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { - if ( false !== strpos( $revisions[ $i ]->post_name, 'autosave' ) ) { + if ( str_contains( $revisions[ $i ]->post_name, 'autosave' ) ) { continue; } @@ -219,7 +262,7 @@ } /** - * Retrieve the autosaved data of the specified post. + * Retrieves the autosaved data of the specified post. * * Returns a post object with the information that was autosaved for the specified post. * If the optional $user_id is passed, returns the autosave for that user, otherwise @@ -230,7 +273,7 @@ * @global wpdb $wpdb WordPress database abstraction object. * * @param int $post_id The post ID. - * @param int $user_id Optional The post author ID. + * @param int $user_id Optional. The post author ID. Default 0. * @return WP_Post|false The autosaved data or false on failure or when no autosave exists. */ function wp_get_post_autosave( $post_id, $user_id = 0 ) { @@ -275,6 +318,7 @@ */ function wp_is_post_revision( $post ) { $post = wp_get_post_revision( $post ); + if ( ! $post ) { return false; } @@ -292,11 +336,12 @@ */ function wp_is_post_autosave( $post ) { $post = wp_get_post_revision( $post ); + if ( ! $post ) { return false; } - if ( false !== strpos( $post->post_name, "{$post->post_parent}-autosave" ) ) { + if ( str_contains( $post->post_name, "{$post->post_parent}-autosave" ) ) { return (int) $post->post_parent; } @@ -310,7 +355,8 @@ * @access private * * @param int|WP_Post|array|null $post Post ID, post object OR post array. - * @param bool $autosave Optional. Is the revision an autosave? + * @param bool $autosave Optional. Whether the revision is an autosave or not. + * Default false. * @return int|WP_Error WP_Error or 0 if error, new revision ID if success. */ function _wp_put_post_revision( $post = null, $autosave = false ) { @@ -341,32 +387,58 @@ * Fires once a revision has been saved. * * @since 2.6.0 + * @since 6.4.0 The post_id parameter was added. * * @param int $revision_id Post revision ID. + * @param int $post_id Post ID. */ - do_action( '_wp_put_post_revision', $revision_id ); + do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] ); } return $revision_id; } + +/** + * Save the revisioned meta fields. + * + * @since 6.4.0 + * + * @param int $revision_id The ID of the revision to save the meta to. + * @param int $post_id The ID of the post the revision is associated with. + */ +function wp_save_revisioned_meta_fields( $revision_id, $post_id ) { + $post_type = get_post_type( $post_id ); + if ( ! $post_type ) { + return; + } + + foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { + if ( metadata_exists( 'post', $post_id, $meta_key ) ) { + _wp_copy_post_meta( $post_id, $revision_id, $meta_key ); + } + } +} + /** * Gets a post revision. * * @since 2.6.0 * - * @param int|WP_Post $post The post ID or object. + * @param int|WP_Post $post Post ID or post object. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which * correspond to a WP_Post object, an associative array, or a numeric array, * respectively. Default OBJECT. - * @param string $filter Optional sanitation filter. See sanitize_post(). + * @param string $filter Optional sanitization filter. See sanitize_post(). Default 'raw'. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. */ function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { $revision = get_post( $post, OBJECT, $filter ); + if ( ! $revision ) { return $revision; } + if ( 'revision' !== $revision->post_type ) { return null; } @@ -391,12 +463,13 @@ * * @since 2.6.0 * - * @param int|WP_Post $revision_id Revision ID or revision object. - * @param array $fields Optional. What fields to restore from. Defaults to all. + * @param int|WP_Post $revision Revision ID or revision object. + * @param array $fields Optional. What fields to restore from. Defaults to all. * @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. */ -function wp_restore_post_revision( $revision_id, $fields = null ) { - $revision = wp_get_post_revision( $revision_id, ARRAY_A ); +function wp_restore_post_revision( $revision, $fields = null ) { + $revision = wp_get_post_revision( $revision, ARRAY_A ); + if ( ! $revision ) { return $revision; } @@ -419,6 +492,7 @@ $update = wp_slash( $update ); // Since data is from DB. $post_id = wp_update_post( $update ); + if ( ! $post_id || is_wp_error( $post_id ) ) { return $post_id; } @@ -440,22 +514,123 @@ } /** + * Restore the revisioned meta values for a post. + * + * @since 6.4.0 + * + * @param int $post_id The ID of the post to restore the meta to. + * @param int $revision_id The ID of the revision to restore the meta from. + */ +function wp_restore_post_revision_meta( $post_id, $revision_id ) { + $post_type = get_post_type( $post_id ); + if ( ! $post_type ) { + return; + } + + // Restore revisioned meta fields. + foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { + + // Clear any existing meta. + delete_post_meta( $post_id, $meta_key ); + + _wp_copy_post_meta( $revision_id, $post_id, $meta_key ); + } +} + +/** + * Copy post meta for the given key from one post to another. + * + * @since 6.4.0 + * + * @param int $source_post_id Post ID to copy meta value(s) from. + * @param int $target_post_id Post ID to copy meta value(s) to. + * @param string $meta_key Meta key to copy. + */ +function _wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key ) { + + foreach ( get_post_meta( $source_post_id, $meta_key ) as $meta_value ) { + /** + * We use add_metadata() function vs add_post_meta() here + * to allow for a revision post target OR regular post. + */ + add_metadata( 'post', $target_post_id, $meta_key, wp_slash( $meta_value ) ); + } +} + +/** + * Determine which post meta fields should be revisioned. + * + * @since 6.4.0 + * + * @param string $post_type The post type being revisioned. + * @return array An array of meta keys to be revisioned. + */ +function wp_post_revision_meta_keys( $post_type ) { + $registered_meta = array_merge( + get_registered_meta_keys( 'post' ), + get_registered_meta_keys( 'post', $post_type ) + ); + + $wp_revisioned_meta_keys = array(); + + foreach ( $registered_meta as $name => $args ) { + if ( $args['revisions_enabled'] ) { + $wp_revisioned_meta_keys[ $name ] = true; + } + } + + $wp_revisioned_meta_keys = array_keys( $wp_revisioned_meta_keys ); + + /** + * Filter the list of post meta keys to be revisioned. + * + * @since 6.4.0 + * + * @param array $keys An array of meta fields to be revisioned. + * @param string $post_type The post type being revisioned. + */ + return apply_filters( 'wp_post_revision_meta_keys', $wp_revisioned_meta_keys, $post_type ); +} + +/** + * Check whether revisioned post meta fields have changed. + * + * @since 6.4.0 + * + * @param bool $post_has_changed Whether the post has changed. + * @param WP_Post $last_revision The last revision post object. + * @param WP_Post $post The post object. + * @return bool Whether the post has changed. + */ +function wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) { + foreach ( wp_post_revision_meta_keys( $post->post_type ) as $meta_key ) { + if ( get_post_meta( $post->ID, $meta_key ) !== get_post_meta( $last_revision->ID, $meta_key ) ) { + $post_has_changed = true; + break; + } + } + return $post_has_changed; +} + +/** * Deletes a revision. * * Deletes the row from the posts table corresponding to the specified revision. * * @since 2.6.0 * - * @param int|WP_Post $revision_id Revision ID or revision object. + * @param int|WP_Post $revision Revision ID or revision object. * @return WP_Post|false|null Null or false if error, deleted post object if success. */ -function wp_delete_post_revision( $revision_id ) { - $revision = wp_get_post_revision( $revision_id ); +function wp_delete_post_revision( $revision ) { + $revision = wp_get_post_revision( $revision ); + if ( ! $revision ) { return $revision; } $delete = wp_delete_post( $revision->ID ); + if ( $delete ) { /** * Fires once a post revision has been deleted. @@ -478,12 +653,13 @@ * * @see get_children() * - * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`. - * @param array|null $args Optional. Arguments for retrieving post revisions. Default null. - * @return array An array of revisions, or an empty array if none. + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. + * @param array|null $args Optional. Arguments for retrieving post revisions. Default null. + * @return WP_Post[]|int[] Array of revision objects or IDs, or an empty array if none. */ -function wp_get_post_revisions( $post_id = 0, $args = null ) { - $post = get_post( $post_id ); +function wp_get_post_revisions( $post = 0, $args = null ) { + $post = get_post( $post ); + if ( ! $post || empty( $post->ID ) ) { return array(); } @@ -509,6 +685,7 @@ ); $revisions = get_children( $args ); + if ( ! $revisions ) { return array(); } @@ -517,15 +694,67 @@ } /** + * Returns the latest revision ID and count of revisions for a post. + * + * @since 6.1.0 + * + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. + * @return array|WP_Error { + * Returns associative array with latest revision ID and total count, + * or a WP_Error if the post does not exist or revisions are not enabled. + * + * @type int $latest_id The latest revision post ID or 0 if no revisions exist. + * @type int $count The total count of revisions for the given post. + * } + */ +function wp_get_latest_revision_id_and_total_count( $post = 0 ) { + $post = get_post( $post ); + + if ( ! $post ) { + return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); + } + + if ( ! wp_revisions_enabled( $post ) ) { + return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) ); + } + + $args = array( + 'post_parent' => $post->ID, + 'fields' => 'ids', + 'post_type' => 'revision', + 'post_status' => 'inherit', + 'order' => 'DESC', + 'orderby' => 'date ID', + 'posts_per_page' => 1, + 'ignore_sticky_posts' => true, + ); + + $revision_query = new WP_Query(); + $revisions = $revision_query->query( $args ); + + if ( ! $revisions ) { + return array( + 'latest_id' => 0, + 'count' => 0, + ); + } + + return array( + 'latest_id' => $revisions[0], + 'count' => $revision_query->found_posts, + ); +} + +/** * Returns the url for viewing and potentially restoring revisions of a given post. * * @since 5.9.0 * - * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`. - * @return null|string The URL for editing revisions on the given post, otherwise null. + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. + * @return string|null The URL for editing revisions on the given post, otherwise null. */ -function wp_get_post_revisions_url( $post_id = 0 ) { - $post = get_post( $post_id ); +function wp_get_post_revisions_url( $post = 0 ) { + $post = get_post( $post ); if ( ! $post instanceof WP_Post ) { return null; @@ -540,18 +769,17 @@ return null; } - $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) ); + $revisions = wp_get_latest_revision_id_and_total_count( $post->ID ); - if ( 0 === count( $revisions ) ) { + if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) { return null; } - $revision = reset( $revisions ); - return get_edit_post_link( $revision ); + return get_edit_post_link( $revisions['latest_id'] ); } /** - * Determine if revisions are enabled for a given post. + * Determines whether revisions are enabled for a given post. * * @since 3.6.0 * @@ -563,7 +791,7 @@ } /** - * Determine how many revisions to retain for a given post. + * Determines how many revisions to retain for a given post. * * By default, an infinite number of revisions are kept. * @@ -649,6 +877,7 @@ add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); + add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); return $post; } @@ -684,11 +913,12 @@ */ function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { $post = get_post(); + if ( ! $post ) { return $terms; } - if ( empty( $_REQUEST['post_format'] ) || $post->ID != $post_id + if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id || 'post_format' !== $taxonomy || 'revision' === $post->post_type ) { return $terms; @@ -698,6 +928,7 @@ $terms = array(); } else { $term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ); + if ( $term ) { $terms = array( $term ); // Can only have one post format. } @@ -719,21 +950,20 @@ */ function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { $post = get_post(); + if ( ! $post ) { return $value; } - if ( empty( $_REQUEST['_thumbnail_id'] ) || - empty( $_REQUEST['preview_id'] ) || - $post->ID != $post_id || - '_thumbnail_id' !== $meta_key || - 'revision' === $post->post_type || - $post_id != $_REQUEST['preview_id'] ) { - + if ( empty( $_REQUEST['_thumbnail_id'] ) || empty( $_REQUEST['preview_id'] ) + || $post->ID !== $post_id || $post_id !== (int) $_REQUEST['preview_id'] + || '_thumbnail_id' !== $meta_key || 'revision' === $post->post_type + ) { return $value; } $thumbnail_id = (int) $_REQUEST['_thumbnail_id']; + if ( $thumbnail_id <= 0 ) { return ''; } @@ -765,16 +995,16 @@ } /** - * Upgrade the revisions author, add the current post as a revision and set the revisions version to 1 + * Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1. * * @since 3.6.0 * @access private * * @global wpdb $wpdb WordPress database abstraction object. * - * @param WP_Post $post Post object - * @param array $revisions Current revisions of the post - * @return bool true if the revisions were upgraded, false if problems + * @param WP_Post $post Post object. + * @param array $revisions Current revisions of the post. + * @return bool true if the revisions were upgraded, false if problems. */ function _wp_upgrade_revisions_of_post( $post, $revisions ) { global $wpdb; @@ -782,17 +1012,21 @@ // Add post option exclusively. $lock = "revision-upgrade-{$post->ID}"; $now = time(); - $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) ); + $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'off') /* LOCK */", $lock, $now ) ); + if ( ! $result ) { // If we couldn't get a lock, see how old the previous lock is. $locked = get_option( $lock ); + if ( ! $locked ) { - // Can't write to the lock, and can't read the lock. - // Something broken has happened. + /* + * Can't write to the lock, and can't read the lock. + * Something broken has happened. + */ return false; } - if ( $locked > $now - 3600 ) { + if ( $locked > $now - HOUR_IN_SECONDS ) { // Lock is not too old: some other process may be upgrading this post. Bail. return false; } @@ -817,8 +1051,10 @@ continue; } - // 1 is the latest revision version, so we're already up to date. - // No need to add a copy of the post as latest revision. + /* + * 1 is the latest revision version, so we're already up to date. + * No need to add a copy of the post as latest revision. + */ if ( 0 < $this_revision_version ) { $add_last = false; continue; @@ -860,3 +1096,38 @@ return true; } + +/** + * Filters preview post meta retrieval to get values from the autosave. + * + * Filters revisioned meta keys only. + * + * @since 6.4.0 + * + * @param mixed $value Meta value to filter. + * @param int $object_id Object ID. + * @param string $meta_key Meta key to filter a value for. + * @param bool $single Whether to return a single value. Default false. + * @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist, + * the post type is a revision or the post ID doesn't match the object ID. + * Otherwise, the revisioned meta value is returned for the preview. + */ +function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { + + $post = get_post(); + if ( + empty( $post ) || + $post->ID !== $object_id || + ! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) || + 'revision' === $post->post_type + ) { + return $value; + } + + $preview = wp_get_post_autosave( $post->ID ); + if ( false === $preview ) { + return $value; + } + + return get_post_meta( $preview->ID, $meta_key, $single ); +}