PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.3.5
Jetpack – WP Security, Backup, Speed, & Growth v4.3.5
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
230 lines 6.4 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_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->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 = $user->data->user_login;
102
103 return array( $login, $user );
104 }
105
106 function save_user_handler( $user_id, $old_user_data = null ) {
107
108 // ensure we only sync users who are members of the current blog
109 if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
110 return;
111 }
112
113 $user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
114
115 // Older versions of WP don't pass the old_user_data in ->data
116 if ( isset( $old_user_data->data ) ) {
117 $old_user = $old_user_data->data;
118 } else {
119 $old_user = $old_user_data;
120 }
121
122 if ( $old_user !== null ) {
123 unset( $old_user->user_pass );
124 if ( serialize( $old_user ) === serialize( $user->data ) ) {
125 return;
126 }
127 }
128 /**
129 * Fires when the client needs to sync an updated user
130 *
131 * @since 4.2.0
132 *
133 * @param object The WP_User object
134 */
135 do_action( 'jetpack_sync_save_user', $user );
136 }
137
138 function save_user_role_handler( $user_id, $role, $old_roles = null ) {
139 $user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
140
141 /**
142 * Fires when the client needs to sync an updated user
143 *
144 * @since 4.2.0
145 *
146 * @param object The WP_User object
147 */
148 do_action( 'jetpack_sync_save_user', $user );
149 }
150
151 function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
152
153 // if a user is currently being removed as a member of this blog, we don't fire the event
154 if ( current_filter() === 'deleted_user_meta'
155 &&
156 preg_match( '/capabilities|user_level/', $meta_key )
157 &&
158 ! is_user_member_of_blog( $user_id, get_current_blog_id() )
159 ) {
160 return;
161 }
162
163 $user = get_user_by( 'id', $user_id );
164 if ( $meta_key === $user->cap_key ) {
165 /**
166 * Fires when the client needs to sync an updated user
167 *
168 * @since 4.2.0
169 *
170 * @param object The Sanitized WP_User object
171 */
172 do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) );
173 }
174 }
175
176 public function enqueue_full_sync_actions( $config ) {
177 global $wpdb;
178 return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->usermeta, 'user_id', $this->get_where_sql( $config ) );
179 }
180
181 public function estimate_full_sync_actions( $config ) {
182 global $wpdb;
183
184 $query = "SELECT count(*) FROM $wpdb->usermeta";
185
186 if ( $where_sql = $this->get_where_sql( $config ) ) {
187 $query .= ' WHERE ' . $where_sql;
188 }
189
190 $count = $wpdb->get_var( $query );
191
192 return (int) ceil( $count / self::ARRAY_CHUNK_SIZE );
193 }
194
195 private function get_where_sql( $config ) {
196 global $wpdb;
197
198 $query = "meta_key = '{$wpdb->prefix}capabilities'";
199
200 // config is a list of user IDs to sync
201 if ( is_array( $config ) ) {
202 $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')';
203 }
204
205 return $query;
206 }
207
208 function get_full_sync_actions() {
209 return array( 'jetpack_full_sync_users' );
210 }
211
212 function get_initial_sync_user_config() {
213 global $wpdb;
214
215 $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 ) );
216
217 if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) {
218 return $user_ids;
219 } else {
220 return false;
221 }
222 }
223
224 public function expand_users( $args ) {
225 $user_ids = $args[0];
226
227 return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) );
228 }
229 }
230