Completed
Push — add/sync-options-checksums ( fe9c0b...b8783d )
by
unknown
20:47 queued 07:13
created

Jetpack_Sync_Module_Users::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
42
	public function sanitize_user_and_expand( $user ) {
43
		$user = $this->sanitize_user( $user );
44
		return $this->add_to_user( $user );
45
	}
46
47
	public function sanitize_user( $user ) {
48
		unset( $user->data->user_pass );
49
		return $user;
50
	}
51
52
	public function add_to_user( $user ) {
53
		$user->allowed_mime_types = get_allowed_mime_types( $user );
54
		return $user;
55
	}
56
57
	public function expand_user( $args ) {
58
		list( $user ) = $args;
59
		return array( $this->add_to_user( $user ) );
60
	}
61
62
	public function expand_logout_username( $args, $user_id ) {
63
		$username = get_userdata( $user_id )->user_login;
64
		$args[] = $username;
65
		return $args;
66
	}
67
68
	function save_user_handler( $user_id, $old_user_data = null ) {
69
70
		// ensure we only sync users who are members of the current blog
71
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
72
			return;
73
		}
74
75
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
76
77
		// Older versions of WP don't pass the old_user_data in ->data
78
		if ( isset( $old_user_data->data ) ) {
79
			$old_user = $old_user_data->data;
80
		} else {
81
			$old_user = $old_user_data;
82
		}
83
84
		if ( $old_user !== null ) {
85
			unset( $old_user->user_pass );
86
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
87
				return;
88
			}
89
		}
90
		/**
91
		 * Fires when the client needs to sync an updated user
92
		 *
93
		 * @since 4.2.0
94
		 *
95
		 * @param object The WP_User object
96
		 */
97
		do_action( 'jetpack_sync_save_user', $user );
98
	}
99
100
	function save_user_role_handler( $user_id, $role, $old_roles = null ) {
0 ignored issues
show
Unused Code introduced by
The parameter $role is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_roles is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
102
103
		/**
104
		 * Fires when the client needs to sync an updated user
105
		 *
106
		 * @since 4.2.0
107
		 *
108
		 * @param object The WP_User object
109
		 */
110
		do_action( 'jetpack_sync_save_user', $user );
111
	}
112
113
	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) {
0 ignored issues
show
Unused Code introduced by
The parameter $capabilities is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
114
115
		// if a user is currently being removed as a member of this blog, we don't fire the event
116
		if ( current_filter() === 'deleted_user_meta'
117
		&&
118
			preg_match( '/capabilities|user_level/', $meta_key )
119
		&&
120
			! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
121
			return;
122
		}
123
124
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
125
		if ( $meta_key === $user->cap_key ) {
126
			/**
127
			 * Fires when the client needs to sync an updated user
128
			 *
129
			 * @since 4.2.0
130
			 *
131
			 * @param object The WP_User object
132
			 */
133
			do_action( 'jetpack_sync_save_user', $user );
134
		}
135
	}
136
}
137