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
|
|
|
return $this->add_to_user( $user ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function sanitize_user( $user ) { |
52
|
|
|
unset( $user->data->user_pass ); |
53
|
|
|
return $user; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function add_to_user( $user ) { |
57
|
|
|
$user->allowed_mime_types = get_allowed_mime_types( $user ); |
58
|
|
|
return $user; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function expand_user( $args ) { |
62
|
|
|
list( $user ) = $args; |
63
|
|
|
return array( $this->add_to_user( $user ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function expand_login_username( $args ) { |
67
|
|
|
list( $login, $user ) = $args; |
68
|
|
|
$user = $this->sanitize_user( $user ); |
69
|
|
|
return array( $login ,$user ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function expand_logout_username( $args, $user_id ) { |
73
|
|
|
$user = get_userdata( $user_id ); |
74
|
|
|
$user = $this->sanitize_user( $user ); |
75
|
|
|
$login = $user->data->user_login; |
76
|
|
|
return array( $login, $user ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function save_user_handler( $user_id, $old_user_data = null ) { |
80
|
|
|
|
81
|
|
|
// ensure we only sync users who are members of the current blog |
82
|
|
|
if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
87
|
|
|
|
88
|
|
|
// Older versions of WP don't pass the old_user_data in ->data |
89
|
|
|
if ( isset( $old_user_data->data ) ) { |
90
|
|
|
$old_user = $old_user_data->data; |
91
|
|
|
} else { |
92
|
|
|
$old_user = $old_user_data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ( $old_user !== null ) { |
96
|
|
|
unset( $old_user->user_pass ); |
97
|
|
|
if ( serialize( $old_user ) === serialize( $user->data ) ) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
/** |
102
|
|
|
* Fires when the client needs to sync an updated user |
103
|
|
|
* |
104
|
|
|
* @since 4.2.0 |
105
|
|
|
* |
106
|
|
|
* @param object The WP_User object |
107
|
|
|
*/ |
108
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
|
|
|
|
112
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Fires when the client needs to sync an updated user |
116
|
|
|
* |
117
|
|
|
* @since 4.2.0 |
118
|
|
|
* |
119
|
|
|
* @param object The WP_User object |
120
|
|
|
*/ |
121
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
|
|
|
|
125
|
|
|
|
126
|
|
|
// if a user is currently being removed as a member of this blog, we don't fire the event |
127
|
|
|
if ( current_filter() === 'deleted_user_meta' |
128
|
|
|
&& |
129
|
|
|
preg_match( '/capabilities|user_level/', $meta_key ) |
130
|
|
|
&& |
131
|
|
|
! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
136
|
|
|
if ( $meta_key === $user->cap_key ) { |
137
|
|
|
/** |
138
|
|
|
* Fires when the client needs to sync an updated user |
139
|
|
|
* |
140
|
|
|
* @since 4.2.0 |
141
|
|
|
* |
142
|
|
|
* @param object The WP_User object |
143
|
|
|
*/ |
144
|
|
|
do_action( 'jetpack_sync_save_user', $user ); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function enqueue_full_sync_actions() { |
149
|
|
|
global $wpdb; |
150
|
|
|
return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->users, 'ID', null ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
function get_full_sync_actions() { |
154
|
|
|
return array( 'jetpack_full_sync_users' ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function expand_users( $args ) { |
158
|
|
|
$user_ids = $args[0]; |
159
|
|
|
return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) ); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|