Completed
Push — add/sync-user-password-changes ( fb1202...73e193 )
by
unknown
08:29
created

Jetpack_Sync_Module_Users::save_user_cap_handler()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 5
nop 4
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module {
4
	const MAX_INITIAL_SYNC_USERS = 100;
5
6
	protected $previous_role = 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
		// users
23
		add_action( 'user_register', array( $this, 'save_user_handler' ) );
24
		add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
25
		add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) );
26
		add_action( 'jetpack_sync_add_user', $callable, 10, 2 );
27
		add_action( 'jetpack_sync_register_user', $callable, 10, 2 );
28
		add_action( 'jetpack_sync_save_user', $callable, 10, 2 );
29
		add_action( 'jetpack_updated_user_password', $callable );
30
31
		//Edit user info, see https://github.com/WordPress/WordPress/blob/c05f1dc805bddcc0e76fd90c4aaf2d9ea76dc0fb/wp-admin/user-edit.php#L126
32
		add_action( 'personal_options_update', array( $this, 'edited_user_handler' ) );
33
		add_action( 'edit_user_profile_update', array( $this, 'edited_user_handler' ) );
34
		add_action( 'jetpack_user_edited', $callable );
35
36
		add_action( 'jetpack_sync_user_locale', $callable, 10, 2 );
37
		add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 );
38
39
		add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 );
40
		add_action( 'jetpack_deleted_user', $callable, 10, 3 );
41
		add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 );
42
		add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 );
43
44
		// user roles
45
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
46
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
47
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
48
49
		// user capabilities
50
		add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
51
		add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
52
		add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
53
54
		// user authentication
55
		add_action( 'wp_login', $callable, 10, 2 );
56
		add_action( 'wp_logout', $callable, 10, 0 );
57
		add_action( 'wp_masterbar_logout', $callable, 10, 0 );
58
	}
59
60
	public function init_full_sync_listeners( $callable ) {
61
		add_action( 'jetpack_full_sync_users', $callable );
62
	}
63
64
	public function init_before_send() {
65
		add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) );
66
		add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) );
67
		add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) );
68
		add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
69
		add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
70
71
		// full sync
72
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
73
	}
74
75
	public function sanitize_user_and_expand( $user ) {
76
		$user = $this->get_user( $user );
77
		$user = $this->add_to_user( $user );
78
		return $this->sanitize_user( $user );
79
	}
80
81
	private function get_user( $user ) {
82
		if ( $user && ! is_object( $user ) && is_numeric( $user ) ) {
83
			$user = get_user_by( 'id', $user );
84
		}
85
		if ( $user instanceof WP_User ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
86
			return $user;
87
		}
88
		return null;
89
	}
90
91
	public function sanitize_user( $user ) {
92
		$user = $this->get_user( $user );
93
		// this create a new user object and stops the passing of the object by reference.
94
		$user = unserialize( serialize( $user ) );
95
96
		if ( is_object( $user ) && is_object( $user->data ) ) {
97
			unset( $user->data->user_pass );
98
		}
99
		if ( $user ) {
100
			$user->allcaps = $this->get_real_user_capabilities( $user );
101
		}
102
		return $user;
103
	}
104
105
	public function add_to_user( $user ) {
106
		if ( ! is_object( $user ) ) {
107
			return null;
108
		}
109
		$user->allowed_mime_types = get_allowed_mime_types( $user );
110
111
		if ( function_exists( 'get_user_locale' ) ) {
112
113
			// Only set the user locale if it is different from the site local
114
			if ( get_locale() !== get_user_locale( $user->ID ) ) {
115
				$user->locale = get_user_locale( $user->ID );
116
			}
117
		}
118
119
		return $user;
120
	}
121
122
	public function get_real_user_capabilities( $user ) {
123
		$user_capabilities = array();
124
		if ( is_wp_error( $user ) ) {
125
			return $user_capabilities;
126
		}
127
		foreach( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) {
128
			if ( $user_has_capabilities = user_can( $user , $capability ) ) {
0 ignored issues
show
Unused Code introduced by
$user_has_capabilities is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129
				$user_capabilities[ $capability ] = true;
130
			}
131
		}
132
		return $user_capabilities;
133
	}
134
135
	public function expand_user( $args ) {
136
		list( $user ) = $args;
137
		if ( $user ) {
138
			if ( isset( $args[1] ) ) { // if state is available also send state.
139
				return array( $this->add_to_user( $user ), $args[1] );
140
			}
141
			return array( $this->add_to_user( $user ) );
142
		}
143
144
		return false;
145
	}
