← All changes
|
includes/classes/Indexable/Term/QueryIntegration.php
+27
-5
4.5.2
→
5.3.5
View file →
| @@ -7,11 +7,11 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ElasticPress\Indexable\Term; |
| 10 | 10 | |
| 11 | -use ElasticPress\Indexables as Indexables; | |
| 12 | -use \WP_Term_Query as WP_Term_Query; | |
| 13 | -use ElasticPress\Utils as Utils; | |
| 11 | +use WP_Term_Query; | |
| 12 | +use ElasticPress\Indexables; | |
| 13 | +use ElasticPress\Utils; | |
| 14 | 14 | |
| 15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | 16 | exit; // Exit if accessed directly. |
| 17 | 17 | } |
| @@ -52,8 +52,10 @@ | ||
| 52 | 52 | add_action( 'pre_get_terms', array( $this, 'action_pre_get_terms' ), 5 ); |
| 53 | 53 | |
| 54 | 54 | // Filter term query |
| 55 | 55 | add_filter( 'terms_pre_query', [ $this, 'maybe_filter_query' ], 10, 2 ); |
| 56 | + | |
| 57 | + add_filter( 'rest_post_tag_query', [ $this, 'maybe_set_search_fields' ], 10, 2 ); | |
| 56 | 58 | } |
| 57 | 59 | |
| 58 | 60 | /** |
| 59 | 61 | * Add EP header |
| @@ -150,9 +152,9 @@ | ||
| 150 | 152 | |
| 151 | 153 | $index = implode( ',', $index ); |
| 152 | 154 | } elseif ( ! empty( $site__not_in ) ) { |
| 153 | 155 | |
| 154 | - $sites = get_sites( | |
| 156 | + $sites = \get_sites( | |
| 155 | 157 | array( |
| 156 | 158 | 'fields' => 'ids', |
| 157 | 159 | 'site__not_in' => $site__not_in, |
| 158 | 160 | ) |
| @@ -241,8 +243,29 @@ | ||
| 241 | 243 | return $new_terms; |
| 242 | 244 | } |
| 243 | 245 | |
| 244 | 246 | /** |
| 247 | + * Conditionally set search fields for term queries in REST API requests. | |
| 248 | + * | |
| 249 | + * If in an REST API request that came from WordPress edit screen, do not search for term description. | |
| 250 | + * | |
| 251 | + * @param array $prepared_args Array of arguments for get_terms(). | |
| 252 | + * @param WP_REST_Request $request The REST API request. | |
| 253 | + * @return array | |
| 254 | + */ | |
| 255 | + public function maybe_set_search_fields( $prepared_args, $request ) { | |
| 256 | + $referer = $request->get_header( 'referer' ); | |
| 257 | + | |
| 258 | + if ( empty( $referer ) || ! preg_match( '/post\.php\?post=([0-9]*)&action=edit/', $referer ) ) { | |
| 259 | + return $prepared_args; | |
| 260 | + } | |
| 261 | + | |
| 262 | + $prepared_args['search_fields'] = [ 'name', 'slug' ]; | |
| 263 | + | |
| 264 | + return $prepared_args; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 245 | 268 | * Format the ES hits/results as term objects. |
| 246 | 269 | * |
| 247 | 270 | * @param array $terms The terms that should be formatted. |
| 248 | 271 | * @param array $new_terms Array of terms from cache. |
| @@ -395,6 +418,5 @@ | ||
| 395 | 418 | |
| 396 | 419 | $indexable_taxonomies = Indexables::factory()->get( 'term' )->get_indexable_taxonomies(); |
| 397 | 420 | return empty( array_diff( $taxonomies, $indexable_taxonomies ) ); |
| 398 | 421 | } |
| 399 | - | |
| 400 | 422 | } |