源代碼
File: wp-includes/revision.php
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, 'no') /* 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 - 3600 ) {
// 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;
}
更新日志
Version | 描述 |
---|---|
3.6.0 | Introduced. |
在WordPress中,wp_upgrade_revisions_of_post()
函數(shù)用于將舊版本的帖子修訂版本升級到新版本的帖子修訂系統(tǒng)。這個函數(shù)通常在插件或主題的代碼中使用,或者在WordPress核心更新過程中用于處理修訂版本的數(shù)據(jù)遷移。
以下是 wp_upgrade_revisions_of_post()
函數(shù)的基本用法:
wp_upgrade_revisions_of_post( $post_id );
參數(shù)解釋如下:
$post_id
:要升級修訂版本的帖子ID。
使用wp_upgrade_revisions_of_post()
的步驟:
- 確定帖子ID:首先,你需要確定你想要升級修訂版本的帖子的ID。
- 調(diào)用函數(shù):使用
wp_upgrade_revisions_of_post()
函數(shù),并傳入相應(yīng)的帖子ID。
下面是一個使用wp_upgrade_revisions_of_post()
函數(shù)的例子:
<?php
// 假設(shè)我們有一個特定的帖子ID
$post_id = 123;
// 升級該帖子的修訂版本
wp_upgrade_revisions_of_post( $post_id );
?>
在這個例子中,我們假設(shè)有一個帖子ID為123,然后我們調(diào)用 wp_upgrade_revisions_of_post()
函數(shù)來升級這個帖子的修訂版本。
需要注意的是,這個函數(shù)主要用于WordPress內(nèi)部的數(shù)據(jù)遷移和升級過程。通常,開發(fā)者或網(wǎng)站管理員不需要直接使用這個函數(shù),因為WordPress會自動處理帖子修訂版本的升級。
此外,wp_upgrade_revisions_of_post()
函數(shù)可能在未來的WordPress版本中被棄用或更改,因為它涉及到WordPress內(nèi)部的數(shù)據(jù)處理。因此,除非你明確知道自己在做什么,并且有充分的理由需要手動處理帖子修訂版本的升級,否則建議不要在常規(guī)的插件或主題開發(fā)中使用這個函數(shù)。如果你確實需要使用它,請確保對WordPress核心有深入的了解,并且已經(jīng)對數(shù)據(jù)進(jìn)行備份,以防萬一。
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress函數(shù)_wp_upgrade_revisions_of_post()用法