Make WordPress Core


Ignore:
Timestamp:
05/16/2017 05:36:25 AM (9 years ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme.php

    r40300 r40676  
    30603060        return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
    30613061}
     3062
     3063/**
     3064 * Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.
     3065 *
     3066 * When a changeset is updated but remains an auto-draft, ensure the post_date
     3067 * for the auto-draft posts remains the same so that it will be
     3068 * garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise,
     3069 * if the changeset is updated to be a draft then update the posts
     3070 * to have a far-future post_date so that they will never be garbage collected
     3071 * unless the changeset post itself is deleted.
     3072 *
     3073 * @since 4.8.0
     3074 * @access private
     3075 * @see wp_delete_auto_drafts()
     3076 *
     3077 * @param string   $new_status Transition to this post status.
     3078 * @param string   $old_status Previous post status.
     3079 * @param \WP_Post $post       Post data.
     3080 * @global wpdb $wpdb
     3081 */
     3082function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) {
     3083        global $wpdb;
     3084        unset( $old_status );
     3085
     3086        // Short-circuit if not a changeset or if the changeset was published.
     3087        if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
     3088                return;
     3089        }
     3090
     3091        if ( 'auto-draft' === $new_status ) {
     3092                /*
     3093                 * Keep the post date for the post matching the changeset
     3094                 * so that it will not be garbage-collected before the changeset.
     3095                 */
     3096                $new_post_date = $post->post_date;
     3097        } else {
     3098                /*
     3099                 * Since the changeset no longer has an auto-draft (and it is not published)
     3100                 * it is now a persistent changeset, a long-lived draft, and so any
     3101                 * associated auto-draft posts should have their dates
     3102                 * pushed out very far into the future to prevent them from ever
     3103                 * being garbage-collected.
     3104                 */
     3105                $new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
     3106        }
     3107
     3108        $data = json_decode( $post->post_content, true );
     3109        if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
     3110                return;
     3111        }
     3112        foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
     3113                if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
     3114                        continue;
     3115                }
     3116                $wpdb->update(
     3117                        $wpdb->posts,
     3118                        array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this this date.
     3119                        array( 'ID' => $post_id )
     3120                );
     3121                clean_post_cache( $post_id );
     3122        }
     3123}
Note: See TracChangeset for help on using the changeset viewer.