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:
| 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_save_user', $callable, 10, 2 ); |
||
| 25 | |||
| 26 | add_action( 'deleted_user', $callable, 10, 2 ); |
||
| 27 | add_action( 'remove_user_from_blog', $callable, 10, 2 ); |
||
| 28 | |||
| 29 | // user roles |
||
| 30 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 31 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 32 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 33 | |||
| 34 | // user capabilities |
||
| 35 | add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 36 | add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 37 | add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 38 | |||
| 39 | // user authentication |
||
| 40 | add_action( 'wp_login', $callable, 10, 2 ); |
||
| 41 | add_action( 'wp_login_failed', $callable, 10, 2 ); |
||
| 42 | add_action( 'wp_logout', $callable, 10, 0 ); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function init_full_sync_listeners( $callable ) { |
||
| 46 | add_action( 'jetpack_full_sync_users', $callable ); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function init_before_send() { |
||
| 57 | |||
| 58 | public function sanitize_user_and_expand( $user ) { |
||
| 59 | $user = $this->sanitize_user( $user ); |
||
| 60 | |||
| 61 | return $this->add_to_user( $user ); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function sanitize_user( $user ) { |
||
| 65 | // this create a new user object and stops the passing of the object by reference. |
||
| 66 | $user = unserialize( serialize( $user ) ); |
||
| 67 | unset( $user->data->user_pass ); |
||
| 68 | |||
| 69 | return $user; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function add_to_user( $user ) { |
||
| 73 | $user->allowed_mime_types = get_allowed_mime_types( $user ); |
||
| 74 | |||
| 75 | return $user; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function expand_user( $args ) { |
||
| 79 | list( $user ) = $args; |
||
| 80 | |||
| 81 | if ( $user ) { |
||
| 82 | return array( $this->add_to_user( $user ) ); |
||
| 83 | } |
||
| 84 | |||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function expand_login_username( $args ) { |
||
| 89 | list( $login, $user ) = $args; |
||
| 90 | $user = $this->sanitize_user( $user ); |
||
| 91 | |||
| 92 | return array( $login, $user ); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function expand_logout_username( $args, $user_id ) { |
||
| 96 | $user = get_userdata( $user_id ); |
||
| 97 | $user = $this->sanitize_user( $user ); |
||
| 98 | $login = $user->data->user_login; |
||
| 99 | |||
| 100 | return array( $login, $user ); |
||
| 101 | } |
||
| 102 | |||
| 103 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 134 | |||
| 135 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 136 | $user = $this->sanitize_user( get_user_by( 'id', $user_id ) ); |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Fires when the client needs to sync an updated user |
||
| 140 | * |
||
| 141 | * @since 4.2.0 |
||
| 142 | * |
||
| 143 | * @param object The WP_User object |
||
| 144 | */ |
||
| 145 | do_action( 'jetpack_sync_save_user', $user ); |
||
| 146 | } |
||
| 147 | |||
| 148 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 149 | |||
| 150 | // if a user is currently being removed as a member of this blog, we don't fire the event |
||
| 151 | if ( current_filter() === 'deleted_user_meta' |
||
| 152 | && |
||
| 153 | preg_match( '/capabilities|user_level/', $meta_key ) |
||
| 154 | && |
||
| 155 | ! is_user_member_of_blog( $user_id, get_current_blog_id() ) |
||
| 156 | ) { |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | $user = get_user_by( 'id', $user_id ); |
||
| 161 | if ( $meta_key === $user->cap_key ) { |
||
| 162 | /** |
||
| 163 | * Fires when the client needs to sync an updated user |
||
| 164 | * |
||
| 165 | * @since 4.2.0 |
||
| 166 | * |
||
| 167 | * @param object The Sanitized WP_User object |
||
| 168 | */ |
||
| 169 | do_action( 'jetpack_sync_save_user', $this->sanitize_user( $user ) ); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | public function enqueue_full_sync_actions( $config ) { |
||
| 174 | global $wpdb; |
||
| 175 | return $this->enqueue_all_ids_as_action( 'jetpack_full_sync_users', $wpdb->usermeta, 'user_id', $this->get_where_sql( $config ) ); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function estimate_full_sync_actions( $config ) { |
||
| 179 | global $wpdb; |
||
| 180 | |||
| 181 | $query = "SELECT count(*) FROM $wpdb->usermeta"; |
||
| 182 | |||
| 183 | if ( $where_sql = $this->get_where_sql( $config ) ) { |
||
| 184 | $query .= ' WHERE ' . $where_sql; |
||
| 185 | } |
||
| 186 | |||
| 187 | $count = $wpdb->get_var( $query ); |
||
| 188 | |||
| 189 | return (int) ceil( $count / self::ARRAY_CHUNK_SIZE ); |
||
| 190 | } |
||
| 191 | |||
| 192 | View Code Duplication | private function get_where_sql( $config ) { |
|
| 193 | global $wpdb; |
||
| 194 | |||
| 195 | $query = "meta_key = '{$wpdb->prefix}capabilities'"; |
||
| 196 | |||
| 197 | // config is a list of user IDs to sync |
||
| 198 | if ( is_array( $config ) ) { |
||
| 199 | $query .= ' AND user_id IN (' . implode( ',', array_map( 'intval', $config ) ) . ')'; |
||
| 200 | } |
||
| 201 | |||
| 202 | return $query; |
||
| 203 | } |
||
| 204 | |||
| 205 | function get_full_sync_actions() { |
||
| 208 | |||
| 209 | function get_initial_sync_user_config() { |
||
| 210 | global $wpdb; |
||
| 211 | |||
| 212 | $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 ) ); |
||
| 213 | |||
| 214 | if ( count( $user_ids ) <= self::MAX_INITIAL_SYNC_USERS ) { |
||
| 215 | return $user_ids; |
||
| 216 | } else { |
||
| 217 | return false; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | public function expand_users( $args ) { |
||
| 222 | $user_ids = $args[0]; |
||
| 226 | } |
||
| 227 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.