Completed
Push — fix/plugin-action-links ( a3870b )
by
unknown
37:01 queued 27:38
created

get_real_user_capabilities()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 9.6666
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
	function name() {
7
		return 'users';
8
	}
9
10
	// this is here to support the backfill API
11
	public function get_object_by_id( $object_type, $id ) {
12
		if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) {
13
			return $this->sanitize_user_and_expand( $user );
14
		}
15
16
		return false;
17
	}
18
19
	public function init_listeners( $callable ) {
20
		// users
21
		add_action( 'user_register', array( $this, 'save_user_handler' ) );
22
		add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 );
23
		add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) );
24
		add_action( 'jetpack_sync_add_user', $callable, 10, 2 );
25
		add_action( 'jetpack_sync_register_user', $callable, 10, 2 );
26
		add_action( 'jetpack_sync_save_user', $callable );
27
28
		//Edit user info, see https://github.com/WordPress/WordPress/blob/c05f1dc805bddcc0e76fd90c4aaf2d9ea76dc0fb/wp-admin/user-edit.php#L126
29
		add_action( 'personal_options_update', array( $this, 'edited_user_handler' ) );
30
		add_action( 'edit_user_profile_update', array( $this, 'edited_user_handler' ) );
31
		add_action( 'jetpack_user_edited', $callable );
32
33
		add_action( 'jetpack_sync_user_locale', $callable, 10, 2 );
34
		add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 );
35
36
		add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 );
37
		add_action( 'jetpack_deleted_user', $callable, 10, 3 );
38
		add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 );
39
		add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 );
40
41
		// user roles
42
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
43
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
44
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
45
46
		// user capabilities
47
		add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
48
		add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
49
		add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
50
51
		// user authentication
52
		add_action( 'wp_login', $callable, 10, 2 );
53
		add_action( 'wp_login_failed', $callable, 10, 2 );
54
		add_action( 'wp_logout', $callable, 10, 0 );
55
		add_action( 'wp_masterbar_logout', $callable, 10, 0 );
56
	}
57
58
	public function init_full_sync_listeners( $callable ) {
59
		add_action( 'jetpack_full_sync_users', $callable );
60
	}
61
62
	public function init_before_send() {
63
		add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) );
64
		add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) );
65
		add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 );
66
		add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
67
		add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
68
69
		// full sync
70
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
71
	}
72
73
	public function sanitize_user_and_expand( $user ) {
74
		$user = $this->add_to_user( $user );
75
		return $this->sanitize_user( $user );
76
	}
77
78
	public function sanitize_user( $user ) {
79
		// this create a new user object and stops the passing of the object by reference.
80
		$user = unserialize( serialize( $user ) );
81
82
		if ( is_object( $user ) && is_object( $user->data ) ) {
83
			unset( $user->data->user_pass );
84
		}
85
86
		$user->allcaps = $this->get_real_user_capabilities( $user );
87
		return $user;
88
	}
89
90
	public function add_to_user( $user ) {
91
		$user->allowed_mime_types = get_allowed_mime_types( $user );
92
93
		if ( function_exists( 'get_user_locale' ) ) {
94
95
			// Only set the user locale if it is different from the site local
96
			if ( get_locale() !== get_user_locale( $user->ID ) ) {
97
				$user->locale = get_user_locale( $user->ID );
98
			}
99
		}
100
101
		return $user;
102
	}
103
104
	public function get_real_user_capabilities( $user ) {
105
		$user_capabilities = array();
106
		foreach( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) {
107
			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...
108
				$user_capabilities[ $capability ] = true;
109
			}
110
		}
111
		return $user_capabilities;
112
	}
113
114
	public function expand_user( $args ) {
115
		list( $user ) = $args;
116
117
		if ( $user ) {
118
			return array( $this->add_to_user( $user ) );
119
		}
120
121
		return false;
122
	}
123
124
	public function expand_login_username( $args ) {
125
		list( $login, $user ) = $args;
126
		$user = $this->sanitize_user( $user );
127
128
		return array( $login, $user );
129
	}
130
131
	public function expand_logout_username( $args, $user_id ) {
132
		$user  = get_userdata( $user_id );
133
		$user  = $this->sanitize_user( $user );
134
		$login = '';
135
		if ( is_object( $user ) && is_object( $user->data ) ) {
136
			$login = $user->data->user_login;
137
		}
138
139
		return array( $login, $user );
140
	}
141
142
	public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) {
143
		$is_multisite = is_multisite();
144
		/**
145
		 * Fires when a user is deleted on a site
146
		 *
147
		 * @since 5.4.0
148
		 *
149
		 * @param int $deleted_user_id - ID of the deleted user
150
		 * @param int $reassigned_user_id - ID of the user the deleted user's posts is reassigned to (if any)
151
		 * @param bool $is_multisite - Whether this site is a multisite installation
152
		 */
153
		do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, $is_multisite );
154
	}
155
156
	public function edited_user_handler( $user_id ) {
157
		/**
158
		 * Fires when a user is edited on a site
159
		 *
160
		 * @since 5.4.0
161
		 *
162
		 * @param int $user_id - ID of the edited user
163
		 */
164
		do_action( 'jetpack_user_edited', $user_id );
165
	}
166
	
167
	function save_user_handler( $user_id, $old_user_data = null ) {
168
		// ensure we only sync users who are members of the current blog
169
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
170
			return;
171
		}
172
173
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
174
175
		// Older versions of WP don't pass the old_user_data in ->data
176
		if ( isset( $old_user_data->data ) ) {
177
			$old_user = $old_user_data->data;
178
		} else {
179
			$old_user = $old_user_data;
180
		}
181
182
		if ( $old_user !== null ) {
183
			unset( $old_user->user_pass );
184
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
185
				return;
186
			}
