Completed
Push — fix/nosara ( 09438e...93ba64 )
by
unknown
11:29
created

Jetpack_Sync_Module_Users::save_user_cap_handler()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 4
nop 4
dl 0
loc 28
rs 6.7272
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->sanitize_user( $user );
75
76
		return $this->add_to_user( $user );
77
	}
78
79
	public function sanitize_user( $user ) {
80
		// this create a new user object and stops the passing of the object by reference.
81
		$user = unserialize( serialize( $user ) );
82
83
		if ( is_object( $user ) && is_object( $user->data ) ) {
84
			unset( $user->data->user_pass );
85
		}
86
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 expand_user( $args ) {
105
		list( $user ) = $args;
106
107
		if ( $user ) {
108
			return array( $this->add_to_user( $user ) );
109
		}
110
111
		return false;
112
	}
113
114
	public function expand_login_username( $args ) {
115
		list( $login, $user ) = $args;
116
		$user = $this->sanitize_user( $user );
117
118
		return array( $login, $user );
119
	}
120
121
	public function expand_logout_username( $args, $user_id ) {
122
		$user  = get_userdata( $user_id );
123
		$user  = $this->sanitize_user( $user );
124
		$login = '';
125
		if ( is_object( $user ) && is_object( $user->data ) ) {
126
			$login = $user->data->user_login;
127
		}
128
129
		return array( $login, $user );
130
	}
131
132
	public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) {
133
		do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, is_multisite() );
134
	}
135
136
	public function edited_user_handler( $user_id ) {
137
		do_action( 'jetpack_user_edited', $user_id );
138
	}
139
	
140
	function save_user_handler( $user_id, $old_user_data = null ) {
141
		// ensure we only sync users who are members of the current blog
142
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
143
			return;
144
		}
145
146
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
147
148
		// Older versions of WP don't pass the old_user_data in ->data
149
		if ( isset( $old_user_data->data ) ) {
150
			$old_user = $old_user_data->data;
151
		} else {
152
			$old_user = $old_user_data;
153
		}
154
155
		if ( $old_user !== null ) {
156
			unset( $old_user->user_pass );
157
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
158
				return;
159
			}
160
		}
161
162
		if ( 'user_register' === current_filter() ) {
163
			/**
164
			 * Fires when a new user is registered on a site
165
			 *
166
			 * @since 4.9.0
167
			 *
168
			 * @param object The WP_User object
169
			 */
170
			do_action( 'jetpack_sync_register_user', $user );
171
172
			return;
173
		}
174
		/* MU Sites add users instead of register them to sites */
175
		if ( 'add_user_to_blog' === current_filter() ) {
176
			/**
177
			 * Fires when a new user is added to a site. (WordPress Multisite)
178
			 *
179
			 * @since 4.9.0
180
			 *
181
			 * @param object The WP_User object
182
			 */
183
			do_action( 'jetpack_sync_add_user', $user );
184
185
			return;
186
		}
187
188
		/**
189
		 * Fires when the client needs to sync an updated user
190
		 *
191
		 * @since 4.2.0
192
		 *
193
		 * @param object The WP_User object
194
		 */
195
		do_action( 'jetpack_sync_save_user', $user );
196
	}
197
198
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
199
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
200
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
201
			return;
202
		}
203
204
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
205
		/**
206
		 * Fires when the client needs to sync an updated user
207
		 *
208
		 * @since 4.2.0
209
		 *
210
		 * @param object The WP_User object
211
		 */
212
		do_action( 'jetpack_sync_save_user', $user );
213
	}
214
215
	function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
216
		if ( $meta_key === 'locale' ) {
217
			if ( current_filter() === 'deleted_user_meta' ) {
218
				/**
219
				 * Allow listeners to listen for user local delete changes
220
				 *
221
				 * @since 4.8.0
222
				 *
223
				 * @param int $user_id - The ID of the user whos locale is being deleted
224
				 */
225
				do_action( 'jetpack_sync_user_locale_delete', $user_id );
226
			} else {
227
				/**
228
				 * Allow listeners to listen for user local changes
229
				 *
230
				 * @since 4.8.0
231
				 *
232
				 * @param int $user_id - The ID of the user whos locale is being changed
233
				 * @param int $value - The value of the new locale
234
				 */
235
				do_action( 'jetpack_sync_user_locale', $user_id, $value );
236
			}
237
		}
238
		$this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value );
239
	}
240
241
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
242
		//The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both
243
		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) {
244
			return;
245
		}
246
247
		// if a user is currently being removed as a member of this blog, we don't fire the event
248
		if ( current_filter() === 'deleted_user_meta'
249
		     &&
250
		     preg_match( '/capabilities|user_level/', $meta_key )
251
		     &&
252
		     ! is_user_member_of_blog( $user_id, get_current_blog_id() )
253
		) {
254
			return;
255
		}
256
257
		$user = get_user_by( 'id', $user_id );
258
		if ( $meta_key === $user->cap_key ) {
259
			/**
260
			 * Fires when the client needs to sync an updated user
261
			 *
262
			 * @since 4.2.0
263
			 *
264
			 * @param object The Sanitized WP_User object
265
			 */
266
			do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) );
267
		}
268
	}
269
270
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
271
		global $wpdb;
272
273
		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 );
274
	}
275
276 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
277
		global $wpdb;
278
279
		$query = "SELECT count(*) FROM $wpdb->usermeta";
280
281
		if ( $where_sql = $this->get_where_sql( $config ) ) {
282
			$query .= ' WHERE ' . $where_sql;
283
		}
284
285
		$count = $wpdb->get_var( $query );
286
287
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
288
	}
289
290 View Code Duplication
	private function get_where_sql( $config ) {
291
		global $wpdb;
292
293
		$query = "meta_key = '{$wpdb->prefix}capabilities'";
294
295
		// config is a list of user IDs to sync
296
		if ( is_array( $config ) ) {
297
			$query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
298
		}
299
300
		return $query;
301
	}
302
303
	function get_full_sync_actions() {
304
		return array( 'jetpack_full_sync_users' );
305
	}
306
307
	function get_initial_sync_user_config() {
308
		global $wpdb;
309
310
		$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 ) );
311
312
		if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
313
			return $user_ids;
314
		} else {
315
			return false;
316
		}
317
	}
318
319
	public function expand_users( $args ) {
320
		$user_ids = $args[0];
321
322
		return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
323
	}
324
325
	public function remove_user_from_blog_handler( $user_id, $blog_id ) {
326
		//User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114
327
		if ( $this->is_add_new_user_to_blog() ) {
328
			return;
329
		}
330
331
		$reassigned_user_id = $this->get_reassigned_network_user_id();
332
333
		//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
334
		do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id );
335
	}
336
337
	private function is_add_new_user_to_blog() {
338
		return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' );
339
	}
340
341
	private function is_add_user_to_blog() {
342
		return Jetpack::is_function_in_backtrace( 'add_user_to_blog' );
343
	}
344
345
	private function is_create_user() {
346
		$functions = array(
347
			'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site
348
			'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site
349
			'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
350
		);
351
352
		return Jetpack::is_function_in_backtrace( $functions );
353
	}
354
355
	private function get_reassigned_network_user_id() {
356
		$backtrace = debug_backtrace( false );
357
		foreach ( $backtrace as $call ) {
358
			if (
359
				'remove_user_from_blog' === $call['function'] &&
360
				3 === count( $call['args'] )
361
			) {
362
				return $call['args'][2];
363
			}
364
		}
365
366
		return false;
367
	}
368
}
369