PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.3.2
Jetpack – WP Security, Backup, Speed, & Growth v12.3.2
12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 14.4.2 All 500 releases
jetpack / jetpack_vendor / automattic / jetpack-sync / src / class-users.php
class-users.php
153 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sync for users.
4 *
5 * @package automattic/jetpack-sync
6 */
7
8 namespace Automattic\Jetpack\Sync;
9
10 use Automattic\Jetpack\Connection\Manager as Jetpack_Connection;
11 use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
12 use Automattic\Jetpack\Roles;
13
14 /**
15 * Class Users.
16 *
17 * Responsible for syncing user data changes.
18 */
19 class Users {
20 /**
21 * Roles of all users, indexed by user ID.
22 *
23 * @access public
24 * @static
25 *
26 * @var array
27 */
28 public static $user_roles = array();
29
30 /**
31 * Initialize sync for user data changes.
32 *
33 * @access public
34 * @static
35 * @todo Eventually, connection needs to be instantiated at the top level in the sync package.
36 */
37 public static function init() {
38 add_action( 'jetpack_user_authorized', array( 'Automattic\\Jetpack\\Sync\\Actions', 'do_initial_sync' ), 10, 0 );
39 $connection = new Jetpack_Connection();
40 if ( $connection->has_connected_user() ) {
41 // Kick off synchronization of user role when it changes.
42 add_action( 'set_user_role', array( __CLASS__, 'user_role_change' ) );
43 }
44 }
45
46 /**
47 * Synchronize connected user role changes.
48 *
49 * @access public
50 * @static
51 *
52 * @param int $user_id ID of the user.
53 */
54 public static function user_role_change( $user_id ) {
55 $connection = new Jetpack_Connection();
56 if ( $connection->is_user_connected( $user_id ) ) {
57 self::update_role_on_com( $user_id );
58 // Try to choose a new master if we're demoting the current one.
59 self::maybe_demote_master_user( $user_id );
60 }
61 }
62
63 /**
64 * Retrieve the role of a user by their ID.
65 *
66 * @access public
67 * @static
68 *
69 * @param int $user_id ID of the user.
70 * @return string Role of the user.
71 */
72 public static function get_role( $user_id ) {
73 if ( isset( self::$user_roles[ $user_id ] ) ) {
74 return self::$user_roles[ $user_id ];
75 }
76
77 $current_user_id = get_current_user_id();
78 wp_set_current_user( $user_id );
79 $roles = new Roles();
80 $role = $roles->translate_current_user_to_role();
81 wp_set_current_user( $current_user_id );
82 self::$user_roles[ $user_id ] = $role;
83
84 return $role;
85 }
86
87 /**
88 * Retrieve the signed role of a user by their ID.
89 *
90 * @access public
91 * @static
92 *
93 * @param int $user_id ID of the user.
94 * @return string Signed role of the user.
95 */
96 public static function get_signed_role( $user_id ) {
97 $connection = new Jetpack_Connection();
98 return $connection->sign_role( self::get_role( $user_id ), $user_id );
99 }
100
101 /**
102 * Retrieve the signed role and update it in WP.com for that user.
103 *
104 * @access public
105 * @static
106 *
107 * @param int $user_id ID of the user.
108 */
109 public static function update_role_on_com( $user_id ) {
110 $signed_role = self::get_signed_role( $user_id );
111 XMLRPC_Async_Call::add_call( 'jetpack.updateRole', get_current_user_id(), $user_id, $signed_role );
112 }
113
114 /**
115 * Choose a new master user if we're demoting the current one.
116 *
117 * @access public
118 * @static
119 * @todo Disconnect if there is no user with enough capabilities to be the master user.
120 * @uses \WP_User_Query
121 *
122 * @param int $user_id ID of the user.
123 */
124 public static function maybe_demote_master_user( $user_id ) {
125 $master_user_id = (int) \Jetpack_Options::get_option( 'master_user' );
126 $role = self::get_role( $user_id );
127 if ( $user_id === $master_user_id && 'administrator' !== $role ) {
128 $query = new \WP_User_Query(
129 array(
130 'fields' => array( 'ID' ),
131 'role' => 'administrator',
132 'orderby' => 'ID',
133 'exclude' => array( $master_user_id ),
134 )
135 );
136 $new_master = false;
137 $connection = new Jetpack_Connection();
138 foreach ( $query->results as $result ) {
139 $found_user_id = absint( $result->ID );
140 if ( $found_user_id && $connection->is_user_connected( $found_user_id ) ) {
141 $new_master = $found_user_id;
142 break;
143 }
144 }
145
146 if ( $new_master ) {
147 \Jetpack_Options::update_option( 'master_user', $new_master );
148 }
149 // TODO: else disconnect..?
150 }
151 }
152 }
153