PluginProbe
ElasticPress / 4.6.0
ElasticPress v4.6.0
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
elasticpress / includes / classes / Indexable / User / QueryIntegration.php

QueryIntegration.php in ElasticPress 4.6.0, at includes/classes/Indexable/User/QueryIntegration.php

227 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Integrate with WP_User_Query
4 *
5 * @since 1.0
6 * @package elasticpress
7 */
8
9 namespace ElasticPress\Indexable\User;
10
11 use ElasticPress\Indexables as Indexables;
12 use \WP_User_Query as WP_User_Query;
13 use ElasticPress\Utils as Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Query integration class
21 */
22 class QueryIntegration {
23
24 /**
25 * Checks to see if we should be integrating and if so, sets up the appropriate actions and filters.
26 *
27 * @param string $indexable_slug Indexable slug. Optional.
28 *
29 * @since 0.9
30 * @since 3.6.0 Added $indexable_slug
31 */
32 public function __construct( $indexable_slug = 'user' ) {
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
45 // Ensure that we are currently allowing ElasticPress to override the normal WP_Query
46 // Indexable->is_full_reindexing() is not available at this point yet, so using the IndexHelper version of it.
47 if ( \ElasticPress\IndexHelper::factory()->is_full_reindexing( $indexable_slug ) && ! $allow_query_integration_during_indexing ) {
48 return;
49 }
50
51 add_filter( 'users_pre_query', [ $this, 'maybe_filter_query' ], 10, 2 );
52
53 // Add header
54 add_action( 'pre_get_users', array( $this, 'action_pre_get_users' ), 5 );
55 }
56
57 /**
58 * If WP_User_Query meets certain conditions, query results from ES
59 *
60 * @param array $results Users array.
61 * @param WP_User_Query $query Current query.
62 * @since 3.0
63 * @return array
64 */
65 public function maybe_filter_query( $results, WP_User_Query $query ) {
66 $user_indexable = Indexables::factory()->get( 'user' );
67
68 /**
69 * Filter to skip user query integration
70 *
71 * @hook ep_skip_user_query_integration
72 * @param {bool} $skip True meanas skip query
73 * @param {WP_User_Query} $query User query
74 * @since 3.0
75 * @return {boolean} New value
76 */
77 if ( ! $user_indexable->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_user_query_integration', false, $query ) ) {
78 return $results;
79 }
80
81 /**
82 * Filter cached user query users
83 *
84 * @hook ep_wp_query_search_cached_users
85 * @param {array} $users Array of users
86 * @param {WP_User_Query} $query User query
87 * @since 3.0
88 * @return {array} New users
89 */
90 $new_users = apply_filters( 'ep_wp_query_search_cached_users', null, $query );
91
92 if ( null === $new_users ) {
93 $formatted_args = $user_indexable->format_args( $query->query_vars, $query );
94
95 $ep_query = $user_indexable->query_es( $formatted_args, $query->query_vars, null, $query );
96
97 if ( false === $ep_query ) {
98 return $results;
99 }
100
101 /**
102 * WP_User_Query does not let us set this property:
103 *
104 * $query->elasticsearch_success = true;
105 */
106 $query->query_vars['elasticsearch_success'] = true;
107
108 $fields = $query->get( 'fields' );
109 $new_users = [];
110
111 if ( in_array( $fields, [ 'all', 'all_with_meta' ], true ) ) {
112 foreach ( $ep_query['documents'] as $document ) {
113 $new_users[] = $document['ID'];
114 }
115 } elseif ( is_array( $fields ) ) {
116 // WP_User_Query returns a stdClass.
117 foreach ( $ep_query['documents'] as $document ) {
118
119 $user = new \stdClass();
120 $user->elasticsearch = true; // Super useful for debugging.
121
122 foreach ( $fields as $field ) {
123 if ( 'id' === $field ) {
124 $field = 'ID';
125 }
126 $user->$field = $document[ $field ];
127 }
128
129 $new_users[] = $user;
130 }
131 } elseif ( is_string( $fields ) && ! empty( $fields ) ) {
132 foreach ( $ep_query['documents'] as $document ) {
133 $new_users[] = $document[ $fields ];
134 }
135 } else {
136 $new_users = $this->format_hits_as_users( $ep_query['documents'] );
137 }
138 }
139
140 $query->total_users = is_array( $ep_query['found_documents'] ) ? $ep_query['found_documents']['value'] : $ep_query['found_documents']; // 7.0+ have this as an array rather than int;
141
142 return $new_users;
143 }
144
145 /**
146 * Format the ES hits/results as WP_User objects.
147 *
148 * @param array $users The users that should be formatted.
149 * @since 3.0
150 * @return array
151 */
152 protected function format_hits_as_users( $users ) {
153 $new_users = [];
154
155 foreach ( $users as $user_array ) {
156 $user = new \stdClass();
157
158 /**
159 * Filter arguments inserted into user object after search
160 *
161 * @hook ep_search_user_return_args
162 * @param {array} $args Array of arguments
163 * @since 3.0
164 * @return {array} New arguments
165 */
166 $user_return_args = apply_filters(
167 'ep_search_user_return_args',
168 [
169 'ID',
170 'user_login',
171 'user_nicename',
172 'user_email',
173 'user_url',
174 'user_registered',
175 'user_status',
176 'display_name',
177 'spam',
178 'deleted',
179 'terms',
180 'meta',
181 ]
182 );
183
184 foreach ( $user_return_args as $key ) {
185 if ( isset( $user_array[ $key ] ) ) {
186 $user->$key = $user_array[ $key ];
187 }
188 }
189
190 $user->elasticsearch = true; // Super useful for debugging.
191
192 $new_users[] = $user;
193 }
194
195 return $new_users;
196 }
197
198 /**
199 * Disables cache_results, adds header.
200 *
201 * @param WP_User_Query $query User query
202 * @since 3.0
203 */
204 public function action_pre_get_users( $query ) {
205 /**
206 * Filter to skip user query integration
207 *
208 * @hook ep_skip_user_query_integration
209 * @param {bool} $skip True meanas skip query
210 * @param {WP_User_Query} $query User query
211 * @since 3.0
212 * @return {boolean} New value
213 */
214 if ( ! Indexables::factory()->get( 'user' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_user_query_integration', false, $query ) ) {
215 return;
216 }
217
218 if ( ! headers_sent() ) {
219 /**
220 * Manually setting a header as $wp_query isn't yet initialized
221 * when we call: add_filter('wp_headers', 'filter_wp_headers');
222 */
223 header( 'X-ElasticPress-Search: true' );
224 }
225 }
226 }
227