PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1.4
Jetpack – WP Security, Backup, Speed, & Growth v7.1.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sync / class.jetpack-sync-module-users.php
class.jetpack-sync-module-users.php
428 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module {
4 const MAX_INITIAL_SYNC_USERS = 100;
5
6 protected $flags = array();
7
8 function name() {
9 return 'users';
10 }
11
12 // this is here to support the backfill API
13 public function get_object_by_id( $object_type, $id ) {
14 if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) {
15 return $this->sanitize_user_and_expand( $user );
16 }
17
18 return false;
19 }
20
21 public function init_listeners( $callable ) {
22
23 // users
24 add_action( 'user_register', array( $this, 'user_register_handler' ) );
25 add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
26
27 add_action( 'add_user_to_blog', array( $this, 'add_user_to_blog_handler' ) );
28 add_action( 'jetpack_sync_add_user', $callable, 10, 2 );
29 add_action( 'jetpack_sync_add_user', array( $this, 'clear_flags' ), 11 );
30
31 add_action( 'jetpack_sync_register_user', $callable, 10, 2 );
32 add_action( 'jetpack_sync_register_user', array( $this, 'clear_flags' ), 11 );
33
34 add_action( 'jetpack_sync_save_user', $callable, 10, 2 );
35 add_action( 'jetpack_sync_save_user', array( $this, 'clear_flags' ), 11 );
36
37 add_action( 'jetpack_sync_user_locale', $callable, 10, 2 );
38 add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 );
39
40 add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 );
41 add_action( 'jetpack_deleted_user', $callable, 10, 3 );
42 add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 );
43 add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 );
44
45 // user roles
46 add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
47 add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
48 add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
49
50 // user capabilities
51 add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
52 add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
53 add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
54
55 // user authentication
56 add_action( 'wp_login', $callable, 10, 2 );
57 add_action( 'wp_logout', $callable, 10, 0 );
58 add_action( 'wp_masterbar_logout', $callable, 10, 0 );
59
60 // Add on init
61 add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_add_user', array( $this, 'expand_action' ) );
62 add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_register_user', array( $this, 'expand_action' ) );
63 add_filter( 'jetpack_sync_before_enqueue_jetpack_sync_save_user', array( $this, 'expand_action' ) );
64 }
65
66 public function init_full_sync_listeners( $callable ) {
67 add_action( 'jetpack_full_sync_users', $callable );
68 }
69
70 public function init_before_send() {
71
72 add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
73 add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
74
75 // full sync
76 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
77 }
78
79 private function get_user( $user ) {
80 if ( is_numeric( $user ) ) {
81 $user = get_user_by( 'id', $user );
82 }
83 if ( $user instanceof WP_User ) {
84 return $user;
85 }
86 return null;
87 }
88
89 public function sanitize_user( $user ) {
90 $user = $this->get_user( $user );
91 // this create a new user object and stops the passing of the object by reference.
92 $user = unserialize( serialize( $user ) );
93
94 if ( is_object( $user ) && is_object( $user->data ) ) {
95 unset( $user->data->user_pass );
96 }
97 return $user;
98 }
99
100 public function expand_user( $user ) {
101 if ( ! is_object( $user ) ) {
102 return null;
103 }
104 $user->allowed_mime_types = get_allowed_mime_types( $user );
105 $user->allcaps = $this->get_real_user_capabilities( $user );
106
107 // Only set the user locale if it is different from the site local
108 if ( get_locale() !== get_user_locale( $user->ID ) ) {
109 $user->locale = get_user_locale( $user->ID );
110 }
111
112 return $user;
113 }
114
115 public function get_real_user_capabilities( $user ) {
116 $user_capabilities = array();
117 if ( is_wp_error( $user ) ) {
118 return $user_capabilities;
119 }
120 foreach ( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) {
121 if ( $user_has_capabilities = user_can( $user, $capability ) ) {
122 $user_capabilities[ $capability ] = true;
123 }
124 }
125 return $user_capabilities;
126 }
127
128 public function sanitize_user_and_expand( $user ) {
129 $user = $this->get_user( $user );
130 $user = $this->expand_user( $user );
131 return $this->sanitize_user( $user );
132 }
133
134 public function expand_action( $args ) {
135 // the first argument is always the user
136 list( $user ) = $args;
137 if ( $user ) {
138 $args[0] = $this->sanitize_user_and_expand( $user );
139 return $args;
140 }
141
142 return false;
143 }
144
145 public function expand_login_username( $args ) {
146 list( $login, $user ) = $args;
147 $user = $this->sanitize_user( $user );
148
149 return array( $login, $user );
150 }
151
152 public function expand_logout_username( $args, $user_id ) {
153 $user = get_userdata( $user_id );
154 $user = $this->sanitize_user( $user );
155
156 $login = '';
157 if ( is_object( $user ) && is_object( $user->data ) ) {
158 $login = $user->data->user_login;
159 }
160 // if we don't have a user here lets not send anything.
161 if ( empty( $login ) ) {
162 return false;
163 }
164
165 return array( $login, $user );
166 }
167
168 public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) {
169 $is_multisite = is_multisite();
170 /**
171 * Fires when a user is deleted on a site
172 *
173 * @since 5.4.0
174 *
175 * @param int $deleted_user_id - ID of the deleted user
176 * @param int $reassigned_user_id - ID of the user the deleted user's posts is reassigned to (if any)
177 * @param bool $is_multisite - Whether this site is a multisite installation
178 */
179 do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, $is_multisite );
180 }
181
182 function user_register_handler( $user_id, $old_user_data = null ) {
183 // ensure we only sync users who are members of the current blog
184 if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
185 return;
186 }
187
188 if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) {
189 $this->add_flags( $user_id, array( 'invitation_accepted' => true ) );
190 }
191 /**
192 * Fires when a new user is registered on a site
193 *
194 * @since 4.9.0
195 *
196 * @param object The WP_User object
197 */
198 do_action( 'jetpack_sync_register_user', $user_id, $this->get_flags( $user_id ) );
199
200 }
201
202 function add_user_to_blog_handler( $user_id, $old_user_data = null ) {
203 // ensure we only sync users who are members of the current blog
204 if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
205 return;
206 }
207
208 if ( Jetpack_Constants::is_true( 'JETPACK_INVITE_ACCEPTED' ) ) {
209 $this->add_flags( $user_id, array( 'invitation_accepted' => true ) );
210 }
211 /**
212 * Fires when a user is added on a site
213 *
214 * @since 4.9.0
215 *
216 * @param object The WP_User object
217 */
218 do_action( 'jetpack_sync_add_user', $user_id, $this->get_flags( $user_id ) );
219 }
220
221 function save_user_handler( $user_id, $old_user_data = null ) {
222 // ensure we only sync users who are members of the current blog
223 if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
224 return;
225 }
226
227 $user = get_user_by( 'id', $user_id );
228
229 // Older versions of WP don't pass the old_user_data in ->data
230 if ( isset( $old_user_data->data ) ) {
231 $old_user = $old_user_data->data;
232 } else {
233 $old_user = $old_user_data;
234 }
235
236 if ( $old_user !== null && $user->user_pass !== $old_user->user_pass ) {
237 $this->flags[ $user_id ]['password_changed'] = true;
238 }
239 if ( $old_user !== null && $user->data->user_email !== $old_user->user_email ) {
240 // The '_new_email' user meta is deleted right after the call to wp_update_user
241 // that got us to this point so if it's still set then this was a user confirming
242 // their new email address
243 if ( 1 === intval( get_user_meta( $user->ID, '_new_email', true ) ) ) {
244 $this->flags[ $user_id ]['email_changed'] = true;
245 }
246 }
247
248 /**
249 * Fires when the client needs to sync an updated user
250 *
251 * @since 4.2.0
252 *
253 * @param object The WP_User object
254 * @param array state - New since 5.8.0
255 */
256 do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
257 }
258
259 function save_user_role_handler( $user_id, $role, $old_roles = null ) {
260 $this->add_flags(
261 $user_id,
262 array(
263 'role_changed' => true,
264 'previous_role' => $old_roles,
265 )
266 );
267
268 // The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
269 if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
270 return;
271 }
272 /**
273 * This action is documented already in this file
274 */
275 do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
276 }
277
278 function get_flags( $user_id ) {
279 if ( isset( $this->flags[ $user_id ] ) ) {
280 return $this->flags[ $user_id ];
281 }
282 return array();
283 }
284
285 function clear_flags( $user_id ) {
286 if ( isset( $this->flags[ $user_id ] ) ) {
287 unset( $this->flags[ $user_id ] );
288 }
289 }
290
291 function add_flags( $user_id, $flags ) {
292 $this->flags[ $user_id ] = wp_parse_args( $flags, $this->get_flags( $user_id ) );
293 }
294
295 function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
296 if ( $meta_key === 'locale' ) {
297 $this->add_flags( $user_id, array( 'locale_changed' => true ) );
298 }
299
300 $user = get_user_by( 'id', $user_id );
301 if ( isset( $user->cap_key ) && $meta_key === $user->cap_key ) {
302 $this->add_flags( $user_id, array( 'capabilities_changed' => true ) );
303 }
304
305 if ( $this->is_create_user() || $this->is_add_user_to_blog() || $this->is_delete_user() ) {
306 return;
307 }
308
309 if ( isset( $this->flags[ $user_id ] ) ) {
310 /**
311 * This action is documented already in this file
312 */
313 do_action( 'jetpack_sync_save_user', $user_id, $this->get_flags( $user_id ) );
314 }
315 }
316
317 public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
318 global $wpdb;
319
320 return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->usermeta, 'user_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state );
321 }
322
323 public function estimate_full_sync_actions( $config ) {
324 global $wpdb;
325
326 $query = "SELECT count(*) FROM $wpdb->usermeta";
327
328 if ( $where_sql = $this->get_where_sql( $config ) ) {
329 $query .= ' WHERE ' . $where_sql;
330 }
331
332 $count = $wpdb->get_var( $query );
333
334 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
335 }
336
337 private function get_where_sql( $config ) {
338 global $wpdb;
339
340 $query = "meta_key = '{$wpdb->prefix}capabilities'";
341
342 // config is a list of user IDs to sync
343 if ( is_array( $config ) ) {
344 $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
345 }
346
347 return $query;
348 }
349
350 function get_full_sync_actions() {
351 return array( 'jetpack_full_sync_users' );
352 }
353
354 function get_initial_sync_user_config() {
355 global $wpdb;
356
357 $user_ids = $wpdb->get_col( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}user_level' AND meta_value > 0 LIMIT " . ( self::MAX_INITIAL_SYNC_USERS + 1 ) );
358
359 if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
360 return $user_ids;
361 } else {
362 return false;
363 }
364 }
365
366 public function expand_users( $args ) {
367 $user_ids = $args[0];
368
369 return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
370 }
371
372 public function remove_user_from_blog_handler( $user_id, $blog_id ) {
373 // User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114
374 if ( $this->is_add_new_user_to_blog() ) {
375 return;
376 }
377
378 $reassigned_user_id = $this->get_reassigned_network_user_id();
379
380 // Note that we are in the context of the blog the user is removed from, see https://github.com/WordPress/WordPress/blob/473e1ba73bc5c18c72d7f288447503713d518790/wp-includes/ms-functions.php#L233
381 /**
382 * Fires when a user is removed from a blog on a multisite installation
383 *
384 * @since 5.4.0
385 *
386 * @param int $user_id - ID of the removed user
387 * @param int $reassigned_user_id - ID of the user the removed user's posts is reassigned to (if any)
388 */
389 do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id );
390 }
391
392 protected function is_add_new_user_to_blog() {
393 return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' );
394 }
395
396 protected function is_add_user_to_blog() {
397 return Jetpack::is_function_in_backtrace( 'add_user_to_blog' );
398 }
399
400 protected function is_delete_user() {
401 return Jetpack::is_function_in_backtrace( array( 'wp_delete_user', 'remove_user_from_blog' ) );
402 }
403
404 protected function is_create_user() {
405 $functions = array(
406 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site
407 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site
408 'wp_insert_user', // Used to suppress jetpack_sync_save_user in save_user_cap_handler and save_user_role_handler when user registered on single site
409 );
410
411 return Jetpack::is_function_in_backtrace( $functions );
412 }
413
414 protected function get_reassigned_network_user_id() {
415 $backtrace = debug_backtrace( false ); // phpcs:ignore PHPCompatibility.PHP.NewFunctionParameters.debug_backtrace_optionsFound
416 foreach ( $backtrace as $call ) {
417 if (
418 'remove_user_from_blog' === $call['function'] &&
419 3 === count( $call['args'] )
420 ) {
421 return $call['args'][2];
422 }
423 }
424
425 return false;
426 }
427 }
428