函数文档

_wp_upgrade_revisions_of_post()

💡 云策文档标注

概述

_wp_upgrade_revisions_of_post() 函数用于升级文章修订版本,包括更新修订作者、将当前文章添加为修订版本,并设置修订版本号为 1。它通过数据库操作和锁机制确保升级过程的安全性和一致性。

关键要点

  • 函数参数:接受 $post(WP_Post 对象)和 $revisions(数组)作为必需参数,返回布尔值表示升级成功与否。
  • 锁机制:使用数据库选项实现锁,防止并发升级冲突,锁超时时间为 1 小时。
  • 修订版本处理:遍历修订版本,更新版本号为 1,并调整 post_name 格式(如添加 '-v1' 后缀)。
  • 作者更新:基于前一修订版本推断并更新作者信息,若无法推断则保留原作者。
  • 缓存清理:升级后使用 wp_cache_delete() 清理缓存,确保数据一致性。
  • 最终操作:升级完成后删除锁,并调用 wp_save_post_revision() 将当前文章添加为最新修订版本。

代码示例

function _wp_upgrade_revisions_of_post( $post, $revisions ) {
    global $wpdb;
    // 添加锁选项
    $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, 'off') /* LOCK */", $lock, $now ) );
    // 锁处理逻辑
    if ( ! $result ) {
        $locked = get_option( $lock );
        if ( ! $locked ) {
            return false;
        }
        if ( $locked > $now - HOUR_IN_SECONDS ) {
            return false;
        }
    }
    update_option( $lock, $now );
    reset( $revisions );
    $add_last = true;
    do {
        $this_revision = current( $revisions );
        $prev_revision = next( $revisions );
        $this_revision_version = _wp_get_post_revision_version( $this_revision );
        if ( false === $this_revision_version ) {
            continue;
        }
        if ( 0 < $this_revision_version ) {
            $add_last = false;
        }
        $update = array(
            'post_author' => $this_revision->post_author,
            'post_name' => preg_replace( '/^(d+-(?:autosave|revision))[d-]*$/', '$1-v1', $this_revision->post_name ),
        );
        if ( $prev_revision ) {
            $prev_revision_version = _wp_get_post_revision_version( $prev_revision );
            if ( $prev_revision_version < 1 ) {
                $update['post_author'] = $prev_revision->post_author;
            }
        }
        $result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
        if ( $result ) {
            wp_cache_delete( $this_revision->ID, 'posts' );
        }
    } while ( $prev_revision );
    delete_option( $lock );
    if ( $add_last ) {
        wp_save_post_revision( $post->ID );
    }
    return true;
}

注意事项

  • 函数内部依赖全局 $wpdb 对象进行数据库操作,需确保数据库连接正常。
  • 锁机制基于选项表,可能在高并发环境下影响性能,建议在低流量时执行升级。
  • 修订版本号通过 _wp_get_post_revision_version() 获取,若返回 false 则跳过处理。
  • 函数在 WordPress 3.6.0 版本引入,主要用于 edit_post() 等内部函数调用。

📄 原文内容

Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1.

Parameters

$postWP_Postrequired
Post object.
$revisionsarrayrequired
Current revisions of the post.

Return

bool true if the revisions were upgraded, false if problems.

Source

function _wp_upgrade_revisions_of_post( $post, $revisions ) {
	global $wpdb;

	// 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, '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.
			 */
			return false;
		}

		if ( $locked > $now - HOUR_IN_SECONDS ) {
			// Lock is not too old: some other process may be upgrading this post. Bail.
			return false;
		}

		// Lock is too old - update it (below) and continue.
	}

	// If we could get a lock, re-"add" the option to fire all the correct filters.
	update_option( $lock, $now );

	reset( $revisions );
	$add_last = true;

	do {
		$this_revision = current( $revisions );
		$prev_revision = next( $revisions );

		$this_revision_version = _wp_get_post_revision_version( $this_revision );

		// Something terrible happened.
		if ( false === $this_revision_version ) {
			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.
		 */
		if ( 0 < $this_revision_version ) {
			$add_last = false;
			continue;
		}

		// Always update the revision version.
		$update = array(
			'post_name' => preg_replace( '/^(d+-(?:autosave|revision))[d-]*$/', '$1-v1', $this_revision->post_name ),
		);

		/*
		 * If this revision is the oldest revision of the post, i.e. no $prev_revision,
		 * the correct post_author is probably $post->post_author, but that's only a good guess.
		 * Update the revision version only and Leave the author as-is.
		 */
		if ( $prev_revision ) {
			$prev_revision_version = _wp_get_post_revision_version( $prev_revision );

			// If the previous revision is already up to date, it no longer has the information we need :(
			if ( $prev_revision_version < 1 ) {
				$update['post_author'] = $prev_revision->post_author;
			}
		}

		// Upgrade this revision.
		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );

		if ( $result ) {
			wp_cache_delete( $this_revision->ID, 'posts' );
		}
	} while ( $prev_revision );

	delete_option( $lock );

	// Add a copy of the post as latest revision.
	if ( $add_last ) {
		wp_save_post_revision( $post->ID );
	}

	return true;
}

Changelog

Version Description
3.6.0 Introduced.