_wp_keep_alive_customize_changeset_dependent_auto_drafts

函数
_wp_keep_alive_customize_changeset_dependent_auto_drafts ( $new_status, $old_status, $post )
Access
Private
参数
  • (string) $new_status Transition to this post status.
    Required:
  • (string) $old_status Previous post status.
    Required:
  • (WP_Post) $post Post data.
    Required:
相关
  • wp_delete_auto_drafts()
定义位置
相关方法
_wp_delete_customize_changeset_dependent_auto_drafts_wp_customize_changeset_filter_insert_post_data_wp_customize_publish_changesetcustomize_themes_print_templates
引入
4.8.0
弃用
-

_wp_keep_alive_customize_changeset_dependent_auto_drafts: 调用此函数是为了在自定义器更改期间保持依赖变化集的自动草稿的活力。

确保自动起草的文章得到他们的post_date bumped或状态改变为草稿,以防止过早的垃圾收集。

当一个变化集被更新但仍然是自动草稿时,确保自动草稿的文章的post_date保持不变,这样它就会被
在同一时间被`wp_delete_auto_drafts()`垃圾收集。否则,如果改变集被更新为草稿,那么更新文章
有一个远期的post_date,这样它们就不会被垃圾收集,除非改变集的文章本身被删除。

当一个变化集被更新为一个持久的草稿或被计划发布时,然后将任何依赖的自动草稿过渡到草稿状态,以便它们同样不会被垃圾收集,但也可以在发布前在管理员中编辑它们,因为在自定义器中还没有一个文章/页面编辑流程。参见#39752。

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;
	}

	$data = json_decode( $post->post_content, true );
	if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
		return;
	}

	/*
	 * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is
	 * getting trashed. This is needed because when a changeset transitions to a draft, then any of the
	 * dependent auto-draft post/page stubs will also get transitioned to customization drafts which
	 * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which
	 * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be
	 * trashed to remove from visibility immediately.
	 */
	if ( 'trash' === $new_status ) {
		foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
			if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
				wp_trash_post( $post_id );
			}
		}
		return;
	}

	$post_args = array();
	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.
		 */
		$post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this 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 likewise transition into having a draft
		 * status. These drafts will be treated differently than regular drafts in
		 * that they will be tied to the given changeset. The publish meta box is
		 * replaced with a notice about how the post is part of a set of customized changes
		 * which will be published when the changeset is published.
		 */
		$post_args['post_status'] = 'draft';
	}

	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,
			$post_args,
			array( 'ID' => $post_id )
		);
		clean_post_cache( $post_id );
	}
}

常见问题

FAQs
查看更多 >