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_Network 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_Network, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Jetpack_Network { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Holds a static copy of Jetpack_Network for the singleton |
||
| 16 | * |
||
| 17 | * @since 2.9 |
||
| 18 | * @var Jetpack_Network |
||
| 19 | */ |
||
| 20 | private static $instance = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Name of the network wide settings |
||
| 24 | * |
||
| 25 | * @since 2.9 |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $settings_name = 'jetpack-network-settings'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Defaults for settings found on the Jetpack > Settings page |
||
| 32 | * |
||
| 33 | * @since 2.9 |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $setting_defaults = array( |
||
| 37 | 'auto-connect' => 0, |
||
| 38 | 'sub-site-connection-override' => 1, |
||
| 39 | //'manage_auto_activated_modules' => 0, |
||
| 40 | ); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * |
||
| 45 | * @since 2.9 |
||
| 46 | */ |
||
| 47 | private function __construct() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets which modules get activated by default on subsite connection. |
||
| 86 | * Modules can be set in Network Admin > Jetpack > Settings |
||
| 87 | * |
||
| 88 | * @since 2.9 |
||
| 89 | * |
||
| 90 | * @param array $modules |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | **/ |
||
| 94 | public function set_auto_activated_modules( $modules ) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Registers new sites upon creation |
||
| 108 | * |
||
| 109 | * @since 2.9 |
||
| 110 | * @uses wpmu_new_blog |
||
| 111 | * |
||
| 112 | * @param int $blog_id |
||
| 113 | **/ |
||
| 114 | public function do_automatically_add_new_site( $blog_id ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Adds .network-admin class to the body tag |
||
| 120 | * Helps distinguish network admin JP styles from regular site JP styles |
||
| 121 | * |
||
| 122 | * @since 2.9 |
||
| 123 | */ |
||
| 124 | public function body_class( $classes ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Provides access to an instance of Jetpack_Network |
||
| 130 | * |
||
| 131 | * This is how the Jetpack_Network object should *always* be accessed |
||
| 132 | * |
||
| 133 | * @since 2.9 |
||
| 134 | * @return Jetpack_Network |
||
| 135 | */ |
||
| 136 | public static function init() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Registers the Multisite admin bar menu item shortcut. |
||
| 146 | * This shortcut helps users quickly and easily navigate to the Jetpack Network Admin |
||
| 147 | * menu from anywhere in their network. |
||
| 148 | * |
||
| 149 | * @since 2.9 |
||
| 150 | */ |
||
| 151 | public function register_menubar() { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Runs when Jetpack is deactivated from the network admin plugins menu. |
||
| 157 | * Each individual site will need to have Jetpack::disconnect called on it. |
||
| 158 | * Site that had Jetpack individually enabled will not be disconnected as |
||
| 159 | * on Multisite individually activated plugins are still activated when |
||
| 160 | * a plugin is deactivated network wide. |
||
| 161 | * |
||
| 162 | * @since 2.9 |
||
| 163 | **/ |
||
| 164 | public function deactivate() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Adds a link to the Jetpack Network Admin page in the network admin menu bar. |
||
| 191 | * |
||
| 192 | * @since 2.9 |
||
| 193 | **/ |
||
| 194 | public function add_to_menubar() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns various URL strings. Factory like |
||
| 211 | * |
||
| 212 | * $args can be a string or an array. |
||
| 213 | * If $args is an array there must be an element called name for the switch statement |
||
| 214 | * |
||
| 215 | * Currently supports: |
||
| 216 | * - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID ) |
||
| 217 | * - network_admin_page: Provides link to /wp-admin/network/JETPACK |
||
| 218 | * - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID ) |
||
| 219 | * |
||
| 220 | * @since 2.9 |
||
| 221 | * |
||
| 222 | * @param Mixed $args |
||
| 223 | * |
||
| 224 | * @return String |
||
| 225 | **/ |
||
| 226 | public function get_url( $args ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Adds the Jetpack menu item to the Network Admin area |
||
| 266 | * |
||
| 267 | * @since 2.9 |
||
| 268 | */ |
||
| 269 | public function add_network_admin_menu() { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Adds JP menu icon |
||
| 291 | * |
||
| 292 | * @since 2.9 |
||
| 293 | **/ |
||
| 294 | function admin_menu_css() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Provides functionality for the Jetpack > Sites page. |
||
| 300 | * Does not do the display! |
||
| 301 | * |
||
| 302 | * @since 2.9 |
||
| 303 | */ |
||
| 304 | public function jetpack_sites_list() { |
||
| 356 | |||
| 357 | public function show_jetpack_notice() { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Disconnect functionality for an individual site |
||
| 369 | * |
||
| 370 | * @since 2.9 |
||
| 371 | * @see Jetpack_Network::jetpack_sites_list() |
||
| 372 | */ |
||
| 373 | public function do_subsitedisconnect( $site_id = null ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Registers a subsite with the Jetpack servers |
||
| 385 | * |
||
| 386 | * @since 2.9 |
||
| 387 | * @todo Break apart into easier to manage chunks that can be unit tested |
||
| 388 | * @see Jetpack_Network::jetpack_sites_list(); |
||
| 389 | */ |
||
| 390 | public function do_subsiteregister( $site_id = null ) { |
||
| 391 | if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
||
| 392 | return; |
||
| 393 | } |
||
| 394 | |||
| 395 | if ( Jetpack::is_development_mode() ) { |
||
| 396 | return; |
||
| 397 | } |
||
| 398 | |||
| 399 | $jp = Jetpack::init(); |
||
| 400 | |||
| 401 | // Figure out what site we are working on |
||
| 402 | $site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id; |
||
| 403 | |||
| 404 | // Remote query timeout limit |
||
| 405 | $timeout = $jp->get_remote_query_timeout_limit(); |
||
| 406 | |||
| 407 | // The blog id on WordPress.com of the primary network site |
||
| 408 | $network_wpcom_blog_id = Jetpack_Options::get_option( 'id' ); |
||
| 409 | |||
| 410 | /* |
||
| 411 | * Here we need to switch to the subsite |
||
| 412 | * For the registration process we really only hijack how it |
||
| 413 | * works for an individual site and pass in some extra data here |
||
| 414 | */ |
||
| 415 | switch_to_blog( $site_id ); |
||
| 416 | |||
| 417 | // Save the secrets in the subsite so when the wpcom server does a pingback it |
||
| 418 | // will be able to validate the connection |
||
| 419 | $secrets = $jp->generate_secrets( 'register' ); |
||
| 420 | @list( $secret_1, $secret_2, $secret_eol ) = explode( ':', $secrets ); |
||
| 421 | View Code Duplication | if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) { |
|
| 422 | return new Jetpack_Error( 'missing_secrets' ); |
||
| 423 | } |
||
| 424 | |||
| 425 | // Gra info for gmt offset |
||
| 426 | $gmt_offset = get_option( 'gmt_offset' ); |
||
| 427 | if ( ! $gmt_offset ) { |
||
| 428 | $gmt_offset = 0; |
||
| 429 | } |
||
| 430 | |||
| 431 | /* |
||
| 432 | * Get the stats_option option from the db. |
||
| 433 | * It looks like the server strips this out so maybe it is not necessary? |
||
| 434 | * Does it match the Jetpack site with the old stats plugin id? |
||
| 435 | * |
||
| 436 | * @todo Find out if sending the stats_id is necessary |
||
| 437 | */ |
||
| 438 | $stat_options = get_option( 'stats_options' ); |
||
| 439 | $stat_id = $stat_options = isset( $stats_options['blog_id'] ) ? $stats_options['blog_id'] : null; |
||
| 440 | $user_id = get_current_user_id(); |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Both `state` and `user_id` need to be sent in the request, even though they are the same value. |
||
| 444 | * Connecting via the network admin combines `register()` and `authorize()` methods into one step, |
||
| 445 | * because we assume the main site is already authorized. `state` is used to verify the `register()` |
||
| 446 | * request, while `user_id()` is used to create the token in the `authorize()` request. |
||
| 447 | */ |
||
| 448 | $args = array( |
||
| 449 | 'method' => 'POST', |
||
| 450 | 'body' => array( |
||
| 451 | 'network_url' => $this->get_url( 'network_admin_page' ), |
||
| 452 | 'network_wpcom_blog_id' => $network_wpcom_blog_id, |
||
| 453 | 'siteurl' => site_url(), |
||
| 454 | 'home' => home_url(), |
||
| 455 | 'gmt_offset' => $gmt_offset, |
||
| 456 | 'timezone_string' => (string) get_option( 'timezone_string' ), |
||
| 457 | 'site_name' => (string) get_option( 'blogname' ), |
||
| 458 | 'secret_1' => $secret_1, |
||
| 459 | 'secret_2' => $secret_2, |
||
| 460 | 'site_lang' => get_locale(), |
||
| 461 | 'timeout' => $timeout, |
||
| 462 | 'stats_id' => $stat_id, // Is this still required? |
||
| 463 | 'user_id' => $user_id, |
||
| 464 | 'state' => $user_id |
||
| 465 | ), |
||
| 466 | 'headers' => array( |
||
| 467 | 'Accept' => 'application/json', |
||
| 468 | ), |
||
| 469 | 'timeout' => $timeout, |
||
| 470 | ); |
||
| 471 | |||
| 472 | // Attempt to retrieve shadow blog details |
||
| 473 | $response = Jetpack_Client::_wp_remote_request( |
||
| 474 | Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'subsiteregister' ) ), $args, true |
||
| 475 | ); |
||
| 476 | |||
| 477 | /* |
||
| 478 | * $response should either be invalid or contain: |
||
| 479 | * - jetpack_id => id |
||
| 480 | * - jetpack_secret => blog_token |
||
| 481 | * - jetpack_public |
||
| 482 | * |
||
| 483 | * Store the wpcom site details |
||
| 484 | */ |
||
| 485 | $valid_response = $jp->validate_remote_register_response( $response ); |
||
| 486 | |||
| 487 | if ( is_wp_error( $valid_response ) || ! $valid_response ) { |
||
| 488 | restore_current_blog(); |
||
| 489 | return $valid_response; |
||
| 490 | } |
||
| 491 | |||
| 492 | // Grab the response values to work with |
||
| 493 | $code = wp_remote_retrieve_response_code( $response ); |
||
| 494 | $entity = wp_remote_retrieve_body( $response ); |
||
| 495 | if ( $entity ) { |
||
| 496 | $json = json_decode( $entity ); |
||
| 497 | } else { |
||
| 498 | $json = false; |
||
| 499 | } |
||
| 500 | |||
| 501 | View Code Duplication | if ( empty( $json->jetpack_secret ) || ! is_string( $json->jetpack_secret ) ) { |
|
| 502 | restore_current_blog(); |
||
| 503 | return new Jetpack_Error( 'jetpack_secret', '', $code ); |
||
| 504 | } |
||
| 505 | |||
| 506 | if ( isset( $json->jetpack_public ) ) { |
||
| 507 | $jetpack_public = (int) $json->jetpack_public; |
||
| 508 | } else { |
||
| 509 | $jetpack_public = false; |
||
| 510 | } |
||
| 511 | |||
| 512 | Jetpack_Options::update_options( array( |
||
| 513 | 'id' => (int) $json->jetpack_id, |
||
| 514 | 'blog_token' => (string) $json->jetpack_secret, |
||
| 515 | 'public' => $jetpack_public, |
||
| 516 | ) ); |
||
| 517 | |||
| 518 | /* |
||
| 519 | * Update the subsiteregister method on wpcom so that it also sends back the |
||
| 520 | * token in this same request |
||
| 521 | */ |
||
| 522 | $is_master_user = ! Jetpack::is_active(); |
||
| 523 | Jetpack::update_user_token( |
||
| 524 | get_current_user_id(), |
||
| 525 | sprintf( '%s.%d', $json->token->secret, get_current_user_id() ), |
||
| 526 | $is_master_user |
||
| 527 | ); |
||
| 528 | |||
| 529 | Jetpack::activate_default_modules(); |
||
| 530 | |||
| 531 | restore_current_blog(); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Handles the displaying of all sites on the network that are |
||
| 536 | * dis/connected to Jetpack |
||
| 537 | * |
||
| 538 | * @since 2.9 |
||
| 539 | * @see Jetpack_Network::jetpack_sites_list() |
||
| 540 | */ |
||
| 541 | function network_admin_page() { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Stylized JP header formatting |
||
| 586 | * |
||
| 587 | * @since 2.9 |
||
| 588 | */ |
||
| 589 | function network_admin_page_header() { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Stylized JP footer formatting |
||
| 602 | * |
||
| 603 | * @since 2.9 |
||
| 604 | */ |
||
| 605 | function network_admin_page_footer() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Fires when the Jetpack > Settings page is saved. |
||
| 611 | * |
||
| 612 | * @since 2.9 |
||
| 613 | */ |
||
| 614 | public function save_network_settings_page() { |
||
| 685 | |||
| 686 | public function render_network_admin_settings_page() { |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Updates a site wide option |
||
| 716 | * |
||
| 717 | * @since 2.9 |
||
| 718 | * |
||
| 719 | * @param string $key |
||
| 720 | * @param mixed $value |
||
| 721 | * |
||
| 722 | * @return boolean |
||
| 723 | **/ |
||
| 724 | public function update_option( $key, $value ) { |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Retrieves a site wide option |
||
| 733 | * |
||
| 734 | * @since 2.9 |
||
| 735 | * |
||
| 736 | * @param string $name - Name of the option in the database |
||
| 737 | **/ |
||
| 738 | public function get_option( $name ) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Return an array of sites on the specified network. If no network is specified, |
||
| 751 | * return all sites, regardless of network. |
||
| 752 | * |
||
| 753 | * @todo REMOVE THIS FUNCTION! This function is moving to core. Use that one in favor of this. WordPress::wp_get_sites(). http://codex.wordpress.org/Function_Reference/wp_get_sites NOTE, This returns an array instead of stdClass. Be sure to update class.network-sites-list-table.php |
||
| 754 | * @since 2.9 |
||
| 755 | * @deprecated 2.4.5 |
||
| 756 | * |
||
| 757 | * @param array|string $args Optional. Specify the status of the sites to return. |
||
| 758 | * |
||
| 759 | * @return array An array of site data |
||
| 760 | */ |
||
| 761 | public function wp_get_sites( $args = array() ) { |
||
| 811 | } |
||
| 812 | |||
| 814 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.