WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/29/2016 10:58:32 PM (4 years ago)
Author:
westonruter
Message:

Customize: Allow users to more seamlessly create page-based nav menus during customization.

Introduces the ability to create stubs for the various post types to add to a given menu. This eliminates the need to leave the customizer to first create the post in the admin and then return to managing menus. Only the title of the newly-created post can be supplied; the post content will be blank and will need to be provided in the normal edit post screen outside the customizer, unless a plugin enables a post editing in the customizer experience. When a post is created and added to a nav menu in the customizer, the newly created post that is added to a menu is given the auto-draft status, and if the changes are not published, the auto-draft post will be automatically deleted within 7 days via wp_delete_auto_drafts(). However, if the customizer changes are saved, then these nav menu item auto-draft post stubs will be transitioned to publish.

Includes portions of code from the Customize Posts <https://github.com/xwp/wp-customize-posts> and Front-end Editor <https://github.com/iseulde/wp-front-end-editor> plugins.

For more information, see https://make.wordpress.org/core/2016/06/16/feature-proposal-content-authorship-in-menus-with-live-preview/

Props celloexpressions, westonruter, valendesigns, afercia, melchoyce, mapk, iseulde, mrahmadawais.
Fixes #34923.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/ajax/CustomizeMenus.php

    r35244 r38436  
    527527        );
    528528    }
     529
     530    /**
     531     * Testing successful ajax_insert_auto_draft_post() call.
     532     *
     533     * @covers WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
     534     */
     535    function test_ajax_insert_auto_draft_post_success() {
     536        $_POST = wp_slash( array(
     537            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     538            'params' => array(
     539                'post_type' => 'post',
     540                'post_title' => 'Hello World',
     541            ),
     542        ) );
     543        $this->_last_response = '';
     544        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     545        $response = json_decode( $this->_last_response, true );
     546
     547        $this->assertTrue( $response['success'] );
     548        $this->assertArrayHasKey( 'post_id', $response['data'] );
     549        $this->assertArrayHasKey( 'url', $response['data'] );
     550    }
     551
     552    /**
     553     * Testing unsuccessful ajax_insert_auto_draft_post() call.
     554     *
     555     * @covers WP_Customize_Nav_Menus::ajax_insert_auto_draft_post()
     556     */
     557    function test_ajax_insert_auto_draft_failures() {
     558        // No nonce.
     559        $_POST = array();
     560        $this->_last_response = '';
     561        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     562        $response = json_decode( $this->_last_response, true );
     563        $this->assertFalse( $response['success'] );
     564        $this->assertEquals( 'bad_nonce', $response['data'] );
     565
     566        // Bad nonce.
     567        $_POST = wp_slash( array(
     568            'customize-menus-nonce' => 'bad',
     569        ) );
     570        $this->_last_response = '';
     571        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     572        $response = json_decode( $this->_last_response, true );
     573        $this->assertFalse( $response['success'] );
     574        $this->assertEquals( 'bad_nonce', $response['data'] );
     575
     576        // Bad nonce.
     577        wp_set_current_user( $this->factory()->user->create( array( 'role' => 'subscriber' ) ) );
     578        $_POST = wp_slash( array(
     579            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     580        ) );
     581        $this->_last_response = '';
     582        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     583        $response = json_decode( $this->_last_response, true );
     584        $this->assertFalse( $response['success'] );
     585        $this->assertEquals( 'customize_not_allowed', $response['data'] );
     586
     587        // Missing params.
     588        wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
     589        $_POST = wp_slash( array(
     590            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     591        ) );
     592        $this->_last_response = '';
     593        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     594        $response = json_decode( $this->_last_response, true );
     595        $this->assertFalse( $response['success'] );
     596        $this->assertEquals( 'missing_params', $response['data'] );
     597
     598        // insufficient_post_permissions.
     599        register_post_type( 'privilege', array( 'capability_type' => 'privilege' ) );
     600        $_POST = wp_slash( array(
     601            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     602            'params' => array(
     603                'post_type' => 'privilege',
     604            ),
     605        ) );
     606        $this->_last_response = '';
     607        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     608        $response = json_decode( $this->_last_response, true );
     609        $this->assertFalse( $response['success'] );
     610        $this->assertEquals( 'insufficient_post_permissions', $response['data'] );
     611
     612        // insufficient_post_permissions.
     613        $_POST = wp_slash( array(
     614            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     615            'params' => array(
     616                'post_type' => 'non-existent',
     617            ),
     618        ) );
     619        $this->_last_response = '';
     620        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     621        $response = json_decode( $this->_last_response, true );
     622        $this->assertFalse( $response['success'] );
     623        $this->assertEquals( 'missing_post_type_param', $response['data'] );
     624
     625        // missing_post_title.
     626        $_POST = wp_slash( array(
     627            'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
     628            'params' => array(
     629                'post_type' => 'post',
     630                'post_title' => '    ',
     631            ),
     632        ) );
     633        $this->_last_response = '';
     634        $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' );
     635        $response = json_decode( $this->_last_response, true );
     636        $this->assertFalse( $response['success'] );
     637        $this->assertEquals( 'missing_post_title', $response['data'] );
     638    }
    529639}
Note: See TracChangeset for help on using the changeset viewer.