Complex classes like WPBO_MailChimp 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 WPBO_MailChimp, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | final class WPBO_MailChimp { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var WPBO_MailChimp $instance Holds the unique instance of the MailChimp provider |
||
| 21 | * @since 2.0 |
||
| 22 | */ |
||
| 23 | private static $instance; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string MailChimp API key |
||
| 27 | * @since 1.0 |
||
| 28 | */ |
||
| 29 | private static $api_key; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string MailChimp list ID |
||
| 33 | * @since 1.0 |
||
| 34 | */ |
||
| 35 | private static $list_id; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool Whether or not to use double optin for new subscribers |
||
| 39 | * @since 1.0 |
||
| 40 | */ |
||
| 41 | private static $double_optin; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool Whether to update existing contacts or add new |
||
| 45 | * @since 1.0 |
||
| 46 | */ |
||
| 47 | private static $update; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool Whether or not to send the welcome message to new subscribers |
||
| 51 | * @since 1.0 |
||
| 52 | */ |
||
| 53 | private static $welcome; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array A list of allowed fields to be passed to MailChimp |
||
| 57 | * @since 1.0 |
||
| 58 | */ |
||
| 59 | private static $fields; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Instantiate and return the unique BetterOptin object |
||
| 63 | * |
||
| 64 | * @since 2.0 |
||
| 65 | * @return object BetterOptin Unique instance of BetterOptin |
||
| 66 | */ |
||
| 67 | public static function instance() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Throw error on object clone |
||
| 84 | * |
||
| 85 | * The whole idea of the singleton design pattern is that there is a single |
||
| 86 | * object therefore, we don't want the object to be cloned. |
||
| 87 | * |
||
| 88 | * @since 2.0 |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function __clone() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Disable unserializing of the class |
||
| 98 | * |
||
| 99 | * @since 2.0 |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function __wakeup() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Setup MailChimp options |
||
| 109 | * |
||
| 110 | * @since 2.0 |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | private function setup_options() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Load the MailChimp API wrapper |
||
| 124 | * |
||
| 125 | * @since 1.0 |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | private function load_mailchimp_api_wrapper() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Trigger form submission. |
||
| 141 | * |
||
| 142 | * Add a last couple of checks, set the redirects and |
||
| 143 | * finally subscribe the visitor to the MailChimp list. |
||
| 144 | * |
||
| 145 | * @since 1.0.0 |
||
| 146 | * |
||
| 147 | * @param array $data Popup form data |
||
| 148 | * |
||
| 149 | * @return null |
||
| 150 | */ |
||
| 151 | public function submit( $data ) { |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Check if MailChimp settings are correct. |
||
| 172 | * |
||
| 173 | * @since 1.0.0 |
||
| 174 | * @return boolean True if MailChimp integration is ready to work |
||
| 175 | */ |
||
| 176 | public function is_mailchimp_ready() { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Clean the post. |
||
| 188 | * |
||
| 189 | * Filter the post data and only keep |
||
| 190 | * values that are actually supported |
||
| 191 | * by the API. |
||
| 192 | * |
||
| 193 | * @since 1.0.0 |
||
| 194 | * @return array Clean list of merge fields |
||
| 195 | */ |
||
| 196 | protected function clean_fields() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Subscribe the visitor to a list. |
||
| 220 | * |
||
| 221 | * @since 1.0.0 |
||
| 222 | * |
||
| 223 | * @param array $data Popup form data |
||
| 224 | * |
||
| 225 | * @return array Result |
||
| 226 | */ |
||
| 227 | private function subscribe( $data ) { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get user lists. |
||
| 302 | * |
||
| 303 | * @since 1.0.0 |
||
| 304 | * @return array Array of available lists for this account |
||
| 305 | */ |
||
| 306 | public function get_lists() { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get the groups list. |
||
| 323 | * |
||
| 324 | * @since 1.1.0 |
||
| 325 | * |
||
| 326 | * @param string $list_id ID of the list to get the groups of |
||
| 327 | * |
||
| 328 | * @return mixed Array of groups or error |
||
| 329 | */ |
||
| 330 | public function get_groups( $list_id = '' ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Check if a list has groups. |
||
| 377 | * |
||
| 378 | * Use the MailChimp API to check if a given mailing list |
||
| 379 | * has groups available. |
||
| 380 | * |
||
| 381 | * @since 1.1.0 |
||
| 382 | * |
||
| 383 | * @param string $list_id The list ID |
||
| 384 | * |
||
| 385 | * @return boolean Request result |
||
| 386 | */ |
||
| 387 | public function has_groups( $list_id = '' ) { |
||
| 404 | |||
| 405 | } |