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( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 ); |
||
| 34 | add_action( 'jetpack_deleted_user', $callable, 10, 3 ); |
||
| 35 | add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 ); |
||
| 36 | add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 ); |
||
| 37 | |||
| 38 | // user authentication |
||
| 39 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 40 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 41 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 42 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); |
||
| 43 | |||
| 44 | // listen for meta changes ( locale, roles and capabilities ) |
||
| 45 | $this->init_listeners_for_meta_type( 'user', $callable ); |
||
| 46 | $this->init_meta_whitelist_handler( 'user', array( $this, 'filter_meta' ) ); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function filter_meta( $args ) { |
||
| 50 | if ( $this->is_whitelisted_user_meta( $args[2] ) ) { |
||
| 51 | return $args; |
||
| 52 | } |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function is_whitelisted_user_meta( $meta_key ) { |
||
| 57 | $user_meta_keys = (array)Jetpack_Sync_Settings::get_setting( 'user_meta_whitelist' ); |
||
| 58 | $user_keys = array_map( array( $this, 'map_user_meta_key' ), $user_meta_keys ); |
||
| 59 | |||
| 60 | return in_array( $meta_key, $user_keys ); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function map_user_meta_key( $key ) { |
||
| 64 | global $wpdb; |
||
| 65 | return str_replace( '*_', $wpdb->get_blog_prefix(), $key ); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function init_full_sync_listeners( $callable ) { |
||
| 69 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function init_before_send() { |
||
| 73 | add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) ); |
||
| 74 | add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) ); |
||
| 75 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 ); |
||
| 76 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 77 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 78 | |||
| 79 | // full sync |
||
| 80 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function sanitize_user_and_expand( $user ) { |
||
| 84 | $user = $this->sanitize_user( $user ); |
||
| 85 | |||
| 86 | return $this->add_to_user( $user ); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function sanitize_user( $user ) { |
||
| 90 | // this create a new user object and stops the passing of the object by reference. |
||
| 91 | $user = unserialize( serialize( $user ) ); |
||
| 92 | |||
| 93 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 94 | unset( $user->data->user_pass ); |
||
| 95 | unset( $user->cap_key ); // |
||
| 96 | unset( $user->allcaps ); // We should be able to constuct this from the roles data that we have. |
||
| 97 | |||
| 98 | if ( isset( $user->filter ) ) { |
||
| 99 | unset( $user->filter ); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | return $user; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function add_to_user( $user ) { |
||
| 106 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 107 | return $user; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function expand_user( $args ) { |
||
| 111 | list( $user ) = $args; |
||
| 112 | |||
| 113 | if ( $user ) { |
||
| 114 | |||
| 115 | return array( $this->sanitize_user_and_expand( $user ) ); |
||
| 116 | } |
||
| 117 | |||
| 118 | return false; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function expand_login_username( $args ) { |
||
| 127 | |||
| 128 | public function expand_logout_username( $args, $user_id ) { |
||
| 138 | |||
| 139 | public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) { |
||
| 140 | $is_multisite = is_multisite(); |
||
| 141 | /** |
||
| 142 | * Fires when a user is deleted on a site |
||
| 143 | * |
||
| 144 | * @since 5.4.0 |
||
| 145 | * |
||
| 146 | * @param int $deleted_user_id - ID of the deleted user |
||
| 147 | * @param int $reassigned_user_id - ID of the user the deleted user's posts is reassigned to (if any) |
||
| 148 | * @param bool $is_multisite - Whether this site is a multisite installation |
||
| 152 | |||
| 153 | public function edited_user_handler( $user_id ) { |
||
| 163 | |||
| 164 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 221 | |||
| 222 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 238 | |||
| 239 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 264 | |||
| 265 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 293 | |||
| 294 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 299 | |||
| 300 | public function estimate_full_sync_actions( $config ) { |
||
| 313 | |||
| 314 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 326 | |||
| 327 | function get_full_sync_actions() { |
||
| 330 | |||
| 331 | function get_initial_sync_user_config() { |
||
| 342 | |||
| 343 | public function expand_users( $args ) { |
||
| 348 | |||
| 349 | public function remove_user_from_blog_handler( $user_id, $blog_id ) { |
||
| 368 | |||
| 369 | private function is_add_new_user_to_blog() { |
||
| 372 | |||
| 373 | private function is_add_user_to_blog() { |
||
| 376 | |||
| 377 | private function is_create_user() { |
||
| 386 | |||
| 387 | private function get_reassigned_network_user_id() { |
||
| 400 | } |
||
| 401 |