Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Sync_Module_Users often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Sync_Module_Users, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 3 | class Jetpack_Sync_Module_Users extends Jetpack_Sync_Module { | ||
| 4 | const MAX_INITIAL_SYNC_USERS = 100; | ||
| 5 | |||
| 6 | 	function name() { | ||
| 9 | |||
| 10 | // this is here to support the backfill API | ||
| 11 | 	public function get_object_by_id( $object_type, $id ) { | ||
| 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_add_user', $callable, 10, 2 ); | ||
| 25 | add_action( 'jetpack_sync_register_user', $callable, 10, 2 ); | ||
| 26 | add_action( 'jetpack_sync_save_user', $callable ); | ||
| 27 | |||
| 28 | //Edit user info, see https://github.com/WordPress/WordPress/blob/c05f1dc805bddcc0e76fd90c4aaf2d9ea76dc0fb/wp-admin/user-edit.php#L126 | ||
| 29 | add_action( 'personal_options_update', array( $this, 'edited_user_handler' ) ); | ||
| 30 | add_action( 'edit_user_profile_update', array( $this, 'edited_user_handler' ) ); | ||
| 31 | add_action( 'jetpack_user_edited', $callable ); | ||
| 32 | |||
| 33 | add_action( 'jetpack_sync_user_locale', $callable, 10, 2 ); | ||
| 34 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); | ||
| 35 | |||
| 36 | add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 ); | ||
| 37 | add_action( 'jetpack_deleted_user', $callable, 10, 3 ); | ||
| 38 | add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 ); | ||
| 39 | add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 ); | ||
| 40 | |||
| 41 | // user roles | ||
| 42 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); | ||
| 43 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); | ||
| 44 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); | ||
| 45 | |||
| 46 | // user capabilities | ||
| 47 | add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); | ||
| 48 | add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); | ||
| 49 | add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); | ||
| 50 | |||
| 51 | // user authentication | ||
| 52 | add_action( 'wp_login', $callable, 10, 2 ); | ||
| 53 | add_action( 'wp_login_failed', $callable, 10, 2 ); | ||
| 54 | add_action( 'wp_logout', $callable, 10, 0 ); | ||
| 55 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); | ||
| 56 | } | ||
| 57 | |||
| 58 | 	public function init_full_sync_listeners( $callable ) { | ||
| 61 | |||
| 62 | 	public function init_before_send() { | ||
| 72 | |||
| 73 | 	public function sanitize_user_and_expand( $user ) { | ||
| 78 | |||
| 79 | 	public function sanitize_user( $user ) { | ||
| 89 | |||
| 90 | 	public function add_to_user( $user ) { | ||
| 103 | |||
| 104 | 	public function expand_user( $args ) { | ||
| 105 | list( $user ) = $args; | ||
| 106 | |||
| 107 | 		if ( $user ) { | ||
| 108 | return array( $this->add_to_user( $user ) ); | ||
| 109 | } | ||
| 110 | |||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | 	public function expand_login_username( $args ) { | ||
| 115 | list( $login, $user ) = $args; | ||
| 116 | $user = $this->sanitize_user( $user ); | ||
| 117 | |||
| 118 | return array( $login, $user ); | ||
| 119 | } | ||
| 120 | |||
| 121 | 	public function expand_logout_username( $args, $user_id ) { | ||
| 122 | $user = get_userdata( $user_id ); | ||
| 123 | $user = $this->sanitize_user( $user ); | ||
| 124 | $login = ''; | ||
| 125 | 		if ( is_object( $user ) && is_object( $user->data ) ) { | ||
| 126 | $login = $user->data->user_login; | ||
| 127 | } | ||
| 128 | |||
| 129 | return array( $login, $user ); | ||
| 130 | } | ||
| 131 | |||
| 132 | 	public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) { | ||
| 133 | do_action( 'jetpack_deleted_user', $deleted_user_id, $reassigned_user_id, is_multisite() ); | ||
| 134 | } | ||
| 135 | |||
| 136 | 	public function edited_user_handler( $user_id ) { | ||
| 137 | do_action( 'jetpack_user_edited', $user_id ); | ||
| 138 | } | ||
| 139 | |||
| 140 | 	function save_user_handler( $user_id, $old_user_data = null ) { | ||
| 141 | // ensure we only sync users who are members of the current blog | ||
| 142 | 		if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); | ||
| 147 | |||
| 148 | // Older versions of WP don't pass the old_user_data in ->data | ||
| 149 | 		if ( isset( $old_user_data->data ) ) { | ||
| 150 | $old_user = $old_user_data->data; | ||
| 151 | 		} else { | ||
| 152 | $old_user = $old_user_data; | ||
| 153 | } | ||
| 154 | |||
| 155 | 		if ( $old_user !== null ) { | ||
| 156 | unset( $old_user->user_pass ); | ||
| 157 | 			if ( serialize( $old_user ) === serialize( $user->data ) ) { | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | 		if ( 'user_register' === current_filter() ) { | ||
| 163 | /** | ||
| 164 | * Fires when a new user is registered on a site | ||
| 165 | * | ||
| 166 | * @since 4.9.0 | ||
| 167 | * | ||
| 168 | * @param object The WP_User object | ||
| 169 | */ | ||
| 170 | do_action( 'jetpack_sync_register_user', $user ); | ||
| 171 | |||
| 172 | return; | ||
| 173 | } | ||
| 174 | /* MU Sites add users instead of register them to sites */ | ||
| 175 | 		if ( 'add_user_to_blog' === current_filter() ) { | ||
| 176 | /** | ||
| 177 | * Fires when a new user is added to a site. (WordPress Multisite) | ||
| 178 | * | ||
| 179 | * @since 4.9.0 | ||
| 180 | * | ||
| 181 | * @param object The WP_User object | ||
| 182 | */ | ||
| 183 | do_action( 'jetpack_sync_add_user', $user ); | ||
| 184 | |||
| 185 | return; | ||
| 186 | } | ||
| 187 | |||
| 188 | /** | ||
| 189 | * Fires when the client needs to sync an updated user | ||
| 190 | * | ||
| 191 | * @since 4.2.0 | ||
| 192 | * | ||
| 193 | * @param object The WP_User object | ||
| 194 | */ | ||
| 195 | do_action( 'jetpack_sync_save_user', $user ); | ||
| 196 | } | ||
| 197 | |||
| 198 | 	function save_user_role_handler( $user_id, $role, $old_roles = null ) { | ||
| 199 | //The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both | ||
| 200 | 		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) { | ||
| 201 | return; | ||
| 202 | } | ||
| 203 | |||
| 204 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); | ||
| 205 | /** | ||
| 206 | * Fires when the client needs to sync an updated user | ||
| 207 | * | ||
| 208 | * @since 4.2.0 | ||
| 209 | * | ||
| 210 | * @param object The WP_User object | ||
| 211 | */ | ||
| 212 | do_action( 'jetpack_sync_save_user', $user ); | ||
| 213 | } | ||
| 214 | |||
| 215 | 	function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { | ||
| 216 | 		if ( $meta_key === 'locale' ) { | ||
| 217 | 			if ( current_filter() === 'deleted_user_meta' ) { | ||
| 218 | /** | ||
| 219 | * Allow listeners to listen for user local delete changes | ||
| 220 | * | ||
| 221 | * @since 4.8.0 | ||
| 222 | * | ||
| 223 | * @param int $user_id - The ID of the user whos locale is being deleted | ||
| 224 | */ | ||
| 225 | do_action( 'jetpack_sync_user_locale_delete', $user_id ); | ||
| 226 | 			} else { | ||
| 227 | /** | ||
| 228 | * Allow listeners to listen for user local changes | ||
| 229 | * | ||
| 230 | * @since 4.8.0 | ||
| 231 | * | ||
| 232 | * @param int $user_id - The ID of the user whos locale is being changed | ||
| 233 | * @param int $value - The value of the new locale | ||
| 234 | */ | ||
| 235 | do_action( 'jetpack_sync_user_locale', $user_id, $value ); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $this->save_user_cap_handler( $meta_id, $user_id, $meta_key, $value ); | ||
| 239 | } | ||
| 240 | |||
| 241 | 	function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { | ||
| 242 | //The jetpack_sync_register_user payload is identical to jetpack_sync_save_user, don't send both | ||
| 243 | 		if ( $this->is_create_user() || $this->is_add_user_to_blog() ) { | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | |||
| 247 | // if a user is currently being removed as a member of this blog, we don't fire the event | ||
| 248 | if ( current_filter() === 'deleted_user_meta' | ||
| 249 | && | ||
| 250 | preg_match( '/capabilities|user_level/', $meta_key ) | ||
| 251 | && | ||
| 252 | ! is_user_member_of_blog( $user_id, get_current_blog_id() ) | ||
| 253 | 		) { | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | $user = get_user_by( 'id', $user_id ); | ||
| 258 | 		if ( $meta_key === $user->cap_key ) { | ||
| 259 | /** | ||
| 260 | * Fires when the client needs to sync an updated user | ||
| 261 | * | ||
| 262 | * @since 4.2.0 | ||
| 263 | * | ||
| 264 | * @param object The Sanitized WP_User object | ||
| 265 | */ | ||
| 266 | do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) ); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | 	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { | ||
| 275 | |||
| 276 | 	public function estimate_full_sync_actions( $config ) { | ||
| 277 | global $wpdb; | ||
| 278 | |||
| 279 | $query = "SELECT count(*) FROM $wpdb->usermeta"; | ||
| 280 | |||
| 281 | 		if ( $where_sql = $this->get_where_sql( $config ) ) { | ||
| 282 | $query .= ' WHERE ' . $where_sql; | ||
| 283 | } | ||
| 284 | |||
| 285 | $count = $wpdb->get_var( $query ); | ||
| 286 | |||
| 287 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); | ||
| 288 | } | ||
| 289 | |||
| 290 | View Code Duplication | 	private function get_where_sql( $config ) { | |
| 302 | |||
| 303 | 	function get_full_sync_actions() { | ||
| 306 | |||
| 307 | 	function get_initial_sync_user_config() { | ||
| 318 | |||
| 319 | 	public function expand_users( $args ) { | ||
| 324 | |||
| 325 | 	public function remove_user_from_blog_handler( $user_id, $blog_id ) { | ||
| 326 | //User is removed on add, see https://github.com/WordPress/WordPress/blob/0401cee8b36df3def8e807dd766adc02b359dfaf/wp-includes/ms-functions.php#L2114 | ||
| 327 | 		if ( $this->is_add_new_user_to_blog() ) { | ||
| 328 | return; | ||
| 329 | } | ||
| 330 | |||
| 331 | $reassigned_user_id = $this->get_reassigned_network_user_id(); | ||
| 332 | |||
| 333 | //Note that we are in the context of the blog the user is removed from, see https://github.com/WordPress/WordPress/blob/473e1ba73bc5c18c72d7f288447503713d518790/wp-includes/ms-functions.php#L233 | ||
| 334 | do_action( 'jetpack_removed_user_from_blog', $user_id, $reassigned_user_id ); | ||
| 335 | } | ||
| 336 | |||
| 337 | 	private function is_add_new_user_to_blog() { | ||
| 338 | return Jetpack::is_function_in_backtrace( 'add_new_user_to_blog' ); | ||
| 339 | } | ||
| 340 | |||
| 341 | 	private function is_add_user_to_blog() { | ||
| 342 | return Jetpack::is_function_in_backtrace( 'add_user_to_blog' ); | ||
| 343 | } | ||
| 344 | |||
| 345 | 	private function is_create_user() { | ||
| 346 | $functions = array( | ||
| 347 | 'add_new_user_to_blog', // Used to suppress jetpack_sync_save_user in save_user_cap_handler when user registered on multi site | ||
| 348 | 'wp_create_user', // Used to suppress jetpack_sync_save_user in save_user_role_handler when user registered on multi site | ||
| 349 | 'wp_insert_user', // Used to suppress jetpack_sync_save_user in save_user_cap_handler and save_user_role_handler when user registered on single site | ||
| 350 | ); | ||
| 351 | |||
| 352 | return Jetpack::is_function_in_backtrace( $functions ); | ||
| 353 | } | ||
| 354 | |||
| 355 | 	private function get_reassigned_network_user_id() { | ||
| 368 | } | ||
| 369 |