PluginProbe
ElasticPress / 4.7.2
ElasticPress v4.7.2
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.7.2, at includes/classes/Indexable/User/QueryIntegration.php

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