_wp_upgrade_revisions_of_post

函式
_wp_upgrade_revisions_of_post ( $post, $revisions )
Access
Private
引數
  • (WP_Post) $post Post object.
    Required:
  • (array) $revisions Current revisions of the post.
    Required:
返回值
  • (bool) true if the revisions were upgraded, false if problems.
定義位置
相關方法
wp_prepare_revisions_for_js_wp_post_revision_fields_wp_post_revision_datawp_get_revision_ui_diffwp_ajax_get_revision_diffs
引入
3.6.0
棄用
-

_wp_upgrade_revisions_of_post: 這個函式用於將一個文章的修訂版升級到最新的格式。每當一個文章被更新時,它就被呼叫,並確保文章的修訂版以正確的格式儲存在資料庫中。

升級修訂的作者,將當前的文章新增為修訂,並將修訂的版本設定為1。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >