A WordPress-centric search engine for devs and theme authors



_wp_keep_alive_customize_changeset_dependent_auto_drafts ›

Since4.8.0
Deprecatedn/a
_wp_keep_alive_customize_changeset_dependent_auto_drafts ( $new_status, $old_status, $post )
Access:
  • private
Parameters: (3)
  • (string) $new_status Transition to this post status.
    Required: Yes
  • (string) $old_status Previous post status.
    Required: Yes
  • (WP_Post) $post Post data.
    Required: Yes
See:
Defined at:
Codex:

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

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.



Source

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