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.1'; |
||
| 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 to 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_sub_product_scheme_option' filter. |
||
| 143 | add_filter( 'wcsatt_sub_product_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_get_single_product_price_string' filter. |
||
| 146 | add_filter( 'wcsatt_get_single_product_price_string', 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() { |
||
| 167 | // Subscription scheme options displayed on the 'wcsatt_subscription_scheme_product_content' action. |
||
| 168 | add_action( 'wcsatt_subscription_scheme_product_content', array( $this, 'wcsatt_stt_fields' ), 15, 3 ); |
||
| 169 | |||
| 170 | // Filter the subscription scheme data to process the sign up and trial options on the ''wcsatt_subscription_scheme_process_scheme_data' filter. |
||
| 171 | add_filter( 'wcsatt_subscription_scheme_process_scheme_data', array( $this, 'wcsatt_stt_process_scheme_data' ), 10, 2 ); |
||
| 172 | } // END 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 ) { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Adds the default values for subscriptions schemes content. |
||
| 192 | * |
||
| 193 | * @param array $defaults |
||
| 194 | * @return void |
||
| 195 | */ |
||
| 196 | public static function add_default_subscription_schemes_content( $defaults ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Adds the trial and sign up fields under the subscription section. |
||
| 208 | * |
||
| 209 | * @param int $index |
||
| 210 | * @param array $scheme_data |
||
| 211 | * @param int $post_id |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function wcsatt_stt_fields( $index, $scheme_data, $post_id ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Filters the subscription scheme data to pass the |
||
| 269 | * sign up and trial options when saving. |
||
| 270 | * |
||
| 271 | * @param ini $posted_scheme |
||
| 272 | * @param string $product_type |
||
| 273 | * @return void |
||
| 274 | */ |
||
| 275 | public function wcsatt_stt_process_scheme_data( $posted_scheme, $product_type ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds the additional subscription scheme data for products with attached subscription schemes. |
||
| 314 | * |
||
| 315 | * @param object $_product |
||
| 316 | * @param array $subscription_scheme |
||
| 317 | * @param WC_Product $product |
||
| 318 | * @return string |
||
| 319 | */ |
||
| 320 | public function add_sub_scheme_data_price_html( $_product, $subscription_scheme, $product ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Adds the extra subscription scheme data to the product object. |
||
| 338 | * This allows the subscription price to change the initial and |
||
| 339 | * recurring subscriptions. |
||
| 340 | * |
||
| 341 | * @param object $_cloned |
||
| 342 | * @param array $subscription_scheme |
||
| 343 | * @return object |
||
| 344 | */ |
||
| 345 | public function sub_product_scheme_option( $_cloned, $subscription_scheme ) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Filters the price string to include the sign up fee and/or trial |
||
| 360 | * to pass per subscription scheme option. |
||
| 361 | * |
||
| 362 | * @param array $prices |
||
| 363 | * @param array $subscription_scheme |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function get_price_string( $prices, $subscription_scheme ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Filters the price string to include the sign up |
||
| 380 | * fee on the lowest subscription scheme. |
||
| 381 | * |
||
| 382 | * @param array $prices |
||
| 383 | * @param array $lowest_subscription_scheme |
||
| 384 | * @return array |
||
| 385 | */ |
||
| 386 | public function get_lowest_price_string( $prices, $lowest_subscription_scheme ) { |
||
| 387 | if ( isset( $lowest_subscription_scheme[ 'sign_up_fee' ] ) && $lowest_subscription_scheme[ 'sign_up_fee' ] > 0 ) { |
||
| 388 | $prices[ 'sign_up_fee' ] = $lowest_subscription_scheme[ 'sign_up_fee' ]; |
||
| 389 | } |
||
| 390 | |||
| 391 | return $prices; |
||
| 392 | } // END get_lowest_price_string() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Adds the sign-up fee to the lowest subscription scheme option. |
||
| 396 | * |
||
| 397 | * @param array $data |
||
| 398 | * @param array $lowest_scheme |
||
| 399 | * @return array |
||
| 400 | */ |
||
| 401 | public function get_lowest_price_sub_scheme_data( $data, $lowest_scheme ) { |
||
| 402 | if ( isset( $lowest_scheme['subscription_sign_up_fee'] ) && $lowest_scheme['subscription_sign_up_fee'] > 0 ) { |
||
| 403 | $data['sign_up_fee'] = $lowest_scheme['subscription_sign_up_fee']; |
||
| 404 | } |
||
| 405 | |||
| 406 | return $data; |
||
| 407 | } // END get_lowest_price_sub_scheme_data() |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Adds the sign-up and/or trial data to the subscription scheme prices. |
||
| 411 | * |
||
| 412 | * @param array $prices |
||
| 413 | * @param array $subscription_scheme |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function add_subscription_scheme_prices( $prices, $subscription_scheme ) { |
||
| 417 | if ( isset( $subscription_scheme[ 'subscription_sign_up_fee' ] ) ) { |
||
| 418 | $prices[ 'sign_up_fee' ] = $subscription_scheme[ 'subscription_sign_up_fee' ]; |
||
| 419 | } |
||
| 420 | |||
| 421 | if ( isset( $subscription_scheme[ 'subscription_trial_length' ] ) ) { |
||
| 422 | $prices[ 'trial_length' ] = $subscription_scheme[ 'subscription_trial_length' ]; |
||
| 423 | } |
||
| 424 | |||
| 425 | if ( isset( $subscription_scheme[ 'subscription_trial_period' ] ) ) { |
||
| 426 | $prices[ 'trial_period' ] = $subscription_scheme[ 'subscription_trial_period' ]; |
||
| 427 | } |
||
| 428 | |||
| 429 | return $prices; |
||
| 430 | } // END add_subscription_scheme_prices() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Updates the cart item data for a subscription product that |
||
| 434 | * has a sign-up fee and/or trial period applied. |
||
| 435 | * |
||
| 436 | * @param array $cart_item |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | public function update_cart_item_sub_data( $cart_item ) { |
||
| 482 | |||
| 483 | } // END class |
||
| 484 | |||
| 485 | } // END if class exists |
||
| 486 | |||
| 487 | 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.