Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_Sync_Module_Users   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 230
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 38
lcom 2
cbo 1
dl 25
loc 230
rs 8.3999
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
B init_listeners() 0 25 1
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 8 1
A sanitize_user_and_expand() 0 5 1
A get_object_by_id() 0 7 3
A add_to_user() 0 5 1
A expand_user() 0 9 2
A expand_login_username() 0 6 1
A sanitize_user() 0 10 3
A expand_logout_username() 0 10 3
B save_user_handler() 0 31 5
A save_user_role_handler() 0 12 1
B save_user_cap_handler() 0 24 5
A enqueue_full_sync_actions() 0 4 1
A get_where_sql() 12 12 2
A get_full_sync_actions() 0 3 1
A get_initial_sync_user_config() 0 11 2
A expand_users() 0 5 1
A estimate_full_sync_actions() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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_save_user', $callable, 10, 2 );
25
26
		add_action( 'deleted_user', $callable, 10, 2 );
27
		add_action( 'remove_user_from_blog', $callable, 10, 2 );
28
29
		// user roles
30
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
31
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
32
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
33
34
		// user capabilities
35
		add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
36
		add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
37
		add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
38
39
		// user authentication
40
		add_action( 'wp_login', $callable, 10, 2 );
41
		add_action( 'wp_login_failed', $callable, 10, 2 );
42
		add_action( 'wp_logout', $callable, 10, 0 );
43
	}
44
45
	public function init_full_sync_listeners( $callable ) {
46
		add_action( 'jetpack_full_sync_users', $callable );
47
	}
48
49
	public function init_before_send() {
50
		add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) );
51
		add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
52
		add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
53
54
		// full sync
55
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
56
	}
57
58
	public function sanitize_user_and_expand( $user ) {
59
		$user = $this->sanitize_user( $user );
60
61
		return $this->add_to_user( $user );
62
	}
63
64
	public function sanitize_user( $user ) {
65
		// this create a new user object and stops the passing of the object by reference.
66
		$user = unserialize( serialize( $user ) );
67
68
		if ( is_object( $user ) && is_object( $user->data ) ) {
69
			unset( $user->data->user_pass );
70
		}
71
72
		return $user;
73
	}
74
75
	public function add_to_user( $user ) {
76
		$user->allowed_mime_types = get_allowed_mime_types( $user );
77
78
		return $user;
79
	}
80
81
	public function expand_user( $args ) {
82
		list( $user ) = $args;
83
84
		if ( $user ) {
85
			return array( $this->add_to_user( $user ) );	
86
		}
87
88
		return false;
89
	}
90
91
	public function expand_login_username( $args ) {
92
		list( $login, $user ) = $args;
93
		$user = $this->sanitize_user( $user );
94
95
		return array( $login, $user );
96
	}
97
98
	public function expand_logout_username( $args, $user_id ) {
99
		$user  = get_userdata( $user_id );
100
		$user  = $this->sanitize_user( $user );
101
		$login = '';
102
		if( is_object( $user ) && is_object( $user->data ) ) {
103
			$login = $user->data->user_login;
104
		}
105
106
		return array( $login, $user );
107
	}
108
109
	function save_user_handler( $user_id, $old_user_data = null ) {
110
111
		// ensure we only sync users who are members of the current blog
112
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
113
			return;
114
		}
115
116
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
117
118
		// Older versions of WP don't pass the old_user_data in ->data
119
		if ( isset( $old_user_data->data ) ) {
120
			$old_user = $old_user_data->data;
121
		} else {
122
			$old_user = $old_user_data;
123
		}
124
125
		if ( $old_user !== null ) {
126
			unset( $old_user->user_pass );
127
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
128
				return;
129
			}
130
		}
131
		/**
132
		 * Fires when the client needs to sync an updated user
133
		 *
134
		 * @since 4.2.0
135
		 *
136
		 * @param object The WP_User object
137
		 */
138
		do_action( 'jetpack_sync_save_user', $user );
139
	}
140
141
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $role is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_roles is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
143
144
		/**
145
		 * Fires when the client needs to sync an updated user
146
		 *
147
		 * @since 4.2.0
148
		 *
149
		 * @param object The WP_User object
150
		 */
151
		do_action( 'jetpack_sync_save_user', $user );
152
	}
153
154
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
0 ignored issues
show
Unused Code introduced by
The parameter $capabilities is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
155
156
		// if a user is currently being removed as a member of this blog, we don't fire the event
157
		if ( current_filter() === 'deleted_user_meta'
158
		     &&
159
		     preg_match( '/capabilities|user_level/', $meta_key )
160
		     &&
161
		     ! is_user_member_of_blog( $user_id, get_current_blog_id() )
162
		) {
163
			return;
164
		}
165
166
		$user =  get_user_by( 'id', $user_id );
167
		if ( $meta_key === $user->cap_key ) {
168
			/**
169
			 * Fires when the client needs to sync an updated user
170
			 *
171
			 * @since 4.2.0
172
			 *
173
			 * @param object The Sanitized WP_User object
174
			 */
175
			do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) );
176
		}
177
	}
178
179
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
180
		global $wpdb;
181
		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 );
182
	}
183
184 View Code Duplication
	public function estimate_full_sync_actions( $config ) {
185
		global $wpdb;
186
187
		$query = "SELECT count(*) FROM $wpdb->usermeta";
188
		
189
		if ( $where_sql = $this->get_where_sql( $config ) ) {
190
			$query .= ' WHERE ' . $where_sql;
191
		}
192
193
		$count = $wpdb->get_var( $query );
194
195
		return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
196
	}
197
198 View Code Duplication
	private function get_where_sql( $config ) {
199
		global $wpdb;
200
201
		$query = "meta_key = '{$wpdb->prefix}capabilities'";
202
		
203
		// config is a list of user IDs to sync
204
		if ( is_array( $config ) ) {
205
			$query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
206
		}
207
208
		return $query;
209
	}
210
211
	function get_full_sync_actions() {
212
		return array( 'jetpack_full_sync_users' );
213
	}
214
215
	function get_initial_sync_user_config() {
216
		global $wpdb;
217
218
		$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 ) );
219
220
		if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
221
			return $user_ids;
222
		} else {
223
			return false;
224
		}
225
	}
226
227
	public function expand_users( $args ) {
228
		$user_ids = $args[0];
229
230
		return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
231
	}
232
}
233