← All changes
|
includes/classes/Indexable/Term/QueryIntegration.php
+95
-9
4.3.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 |
| @@ -78,8 +92,12 @@ | ||
| 78 | 92 | if ( ! $indexable->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_term_query_integration', false, $query ) ) { |
| 79 | 93 | return $results; |
| 80 | 94 | } |
| 81 | 95 | |
| 96 | + if ( ! $this->is_searchable( $query ) ) { | |
| 97 | + return $results; | |
| 98 | + } | |
| 99 | + | |
| 82 | 100 | $new_terms = apply_filters( 'ep_wp_query_cached_terms', null, $query ); |
| 83 | 101 | |
| 84 | 102 | if ( null === $new_terms ) { |
| 85 | 103 | $formatted_args = $indexable->format_args( $query->query_vars ); |
| @@ -84,12 +102,30 @@ | ||
| 84 | 102 | if ( null === $new_terms ) { |
| 85 | 103 | $formatted_args = $indexable->format_args( $query->query_vars ); |
| 86 | 104 | |
| 87 | 105 | $scope = 'current'; |
| 106 | + | |
| 107 | + $site__in = []; | |
| 108 | + $site__not_in = []; | |
| 109 | + | |
| 88 | 110 | if ( ! empty( $query->query_vars['sites'] ) ) { |
| 89 | - $scope = $query->query_vars['sites']; | |
| 111 | + _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) ); | |
| 90 | 112 | } |
| 91 | 113 | |
| 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 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( ! empty( $query->query_vars['site__not_in'] ) ) { | |
| 125 | + $site__not_in = (array) $query->query_vars['site__not_in']; | |
| 126 | + } | |
| 127 | + | |
| 92 | 128 | /** |
| 93 | 129 | * Filter search scope |
| 94 | 130 | * |
| 95 | 131 | * @since 3.1 |
| @@ -106,18 +142,31 @@ | ||
| 106 | 142 | $index = null; |
| 107 | 143 | |
| 108 | 144 | if ( 'all' === $scope ) { |
| 109 | 145 | $index = $indexable->get_network_alias(); |
| 110 | - } elseif ( is_numeric( $scope ) ) { | |
| 111 | - $index = $indexable->get_index_name( (int) $scope ); | |
| 112 | - } elseif ( is_array( $scope ) ) { | |
| 146 | + } elseif ( ! empty( $site__in ) ) { | |
| 113 | 147 | $index = []; |
| 114 | 148 | |
| 115 | - foreach ( $scope as $site_id ) { | |
| 149 | + foreach ( $site__in as $site_id ) { | |
| 116 | 150 | $index[] = $indexable->get_index_name( $site_id ); |
| 117 | 151 | } |
| 118 | 152 | |
| 119 | 153 | $index = implode( ',', $index ); |
| 154 | + } elseif ( ! empty( $site__not_in ) ) { | |
| 155 | + | |
| 156 | + $sites = \get_sites( | |
| 157 | + array( | |
| 158 | + 'fields' => 'ids', | |
| 159 | + 'site__not_in' => $site__not_in, | |
| 160 | + ) | |
| 161 | + ); | |
| 162 | + foreach ( $sites as $site_id ) { | |
| 163 | + if ( ! Utils\is_site_indexable( $site_id ) ) { | |
| 164 | + continue; | |
| 165 | + } | |
| 166 | + $index[] = Indexables::factory()->get( 'term' )->get_index_name( $site_id ); | |
| 167 | + } | |
| 168 | + $index = implode( ',', $index ); | |
| 120 | 169 | } |
| 121 | 170 | |
| 122 | 171 | $ep_query = $indexable->query_es( $formatted_args, $query->query_vars, $index ); |
| 123 | 172 | |
| @@ -194,8 +243,29 @@ | ||
| 194 | 243 | return $new_terms; |
| 195 | 244 | } |
| 196 | 245 | |
| 197 | 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 | + /** | |
| 198 | 268 | * Format the ES hits/results as term objects. |
| 199 | 269 | * |
| 200 | 270 | * @param array $terms The terms that should be formatted. |
| 201 | 271 | * @param array $new_terms Array of terms from cache. |
| @@ -332,5 +402,21 @@ | ||
| 332 | 402 | |
| 333 | 403 | return $new_terms; |
| 334 | 404 | } |
| 335 | 405 | |
| 406 | + /** | |
| 407 | + * Determine whether ES should be used for the query if all taxonomies are indexable. | |
| 408 | + * | |
| 409 | + * @param \WP_Term_Query $query The WP_Term_Query object. | |
| 410 | + * @return boolean | |
| 411 | + */ | |
| 412 | + protected function is_searchable( $query ) { | |
| 413 | + | |
| 414 | + $taxonomies = $query->query_vars['taxonomy']; | |
| 415 | + if ( ! $taxonomies ) { | |
| 416 | + return true; | |
| 417 | + } | |
| 418 | + | |
| 419 | + $indexable_taxonomies = Indexables::factory()->get( 'term' )->get_indexable_taxonomies(); | |
| 420 | + return empty( array_diff( $taxonomies, $indexable_taxonomies ) ); | |
| 421 | + } | |
| 336 | 422 | } |