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

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