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 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function init_full_sync_listeners( $callable ) { |
||
| 49 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function init_before_send() { |
||
| 53 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ) ); |
||
| 54 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 55 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 56 | |||
| 57 | // full sync |
||
| 58 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function sanitize_user_and_expand( $user ) { |
||
| 62 | $user = $this->sanitize_user( $user ); |
||
| 63 | |||
| 64 | return $this->add_to_user( $user ); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function sanitize_user( $user ) { |
||
| 68 | // this create a new user object and stops the passing of the object by reference. |
||
| 69 | $user = unserialize( serialize( $user ) ); |
||
| 70 | |||
| 71 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 72 | unset( $user->data->user_pass ); |
||
| 73 | } |
||
| 74 | |||
| 75 | return $user; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function add_to_user( $user ) { |
||
| 79 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 80 | |||
| 81 | if ( function_exists( 'get_user_locale' ) ) { |
||
| 82 | |||
| 83 | // Only set the user locale if it is different from the site local |
||
| 84 | if ( get_locale() !== get_user_locale( $user->ID ) ) { |
||
| 85 | $user->locale = get_user_locale( $user->ID ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | return $user; |
||
| 89 | } |
||
| 90 | |||
| 91 | public function expand_user( $args ) { |
||
| 92 | list( $user ) = $args; |
||
| 93 | |||
| 94 | if ( $user ) { |
||
| 95 | return array( $this->add_to_user( $user ) ); |
||
| 96 | } |
||
| 97 | |||
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function expand_login_username( $args ) { |
||
| 102 | list( $login, $user ) = $args; |
||
| 103 | $user = $this->sanitize_user( $user ); |
||
| 104 | |||
| 105 | return array( $login, $user ); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function expand_logout_username( $args, $user_id ) { |
||
| 109 | $user = get_userdata( $user_id ); |
||
| 110 | $user = $this->sanitize_user( $user ); |
||
| 111 | $login = ''; |
||
| 112 | if( is_object( $user ) && is_object( $user->data ) ) { |
||
| 113 | $login = $user->data->user_login; |
||
| 114 | } |
||
| 115 | |||
| 116 | return array( $login, $user ); |
||
| 117 | } |
||
| 118 | |||
| 119 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 120 | // ensure we only sync users who are members of the current blog |
||
| 121 | if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 126 | |||
| 127 | // Older versions of WP don't pass the old_user_data in ->data |
||
| 128 | if ( isset( $old_user_data->data ) ) { |
||
| 129 | $old_user = $old_user_data->data; |
||
| 130 | } else { |
||
| 131 | $old_user = $old_user_data; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ( $old_user !== null ) { |
||
| 135 | unset( $old_user->user_pass ); |
||
| 136 | if ( serialize( $old_user ) === serialize( $user->data ) ) { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | /** |
||
| 141 | * Fires when the client needs to sync an updated user |
||
| 142 | * |
||
| 143 | * @since 4.2.0 |
||
| 144 | * |
||
| 145 | * @param object The WP_User object |
||
| 146 | */ |
||
| 147 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 148 | } |
||
| 149 | |||
| 150 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
|
0 ignored issues
–
show
|
|||
| 151 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Fires when the client needs to sync an updated user |
||
| 155 | * |
||
| 156 | * @since 4.2.0 |
||
| 157 | * |
||
| 158 | * @param object The WP_User object |
||
| 159 | */ |
||
| 160 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 161 | } |
||
| 162 | |||
| 163 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 164 | if ( $meta_key === 'locale' ) { |
||
| 165 | if ( current_filter() === 'deleted_user_meta' ) { |
||
| 166 | /** |
||
| 167 | * Allow listeners to listen for user local delete changes |
||
| 168 | * |
||
| 169 | * @since 4.8.0 |
||
| 170 | * |
||
| 171 | * @param int $user_id - The ID of the user whos locale is being deleted |
||
| 172 | */ |
||
| 173 | do_action( 'jetpack_sync_user_locale_delete', $user_id ); |
||
| 174 | } else { |
||
| 175 | /** |
||
| 176 | * Allow listeners to listen for user local changes |
||
| 177 | * |
||
| 178 | * @since 4.8.0 |
||
| 179 | * |
||
| 180 | * @param int $user_id - The ID of the user whos locale is being changed |
||
| 181 | * @param int $value - The value of the new locale |
||
| 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.