| Total Complexity | 43 |
| Total Lines | 332 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WPInv_Subscriptions 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.
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 WPInv_Subscriptions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WPInv_Subscriptions { |
||
| 16 | |||
| 17 | private static $instance; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Main WPInv_Subscriptions Instance |
||
| 21 | */ |
||
| 22 | public static function instance() { |
||
| 23 | if ( ! isset( self::$instance ) ) { |
||
| 24 | self::$instance = new WPInv_Subscriptions; |
||
| 25 | |||
| 26 | self::$instance->init(); |
||
| 27 | } |
||
| 28 | |||
| 29 | return self::$instance; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor -- prevent new instances |
||
| 34 | * |
||
| 35 | * @since 1.0.0 |
||
| 36 | */ |
||
| 37 | private function __construct(){ |
||
| 38 | |||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get things started |
||
| 43 | * |
||
| 44 | * Sets up inits actions and filters |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | */ |
||
| 48 | function init() { |
||
|
|
|||
| 49 | |||
| 50 | self::setup_constants(); |
||
| 51 | self::actions(); |
||
| 52 | self::filters(); |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Setup plugin constants. |
||
| 58 | * |
||
| 59 | * @access private |
||
| 60 | * @since 1.0.0 |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | private function setup_constants() { |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add our actions |
||
| 73 | * |
||
| 74 | * @since 1.0.0 |
||
| 75 | * @return void |
||
| 76 | */ |
||
| 77 | private function actions() { |
||
| 78 | |||
| 79 | add_action( 'admin_menu', array( $this, 'wpinv_subscriptions_list' ), 40 ); |
||
| 80 | add_action( 'admin_notices', array( $this, 'notices' ) ); |
||
| 81 | add_action( 'init', array( $this, 'wpinv_post_actions' ) ); |
||
| 82 | add_action( 'init', array( $this, 'wpinv_get_actions' ) ); |
||
| 83 | add_action( 'wpinv_cancel_subscription', array( $this, 'wpinv_process_cancellation' ) ); |
||
| 84 | add_action( 'wpinv_checkout_before_send_to_gateway', array( $this, 'wpinv_checkout_add_subscription' ), -999, 2 ); |
||
| 85 | add_action( 'wpinv_subscriptions_front_notices', array( $this, 'notices' ) ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add our filters |
||
| 90 | * |
||
| 91 | * @since 1.0 |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | private function filters() { |
||
| 95 | |||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Register our Subscriptions submenu |
||
| 100 | * |
||
| 101 | * @since 2.4 |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function wpinv_subscriptions_list() { |
||
| 105 | add_submenu_page( |
||
| 106 | 'wpinv', |
||
| 107 | __( 'Subscriptions', 'invoicing' ), |
||
| 108 | __( 'Subscriptions', 'invoicing' ), |
||
| 109 | wpinv_get_capability(), |
||
| 110 | 'wpinv-subscriptions', |
||
| 111 | 'wpinv_subscriptions_page' |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function notices() { |
||
| 148 | } |
||
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Every wpinv_action present in $_GET is called using WordPress's do_action function. |
||
| 154 | * These functions are called on init. |
||
| 155 | * |
||
| 156 | * @since 1.0.0 |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | function wpinv_get_actions() { |
||
| 160 | if ( isset( $_GET['wpinv_action'] ) ) { |
||
| 161 | do_action( 'wpinv_' . $_GET['wpinv_action'], $_GET ); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Every wpinv_action present in $_POST is called using WordPress's do_action function. |
||
| 167 | * These functions are called on init. |
||
| 168 | * |
||
| 169 | * @since 1.0.0 |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | function wpinv_post_actions() { |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get pretty subscription frequency |
||
| 180 | * |
||
| 181 | * @param $period |
||
| 182 | * @param int $frequency_count The frequency of the period. |
||
| 183 | * @return mixed|string|void |
||
| 184 | */ |
||
| 185 | public static function wpinv_get_pretty_subscription_frequency( $period, $frequency_count = 1) { |
||
| 186 | |||
| 187 | $frequency = ''; |
||
| 188 | //Format period details |
||
| 189 | switch ( strtolower( $period ) ) { |
||
| 190 | case 'day' : |
||
| 191 | case 'd' : |
||
| 192 | $frequency = sprintf( _n('Day', '%d Days', $frequency_count, 'invoicing'), $frequency_count); |
||
| 193 | break; |
||
| 194 | case 'week' : |
||
| 195 | case 'w' : |
||
| 196 | $frequency = sprintf( _n('Week', '%d Weeks', $frequency_count, 'invoicing'), $frequency_count); |
||
| 197 | break; |
||
| 198 | case 'month' : |
||
| 199 | case 'm' : |
||
| 200 | $frequency = sprintf( _n('Month', '%d Months', $frequency_count, 'invoicing'), $frequency_count); |
||
| 201 | break; |
||
| 202 | case 'year' : |
||
| 203 | case 'y' : |
||
| 204 | $frequency = sprintf( _n('Year', '%d Years', $frequency_count, 'invoicing'), $frequency_count); |
||
| 205 | break; |
||
| 206 | default : |
||
| 207 | $frequency = apply_filters( 'wpinv_recurring_subscription_frequency', $frequency, $period, $frequency_count ); |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | |||
| 211 | return $frequency; |
||
| 212 | |||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Handles cancellation requests for a subscription |
||
| 217 | * |
||
| 218 | * @access public |
||
| 219 | * @since 1.0.0 |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public function wpinv_process_cancellation( $data ) { |
||
| 223 | |||
| 224 | |||
| 225 | if( empty( $data['sub_id'] ) ) { |
||
| 226 | return; |
||
| 227 | } |
||
| 228 | |||
| 229 | if( ! is_user_logged_in() ) { |
||
| 230 | return; |
||
| 231 | } |
||
| 232 | |||
| 233 | if( ! wp_verify_nonce( $data['_wpnonce'], 'wpinv-recurring-cancel' ) ) { |
||
| 234 | wp_die( __( 'Error', 'invoicing' ), __( 'Nonce verification failed', 'invoicing' ), array( 'response' => 403 ) ); |
||
| 235 | } |
||
| 236 | |||
| 237 | $data['sub_id'] = absint( $data['sub_id'] ); |
||
| 238 | $subscription = new WPInv_Subscription( $data['sub_id'] ); |
||
| 239 | |||
| 240 | if( ! $subscription->can_cancel() ) { |
||
| 241 | wp_die( __( 'Error', 'invoicing' ), __( 'This subscription cannot be cancelled', 'invoicing' ), array( 'response' => 403 ) ); |
||
| 242 | } |
||
| 243 | |||
| 244 | try { |
||
| 245 | |||
| 246 | do_action( 'wpinv_recurring_cancel_' . $subscription->gateway . '_subscription', $subscription, true ); |
||
| 247 | |||
| 248 | $subscription->cancel(); |
||
| 249 | |||
| 250 | if( is_admin() ) { |
||
| 251 | |||
| 252 | wp_redirect( admin_url( 'admin.php?page=wpinv-subscriptions&wpinv-message=cancelled&id=' . $subscription->id ) ); |
||
| 253 | exit; |
||
| 254 | |||
| 255 | } else { |
||
| 256 | |||
| 257 | $redirect = remove_query_arg( array( '_wpnonce', 'wpinv_action', 'sub_id' ), add_query_arg( array( 'wpinv-message' => 'cancelled' ) ) ); |
||
| 258 | $redirect = apply_filters( 'wpinv_recurring_cancellation_redirect', $redirect, $subscription ); |
||
| 259 | wp_safe_redirect( $redirect ); |
||
| 260 | exit; |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | } catch ( Exception $e ) { |
||
| 265 | wp_die( __( 'Error', 'invoicing' ), $e->getMessage(), array( 'response' => 403 ) ); |
||
| 266 | } |
||
| 267 | |||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Create subscription on checkout |
||
| 272 | * |
||
| 273 | * @access public |
||
| 274 | * @param WPInv_Invoice $invoice |
||
| 275 | * @since 1.0.0 |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | public function wpinv_checkout_add_subscription( $invoice, $invoice_data ) { |
||
| 347 | } |
||
| 348 | } |
||
| 349 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.