146
147
	public function expand_login_username( $args ) {
148
		list( $login, $user ) = $args;
149
		$user = $this->sanitize_user( $user );
150
151
		return array( $login, $user );
152
	}
153
154
	public function expand_logout_username( $args, $user_id ) {
155
		$user  = get_userdata( $user_id );
156
		$user  = $this->sanitize_user( $user );
157
158
		$login = '';
159
		if ( is_object( $user ) && is_object( $user->data ) ) {
160
			$login = $user->data->user_login;
161
		}
162
		// if we don't have a user here lets not send anything.
163
		if ( empty( $login ) ) {
164
			return false;
165
		}
166
167
		return array( $login, $user );
168
	}
169
170
	public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) {
171
		$is_multisite = is_multisite();
172
		/**
173
		 * Fires when a user is deleted on a site
174
		 *
175
		 * @since 5.4.0
176
		 *
177
		 * @param int $deleted_user_id - ID of the deleted user
178
		 * @param int $reassigned_user_id - ID of the user the deleted user's posts is reassigned to (if any)
179
		 * @param bool $is_multisite - Whether this site is a multisite installation
180
		 */
181
		do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, $is_multisite );
182
	}
183
184
	public function edited_user_handler( $user_id ) {
185
		/**
186
		 * Fires when a user is edited on a site
187
		 *
188
		 * @since 5.4.0
189
		 *
190
		 * @param int $user_id - ID of the edited user
191
		 */
192
		do_action( 'jetpack_user_edited', $user_id );
193
	}
194
195
	function save_user_handler( $user_id, $old_user_data = null ) {
196
		// ensure we only sync users who are members of the current blog
197
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
198
			return;
199
		}
200
		$raw_user = get_user_by( 'id', $user_id );
201
		$user = $this->sanitize_user( $raw_user );
202
		$user_password_changed = false;
203
204
		// Older versions of WP don't pass the old_user_data in ->data
205
		if ( isset( $old_user_data->data ) ) {
206
			$old_user = $old_user_data->data;
207
		} else {
208
			$old_user = $old_user_data;
209
		}
210
211
		$role_changed = isset( $this->previous_role[ $user_id ] ) ? $this->previous_role[ $user_id ] : false;
212
213
		if ( $old_user !== null ) {
214
			if ( $raw_user->user_pass !== $old_user->user_pass ) {
215
				$user_password_changed = true;
216
			}
217
			unset( $old_user->user_pass );
218
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
219
				if ( $user_password_changed ) {
220
					/**
221
					 * Documented already in this file
222
					 * @param array state - New since 5.8.0
223
					 */
224
					do_action( 'jetpack_sync_save_user', $user, array(
225
						'password_changed' => true,
226
						'user_data_changed' => false,
227
						'role_changed' => (bool) $role_changed,
228
						'previous_role' => $role_changed,
229
					) );
230
				}
231
				return;
232
			}
233
		}
234
235
		if ( 'user_register' === current_filter() ) {
236
			/**
237
			 * Fires when a new user is registered on a site
238
			 *
239
			 * @since 4.9.0
240
			 *
241
			 * @param object The WP_User object
242
			 */
243
			do_action( 'jetpack_sync_register_user', $user );
244
245
			return;
246
		}
247
		/* MU Sites add users instead of register them to sites */
248
		if ( 'add_user_to_blog' === current_filter() ) {
249
			/**
250
			 * Fires when a new user is added to a site. (WordPress Multisite)
251
			 *
252
			 * @since 4.9.0
253
			 *
254
			 * @param object The WP_User object
255
			 */
256
			do_action( 'jetpack_sync_add_user', $user );
257
258
			return;
259
		}
260
261
262
263
		/**
264
		 * Fires when the client needs to sync an updated user
265
		 *
266
		 * @since 4.2.0
267
		 *
268
		 * @param object The WP_User object
269
		 * @param array state - New since 5.8.0
270
		 */
271
		do_action( 'jetpack_sync_save_user', $user, array(
272
			'password_changed' => $user_password_changed,
273
			'user_data_changed' => true,
274
			'role_changed' => (bool) $role_changed,
275
			'previous_role' => $role_changed,
276
			) );
277
	}
278
279
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
280
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
281
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
282
			$this->previous_role[ $user_id ] = $old_roles;
283
			return;
284
		}
285
286
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
287
		/**
288
		 * Fires when the client needs to sync an updated user
289
		 *
290
		 * @since 4.2.0
291
		 *
292
		 * @param object The WP_User object
293
	 	 * @param array state
294
		 */
295
		do_action( 'jetpack_sync_save_user', $user, array(
296
			'role_changed' => true,
297
			'previous_role' => $old_roles ) );
