Total Complexity | 81 |
Total Lines | 586 |
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 |
||
12 | class WPInv_Subscriptions { |
||
13 | |||
14 | /** |
||
15 | * Class constructor. |
||
16 | */ |
||
17 | public function __construct() { |
||
18 | |||
19 | // Fire gateway specific hooks when a subscription changes. |
||
20 | add_action( 'getpaid_subscription_status_changed', array( $this, 'process_subscription_status_change' ), 10, 3 ); |
||
21 | |||
22 | // De-activate a subscription whenever the invoice changes payment statuses. |
||
23 | add_action( 'getpaid_invoice_status_wpi-refunded', array( $this, 'maybe_deactivate_invoice_subscription' ), 20 ); |
||
24 | add_action( 'getpaid_invoice_status_wpi-failed', array( $this, 'maybe_deactivate_invoice_subscription' ), 20 ); |
||
25 | add_action( 'getpaid_invoice_status_wpi-cancelled', array( $this, 'maybe_deactivate_invoice_subscription' ), 20 ); |
||
26 | add_action( 'getpaid_invoice_status_wpi-pending', array( $this, 'maybe_deactivate_invoice_subscription' ), 20 ); |
||
27 | |||
28 | // Handles subscription cancelations. |
||
29 | add_action( 'getpaid_authenticated_action_subscription_cancel', array( $this, 'user_cancel_single_subscription' ) ); |
||
30 | |||
31 | // Create a subscription whenever an invoice is created, (and update it when it is updated). |
||
32 | add_action( 'wpinv_invoice_metabox_saved', array( $this, 'maybe_update_invoice_subscription' ), 5 ); |
||
33 | add_action( 'getpaid_checkout_invoice_updated', array( $this, 'maybe_update_invoice_subscription' ), 5 ); |
||
34 | |||
35 | // Handles admin subscription update actions. |
||
36 | add_action( 'getpaid_authenticated_admin_action_update_single_subscription', array( $this, 'admin_update_single_subscription' ) ); |
||
37 | add_action( 'getpaid_authenticated_admin_action_subscription_manual_renew', array( $this, 'admin_renew_single_subscription' ) ); |
||
38 | add_action( 'getpaid_authenticated_admin_action_subscription_manual_delete', array( $this, 'admin_delete_single_subscription' ) ); |
||
39 | |||
40 | // Filter invoice item row actions. |
||
41 | add_action( 'getpaid-invoice-page-line-item-actions', array( $this, 'filter_invoice_line_item_actions' ), 10, 3 ); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Returns an invoice's subscription. |
||
46 | * |
||
47 | * @param WPInv_Invoice $invoice |
||
48 | * @return WPInv_Subscription|bool |
||
49 | */ |
||
50 | public function get_invoice_subscription( $invoice ) { |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Deactivates the invoice subscription(s) whenever an invoice status changes. |
||
67 | * |
||
68 | * @param WPInv_Invoice $invoice |
||
69 | */ |
||
70 | public function maybe_deactivate_invoice_subscription( $invoice ) { |
||
71 | |||
72 | $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
||
73 | |||
74 | if ( empty( $subscriptions ) ) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | if ( ! is_array( $subscriptions ) ) { |
||
|
|||
79 | $subscriptions = array( $subscriptions ); |
||
80 | } |
||
81 | |||
82 | foreach ( $subscriptions as $subscription ) { |
||
83 | if ( $subscription->is_active() ) { |
||
84 | $subscription->set_status( 'pending' ); |
||
85 | $subscription->save(); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Processes subscription status changes. |
||
93 | * |
||
94 | * @param WPInv_Subscription $subscription |
||
95 | * @param string $from |
||
96 | * @param string $to |
||
97 | */ |
||
98 | public function process_subscription_status_change( $subscription, $from, $to ) { |
||
107 | } |
||
108 | |||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get pretty subscription frequency |
||
113 | * |
||
114 | * @param $period |
||
115 | * @param int $frequency_count The frequency of the period. |
||
116 | * @deprecated |
||
117 | * @return mixed|string|void |
||
118 | */ |
||
119 | public static function wpinv_get_pretty_subscription_frequency( $period, $frequency_count = 1 ) { |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Handles cancellation requests for a subscription |
||
125 | * |
||
126 | * @access public |
||
127 | * @since 1.0.0 |
||
128 | * @return void |
||
129 | */ |
||
130 | public function user_cancel_single_subscription( $data ) { |
||
131 | |||
132 | // Ensure there is a subscription to cancel. |
||
133 | if ( empty( $data['subscription'] ) ) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | $subscription = new WPInv_Subscription( (int) $data['subscription'] ); |
||
138 | |||
139 | // Ensure that it exists and that it belongs to the current user. |
||
140 | if ( ! $subscription->exists() || $subscription->get_customer_id() != get_current_user_id() ) { |
||
141 | $notice = 'perm_cancel_subscription'; |
||
142 | |||
143 | // Can it be cancelled. |
||
144 | } elseif ( ! $subscription->can_cancel() ) { |
||
145 | $notice = 'cannot_cancel_subscription'; |
||
146 | |||
147 | // Cancel it. |
||
148 | } else { |
||
149 | |||
150 | $subscription->cancel(); |
||
151 | $notice = 'cancelled_subscription'; |
||
152 | } |
||
153 | |||
154 | $redirect = array( |
||
155 | 'getpaid-action' => false, |
||
156 | 'getpaid-nonce' => false, |
||
157 | 'wpinv-notice' => $notice, |
||
158 | ); |
||
159 | |||
160 | wp_safe_redirect( add_query_arg( $redirect ) ); |
||
161 | exit; |
||
162 | |||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Creates a subscription(s) for an invoice. |
||
167 | * |
||
168 | * @access public |
||
169 | * @param WPInv_Invoice $invoice |
||
170 | * @since 1.0.0 |
||
171 | */ |
||
172 | public function maybe_create_invoice_subscription( $invoice ) { |
||
173 | global $getpaid_subscriptions_skip_invoice_update; |
||
174 | |||
175 | // Abort if it is not recurring. |
||
176 | if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() || $invoice->is_renewal() ) { |
||
177 | return; |
||
178 | } |
||
179 | |||
180 | // Either group the subscriptions or only process a single suscription. |
||
181 | if ( getpaid_should_group_subscriptions( $invoice ) ) { |
||
182 | |||
183 | $subscription_groups = array(); |
||
184 | $is_first = true; |
||
185 | |||
186 | foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) { |
||
187 | $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, 0, $is_first ); |
||
188 | |||
189 | if ( $is_first ) { |
||
190 | $getpaid_subscriptions_skip_invoice_update = true; |
||
191 | $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] ); |
||
192 | $invoice->save(); |
||
193 | $getpaid_subscriptions_skip_invoice_update = false; |
||
194 | } |
||
195 | |||
196 | $is_first = false; |
||
197 | } |
||
198 | |||
199 | // Cache subscription groups. |
||
200 | update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups ); |
||
201 | return true; |
||
202 | |||
203 | } |
||
204 | |||
205 | $subscription = new WPInv_Subscription(); |
||
206 | return $this->update_invoice_subscription( $subscription, $invoice ); |
||
207 | |||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Saves a new invoice subscription group. |
||
212 | * |
||
213 | * @access public |
||
214 | * @param array $totals |
||
215 | * @param WPInv_Invoice $invoice |
||
216 | * @param int $subscription_id Current subscription id of the group. |
||
217 | * @param bool $is_first Whether or not this is the first subscription group for the invoice. In which case we'll add totals of non-recurring items. |
||
218 | * |
||
219 | * @since 2.3.0 |
||
220 | */ |
||
221 | public function create_invoice_subscription_group( $totals, $invoice, $subscription_id = 0, $is_first = false ) { |
||
222 | |||
223 | $subscription = new WPInv_Subscription( (int) $subscription_id ); |
||
224 | $initial_amt = $totals['initial_total']; |
||
225 | $recurring_amt = $totals['recurring_total']; |
||
226 | $fees = array(); |
||
227 | |||
228 | // Maybe add recurring fees. |
||
229 | if ( $is_first ) { |
||
230 | |||
231 | foreach ( $invoice->get_fees() as $i => $fee ) { |
||
232 | if ( ! empty( $fee['recurring_fee'] ) ) { |
||
233 | $initial_amt += wpinv_sanitize_amount( $fee['initial_fee'] ); |
||
234 | $recurring_amt += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
||
235 | $fees[ $i ] = $fee; |
||
236 | } |
||
237 | } |
||
238 | } |
||
239 | |||
240 | $subscription->set_customer_id( $invoice->get_customer_id() ); |
||
241 | $subscription->set_parent_invoice_id( $invoice->get_id() ); |
||
242 | $subscription->set_initial_amount( $initial_amt ); |
||
243 | $subscription->set_recurring_amount( $recurring_amt ); |
||
244 | $subscription->set_date_created( current_time( 'mysql' ) ); |
||
245 | $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' ); |
||
246 | $subscription->set_product_id( $totals['item_id'] ); |
||
247 | $subscription->set_period( $totals['period'] ); |
||
248 | $subscription->set_frequency( $totals['interval'] ); |
||
249 | $subscription->set_bill_times( $totals['recurring_limit'] ); |
||
250 | $subscription->set_next_renewal_date( $totals['renews_on'] ); |
||
251 | |||
252 | // Trial periods. |
||
253 | if ( ! empty( $totals['trialling'] ) ) { |
||
254 | $subscription->set_trial_period( $totals['trialling'] ); |
||
255 | $subscription->set_status( 'trialling' ); |
||
256 | |||
257 | // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial. |
||
258 | } elseif ( empty( $initial_amt ) ) { |
||
259 | $subscription->set_trial_period( $totals['interval'] . ' ' . $totals['period'] ); |
||
260 | $subscription->set_status( 'trialling' ); |
||
261 | } |
||
262 | |||
263 | $subscription->save(); |
||
264 | |||
265 | $totals['subscription_id'] = $subscription->get_id(); |
||
266 | $totals['fees'] = $fees; |
||
267 | |||
268 | return $totals; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * (Maybe) Updates a subscription for an invoice. |
||
273 | * |
||
274 | * @access public |
||
275 | * @param WPInv_Invoice $invoice |
||
276 | * @since 1.0.19 |
||
277 | */ |
||
278 | public function maybe_update_invoice_subscription( $invoice ) { |
||
359 | |||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Deletes invoice subscription(s). |
||
364 | * |
||
365 | * @param WPInv_Invoice $invoice |
||
366 | */ |
||
367 | public function delete_invoice_subscriptions( $invoice ) { |
||
368 | |||
369 | $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
||
370 | |||
371 | if ( empty( $subscriptions ) ) { |
||
372 | return; |
||
373 | } |
||
374 | |||
375 | if ( ! is_array( $subscriptions ) ) { |
||
376 | $subscriptions = array( $subscriptions ); |
||
377 | } |
||
378 | |||
379 | foreach ( $subscriptions as $subscription ) { |
||
380 | $subscription->delete( true ); |
||
381 | } |
||
382 | |||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Updates a subscription for an invoice. |
||
387 | * |
||
388 | * @access public |
||
389 | * @param WPInv_Subscription $subscription |
||
390 | * @param WPInv_Invoice $invoice |
||
391 | * @since 1.0.19 |
||
392 | */ |
||
393 | public function update_invoice_subscription( $subscription, $invoice ) { |
||
445 | |||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Fired when an admin updates a subscription via the single subscription single page. |
||
450 | * |
||
451 | * @param array $data |
||
452 | * @since 1.0.19 |
||
453 | */ |
||
454 | public function admin_update_single_subscription( $args ) { |
||
455 | |||
456 | // Ensure the subscription exists and that a status has been given. |
||
457 | if ( empty( $args['subscription_id'] ) ) { |
||
458 | return; |
||
459 | } |
||
460 | |||
461 | // Retrieve the subscriptions. |
||
462 | $subscription = new WPInv_Subscription( $args['subscription_id'] ); |
||
463 | |||
464 | if ( $subscription->get_id() ) { |
||
465 | |||
466 | $subscription->set_props( |
||
467 | array( |
||
468 | 'status' => isset( $args['subscription_status'] ) ? $args['subscription_status'] : null, |
||
469 | 'profile_id' => isset( $args['wpinv_subscription_profile_id'] ) ? $args['wpinv_subscription_profile_id'] : null, |
||
470 | 'date_created' => ! empty( $args['wpinv_subscription_date_created'] ) ? $args['wpinv_subscription_date_created'] : null, |
||
471 | 'expiration' => ! empty( $args['wpinv_subscription_expiration'] ) ? $args['wpinv_subscription_expiration'] : null, |
||
472 | ) |
||
473 | ); |
||
474 | |||
475 | $subscription->save(); |
||
476 | getpaid_admin()->show_info( __( 'Subscription updated', 'invoicing' ) ); |
||
477 | |||
478 | do_action( 'getpaid_admin_updated_subscription', $subscription, $args ); |
||
479 | } |
||
480 | |||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Fired when an admin manually renews a subscription. |
||
485 | * |
||
486 | * @param array $data |
||
487 | * @since 1.0.19 |
||
488 | */ |
||
489 | public function admin_renew_single_subscription( $args ) { |
||
521 | |||
522 | } |
||
523 | |||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Fired when an admin manually deletes a subscription. |
||
528 | * |
||
529 | * @param array $data |
||
530 | * @since 1.0.19 |
||
531 | */ |
||
532 | public function admin_delete_single_subscription( $args ) { |
||
560 | } |
||
561 | |||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Filters the invoice line items actions. |
||
566 | * |
||
567 | * @param array actions |
||
568 | * @param WPInv_Item $item |
||
569 | * @param WPInv_Invoice $invoice |
||
570 | */ |
||
571 | public function filter_invoice_line_item_actions( $actions, $item, $invoice ) { |
||
602 |