Complex classes like WCSATT_STT 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 WCSATT_STT, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WCSATT_STT { |
||
24 | |||
25 | /* Plugin version. */ |
||
26 | const VERSION = '1.0.0'; |
||
27 | |||
28 | /* Required WC version. */ |
||
29 | const REQ_WC_VERSION = '2.3.0'; |
||
30 | |||
31 | /* Required WCSATT version */ |
||
32 | const REQ_WCSATT_VERSION = '1.1.0'; |
||
33 | |||
34 | /* Text domain. */ |
||
35 | const TEXT_DOMAIN = 'wc-satt-stt'; |
||
36 | |||
37 | /** |
||
38 | * @var WCSATT_STT - the single instance of the class. |
||
39 | * |
||
40 | * @since 1.0.0 |
||
41 | */ |
||
42 | protected static $_instance = null; |
||
43 | |||
44 | /** |
||
45 | * Main WCSATT_STT Instance. |
||
46 | * |
||
47 | * Ensures only one instance of WCSATT_STT is loaded or can be loaded. |
||
48 | * |
||
49 | * @static |
||
50 | * @see WCSATT_STT() |
||
51 | * @return WCSATT_STT - Main instance |
||
52 | * @since 1.0.0 |
||
53 | */ |
||
54 | public static function instance() { |
||
60 | |||
61 | /** |
||
62 | * Cloning is forbidden. |
||
63 | * |
||
64 | * @since 1.0.0 |
||
65 | */ |
||
66 | public function __clone() { |
||
69 | |||
70 | /** |
||
71 | * Unserializing instances of this class is forbidden. |
||
72 | * |
||
73 | * @since 1.0.0 |
||
74 | */ |
||
75 | public function __wakeup() { |
||
78 | |||
79 | /** |
||
80 | * Load the plugin. |
||
81 | */ |
||
82 | public function __construct() { |
||
88 | |||
89 | public function plugin_path() { |
||
92 | |||
93 | /* |
||
94 | * Check requirements on activation. |
||
95 | */ |
||
96 | public function load_plugin() { |
||
97 | global $woocommerce; |
||
98 | |||
99 | // Check that the required WooCommerce is running. |
||
100 | if ( version_compare( $woocommerce->version, self::REQ_WC_VERSION, '<' ) ) { |
||
101 | add_action( 'admin_notices', array( $this, 'wcsatt_stt_wc_admin_notice' ) ); |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | // Checks that WooCommerce Subscribe All the Things is running or is less than the required version. |
||
106 | if ( ! class_exists( 'WCS_ATT' ) || version_compare( WCS_ATT::VERSION, self::REQ_WCSATT_VERSION, '<' ) ) { |
||
107 | add_action( 'admin_notices', array( $this, 'wcsatt_stt_admin_notice' ) ); |
||
108 | return false; |
||
109 | } |
||
110 | } // END load_plugin() |
||
111 | |||
112 | /** |
||
113 | * Display a warning message if minimum version of WooCommerce check fails. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function wcsatt_stt_wc_admin_notice() { |
||
120 | |||
121 | /** |
||
122 | * Display a warning message if minimum version of WooCommerce Subscribe All the Things check fails. |
||
123 | * |
||
124 | * @return void |
||
125 | */ |
||
126 | public function wcsatt_stt_admin_notice() { |
||
129 | |||
130 | /** |
||
131 | * Initialize the plugin if ready. |
||
132 | * |
||
133 | * @return void |
||
134 | */ |
||
135 | public function init_plugin() { |
||
136 | // Load text domain. |
||
137 | load_plugin_textdomain( 'wc-satt-stt', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
||
138 | |||
139 | // Adds the sign up fee and trial data to the price html on the 'wcsatt_overridden_subscription_prices_product' filter. |
||
140 | add_filter( 'wcsatt_overridden_subscription_prices_product', array( $this, 'add_sub_scheme_data_price_html' ), 10, 3 ); |
||
141 | |||
142 | // Adds the extra subscription scheme data to the product object on the 'wcsatt_converted_product_for_scheme_option' filter. |
||
143 | add_filter( 'wcsatt_converted_product_for_scheme_option', array( $this, 'sub_product_scheme_option' ), 10, 2 ); |
||
144 | |||
145 | // Filters the price string to include the sign up fee and/or trial to pass per scheme option on the 'wcsatt_single_product_subscription_scheme_price_html' filter. |
||
146 | add_filter( 'wcsatt_single_product_subscription_scheme_price_html', array( $this, 'get_price_string' ), 10, 2 ); |
||
147 | |||
148 | // Filters the lowest price string to include the sign up fee on the 'wcsatt_get_single_product_lowest_price_string' filter. |
||
149 | add_filter( 'wcsatt_get_single_product_lowest_price_string', array( $this, 'get_lowest_price_string' ), 10, 2 ); |
||
150 | |||
151 | // Filters the lowest price subscription scheme data on the 'wcsatt_get_lowest_price_sub_scheme_data' filter. |
||
152 | add_filter( 'wcsatt_get_lowest_price_sub_scheme_data', array( $this, 'get_lowest_price_sub_scheme_data' ), 10, 2 ); |
||
153 | |||
154 | // Adds the sign-up and/or trial data to the subscription scheme prices on the 'wcsatt_subscription_scheme_prices' filter. |
||
155 | add_filter( 'wcsatt_subscription_scheme_prices', array( $this, 'add_subscription_scheme_prices' ), 10, 2 ); |
||
156 | |||
157 | // Overrides the price of the subscription for sign up fee and/or trial on the 'wcsatt_cart_item' filter. |
||
158 | add_filter( 'wcsatt_cart_item', array( $this, 'update_cart_item_sub_data' ), 10, 1 ); |
||
159 | } // END init_plugin() |
||
160 | |||
161 | /** |
||
162 | * Register the product meta data fields. |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function admin_wcsatt_stt_product_meta() { |
||
173 | |||
174 | /** |
||
175 | * Show row meta on the plugin screen. |
||
176 | * |
||
177 | * @param mixed $links Plugin Row Meta |
||
178 | * @param mixed $file Plugin Base file |
||
179 | * @return array |
||
180 | */ |
||
181 | public function plugin_meta_links( $links, $file, $data, $status ) { |
||
190 | |||
191 | /** |
||
192 | * Adds the default values for subscriptions schemes content. |
||
193 | * |
||
194 | * @param array $defaults |
||
195 | * @return void |
||
196 | */ |
||
197 | public static function add_default_subscription_schemes_content( $defaults ) { |
||
206 | |||
207 | /** |
||
208 | * Adds the trial and sign up fields under the subscription section. |
||
209 | * |
||
210 | * @param int $index |
||
211 | * @param array $scheme_data |
||
212 | * @param int $post_id |
||
213 | * @return void |
||
214 | */ |
||
215 | public function wcsatt_stt_fields( $index, $scheme_data, $post_id ) { |
||
267 | |||
268 | /** |
||
269 | * Filters the subscription scheme data to pass the |
||
270 | * sign up and trial options when saving. |
||
271 | * |
||
272 | * @param ini $posted_scheme |
||
273 | * @param string $product_type |
||
274 | * @return void |
||
275 | */ |
||
276 | public function wcsatt_stt_process_scheme_data( $posted_scheme, $product_type ) { |
||
312 | |||
313 | /** |
||
314 | * Adds the additional subscription scheme data for products with attached subscription schemes. |
||
315 | * |
||
316 | * @param object $_product |
||
317 | * @param array $subscription_scheme |
||
318 | * @param WC_Product $product |
||
319 | * @return string |
||
320 | */ |
||
321 | public function add_sub_scheme_data_price_html( $_product, $subscription_scheme, $product ) { |
||
336 | |||
337 | /** |
||
338 | * Adds the extra subscription scheme data to the product object. |
||
339 | * This allows the subscription price to change the initial and |
||
340 | * recurring subscriptions. |
||
341 | * |
||
342 | * @param object $_cloned |
||
343 | * @param array $subscription_scheme |
||
344 | * @return object |
||
345 | */ |
||
346 | public function sub_product_scheme_option( $_cloned, $subscription_scheme ) { |
||
358 | |||
359 | /** |
||
360 | * Filters the price string to include the sign up fee and/or trial |
||
361 | * to pass per subscription scheme option. |
||
362 | * |
||
363 | * @param array $prices |
||
364 | * @param array $subscription_scheme |
||
365 | * @return array |
||
366 | */ |
||
367 | public function get_price_string( $prices, $subscription_scheme ) { |
||
378 | |||
379 | /** |
||
380 | * Filters the price string to include the sign up |
||
381 | * fee on the lowest subscription scheme. |
||
382 | * |
||
383 | * @param array $prices |
||
384 | * @param array $lowest_subscription_scheme |
||
385 | * @return array |
||
386 | */ |
||
387 | public function get_lowest_price_string( $prices, $lowest_subscription_scheme ) { |
||
394 | |||
395 | /** |
||
396 | * Adds the sign-up fee to the lowest subscription scheme option. |
||
397 | * |
||
398 | * @param array $data |
||
399 | * @param array $lowest_scheme |
||
400 | * @return array |
||
401 | */ |
||
402 | public function get_lowest_price_sub_scheme_data( $data, $lowest_scheme ) { |
||
409 | |||
410 | /** |
||
411 | * Adds the sign-up and/or trial data to the subscription scheme prices. |
||
412 | * |
||
413 | * @param array $prices |
||
414 | * @param array $subscription_scheme |
||
415 | * @return array |
||
416 | */ |
||
417 | public function add_subscription_scheme_prices( $prices, $subscription_scheme ) { |
||
432 | |||
433 | /** |
||
434 | * Updates the cart item data for a subscription product that |
||
435 | * has a sign-up fee and/or trial period applied. |
||
436 | * |
||
437 | * @param array $cart_item |
||
438 | * @return array |
||
439 | */ |
||
440 | public function update_cart_item_sub_data( $cart_item ) { |
||
477 | |||
478 | } // END class |
||
479 | |||
482 | return WCSATT_STT::instance(); |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.