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() { |
||
| 48 | require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); // For the is_plugin... check |
||
| 49 | require_once( JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php' ); // For managing the global whitelist |
||
| 50 | /* |
||
| 51 | * Sanity check to ensure the install is Multisite and we |
||
| 52 | * are in Network Admin |
||
| 53 | */ |
||
| 54 | if ( is_multisite() && is_network_admin() ) { |
||
| 55 | add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ) ); |
||
| 56 | add_action( 'network_admin_edit_jetpack-network-settings', array( $this, 'save_network_settings_page' ), 10, 0 ); |
||
| 57 | add_filter( 'admin_body_class', array( $this, 'body_class' ) ); |
||
| 58 | |||
| 59 | if ( isset( $_GET['page'] ) && 'jetpack' == $_GET['page'] ) { |
||
| 60 | add_action( 'admin_init', array( $this, 'jetpack_sites_list' ) ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /* |
||
| 65 | * Things that should only run on multisite |
||
| 66 | */ |
||
| 67 | if ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 68 | add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) ); |
||
| 69 | |||
| 70 | /* |
||
| 71 | * If admin wants to automagically register new sites set the hook here |
||
| 72 | * |
||
| 73 | * This is a hacky way because xmlrpc is not available on wpmu_new_blog |
||
| 74 | */ |
||
| 75 | if ( $this->get_option( 'auto-connect' ) == 1 ) { |
||
| 76 | add_action( 'wpmu_new_blog', array( $this, 'do_automatically_add_new_site' ) ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | // Remove the toggles for 2.9, re-evaluate how they're done and added for a 3.0 release. They don't feel quite right yet. |
||
| 81 | // add_filter( 'jetpack_get_default_modules', array( $this, 'set_auto_activated_modules' ) ); |
||
| 82 | } |
||
| 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 ) { |
||
| 95 | return $modules; |
||
| 96 | |||
| 97 | /* Remove the toggles for 2.9, re-evaluate how they're done and added for a 3.0 release. They don't feel quite right yet. |
||
| 98 | if( 1 == $this->get_option( 'manage_auto_activated_modules' ) ) { |
||
| 99 | return (array) $this->get_option( 'modules' ); |
||
| 100 | } else { |
||
| 101 | return $modules; |
||
| 102 | } |
||
| 103 | */ |
||
| 104 | } |
||
| 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() { |
||
| 137 | if ( ! self::$instance || ! is_a( self::$instance, 'Jetpack_Network' ) ) { |
||
| 138 | self::$instance = new Jetpack_Network; |
||
| 139 | } |
||
| 140 | |||
| 141 | return self::$instance; |
||
| 142 | } |
||
| 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() { |
||
| 165 | // Only fire if in network admin |
||
| 166 | if ( ! is_network_admin() ) { |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | $sites = $this->wp_get_sites(); |
||
|
|
|||
| 171 | |||
| 172 | foreach ( $sites as $s ) { |
||
| 173 | switch_to_blog( $s->blog_id ); |
||
| 174 | $active_plugins = get_option( 'active_plugins' ); |
||
| 175 | |||
| 176 | /* |
||
| 177 | * If this plugin was activated in the subsite individually |
||
| 178 | * we do not want to call disconnect. Plugins activated |
||
| 179 | * individually (before network activation) stay activated |
||
| 180 | * when the network deactivation occurs |
||
| 181 | */ |
||
| 182 | if ( ! in_array( 'jetpack/jetpack.php', $active_plugins ) ) { |
||
| 183 | Jetpack::disconnect(); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | restore_current_blog(); |
||
| 187 | } |
||
| 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() { |
||
| 195 | global $wp_admin_bar; |
||
| 196 | // Don't show for logged out users or single site mode. |
||
| 197 | if ( ! is_user_logged_in() || ! is_multisite() ) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | $wp_admin_bar->add_node( array( |
||
| 202 | 'parent' => 'network-admin', |
||
| 203 | 'id' => 'network-admin-jetpack', |
||
| 204 | 'title' => __( 'Jetpack', 'jetpack' ), |
||
| 205 | 'href' => $this->get_url( 'network_admin_page' ), |
||
| 206 | ) ); |
||
| 207 | } |
||
| 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 ) { |
||
| 227 | $url = null; // Default url value |
||
| 228 | |||
| 229 | if ( is_string( $args ) ) { |
||
| 230 | $name = $args; |
||
| 231 | } else { |
||
| 232 | $name = $args['name']; |
||
| 233 | } |
||
| 234 | |||
| 235 | switch ( $name ) { |
||
| 236 | case 'subsiteregister': |
||
| 237 | if ( ! isset( $args['site_id'] ) ) { |
||
| 238 | break; // If there is not a site id present we cannot go further |
||
| 239 | } |
||
| 240 | $url = network_admin_url( |
||
| 241 | 'admin.php?page=jetpack&action=subsiteregister&site_id=' |
||
| 242 | . $args['site_id'] |
||
| 243 | ); |
||
| 244 | break; |
||
| 245 | |||
| 246 | case 'network_admin_page': |
||
| 247 | $url = network_admin_url( 'admin.php?page=jetpack' ); |
||
| 248 | break; |
||
| 249 | |||
| 250 | case 'subsitedisconnect': |
||
| 251 | if ( ! isset( $args['site_id'] ) ) { |
||
| 252 | break; // If there is not a site id present we cannot go further |
||
| 253 | } |
||
| 254 | $url = network_admin_url( |
||
| 255 | 'admin.php?page=jetpack&action=subsitedisconnect&site_id=' |
||
| 256 | . $args['site_id'] |
||
| 257 | ); |
||
| 258 | break; |
||
| 259 | } |
||
| 260 | |||
| 261 | return $url; |
||
| 262 | } |
||
| 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() { |
||
| 270 | add_menu_page( __( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'jetpack_network_admin_page', 'jetpack', array( $this, 'network_admin_page' ), 'div', 3 ); |
||
| 271 | add_submenu_page( 'jetpack', __( 'Jetpack Sites', 'jetpack' ), __( 'Sites', 'jetpack' ), 'jetpack_network_sites_page', 'jetpack', array( $this, 'network_admin_page' ) ); |
||
| 272 | add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_network_settings_page', 'jetpack-settings', array( $this, 'render_network_admin_settings_page' ) ); |
||
| 273 | |||
| 274 | /** |
||
| 275 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 276 | * the hook may have already fired by this point. |
||
| 277 | * So, let's just trigger it manually. |
||
| 278 | */ |
||
| 279 | require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' ); |
||
| 280 | jetpack_register_genericons(); |
||
| 281 | |||
| 282 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) { |
|
| 283 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 284 | } |
||
| 285 | |||
| 286 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
| 287 | } |
||
| 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 ) { |
||
| 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.