Make WordPress Core

Changeset 41887


Ignore:
Timestamp:
10/17/2017 08:14:56 PM (9 years ago)
Author:
westonruter
Message:

Customize: Allow post/page stubs to be edited in WP Admin as "customization drafts" when changeset is saved as draft or scheduled.

  • Update stubs to have draft status when changeset is saved as draft, instead of preventing auto-draft garbage collection by giving them a far-future post_date.
  • Show notice in publish metabox when editing a customization draft indicating that it will be published automatically with its changeset; a link to Customizer is included.
  • Include a new "Customization Draft" display post state in the post list table.
  • Disconnect stubs from their changesets when they are updated with a status other than "Draft".
  • Trash customization drafts when their related changeset is trashed or deleted.
  • Add a _customize_changeset_uuid postmeta to stubs to link them with their associated changeset.
  • Include customize_changeset_uuid as context when requesting to insert a new auto-draft.

Props westonruter, melchoyce.
See #39896, #39752, #34923.
Fixes #42220.

Location:
trunk
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/css/common.css

    r41882 r41887  
    965965}
    966966
     967#misc-publishing-actions .notice {
     968        margin-left: 10px;
     969        margin-right: 10px;
     970}
     971
    967972/* Filter bar */
    968973.wp-filter {
  • trunk/src/wp-admin/includes/meta-boxes.php

    r41763 r41887  
    222222        </fieldset>
    223223</div><?php // /misc-pub-section ?>
     224<?php endif; ?>
     225
     226<?php if ( 'draft' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) : ?>
     227        <div class="notice notice-info notice-alt inline">
     228                <p>
     229                        <?php
     230                        echo sprintf(
     231                                /* translators: %s is the URL to the Customizer */
     232                                __( 'This draft comes from your <a href="%s">unpublished customization changes</a>. You can edit, but there&#8217;s no need to publish now. It will be published automatically with those changes.' ),
     233                                esc_url(
     234                                        add_query_arg(
     235                                                'changeset_uuid',
     236                                                rawurlencode( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ),
     237                                                admin_url( 'customize.php' )
     238                                        )
     239                                )
     240                        );
     241                        ?>
     242                </p>
     243        </div>
    224244<?php endif; ?>
    225245
  • trunk/src/wp-admin/includes/template.php

    r41684 r41887  
    17371737        if ( 'private' == $post->post_status && 'private' != $post_status )
    17381738                $post_states['private'] = __('Private');
    1739         if ( 'draft' == $post->post_status && 'draft' != $post_status )
    1740                 $post_states['draft'] = __('Draft');
     1739        if ( 'draft' === $post->post_status ) {
     1740                if ( get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
     1741                        $post_states[] = __( 'Customization Draft' );
     1742                } elseif ( 'draft' !== $post_status ) {
     1743                        $post_states['draft'] = __( 'Draft' );
     1744                }
     1745        } elseif ( 'trash' === $post->post_status && get_post_meta( $post->ID, '_customize_changeset_uuid', true ) ) {
     1746                $post_states[] = __( 'Customization Draft' );
     1747        }
    17411748        if ( 'pending' == $post->post_status && 'pending' != $post_status )
    17421749                $post_states['pending'] = _x('Pending', 'post status');
  • trunk/src/wp-admin/js/customize-nav-menus.js

    r41868 r41887  
    9898                        'customize-menus-nonce': api.settings.nonce['customize-menus'],
    9999                        'wp_customize': 'on',
     100                        'customize_changeset_uuid': api.settings.changeset.uuid,
    100101                        'params': params
    101102                } );
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r41868 r41887  
    795795                }
    796796
     797                /*
     798                 * If the changeset is a draft, this will change to draft the next time the changeset
     799                 * is updated; otherwise, auto-draft will persist in autosave revisions, until save.
     800                 */
    797801                $postarr['post_status'] = 'auto-draft';
    798802
     
    805809                }
    806810                $postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
     811                $postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid();
    807812                unset( $postarr['post_name'] );
    808813
     
    11731178
    11741179        /**
    1175          * Sanitize post IDs for auto-draft posts created for nav menu items to be published.
     1180         * Sanitize post IDs for posts created for nav menu items to be published.
    11761181         *
    11771182         * @since 4.7.0
     
    11871192                        }
    11881193                        $post = get_post( $post_id );
    1189                         if ( 'auto-draft' !== $post->post_status ) {
     1194                        if ( 'auto-draft' !== $post->post_status && 'draft' !== $post->post_status ) {
    11901195                                continue;
    11911196                        }
     
    12181223                if ( ! empty( $post_ids ) ) {
    12191224                        foreach ( $post_ids as $post_id ) {
     1225
     1226                                // Prevent overriding the status that a user may have prematurely updated the post to.
     1227                                $current_status = get_post_status( $post_id );
     1228                                if ( 'auto-draft' !== $current_status && 'draft' !== $current_status ) {
     1229                                        continue;
     1230                                }
     1231
    12201232                                $target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
    12211233                                $args = array(
  • trunk/src/wp-includes/nav-menu.php

    r41811 r41887  
    10521052        }
    10531053        remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
    1054         foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
    1055                 if ( ! empty( $post_id ) && 'auto-draft' === get_post_status( $post_id ) ) {
    1056                         wp_delete_post( $post_id, true );
     1054        foreach ( $data['nav_menus_created_posts']['value'] as $stub_post_id ) {
     1055                if ( empty( $stub_post_id ) ) {
     1056                        continue;
     1057                }
     1058                if ( 'auto-draft' === get_post_status( $stub_post_id ) ) {
     1059                        wp_delete_post( $stub_post_id, true );
     1060                } elseif ( 'draft' === get_post_status( $stub_post_id ) ) {
     1061                        wp_trash_post( $stub_post_id );
     1062                        delete_post_meta( $stub_post_id, '_customize_changeset_uuid' );
    10571063                }
    10581064        }
  • trunk/src/wp-includes/theme.php

    r41824 r41887  
    30523052
    30533053/**
    3054  * Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.
     3054 * Make sure that auto-draft posts get their post_date bumped or status changed to draft to prevent premature garbage-collection.
    30553055 *
    30563056 * When a changeset is updated but remains an auto-draft, ensure the post_date
     
    30613061 * unless the changeset post itself is deleted.
    30623062 *
     3063 * When a changeset is updated to be a persistent draft or to be scheduled for
     3064 * publishing, then transition any dependent auto-drafts to a draft status so
     3065 * that they likewise will not be garbage-collected but also so that they can
     3066 * be edited in the admin before publishing since there is not yet a post/page
     3067 * editing flow in the Customizer. See #39752.
     3068 *
     3069 * @link https://core.trac.wordpress.org/ticket/39752
     3070 *
    30633071 * @since 4.8.0
    30643072 * @access private
     
    30793087        }
    30803088
     3089        $data = json_decode( $post->post_content, true );
     3090        if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
     3091                return;
     3092        }
     3093
     3094        /*
     3095         * Actually, in lieu of keeping alive, trash any customization drafts here if the changeset itself is
     3096         * getting trashed. This is needed because when a changeset transitions to a draft, then any of the
     3097         * dependent auto-draft post/page stubs will also get transitioned to customization drafts which
     3098         * are then visible in the WP Admin. We cannot wait for the deletion of the changeset in which
     3099         * _wp_delete_customize_changeset_dependent_auto_drafts() will be called, since they need to be
     3100         * trashed to remove from visibility immediately.
     3101         */
     3102        if ( 'trash' === $new_status ) {
     3103                foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
     3104                        if ( ! empty( $post_id ) && 'draft' === get_post_status( $post_id ) ) {
     3105                                wp_trash_post( $post_id );
     3106                        }
     3107                }
     3108                return;
     3109        }
     3110
     3111        $post_args = array();
    30813112        if ( 'auto-draft' === $new_status ) {
    30823113                /*
     
    30843115                 * so that it will not be garbage-collected before the changeset.
    30853116                 */
    3086                 $new_post_date = $post->post_date;
     3117                $post_args['post_date'] = $post->post_date; // Note wp_delete_auto_drafts() only looks at this date.
    30873118        } else {
    30883119                /*
    30893120                 * Since the changeset no longer has an auto-draft (and it is not published)
    30903121                 * it is now a persistent changeset, a long-lived draft, and so any
    3091                  * associated auto-draft posts should have their dates
    3092                  * pushed out very far into the future to prevent them from ever
    3093                  * being garbage-collected.
     3122                 * associated auto-draft posts should likewise transition into having a draft
     3123                 * status. These drafts will be treated differently than regular drafts in
     3124                 * that they will be tied to the given changeset. The publish metabox is
     3125                 * replaced with a notice about how the post is part of a set of customized changes
     3126                 * which will be published when the changeset is published.
    30943127                 */
    3095                 $new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
    3096         }
    3097 
    3098         $data = json_decode( $post->post_content, true );
    3099         if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
    3100                 return;
    3101         }
     3128                $post_args['post_status'] = 'draft';
     3129        }
     3130
    31023131        foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
    31033132                if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
     
    31063135                $wpdb->update(
    31073136                        $wpdb->posts,
    3108                         array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this date.
     3137                        $post_args,
    31093138                        array( 'ID' => $post_id )
    31103139                );
  • trunk/tests/phpunit/tests/ajax/CustomizeMenus.php

    r39506 r41887  
    604604                $this->assertEquals( '', $post->post_name );
    605605                $this->assertEquals( 'hello-world', get_post_meta( $post->ID, '_customize_draft_post_name', true ) );
     606                $this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $post->ID, '_customize_changeset_uuid', true ) );
    606607        }
    607608
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r41204 r41887  
    542542                $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Non-existent', 'post_type' => 'nonexistent' ) );
    543543                $this->assertInstanceOf( 'WP_Post', $r );
     544                $this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
    544545
    545546                $r = $menus->insert_auto_draft_post( array( 'post_type' => 'post' ) );
     
    556557                $this->assertEquals( '', $r->post_name );
    557558                $this->assertEquals( 'hello-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
     559                $this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
    558560                $this->assertEquals( 'post', $r->post_type );
    559561
     
    564566                $this->assertEquals( '', $r->post_name );
    565567                $this->assertEquals( 'greetings-world', get_post_meta( $r->ID, '_customize_draft_post_name', true ) );
     568                $this->assertEquals( $this->wp_customize->changeset_uuid(), get_post_meta( $r->ID, '_customize_changeset_uuid', true ) );
    566569                $this->assertEquals( 'Hi World', $r->post_content );
    567570        }
     
    717720                ) );
    718721
     722                $draft_post_id = $this->factory()->post->create( array(
     723                        'post_status' => 'draft',
     724                        'post_title' => 'Draft',
     725                        'post_author' => $administrator_user_id,
     726                ) );
     727
     728                $private_post_id = $this->factory()->post->create( array(
     729                        'post_status' => 'private',
     730                        'post_title' => 'Private',
     731                        'post_author' => $administrator_user_id,
     732                ) );
     733
    719734                $value = array(
    720735                        'bad',
     
    722737                        $author_post_id,
    723738                        $administrator_post_id,
     739                        $draft_post_id,
     740                        $private_post_id,
    724741                );
    725742
     
    734751                wp_set_current_user( $administrator_user_id );
    735752                $sanitized = $menus->sanitize_nav_menus_created_posts( $value );
    736                 $this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id ), $sanitized );
     753                $this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id, $draft_post_id ), $sanitized );
    737754        }
    738755
     
    747764
    748765                $post_ids = array();
    749                 for ( $i = 0; $i < 3; $i += 1 ) {
    750                         $r = $menus->insert_auto_draft_post( array(
    751                                 'post_title' => 'Auto Draft ' . $i,
    752                                 'post_type' => 'post',
    753                                 'post_name' => 'auto-draft-' . $i,
    754                         ) );
    755                         $this->assertInstanceOf( 'WP_Post', $r );
    756                         $post_ids[] = $r->ID;
    757                 }
     766
     767                // Auto-draft.
     768                $r = $menus->insert_auto_draft_post( array(
     769                        'post_title' => 'Auto Draft',
     770                        'post_type' => 'post',
     771                        'post_name' => 'auto-draft-1',
     772                ) );
     773                $this->assertInstanceOf( 'WP_Post', $r );
     774                $post_ids[] = $r->ID;
     775
     776                // Draft.
     777                $r = $menus->insert_auto_draft_post( array(
     778                        'post_title' => 'Draft',
     779                        'post_type' => 'post',
     780                        'post_name' => 'auto-draft-2',
     781                ) );
     782                $this->assertInstanceOf( 'WP_Post', $r );
     783                wp_update_post( array(
     784                        'ID' => $r->ID,
     785                        'post_status' => 'draft',
     786                ) );
     787                $post_ids[] = $r->ID;
     788
     789                $drafted_post_ids = $post_ids;
     790
     791                // Private (this will exclude it from being considered a stub).
     792                $r = $menus->insert_auto_draft_post( array(
     793                        'post_title' => 'Private',
     794                        'post_type' => 'post',
     795                        'post_name' => 'auto-draft-3',
     796                ) );
     797                $this->assertInstanceOf( 'WP_Post', $r );
     798                wp_update_post( array(
     799                        'ID' => $r->ID,
     800                        'post_status' => 'private',
     801                ) );
     802                $post_ids[] = $r->ID;
     803                $private_post_id = $r->ID;
     804
     805                // Trashed (this will exclude it from being considered a stub).
     806                $r = $menus->insert_auto_draft_post( array(
     807                        'post_title' => 'Trash',
     808                        'post_type' => 'post',
     809                        'post_name' => 'auto-draft-4',
     810                ) );
     811                $this->assertInstanceOf( 'WP_Post', $r );
     812                wp_trash_post( $r->ID );
     813                $post_ids[] = $r->ID;
     814                $trashed_post_id = $r->ID;
    758815
    759816                $pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
     
    764821                $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting );
    765822                $this->assertEquals( array( $menus, 'sanitize_nav_menus_created_posts' ), $setting->sanitize_callback );
    766                 $this->assertEquals( $post_ids, $setting->post_value() );
    767                 foreach ( $post_ids as $post_id ) {
    768                         $this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
     823                $this->assertEquals( $drafted_post_ids, $setting->post_value() );
     824                $this->assertArrayNotHasKey( $private_post_id, $post_ids );
     825                $this->assertArrayNotHasKey( $trashed_post_id, $post_ids );
     826
     827                $this->assertEquals( 'auto-draft', get_post_status( $drafted_post_ids[0] ) );
     828                $this->assertEquals( 'draft', get_post_status( $drafted_post_ids[1] ) );
     829                foreach ( $drafted_post_ids as $post_id ) {
    769830                        $this->assertEmpty( get_post( $post_id )->post_name );
    770831                        $this->assertNotEmpty( get_post_meta( $post_id, '_customize_draft_post_name', true ) );
     
    774835                $setting->save();
    775836                $this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
    776                 foreach ( $post_ids as $post_id ) {
     837                foreach ( $drafted_post_ids as $post_id ) {
    777838                        $this->assertEquals( 'publish', get_post_status( $post_id ) );
    778839                        $this->assertRegExp( '/^auto-draft-\d+$/', get_post( $post_id )->post_name );
     
    780841                }
    781842
     843                $this->assertEquals( 'private', get_post_status( $private_post_id ) );
     844                $this->assertEquals( 'trash', get_post_status( $trashed_post_id ) );
     845
    782846                // Ensure that unique slugs were assigned.
    783                 $posts = array_map( 'get_post', $post_ids );
     847                $posts = array_map( 'get_post', $drafted_post_ids );
    784848                $post_names = wp_list_pluck( $posts, 'post_name' );
    785849                $this->assertEqualSets( $post_names, array_unique( $post_names ) );
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r41766 r41887  
    550550         */
    551551        function test_wp_delete_customize_changeset_dependent_auto_drafts() {
    552                 $nav_created_post_ids = $this->factory()->post->create_many(2, array(
     552                $auto_draft_post_id = $this->factory()->post->create( array(
    553553                        'post_status' => 'auto-draft',
    554554                ) );
     555                $draft_post_id = $this->factory()->post->create( array(
     556                        'post_status' => 'draft',
     557                ) );
     558                $private_post_id = $this->factory()->post->create( array(
     559                        'post_status' => 'private',
     560                ) );
     561
     562                $nav_created_post_ids = array(
     563                        $auto_draft_post_id,
     564                        $draft_post_id,
     565                        $private_post_id,
     566                );
    555567                $data = array(
    556568                        'nav_menus_created_posts' => array(
     
    567579                        'data' => $data,
    568580                ) );
    569                 $this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
    570                 $this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
     581                $this->assertEquals( 'auto-draft', get_post_status( $auto_draft_post_id ) );
     582                $this->assertEquals( 'draft', get_post_status( $draft_post_id ) );
     583                $this->assertEquals( 'private', get_post_status( $private_post_id ) );
    571584                wp_delete_post( $wp_customize->changeset_post_id(), true );
    572                 $this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
    573                 $this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
     585                $this->assertFalse( get_post_status( $auto_draft_post_id ) );
     586                $this->assertEquals( 'trash', get_post_status( $draft_post_id ) );
     587                $this->assertEquals( 'private', get_post_status( $private_post_id ) );
    574588        }
    575589
  • trunk/tests/phpunit/tests/theme.php

    r40676 r41887  
    319319                $nav_created_post_ids = $this->factory()->post->create_many(2, array(
    320320                        'post_status' => 'auto-draft',
     321                        'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-2 days' ) ),
    321322                ) );
    322323                $data = array(
     
    329330                $wp_customize = new WP_Customize_Manager();
    330331                do_action( 'customize_register', $wp_customize );
     332
     333                // The post_date for auto-drafts is bumped to match the changeset post_date whenever it is modified to keep them from from being garbage collected by wp_delete_auto_drafts().
    331334                $wp_customize->save_changeset_post( array(
    332335                        'data' => $data,
     
    334337                $this->assertEquals( get_post( $nav_created_post_ids[0] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
    335338                $this->assertEquals( get_post( $nav_created_post_ids[1] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
     339                $this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[0] ) );
     340                $this->assertEquals( 'auto-draft', get_post_status( $nav_created_post_ids[1] ) );
     341
     342                // Stubs transition to drafts when changeset is saved as a draft.
    336343                $wp_customize->save_changeset_post( array(
    337344                        'status' => 'draft',
    338345                        'data' => $data,
    339346                ) );
    340                 $expected_year = date( 'Y' ) + 100;
    341                 $this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[0] )->post_date ) ) );
    342                 $this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[1] )->post_date ) ) );
     347                $this->assertEquals( get_post( $nav_created_post_ids[0] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
     348                $this->assertEquals( get_post( $nav_created_post_ids[1] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
     349                $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
     350                $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[1] ) );
     351
     352                // Status remains unchanged for stub that the user broke out of the changeset.
     353                wp_update_post( array(
     354                        'ID' => $nav_created_post_ids[1],
     355                        'post_status' => 'private',
     356                ) );
     357                $wp_customize->save_changeset_post( array(
     358                        'status' => 'draft',
     359                        'data' => $data,
     360                ) );
     361                $this->assertEquals( 'draft', get_post_status( $nav_created_post_ids[0] ) );
     362                $this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
     363
     364                // Draft stub is trashed when the changeset is trashed.
     365                $wp_customize->trash_changeset_post( $wp_customize->changeset_post_id() );
     366                $this->assertEquals( 'trash', get_post_status( $nav_created_post_ids[0] ) );
     367                $this->assertEquals( 'private', get_post_status( $nav_created_post_ids[1] ) );
    343368        }
    344369}
Note: See TracChangeset for help on using the changeset viewer.