298
	}
299
300
	function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
301
		if ( $meta_key === 'locale' ) {
302
			if ( current_filter() === 'deleted_user_meta' ) {
303
				/**
304
				 * Allow listeners to listen for user local delete changes
305
				 *
306
				 * @since 4.8.0
307
				 *
308
				 * @param int $user_id - The ID of the user whos locale is being deleted
309
				 */
310
				do_action( 'jetpack_sync_user_locale_delete', $user_id );
311
			} else {
312
				/**
313
				 * Allow listeners to listen for user local changes
314
				 *
315
				 * @since 4.8.0
316
				 *
317
				 * @param int $user_id - The ID of the user whos locale is being changed
318
				 * @param int $value - The value of the new locale
319
				 */
320
				do_action( 'jetpack_sync_user_locale', $user_id, $value );
321
			}
322
		}
323
		$this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value );
324
	}
325
326
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
327
		// if a user is currently being removed as a member of this blog, we don't fire the event
328
		if ( current_filter() === 'deleted_user_meta' ) {
329
			return;
330
		}
331
332
		// Since we are currently only caring about capabilities at this point don't need to save the user info at this save the user info at this point.
333
		if ( current_filter() === 'added_user_meta' ) {
334
			return;
335
		}
336
337
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
338
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
339
			return;
340
		}
341
		$user = get_user_by( 'id', $user_id );
342
		if ( $meta_key === $user->cap_key  ) {
343
344
			/**
345
			 * Fires when the client needs to sync an updated user
346
			 *
347
			 * @since 4.2.0
348
			 *
349
			 * @param object The Sanitized WP_User object
350
		     * @param array state Since 5.8
351
			 */
352
			do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ),
353
				array( 'capabilities_action' => current_filter(), 'capabilities' => $capabilities )
354
			);
355
		}
356
	}
357
358
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
359
		global $wpdb;
360
361
		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 );
362
	}
363
364
	public function estimate_full_sync_actions( $config ) {
365
		global $wpdb;
366
367
		$query = "SELECT count(*) FROM $wpdb->usermeta";
368
369
		if ( $where_sql = $this->get_where_sql( $config ) ) {
370
			$query .= ' WHERE ' . $where_sql;
371
		}
372
373
		$count = $wpdb->get_var( $query );
374
375
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
376
	}
377
378 View Code Duplication
	private function get_where_sql( $config ) {
379
		global $wpdb;
380
381
		$query = "meta_key = '{$wpdb->prefix}capabilities'";
382
383
		// config is a list of user IDs to sync
384
		if ( is_array( $config ) ) {
385
			$query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
386
		}
387
388
		return $query;
389
	}
390
391
	function get_full_sync_actions() {
392
		return array( 'jetpack_full_sync_users' );
393
	}
394
395
	function get_initial_sync_user_config() {
396
		global $wpdb;
397
398
		$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 ) );
399
400
		if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
401
			return $user_ids;
402
		} else {
403
			return false;
404
		}
405
	}
406
407
	public function expand_users( $args ) {
408
		$user_ids = $args[0];
409
410
		return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
411
	}
412
413
	public function remove_user_from_blog_handler( $user_id, $blog_id ) {
414
		//User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114
415
		if ( $this->is_add_new_user_to_blog() ) {
416
			return;
417
		}
418
419
		$reassigned_user_id = $this->get_reassigned_network_user_id();
420
421
		//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
422
		/**
423
		 * Fires when a user is removed from a blog on a multisite installation
424
		 *
425
		 * @since 5.4.0
426
		 *
427
		 * @param int $user_id - ID of the removed user
428
		 * @param int $reassigned_user_id - ID of the user the removed user's posts is reassigned to (if any)
429
		 */
430
		do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id );
431
	}
432
433
	private function is_add_new_user_to_blog() {
434
		return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' );
435
	}
436
437
	private function is_add_user_to_blog() {
438
		return Jetpack::is_function_in_backtrace( 'add_user_to_blog' );
439
	}
440
441
	private function is_create_user() {
442
		$functions = array(
443
			'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site
444
			'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site
445
			'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
446
		);
447
448
		return Jetpack::is_function_in_backtrace( $functions );
449
	}
450
451
	private function get_reassigned_network_user_id() {
452
		$backtrace = debug_backtrace( false );
453
		foreach ( $backtrace as $call ) {
454
			if (
455
				'remove_user_from_blog' === $call['function'] &&
456
				3 === count( $call['args'] )
457
			) {
458
				return $call['args'][2];
459
			}
460
		}
461
462
		return false;
463
	}
464
}
465