Make WordPress Core

Ticket #33404: 33404.3.diff

File 33404.3.diff, 3.9 KB (added by valendesigns, 8 years ago)
  • src/wp-admin/js/customize-nav-menus.js

    diff --git src/wp-admin/js/customize-nav-menus.js src/wp-admin/js/customize-nav-menus.js
    index b4e7be5..7b0ba4c 100644
     
    115115                pages: {},
    116116                sectionContent: '',
    117117                loading: false,
     118                excludeTerms: [],
    118119
    119120                initialize: function() {
    120121                        var self = this;
     
    218219                        }
    219220                       
    220221                        this.searchTerm = event.target.value;
     222                        this.excludeTerms = [];
    221223                        this.pages.search = 1;
    222224                        this.doSearch( 1 );
    223225                },
     
    251253                                'customize-menus-nonce': api.Menus.data.nonce,
    252254                                'wp_customize': 'on',
    253255                                'search': self.searchTerm,
    254                                 'page': page
     256                                'page': page,
     257                                'exclude': _.filter( self.excludeTerms, function( value, index, list ) {
     258                                        return list.indexOf( value ) === index;
     259                                } )
    255260                        };
    256261
    257262                        self.currentRequest = wp.ajax.post( 'search-available-menu-items-customizer', params );
    258263
    259264                        self.currentRequest.done(function( data ) {
    260                                 var items;
     265                                var items, lastItem, lastItemPosition;
    261266                                if ( 1 === page ) {
    262267                                        // Clear previous results as it's a new search.
    263268                                        $content.empty();
     
    270275                                self.collection.add( items.models );
    271276                                items.each( function( menuItem ) {
    272277                                        $content.append( itemTemplate( menuItem.attributes ) );
     278                                        if ( 'taxonomy' === menuItem.attributes.type ) {
     279                                                self.excludeTerms.push( menuItem.attributes.object_id );
     280                                        }
    273281                                } );
    274282                                if ( 20 > items.length ) {
    275283                                        self.pages.search = -1; // Up to 20 posts and 20 terms in results, if <20, no more results for either.
     
    281289                                } else if ( items && page === 1 ) {
    282290                                        wp.a11y.speak( api.Menus.data.l10n.itemsFound.replace( '%d', items.length ) );
    283291                                }
     292
     293                                lastItem = $content.find( 'li' ).last();
     294                                lastItemPosition = lastItem.position().top + lastItem.outerHeight( true );
     295
     296                                // The window is larger than the results. Initiate a scroll event to query page 2.
     297                                if ( -1 !== self.pages.search && 1 === page && $content.prop( 'scrollHeight' ) > lastItemPosition ) {
     298                                        $content.scroll();
     299                                }
    284300                        });
    285301
    286302                        self.currentRequest.fail(function( data ) {
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index adccacd..e832275 100644
    final class WP_Customize_Nav_Menus { 
    208208                        wp_send_json_error( 'nav_menus_missing_search_parameter' );
    209209                }
    210210
     211                $e = ! empty( $_POST['exclude'] ) ? array_filter( $_POST['exclude'], 'absint' ) : array();
    211212                $p = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
    212213                if ( $p < 1 ) {
    213214                        $p = 1;
    214215                }
    215216
    216217                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    217                 $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     218                $items = $this->search_available_items_query( array( 'exclude' => $e, 'pagenum' => $p, 's' => $s ) );
    218219
    219220                if ( empty( $items ) ) {
    220221                        wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
    final class WP_Customize_Nav_Menus { 
    231232         * @since 4.3.0
    232233         * @access public
    233234         *
    234          * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
     235         * @param array $args Optional. Accepts 'exclude' (term IDs), 'pagenum', and 's' (search) arguments.
    235236         * @return array Menu items.
    236237         */
    237238        public function search_available_items_query( $args = array() ) {
    final class WP_Customize_Nav_Menus { 
    254255                        $query['s'] = $args['s'];
    255256                }
    256257
     258                if ( ! isset( $args['exclude'] ) ) {
     259                        $args['exclude'] = array();
     260                }
     261
    257262                // Query posts.
    258263                $get_posts = new WP_Query( $query );
    259264
    final class WP_Customize_Nav_Menus { 
    282287                $terms = get_terms( $taxonomies, array(
    283288                        'name__like' => $args['s'],
    284289                        'number'     => 20,
    285                         'offset'     => 20 * ($args['pagenum'] - 1),
     290                        'exclude'    => $args['exclude'],
    286291                ) );
    287292
    288293                // Check if any taxonomies were found.