WordPress.org

Make WordPress Core

Changeset 38584


Ignore:
Timestamp:
09/09/2016 04:59:01 AM (23 months ago)
Author:
westonruter
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/nav-menu.php

    r38470 r38584  
    7171    } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
    7272        if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
    73             $search_results_query = new WP_Query( array(
     73            $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
     74            $args = array(
    7475                'no_found_rows'          => true,
    7576                'update_post_meta_cache' => false,
     
    7879                'post_type'              => $matches[2],
    7980                's'                      => $query,
    80             ) );
     81            );
     82            if ( isset( $post_type_obj->_default_query ) ) {
     83                $args = array_merge( $args, (array) $post_type_obj->_default_query );
     84            }
     85            $search_results_query = new WP_Query( $args );
    8186            if ( ! $search_results_query->have_posts() ) {
    8287                return;
  • trunk/tests/phpunit/tests/menu/wpAjaxMenuQuickSearch.php

    r37881 r38584  
    77
    88    /**
     9     * Current screen.
     10     *
     11     * @var mixed
     12     */
     13    protected $current_screen;
     14
     15    /**
     16     * Set up. Workaround set_current_screen( null ) not working due to $hook_suffix not being set.
     17     */
     18    function setUp() {
     19        global $current_screen;
     20        $this->current_screen = $current_screen;
     21        parent::setUp();
     22    }
     23
     24    /**
     25     * Tear down. Workaround set_current_screen( null ) not working due to $hook_suffix not being set.
     26     */
     27    function tearDown() {
     28        global $current_screen;
     29        parent::tearDown();
     30        $current_screen = $this->current_screen;
     31    }
     32
     33    /**
     34     * Test search returns results for pages.
     35     *
    936     * @ticket 27042
    1037     */
     
    2754        $this->assertCount( 3, $results );
    2855    }
     56
     57    /**
     58     * Test that search only returns results for publihed posts.
     59     *
     60     * @ticket 33742
     61     */
     62    public function test_search_returns_results_for_published_posts() {
     63        require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
     64
     65        // This will make sure that WP_Query sets is_admin to true.
     66        set_current_screen( 'nav-menu.php' );
     67
     68        self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'Publish', 'post_content' => 'FOO' ) );
     69        self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'draft', 'post_title' => 'Draft', 'post_content' => 'FOO' ) );
     70        self::factory()->post->create( array( 'post_type' => 'post', 'post_status' => 'pending', 'post_title' => 'Pending', 'post_content' => 'FOO' ) );
     71        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' ) ) ) );
     72
     73        $request = array(
     74            'type' => 'quick-search-posttype-post',
     75            'q' => 'FOO',
     76        );
     77        $output = get_echo( '_wp_ajax_menu_quick_search', array( $request ) );
     78
     79        $this->assertNotEmpty( $output );
     80        $results = explode( "\n", trim( $output ) );
     81        $this->assertCount( 1, $results );
     82    }
    2983}
Note: See TracChangeset for help on using the changeset viewer.