Menus: Prevent non-published posts/pages from being returned in search results for adding as nav menu items.
Re-use the same query vars in searching as when listing posts. Aligns with behavior of nav menus in customizer. Fixes #33742. Props welcher, westonruter. git-svn-id: https://develop.svn.wordpress.org/trunk@38584 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
e7b058117a
commit
6c8c98fe5b
@ -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) ) {
|
} elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
|
||||||
if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
|
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,
|
'no_found_rows' => true,
|
||||||
'update_post_meta_cache' => false,
|
'update_post_meta_cache' => false,
|
||||||
'update_post_term_cache' => false,
|
'update_post_term_cache' => false,
|
||||||
'posts_per_page' => 10,
|
'posts_per_page' => 10,
|
||||||
'post_type' => $matches[2],
|
'post_type' => $matches[2],
|
||||||
's' => $query,
|
'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() ) {
|
if ( ! $search_results_query->have_posts() ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,33 @@
|
|||||||
class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase {
|
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
|
* @ticket 27042
|
||||||
*/
|
*/
|
||||||
public function test_search_returns_results_for_pages() {
|
public function test_search_returns_results_for_pages() {
|
||||||
@ -26,4 +53,31 @@ class Tests_Menu_WpAjaxMenuQuickSeach extends WP_UnitTestCase {
|
|||||||
$results = explode( "\n", trim( $output ) );
|
$results = explode( "\n", trim( $output ) );
|
||||||
$this->assertCount( 3, $results );
|
$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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user