PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 5.0.3
Jetpack – WP Security, Backup, Speed, & Growth v5.0.3
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
296 lines 8.6 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 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, 10, 2 );
27
28 add_action( 'jetpack_sync_user_locale', $callable, 10, 2 );
29 add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 );
30
31 add_action( 'deleted_user', $callable, 10, 2 );
32 add_action( 'remove_user_from_blog', $callable, 10, 2 );
33
34 // user roles
35 add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
36 add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
37 add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
38
39 // user capabilities
40 add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
41 add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
42 add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 );
43
44 // user authentication
45 add_action( 'wp_login', $callable, 10, 2 );
46 add_action( 'wp_login_failed', $callable, 10, 2 );
47 add_action( 'wp_logout', $callable, 10, 0 );
48 add_action( 'wp_masterbar_logout', $callable, 10, 0 );
49 }
50
51 public function init_full_sync_listeners( $callable ) {
52 add_action( 'jetpack_full_sync_users', $callable );
53 }
54
55 public function init_before_send() {
56 add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) );
57 add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) );
58 add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) );
59 add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 );
60 add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 );
61
62 // full sync
63 add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) );
64 }
65
66 public function sanitize_user_and_expand( $user ) {
67 $user = $this->sanitize_user( $user );
68
69 return $this->add_to_user( $user );
70 }
71
72 public function sanitize_user( $user ) {
73 // this create a new user object and stops the passing of the object by reference.
74 $user = unserialize( serialize( $user ) );
75
76 if ( is_object( $user ) && is_object( $user->data ) ) {
77 unset( $user->data->user_pass );
78 }
79
80 return $user;
81 }
82
83 public function add_to_user( $user ) {
84 $user->allowed_mime_types = get_allowed_mime_types( $user );
85
86 if ( function_exists( 'get_user_locale' ) ) {
87
88 // Only set the user locale if it is different from the site local
89 if ( get_locale() !== get_user_locale( $user->ID ) ) {
90 $user->locale = get_user_locale( $user->ID );
91 }
92 }
93 return $user;
94 }
95
96 public function expand_user( $args ) {
97 list( $user ) = $args;
98
99 if ( $user ) {
100 return array( $this->add_to_user( $user ) );
101 }
102
103 return false;
104 }
105
106 public function expand_login_username( $args ) {
107 list( $login, $user ) = $args;
108 $user = $this->sanitize_user( $user );
109
110 return array( $login, $user );
111 }
112
113 public function expand_logout_username( $args, $user_id ) {
114 $user = get_userdata( $user_id );
115 $user = $this->sanitize_user( $user );
116 $login = '';
117 if( is_object( $user ) && is_object( $user->data ) ) {
118 $login = $user->data->user_login;
119 }
120
121 return array( $login, $user );
122 }
123
124 function save_user_handler( $user_id, $old_user_data = null ) {
125 // ensure we only sync users who are members of the current blog
126 if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
127 return;
128 }
129
130 $user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
131
132 // Older versions of WP don't pass the old_user_data in ->data
133 if ( isset( $old_user_data->data ) ) {
134 $old_user = $old_user_data->data;
135 } else {
136 $old_user = $old_user_data;
137 }
138
139 if ( $old_user !== null ) {
140 unset( $old_user->user_pass );
141 if ( serialize( $old_user ) === serialize( $user->data ) ) {
142 return;
143 }
144 }
145
146 if ( 'user_register' === current_filter() ) {
147 /**
148 * Fires when a new user is registered on a site
149 *
150 * @since 4.9.0
151 *
152 * @param object The WP_User object
153 */
154 do_action( 'jetpack_sync_register_user', $user );
155 return;
156 }
157 /* MU Sites add users instead of register them to sites */
158 if ( 'add_user_to_blog' === current_filter() ) {
159 /**
160 * Fires when a new user is added to a site. (WordPress Multisite)
161 *
162 * @since 4.9.0
163 *
164 * @param object The WP_User object
165 */
166 do_action( 'jetpack_sync_add_user', $user );
167 return;
168 }
169 /**
170 * Fires when the client needs to sync an updated user
171 *
172 * @since 4.2.0
173 *
174 * @param object The WP_User object
175 */
176 do_action( 'jetpack_sync_save_user', $user );
177 }
178
179 function save_user_role_handler( $user_id, $role, $old_roles = null ) {
180 $user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
181
182 /**
183 * Fires when the client needs to sync an updated user
184 *
185 * @since 4.2.0
186 *
187 * @param object The WP_User object
188 */
189 do_action( 'jetpack_sync_save_user', $user );
190 }
191
192 function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) {
193 if ( $meta_key === 'locale' ) {
194 if ( current_filter() === 'deleted_user_meta' ) {
195 /**
196 * Allow listeners to listen for user local delete changes
197 *
198 * @since 4.8.0
199 *
200 * @param int $user_id - The ID of the user whos locale is being deleted
201 */
202 do_action( 'jetpack_sync_user_locale_delete', $user_id );
203 } else {
204 /**
205 * Allow listeners to listen for user local changes
206 *
207 * @since 4.8.0
208 *
209 * @param int $user_id - The ID of the user whos locale is being changed
210 * @param int $value - The value of the new locale
211 */
212 do_action( 'jetpack_sync_user_locale', $user_id, $value );
213 }
214 }
215 $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value );
216 }
217
218 function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
219 // if a user is currently being removed as a member of this blog, we don't fire the event
220 if ( current_filter() === 'deleted_user_meta'
221 &&
222 preg_match( '/capabilities|user_level/', $meta_key )
223 &&
224 ! is_user_member_of_blog( $user_id, get_current_blog_id() )
225 ) {
226 return;
227 }
228
229 $user = get_user_by( 'id', $user_id );
230 if ( $meta_key === $user->cap_key ) {
231 /**
232 * Fires when the client needs to sync an updated user
233 *
234 * @since 4.2.0
235 *
236 * @param object The Sanitized WP_User object
237 */
238 do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) );
239 }
240 }
241
242 public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
243 global $wpdb;
244 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 );
245 }
246
247 public function estimate_full_sync_actions( $config ) {
248 global $wpdb;
249
250 $query = "SELECT count(*) FROM $wpdb->usermeta";
251
252 if ( $where_sql = $this->get_where_sql( $config ) ) {
253 $query .= ' WHERE ' . $where_sql;
254 }
255
256 $count = $wpdb->get_var( $query );
257
258 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
259 }
260
261 private function get_where_sql( $config ) {
262 global $wpdb;
263
264 $query = "meta_key = '{$wpdb->prefix}capabilities'";
265
266 // config is a list of user IDs to sync
267 if ( is_array( $config ) ) {
268 $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
269 }
270
271 return $query;
272 }
273
274 function get_full_sync_actions() {
275 return array( 'jetpack_full_sync_users' );
276 }
277
278 function get_initial_sync_user_config() {
279 global $wpdb;
280
281 $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 ) );
282
283 if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
284 return $user_ids;
285 } else {
286 return false;
287 }
288 }
289
290 public function expand_users( $args ) {
291 $user_ids = $args[0];
292
293 return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
294 }
295 }
296