diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index ef412e9e6a..84ad9cf340 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -325,6 +325,7 @@ add_action( 'welcome_panel', 'wp_welcome_panel' add_action( 'delete_post', '_wp_delete_post_menu_item' ); add_action( 'delete_term', '_wp_delete_tax_menu_item', 10, 3 ); add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 ); +add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); // Post Thumbnail CSS class filtering add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' ); @@ -400,6 +401,7 @@ add_action( 'plugins_loaded', '_wp_customize_include' ); add_action( 'transition_post_status', '_wp_customize_publish_changeset', 10, 3 ); add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' ); add_action( 'delete_attachment', '_delete_attachment_theme_mod' ); +add_action( 'transition_post_status', '_wp_keep_alive_customize_changeset_dependent_auto_drafts', 20, 3 ); // Calendar widget cache add_action( 'save_post', 'delete_get_calendar_cache' ); diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 0710055c79..2177d6af57 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -978,3 +978,31 @@ function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { wp_update_nav_menu_item( $menu_id, 0, $args ); } } + +/** + * Delete auto-draft posts associated with the supplied changeset. + * + * @since 4.8.0 + * @access private + * + * @param int $post_id Post ID for the customize_changeset. + */ +function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) { + $post = get_post( $post_id ); + + if ( ! $post || 'customize_changeset' !== $post->post_type ) { + return; + } + + $data = json_decode( $post->post_content, true ); + if ( empty( $data['nav_menus_created_posts']['value'] ) ) { + return; + } + remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); + foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) { + if ( ! empty( $post_id ) && 'auto-draft' === get_post_status( $post_id ) ) { + wp_delete_post( $post_id, true ); + } + } + add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' ); +} diff --git a/wp-includes/theme.php b/wp-includes/theme.php index fdf5a34446..e5cdd952b8 100644 --- a/wp-includes/theme.php +++ b/wp-includes/theme.php @@ -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 ); + } +} diff --git a/wp-includes/version.php b/wp-includes/version.php index acc20fd9b9..2614f176c1 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.8-beta1-40675'; +$wp_version = '4.8-beta1-40676'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.