返回介绍

_wp_keep_alive_customize_changeset_dependent_auto_drafts()

发布于 2017-09-11 13:45:17 字数 3389 浏览 864 评论 0 收藏 0

Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness. Use wp_delete_auto_drafts() instead.

_wp_keep_alive_customize_changeset_dependent_auto_drafts( string $new_status,  string $old_status,  WP_Post $post )

Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.


description

When a changeset is updated but remains an auto-draft, ensure the post_date for the auto-draft posts remains the same so that it will be garbage-collected at the same time by wp_delete_auto_drafts(). Otherwise, if the changeset is updated to be a draft then update the posts to have a far-future post_date so that they will never be garbage collected unless the changeset post itself is deleted.


参数

$new_status

(string) (Required) Transition to this post status.

$old_status

(string) (Required) Previous post status.

$post

(WP_Post) (Required) Post data.


源代码

File: wp-includes/theme.php

function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) {
	global $wpdb;
	unset( $old_status );

	// Short-circuit if not a changeset or if the changeset was published.
	if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
		return;
	}

	if ( 'auto-draft' === $new_status ) {
		/*
		 * Keep the post date for the post matching the changeset
		 * so that it will not be garbage-collected before the changeset.
		 */
		$new_post_date = $post->post_date;
	} else {
		/*
		 * Since the changeset no longer has an auto-draft (and it is not published)
		 * it is now a persistent changeset, a long-lived draft, and so any
		 * associated auto-draft posts should have their dates
		 * pushed out very far into the future to prevent them from ever
		 * being garbage-collected.
		 */
		$new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
	}

	$data = json_decode( $post->post_content, true );
	if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
		return;
	}
	foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
		if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
			continue;
		}
		$wpdb->update(
			$wpdb->posts,
			array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this this date.
			array( 'ID' => $post_id )
		);
		clean_post_cache( $post_id );
	}
}

更新日志

Versiondescription
4.8.0Introduced.

相关函数

Uses

  • wp-includes/compat.php: json_decode()
  • wp-includes/post.php: clean_post_cache()
  • wp-includes/post.php: get_post_status()
  • wp-includes/wp-db.php: wpdb::update()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文