Complex classes like WooCommerce 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 WooCommerce, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | final class WooCommerce { |
||
32 | |||
33 | /** |
||
34 | * WooCommerce version. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | public $version = '2.6.3'; |
||
39 | |||
40 | /** |
||
41 | * The single instance of the class. |
||
42 | * |
||
43 | * @var WooCommerce |
||
44 | * @since 2.1 |
||
45 | */ |
||
46 | protected static $_instance = null; |
||
47 | |||
48 | /** |
||
49 | * Session instance. |
||
50 | * |
||
51 | * @var WC_Session |
||
52 | */ |
||
53 | public $session = null; |
||
54 | |||
55 | /** |
||
56 | * Query instance. |
||
57 | * |
||
58 | * @var WC_Query |
||
59 | */ |
||
60 | public $query = null; |
||
61 | |||
62 | /** |
||
63 | * Product factory instance. |
||
64 | * |
||
65 | * @var WC_Product_Factory |
||
66 | */ |
||
67 | public $product_factory = null; |
||
68 | |||
69 | /** |
||
70 | * Countries instance. |
||
71 | * |
||
72 | * @var WC_Countries |
||
73 | */ |
||
74 | public $countries = null; |
||
75 | |||
76 | /** |
||
77 | * Integrations instance. |
||
78 | * |
||
79 | * @var WC_Integrations |
||
80 | */ |
||
81 | public $integrations = null; |
||
82 | |||
83 | /** |
||
84 | * Cart instance. |
||
85 | * |
||
86 | * @var WC_Cart |
||
87 | */ |
||
88 | public $cart = null; |
||
89 | |||
90 | /** |
||
91 | * Customer instance. |
||
92 | * |
||
93 | * @var WC_Customer |
||
94 | */ |
||
95 | public $customer = null; |
||
96 | |||
97 | /** |
||
98 | * Order factory instance. |
||
99 | * |
||
100 | * @var WC_Order_Factory |
||
101 | */ |
||
102 | public $order_factory = null; |
||
103 | |||
104 | /** |
||
105 | * Main WooCommerce Instance. |
||
106 | * |
||
107 | * Ensures only one instance of WooCommerce is loaded or can be loaded. |
||
108 | * |
||
109 | * @since 2.1 |
||
110 | * @static |
||
111 | * @see WC() |
||
112 | * @return WooCommerce - Main instance. |
||
113 | */ |
||
114 | public static function instance() { |
||
120 | |||
121 | /** |
||
122 | * Cloning is forbidden. |
||
123 | * @since 2.1 |
||
124 | */ |
||
125 | public function __clone() { |
||
128 | |||
129 | /** |
||
130 | * Unserializing instances of this class is forbidden. |
||
131 | * @since 2.1 |
||
132 | */ |
||
133 | public function __wakeup() { |
||
136 | |||
137 | /** |
||
138 | * Auto-load in-accessible properties on demand. |
||
139 | * @param mixed $key |
||
140 | * @return mixed |
||
141 | */ |
||
142 | public function __get( $key ) { |
||
147 | |||
148 | /** |
||
149 | * WooCommerce Constructor. |
||
150 | */ |
||
151 | public function __construct() { |
||
158 | |||
159 | /** |
||
160 | * Hook into actions and filters. |
||
161 | * @since 2.3 |
||
162 | */ |
||
163 | private function init_hooks() { |
||
173 | |||
174 | /** |
||
175 | * Define WC Constants. |
||
176 | */ |
||
177 | private function define_constants() { |
||
178 | $upload_dir = wp_upload_dir(); |
||
179 | |||
180 | $this->define( 'WC_PLUGIN_FILE', __FILE__ ); |
||
181 | $this->define( 'WC_ABSPATH', dirname( __FILE__ ) . '/' ); |
||
182 | $this->define( 'WC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); |
||
183 | $this->define( 'WC_VERSION', $this->version ); |
||
184 | $this->define( 'WOOCOMMERCE_VERSION', $this->version ); |
||
185 | $this->define( 'WC_ROUNDING_PRECISION', 4 ); |
||
186 | $this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 ); |
||
187 | $this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 ); |
||
188 | $this->define( 'WC_DELIMITER', '|' ); |
||
189 | $this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' ); |
||
190 | $this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' ); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Define constant if not already set. |
||
195 | * |
||
196 | * @param string $name |
||
197 | * @param string|bool $value |
||
198 | */ |
||
199 | private function define( $name, $value ) { |
||
204 | |||
205 | /** |
||
206 | * What type of request is this? |
||
207 | * |
||
208 | * @param string $type admin, ajax, cron or frontend. |
||
209 | * @return bool |
||
210 | */ |
||
211 | private function is_request( $type ) { |
||
223 | |||
224 | /** |
||
225 | * Include required core files used in admin and on the frontend. |
||
226 | */ |
||
227 | public function includes() { |
||
228 | include_once( WC_ABSPATH . 'includes/class-wc-autoloader.php' ); |
||
229 | include_once( WC_ABSPATH . 'includes/wc-core-functions.php' ); |
||
230 | include_once( WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php' ); |
||
231 | include_once( WC_ABSPATH . 'includes/wc-widget-functions.php' ); |
||
232 | include_once( WC_ABSPATH . 'includes/wc-webhook-functions.php' ); |
||
233 | include_once( WC_ABSPATH . 'includes/class-wc-install.php' ); |
||
234 | include_once( WC_ABSPATH . 'includes/class-wc-geolocation.php' ); |
||
235 | include_once( WC_ABSPATH . 'includes/class-wc-download-handler.php' ); |
||
236 | include_once( WC_ABSPATH . 'includes/class-wc-comments.php' ); |
||
237 | include_once( WC_ABSPATH . 'includes/class-wc-post-data.php' ); |
||
238 | include_once( WC_ABSPATH . 'includes/class-wc-ajax.php' ); |
||
239 | |||
240 | include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD |
||
241 | |||
242 | if ( $this->is_request( 'admin' ) ) { |
||
243 | include_once( WC_ABSPATH . 'includes/admin/class-wc-admin.php' ); |
||
244 | } |
||
245 | |||
246 | if ( $this->is_request( 'frontend' ) ) { |
||
247 | $this->frontend_includes(); |
||
248 | } |
||
249 | |||
250 | if ( $this->is_request( 'frontend' ) || $this->is_request( 'cron' ) ) { |
||
251 | include_once( WC_ABSPATH . 'includes/class-wc-session-handler.php' ); |
||
252 | } |
||
253 | |||
254 | if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) { |
||
255 | include_once( WC_ABSPATH . 'includes/class-wc-tracker.php' ); |
||
256 | } |
||
257 | |||
258 | include_once( WC_ABSPATH . 'includes/class-wc-query.php' ); // The main query class |
||
259 | include_once( WC_ABSPATH . 'includes/class-wc-api.php' ); // API Class |
||
260 | include_once( WC_ABSPATH . 'includes/class-wc-auth.php' ); // Auth Class |
||
261 | include_once( WC_ABSPATH . 'includes/class-wc-post-types.php' ); // Registers post types |
||
262 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD |
||
263 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens |
||
264 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php' ); // Products |
||
265 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php' ); // Orders |
||
266 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations) |
||
267 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method |
||
268 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway |
||
269 | include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php' ); // An integration with a service |
||
270 | include_once( WC_ABSPATH . 'includes/class-wc-product-factory.php' ); // Product factory |
||
271 | include_once( WC_ABSPATH . 'includes/class-wc-payment-tokens.php' ); // Payment tokens controller |
||
272 | include_once( WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php' ); // CC Payment Gateway |
||
273 | include_once( WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php' ); // eCheck Payment Gateway |
||
274 | include_once( WC_ABSPATH . 'includes/class-wc-countries.php' ); // Defines countries and states |
||
275 | include_once( WC_ABSPATH . 'includes/class-wc-integrations.php' ); // Loads integrations |
||
276 | include_once( WC_ABSPATH . 'includes/class-wc-cache-helper.php' ); // Cache Helper |
||
277 | include_once( WC_ABSPATH . 'includes/class-wc-https.php' ); // https Helper |
||
278 | |||
279 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
280 | include_once( WC_ABSPATH . 'includes/class-wc-cli.php' ); |
||
281 | } |
||
282 | |||
283 | $this->query = new WC_Query(); |
||
284 | $this->api = new WC_API(); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Include required frontend files. |
||
289 | */ |
||
290 | public function frontend_includes() { |
||
304 | |||
305 | /** |
||
306 | * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes. |
||
307 | */ |
||
308 | public function include_template_functions() { |
||
311 | |||
312 | /** |
||
313 | * Init WooCommerce when WordPress Initialises. |
||
314 | */ |
||
315 | public function init() { |
||
346 | |||
347 | /** |
||
348 | * Load Localisation files. |
||
349 | * |
||
350 | * Note: the first-loaded translation file overrides any following ones if the same translation is present. |
||
351 | * |
||
352 | * Locales found in: |
||
353 | * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo |
||
354 | * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo |
||
355 | */ |
||
356 | public function load_plugin_textdomain() { |
||
362 | |||
363 | /** |
||
364 | * Ensure theme and server variable compatibility and setup image sizes. |
||
365 | */ |
||
366 | public function setup_environment() { |
||
375 | |||
376 | /** |
||
377 | * Ensure post thumbnail support is turned on. |
||
378 | */ |
||
379 | private function add_thumbnail_support() { |
||
385 | |||
386 | /** |
||
387 | * Add WC Image sizes to WP. |
||
388 | * |
||
389 | * @since 2.3 |
||
390 | */ |
||
391 | private function add_image_sizes() { |
||
400 | |||
401 | /** |
||
402 | * Get the plugin url. |
||
403 | * @return string |
||
404 | */ |
||
405 | public function plugin_url() { |
||
408 | |||
409 | /** |
||
410 | * Get the plugin path. |
||
411 | * @return string |
||
412 | */ |
||
413 | public function plugin_path() { |
||
416 | |||
417 | /** |
||
418 | * Get the template path. |
||
419 | * @return string |
||
420 | */ |
||
421 | public function template_path() { |
||
424 | |||
425 | /** |
||
426 | * Get Ajax URL. |
||
427 | * @return string |
||
428 | */ |
||
429 | public function ajax_url() { |
||
432 | |||
433 | /** |
||
434 | * Return the WC API URL for a given request. |
||
435 | * |
||
436 | * @param string $request |
||
437 | * @param mixed $ssl (default: null) |
||
438 | * @return string |
||
439 | */ |
||
440 | public function api_request_url( $request, $ssl = null ) { |
||
459 | |||
460 | /** |
||
461 | * Load & enqueue active webhooks. |
||
462 | * |
||
463 | * @since 2.2 |
||
464 | */ |
||
465 | private function load_webhooks() { |
||
480 | |||
481 | /** |
||
482 | * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names. |
||
483 | */ |
||
484 | public function wpdb_table_fix() { |
||
496 | |||
497 | /** |
||
498 | * Get Checkout Class. |
||
499 | * @return WC_Checkout |
||
500 | */ |
||
501 | public function checkout() { |
||
504 | |||
505 | /** |
||
506 | * Get gateways class. |
||
507 | * @return WC_Payment_Gateways |
||
508 | */ |
||
509 | public function payment_gateways() { |
||
512 | |||
513 | /** |
||
514 | * Get shipping class. |
||
515 | * @return WC_Shipping |
||
516 | */ |
||
517 | public function shipping() { |
||
520 | |||
521 | /** |
||
522 | * Email Class. |
||
523 | * @return WC_Emails |
||
524 | */ |
||
525 | public function mailer() { |
||
528 | } |
||
529 | |||
546 |