Make WordPress Core


Ignore:
Timestamp:
09/27/2017 10:24:37 PM (7 years ago)
Author:
westonruter
Message:

Customize: Introduce drafting and scheduling for Customizer changesets.

  • Incorporates code from the Customize Snapshots and Customize Posts feature plugins.
  • Adds a new Publish Settings section for managing the changeset status, scheduled date, and frontend preview link.
  • Updates Publish button to reflect the status selected in the Publish Settings (including Save Draft and Schedule).
  • Deactivates the Themes section when a non-publish status selected, and deactivates the Publish Settings section when previewing a theme switch.
  • Introduces an outer section type (wp.customize.OuterSection in JS) for the Publish Settings section to use and for available widgets and available nav menu panels to use in the future. These sections can be expanded while other sections are expanded.
  • Introduces WP_Customize_Date_Time_Control in PHP and wp.customize.DateTimeControl in JS for managing a date/time value.
  • Keeps track of scheduled time and proactively publish from the client when the time arrives, as opposed to waiting for WP Cron.
  • Auto-publishes a scheduled changeset when attempting to access one that missed its schedule.
  • Starts a new changeset if attempting to save a changeset that was previously publish.
  • Adds force arg to requestChangesetUpdate() to force an update request even when there are no pending changes.
  • Adds utils methods for getCurrentTimestamp and getRemainingTime.
  • Adds new state values for selectedChangesetStatus, changesetDate, selectedChangesetDate.
  • Fixes logic for when to short-circuit check to close Customizer when there are unsaved changes.
  • Adds getter methods for autosaved and branching parameters, with the latter applying the customize_changeset_branching filter.
  • Call to establish_loaded_changeset on the fly when changeset_uuid() is called if no changeset UUID was specififed.
  • De-duplicates logic for dismissing auto-draft changesets.
  • Includes unit tests.

