Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 104 additions & 12 deletions src/wp-admin/includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Prints the appropriate response to a menu quick search.
*
* @since 3.0.0
* @since 7.2.0 Results now include an exact post/term slug match, if any, ranked first.
*
* @param array $request The unsanitized request values.
*/
Expand Down Expand Up @@ -116,14 +117,33 @@ function _wp_ajax_menu_quick_search( $request = array() ) {
$args = array_merge( $args, (array) $post_type_obj->_default_query );
}

$search_results_query = new WP_Query( $args );
if ( ! $search_results_query->have_posts() ) {
return;
$exact_match_post = null;
if ( '' !== trim( $query ) ) {
$exact_match_post = get_page_by_path( trim( $query ), OBJECT, array( $matches[2] ) );
if ( $exact_match_post && 'publish' !== $exact_match_post->post_status ) {
$exact_match_post = null;
}
}

if ( $exact_match_post && isset( $args['posts_per_page'] ) && $args['posts_per_page'] > 0 ) {
--$args['posts_per_page'];
}

$search_results_query = new WP_Query( $args );

$posts = $exact_match_post ? array( $exact_match_post ) : array();
while ( $search_results_query->have_posts() ) {
$post = $search_results_query->next_post();
if ( ! $exact_match_post || $post->ID !== $exact_match_post->ID ) {
$posts[] = $post;
}
}

if ( ! $posts ) {
return;
}

foreach ( $posts as $post ) {
if ( 'markup' === $response_format ) {
$var_by_ref = $post->ID;
echo walk_nav_menu_tree(
Expand All @@ -143,20 +163,44 @@ function _wp_ajax_menu_quick_search( $request = array() ) {
}
}
} elseif ( 'taxonomy' === $matches[1] ) {
$exact_match_term = null;
if ( '' !== trim( $query ) ) {
$exact_match_terms = get_terms(
array(
'taxonomy' => array( $matches[2] ),
'slug' => trim( $query ),
'number' => 1,
'hide_empty' => false,
)
);
if ( ! is_wp_error( $exact_match_terms ) && ! empty( $exact_match_terms ) ) {
$exact_match_term = reset( $exact_match_terms );
}
}

$terms = get_terms(
array(
'taxonomy' => $matches[2],
'name__like' => $query,
'number' => 10,
'number' => $exact_match_term ? 9 : 10,
'hide_empty' => false,
)
);

if ( empty( $terms ) || is_wp_error( $terms ) ) {
$all_terms = $exact_match_term ? array( $exact_match_term ) : array();
if ( ! is_wp_error( $terms ) ) {
foreach ( (array) $terms as $term ) {
if ( ! $exact_match_term || $term->term_id !== $exact_match_term->term_id ) {
$all_terms[] = $term;
}
}
}

if ( ! $all_terms ) {
return;
}

foreach ( (array) $terms as $term ) {
foreach ( $all_terms as $term ) {
if ( 'markup' === $response_format ) {
echo walk_nav_menu_tree(
array_map( 'wp_setup_nav_menu_item', array( $term ) ),
Expand Down Expand Up @@ -397,6 +441,7 @@ class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' )
* Displays a meta box for a post type menu item.
*
* @since 3.0.0
* @since 7.2.0 The search tab now includes an exact slug match, if any, ranked first.
*
* @global int $_nav_menu_placeholder
* @global int|string $nav_menu_selected_id
Expand Down Expand Up @@ -680,15 +725,35 @@ class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' :
>
<?php
if ( isset( $_REQUEST[ "quick-search-posttype-{$post_type_name}" ] ) ) {
$searched = esc_attr( $_REQUEST[ "quick-search-posttype-{$post_type_name}" ] );
$searched = esc_attr( $_REQUEST[ "quick-search-posttype-{$post_type_name}" ] );
$exact_match_post = null;

if ( '' !== trim( $searched ) ) {
$exact_match_post = get_page_by_path( trim( $searched ), OBJECT, array( $post_type_name ) );
if ( $exact_match_post && 'publish' !== $exact_match_post->post_status ) {
$exact_match_post = null;
}
}

$search_results = get_posts(
array(
's' => $searched,
'post_type' => $post_type_name,
'fields' => 'all',
'order' => 'DESC',
's' => $searched,
'post_type' => $post_type_name,
'fields' => 'all',
'order' => 'DESC',
'numberposts' => $exact_match_post ? 4 : 5,
)
);

if ( $exact_match_post ) {
$search_results = array_filter(
$search_results,
static function ( $post ) use ( $exact_match_post ) {
return $post->ID !== $exact_match_post->ID;
}
);
array_unshift( $search_results, $exact_match_post );
}
} else {
$searched = '';
$search_results = array();
Expand Down Expand Up @@ -845,6 +910,7 @@ class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' )
* Displays a meta box for a taxonomy menu item.
*
* @since 3.0.0
* @since 7.2.0 The search tab now includes an exact slug match, if any, ranked first.
*
* @global int|string $nav_menu_selected_id
*
Expand Down Expand Up @@ -1054,7 +1120,23 @@ class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' :
role="region" aria-label="<?php echo esc_attr( $taxonomy->labels->search_items ); ?>" tabindex="0">
<?php
if ( isset( $_REQUEST[ "quick-search-taxonomy-{$taxonomy_name}" ] ) ) {
$searched = esc_attr( $_REQUEST[ "quick-search-taxonomy-{$taxonomy_name}" ] );
$searched = esc_attr( $_REQUEST[ "quick-search-taxonomy-{$taxonomy_name}" ] );
$exact_match_term = null;

if ( '' !== trim( $searched ) ) {
$exact_match_terms = get_terms(
array(
'taxonomy' => array( $taxonomy_name ),
'slug' => trim( $searched ),
'number' => 1,
'hide_empty' => false,
)
);
if ( ! is_wp_error( $exact_match_terms ) && ! empty( $exact_match_terms ) ) {
$exact_match_term = reset( $exact_match_terms );
}
}

$search_results = get_terms(
array(
'taxonomy' => $taxonomy_name,
Expand All @@ -1065,6 +1147,16 @@ class="tabs-panel <?php echo ( 'search' === $current_tab ? 'tabs-panel-active' :
'hierarchical' => false,
)
);

if ( $exact_match_term && ! is_wp_error( $search_results ) ) {
$search_results = array_filter(
$search_results,
static function ( $term ) use ( $exact_match_term ) {
return $term->term_id !== $exact_match_term->term_id;
}
);
array_unshift( $search_results, $exact_match_term );
}
} else {
$searched = '';
$search_results = array();
Expand Down
68 changes: 65 additions & 3 deletions src/wp-includes/class-wp-customize-nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ public function ajax_search_available_items() {
* Based on WP_Editor::wp_link_query().
*
* @since 4.3.0
* @since 7.2.0 Results now include an exact post/term slug match, if any, ranked first.
*
* @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
* @return array Menu items.
Expand Down Expand Up @@ -397,9 +398,35 @@ public function search_available_items_query( $args = array() ) {
$posts = array_merge( $posts, $stub_post_query->posts );
}

// Look up an exact post slug match, if any, to disambiguate identically-titled items.
$exact_match_post = null;
if ( 1 === $args['pagenum'] && isset( $args['s'] ) && '' !== trim( $args['s'] ) ) {
$exact_match_post = get_page_by_path( trim( $args['s'] ), OBJECT, array_keys( $post_type_objects ) );
if ( $exact_match_post && 'publish' !== $exact_match_post->post_status ) {
$exact_match_post = null;
}
}

// Query posts.
$get_posts = new WP_Query( $query );
$posts = array_merge( $posts, $get_posts->posts );
$get_posts = new WP_Query( $query );
$fuzzy_posts = $get_posts->posts;

if ( $exact_match_post ) {
$fuzzy_posts = array_values(
array_filter(
$fuzzy_posts,
static function ( $post ) use ( $exact_match_post ) {
return $post->ID !== $exact_match_post->ID;
}
)
);
$fuzzy_posts = array_merge( array( $exact_match_post ), $fuzzy_posts );
if ( count( $fuzzy_posts ) > $query['posts_per_page'] ) {
$fuzzy_posts = array_slice( $fuzzy_posts, 0, $query['posts_per_page'] );
}
}

$posts = array_merge( $posts, $fuzzy_posts );

// Create items for posts.
foreach ( $posts as $post ) {
Expand Down Expand Up @@ -428,7 +455,23 @@ public function search_available_items_query( $args = array() ) {

// Query taxonomy terms.
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' );
$terms = get_terms(

// Look up an exact term slug match, if any, to disambiguate identically-named items.
$exact_match_terms = array();
if ( 1 === $args['pagenum'] && isset( $args['s'] ) && '' !== trim( $args['s'] ) ) {
$found_terms = get_terms(
array(
'taxonomy' => $taxonomies,
'slug' => trim( $args['s'] ),
'hide_empty' => false,
)
);
if ( ! is_wp_error( $found_terms ) ) {
$exact_match_terms = $found_terms;
}
}

$terms = get_terms(
array(
'taxonomies' => $taxonomies,
'name__like' => $args['s'],
Expand All @@ -438,6 +481,25 @@ public function search_available_items_query( $args = array() ) {
)
);

if ( ! empty( $exact_match_terms ) ) {
$exact_match_term_ids = wp_list_pluck( $exact_match_terms, 'term_id' );
$fuzzy_terms = is_array( $terms )
? array_values(
array_filter(
$terms,
static function ( $term ) use ( $exact_match_term_ids ) {
return ! in_array( $term->term_id, $exact_match_term_ids, true );
}
)
)
: array();

$terms = array_merge( $exact_match_terms, $fuzzy_terms );
if ( count( $terms ) > 20 ) {
$terms = array_slice( $terms, 0, 20 );
}
}

// Check if any taxonomies were found.
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
Expand Down
Loading
Loading