Menus: Allow empty taxonomy terms to be surfaced when searching for items.

This brings the behaviour inline with that of browsing terms or using the All Items tab, which correctly shows empty terms.

Props birgire, audrasjb

Fixes #45298

git-svn-id: https://develop.svn.wordpress.org/trunk@47747 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2020-05-02 22:34:50 +00:00
parent 1510f69d69
commit 4889e7a8d2
4 changed files with 77 additions and 2 deletions

View File

@ -113,6 +113,7 @@ function _wp_ajax_menu_quick_search( $request = array() ) {
'taxonomy' => $matches[2],
'name__like' => $query,
'number' => 10,
'hide_empty' => false,
)
);
if ( empty( $terms ) || is_wp_error( $terms ) ) {

View File

@ -369,6 +369,7 @@ final class WP_Customize_Nav_Menus {
'taxonomies' => $taxonomies,
'name__like' => $args['s'],
'number' => 20,
'hide_empty' => false,
'offset' => 20 * ( $args['pagenum'] - 1 ),
)
);

View File

@ -371,7 +371,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
's' => 'cat',
)
);
$this->assertEquals( 1, count( $results ) );
$this->assertCount( 2, $results ); // Category terms Cats Drool and Uncategorized.
$count = $this->filter_count_customize_nav_menu_searched_items;
add_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10, 2 );
$results = $menus->search_available_items_query(
@ -382,7 +382,7 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
);
$this->assertEquals( $count + 1, $this->filter_count_customize_nav_menu_searched_items );
$this->assertInternalType( 'array', $results );
$this->assertEquals( 2, count( $results ) );
$this->assertCount( 3, $results );
remove_filter( 'customize_nav_menu_searched_items', array( $this, 'filter_search' ), 10 );
// Test home.
@ -398,6 +398,53 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertEquals( 'custom', $results[0]['type'] );
}
/*
* Tests that the search_available_items_query method should return term items
* not assigned to any posts.
*
* @ticket 45298
*/
public function test_search_available_items_query_should_return_unassigned_term_items() {
$menus = new WP_Customize_Nav_Menus( $this->wp_customize );
register_taxonomy(
'wptests_tax',
'post',
array(
'labels' => array(
'name' => 'Tests Taxonomy',
),
)
);
$term_id = $this->factory->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foobar',
)
);
// Expected menu item array.
$expected = array(
'title' => 'foobar',
'id' => "term-{$term_id}",
'type' => 'taxonomy',
'type_label' => 'Tests Taxonomy',
'object' => 'wptests_tax',
'object_id' => intval( $term_id ),
'url' => get_term_link( intval( $term_id ), '' ),
);
$results = $menus->search_available_items_query(
array(
'pagenum' => 1,
's' => 'foo',
)
);
$this->assertEqualSets( $expected, $results[0] );
}
/**
* Count for number of times customize_nav_menu_searched_items filtered.
*

View File

@ -120,4 +120,30 @@ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase {
$results = explode( "\n", trim( $output ) );
$this->assertCount( 1, $results );
}
/**
* Test that search displays terms that are not assigned to any posts.
*
* @ticket 45298
*/
public function test_search_should_return_unassigned_term_items() {
register_taxonomy( 'wptests_tax', 'post' );
$this->factory->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foobar',
)
);
$request = array(
'type' => 'quick-search-taxonomy-wptests_tax',
'q' => 'foobar',
);
$output = get_echo( '_wp_ajax_menu_quick_search', array( $request ) );
$this->assertNotEmpty( $output );
$results = explode( "\n", trim( $output ) );
$this->assertCount( 1, $results );
}
}