Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { |
||
| 4 | const MAX_INITIAL_SYNC_USERS = 100; |
||
| 5 | |||
| 6 | function name() { |
||
| 7 | return 'users'; |
||
| 8 | } |
||
| 9 | |||
| 10 | // this is here to support the backfill API |
||
| 11 | public function get_object_by_id( $object_type, $id ) { |
||
| 12 | if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) { |
||
| 13 | return $this->sanitize_user_and_expand( $user ); |
||
| 14 | } |
||
| 15 | |||
| 16 | return false; |
||
| 17 | } |
||
| 18 | |||
| 19 | public function init_listeners( $callable ) { |
||
| 20 | // users |
||
| 21 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 22 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 23 | add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
||
| 24 | add_action( 'jetpack_sync_save_user', $callable, 10, 2 ); |
||
| 25 | add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); |
||
| 26 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); |
||
| 27 | |||
| 28 | add_action( 'deleted_user', $callable, 10, 2 ); |
||
| 29 | add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
||
| 30 | |||
| 31 | // user roles |
||
| 32 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 33 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 34 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 35 | |||
| 36 | // user capabilities |
||
| 37 | add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 38 | add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 39 | add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 40 | |||
| 41 | // user authentication |
||
| 42 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 43 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 44 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 45 | } |
||
| 46 | |||
| 47 | public function init_full_sync_listeners( $callable ) { |
||
| 48 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function init_before_send() { |
||
| 52 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
||
| 53 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 55 | |||
| 56 | // full sync |
||
| 57 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function sanitize_user_and_expand( $user ) { |
||
| 61 | $user = $this->sanitize_user( $user ); |
||
| 62 | |||
| 63 | return $this->add_to_user( $user ); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function sanitize_user( $user ) { |
||
| 67 | // this create a new user object and stops the passing of the object by reference. |
||
| 68 | $user = unserialize( serialize( $user ) ); |
||
| 69 | |||
| 70 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 71 | unset( $user->data->user_pass ); |
||
| 72 | } |
||
| 73 | |||
| 74 | return $user; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function add_to_user( $user ) { |
||
| 78 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 79 | |||
| 80 | if ( function_exists( 'get_user_locale' ) ) { |
||
| 81 | |||
| 82 | // Only set the user locale if it is different from the site local |
||
| 83 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
||
| 84 | $user->locale = get_user_locale( $user->ID ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | return $user; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function expand_user( $args ) { |
||
| 91 | list( $user ) = $args; |
||
| 92 | |||
| 93 | if ( $user ) { |
||
| 94 | return array( $this->add_to_user( $user ) ); |
||
| 95 | } |
||
| 96 | |||
| 97 | return false; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function expand_login_username( $args ) { |
||
| 101 | list( $login, $user ) = $args; |
||
| 102 | $user = $this->sanitize_user( $user ); |
||
| 103 | |||
| 104 | return array( $login, $user ); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function expand_logout_username( $args, $user_id ) { |
||
| 108 | $user = get_userdata( $user_id ); |
||
| 109 | $user = $this->sanitize_user( $user ); |
||
| 110 | $login = ''; |
||
| 111 | if( is_object( $user ) && is_object( $user->data ) ) { |
||
| 112 | $login = $user->data->user_login; |
||
| 113 | } |
||
| 114 | |||
| 115 | return array( $login, $user ); |
||
| 116 | } |
||
| 117 | |||
| 118 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 119 | // ensure we only sync users who are members of the current blog |
||
| 120 | if ( ! 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 | |||
| 126 | // Older versions of WP don't pass the old_user_data in ->data |
||
| 127 | if ( isset( $old_user_data->data ) ) { |
||
| 128 | $old_user = $old_user_data->data; |
||
| 129 | } else { |
||
| 130 | $old_user = $old_user_data; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ( $old_user !== null ) { |
||
| 134 | unset( $old_user->user_pass ); |
||
| 135 | if ( serialize( $old_user ) === serialize( $user->data ) ) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | /** |
||
| 140 | * Fires when the client needs to sync an updated user |
||
| 141 | * |
||
| 142 | * @since 4.2.0 |
||
| 143 | * |
||
| 144 | * @param object The WP_User object |
||
| 145 | */ |
||
| 146 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 147 | } |
||
| 148 | |||
| 149 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 150 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Fires when the client needs to sync an updated user |
||
| 154 | * |
||
| 155 | * @since 4.2.0 |
||
| 156 | * |
||
| 157 | * @param object The WP_User object |
||
| 158 | */ |
||
| 159 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 160 | } |
||
| 161 | |||
| 162 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 163 | if ( $meta_key === 'locale' ) { |
||
| 164 | if ( current_filter() === 'deleted_user_meta' ) { |
||
| 165 | /** |
||
| 166 | * Allow listeners to listen for user local delete changes |
||
| 167 | * |
||
| 168 | * @since 4.8 |
||
| 169 | * |
||
| 170 | * @param int $user_id - The ID of the user whos locale is being deleted |
||
| 171 | */ |
||
| 172 | do_action( 'jetpack_sync_user_locale_delete', $user_id ); |
||
| 173 | } else { |
||
| 174 | /** |
||
| 175 | * Allow listeners to listen for user local changes |
||
| 176 | * |
||
| 177 | * @since 4.8 |
||
| 178 | * |
||
| 179 | * @param int $user_id - The ID of the user whos locale is being changed |
||
| 180 | * @param int $value - The value of the new locale |
||
| 181 | * |
||
| 182 | */ |
||
| 183 | do_action( 'jetpack_sync_user_locale', $user_id, $value ); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value ); |
||
| 187 | } |
||
| 188 | |||
| 189 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
|
0 ignored issues
–
show
|
|||
| 190 | // if a user is currently being removed as a member of this blog, we don't fire the event |
||
| 191 | if ( current_filter() === 'deleted_user_meta' |
||
| 192 | && |
||
| 193 | preg_match( '/capabilities|user_level/', $meta_key ) |
||
| 194 | && |
||
| 195 | ! is_user_member_of_blog( $user_id, get_current_blog_id() ) |
||
| 196 | ) { |
||
| 197 | return; |
||
| 198 | } |
||
| 199 | |||
| 200 | $user = get_user_by( 'id', $user_id ); |
||
| 201 | if ( $meta_key === $user->cap_key ) { |
||
| 202 | /** |
||
| 203 | * Fires when the client needs to sync an updated user |
||
| 204 | * |
||
| 205 | * @since 4.2.0 |
||
| 206 | * |
||
| 207 | * @param object The Sanitized WP_User object |
||
| 208 | */ |
||
| 209 | do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) ); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 214 | global $wpdb; |
||
| 215 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->usermeta, 'user_id', $this->get_where_sql( $config ), $max_items_to_enqueue, $state ); |
||
| 216 | } |
||
| 217 | |||
| 218 | View Code Duplication | public function estimate_full_sync_actions( $config ) { |
|
| 219 | global $wpdb; |
||
| 220 | |||
| 221 | $query = "SELECT count(*) FROM $wpdb->usermeta"; |
||
| 222 | |||
| 223 | if ( $where_sql = $this->get_where_sql( $config ) ) { |
||
| 224 | $query .= ' WHERE ' . $where_sql; |
||
| 225 | } |
||
| 226 | |||
| 227 | $count = $wpdb->get_var( $query ); |
||
| 228 | |||
| 229 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
| 230 | } |
||
| 231 | |||
| 232 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 233 | global $wpdb; |
||
| 234 | |||
| 235 | $query = "meta_key = '{$wpdb->prefix}capabilities'"; |
||
| 236 | |||
| 237 | // config is a list of user IDs to sync |
||
| 238 | if ( is_array( $config ) ) { |
||
| 239 | $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
||
| 240 | } |
||
| 241 | |||
| 242 | return $query; |
||
| 243 | } |
||
| 244 | |||
| 245 | function get_full_sync_actions() { |
||
| 246 | return array( 'jetpack_full_sync_users' ); |
||
| 247 | } |
||
| 248 | |||
| 249 | function get_initial_sync_user_config() { |
||
| 250 | global $wpdb; |
||
| 251 | |||
| 252 | $user_ids = $wpdb->get_col( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}user_level' AND meta_value > 0 LIMIT " . ( self::MAX_INITIAL_SYNC_USERS + 1 ) ); |
||
| 253 | |||
| 254 | if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) { |
||
| 255 | return $user_ids; |
||
| 256 | } else { |
||
| 257 | return false; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | public function expand_users( $args ) { |
||
| 262 | $user_ids = $args[0]; |
||
| 263 | |||
| 264 | return array_map( array( $this, 'sanitize_user_and_expand' ), get_users( array( 'include' => $user_ids ) ) ); |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.