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

214 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
95 $fields = $query->get( 'fields' );
96 $new_users = [];
97
98 if ( 'all_with_meta' === $fields ) {
99 foreach ( $ep_query['documents'] as $document ) {
100 $new_users[] = $document['ID'];
101 }
102 } elseif ( is_array( $fields ) ) {
103 // WP_User_Query returns a stdClass.
104 foreach ( $ep_query['documents'] as $document ) {
105
106 $user = new \stdClass();
107 $user->elasticsearch = true; // Super useful for debugging.
108
109 foreach ( $fields as $field ) {
110 if ( 'id' === $field ) {
111 $field = 'ID';
112 }
113 $user->$field = $document[ $field ];
114 }
115
116 $new_users[] = $user;
117 }
118 } elseif ( is_string( $fields ) && ! empty( $fields ) && 'all' !== $fields ) {
119 foreach ( $ep_query['documents'] as $document ) {
120 $new_users[] = $document[ $fields ];
121 }
122 } else {
123 $new_users = $this->format_hits_as_users( $ep_query['documents'] );
124 }
125 }
126
127 $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;
128
129 return $new_users;
130 }
131
132 /**
133 * Format the ES hits/results as WP_User objects.
134 *
135 * @param array $users The users that should be formatted.
136 * @since 3.0
137 * @return array
138 */
139 protected function format_hits_as_users( $users ) {
140 $new_users = [];
141
142 foreach ( $users as $user_array ) {
143 $user = new \stdClass();
144
145 /**
146 * Filter arguments inserted into user object after search
147 *
148 * @hook ep_search_user_return_args
149 * @param {array} $args Array of arguments
150 * @since 3.0
151 * @return {array} New arguments
152 */
153 $user_return_args = apply_filters(
154 'ep_search_user_return_args',
155 [
156 'ID',
157 'user_login',
158 'user_nicename',
159 'user_email',
160 'user_url',
161 'user_registered',
162 'user_status',
163 'display_name',
164 'spam',
165 'deleted',
166 'terms',
167 'meta',
168 ]
169 );
170
171 foreach ( $user_return_args as $key ) {
172 if ( isset( $user_array[ $key ] ) ) {
173 $user->$key = $user_array[ $key ];
174 }
175 }
176
177 $user->elasticsearch = true; // Super useful for debugging.
178
179 $new_users[] = $user;
180 }
181
182 return $new_users;
183 }
184
185 /**
186 * Disables cache_results, adds header.
187 *
188 * @param WP_User_Query $query User query
189 * @since 3.0
190 */
191 public function action_pre_get_users( $query ) {
192 /**
193 * Filter to skip user query integration
194 *
195 * @hook ep_skip_user_query_integration
196 * @param {bool} $skip True meanas skip query
197 * @param {WP_User_Query} $query User query
198 * @since 3.0
199 * @return {boolean} New value
200 */
201 if ( ! Indexables::factory()->get( 'user' )->elasticpress_enabled( $query ) || apply_filters( 'ep_skip_user_query_integration', false, $query ) ) {
202 return;
203 }
204
205 if ( ! headers_sent() ) {
206 /**
207 * Manually setting a header as $wp_query isn't yet initialized
208 * when we call: add_filter('wp_headers', 'filter_wp_headers');
209 */
210 header( 'X-ElasticPress-Search: true' );
211 }
212 }
213 }
214