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 |
||
16 | final class WooCommerce { |
||
17 | |||
18 | /** |
||
19 | * WooCommerce version. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | public $version = '3.7.0'; |
||
24 | |||
25 | /** |
||
26 | * The single instance of the class. |
||
27 | * |
||
28 | * @var WooCommerce |
||
29 | * @since 2.1 |
||
30 | */ |
||
31 | protected static $_instance = null; |
||
32 | |||
33 | /** |
||
34 | * Session instance. |
||
35 | * |
||
36 | * @var WC_Session|WC_Session_Handler |
||
37 | */ |
||
38 | public $session = null; |
||
39 | |||
40 | /** |
||
41 | * Query instance. |
||
42 | * |
||
43 | * @var WC_Query |
||
44 | */ |
||
45 | public $query = null; |
||
46 | |||
47 | /** |
||
48 | * Product factory instance. |
||
49 | * |
||
50 | * @var WC_Product_Factory |
||
51 | */ |
||
52 | public $product_factory = null; |
||
53 | |||
54 | /** |
||
55 | * Countries instance. |
||
56 | * |
||
57 | * @var WC_Countries |
||
58 | */ |
||
59 | public $countries = null; |
||
60 | |||
61 | /** |
||
62 | * Integrations instance. |
||
63 | * |
||
64 | * @var WC_Integrations |
||
65 | */ |
||
66 | public $integrations = null; |
||
67 | |||
68 | /** |
||
69 | * Cart instance. |
||
70 | * |
||
71 | * @var WC_Cart |
||
72 | */ |
||
73 | public $cart = null; |
||
74 | |||
75 | /** |
||
76 | * Customer instance. |
||
77 | * |
||
78 | * @var WC_Customer |
||
79 | */ |
||
80 | public $customer = null; |
||
81 | |||
82 | /** |
||
83 | * Order factory instance. |
||
84 | * |
||
85 | * @var WC_Order_Factory |
||
86 | */ |
||
87 | public $order_factory = null; |
||
88 | |||
89 | /** |
||
90 | * Structured data instance. |
||
91 | * |
||
92 | * @var WC_Structured_Data |
||
93 | */ |
||
94 | public $structured_data = null; |
||
95 | |||
96 | /** |
||
97 | * Array of deprecated hook handlers. |
||
98 | * |
||
99 | * @var array of WC_Deprecated_Hooks |
||
100 | */ |
||
101 | public $deprecated_hook_handlers = array(); |
||
102 | |||
103 | /** |
||
104 | * Main WooCommerce Instance. |
||
105 | * |
||
106 | * Ensures only one instance of WooCommerce is loaded or can be loaded. |
||
107 | * |
||
108 | * @since 2.1 |
||
109 | * @static |
||
110 | * @see WC() |
||
111 | * @return WooCommerce - Main instance. |
||
112 | */ |
||
113 | 827 | public static function instance() { |
|
119 | |||
120 | /** |
||
121 | * Cloning is forbidden. |
||
122 | * |
||
123 | * @since 2.1 |
||
124 | */ |
||
125 | public function __clone() { |
||
128 | |||
129 | /** |
||
130 | * Unserializing instances of this class is forbidden. |
||
131 | * |
||
132 | * @since 2.1 |
||
133 | */ |
||
134 | public function __wakeup() { |
||
137 | |||
138 | /** |
||
139 | * Auto-load in-accessible properties on demand. |
||
140 | * |
||
141 | * @param mixed $key Key name. |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 98 | public function __get( $key ) { |
|
149 | |||
150 | /** |
||
151 | * WooCommerce Constructor. |
||
152 | */ |
||
153 | public function __construct() { |
||
154 | $this->define_constants(); |
||
155 | $this->define_tables(); |
||
156 | $this->includes(); |
||
157 | $this->init_hooks(); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * When WP has loaded all plugins, trigger the `woocommerce_loaded` hook. |
||
162 | * |
||
163 | * This ensures `woocommerce_loaded` is called only after all other plugins |
||
164 | * are loaded, to avoid issues caused by plugin directory naming changing |
||
165 | * the load order. See #21524 for details. |
||
166 | * |
||
167 | * @since 3.6.0 |
||
168 | */ |
||
169 | public function on_plugins_loaded() { |
||
170 | do_action( 'woocommerce_loaded' ); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Hook into actions and filters. |
||
175 | * |
||
176 | * @since 2.3 |
||
177 | */ |
||
178 | private function init_hooks() { |
||
179 | register_activation_hook( WC_PLUGIN_FILE, array( 'WC_Install', 'install' ) ); |
||
180 | register_shutdown_function( array( $this, 'log_errors' ) ); |
||
181 | |||
182 | add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 ); |
||
183 | add_action( 'after_setup_theme', array( $this, 'setup_environment' ) ); |
||
184 | add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); |
||
185 | add_action( 'init', array( $this, 'init' ), 0 ); |
||
186 | add_action( 'init', array( 'WC_Shortcodes', 'init' ) ); |
||
187 | add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) ); |
||
188 | add_action( 'init', array( $this, 'add_image_sizes' ) ); |
||
189 | add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 ); |
||
190 | add_action( 'activated_plugin', array( $this, 'activated_plugin' ) ); |
||
191 | add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Ensures fatal errors are logged so they can be picked up in the status report. |
||
196 | * |
||
197 | * @since 3.2.0 |
||
198 | */ |
||
199 | public function log_errors() { |
||
213 | |||
214 | /** |
||
215 | * Define WC Constants. |
||
216 | */ |
||
217 | private function define_constants() { |
||
232 | |||
233 | /** |
||
234 | * Register custom tables within $wpdb object. |
||
235 | */ |
||
236 | private function define_tables() { |
||
251 | |||
252 | /** |
||
253 | * Define constant if not already set. |
||
254 | * |
||
255 | * @param string $name Constant name. |
||
256 | * @param string|bool $value Constant value. |
||
257 | */ |
||
258 | private function define( $name, $value ) { |
||
263 | |||
264 | /** |
||
265 | * Returns true if the request is a non-legacy REST API request. |
||
266 | * |
||
267 | * Legacy REST requests should still run some extra code for backwards compatibility. |
||
268 | * |
||
269 | * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061. |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | 19 | public function is_rest_api_request() { |
|
283 | |||
284 | /** |
||
285 | * What type of request is this? |
||
286 | * |
||
287 | * @param string $type admin, ajax, cron or frontend. |
||
288 | * @return bool |
||
289 | */ |
||
290 | private function is_request( $type ) { |
||
302 | |||
303 | /** |
||
304 | * Include required core files used in admin and on the frontend. |
||
305 | */ |
||
306 | public function includes() { |
||
463 | |||
464 | /** |
||
465 | * Include classes for theme support. |
||
466 | * |
||
467 | * @since 3.3.0 |
||
468 | */ |
||
469 | private function theme_support_includes() { |
||
502 | |||
503 | /** |
||
504 | * Include required frontend files. |
||
505 | */ |
||
506 | public function frontend_includes() { |
||
520 | |||
521 | /** |
||
522 | * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes. |
||
523 | */ |
||
524 | public function include_template_functions() { |
||
527 | |||
528 | /** |
||
529 | * Init WooCommerce when WordPress Initialises. |
||
530 | */ |
||
531 | public function init() { |
||
557 | |||
558 | /** |
||
559 | * Load Localisation files. |
||
560 | * |
||
561 | * Note: the first-loaded translation file overrides any following ones if the same translation is present. |
||
562 | * |
||
563 | * Locales found in: |
||
564 | * - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo |
||
565 | * - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo |
||
566 | */ |
||
567 | 1 | public function load_plugin_textdomain() { |
|
575 | |||
576 | /** |
||
577 | * Ensure theme and server variable compatibility and setup image sizes. |
||
578 | */ |
||
579 | public function setup_environment() { |
||
589 | |||
590 | /** |
||
591 | * Ensure post thumbnail support is turned on. |
||
592 | */ |
||
593 | private function add_thumbnail_support() { |
||
599 | |||
600 | /** |
||
601 | * Add WC Image sizes to WP. |
||
602 | * |
||
603 | * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce |
||
604 | * and defining an array of args. If these are not defined, we will use defaults. This is |
||
605 | * handled in wc_get_image_size function. |
||
606 | * |
||
607 | * 3.3 sizes: |
||
608 | * |
||
609 | * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout. |
||
610 | * woocommerce_single - Used on single product pages for the main image. |
||
611 | * |
||
612 | * @since 2.3 |
||
613 | */ |
||
614 | public function add_image_sizes() { |
||
632 | |||
633 | /** |
||
634 | * Get the plugin url. |
||
635 | * |
||
636 | * @return string |
||
637 | */ |
||
638 | 24 | public function plugin_url() { |
|
641 | |||
642 | /** |
||
643 | * Get the plugin path. |
||
644 | * |
||
645 | * @return string |
||
646 | */ |
||
647 | 43 | public function plugin_path() { |
|
650 | |||
651 | /** |
||
652 | * Get the template path. |
||
653 | * |
||
654 | * @return string |
||
655 | */ |
||
656 | 25 | public function template_path() { |
|
659 | |||
660 | /** |
||
661 | * Get Ajax URL. |
||
662 | * |
||
663 | * @return string |
||
664 | */ |
||
665 | public function ajax_url() { |
||
668 | |||
669 | /** |
||
670 | * Return the WC API URL for a given request. |
||
671 | * |
||
672 | * @param string $request Requested endpoint. |
||
673 | * @param bool|null $ssl If should use SSL, null if should auto detect. Default: null. |
||
674 | * @return string |
||
675 | */ |
||
676 | public function api_request_url( $request, $ssl = null ) { |
||
695 | |||
696 | /** |
||
697 | * Load & enqueue active webhooks. |
||
698 | * |
||
699 | * @since 2.2 |
||
700 | */ |
||
701 | private function load_webhooks() { |
||
717 | |||
718 | /** |
||
719 | * Initialize the customer and cart objects and setup customer saving on shutdown. |
||
720 | * |
||
721 | * @since 3.6.4 |
||
722 | * @return void |
||
723 | */ |
||
724 | 1 | public function initialize_cart() { |
|
735 | |||
736 | /** |
||
737 | * Initialize the session class. |
||
738 | * |
||
739 | * @since 3.6.4 |
||
740 | * @return void |
||
741 | */ |
||
742 | 1 | public function initialize_session() { |
|
750 | |||
751 | /** |
||
752 | * Set tablenames inside WPDB object. |
||
753 | */ |
||
754 | public function wpdb_table_fix() { |
||
757 | |||
758 | /** |
||
759 | * Ran when any plugin is activated. |
||
760 | * |
||
761 | * @since 3.6.0 |
||
762 | * @param string $filename The filename of the activated plugin. |
||
763 | */ |
||
764 | public function activated_plugin( $filename ) { |
||
769 | |||
770 | /** |
||
771 | * Ran when any plugin is deactivated. |
||
772 | * |
||
773 | * @since 3.6.0 |
||
774 | * @param string $filename The filename of the deactivated plugin. |
||
775 | */ |
||
776 | public function deactivated_plugin( $filename ) { |
||
781 | |||
782 | /** |
||
783 | * Get queue instance. |
||
784 | * |
||
785 | * @return WC_Queue_Interface |
||
786 | */ |
||
787 | public function queue() { |
||
790 | |||
791 | /** |
||
792 | * Get Checkout Class. |
||
793 | * |
||
794 | * @return WC_Checkout |
||
795 | */ |
||
796 | 1 | public function checkout() { |
|
799 | |||
800 | /** |
||
801 | * Get gateways class. |
||
802 | * |
||
803 | * @return WC_Payment_Gateways |
||
804 | */ |
||
805 | 102 | public function payment_gateways() { |
|
808 | |||
809 | /** |
||
810 | * Get shipping class. |
||
811 | * |
||
812 | * @return WC_Shipping |
||
813 | */ |
||
814 | 569 | public function shipping() { |
|
817 | |||
818 | /** |
||
819 | * Email Class. |
||
820 | * |
||
821 | * @return WC_Emails |
||
822 | */ |
||
823 | 1 | public function mailer() { |
|
826 | } |
||
827 |