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/customize/nav-menus.php

    r36889 r38436  
    4646    function filter_item_types( $items ) {
    4747        $items[] = array(
    48             'title'  => 'Custom',
    49             'type'   => 'custom_type',
     48            'title' => 'Custom',
     49            'type' => 'custom_type',
    5050            'object' => 'custom_object',
     51            'type_label' => 'Custom Type',
    5152        );
    5253
     
    8586        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    8687        $this->assertInstanceOf( 'WP_Customize_Manager', $menus->manager );
     88
     89        $this->assertEquals( 10, add_filter( 'customize_refresh_nonces', array( $menus, 'filter_nonces' ) ) );
     90        $this->assertEquals( 10, add_action( 'wp_ajax_load-available-menu-items-customizer', array( $menus, 'ajax_load_available_items' ) ) );
     91        $this->assertEquals( 10, add_action( 'wp_ajax_search-available-menu-items-customizer', array( $menus, 'ajax_search_available_items' ) ) );
     92        $this->assertEquals( 10, add_action( 'wp_ajax_customize-nav-menus-insert-auto-draft', array( $menus, 'ajax_insert_auto_draft_post' ) ) );
     93        $this->assertEquals( 10, add_action( 'customize_controls_enqueue_scripts', array( $menus, 'enqueue_scripts' ) ) );
     94        $this->assertEquals( 11, add_action( 'customize_register', array( $menus, 'customize_register' ) ) );
     95        $this->assertEquals( 10, add_filter( 'customize_dynamic_setting_args', array( $menus, 'filter_dynamic_setting_args' ) ) );
     96        $this->assertEquals( 10, add_filter( 'customize_dynamic_setting_class', array( $menus, 'filter_dynamic_setting_class' ) ) );
     97        $this->assertEquals( 10, add_action( 'customize_controls_print_footer_scripts', array( $menus, 'print_templates' ) ) );
     98        $this->assertEquals( 10, add_action( 'customize_controls_print_footer_scripts', array( $menus, 'available_items_template' ) ) );
     99        $this->assertEquals( 10, add_action( 'customize_preview_init', array( $menus, 'customize_preview_init' ) ) );
     100        $this->assertEquals( 10, add_action( 'customize_preview_init', array( $menus, 'make_auto_draft_status_previewable' ) ) );
     101        $this->assertEquals( 10, add_action( 'customize_save_nav_menus_created_posts', array( $menus, 'save_nav_menus_created_posts' ) ) );
     102        $this->assertEquals( 10, add_filter( 'customize_dynamic_partial_args', array( $menus, 'customize_dynamic_partial_args' ) ) );
    87103    }
    88104
     
    445461            'menu-item-status'    => 'publish',
    446462        ) );
    447         $setting = new WP_Customize_Nav_Menu_Item_Setting( $this->wp_customize, "nav_menu_item[$item_id]" );
    448         do_action( 'customize_register', $this->wp_customize );
     463        do_action( 'customize_register', $this->wp_customize );
     464        $this->assertInstanceOf( 'WP_Customize_Nav_Menu_Item_Setting', $this->wp_customize->get_setting( "nav_menu_item[$item_id]" ) );
    449465        $this->assertEquals( 'Primary', $this->wp_customize->get_section( "nav_menu[$menu_id]" )->title );
    450466        $this->assertEquals( 'Hello World', $this->wp_customize->get_control( "nav_menu_item[$item_id]" )->label );
     467
     468        $nav_menus_created_posts_setting = $this->wp_customize->get_setting( 'nav_menus_created_posts' );
     469        $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $nav_menus_created_posts_setting );
     470        $this->assertEquals( 'postMessage', $nav_menus_created_posts_setting->transport );
     471        $this->assertEquals( array(), $nav_menus_created_posts_setting->default );
     472        $this->assertEquals( array( $this->wp_customize->nav_menus, 'sanitize_nav_menus_created_posts' ), $nav_menus_created_posts_setting->sanitize_callback );
    451473    }
    452474
     
    480502
    481503        $expected = array(
    482             array( 'title' => 'Posts', 'type' => 'post_type', 'object' => 'post' ),
    483             array( 'title' => 'Pages', 'type' => 'post_type', 'object' => 'page' ),
    484             array( 'title' => 'Categories', 'type' => 'taxonomy', 'object' => 'category' ),
    485             array( 'title' => 'Tags', 'type' => 'taxonomy', 'object' => 'post_tag' ),
     504            array( 'title' => 'Posts', 'type' => 'post_type', 'object' => 'post', 'type_label' => __( 'Post' ) ),
     505            array( 'title' => 'Pages', 'type' => 'post_type', 'object' => 'page', 'type_label' => __( 'Page' ) ),
     506            array( 'title' => 'Categories', 'type' => 'taxonomy', 'object' => 'category', 'type_label' => __( 'Category' ) ),
     507            array( 'title' => 'Tags', 'type' => 'taxonomy', 'object' => 'post_tag', 'type_label' => __( 'Tag' ) ),
    486508        );
    487509
    488510        if ( current_theme_supports( 'post-formats' ) ) {
    489             $expected[] = array( 'title' => 'Format', 'type' => 'taxonomy', 'object' => 'post_format' );
     511            $expected[] = array( 'title' => 'Format', 'type' => 'taxonomy', 'object' => 'post_format', 'type_label' => __( 'Format' ) );
    490512        }
    491513
     
    493515
    494516        register_taxonomy( 'wptests_tax', array( 'post' ), array( 'labels' => array( 'name' => 'Foo' ) ) );
    495         $expected[] = array( 'title' => 'Foo', 'type' => 'taxonomy', 'object' => 'wptests_tax' );
     517        $expected[] = array( 'title' => 'Foo', 'type' => 'taxonomy', 'object' => 'wptests_tax', 'type_label' => 'Foo' );
    496518
    497519        $this->assertEquals( $expected, $menus->available_item_types() );
    498520
    499         $expected[] = array( 'title' => 'Custom', 'type' => 'custom_type', 'object' => 'custom_object' );
     521        $expected[] = array( 'title' => 'Custom', 'type' => 'custom_type', 'object' => 'custom_object', 'type_label' => 'Custom Type' );
    500522
    501523        add_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
     
    503525        remove_filter( 'customize_nav_menu_available_item_types', array( $this, 'filter_item_types' ) );
    504526
     527    }
     528
     529    /**
     530     * Test insert_auto_draft_post method.
     531     *
     532     * @covers WP_Customize_Nav_Menus::insert_auto_draft_post()
     533     */
     534    public function test_insert_auto_draft_post() {
     535        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     536
     537        $r = $menus->insert_auto_draft_post( array() );
     538        $this->assertInstanceOf( 'WP_Error', $r );
     539        $this->assertEquals( 'unknown_post_type', $r->get_error_code() );
     540
     541        $r = $menus->insert_auto_draft_post( array( 'post_type' => 'fake' ) );
     542        $this->assertInstanceOf( 'WP_Error', $r );
     543        $this->assertEquals( 'unknown_post_type', $r->get_error_code() );
     544
     545        $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post' ) );
     546        $this->assertInstanceOf( 'WP_Post', $r );
     547        $this->assertEquals( 'Hello World', $r->post_title );
     548        $this->assertEquals( 'post', $r->post_type );
     549        $this->assertEquals( sanitize_title( $r->post_title ), $r->post_name );
    505550    }
    506551
     
    554599                $this->assertContains( 'data-type="post_type"', $template );
    555600                $this->assertContains( 'data-object="' . esc_attr( $type->name ) . '"', $template );
     601                $this->assertContains( 'data-type_label="' . esc_attr( $type->labels->singular_name ) . '"', $template );
    556602            }
    557603        }
     
    564610                $this->assertContains( 'data-type="taxonomy"', $template );
    565611                $this->assertContains( 'data-object="' . esc_attr( $tax->name ) . '"', $template );
     612                $this->assertContains( 'data-type_label="' . esc_attr( $tax->labels->singular_name ) . '"', $template );
    566613            }
    567614        }
     
    571618        $this->assertContains( 'data-type="custom_type"', $template );
    572619        $this->assertContains( 'data-object="custom_object"', $template );
     620        $this->assertContains( 'data-type_label="Custom Type"', $template );
    573621    }
    574622
     
    608656        $this->assertEquals( 1000, has_filter( 'wp_nav_menu_args', array( $menus, 'filter_wp_nav_menu_args' ) ) );
    609657        $this->assertEquals( 10, has_filter( 'wp_nav_menu', array( $menus, 'filter_wp_nav_menu' ) ) );
     658    }
     659
     660    /**
     661     * Test make_auto_draft_status_previewable.
     662     *
     663     * @covers WP_Customize_Nav_Menus::make_auto_draft_status_previewable()
     664     */
     665    function test_make_auto_draft_status_previewable() {
     666        global $wp_post_statuses;
     667        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     668        $menus->make_auto_draft_status_previewable();
     669        $this->assertTrue( $wp_post_statuses['auto-draft']->protected );
     670    }
     671
     672    /**
     673     * Test sanitize_nav_menus_created_posts.
     674     *
     675     * @covers WP_Customize_Nav_Menus::sanitize_nav_menus_created_posts()
     676     */
     677    function test_sanitize_nav_menus_created_posts() {
     678        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     679        $contributor_user_id = $this->factory()->user->create( array( 'role' => 'contributor' ) );
     680        $author_user_id = $this->factory()->user->create( array( 'role' => 'author' ) );
     681        $administrator_user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
     682
     683        $contributor_post_id = $this->factory()->post->create( array(
     684            'post_status' => 'auto-draft',
     685            'post_title' => 'Contributor Post',
     686            'post_type' => 'post',
     687            'post_author' => $contributor_user_id,
     688        ) );
     689        $author_post_id = $this->factory()->post->create( array(
     690            'post_status' => 'auto-draft',
     691            'post_title' => 'Author Post',
     692            'post_type' => 'post',
     693            'post_author' => $author_user_id,
     694        ) );
     695        $administrator_post_id = $this->factory()->post->create( array(
     696            'post_status' => 'auto-draft',
     697            'post_title' => 'Admin Post',
     698            'post_type' => 'post',
     699            'post_author' => $administrator_user_id,
     700        ) );
     701
     702        $value = array(
     703            'bad',
     704            $contributor_post_id,
     705            $author_post_id,
     706            $administrator_post_id,
     707        );
     708
     709        wp_set_current_user( $contributor_user_id );
     710        $sanitized = $menus->sanitize_nav_menus_created_posts( $value );
     711        $this->assertEquals( array(), $sanitized );
     712
     713        wp_set_current_user( $author_user_id );
     714        $sanitized = $menus->sanitize_nav_menus_created_posts( $value );
     715        $this->assertEquals( array( $author_post_id ), $sanitized );
     716
     717        wp_set_current_user( $administrator_user_id );
     718        $sanitized = $menus->sanitize_nav_menus_created_posts( $value );
     719        $this->assertEquals( array( $contributor_post_id, $author_post_id, $administrator_post_id ), $sanitized );
     720    }
     721
     722    /**
     723     * Test save_nav_menus_created_posts.
     724     *
     725     * @covers WP_Customize_Nav_Menus::save_nav_menus_created_posts()
     726     */
     727    function test_save_nav_menus_created_posts() {
     728        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     729        do_action( 'customize_register', $this->wp_customize );
     730
     731        $post_ids = $this->factory()->post->create_many( 3, array(
     732            'post_status' => 'auto-draft',
     733            'post_type' => 'post',
     734        ) );
     735        $pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) );
     736
     737        $setting_id = 'nav_menus_created_posts';
     738        $this->wp_customize->set_post_value( $setting_id, array_merge( $post_ids, array( $pre_published_post_id ) ) );
     739        $setting = $this->wp_customize->get_setting( $setting_id );
     740        $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting );
     741        $this->assertEquals( array( $menus, 'sanitize_nav_menus_created_posts' ), $setting->sanitize_callback );
     742        $this->assertEquals( $post_ids, $setting->post_value() );
     743        foreach ( $post_ids as $post_id ) {
     744            $this->assertEquals( 'auto-draft', get_post_status( $post_id ) );
     745        }
     746
     747        $save_action_count = did_action( 'customize_save_nav_menus_created_posts' );
     748        $setting->save();
     749        $this->assertEquals( $save_action_count + 1, did_action( 'customize_save_nav_menus_created_posts' ) );
     750        foreach ( $post_ids as $post_id ) {
     751            $this->assertEquals( 'publish', get_post_status( $post_id ) );
     752        }
    610753    }
    611754
Note: See TracChangeset for help on using the changeset viewer.