Customize: Keep alive auto-drafts created for page/post stubs when parent changeset is updated, and delete when changeset is garbage-collected.

Props utkarshpatel, westonruter.
See #31089.
Fixes #39715.


git-svn-id: https://develop.svn.wordpress.org/trunk@40676 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2017-05-16 05:36:25 +00:00
parent 8eade12087
commit 77fe870bd5
5 changed files with 154 additions and 0 deletions

View File

@@ -3059,3 +3059,65 @@ function is_customize_preview() {
return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
}
/**
* 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.
*
* @since 4.8.0
* @access private
* @see wp_delete_auto_drafts()
*
* @param string $new_status Transition to this post status.
* @param string $old_status Previous post status.
* @param \WP_Post $post Post data.
* @global wpdb $wpdb
*/
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 );
}
}