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_Client 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_Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Jetpack_Sync_Client { |
||
| 9 | |||
| 10 | const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum'; |
||
| 11 | const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum'; |
||
| 12 | const SYNC_THROTTLE_OPTION_NAME = 'jetpack_sync_min_wait'; |
||
| 13 | const LAST_SYNC_TIME_OPTION_NAME = 'jetpack_last_sync_time'; |
||
| 14 | const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await'; |
||
| 15 | const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await'; |
||
| 16 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
| 17 | |||
| 18 | private static $valid_settings = array( 'dequeue_max_bytes' => true, 'upload_max_bytes' => true, 'upload_max_rows' => true, 'sync_wait_time' => true ); |
||
| 19 | |||
| 20 | private $dequeue_max_bytes; |
||
| 21 | private $upload_max_bytes; |
||
| 22 | private $upload_max_rows; |
||
| 23 | private $sync_queue; |
||
| 24 | private $full_sync_client; |
||
| 25 | private $codec; |
||
| 26 | private $options_whitelist; |
||
| 27 | private $constants_whitelist; |
||
| 28 | private $meta_types = array( 'post', 'comment' ); |
||
| 29 | private $callable_whitelist; |
||
| 30 | private $network_options_whitelist; |
||
| 31 | private $taxonomy_whitelist; |
||
| 32 | private $is_multisite; |
||
| 33 | |||
| 34 | // singleton functions |
||
| 35 | private static $instance; |
||
| 36 | |||
| 37 | public static function getInstance() { |
||
| 44 | |||
| 45 | // this is necessary because you can't use "new" when you declare instance properties >:( |
||
| 46 | protected function __construct() { |
||
| 50 | |||
| 51 | private function init() { |
||
| 52 | |||
| 53 | $handler = array( $this, 'action_handler' ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Most of the following hooks are sent to the same $handler |
||
| 57 | * for immediate serialization and queuing be sent to the server. |
||
| 58 | * The only exceptions are actions which need additional processing. |
||
| 59 | */ |
||
| 60 | |||
| 61 | // constants |
||
| 62 | add_action( 'jetpack_sync_constant', $handler, 10, 2 ); |
||
| 63 | |||
| 64 | // callables |
||
| 65 | add_action( 'jetpack_sync_callable', $handler, 10, 2 ); |
||
| 66 | |||
| 67 | // posts |
||
| 68 | add_action( 'wp_insert_post', $handler, 10, 3 ); |
||
| 69 | add_action( 'deleted_post', $handler, 10 ); |
||
| 70 | add_filter( 'jetpack_sync_before_send_wp_insert_post', array( $this, 'expand_wp_insert_post' ) ); |
||
| 71 | |||
| 72 | add_action( 'jetpack_publicize_post', $handler ); |
||
| 73 | |||
| 74 | // attachments |
||
| 75 | |||
| 76 | add_action( 'edit_attachment', array( $this, 'send_attachment_info' ) ); |
||
| 77 | // Once we don't have to support 4.3 we can start using add_action( 'attachment_updated', $handler, 10, 3 ); instead |
||
| 78 | add_action( 'add_attachment', array( $this, 'send_attachment_info' ) ); |
||
| 79 | add_action( 'jetpack_sync_save_add_attachment', $handler, 10, 2 ); |
||
| 80 | |||
| 81 | // comments |
||
| 82 | add_action( 'wp_insert_comment', $handler, 10, 2 ); |
||
| 83 | add_action( 'deleted_comment', $handler, 10 ); |
||
| 84 | add_action( 'trashed_comment', $handler, 10 ); |
||
| 85 | add_action( 'spammed_comment', $handler, 10 ); |
||
| 86 | |||
| 87 | add_filter( 'jetpack_sync_before_send_wp_insert_comment', array( $this, 'expand_wp_insert_comment' ) ); |
||
| 88 | |||
| 89 | // even though it's messy, we implement these hooks because |
||
| 90 | // the edit_comment hook doesn't include the data |
||
| 91 | // so this saves us a DB read for every comment event |
||
| 92 | foreach ( array( '', 'trackback', 'pingback' ) as $comment_type ) { |
||
| 93 | foreach ( array( 'unapproved', 'approved' ) as $comment_status ) { |
||
| 94 | $comment_action_name = "comment_{$comment_status}_{$comment_type}"; |
||
| 95 | add_action( $comment_action_name, $handler, 10, 2 ); |
||
| 96 | add_filter( 'jetpack_sync_before_send_' . $comment_action_name, array( $this, 'expand_wp_insert_comment' ) ); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // options |
||
| 101 | add_action( 'added_option', $handler, 10, 2 ); |
||
| 102 | add_action( 'updated_option', $handler, 10, 3 ); |
||
| 103 | add_action( 'deleted_option', $handler, 10, 1 ); |
||
| 104 | |||
| 105 | // Sync Core Icon: Detect changes in Core's Site Icon and make it syncable. |
||
| 106 | add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 107 | add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 108 | add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) ); |
||
| 109 | |||
| 110 | // wordpress version |
||
| 111 | add_action( 'upgrader_process_complete', array( $this, 'send_wp_version' ), 10, 2 ); |
||
| 112 | add_action( 'jetpack_sync_wp_version', $handler ); |
||
| 113 | |||
| 114 | // themes |
||
| 115 | add_action( 'switch_theme', array( $this, 'send_theme_info' ) ); |
||
| 116 | add_action( 'jetpack_sync_current_theme_support', $handler, 10 ); // custom hook, see meta-hooks below |
||
| 117 | |||
| 118 | // post-meta, and in the future - other meta? |
||
| 119 | foreach ( $this->meta_types as $meta_type ) { |
||
| 120 | add_action( "added_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 121 | add_action( "updated_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 122 | add_action( "deleted_{$meta_type}_meta", $handler, 10, 4 ); |
||
| 123 | } |
||
| 124 | |||
| 125 | // terms |
||
| 126 | add_action( 'created_term', array( $this, 'save_term_handler' ), 10, 3 ); |
||
| 127 | add_action( 'edited_term', array( $this, 'save_term_handler' ), 10, 3 ); |
||
| 128 | add_action( 'jetpack_sync_save_term', $handler, 10, 4 ); |
||
| 129 | add_action( 'delete_term', $handler, 10, 4 ); |
||
| 130 | add_action( 'set_object_terms', $handler, 10, 6 ); |
||
| 131 | add_action( 'deleted_term_relationships', $handler, 10, 2 ); |
||
| 132 | |||
| 133 | // users |
||
| 134 | add_action( 'user_register', array( $this, 'save_user_handler' ) ); |
||
| 135 | add_action( 'profile_update', array( $this, 'save_user_handler' ), 10, 2 ); |
||
| 136 | add_action( 'jetpack_sync_save_user', $handler, 10, 2 ); |
||
| 137 | add_action( 'deleted_user', $handler, 10, 2 ); |
||
| 138 | |||
| 139 | // user roles |
||
| 140 | add_action( 'add_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 141 | add_action( 'set_user_role', array( $this, 'save_user_role_handler' ), 10, 3 ); |
||
| 142 | add_action( 'remove_user_role', array( $this, 'save_user_role_handler' ), 10, 2 ); |
||
| 143 | |||
| 144 | |||
| 145 | // user capabilities |
||
| 146 | add_action( 'added_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 147 | add_action( 'updated_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 148 | add_action( 'deleted_user_meta', array( $this, 'save_user_cap_handler' ), 10, 4 ); |
||
| 149 | |||
| 150 | // themes |
||
| 151 | add_action( 'set_site_transient_update_plugins', $handler, 10, 1 ); |
||
| 152 | add_action( 'set_site_transient_update_themes', $handler, 10, 1 ); |
||
| 153 | add_action( 'set_site_transient_update_core', $handler, 10, 1 ); |
||
| 154 | |||
| 155 | add_filter( 'jetpack_sync_before_enqueue_set_site_transient_update_plugins', array( $this, 'filter_update_keys' ), 10, 2 ); |
||
| 156 | |||
| 157 | // multi site network options |
||
| 158 | if ( $this->is_multisite ) { |
||
| 159 | add_action( 'add_site_option', $handler, 10, 2 ); |
||
| 160 | add_action( 'update_site_option', $handler, 10, 3 ); |
||
| 161 | add_action( 'delete_site_option', $handler, 10, 1 ); |
||
| 162 | } |
||
| 163 | |||
| 164 | // synthetic actions for full sync |
||
| 165 | add_action( 'jetpack_full_sync_start', $handler ); |
||
| 166 | add_action( 'jetpack_full_sync_end', $handler ); |
||
| 167 | add_action( 'jetpack_full_sync_options', $handler ); |
||
| 168 | add_action( 'jetpack_full_sync_posts', $handler ); // also sends post meta |
||
| 169 | add_action( 'jetpack_full_sync_comments', $handler ); // also send comments meta |
||
| 170 | add_action( 'jetpack_full_sync_constants', $handler ); |
||
| 171 | add_action( 'jetpack_full_sync_callables', $handler ); |
||
| 172 | add_action( 'jetpack_full_sync_updates', $handler ); |
||
| 173 | |||
| 174 | add_action( 'jetpack_full_sync_users', $handler ); |
||
| 175 | add_action( 'jetpack_full_sync_terms', $handler, 10, 2 ); |
||
| 176 | if ( is_multisite() ) { |
||
| 177 | add_action( 'jetpack_full_sync_network_options', $handler ); |
||
| 178 | } |
||
| 179 | |||
| 180 | // Module Activation |
||
| 181 | add_action( 'jetpack_activate_module', $handler ); |
||
| 182 | add_action( 'jetpack_deactivate_module', $handler ); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Sync all pending actions with server |
||
| 186 | */ |
||
| 187 | add_action( 'jetpack_sync_actions', array( $this, 'do_sync' ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | // removes unnecessary keys from synced updates data |
||
| 191 | function filter_update_keys( $args ) { |
||
| 192 | $updates = $args[0]; |
||
| 193 | |||
| 194 | if ( isset( $updates->no_update ) ) { |
||
| 195 | unset( $updates->no_update ); |
||
| 196 | } |
||
| 197 | |||
| 198 | return $args; |
||
| 199 | } |
||
| 200 | |||
| 201 | function set_options_whitelist( $options ) { |
||
| 202 | $this->options_whitelist = $options; |
||
| 203 | } |
||
| 204 | |||
| 205 | function get_options_whitelist() { |
||
| 206 | return $this->options_whitelist; |
||
| 207 | } |
||
| 208 | |||
| 209 | function set_constants_whitelist( $constants ) { |
||
| 210 | $this->constants_whitelist = $constants; |
||
| 211 | } |
||
| 212 | |||
| 213 | function get_constants_whitelist() { |
||
| 214 | return $this->constants_whitelist; |
||
| 215 | } |
||
| 216 | |||
| 217 | function set_callable_whitelist( $callables ) { |
||
| 218 | $this->callable_whitelist = $callables; |
||
| 219 | } |
||
| 220 | |||
| 221 | function get_callable_whitelist() { |
||
| 222 | return $this->callable_whitelist; |
||
| 223 | } |
||
| 224 | |||
| 225 | function set_network_options_whitelist( $options ) { |
||
| 226 | $this->network_options_whitelist = $options; |
||
| 227 | } |
||
| 228 | |||
| 229 | function get_network_options_whitelist() { |
||
| 230 | return $this->network_options_whitelist; |
||
| 231 | } |
||
| 232 | |||
| 233 | function set_dequeue_max_bytes( $size ) { |
||
| 234 | $this->dequeue_max_bytes = $size; |
||
| 235 | } |
||
| 236 | |||
| 237 | // in bytes |
||
| 238 | function set_upload_max_bytes( $max_bytes ) { |
||
| 239 | $this->upload_max_bytes = $max_bytes; |
||
| 240 | } |
||
| 241 | |||
| 242 | // in rows |
||
| 243 | function set_upload_max_rows( $max_rows ) { |
||
| 244 | $this->upload_max_rows = $max_rows; |
||
| 245 | } |
||
| 246 | |||
| 247 | // in seconds |
||
| 248 | function set_sync_wait_time( $seconds ) { |
||
| 249 | update_option( self::SYNC_THROTTLE_OPTION_NAME, $seconds, true ); |
||
| 250 | } |
||
| 251 | |||
| 252 | function get_sync_wait_time() { |
||
| 253 | return get_option( self::SYNC_THROTTLE_OPTION_NAME ); |
||
| 254 | } |
||
| 255 | |||
| 256 | private function get_last_sync_time() { |
||
| 257 | return (double) get_option( self::LAST_SYNC_TIME_OPTION_NAME ); |
||
| 258 | } |
||
| 259 | |||
| 260 | private function set_last_sync_time() { |
||
| 261 | return update_option( self::LAST_SYNC_TIME_OPTION_NAME, microtime( true ), true ); |
||
| 262 | } |
||
| 263 | |||
| 264 | function set_taxonomy_whitelist( $taxonomies ) { |
||
| 265 | $this->taxonomy_whitelist = $taxonomies; |
||
| 266 | } |
||
| 267 | |||
| 268 | function is_whitelisted_option( $option ) { |
||
| 269 | return in_array( $option, $this->options_whitelist ); |
||
| 270 | } |
||
| 271 | |||
| 272 | function is_whitelisted_network_option( $option ) { |
||
| 273 | return $this->is_multisite && in_array( $option, $this->network_options_whitelist ); |
||
| 274 | } |
||
| 275 | |||
| 276 | function set_codec( iJetpack_Sync_Codec $codec ) { |
||
| 277 | $this->codec = $codec; |
||
| 278 | } |
||
| 279 | |||
| 280 | function set_full_sync_client( $full_sync_client ) { |
||
| 281 | if ( $this->full_sync_client ) { |
||
| 282 | remove_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
||
| 283 | } |
||
| 284 | |||
| 285 | $this->full_sync_client = $full_sync_client; |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Sync all objects in the database with the server |
||
| 289 | */ |
||
| 290 | add_action( 'jetpack_sync_full', array( $this->full_sync_client, 'start' ) ); |
||
| 291 | } |
||
| 292 | |||
| 293 | function get_full_sync_client() { |
||
| 294 | return $this->full_sync_client; |
||
| 295 | } |
||
| 296 | |||
| 297 | function action_handler() { |
||
| 298 | |||
| 299 | $current_filter = current_filter(); |
||
| 300 | $args = func_get_args(); |
||
| 301 | |||
| 302 | if ( in_array( $current_filter, array( 'deleted_option', 'added_option', 'updated_option' ) ) |
||
| 303 | && |
||
| 304 | ! $this->is_whitelisted_option( $args[0] ) |
||
| 305 | ) { |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ( in_array( $current_filter, array( 'delete_site_option', 'add_site_option', 'update_site_option' ) ) |
||
| 310 | && |
||
| 311 | ! $this->is_whitelisted_network_option( $args[0] ) |
||
| 312 | ) { |
||
| 313 | return; |
||
| 314 | } |
||
| 315 | |||
| 316 | // don't sync private meta |
||
| 317 | if ( preg_match( '/^(added|updated|deleted)_.*_meta$/', $current_filter ) |
||
| 318 | && $args[2][0] === '_' |
||
| 319 | && ! in_array( $args[2], Jetpack_Sync_Defaults::$default_whitelist_meta_keys ) |
||
|
|
|||
| 320 | ) { |
||
| 321 | return; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Modify the data within an action before it is enqueued locally. |
||
| 326 | * |
||
| 327 | * @since 4.2.0 |
||
| 328 | * |
||
| 329 | * @param array The action parameters |
||
| 330 | */ |
||
| 331 | $args = apply_filters( "jetpack_sync_before_enqueue_$current_filter", $args ); |
||
| 332 | |||
| 333 | // if we add any items to the queue, we should |
||
| 334 | // try to ensure that our script can't be killed before |
||
| 335 | // they are sent |
||
| 336 | if ( function_exists( 'ignore_user_abort' ) ) { |
||
| 337 | ignore_user_abort( true ); |
||
| 338 | } |
||
| 339 | |||
| 340 | $this->sync_queue->add( array( |
||
| 341 | $current_filter, |
||
| 342 | $args, |
||
| 343 | get_current_user_id(), |
||
| 344 | microtime( true ) |
||
| 345 | ) ); |
||
| 346 | } |
||
| 347 | |||
| 348 | function send_theme_info() { |
||
| 349 | global $_wp_theme_features; |
||
| 350 | |||
| 351 | $theme_support = array(); |
||
| 352 | |||
| 353 | foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) { |
||
| 354 | $has_support = current_theme_supports( $theme_feature ); |
||
| 355 | if ( $has_support ) { |
||
| 356 | $theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; |
||
| 357 | } |
||
| 358 | |||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Fires when the client needs to sync theme support info |
||
| 363 | * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist |
||
| 364 | * |
||
| 365 | * @since 4.2.0 |
||
| 366 | * |
||
| 367 | * @param object the theme support hash |
||
| 368 | */ |
||
| 369 | do_action( 'jetpack_sync_current_theme_support', $theme_support ); |
||
| 370 | return 1; // The number of actions enqueued |
||
| 371 | } |
||
| 372 | |||
| 373 | function send_wp_version( $update, $meta_data ) { |
||
| 374 | if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) { |
||
| 375 | $this->force_sync_callables(); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | function save_term_handler( $term_id, $tt_id, $taxonomy ) { |
||
| 395 | |||
| 396 | function send_attachment_info( $attachment_id ) { |
||
| 409 | |||
| 410 | function save_user_handler( $user_id, $old_user_data = null ) { |
||
| 436 | |||
| 437 | function save_user_role_handler( $user_id, $role, $old_roles = null ) { |
||
| 449 | |||
| 450 | function save_user_cap_handler( $meta_id, $user_id, $meta_key, $capabilities ) { |
||
| 463 | |||
| 464 | public function sanitize_user( $user ) { |
||
| 469 | |||
| 470 | |||
| 471 | function do_sync() { |
||
| 472 | // don't sync if importing |
||
| 473 | if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
||
| 474 | $this->schedule_sync( "+1 minute" ); |
||
| 475 | |||
| 476 | return false; |
||
| 477 | } |
||
| 478 | |||
| 479 | // don't sync if we are throttled |
||
| 480 | $sync_wait = $this->get_sync_wait_time(); |
||
| 481 | $last_sync = $this->get_last_sync_time(); |
||
| 482 | |||
| 483 | if ( $last_sync && $sync_wait && $last_sync + $sync_wait > microtime( true ) ) { |
||
| 484 | return false; |
||
| 485 | } |
||
| 486 | |||
| 487 | $this->set_last_sync_time(); |
||
| 488 | $this->maybe_sync_constants(); |
||
| 489 | $this->maybe_sync_callables(); |
||
| 490 | |||
| 491 | if ( $this->sync_queue->size() === 0 ) { |
||
| 492 | return false; |
||
| 493 | } |
||
| 494 | |||
| 495 | // now that we're sure we are about to sync, try to |
||
| 590 | |||
| 591 | function expand_wp_insert_post( $args ) { |
||
| 594 | |||
| 595 | // Expands wp_insert_post to include filtered content |
||
| 596 | function filter_post_content_and_add_links( $post ) { |
||
| 633 | |||
| 634 | |||
| 635 | function expand_wp_comment_status_change( $args ) { |
||
| 638 | |||
| 639 | function expand_wp_insert_comment( $args ) { |
||
| 642 | |||
| 643 | function filter_comment_and_add_hc_meta( $comment ) { |
||
| 679 | |||
| 680 | private function schedule_sync( $when ) { |
||
| 683 | |||
| 684 | function force_sync_constants() { |
||
| 690 | |||
| 691 | function full_sync_constants() { |
||
| 702 | |||
| 703 | function force_sync_options() { |
||
| 714 | |||
| 715 | function force_sync_network_options() { |
||
| 726 | |||
| 727 | public function full_sync_callables() { |
||
| 738 | |||
| 739 | public function full_sync_updates() { |
||
| 750 | |||
| 751 | View Code Duplication | private function maybe_sync_constants() { |
|
| 784 | // public so that we don't have to store an option for each constant |
||
| 785 | function get_all_constants() { |
||
| 791 | |||
| 792 | private function get_constant( $constant ) { |
||
| 797 | |||
| 798 | public function get_all_updates() { |
||
| 805 | |||
| 806 | public function force_sync_callables() { |
||
| 811 | |||
| 812 | View Code Duplication | private function maybe_sync_callables() { |
|
| 845 | |||
| 846 | private function still_valid_checksum( $sums_to_check, $name, $new_sum ) { |
||
| 852 | |||
| 853 | public function get_all_callables() { |
||
| 859 | |||
| 860 | private function get_callable( $callable ) { |
||
| 863 | |||
| 864 | // Is public so that we don't have to store so much data all the options twice. |
||
| 865 | function get_all_options() { |
||
| 873 | |||
| 874 | function get_all_network_options() { |
||
| 882 | |||
| 883 | private function get_check_sum( $values ) { |
||
| 886 | |||
| 887 | function jetpack_sync_core_icon() { |
||
| 903 | |||
| 904 | function get_sync_queue() { |
||
| 907 | |||
| 908 | function reset_sync_queue() { |
||
| 911 | |||
| 912 | function get_settings() { |
||
| 920 | |||
| 921 | function update_settings( $new_settings ) { |
||
| 927 | |||
| 928 | function update_options_whitelist() { |
||
| 932 | |||
| 933 | function set_defaults() { |
||
| 959 | |||
| 960 | function reset_data() { |
||
| 979 | |||
| 980 | function uninstall() { |
||
| 990 | } |
||
| 991 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.