← All changes
|
includes/classes/Indexable/Term/QueryIntegration.php
+48
-12
4.4.0
→
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 | } |
| @@ -29,11 +29,23 @@ | ||
| 29 | 29 | * @since 3.1 |
| 30 | 30 | * @since 3.6.0 Added $indexable_slug |
| 31 | 31 | */ |
| 32 | 32 | public function __construct( $indexable_slug = 'term' ) { |
| 33 | + /** | |
| 34 | + * Filter whether to enable query integration during indexing | |
| 35 | + * | |
| 36 | + * @since 4.5.2 | |
| 37 | + * @hook ep_enable_query_integration_during_indexing | |
| 38 | + * | |
| 39 | + * @param {bool} $enable To allow query integration during indexing | |
| 40 | + * @param {string} $indexable_slug Indexable slug | |
| 41 | + * @return {bool} New value | |
| 42 | + */ | |
| 43 | + $allow_query_integration_during_indexing = apply_filters( 'ep_enable_query_integration_during_indexing', false, $indexable_slug ); | |
| 44 | + | |
| 33 | 45 | // Ensure that we are currently allowing ElasticPress to override the normal WP_Query |
| 34 | 46 | // Indexable->is_full_reindexing() is not available at this point yet, so using the IndexHelper version of it. |
| 35 | - if ( \ElasticPress\IndexHelper::factory()->is_full_reindexing( $indexable_slug, get_current_blog_id() ) ) { | |
| 47 | + if ( \ElasticPress\IndexHelper::factory()->is_full_reindexing( $indexable_slug, get_current_blog_id() ) && ! $allow_query_integration_during_indexing ) { | |
| 36 | 48 | return; |
| 37 | 49 | } |
| 38 | 50 | |
| 39 | 51 | // Add header |
| @@ -40,8 +52,10 @@ | ||
| 40 | 52 | add_action( 'pre_get_terms', array( $this, 'action_pre_get_terms' ), 5 ); |
| 41 | 53 | |
| 42 | 54 | // Filter term query |
| 43 | 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 ); | |
| 44 | 58 | } |
| 45 | 59 | |
| 46 | 60 | /** |
| 47 | 61 | * Add EP header |
| @@ -93,17 +107,19 @@ | ||
| 93 | 107 | $site__in = []; |
| 94 | 108 | $site__not_in = []; |
| 95 | 109 | |
| 96 | 110 | if ( ! empty( $query->query_vars['sites'] ) ) { |
| 97 | - | |
| 98 | 111 | _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); |
| 99 | - $site__in = (array) $query->query_vars['sites']; | |
| 100 | - $scope = 'all' === $query->query_vars['sites'] ? 'all' : $site__in; | |
| 101 | 112 | } |
| 102 | 113 | |
| 103 | - if ( ! empty( $query->query_vars['site__in'] ) ) { | |
| 104 | - $site__in = (array) $query->query_vars['site__in']; | |
| 105 | - $scope = 'all' === $query->query_vars['site__in'] ? 'all' : $site__in; | |
| 114 | + if ( ! empty( $query->query_vars['site__in'] ) || ! empty( $query->query_vars['sites'] ) ) { | |
| 115 | + $site__in = ! empty( $query->query_vars['site__in'] ) ? (array) $query->query_vars['site__in'] : (array) $query->query_vars['sites']; | |
| 116 | + | |
| 117 | + if ( in_array( 'all', $site__in, true ) ) { | |
| 118 | + $scope = 'all'; | |
| 119 | + } elseif ( in_array( 'current', $site__in, true ) ) { | |
| 120 | + $site__in = (array) get_current_blog_id(); | |
| 121 | + } | |
| 106 | 122 | } |
| 107 | 123 | |
| 108 | 124 | if ( ! empty( $query->query_vars['site__not_in'] ) ) { |
| 109 | 125 | $site__not_in = (array) $query->query_vars['site__not_in']; |
| @@ -136,9 +152,9 @@ | ||
| 136 | 152 | |
| 137 | 153 | $index = implode( ',', $index ); |
| 138 | 154 | } elseif ( ! empty( $site__not_in ) ) { |
| 139 | 155 | |
| 140 | - $sites = get_sites( | |
| 156 | + $sites = \get_sites( | |
| 141 | 157 | array( |
| 142 | 158 | 'fields' => 'ids', |
| 143 | 159 | 'site__not_in' => $site__not_in, |
| 144 | 160 | ) |
| @@ -227,8 +243,29 @@ | ||
| 227 | 243 | return $new_terms; |
| 228 | 244 | } |
| 229 | 245 | |
| 230 | 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 | + /** | |
| 231 | 268 | * Format the ES hits/results as term objects. |
| 232 | 269 | * |
| 233 | 270 | * @param array $terms The terms that should be formatted. |
| 234 | 271 | * @param array $new_terms Array of terms from cache. |
| @@ -381,6 +418,5 @@ | ||
| 381 | 418 | |
| 382 | 419 | $indexable_taxonomies = Indexables::factory()->get( 'term' )->get_indexable_taxonomies(); |
| 383 | 420 | return empty( array_diff( $taxonomies, $indexable_taxonomies ) ); |
| 384 | 421 | } |
| 385 | - | |
| 386 | 422 | } |