Skip to content

Commit

Permalink
Merge pull request #3869 from 10up/fix/issue-3699
Browse files Browse the repository at this point in the history
Major refactor of the Term::format_args() method and conditionally set search fields for term queries in REST API requests.
  • Loading branch information
felipeelia committed Mar 20, 2024
2 parents 494aeb8 + 3c46db3 commit 2789e94
Show file tree
Hide file tree
Showing 3 changed files with 665 additions and 446 deletions.
23 changes: 23 additions & 0 deletions includes/classes/Indexable/Term/QueryIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function __construct( $indexable_slug = 'term' ) {

// Filter term query
add_filter( 'terms_pre_query', [ $this, 'maybe_filter_query' ], 10, 2 );

add_filter( 'rest_post_tag_query', [ $this, 'maybe_set_search_fields' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -241,6 +243,27 @@ public function maybe_filter_query( $results, WP_Term_Query $query ) {
return $new_terms;
}

/**
* Conditionally set search fields for term queries in REST API requests.
*
* If in an REST API request that came from WordPress edit screen, do not search for term description.
*
* @param array $prepared_args Array of arguments for get_terms().
* @param WP_REST_Request $request The REST API request.
* @return array
*/
public function maybe_set_search_fields( $prepared_args, $request ) {
$referer = $request->get_header( 'referer' );

if ( empty( $referer ) || ! preg_match( '/post\.php\?post=([0-9]*)&action=edit/', $referer ) ) {
return $prepared_args;
}

$prepared_args['search_fields'] = [ 'name', 'slug' ];

return $prepared_args;
}

/**
* Format the ES hits/results as term objects.
*
Expand Down
Loading

0 comments on commit 2789e94

Please sign in to comment.