Completed
Push — branch-4.2 ( 0eaf06...5c7a26 )
by Jeremy
25:35 queued 16:13
created

Jetpack_Sync_Module_Users::save_user_handler()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 31
rs 8.439
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_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 );
17
		add_action( 'remove_user_from_blog', $callable, 10, 2 );
18
19
		// user roles
20
		add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
21
		add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 );
22
		add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 );
23
24
		// user capabilities
25
		add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
26
		add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
27
		add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 );
28
29
		// full sync
30
		add_action( 'jetpack_full_sync_users', $callable );
31
	}
32
33
	public function sanitize_user_and_expand( $user ) {
34
		$user = $this->sanitize_user( $user );
35
		return $this->add_to_user( $user );
36
	}
37
38
	public function sanitize_user( $user ) {
39
		unset( $user->data->user_pass );
40
		return $user;
41
	}
42
43
	public function add_to_user( $user ) {
44
		$user->allowed_mime_types = get_allowed_mime_types( $user );
45
		return $user;
46
	}
47
48
	public function expand_user( $args ) {
49
		list( $user ) = $args;
50
		return array( $this->add_to_user( $user ) );
51
	}
52
53
	function save_user_handler( $user_id, $old_user_data = null ) {
54
55
		// ensure we only sync users who are members of the current blog
56
		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
57
			return;
58
		}
59
60
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
61
62
		// Older versions of WP don't pass the old_user_data in ->data
63
		if ( isset( $old_user_data->data ) ) {
64
			$old_user = $old_user_data->data;
65
		} else {
66
			$old_user = $old_user_data;
67
		}
68
69
		if ( $old_user !== null ) {
70
			unset( $old_user->user_pass );
71
			if ( serialize( $old_user ) === serialize( $user->data ) ) {
72
				return;
73
			}
74
		}
75
		/**
76
		 * Fires when the client needs to sync an updated user
77
		 *
78
		 * @since 4.2.0
79
		 *
80
		 * @param object The WP_User object
81
		 */
82
		do_action( 'jetpack_sync_save_user', $user );
83
	}
84
85
	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...
86
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
87
88
		/**
89
		 * Fires when the client needs to sync an updated user
90
		 *
91
		 * @since 4.2.0
92
		 *
93
		 * @param object The WP_User object
94
		 */
95
		do_action( 'jetpack_sync_save_user', $user );
96
	}
97
98
	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...
99
100
		// if a user is currently being removed as a member of this blog, we don't fire the event
101
		if ( current_filter() === 'deleted_user_meta' 
102
		&&
103
			preg_match( '/capabilities|user_level/', $meta_key )
104
		&& 
105
			! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) {
106
			return;
107
		}
108
109
		$user = $this->sanitize_user( get_user_by( 'id', $user_id ) );
110
		if ( $meta_key === $user->cap_key ) {
111
			/**
112
			 * Fires when the client needs to sync an updated user
113
			 *
114
			 * @since 4.2.0
115
			 *
116
			 * @param object The WP_User object
117
			 */
118
			do_action( 'jetpack_sync_save_user', $user );
119
		}
120
	}
121
}