Changeset 36676
- Timestamp:
- 02/24/2016 05:56:40 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-customize-nav-menus.php
r36611 r36676 334 334 } 335 335 } 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 ); 336 346 337 347 return $items; -
trunk/tests/phpunit/tests/customize/nav-menus.php
r36586 r36676 278 278 $menus = new WP_Customize_Nav_Menus( $this->wp_customize ); 279 279 280 // Create posts 280 // Create posts. 281 281 $post_ids = array(); 282 282 $post_ids[] = self::factory()->post->create( array( 'post_title' => 'Search & Test' ) ); 283 283 $post_ids[] = self::factory()->post->create( array( 'post_title' => 'Some Other Title' ) ); 284 284 285 // Create terms 285 // Create terms. 286 286 $term_ids = array(); 287 287 $term_ids[] = self::factory()->category->create( array( 'name' => 'Dogs Are Cool' ) ); 288 288 $term_ids[] = self::factory()->category->create( array( 'name' => 'Cats Drool' ) ); 289 289 290 // Test empty results 290 // Test empty results. 291 291 $expected = array(); 292 292 $results = $menus->search_available_items_query( array( 'pagenum' => 1, 's' => 'This Does NOT Exist' ) ); 293 293 $this->assertEquals( $expected, $results ); 294 294 295 // Test posts 295 // Test posts. 296 296 foreach ( $post_ids as $post_id ) { 297 297 $expected = array( … … 311 311 } 312 312 313 // Test terms 313 // Test terms. 314 314 foreach ( $term_ids as $term_id ) { 315 315 $term = get_term_by( 'id', $term_id, 'category' ); … … 327 327 $this->assertEquals( $expected, $results[0] ); 328 328 } 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; 329 379 } 330 380
Note: See TracChangeset
for help on using the changeset viewer.