187
		}
188
189
		if ( 'user_register' === current_filter() ) {
190
			/**
191
			 * Fires when a new user is registered on a site
192
			 *
193
			 * @since 4.9.0
194
			 *
195
			 * @param object The WP_User object
196
			 */
197
			do_action( 'jetpack_sync_register_user', $user );
198
199
			return;
200
		}
201
		/* MU Sites add users instead of register them to sites */
202
		if ( 'add_user_to_blog' === current_filter() ) {
203
			/**
204
			 * Fires when a new user is added to a site. (WordPress Multisite)
205
			 *
206
			 * @since 4.9.0
207
			 *
208
			 * @param object The WP_User object
209
			 */
210
			do_action( 'jetpack_sync_add_user', $user );
211
212
			return;
213
		}
214
215
		/**
216
		 * Fires when the client needs to sync an updated user
217
		 *
218
		 * @since 4.2.0
219
		 *
220
		 * @param object The WP_User object
221
		 */
222
		do_action( 'jetpack_sync_save_user', $user );
223
	}
224
225
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
226
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
227
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
228
			return;
229
		}
230
231
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
232
		/**
233
		 * Fires when the client needs to sync an updated user
234
		 *
235
		 * @since 4.2.0
236
		 *
237
		 * @param object The WP_User object
238
		 */
239
		do_action( 'jetpack_sync_save_user', $user );
240
	}
241
242
	function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
243
		if ( $meta_key === 'locale' ) {
244
			if ( current_filter() === 'deleted_user_meta' ) {
245
				/**
246
				 * Allow listeners to listen for user local delete changes
247
				 *
248
				 * @since 4.8.0
249
				 *
250
				 * @param int $user_id - The ID of the user whos locale is being deleted
251
				 */
252
				do_action( 'jetpack_sync_user_locale_delete', $user_id );
253
			} else {
254
				/**
255
				 * Allow listeners to listen for user local changes
256
				 *
257
				 * @since 4.8.0
258
				 *
259
				 * @param int $user_id - The ID of the user whos locale is being changed
260
				 * @param int $value - The value of the new locale
261
				 */
262
				do_action( 'jetpack_sync_user_locale', $user_id, $value );
263
			}
264
		}
265
		$this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value );
266
	}
267
268
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
269
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
270
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
271
			return;
272
		}
273
274
		// if a user is currently being removed as a member of this blog, we don't fire the event
275
		if ( current_filter() === 'deleted_user_meta'
276
		     &&
277
		     preg_match( '/capabilities|user_level/', $meta_key )
278
		     &&
279
		     ! is_user_member_of_blog( $user_id, get_current_blog_id() )
280
		) {
281
			return;
282
		}
283
284
		$user = get_user_by( 'id', $user_id );
285
		if ( $meta_key === $user->cap_key ) {
286
			/**
287
			 * Fires when the client needs to sync an updated user
288
			 *
289
			 * @since 4.2.0
290
			 *
291
			 * @param object The Sanitized WP_User object
292
			 */
293
			do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) );
294
		}
295
	}
296
297
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
298
		global $wpdb;
299
300
		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 );
301
	}
302
303
	public function estimate_full_sync_actions( $config ) {
304
		global $wpdb;
305
306
		$query = "SELECT count(*) FROM $wpdb->usermeta";
307
308
		if ( $where_sql = $this->get_where_sql( $config ) ) {
309
			$query .= ' WHERE ' . $where_sql;
310
		}
311
312
		$count = $wpdb->get_var( $query );
313
314
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
315
	}
316
317 View Code Duplication
	private function get_where_sql( $config ) {
318
		global $wpdb;
319
320
		$query = "meta_key = '{$wpdb->prefix}capabilities'";
321
322
		// config is a list of user IDs to sync
323
		if ( is_array( $config ) ) {
324
			$query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
325
		}
326
327
		return $query;
328
	}
329
330
	function get_full_sync_actions() {
331
		return array( 'jetpack_full_sync_users' );
332
	}
333
334
	function get_initial_sync_user_config() {
335
		global $wpdb;
336
337
		$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 ) );
338
339
		if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
340
			return $user_ids;
341
		} else {
342
			return false;
343
		}
344
	}
345
346
	public function expand_users( $args ) {
347
		$user_ids = $args[0];
348
349
		return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
350
	}
351
352
	public function remove_user_from_blog_handler( $user_id, $blog_id ) {
353
		//User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114
354
		if ( $this->is_add_new_user_to_blog() ) {
355
			return;
356
		}
357
358
		$reassigned_user_id = $this->get_reassigned_network_user_id();
359
360
		//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
361
		/**
362
		 * Fires when a user is removed from a blog on a multisite installation
363
		 *
364
		 * @since 5.4.0
365
		 *
366
		 * @param int $user_id - ID of the removed user
367
		 * @param int $reassigned_user_id - ID of the user the removed user's posts is reassigned to (if any)
368
		 */
369
		do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id );
370
	}
371
372
	private function is_add_new_user_to_blog() {
373
		return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' );
374
	}
375
376
	private function is_add_user_to_blog() {
377
		return Jetpack::is_function_in_backtrace( 'add_user_to_blog' );
378
	}
379
380
	private function is_create_user() {
381
		$functions = array(
382
			'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site
383
			'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site
384
			'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
385
		);
386
387
		return Jetpack::is_function_in_backtrace( $functions );
388
	}
389
390
	private function get_reassigned_network_user_id() {
391
		$backtrace = debug_backtrace( false );
392
		foreach ( $backtrace as $call ) {
393
			if (
394
				'remove_user_from_blog' === $call['function'] &&
395
				3 === count( $call['args'] )
396
			) {
397
				return $call['args'][2];
398
			}
399
		}
400
401
		return false;
402
	}
403
}
404