1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { |
4
|
|
|
function name() { |
5
|
|
|
return 'users'; |
6
|
|
|
} |
7
|
|
|
|
8
|
|
|
public function init_listeners( $callable ) { |
9
|
|
|
// users |
10
|
|
|
add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
11
|
|
|
add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
12
|
|
|
add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
13
|
|
|
add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); |
14
|
|
|
|
15
|
|
|
add_action( 'deleted_user', $callable, 10, 2 ); |
16
|
|
|
add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
17
|
|
|
|
18
|
|
|
// user roles |
19
|
|
|
add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
20
|
|
|
add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
21
|
|
|
add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
22
|
|
|
|
23
|
|
|
// user capabilities |
24
|
|
|
add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
25
|
|
|
add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
26
|
|
|
add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
27
|
|
|
|
28
|
|
|
// user authentication |
29
|
|
|
add_action( 'wp_login', $callable, 10, 2 ); |
30
|
|
|
add_action( 'wp_login_failed', $callable, 10, 2 ); |
31
|
|
|
add_action( 'wp_logout', $callable, 10, 0 ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function init_full_sync_listeners( $callable ) { |
35
|
|
|
add_action( 'jetpack_full_sync_users', $callable ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function init_before_send() { |
39
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
40
|
|
|
add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 2 ); |
41
|
|
|
add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
42
|
|
|
|
43
|
|
|
// full sync |
44
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function sanitize_user_and_expand( $user ) { |
48
|
|
|
$user = $this->sanitize_user( $user ); |
49
|
|
|
|
50
|
|
|
return $this->add_to_user( $user ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function sanitize_user( $user ) { |
54
|
|
|
unset( $user->data->user_pass ); |
55
|
|
|
|
56
|
|
|
return $user; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function add_to_user( $user ) { |
60
|
|
|
$user->allowed_mime_types = get_allowed_mime_types( $user ); |
61
|
|
|
|
62
|
|
|
return $user; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function expand_user( $args ) { |
66
|
|
|
list( $user ) = $args; |
67
|
|
|
|
68
|
|
|
return array( $this->add_to_user( $user ) ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function expand_login_username( $args ) { |
72
|
|
|
list( $login, $user ) = $args; |
73
|
|
|
$user = $this->sanitize_user( $user ); |
74
|
|
|
|
75
|
|
|
return array( $login, $user ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function expand_logout_username( $args, $user_id ) { |
79
|
|
|
$user = get_userdata( $user_id ); |
80
|
|
|
$user = $this->sanitize_user( $user ); |
81
|
|
|
$login = $user->data->user_login; |
82
|
|
|
|
83
|
|
|
return array( $login, $user ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
function save_user_handler( $user_id, $old_user_data = null ) { |
87
|
|
|
|
88
|
|
|
// ensure we only sync users who are members of the current blog |
89
|
|
|
if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
90
|
|
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
94
|
|
|
|
95
|
|
|
// Older versions of WP don't pass the old_user_data in ->data |
96
|
|
|
if ( isset( $old_user_data->data ) ) { |
97
|
|
|
$old_user = $old_user_data->data; |
98
|
|
|
} else { |
99
|
|
|
$old_user = $old_user_data; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( $old_user !== null ) { |
103
|
|
|
unset( $old_user->user_pass ); |
104
|
|
|
if ( serialize( $old_user ) === serialize( $user->data ) ) { |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
/** |
109
|
|
|
* Fires when the client needs to sync an updated user |
110
|
|
|
* |
111
|
|
|
* @since 4.2.0 |
112
|
|
|
* |
113
|
|
|
* @param object The WP_User object |
114
|
|
|
*/ |
115
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
|
|
|
|
119
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Fires when the client needs to sync an updated user |
123
|
|
|
* |
124
|
|
|
* @since 4.2.0 |
125
|
|
|
* |
126
|
|
|
* @param object The WP_User object |
127
|
|
|
*/ |
128
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
|
|
|
|
132
|
|
|
|
133
|
|
|
// if a user is currently being removed as a member of this blog, we don't fire the event |
134
|
|
|
if ( current_filter() === 'deleted_user_meta' |
135
|
|
|
&& |
136
|
|
|
preg_match( '/capabilities|user_level/', $meta_key ) |
137
|
|
|
&& |
138
|
|
|
! is_user_member_of_blog( $user_id, get_current_blog_id() ) |
139
|
|
|
) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
144
|
|
|
if ( $meta_key === $user->cap_key ) { |
145
|
|
|
/** |
146
|
|
|
* Fires when the client needs to sync an updated user |
147
|
|
|
* |
148
|
|
|
* @since 4.2.0 |
149
|
|
|
* |
150
|
|
|
* @param object The WP_User object |
151
|
|
|
*/ |
152
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function enqueue_full_sync_actions( $config ) { |
157
|
|
|
global $wpdb; |
158
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', $this->get_where_sql( $config ) ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public function estimate_full_sync_actions( $config ) { |
162
|
|
|
global $wpdb; |
163
|
|
|
|
164
|
|
|
$query = "SELECT count(*) FROM $wpdb->users"; |
165
|
|
|
|
166
|
|
|
if ( $where_sql = $this->get_where_sql( $config ) ) { |
167
|
|
|
$query .= ' WHERE ' . $where_sql; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$count = $wpdb->get_var( $query ); |
171
|
|
|
|
172
|
|
|
return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
View Code Duplication |
private function get_where_sql( $config ) { |
176
|
|
|
// config is a list of user IDs to sync |
177
|
|
|
if ( is_array( $config ) ) { |
178
|
|
|
return 'ID IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function get_full_sync_actions() { |
185
|
|
|
return array( 'jetpack_full_sync_users' ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function expand_users( $args ) { |
189
|
|
|
$user_ids = $args[0]; |
190
|
|
|
|
191
|
|
|
return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) ); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.