Make WordPress Core

Changeset 39138


Ignore:
Timestamp:
11/04/2016 07:23:49 AM (8 years ago)
Author:
westonruter
Message:

Customize: Ensure page/post stubs are included in listings and searches for available nav menu items.

Include the customized state in the Ajax requests to load items and search items.

See #38573.
Fixes #38122.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/customize-nav-menus.js

    r39137 r39138  
    303303            $section.addClass( 'loading' );
    304304            self.loading = true;
    305             params = {
     305
     306            params = api.previewer.query( { excludeCustomizedSaved: true } );
     307            _.extend( params, {
    306308                'customize-menus-nonce': api.settings.nonce['customize-menus'],
    307309                'wp_customize': 'on',
    308310                'search': self.searchTerm,
    309311                'page': page
    310             };
     312            } );
    311313
    312314            self.currentRequest = wp.ajax.post( 'search-available-menu-items-customizer', params );
     
    379381         */
    380382        loadItems: function( itemTypes, deprecated ) {
    381             var self = this, _itemTypes, requestItemTypes = [], request, itemTemplate, availableMenuItemContainers = {};
     383            var self = this, _itemTypes, requestItemTypes = [], params, request, itemTemplate, availableMenuItemContainers = {};
    382384            itemTemplate = wp.template( 'available-menu-item' );
    383385
     
    409411
    410412            self.loading = true;
    411             request = wp.ajax.post( 'load-available-menu-items-customizer', {
     413
     414            params = api.previewer.query( { excludeCustomizedSaved: true } );
     415            _.extend( params, {
    412416                'customize-menus-nonce': api.settings.nonce['customize-menus'],
    413417                'wp_customize': 'on',
    414418                'item_types': requestItemTypes
    415419            } );
     420
     421            request = wp.ajax.post( 'load-available-menu-items-customizer', params );
    416422
    417423            request.done(function( data ) {
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r39137 r39138  
    175175            }
    176176
    177             $posts = get_posts( array(
     177            // Prepend posts with nav_menus_created_posts on first page.
     178            $posts = array();
     179            if ( 0 === $page && $this->manager->get_setting( 'nav_menus_created_posts' ) ) {
     180                foreach ( $this->manager->get_setting( 'nav_menus_created_posts' )->value() as $post_id ) {
     181                    $auto_draft_post = get_post( $post_id );
     182                    if ( $post_type->name === $auto_draft_post->post_type ) {
     183                        $posts[] = $auto_draft_post;
     184                    }
     185                }
     186            }
     187
     188            $posts = array_merge( $posts, get_posts( array(
    178189                'numberposts' => 10,
    179190                'offset'      => 10 * $page,
     
    181192                'order'       => 'DESC',
    182193                'post_type'   => $object,
    183             ) );
     194            ) ) );
     195
    184196            foreach ( $posts as $post ) {
    185197                $post_title = $post->post_title;
     
    306318        }
    307319
     320        $posts = array();
     321
     322        // Prepend list of posts with nav_menus_created_posts search results on first page.
     323        $nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
     324        if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting ) > 0 ) {
     325            $stub_post_query = new WP_Query( array_merge(
     326                $query,
     327                array(
     328                    'post_status' => 'auto-draft',
     329                    'post__in' => $nav_menus_created_posts_setting->value(),
     330                    'posts_per_page' => -1,
     331                )
     332            ) );
     333            $posts = array_merge( $posts, $stub_post_query->posts );
     334        }
     335
    308336        // Query posts.
    309337        $get_posts = new WP_Query( $query );
    310 
    311         // Check if any posts were found.
    312         if ( $get_posts->post_count ) {
    313             foreach ( $get_posts->posts as $post ) {
    314                 $post_title = $post->post_title;
    315                 if ( '' === $post_title ) {
    316                     /* translators: %d: ID of a post */
    317                     $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    318                 }
    319                 $items[] = array(
    320                     'id'         => 'post-' . $post->ID,
    321                     'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    322                     'type'       => 'post_type',
    323                     'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,
    324                     'object'     => $post->post_type,
    325                     'object_id'  => intval( $post->ID ),
    326                     'url'        => get_permalink( intval( $post->ID ) ),
    327                 );
    328             }
     338        $posts = array_merge( $posts, $get_posts->posts );
     339
     340        // Create items for posts.
     341        foreach ( $posts as $post ) {
     342            $post_title = $post->post_title;
     343            if ( '' === $post_title ) {
     344                /* translators: %d: ID of a post */
     345                $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
     346            }
     347            $items[] = array(
     348                'id'         => 'post-' . $post->ID,
     349                'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     350                'type'       => 'post_type',
     351                'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,
     352                'object'     => $post->post_type,
     353                'object_id'  => intval( $post->ID ),
     354                'url'        => get_permalink( intval( $post->ID ) ),
     355            );
    329356        }
    330357
  • trunk/tests/phpunit/tests/ajax/CustomizeMenus.php

    r39137 r39138  
    323323     */
    324324    function test2_ajax_load_available_items_structure( $post_args ) {
     325        do_action( 'customize_register', $this->wp_customize );
    325326
    326327        $expected_keys = array(
     
    337338        self::factory()->term->create_many( 5 );
    338339        self::factory()->post->create_many( 5, array( 'post_type' => 'page' ) );
     340        $auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Test Auto Draft', 'post_type' => 'post' ) );
     341        $this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $auto_draft_post->ID ) );
     342        $this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
    339343
    340344        $_POST = array_merge( array(
     
    372376                }
    373377            }
     378        } elseif ( 'post' === $test_item['object'] ) {
     379            $item_ids = wp_list_pluck( $response['data']['items']['post_type:post'], 'id' );
     380            $this->assertContains( 'post-' . $auto_draft_post->ID, $item_ids );
    374381        }
    375382    }
     
    498505     */
    499506    function test_ajax_search_available_items_results( $post_args, $expected_results ) {
     507        do_action( 'customize_register', $this->wp_customize );
    500508
    501509        self::factory()->post->create_many( 5, array( 'post_title' => 'Test Post' ) );
     510        $included_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Test Included Auto Draft', 'post_type' => 'post' ) );
     511        $excluded_auto_draft_post = $this->wp_customize->nav_menus->insert_auto_draft_post( array( 'post_title' => 'Excluded Auto Draft', 'post_type' => 'post' ) );
     512        $this->wp_customize->set_post_value( 'nav_menus_created_posts', array( $included_auto_draft_post->ID, $excluded_auto_draft_post->ID ) );
     513        $this->wp_customize->get_setting( 'nav_menus_created_posts' )->preview();
    502514
    503515        $_POST = array_merge( array(
     
    512524        if ( isset( $post_args['search'] ) && 'test' === $post_args['search'] ) {
    513525            $this->assertsame( true, $response['success'] );
    514             $this->assertSame( 5, count( $response['data']['items'] ) );
     526            $this->assertSame( 6, count( $response['data']['items'] ) );
     527            $item_ids = wp_list_pluck( $response['data']['items'], 'id' );
     528            $this->assertContains( 'post-' . $included_auto_draft_post->ID, $item_ids );
     529            $this->assertNotContains( 'post-' . $excluded_auto_draft_post->ID, $item_ids );
    515530        } else {
    516531            $this->assertSame( $expected_results, $response );
    517532        }
    518 
    519533    }
    520534
Note: See TracChangeset for help on using the changeset viewer.