Complex classes like WP_To_Diaspora 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 WP_To_Diaspora, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class WP_To_Diaspora { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Only instance of this class. |
||
| 45 | * |
||
| 46 | * @var WP_To_Diaspora |
||
| 47 | */ |
||
| 48 | private static $_instance = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The minimum required WordPress version. |
||
| 52 | * |
||
| 53 | * @since 1.5.4 |
||
| 54 | * |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $_min_wp = '3.9.2'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The minimum required PHP version. |
||
| 61 | * |
||
| 62 | * @since 1.5.4 |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $_min_php = '5.3'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Instance of the API class. |
||
| 70 | * |
||
| 71 | * @var WP2D_API |
||
| 72 | */ |
||
| 73 | private $_api = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Create / Get the instance of this class. |
||
| 77 | * |
||
| 78 | * @return WP_To_Diaspora Instance of this class. |
||
| 79 | */ |
||
| 80 | public static function instance() { |
||
| 81 | if ( ! isset( self::$_instance ) ) { |
||
| 82 | self::$_instance = new self(); |
||
| 83 | self::$_instance->_constants(); |
||
| 84 | if ( self::$_instance->_version_check() ) { |
||
| 85 | self::$_instance->_includes(); |
||
| 86 | self::$_instance->_setup(); |
||
| 87 | } else { |
||
| 88 | self::$_instance = null; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | return self::$_instance; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Define all the required constants. |
||
| 96 | * |
||
| 97 | * @since 1.5.0 |
||
| 98 | */ |
||
| 99 | private function _constants() { |
||
| 100 | // Are we in debugging mode? |
||
| 101 | if ( isset( $_GET['debugging'] ) ) { |
||
| 102 | defined( 'WP2D_DEBUGGING') || define( 'WP2D_DEBUGGING', true ); |
||
| 103 | } |
||
| 104 | |||
| 105 | defined( 'WP2D_DIR' ) || define( 'WP2D_DIR', dirname( __FILE__ ) ); |
||
| 106 | defined( 'WP2D_LIB_DIR' ) || define( 'WP2D_LIB_DIR', WP2D_DIR . '/lib' ); |
||
| 107 | defined( 'WP2D_VENDOR_DIR' ) || define( 'WP2D_VENDOR_DIR', WP2D_DIR . '/vendor' ); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Check the minimum WordPress and PHP requirements. |
||
| 112 | * |
||
| 113 | * @since 1.5.4 |
||
| 114 | * |
||
| 115 | * @return bool If version requirements are met. |
||
| 116 | */ |
||
| 117 | private function _version_check() { |
||
|
|
|||
| 118 | // Check for version requirements. |
||
| 119 | if ( version_compare( $GLOBALS['wp_version'], $this->_min_wp, '<' ) |
||
| 120 | || version_compare( PHP_VERSION, $this->_min_php, '<' ) ) { |
||
| 121 | add_action( 'admin_notices', array( $this, 'deactivate' ) ); |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | |||
| 125 | return true; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Callback to deactivate plugin and display admin notice. |
||
| 130 | * |
||
| 131 | * @since 1.5.4 |
||
| 132 | */ |
||
| 133 | public function deactivate() { |
||
| 134 | // First of all, deactivate the plugin. |
||
| 135 | deactivate_plugins( plugin_basename( __FILE__ ) ); |
||
| 136 | |||
| 137 | // Get rid of the "Plugin activated" message. |
||
| 138 | unset( $_GET['activate'] ); |
||
| 139 | |||
| 140 | // Then display the admin notice. |
||
| 141 | ?> |
||
| 142 | <div class="error"> |
||
| 143 | <p><?php echo esc_html( sprintf( 'WP to diaspora* requires at least WordPress %1$s (you have %2$s) and PHP %3$s (you have %4$s)!', $this->_min_wp, $GLOBALS['wp_version'], $this->_min_php, PHP_VERSION ) ); ?></p> |
||
| 144 | </div> |
||
| 145 | <?php |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Include all the required files. |
||
| 150 | * |
||
| 151 | * @since 1.5.0 |
||
| 152 | */ |
||
| 153 | private function _includes() { |
||
| 154 | require WP2D_VENDOR_DIR . '/autoload.php'; |
||
| 155 | require_once WP2D_LIB_DIR . '/class-api.php'; |
||
| 156 | require_once WP2D_LIB_DIR . '/class-contextual-help.php'; |
||
| 157 | require_once WP2D_LIB_DIR . '/class-helpers.php'; |
||
| 158 | require_once WP2D_LIB_DIR . '/class-options.php'; |
||
| 159 | require_once WP2D_LIB_DIR . '/class-post.php'; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set up the plugin. |
||
| 164 | */ |
||
| 165 | private function _setup() { |
||
| 166 | |||
| 167 | // Load languages. |
||
| 168 | add_action( 'plugins_loaded', array( $this, 'l10n' ) ); |
||
| 169 | |||
| 170 | // Add "Settings" link to plugin page. |
||
| 171 | add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'settings_link' ) ); |
||
| 172 | |||
| 173 | // Perform any necessary data upgrades. |
||
| 174 | add_action( 'admin_init', array( $this, 'upgrade' ) ); |
||
| 175 | |||
| 176 | // Enqueue CSS and JS scripts. |
||
| 177 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_load_scripts' ) ); |
||
| 178 | |||
| 179 | // Set up the options. |
||
| 180 | add_action( 'init', array( 'WP2D_Options', 'instance' ) ); |
||
| 181 | |||
| 182 | // WP2D Post. |
||
| 183 | add_action( 'init', array( 'WP2D_Post', 'setup' ) ); |
||
| 184 | |||
| 185 | // AJAX actions for loading pods, aspects and services. |
||
| 186 | add_action( 'wp_ajax_wp_to_diaspora_update_pod_list', array( $this, 'update_pod_list_callback' ) ); |
||
| 187 | add_action( 'wp_ajax_wp_to_diaspora_update_aspects_list', array( $this, 'update_aspects_list_callback' ) ); |
||
| 188 | add_action( 'wp_ajax_wp_to_diaspora_update_services_list', array( $this, 'update_services_list_callback' ) ); |
||
| 189 | |||
| 190 | // Check the pod connection status on the options page. |
||
| 191 | add_action( 'wp_ajax_wp_to_diaspora_check_pod_connection_status', array( $this, 'check_pod_connection_status_callback' ) ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Load the diaspora* API for ease of use. |
||
| 196 | * |
||
| 197 | * @return WP2D_API|boolean The API object, or false. |
||
| 198 | */ |
||
| 199 | private function _load_api() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Initialise upgrade sequence. |
||
| 208 | */ |
||
| 209 | public function upgrade() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set up i18n. |
||
| 252 | */ |
||
| 253 | public function l10n() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Load scripts and styles for Settings and Post pages of allowed post types. |
||
| 259 | */ |
||
| 260 | public function admin_load_scripts() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Add the "Settings" link to the plugins page. |
||
| 288 | * |
||
| 289 | * @param array $links Links to display for plugin on plugins page. |
||
| 290 | * @return array Links to display for plugin on plugins page. |
||
| 291 | */ |
||
| 292 | public function settings_link( $links ) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Fetch the updated list of pods from podupti.me and save it to the settings. |
||
| 299 | * |
||
| 300 | * @return array The list of pods. |
||
| 301 | */ |
||
| 302 | private function _update_pod_list() { |
||
| 303 | // API url to fetch pods list from podupti.me. |
||
| 304 | $pod_list_url = 'http://podupti.me/api.php?format=json&key=4r45tg'; |
||
| 305 | $pods = array(); |
||
| 306 | |||
| 307 | // Get the response from the WP_HTTP request. |
||
| 308 | $response = wp_safe_remote_get( $pod_list_url ); |
||
| 309 | |||
| 310 | if ( $json = wp_remote_retrieve_body( $response ) ) { |
||
| 311 | $pod_list = json_decode( $json ); |
||
| 312 | |||
| 313 | if ( isset( $pod_list->pods ) ) { |
||
| 314 | foreach ( $pod_list->pods as $pod ) { |
||
| 315 | if ( 'no' === $pod->hidden ) { |
||
| 316 | $pods[] = array( |
||
| 317 | 'secure' => $pod->secure, |
||
| 318 | 'domain' => $pod->domain, |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | $options = WP2D_Options::instance(); |
||
| 324 | $options->set_option( 'pod_list', $pods ); |
||
| 325 | $options->save(); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | return $pods; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Update the list of pods and return them for use with AJAX. |
||
| 334 | */ |
||
| 335 | public function update_pod_list_callback() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Fetch the list of aspects or services and save them to the settings. |
||
| 341 | * |
||
| 342 | * NOTE: When updating the lists, always force a fresh fetch. |
||
| 343 | * |
||
| 344 | * @param string $type Type of list to update. |
||
| 345 | * @return array|boolean The list of aspects or services, false if an illegal parameter is passed. |
||
| 346 | */ |
||
| 347 | private function _update_aspects_services_list( $type ) { |
||
| 348 | // Check for correct argument value. |
||
| 349 | if ( ! in_array( $type, array( 'aspects', 'services' ) ) ) { |
||
| 350 | return false; |
||
| 351 | } |
||
| 352 | |||
| 353 | $options = WP2D_Options::instance(); |
||
| 354 | $list = $options->get_option( $type . '_list' ); |
||
| 355 | |||
| 356 | // Make sure that we have at least the 'Public' aspect. |
||
| 357 | if ( 'aspects' === $type && empty( $list ) ) { |
||
| 358 | $list = array( 'public' => __( 'Public' ) ); |
||
| 359 | } |
||
| 360 | |||
| 361 | // Set up the connection to diaspora*. |
||
| 362 | $api = $this->_load_api(); |
||
| 363 | |||
| 364 | // If there was a problem loading the API, return false. |
||
| 365 | if ( $api->has_last_error() ) { |
||
| 366 | return false; |
||
| 367 | } |
||
| 368 | |||
| 369 | if ( 'aspects' === $type ) { |
||
| 370 | $list_new = $api->get_aspects( true ); |
||
| 371 | } elseif ( 'services' === $type ) { |
||
| 372 | $list_new = $api->get_services( true ); |
||
| 373 | } |
||
| 374 | // If the new list couldn't be fetched successfully, return false. |
||
| 375 | if ( $api->has_last_error() ) { |
||
| 376 | return false; |
||
| 377 | } |
||
| 378 | |||
| 379 | // We have a new list to save and return! |
||
| 380 | $options->set_option( $type . '_list', $list_new ); |
||
| 381 | $options->save(); |
||
| 382 | |||
| 383 | return $list_new; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Update the list of aspects and return them for use with AJAX. |
||
| 388 | */ |
||
| 389 | public function update_aspects_list_callback() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Update the list of services and return them for use with AJAX. |
||
| 395 | */ |
||
| 396 | public function update_services_list_callback() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Check the pod connection status. |
||
| 402 | * |
||
| 403 | * @return string The status of the connection. |
||
| 404 | */ |
||
| 405 | private function _check_pod_connection_status() { |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Check the connection to the pod and return the status for use with AJAX. |
||
| 419 | * |
||
| 420 | * @todo esc_html |
||
| 421 | */ |
||
| 422 | public function check_pod_connection_status_callback() { |
||
| 423 | if ( isset( $_REQUEST['debugging'] ) && ! defined( 'WP2D_DEBUGGING' ) ) { |
||
| 442 | } |
||
| 443 | |||
| 446 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: