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-src'; |
||
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 | private function __construct() { |
||
77 | |||
78 | /** |
||
79 | * Create / Get the instance of this class. |
||
80 | * |
||
81 | * @return WP_To_Diaspora Instance of this class. |
||
82 | */ |
||
83 | public static function instance() { |
||
84 | if ( ! isset( self::$_instance ) ) { |
||
85 | self::$_instance = new self(); |
||
86 | if ( self::$_instance->_version_check() ) { |
||
87 | self::$_instance->_constants(); |
||
88 | self::$_instance->_includes(); |
||
89 | self::$_instance->_setup(); |
||
90 | } else { |
||
91 | self::$_instance = null; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | return self::$_instance; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Define all the required constants. |
||
100 | * |
||
101 | * @since 1.5.0 |
||
102 | */ |
||
103 | private function _constants() { |
||
104 | // Are we in debugging mode? |
||
105 | if ( isset( $_GET['debugging'] ) ) { |
||
106 | define( 'WP2D_DEBUGGING', true ); |
||
107 | } |
||
108 | |||
109 | define( 'WP2D_DIR', dirname( __FILE__ ) ); |
||
110 | define( 'WP2D_LIB_DIR', WP2D_DIR . '/lib' ); |
||
111 | define( 'WP2D_VENDOR_DIR', WP2D_DIR . '/vendor' ); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Check the minimum WordPress and PHP requirements. |
||
116 | * |
||
117 | * @since 1.5.4 |
||
118 | * |
||
119 | * @return bool If version requirements are met. |
||
120 | */ |
||
121 | private function _version_check() { |
||
|
|||
122 | // fu(ABSPATH); |
||
123 | printf( |
||
124 | 'WP (min): %s (%s) => %s' . PHP_EOL . 'PHP (min): %s (%s) => %s' . PHP_EOL, |
||
125 | $GLOBALS['wp_version'], |
||
126 | $this->_min_wp, |
||
127 | version_compare( $GLOBALS['wp_version'], $this->_min_wp, '<' ) ? 'fail' : 'ok', |
||
128 | PHP_VERSION, |
||
129 | $this->_min_php, |
||
130 | version_compare( PHP_VERSION, $this->_min_php, '<' ) ? 'fail' : 'ok' |
||
131 | ); |
||
132 | // Check for version requirements. |
||
133 | if ( version_compare( $GLOBALS['wp_version'], $this->_min_wp, '<' ) |
||
134 | || version_compare( PHP_VERSION, $this->_min_php, '<' ) ) { |
||
135 | add_action( 'admin_notices', array( $this, 'deactivate' ) ); |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | return true; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Callback to deactivate plugin and display admin notice. |
||
144 | * |
||
145 | * @since 1.5.4 |
||
146 | */ |
||
147 | public function deactivate() { |
||
161 | |||
162 | /** |
||
163 | * Include all the required files. |
||
164 | * |
||
165 | * @since 1.5.0 |
||
166 | */ |
||
167 | private function _includes() { |
||
175 | |||
176 | /** |
||
177 | * Set up the plugin. |
||
178 | */ |
||
179 | private function _setup() { |
||
207 | |||
208 | /** |
||
209 | * Load the diaspora* API for ease of use. |
||
210 | * |
||
211 | * @return WP2D_API|boolean The API object, or false. |
||
212 | */ |
||
213 | private function _load_api() { |
||
219 | |||
220 | /** |
||
221 | * Initialise upgrade sequence. |
||
222 | */ |
||
223 | public function upgrade() { |
||
263 | |||
264 | /** |
||
265 | * Set up i18n. |
||
266 | */ |
||
267 | public function l10n() { |
||
270 | |||
271 | /** |
||
272 | * Load scripts and styles for Settings and Post pages of allowed post types. |
||
273 | */ |
||
274 | public function admin_load_scripts() { |
||
299 | |||
300 | /** |
||
301 | * Add the "Settings" link to the plugins page. |
||
302 | * |
||
303 | * @param array $links Links to display for plugin on plugins page. |
||
304 | * @return array Links to display for plugin on plugins page. |
||
305 | */ |
||
306 | public function settings_link( $links ) { |
||
310 | |||
311 | /** |
||
312 | * Fetch the updated list of pods from podupti.me and save it to the settings. |
||
313 | * |
||
314 | * @return array The list of pods. |
||
315 | */ |
||
316 | private function _update_pod_list() { |
||
345 | |||
346 | /** |
||
347 | * Update the list of pods and return them for use with AJAX. |
||
348 | */ |
||
349 | public function update_pod_list_callback() { |
||
352 | |||
353 | /** |
||
354 | * Fetch the list of aspects or services and save them to the settings. |
||
355 | * |
||
356 | * NOTE: When updating the lists, always force a fresh fetch. |
||
357 | * |
||
358 | * @param string $type Type of list to update. |
||
359 | * @return array|boolean The list of aspects or services, false if an illegal parameter is passed. |
||
360 | */ |
||
361 | private function _update_aspects_services_list( $type ) { |
||
399 | |||
400 | /** |
||
401 | * Update the list of aspects and return them for use with AJAX. |
||
402 | */ |
||
403 | public function update_aspects_list_callback() { |
||
406 | |||
407 | /** |
||
408 | * Update the list of services and return them for use with AJAX. |
||
409 | */ |
||
410 | public function update_services_list_callback() { |
||
413 | |||
414 | /** |
||
415 | * Check the pod connection status. |
||
416 | * |
||
417 | * @return string The status of the connection. |
||
418 | */ |
||
419 | private function _check_pod_connection_status() { |
||
430 | |||
431 | /** |
||
432 | * Check the connection to the pod and return the status for use with AJAX. |
||
433 | * |
||
434 | * @todo esc_html |
||
435 | */ |
||
436 | public function check_pod_connection_status_callback() { |
||
456 | } |
||
457 | |||
458 | // Get the party started! |
||
459 | WP_To_Diaspora::instance(); |
||
460 |
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: