: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
update_post_meta( $post_id, '_edit_last', get_current_user_id() );
* Fires after a post revision has been restored.
* @param int $post_id Post ID.
* @param int $revision_id Post revision ID.
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );
* Restore the revisioned meta values for a post.
* @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 );
// 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.
* @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.
* @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.
* @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.
* @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;
return $post_has_changed;
* Deletes the row from the posts table corresponding to the specified revision.
* @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 ) {
$revision = wp_get_post_revision( $revision );
$delete = wp_delete_post( $revision->ID );
* Fires once a post revision has been deleted.
* @param int $revision_id Post revision ID.
* @param WP_Post $revision Post revision object.
do_action( 'wp_delete_post_revision', $revision->ID, $revision );
* Returns all revisions of specified post.
* @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 = 0, $args = null ) {
$post = get_post( $post );
if ( ! $post || empty( $post->ID ) ) {
$args = wp_parse_args( $args, $defaults );
if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) {
'post_parent' => $post->ID,
'post_type' => 'revision',
'post_status' => 'inherit',
$revisions = get_children( $args );
* Returns the latest revision ID and count of revisions for a post.
* @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 );
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) );
if ( ! wp_revisions_enabled( $post ) ) {
return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) );
'post_parent' => $post->ID,
'post_type' => 'revision',
'post_status' => 'inherit',
'ignore_sticky_posts' => true,
$revision_query = new WP_Query();
$revisions = $revision_query->query( $args );
'latest_id' => $revisions[0],
'count' => $revision_query->found_posts,
* Returns the url for viewing and potentially restoring revisions of a given post.
* @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 = 0 ) {
$post = get_post( $post );
if ( ! $post instanceof WP_Post ) {
// If the post is a revision, return early.
if ( 'revision' === $post->post_type ) {
return get_edit_post_link( $post );
if ( ! wp_revisions_enabled( $post ) ) {
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID );
if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) {
return get_edit_post_link( $revisions['latest_id'] );
* Determines whether revisions are enabled for a given post.
* @param WP_Post $post The post object.
* @return bool True if number of revisions to keep isn't zero, false otherwise.
function wp_revisions_enabled( $post ) {
return wp_revisions_to_keep( $post ) !== 0;
* Determines how many revisions to retain for a given post.
* By default, an infinite number of revisions are kept.
* The constant WP_POST_REVISIONS can be set in wp-config to specify the limit
* @param WP_Post $post The post object.
* @return int The number of revisions to keep.
function wp_revisions_to_keep( $post ) {
$num = WP_POST_REVISIONS;
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) {
* Filters the number of revisions to save for the given post.
* Overrides the value of WP_POST_REVISIONS.
* @param int $num Number of revisions to store.
* @param WP_Post $post Post object.
$num = apply_filters( 'wp_revisions_to_keep', $num, $post );
* Filters the number of revisions to save for the given post by its post type.
* Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter.
* The dynamic portion of the hook name, `$post->post_type`, refers to
* Possible hook names include:
* - `wp_post_revisions_to_keep`
* - `wp_page_revisions_to_keep`
* @param int $num Number of revisions to store.
* @param WP_Post $post Post object.
$num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post );
* Sets up the post object for preview based on the post autosave.
function _set_preview( $post ) {
if ( ! is_object( $post ) ) {
$preview = wp_get_post_autosave( $post->ID );
if ( is_object( $preview ) ) {
$preview = sanitize_post( $preview );
$post->post_content = $preview->post_content;
$post->post_title = $preview->post_title;
$post->post_excerpt = $preview->post_excerpt;
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 );
* Filters the latest content for preview from the post autosave.
function _show_post_preview() {
if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) {
$id = (int) $_GET['preview_id'];
if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) {
wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 );
add_filter( 'the_preview', '_set_preview' );
* Filters terms lookup to set the post format.
* @param string $taxonomy
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) {
if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id
|| 'post_format' !== $taxonomy || 'revision' === $post->post_type
if ( 'standard' === $_REQUEST['post_format'] ) {
$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' );
$terms = array( $term ); // Can only have one post format.
* Filters post thumbnail lookup to set the post thumbnail.
* @param null|array|string $value The value to return - a single metadata value, or an array of values.
* @param int $post_id Post ID.
* @param string $meta_key Meta key.
* @return null|array The default return value or the post thumbnail meta array.
function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) {
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
$thumbnail_id = (int) $_REQUEST['_thumbnail_id'];
if ( $thumbnail_id <= 0 ) {
return (string) $thumbnail_id;
* Gets the post revision version.
* @param WP_Post $revision
function _wp_get_post_revision_version( $revision ) {
if ( is_object( $revision ) ) {
$revision = get_object_vars( $revision );
} elseif ( ! is_array( $revision ) ) {
if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) {
return (int) $matches[1];
* Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1.