PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/Indexable/Term/QueryIntegration.php +75 -10 4.3.15.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
@@ -88,12 +102,30 @@
88 102 if ( null === $new_terms ) {
89 103 $formatted_args = $indexable->format_args( $query->query_vars );
90 104
91 105 $scope = 'current';
106 +
107 + $site__in = [];
108 + $site__not_in = [];
109 +
92 110 if ( ! empty( $query->query_vars['sites'] ) ) {
93 - $scope = $query->query_vars['sites'];
111 + _deprecated_argument( __FUNCTION__, '4.4.0', esc_html__( 'sites is deprecated. Use site__in instead.', 'elasticpress' ) );
94 112 }
95 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 +
96 128 /**
97 129 * Filter search scope
98 130 *
99 131 * @since 3.1
@@ -110,18 +142,31 @@
110 142 $index = null;
111 143
112 144 if ( 'all' === $scope ) {
113 145 $index = $indexable->get_network_alias();
114 - } elseif ( is_numeric( $scope ) ) {
115 - $index = $indexable->get_index_name( (int) $scope );
116 - } elseif ( is_array( $scope ) ) {
146 + } elseif ( ! empty( $site__in ) ) {
117 147 $index = [];
118 148
119 - foreach ( $scope as $site_id ) {
149 + foreach ( $site__in as $site_id ) {
120 150 $index[] = $indexable->get_index_name( $site_id );
121 151 }
122 152
123 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 );
124 169 }
125 170
126 171 $ep_query = $indexable->query_es( $formatted_args, $query->query_vars, $index );
127 172
@@ -198,8 +243,29 @@
198 243 return $new_terms;
199 244 }
200 245
201 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 + /**
202 268 * Format the ES hits/results as term objects.
203 269 *
204 270 * @param array $terms The terms that should be formatted.
205 271 * @param array $new_terms Array of terms from cache.
@@ -352,6 +418,5 @@
352 418
353 419 $indexable_taxonomies = Indexables::factory()->get( 'term' )->get_indexable_taxonomies();
354 420 return empty( array_diff( $taxonomies, $indexable_taxonomies ) );
355 421 }
356 -
357 422 }