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
|
|
|
// full sync |
34
|
|
|
add_action( 'jetpack_full_sync_users', $callable ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function init_before_send() { |
38
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
39
|
|
|
add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 2 ); |
40
|
|
|
add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
41
|
|
|
|
42
|
|
|
// full sync |
43
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function sanitize_user_and_expand( $user ) { |
47
|
|
|
$user = $this->sanitize_user( $user ); |
48
|
|
|
|
49
|
|
|
return $this->add_to_user( $user ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function sanitize_user( $user ) { |
53
|
|
|
unset( $user->data->user_pass ); |
54
|
|
|
|
55
|
|
|
return $user; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function add_to_user( $user ) { |
59
|
|
|
$user->allowed_mime_types = get_allowed_mime_types( $user ); |
60
|
|
|
|
61
|
|
|
return $user; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function expand_user( $args ) { |
65
|
|
|
list( $user ) = $args; |
66
|
|
|
|
67
|
|
|
return array( $this->add_to_user( $user ) ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function expand_login_username( $args ) { |
71
|
|
|
list( $login, $user ) = $args; |
72
|
|
|
$user = $this->sanitize_user( $user ); |
73
|
|
|
|
74
|
|
|
return array( $login, $user ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function expand_logout_username( $args, $user_id ) { |
78
|
|
|
$user = get_userdata( $user_id ); |
79
|
|
|
$user = $this->sanitize_user( $user ); |
80
|
|
|
$login = $user->data->user_login; |
81
|
|
|
|
82
|
|
|
return array( $login, $user ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
function save_user_handler( $user_id, $old_user_data = null ) { |
86
|
|
|
|
87
|
|
|
// ensure we only sync users who are members of the current blog |
88
|
|
|
if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
93
|
|
|
|
94
|
|
|
// Older versions of WP don't pass the old_user_data in ->data |
95
|
|
|
if ( isset( $old_user_data->data ) ) { |
96
|
|
|
$old_user = $old_user_data->data; |
97
|
|
|
} else { |
98
|
|
|
$old_user = $old_user_data; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ( $old_user !== null ) { |
102
|
|
|
unset( $old_user->user_pass ); |
103
|
|
|
if ( serialize( $old_user ) === serialize( $user->data ) ) { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
/** |
108
|
|
|
* Fires when the client needs to sync an updated user |
109
|
|
|
* |
110
|
|
|
* @since 4.2.0 |
111
|
|
|
* |
112
|
|
|
* @param object The WP_User object |
113
|
|
|
*/ |
114
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
|
|
|
|
118
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Fires when the client needs to sync an updated user |
122
|
|
|
* |
123
|
|
|
* @since 4.2.0 |
124
|
|
|
* |
125
|
|
|
* @param object The WP_User object |
126
|
|
|
*/ |
127
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
|
|
|
|
131
|
|
|
|
132
|
|
|
// if a user is currently being removed as a member of this blog, we don't fire the event |
133
|
|
|
if ( current_filter() === 'deleted_user_meta' |
134
|
|
|
&& |
135
|
|
|
preg_match( '/capabilities|user_level/', $meta_key ) |
136
|
|
|
&& |
137
|
|
|
! is_user_member_of_blog( $user_id, get_current_blog_id() ) |
138
|
|
|
) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
143
|
|
|
if ( $meta_key === $user->cap_key ) { |
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
|
|
|
|
155
|
|
|
public function enqueue_full_sync_actions() { |
156
|
|
|
global $wpdb; |
157
|
|
|
|
158
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', null ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
function get_full_sync_actions() { |
162
|
|
|
return array( 'jetpack_full_sync_users' ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function expand_users( $args ) { |
166
|
|
|
$user_ids = $args[0]; |
167
|
|
|
|
168
|
|
|
return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) ); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.