WordPress.org

Make WordPress Core

Changeset 36676


Ignore:
Timestamp:
02/24/2016 05:56:40 AM (4 years ago)
Author:
westonruter
Message:

Customize: Introduce customize_nav_menu_searched_items filter for modifying results of nav menu item searches.

This new filter can be used in conjunction with the customize_nav_menu_available_items and customize_nav_menu_available_item_types filters.

Props TimothyBlynJacobs, westonruter.
Fixes #34947.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-nav-menus.php

    r36611 r36676  
    334334            }
    335335        }
     336
     337        /**
     338         * Filter the available menu items during a search request.
     339         *
     340         * @since 4.5.0
     341         *
     342         * @param array $items The array of menu items.
     343         * @param array $args  Includes 'pagenum' and 's' (search) arguments.
     344         */
     345        $items = apply_filters( 'customize_nav_menu_searched_items', $items, $args );
    336346
    337347        return $items;
  • trunk/tests/phpunit/tests/customize/nav-menus.php

    r36586 r36676  
    278278        $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    279279
    280         // Create posts
     280        // Create posts.
    281281        $post_ids = array();
    282282        $post_ids[] = self::factory()->post->create( array( 'post_title' => 'Search & Test' ) );
    283283        $post_ids[] = self::factory()->post->create( array( 'post_title' => 'Some Other Title' ) );
    284284
    285         // Create terms
     285        // Create terms.
    286286        $term_ids = array();
    287287        $term_ids[] = self::factory()->category->create( array( 'name' => 'Dogs Are Cool' ) );
    288288        $term_ids[] = self::factory()->category->create( array( 'name' => 'Cats Drool' ) );
    289289
    290         // Test empty results
     290        // Test empty results.
    291291        $expected = array();
    292292        $results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => 'This Does NOT Exist' ) );
    293293        $this->assertEquals( $expected, $results );
    294294
    295         // Test posts
     295        // Test posts.
    296296        foreach ( $post_ids as $post_id ) {
    297297            $expected = array(
     
    311311        }
    312312
    313         // Test terms
     313        // Test terms.
    314314        foreach ( $term_ids as $term_id ) {
    315315            $term = get_term_by( 'id', $term_id, 'category' );
     
    327327            $this->assertEquals( $expected, $results[0] );
    328328        }
     329
     330        // Test filtered results.
     331        $results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => 'cat' ) );
     332        $this->assertEquals( 1, count( $results ) );
     333        $count = $this->filter_count_customize_nav_menu_searched_items;
     334        add_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10, 2 );
     335        $results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => 'cat' ) );
     336        $this->assertEquals( $count + 1, $this->filter_count_customize_nav_menu_searched_items );
     337        $this->assertInternalType( 'array', $results );
     338        $this->assertEquals( 2, count( $results ) );
     339        remove_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10 );
     340    }
     341
     342    /**
     343     * Count for number of times customize_nav_menu_searched_items filtered.
     344     *
     345     * @var int
     346     */
     347    protected $filter_count_customize_nav_menu_searched_items = 0;
     348
     349    /**
     350     * Filter to search menu items.
     351     *
     352     * @param array $items Items.
     353     * @param array $args {
     354     *     Search args.
     355     *
     356     *     @type int    $pagenum Page number.
     357     *     @type string $s       Search string.
     358     * }
     359     * @return array Items.
     360     */
     361    function filter_search( $items, $args ) {
     362        $this->assertInternalType( 'array', $items );
     363        $this->assertInternalType( 'array', $args );
     364        $this->assertArrayHasKey( 's', $args );
     365        $this->assertArrayHasKey( 'pagenum', $args );
     366        $this->filter_count_customize_nav_menu_searched_items += 1;
     367
     368        if ( 'cat' === $args['s'] ) {
     369            array_unshift( $items, array(
     370                'id'         => 'home',
     371                'title'      => 'COOL CAT!',
     372                'type'       => 'custom',
     373                'type_label' => __( 'Custom Link' ),
     374                'object'     => '',
     375                'url'        => home_url( '/cool-cat' ),
     376            ) );
     377        }
     378        return $items;
    329379    }
    330380
Note: See TracChangeset for help on using the changeset viewer.