_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。

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