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 | static $callable; |
||
|
|
|||
| 7 | |||
| 8 | // Stores things that happen inside wp_insert_user so we sync them after the wp_insert_user call |
||
| 9 | static $calls_within_wp_insert_user = array(); |
||
| 10 | |||
| 11 | function name() { |
||
| 12 | return 'users'; |
||
| 13 | } |
||
| 14 | |||
| 15 | // this is here to support the backfill API |
||
| 16 | public function get_object_by_id( $object_type, $id ) { |
||
| 17 | if ( $object_type === 'user' && $user = get_user_by( 'id', intval( $id ) ) ) { |
||
| 18 | return $user; |
||
| 19 | } |
||
| 20 | |||
| 21 | return false; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function init_listeners( $callable ) { |
||
| 25 | self::$callable = $callable; |
||
| 26 | |||
| 27 | add_action( 'user_register', array( $this, 'do_delayed_calls' ), 99 ); |
||
| 28 | add_action( 'profile_update', array( $this, 'do_delayed_calls' ), 99 ); |
||
| 29 | |||
| 30 | // users |
||
| 31 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 32 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 33 | add_action( 'add_user_to_blog', array( $this, 'save_user_handler' ) ); |
||
| 34 | add_action( 'jetpack_sync_add_user', $callable, 10, 2 ); |
||
| 35 | add_action( 'jetpack_sync_register_user', $callable, 10, 2 ); |
||
| 36 | add_action( 'jetpack_sync_save_user', array( $this, 'delay_if_within_wp_insert_user' ) ); |
||
| 37 | |||
| 38 | add_action( 'jetpack_sync_change_role_user', array( $this, 'delay_if_within_wp_insert_user' ), 10, 3 ); |
||
| 39 | |||
| 40 | add_action( 'jetpack_sync_user_locale', array( $this, 'delay_if_within_wp_insert_user' ), 10, 2 ); |
||
| 41 | |||
| 42 | add_action( 'jetpack_sync_user_locale_delete', $callable, 10, 1 ); |
||
| 43 | |||
| 44 | add_action( 'deleted_user', array( $this, 'deleted_user_handler' ), 10, 2 ); |
||
| 45 | add_action( 'jetpack_deleted_user', $callable, 10, 3 ); |
||
| 46 | add_action( 'remove_user_from_blog', array( $this, 'remove_user_from_blog_handler' ), 10, 2 ); |
||
| 47 | add_action( 'jetpack_removed_user_from_blog', $callable, 10, 2 ); |
||
| 48 | |||
| 49 | // user roles |
||
| 50 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 51 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 52 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 53 | |||
| 54 | // user capabilities |
||
| 55 | add_action( 'added_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 56 | add_action( 'updated_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 57 | add_action( 'deleted_user_meta', array( $this, 'maybe_save_user_meta' ), 10, 4 ); |
||
| 58 | |||
| 59 | // user authentication |
||
| 60 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 61 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 62 | add_action( 'wp_masterbar_logout', $callable, 10, 0 ); |
||
| 63 | } |
||
| 64 | |||
| 65 | public function delay_if_within_wp_insert_user() { |
||
| 66 | if ( ! Jetpack::is_function_in_backtrace( 'do_delayed_calls' ) ) { |
||
| 67 | if ( Jetpack::is_function_in_backtrace( 'wp_insert_user' ) ) { |
||
| 68 | self::$calls_within_wp_insert_user[ current_action() ][] = func_get_args(); |
||
| 69 | |||
| 70 | return; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | call_user_func_array( self::$callable, func_get_args() ); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function do_delayed_calls() { |
||
| 77 | // We want the save user action to be synced first |
||
| 78 | if ( isset( self::$calls_within_wp_insert_user['jetpack_sync_save_user'] ) ) { |
||
| 79 | $jetpack_sync_save_user = self::$calls_within_wp_insert_user['jetpack_sync_save_user']; |
||
| 80 | unset( self::$calls_within_wp_insert_user['jetpack_sync_save_user'] ); |
||
| 81 | self::$calls_within_wp_insert_user = array_merge( array( 'jetpack_sync_save_user' => $jetpack_sync_save_user), self::$calls_within_wp_insert_user ); |
||
| 82 | } |
||
| 83 | foreach ( self::$calls_within_wp_insert_user as $action => $calls ) { |
||
| 84 | foreach ( $calls as $arguments ) { |
||
| 85 | if ( 'user_register' === current_filter() ) { |
||
| 86 | if ( in_array( $action, |
||
| 87 | array( |
||
| 88 | 'jetpack_sync_change_role_user', |
||
| 89 | 'jetpack_sync_user_locale', |
||
| 90 | ), true ) ) { |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | // do_action( $action, $arguments[0], $arguments[1], ... ); |
||
| 96 | call_user_func_array( 'do_action', array_merge( array( $action ), $arguments ) ); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | public function init_full_sync_listeners( $callable ) { |
||
| 102 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function init_before_send() { |
||
| 106 | add_filter( 'jetpack_sync_before_send_jetpack_sync_add_user', array( $this, 'expand_user' ) ); |
||
| 107 | add_filter( 'jetpack_sync_before_send_jetpack_sync_register_user', array( $this, 'expand_user' ) ); |
||
| 108 | add_filter( 'jetpack_sync_before_send_jetpack_sync_save_user', array( $this, 'expand_user' ), 10, 2 ); |
||
| 109 | |||
| 110 | add_filter( 'jetpack_sync_before_send_wp_login', array( $this, 'expand_login_username' ), 10, 1 ); |
||
| 111 | add_filter( 'jetpack_sync_before_send_wp_logout', array( $this, 'expand_logout_username' ), 10, 2 ); |
||
| 112 | |||
| 113 | // full sync |
||
| 114 | add_filter( 'jetpack_sync_before_send_jetpack_full_sync_users', array( $this, 'expand_users' ) ); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function sanitize_user_and_expand( $user ) { |
||
| 118 | $user = $this->get_user( $user ); |
||
| 119 | $user = $this->add_to_user( $user ); |
||
| 120 | return $this->sanitize_user( $user ); |
||
| 121 | } |
||
| 122 | |||
| 123 | private function get_user( $user ) { |
||
| 124 | error_log( 'get USEEEEEER'); |
||
| 125 | error_log( print_r($user,1)); |
||
| 126 | if ( $user && ! is_object( $user ) && is_numeric( $user ) ) { |
||
| 127 | $user = get_user_by( 'id', $user ); |
||
| 128 | } |
||
| 129 | if ( $user instanceof WP_User ) { |
||
| 130 | return $user; |
||
| 131 | } |
||
| 132 | return null; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function sanitize_user( $user ) { |
||
| 136 | $user = $this->get_user( $user ); |
||
| 137 | // this create a new user object and stops the passing of the object by reference. |
||
| 138 | $user = unserialize( serialize( $user ) ); |
||
| 139 | |||
| 140 | if ( is_object( $user ) && is_object( $user->data ) ) { |
||
| 141 | unset( $user->data->user_pass ); |
||
| 142 | unset( $user->data->user_activation_key ); |
||
| 143 | } |
||
| 144 | if ( $user ) { |
||
| 145 | $user->allcaps = $this->get_real_user_capabilities( $user ); |
||
| 146 | } |
||
| 147 | return $user; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function add_to_user( $user ) { |
||
| 167 | |||
| 168 | public function get_real_user_capabilities( $user ) { |
||
| 169 | $user_capabilities = array(); |
||
| 170 | if ( is_wp_error( $user ) ) { |
||
| 171 | return $user_capabilities; |
||
| 172 | } |
||
| 173 | foreach( Jetpack_Sync_Defaults::get_capabilities_whitelist() as $capability ) { |
||
| 174 | if ( $user_has_capabilities = user_can( $user , $capability ) ) { |
||
| 175 | $user_capabilities[ $capability ] = true; |
||
| 176 | } |
||
| 177 | } |
||
| 180 | |||
| 181 | public function expand_user( $args ) { |
||
| 193 | |||
| 194 | public function expand_login_username( $args ) { |
||
| 200 | |||
| 201 | public function expand_logout_username( $args, $user_id ) { |
||
| 216 | |||
| 217 | public function deleted_user_handler( $deleted_user_id, $reassigned_user_id = '' ) { |
||
| 230 | |||
| 231 | public function edited_user_handler( $user_id ) { |
||
| 241 | |||
| 242 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 299 | |||
| 300 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 310 | |||
| 311 | function maybe_save_user_meta( $meta_id, $user_id, $meta_key, $value ) { |
||
| 336 | |||
| 337 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 360 | |||
| 361 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 366 | |||
| 367 | public function estimate_full_sync_actions( $config ) { |
||
| 380 | |||
| 381 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 393 | |||
| 394 | function get_full_sync_actions() { |
||
| 397 | |||
| 398 | function get_initial_sync_user_config() { |
||
| 409 | |||
| 410 | public function expand_users( $args ) { |
||
| 415 | |||
| 416 | public function remove_user_from_blog_handler( $user_id, $blog_id ) { |
||
| 435 | |||
| 436 | private function is_add_new_user_to_blog() { |
||
| 439 | |||
| 440 | private function get_reassigned_network_user_id() { |
||
| 453 | } |
||
| 454 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.