diff --git a/src/wp-admin/includes/nav-menu.php b/src/wp-admin/includes/nav-menu.php index 61657c05ce..30f38e9f56 100644 --- a/src/wp-admin/includes/nav-menu.php +++ b/src/wp-admin/includes/nav-menu.php @@ -70,14 +70,19 @@ function _wp_ajax_menu_quick_search( $request = array() ) { } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) { if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) { - $search_results_query = new WP_Query( array( + $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) ); + $args = array( 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, 'posts_per_page' => 10, 'post_type' => $matches[2], 's' => $query, - ) ); + ); + if ( isset( $post_type_obj->_default_query ) ) { + $args = array_merge( $args, (array) $post_type_obj->_default_query ); + } + $search_results_query = new WP_Query( $args ); if ( ! $search_results_query->have_posts() ) { return; } diff --git a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php index 54fa9cfdb6..c622b566ee 100644 --- a/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php +++ b/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php @@ -6,6 +6,33 @@ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase { /** + * Current screen. + * + * @var mixed + */ + protected $current_screen; + + /** + * Set up. Workaround set_current_screen( null ) not working due to $hook_suffix not being set. + */ + function setUp() { + global $current_screen; + $this->current_screen = $current_screen; + parent::setUp(); + } + + /** + * Tear down. Workaround set_current_screen( null ) not working due to $hook_suffix not being set. + */ + function tearDown() { + global $current_screen; + parent::tearDown(); + $current_screen = $this->current_screen; + } + + /** + * Test search returns results for pages. + * * @ticket 27042 */ public function test_search_returns_results_for_pages() { @@ -26,4 +53,31 @@ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase { $results = explode( "\n", trim( $output ) ); $this->assertCount( 3, $results ); } + + /** + * Test that search only returns results for publihed posts. + * + * @ticket 33742 + */ + public function test_search_returns_results_for_published_posts() { + require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; + + // This will make sure that WP_Query sets is_admin to true. + set_current_screen( 'nav-menu.php' ); + + self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'Publish', 'post_content' => 'FOO' ) ); + self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'draft', 'post_title' => 'Draft', 'post_content' => 'FOO' ) ); + self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'pending', 'post_title' => 'Pending', 'post_content' => 'FOO' ) ); + self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'future', 'post_title' => 'Future', 'post_content' => 'FOO', 'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '+1 month' ) ) ) ); + + $request = array( + 'type' => 'quick-search-posttype-post', + 'q' => 'FOO', + ); + $output = get_echo( '_wp_ajax_menu_quick_search', array( $request ) ); + + $this->assertNotEmpty( $output ); + $results = explode( "\n", trim( $output ) ); + $this->assertCount( 1, $results ); + } }