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