Builds on [41597].
Props sayedwp, westonruter, melchoyce, JoshuaWold, folletto, stubgo, karmatosed, dlh, paaljoachim, afercia, johnregan3, utkarshpatel, valendesigns.
See #30937.
Fixes #39896, #28721, #39275.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r41558 r41626  
    121121        $this->assertEquals( $theme, $wp_customize->get_stylesheet() );
    122122        $this->assertEquals( $messenger_channel, $wp_customize->get_messenger_channel() );
     123        $this->assertFalse( $wp_customize->autosaved() );
     124        $this->assertTrue( $wp_customize->branching() );
     125
     126        $wp_customize = new WP_Customize_Manager( array(
     127            'changeset_uuid' => null,
     128        ) );
     129        $this->assertTrue( wp_is_uuid( $wp_customize->changeset_uuid(), 4 ) );
    123130
    124131        $theme = 'twentyfourteen';
     
    134141        $wp_customize = new WP_Customize_Manager();
    135142        $this->assertEquals( $theme, $wp_customize->get_stylesheet() );
    136         $this->assertNotEmpty( $wp_customize->changeset_uuid() );
     143        $this->assertTrue( wp_is_uuid( $wp_customize->changeset_uuid(), 4 ) );
     144    }
     145
     146    /**
     147     * Test constructor when deferring UUID.
     148     *
     149     * @ticket 39896
     150     * @covers WP_Customize_Manager::establish_loaded_changeset()
     151     * @covers WP_Customize_Manager::__construct()
     152     */
     153    public function test_constructor_deferred_changeset_uuid() {
     154        $data = array(
     155            'blogname' => array(
     156                'value' => 'Test',
     157            ),
     158        );
     159        $uuid = wp_generate_uuid4();
     160        $post_id = $this->factory()->post->create( array(
     161            'post_type' => 'customize_changeset',
     162            'post_name' => $uuid,
     163            'post_status' => 'draft',
     164            'post_content' => wp_json_encode( $data ),
     165        ) );
     166        $wp_customize = new WP_Customize_Manager( array(
     167            'changeset_uuid' => false, // Cause UUID to be deferred.
     168            'branching' => false, // To cause drafted changeset to be autoloaded.
     169        ) );
     170        $this->assertEquals( $uuid, $wp_customize->changeset_uuid() );
     171        $this->assertEquals( $post_id, $wp_customize->changeset_post_id() );
    137172    }
    138173
     
    255290
    256291    /**
     292     * Test WP_Customize_Manager::autosaved().
     293     *
     294     * @ticket 39896
     295     * @covers WP_Customize_Manager::autosaved()
     296     */
     297    public function test_autosaved() {
     298        $wp_customize = new WP_Customize_Manager();
     299        $this->assertFalse( $wp_customize->autosaved() );
     300
     301        $wp_customize = new WP_Customize_Manager( array( 'autosaved' => false ) );
     302        $this->assertFalse( $wp_customize->autosaved() );
     303
     304        $wp_customize = new WP_Customize_Manager( array( 'autosaved' => true ) );
     305        $this->assertTrue( $wp_customize->autosaved() );
     306    }
     307
     308    /**
     309     * Test WP_Customize_Manager::branching().
     310     *
     311     * @ticket 39896
     312     * @covers WP_Customize_Manager::branching()
     313     */
     314    public function test_branching() {
     315        $wp_customize = new WP_Customize_Manager();
     316        $this->assertTrue( $wp_customize->branching(), 'Branching should default to true since it is original behavior in 4.7.' );
     317
     318        $wp_customize = new WP_Customize_Manager( array( 'branching' => false ) );
     319        $this->assertFalse( $wp_customize->branching() );
     320        add_filter( 'customize_changeset_branching', '__return_true' );
     321        $this->assertTrue( $wp_customize->branching() );
     322        remove_filter( 'customize_changeset_branching', '__return_true' );
     323
     324        $wp_customize = new WP_Customize_Manager( array( 'branching' => true ) );
     325        $this->assertTrue( $wp_customize->branching() );
     326        add_filter( 'customize_changeset_branching', '__return_false' );
     327        $this->assertFalse( $wp_customize->branching() );
     328    }
     329
     330    /**
    257331     * Test WP_Customize_Manager::changeset_uuid().
    258332     *
     
    338412     */
    339413    function test_changeset_data() {
     414        wp_set_current_user( self::$admin_user_id );
    340415        $uuid = wp_generate_uuid4();
    341416        $wp_customize = new WP_Customize_Manager( array( 'changeset_uuid' => $uuid ) );
     
    343418
    344419        $uuid = wp_generate_uuid4();
    345         $data = array( 'blogname' => array( 'value' => 'Hello World' ) );
     420        $data = array(
     421            'blogname' => array( 'value' => 'Hello World' ),
     422            'blogdescription' => array( 'value' => 'Greet the world' ),
     423        );
    346424        $this->factory()->post->create( array(
    347425            'post_name' => $uuid,
    348426            'post_type' => 'customize_changeset',
    349             'post_status' => 'auto-draft',
     427            'post_status' => 'draft',
    350428            'post_content' => wp_json_encode( $data ),
    351429        ) );
    352430        $wp_customize = new WP_Customize_Manager( array( 'changeset_uuid' => $uuid ) );
    353431        $this->assertEquals( $data, $wp_customize->changeset_data() );
     432
     433        // Autosave.
     434        $wp_customize->set_post_value( 'blogname', 'Hola Mundo' );
     435        $wp_customize->register_controls(); // That is, settings, so blogname setting is registered.
     436        $r = $wp_customize->save_changeset_post( array(
     437            'autosave' => true,
     438        ) );
     439        $this->assertNotInstanceOf( 'WP_Error', $r );
     440
     441        // No change to data if not requesting autosave.
     442        $wp_customize = new WP_Customize_Manager( array(
     443            'changeset_uuid' => $uuid,
     444            'autosaved' => false,
     445        ) );
     446        $wp_customize->register_controls(); // That is, settings.
     447        $this->assertFalse( $wp_customize->autosaved() );
     448        $this->assertEquals( $data, $wp_customize->changeset_data() );
     449
     450        // No change to data if not requesting autosave.
     451        $wp_customize = new WP_Customize_Manager( array(
     452            'changeset_uuid' => $uuid,
     453            'autosaved' => true,
     454        ) );
     455        $this->assertTrue( $wp_customize->autosaved() );
     456        $this->assertNotEquals( $data, $wp_customize->changeset_data() );
     457        $this->assertEquals(
     458            array_merge(
     459                wp_list_pluck( $data, 'value' ),
     460                array( 'blogname' => 'Hola Mundo' )
     461            ),
     462            wp_list_pluck( $wp_customize->changeset_data(), 'value' )
     463        );
    354464    }
    355465
     
    12741384
    12751385    /**
     1386     * Test writing changesets when user supplies unchanged values.
     1387     *
     1388     * @ticket 39896
     1389     * @covers WP_Customize_Manager::save_changeset_post()
     1390     * @covers WP_Customize_Manager::grant_edit_post_capability_for_changeset()
     1391     */
     1392    public function test_save_changeset_post_with_autosave() {
     1393        wp_set_current_user( self::$admin_user_id );
     1394        $uuid = wp_generate_uuid4();
     1395        $changeset_post_id = wp_insert_post( array(
     1396            'post_type' => 'customize_changeset',
     1397            'post_content' => wp_json_encode( array(
     1398                'blogname' => array(
     1399                    'value' => 'Auto-draft Title',
     1400                ),
     1401            ) ),
     1402            'post_author' => self::$admin_user_id,
     1403            'post_name' => $uuid,
     1404            'post_status' => 'auto-draft',
     1405        ) );
     1406
     1407        $wp_customize = new WP_Customize_Manager( array(
     1408            'changeset_uuid' => $uuid,
     1409        ) );
     1410        $wp_customize->register_controls(); // And settings too.
     1411
     1412        // Autosave of an auto-draft overwrites original.
     1413        $wp_customize->save_changeset_post( array(
     1414            'data' => array(
     1415                'blogname' => array(
     1416                    'value' => 'Autosaved Auto-draft Title',
     1417                ),
     1418            ),
     1419            'autosave' => true,
     1420        ) );
     1421        $this->assertFalse( wp_get_post_autosave( $changeset_post_id ) );
     1422        $this->assertContains( 'Autosaved Auto-draft Title', get_post( $changeset_post_id )->post_content );
     1423
     1424        // Update status to draft for subsequent tests.
     1425        $wp_customize->save_changeset_post( array(
     1426            'data' => array(
     1427                'blogname' => array(
     1428                    'value' => 'Draft Title',
     1429                ),
     1430            ),
     1431            'status' => 'draft',
     1432            'autosave' => false,
     1433        ) );
     1434        $this->assertContains( 'Draft Title', get_post( $changeset_post_id )->post_content );
     1435
     1436        // Fail: illegal_autosave_with_date_gmt.
     1437        $r = $wp_customize->save_changeset_post( array(
     1438            'autosave' => true,
     1439            'date_gmt' => ( gmdate( 'Y' ) + 1 ) . '-12-01 00:00:00',
     1440        ) );
     1441        $this->assertInstanceOf( 'WP_Error', $r );
     1442        $this->assertEquals( 'illegal_autosave_with_date_gmt', $r->get_error_code() );
     1443
     1444        // Fail: illegal_autosave_with_status.
     1445        $r = $wp_customize->save_changeset_post( array(
     1446            'autosave' => true,
     1447            'status' => 'pending',
     1448        ) );
     1449        $this->assertEquals( 'illegal_autosave_with_status', $r->get_error_code() );
     1450
     1451        // Fail: illegal_autosave_with_non_current_user.
     1452        $r = $wp_customize->save_changeset_post( array(
     1453            'autosave' => true,
     1454            'user_id' => $this->factory()->user->create( array( 'role' => 'administrator' ) ),
     1455        ) );
     1456        $this->assertEquals( 'illegal_autosave_with_non_current_user', $r->get_error_code() );
     1457
     1458        // Try autosave.
     1459        $this->assertFalse( wp_get_post_autosave( $changeset_post_id ) );
     1460        $r = $wp_customize->save_changeset_post( array(
     1461            'data' => array(
     1462                'blogname' => array(
     1463                    'value' => 'Autosave Title',
     1464                ),
     1465            ),
     1466            'autosave' => true,
     1467        ) );
     1468        $this->assertInternalType( 'array', $r );
     1469
     1470        // Verify that autosave happened.
     1471        $autosave_revision = wp_get_post_autosave( $changeset_post_id );
     1472        $this->assertInstanceOf( 'WP_Post', $autosave_revision );
     1473        $this->assertContains( 'Draft Title', get_post( $changeset_post_id )->post_content );
     1474        $this->assertContains( 'Autosave Title', $autosave_revision->post_content );
     1475    }
     1476
     1477    /**
    12761478     * Test passing `null` for a setting ID to remove it from the changeset.
    12771479     *
     
    23522554        $this->assertNotEmpty( $data );
    23532555
    2354         $this->assertEqualSets( array( 'theme', 'url', 'browser', 'panels', 'sections', 'nonce', 'autofocus', 'documentTitleTmpl', 'previewableDevices', 'changeset', 'timeouts' ), array_keys( $data ) );
     2556        $this->assertEqualSets( array( 'theme', 'url', 'browser', 'panels', 'sections', 'nonce', 'autofocus', 'documentTitleTmpl', 'previewableDevices', 'changeset', 'timeouts', 'initialClientTimestamp', 'initialServerDate', 'initialServerTimestamp' ), array_keys( $data ) );
    23552557        $this->assertEquals( $autofocus, $data['autofocus'] );
    23562558        $this->assertArrayHasKey( 'save', $data['nonce'] );
    23572559        $this->assertArrayHasKey( 'preview', $data['nonce'] );
     2560
     2561        $this->assertEqualSets(
     2562            array(
     2563                'branching',
     2564                'autosaved',
     2565                'hasAutosaveRevision',
     2566                'latestAutoDraftUuid',
     2567                'status',
     2568                'uuid',
     2569                'currentUserCanPublish',
     2570                'publishDate',
     2571            ),
     2572            array_keys( $data['changeset'] )
     2573        );
    23582574    }
    23592575
Note: See TracChangeset for help on using the changeset viewer.