@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | |
7 | -defined( 'ABSPATH' ) || exit; |
|
7 | +defined('ABSPATH') || exit; |
|
8 | 8 | /** |
9 | 9 | * Main Subscriptions class. |
10 | 10 | * |
@@ -17,28 +17,28 @@ discard block |
||
17 | 17 | public function __construct() { |
18 | 18 | |
19 | 19 | // Fire gateway specific hooks when a subscription changes. |
20 | - add_action( 'getpaid_subscription_status_changed', array( $this, 'process_subscription_status_change' ), 10, 3 ); |
|
20 | + add_action('getpaid_subscription_status_changed', array($this, 'process_subscription_status_change'), 10, 3); |
|
21 | 21 | |
22 | 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 ); |
|
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 | 27 | |
28 | 28 | // Handles subscription cancelations. |
29 | - add_action( 'getpaid_authenticated_action_subscription_cancel', array( $this, 'user_cancel_single_subscription' ) ); |
|
29 | + add_action('getpaid_authenticated_action_subscription_cancel', array($this, 'user_cancel_single_subscription')); |
|
30 | 30 | |
31 | 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 ); |
|
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 | 34 | |
35 | 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' ) ); |
|
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 | 39 | |
40 | 40 | // Filter invoice item row actions. |
41 | - add_action( 'getpaid-invoice-page-line-item-actions', array( $this, 'filter_invoice_line_item_actions' ), 10, 3 ); |
|
41 | + add_action('getpaid-invoice-page-line-item-actions', array($this, 'filter_invoice_line_item_actions'), 10, 3); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
@@ -47,19 +47,19 @@ discard block |
||
47 | 47 | * @param WPInv_Invoice $invoice |
48 | 48 | * @return WPInv_Subscription|bool |
49 | 49 | */ |
50 | - public function get_invoice_subscription( $invoice ) { |
|
50 | + public function get_invoice_subscription($invoice) { |
|
51 | 51 | $subscription_id = $invoice->get_subscription_id(); |
52 | 52 | |
53 | 53 | // Fallback to the parent invoice if the child invoice has no subscription id. |
54 | - if ( empty( $subscription_id ) && $invoice->is_renewal() ) { |
|
54 | + if (empty($subscription_id) && $invoice->is_renewal()) { |
|
55 | 55 | $subscription_id = $invoice->get_parent_payment()->get_subscription_id(); |
56 | 56 | } |
57 | 57 | |
58 | 58 | // Fetch the subscription. |
59 | - $subscription = new WPInv_Subscription( $subscription_id ); |
|
59 | + $subscription = new WPInv_Subscription($subscription_id); |
|
60 | 60 | |
61 | 61 | // Return subscription or use a fallback for backwards compatibility. |
62 | - return $subscription->exists() ? $subscription : wpinv_get_invoice_subscription( $invoice ); |
|
62 | + return $subscription->exists() ? $subscription : wpinv_get_invoice_subscription($invoice); |
|
63 | 63 | } |
64 | 64 | |
65 | 65 | /** |
@@ -67,21 +67,21 @@ discard block |
||
67 | 67 | * |
68 | 68 | * @param WPInv_Invoice $invoice |
69 | 69 | */ |
70 | - public function maybe_deactivate_invoice_subscription( $invoice ) { |
|
70 | + public function maybe_deactivate_invoice_subscription($invoice) { |
|
71 | 71 | |
72 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
72 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
73 | 73 | |
74 | - if ( empty( $subscriptions ) ) { |
|
74 | + if (empty($subscriptions)) { |
|
75 | 75 | return; |
76 | 76 | } |
77 | 77 | |
78 | - if ( ! is_array( $subscriptions ) ) { |
|
79 | - $subscriptions = array( $subscriptions ); |
|
78 | + if (!is_array($subscriptions)) { |
|
79 | + $subscriptions = array($subscriptions); |
|
80 | 80 | } |
81 | 81 | |
82 | - foreach ( $subscriptions as $subscription ) { |
|
83 | - if ( $subscription->is_active() ) { |
|
84 | - $subscription->set_status( 'pending' ); |
|
82 | + foreach ($subscriptions as $subscription) { |
|
83 | + if ($subscription->is_active()) { |
|
84 | + $subscription->set_status('pending'); |
|
85 | 85 | $subscription->save(); |
86 | 86 | } |
87 | 87 | } |
@@ -95,15 +95,15 @@ discard block |
||
95 | 95 | * @param string $from |
96 | 96 | * @param string $to |
97 | 97 | */ |
98 | - public function process_subscription_status_change( $subscription, $from, $to ) { |
|
98 | + public function process_subscription_status_change($subscription, $from, $to) { |
|
99 | 99 | |
100 | 100 | $gateway = $subscription->get_gateway(); |
101 | 101 | |
102 | - if ( ! empty( $gateway ) ) { |
|
103 | - $gateway = sanitize_key( $gateway ); |
|
104 | - $from = sanitize_key( $from ); |
|
105 | - $to = sanitize_key( $to ); |
|
106 | - do_action( "getpaid_{$gateway}_subscription_$to", $subscription, $from ); |
|
102 | + if (!empty($gateway)) { |
|
103 | + $gateway = sanitize_key($gateway); |
|
104 | + $from = sanitize_key($from); |
|
105 | + $to = sanitize_key($to); |
|
106 | + do_action("getpaid_{$gateway}_subscription_$to", $subscription, $from); |
|
107 | 107 | } |
108 | 108 | |
109 | 109 | } |
@@ -116,8 +116,8 @@ discard block |
||
116 | 116 | * @deprecated |
117 | 117 | * @return mixed|string|void |
118 | 118 | */ |
119 | - public static function wpinv_get_pretty_subscription_frequency( $period, $frequency_count = 1 ) { |
|
120 | - return getpaid_get_subscription_period_label( $period, $frequency_count ); |
|
119 | + public static function wpinv_get_pretty_subscription_frequency($period, $frequency_count = 1) { |
|
120 | + return getpaid_get_subscription_period_label($period, $frequency_count); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
@@ -127,33 +127,33 @@ discard block |
||
127 | 127 | * @since 1.0.0 |
128 | 128 | * @return void |
129 | 129 | */ |
130 | - public function user_cancel_single_subscription( $data ) { |
|
130 | + public function user_cancel_single_subscription($data) { |
|
131 | 131 | |
132 | 132 | // Ensure there is a subscription to cancel. |
133 | - if ( empty( $data['subscription'] ) ) { |
|
133 | + if (empty($data['subscription'])) { |
|
134 | 134 | return; |
135 | 135 | } |
136 | 136 | |
137 | - $subscription = new WPInv_Subscription( (int) $data['subscription'] ); |
|
137 | + $subscription = new WPInv_Subscription((int) $data['subscription']); |
|
138 | 138 | |
139 | 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 | - wpinv_set_error( 'invalid_subscription', __( 'You do not have permission to cancel this subscription', 'invoicing' ) ); |
|
140 | + if (!$subscription->exists() || $subscription->get_customer_id() != get_current_user_id()) { |
|
141 | + wpinv_set_error('invalid_subscription', __('You do not have permission to cancel this subscription', 'invoicing')); |
|
142 | 142 | |
143 | 143 | // Can it be cancelled. |
144 | - } elseif ( ! $subscription->can_cancel() ) { |
|
145 | - wpinv_set_error( 'cannot_cancel', __( 'This subscription cannot be cancelled as it is not active.', 'invoicing' ) ); |
|
144 | + } elseif (!$subscription->can_cancel()) { |
|
145 | + wpinv_set_error('cannot_cancel', __('This subscription cannot be cancelled as it is not active.', 'invoicing')); |
|
146 | 146 | |
147 | 147 | // Cancel it. |
148 | 148 | } else { |
149 | 149 | |
150 | 150 | $subscription->cancel(); |
151 | - wpinv_set_error( 'cancelled', __( 'This subscription has been cancelled.', 'invoicing' ), 'info' ); |
|
151 | + wpinv_set_error('cancelled', __('This subscription has been cancelled.', 'invoicing'), 'info'); |
|
152 | 152 | } |
153 | 153 | |
154 | - $redirect = remove_query_arg( array( 'getpaid-action', 'getpaid-nonce' ) ); |
|
154 | + $redirect = remove_query_arg(array('getpaid-action', 'getpaid-nonce')); |
|
155 | 155 | |
156 | - wp_safe_redirect( $redirect ); |
|
156 | + wp_safe_redirect($redirect); |
|
157 | 157 | exit; |
158 | 158 | |
159 | 159 | } |
@@ -165,41 +165,41 @@ discard block |
||
165 | 165 | * @param WPInv_Invoice $invoice |
166 | 166 | * @since 1.0.0 |
167 | 167 | */ |
168 | - public function maybe_create_invoice_subscription( $invoice ) { |
|
168 | + public function maybe_create_invoice_subscription($invoice) { |
|
169 | 169 | global $getpaid_subscriptions_skip_invoice_update; |
170 | 170 | |
171 | 171 | // Abort if it is not recurring. |
172 | - if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() || $invoice->is_renewal() ) { |
|
172 | + if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring() || $invoice->is_renewal()) { |
|
173 | 173 | return; |
174 | 174 | } |
175 | 175 | |
176 | 176 | // Either group the subscriptions or only process a single suscription. |
177 | - if ( getpaid_should_group_subscriptions( $invoice ) ) { |
|
177 | + if (getpaid_should_group_subscriptions($invoice)) { |
|
178 | 178 | |
179 | 179 | $subscription_groups = array(); |
180 | 180 | $is_first = true; |
181 | 181 | |
182 | - foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) { |
|
183 | - $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, 0, $is_first ); |
|
182 | + foreach (getpaid_calculate_subscription_totals($invoice) as $group_key => $totals) { |
|
183 | + $subscription_groups[$group_key] = $this->create_invoice_subscription_group($totals, $invoice, 0, $is_first); |
|
184 | 184 | |
185 | - if ( $is_first ) { |
|
185 | + if ($is_first) { |
|
186 | 186 | $getpaid_subscriptions_skip_invoice_update = true; |
187 | - $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] ); |
|
187 | + $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']); |
|
188 | 188 | $invoice->save(); |
189 | 189 | $getpaid_subscriptions_skip_invoice_update = false; |
190 | 190 | } |
191 | 191 | |
192 | - $is_first = false; |
|
192 | + $is_first = false; |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | // Cache subscription groups. |
196 | - update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups ); |
|
196 | + update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups); |
|
197 | 197 | return true; |
198 | 198 | |
199 | 199 | } |
200 | 200 | |
201 | 201 | $subscription = new WPInv_Subscription(); |
202 | - return $this->update_invoice_subscription( $subscription, $invoice ); |
|
202 | + return $this->update_invoice_subscription($subscription, $invoice); |
|
203 | 203 | |
204 | 204 | } |
205 | 205 | |
@@ -214,46 +214,46 @@ discard block |
||
214 | 214 | * |
215 | 215 | * @since 2.3.0 |
216 | 216 | */ |
217 | - public function create_invoice_subscription_group( $totals, $invoice, $subscription_id = 0, $is_first = false ) { |
|
217 | + public function create_invoice_subscription_group($totals, $invoice, $subscription_id = 0, $is_first = false) { |
|
218 | 218 | |
219 | - $subscription = new WPInv_Subscription( (int) $subscription_id ); |
|
219 | + $subscription = new WPInv_Subscription((int) $subscription_id); |
|
220 | 220 | $initial_amt = $totals['initial_total']; |
221 | 221 | $recurring_amt = $totals['recurring_total']; |
222 | 222 | $fees = array(); |
223 | 223 | |
224 | 224 | // Maybe add recurring fees. |
225 | - if ( $is_first ) { |
|
225 | + if ($is_first) { |
|
226 | 226 | |
227 | - foreach ( $invoice->get_fees() as $i => $fee ) { |
|
228 | - if ( ! empty( $fee['recurring_fee'] ) ) { |
|
229 | - $initial_amt += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
230 | - $recurring_amt += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
231 | - $fees[ $i ] = $fee; |
|
227 | + foreach ($invoice->get_fees() as $i => $fee) { |
|
228 | + if (!empty($fee['recurring_fee'])) { |
|
229 | + $initial_amt += wpinv_sanitize_amount($fee['initial_fee']); |
|
230 | + $recurring_amt += wpinv_sanitize_amount($fee['recurring_fee']); |
|
231 | + $fees[$i] = $fee; |
|
232 | 232 | } |
233 | 233 | } |
234 | 234 | } |
235 | 235 | |
236 | - $subscription->set_customer_id( $invoice->get_customer_id() ); |
|
237 | - $subscription->set_parent_invoice_id( $invoice->get_id() ); |
|
238 | - $subscription->set_initial_amount( $initial_amt ); |
|
239 | - $subscription->set_recurring_amount( $recurring_amt ); |
|
240 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
241 | - $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' ); |
|
242 | - $subscription->set_product_id( $totals['item_id'] ); |
|
243 | - $subscription->set_period( $totals['period'] ); |
|
244 | - $subscription->set_frequency( $totals['interval'] ); |
|
245 | - $subscription->set_bill_times( $totals['recurring_limit'] ); |
|
246 | - $subscription->set_next_renewal_date( $totals['renews_on'] ); |
|
236 | + $subscription->set_customer_id($invoice->get_customer_id()); |
|
237 | + $subscription->set_parent_invoice_id($invoice->get_id()); |
|
238 | + $subscription->set_initial_amount($initial_amt); |
|
239 | + $subscription->set_recurring_amount($recurring_amt); |
|
240 | + $subscription->set_date_created(current_time('mysql')); |
|
241 | + $subscription->set_status($invoice->is_paid() ? 'active' : 'pending'); |
|
242 | + $subscription->set_product_id($totals['item_id']); |
|
243 | + $subscription->set_period($totals['period']); |
|
244 | + $subscription->set_frequency($totals['interval']); |
|
245 | + $subscription->set_bill_times($totals['recurring_limit']); |
|
246 | + $subscription->set_next_renewal_date($totals['renews_on']); |
|
247 | 247 | |
248 | 248 | // Trial periods. |
249 | - if ( ! empty( $totals['trialling'] ) ) { |
|
250 | - $subscription->set_trial_period( $totals['trialling'] ); |
|
251 | - $subscription->set_status( 'trialling' ); |
|
249 | + if (!empty($totals['trialling'])) { |
|
250 | + $subscription->set_trial_period($totals['trialling']); |
|
251 | + $subscription->set_status('trialling'); |
|
252 | 252 | |
253 | 253 | // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial. |
254 | - } elseif ( empty( $initial_amt ) ) { |
|
255 | - $subscription->set_trial_period( $totals['interval'] . ' ' . $totals['period'] ); |
|
256 | - $subscription->set_status( 'trialling' ); |
|
254 | + } elseif (empty($initial_amt)) { |
|
255 | + $subscription->set_trial_period($totals['interval'] . ' ' . $totals['period']); |
|
256 | + $subscription->set_status('trialling'); |
|
257 | 257 | } |
258 | 258 | |
259 | 259 | $subscription->save(); |
@@ -271,86 +271,86 @@ discard block |
||
271 | 271 | * @param WPInv_Invoice $invoice |
272 | 272 | * @since 1.0.19 |
273 | 273 | */ |
274 | - public function maybe_update_invoice_subscription( $invoice ) { |
|
274 | + public function maybe_update_invoice_subscription($invoice) { |
|
275 | 275 | global $getpaid_subscriptions_skip_invoice_update; |
276 | 276 | |
277 | 277 | // Avoid infinite loops. |
278 | - if ( ! empty( $getpaid_subscriptions_skip_invoice_update ) ) { |
|
278 | + if (!empty($getpaid_subscriptions_skip_invoice_update)) { |
|
279 | 279 | return; |
280 | 280 | } |
281 | 281 | |
282 | 282 | // Do not process renewals. |
283 | - if ( $invoice->is_renewal() ) { |
|
283 | + if ($invoice->is_renewal()) { |
|
284 | 284 | return; |
285 | 285 | } |
286 | 286 | |
287 | 287 | // Delete existing subscriptions if available and the invoice is not recurring. |
288 | - if ( ! $invoice->is_recurring() ) { |
|
289 | - $this->delete_invoice_subscriptions( $invoice ); |
|
288 | + if (!$invoice->is_recurring()) { |
|
289 | + $this->delete_invoice_subscriptions($invoice); |
|
290 | 290 | return; |
291 | 291 | } |
292 | 292 | |
293 | 293 | // Fetch existing subscriptions. |
294 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
294 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
295 | 295 | |
296 | 296 | // Create new ones if no existing subscriptions. |
297 | - if ( empty( $subscriptions ) ) { |
|
298 | - return $this->maybe_create_invoice_subscription( $invoice ); |
|
297 | + if (empty($subscriptions)) { |
|
298 | + return $this->maybe_create_invoice_subscription($invoice); |
|
299 | 299 | } |
300 | 300 | |
301 | 301 | // Abort if an invoice is paid and already has a subscription. |
302 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
302 | + if ($invoice->is_paid() || $invoice->is_refunded()) { |
|
303 | 303 | return; |
304 | 304 | } |
305 | 305 | |
306 | - $is_grouped = is_array( $subscriptions ); |
|
307 | - $should_group = getpaid_should_group_subscriptions( $invoice ); |
|
306 | + $is_grouped = is_array($subscriptions); |
|
307 | + $should_group = getpaid_should_group_subscriptions($invoice); |
|
308 | 308 | |
309 | 309 | // Ensure that the subscriptions are only grouped if there are more than 1 recurring items. |
310 | - if ( $is_grouped != $should_group ) { |
|
311 | - $this->delete_invoice_subscriptions( $invoice ); |
|
312 | - delete_post_meta( $invoice->get_id(), 'getpaid_subscription_groups' ); |
|
313 | - return $this->maybe_create_invoice_subscription( $invoice ); |
|
310 | + if ($is_grouped != $should_group) { |
|
311 | + $this->delete_invoice_subscriptions($invoice); |
|
312 | + delete_post_meta($invoice->get_id(), 'getpaid_subscription_groups'); |
|
313 | + return $this->maybe_create_invoice_subscription($invoice); |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | // If there is only one recurring item... |
317 | - if ( ! $is_grouped ) { |
|
318 | - return $this->update_invoice_subscription( $subscriptions, $invoice ); |
|
317 | + if (!$is_grouped) { |
|
318 | + return $this->update_invoice_subscription($subscriptions, $invoice); |
|
319 | 319 | } |
320 | 320 | |
321 | 321 | // Process subscription groups. |
322 | - $current_groups = getpaid_get_invoice_subscription_groups( $invoice->get_id() ); |
|
322 | + $current_groups = getpaid_get_invoice_subscription_groups($invoice->get_id()); |
|
323 | 323 | $subscription_groups = array(); |
324 | 324 | $is_first = true; |
325 | 325 | |
326 | 326 | // Create new subscription groups. |
327 | - foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) { |
|
328 | - $subscription_id = isset( $current_groups[ $group_key ] ) ? $current_groups[ $group_key ]['subscription_id'] : 0; |
|
329 | - $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, $subscription_id, $is_first ); |
|
327 | + foreach (getpaid_calculate_subscription_totals($invoice) as $group_key => $totals) { |
|
328 | + $subscription_id = isset($current_groups[$group_key]) ? $current_groups[$group_key]['subscription_id'] : 0; |
|
329 | + $subscription_groups[$group_key] = $this->create_invoice_subscription_group($totals, $invoice, $subscription_id, $is_first); |
|
330 | 330 | |
331 | - if ( $is_first && $invoice->get_subscription_id() !== $subscription_groups[ $group_key ]['subscription_id'] ) { |
|
331 | + if ($is_first && $invoice->get_subscription_id() !== $subscription_groups[$group_key]['subscription_id']) { |
|
332 | 332 | $getpaid_subscriptions_skip_invoice_update = true; |
333 | - $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] ); |
|
333 | + $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']); |
|
334 | 334 | $invoice->save(); |
335 | 335 | $getpaid_subscriptions_skip_invoice_update = false; |
336 | 336 | } |
337 | 337 | |
338 | - $is_first = false; |
|
338 | + $is_first = false; |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | // Delete non-existent subscription groups. |
342 | - foreach ( $current_groups as $group_key => $data ) { |
|
343 | - if ( ! isset( $subscription_groups[ $group_key ] ) ) { |
|
344 | - $subscription = new WPInv_Subscription( (int) $data['subscription_id'] ); |
|
342 | + foreach ($current_groups as $group_key => $data) { |
|
343 | + if (!isset($subscription_groups[$group_key])) { |
|
344 | + $subscription = new WPInv_Subscription((int) $data['subscription_id']); |
|
345 | 345 | |
346 | - if ( $subscription->exists() ) { |
|
347 | - $subscription->delete( true ); |
|
346 | + if ($subscription->exists()) { |
|
347 | + $subscription->delete(true); |
|
348 | 348 | } |
349 | 349 | } |
350 | 350 | } |
351 | 351 | |
352 | 352 | // Cache subscription groups. |
353 | - update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups ); |
|
353 | + update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups); |
|
354 | 354 | return true; |
355 | 355 | |
356 | 356 | } |
@@ -360,20 +360,20 @@ discard block |
||
360 | 360 | * |
361 | 361 | * @param WPInv_Invoice $invoice |
362 | 362 | */ |
363 | - public function delete_invoice_subscriptions( $invoice ) { |
|
363 | + public function delete_invoice_subscriptions($invoice) { |
|
364 | 364 | |
365 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
365 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
366 | 366 | |
367 | - if ( empty( $subscriptions ) ) { |
|
367 | + if (empty($subscriptions)) { |
|
368 | 368 | return; |
369 | 369 | } |
370 | 370 | |
371 | - if ( ! is_array( $subscriptions ) ) { |
|
372 | - $subscriptions = array( $subscriptions ); |
|
371 | + if (!is_array($subscriptions)) { |
|
372 | + $subscriptions = array($subscriptions); |
|
373 | 373 | } |
374 | 374 | |
375 | - foreach ( $subscriptions as $subscription ) { |
|
376 | - $subscription->delete( true ); |
|
375 | + foreach ($subscriptions as $subscription) { |
|
376 | + $subscription->delete(true); |
|
377 | 377 | } |
378 | 378 | |
379 | 379 | } |
@@ -386,57 +386,57 @@ discard block |
||
386 | 386 | * @param WPInv_Invoice $invoice |
387 | 387 | * @since 1.0.19 |
388 | 388 | */ |
389 | - public function update_invoice_subscription( $subscription, $invoice ) { |
|
389 | + public function update_invoice_subscription($subscription, $invoice) { |
|
390 | 390 | |
391 | 391 | // Delete the subscription if an invoice is free or nolonger recurring. |
392 | - if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() ) { |
|
392 | + if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring()) { |
|
393 | 393 | return $subscription->delete(); |
394 | 394 | } |
395 | 395 | |
396 | - $subscription->set_customer_id( $invoice->get_customer_id() ); |
|
397 | - $subscription->set_parent_invoice_id( $invoice->get_id() ); |
|
398 | - $subscription->set_initial_amount( $invoice->get_initial_total() ); |
|
399 | - $subscription->set_recurring_amount( $invoice->get_recurring_total() ); |
|
400 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
401 | - $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' ); |
|
396 | + $subscription->set_customer_id($invoice->get_customer_id()); |
|
397 | + $subscription->set_parent_invoice_id($invoice->get_id()); |
|
398 | + $subscription->set_initial_amount($invoice->get_initial_total()); |
|
399 | + $subscription->set_recurring_amount($invoice->get_recurring_total()); |
|
400 | + $subscription->set_date_created(current_time('mysql')); |
|
401 | + $subscription->set_status($invoice->is_paid() ? 'active' : 'pending'); |
|
402 | 402 | |
403 | 403 | // Get the recurring item and abort if it does not exist. |
404 | - $subscription_item = $invoice->get_recurring( true ); |
|
405 | - if ( ! $subscription_item->get_id() ) { |
|
406 | - $invoice->set_subscription_id( 0 ); |
|
404 | + $subscription_item = $invoice->get_recurring(true); |
|
405 | + if (!$subscription_item->get_id()) { |
|
406 | + $invoice->set_subscription_id(0); |
|
407 | 407 | $invoice->save(); |
408 | 408 | return $subscription->delete(); |
409 | 409 | } |
410 | 410 | |
411 | - $subscription->set_product_id( $subscription_item->get_id() ); |
|
412 | - $subscription->set_period( $subscription_item->get_recurring_period( true ) ); |
|
413 | - $subscription->set_frequency( $subscription_item->get_recurring_interval() ); |
|
414 | - $subscription->set_bill_times( $subscription_item->get_recurring_limit() ); |
|
411 | + $subscription->set_product_id($subscription_item->get_id()); |
|
412 | + $subscription->set_period($subscription_item->get_recurring_period(true)); |
|
413 | + $subscription->set_frequency($subscription_item->get_recurring_interval()); |
|
414 | + $subscription->set_bill_times($subscription_item->get_recurring_limit()); |
|
415 | 415 | |
416 | 416 | // Calculate the next renewal date. |
417 | - $period = $subscription_item->get_recurring_period( true ); |
|
417 | + $period = $subscription_item->get_recurring_period(true); |
|
418 | 418 | $interval = $subscription_item->get_recurring_interval(); |
419 | 419 | |
420 | 420 | // If the subscription item has a trial period... |
421 | - if ( $subscription_item->has_free_trial() ) { |
|
422 | - $period = $subscription_item->get_trial_period( true ); |
|
421 | + if ($subscription_item->has_free_trial()) { |
|
422 | + $period = $subscription_item->get_trial_period(true); |
|
423 | 423 | $interval = $subscription_item->get_trial_interval(); |
424 | - $subscription->set_trial_period( $interval . ' ' . $period ); |
|
425 | - $subscription->set_status( 'trialling' ); |
|
424 | + $subscription->set_trial_period($interval . ' ' . $period); |
|
425 | + $subscription->set_status('trialling'); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial. |
429 | - if ( $invoice->has_free_trial() ) { |
|
430 | - $subscription->set_trial_period( $interval . ' ' . $period ); |
|
431 | - $subscription->set_status( 'trialling' ); |
|
429 | + if ($invoice->has_free_trial()) { |
|
430 | + $subscription->set_trial_period($interval . ' ' . $period); |
|
431 | + $subscription->set_status('trialling'); |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | // Calculate the next renewal date. |
435 | - $expiration = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", strtotime( $subscription->get_date_created() ) ) ); |
|
435 | + $expiration = date('Y-m-d H:i:s', strtotime("+$interval $period", strtotime($subscription->get_date_created()))); |
|
436 | 436 | |
437 | - $subscription->set_next_renewal_date( $expiration ); |
|
437 | + $subscription->set_next_renewal_date($expiration); |
|
438 | 438 | $subscription->save(); |
439 | - $invoice->set_subscription_id( $subscription->get_id() ); |
|
439 | + $invoice->set_subscription_id($subscription->get_id()); |
|
440 | 440 | return $subscription->get_id(); |
441 | 441 | |
442 | 442 | } |
@@ -447,27 +447,27 @@ discard block |
||
447 | 447 | * @param array $data |
448 | 448 | * @since 1.0.19 |
449 | 449 | */ |
450 | - public function admin_update_single_subscription( $args ) { |
|
450 | + public function admin_update_single_subscription($args) { |
|
451 | 451 | |
452 | 452 | // Ensure the subscription exists and that a status has been given. |
453 | - if ( empty( $args['subscription_id'] ) ) { |
|
453 | + if (empty($args['subscription_id'])) { |
|
454 | 454 | return; |
455 | 455 | } |
456 | 456 | |
457 | 457 | // Retrieve the subscriptions. |
458 | - $subscription = new WPInv_Subscription( $args['subscription_id'] ); |
|
458 | + $subscription = new WPInv_Subscription($args['subscription_id']); |
|
459 | 459 | |
460 | - if ( $subscription->get_id() ) { |
|
460 | + if ($subscription->get_id()) { |
|
461 | 461 | |
462 | 462 | $subscription->set_props( |
463 | 463 | array( |
464 | - 'status' => isset( $args['subscription_status'] ) ? $args['subscription_status'] : null, |
|
465 | - 'profile_id' => isset( $args['wpinv_subscription_profile_id'] ) ? $args['wpinv_subscription_profile_id'] : null, |
|
464 | + 'status' => isset($args['subscription_status']) ? $args['subscription_status'] : null, |
|
465 | + 'profile_id' => isset($args['wpinv_subscription_profile_id']) ? $args['wpinv_subscription_profile_id'] : null, |
|
466 | 466 | ) |
467 | 467 | ); |
468 | 468 | |
469 | 469 | $subscription->save(); |
470 | - getpaid_admin()->show_info( __( 'Subscription updated', 'invoicing' ) ); |
|
470 | + getpaid_admin()->show_info(__('Subscription updated', 'invoicing')); |
|
471 | 471 | |
472 | 472 | } |
473 | 473 | |
@@ -479,27 +479,27 @@ discard block |
||
479 | 479 | * @param array $data |
480 | 480 | * @since 1.0.19 |
481 | 481 | */ |
482 | - public function admin_renew_single_subscription( $args ) { |
|
482 | + public function admin_renew_single_subscription($args) { |
|
483 | 483 | |
484 | 484 | // Ensure the subscription exists and that a status has been given. |
485 | - if ( empty( $args['id'] ) ) { |
|
485 | + if (empty($args['id'])) { |
|
486 | 486 | return; |
487 | 487 | } |
488 | 488 | |
489 | 489 | // Retrieve the subscriptions. |
490 | - $subscription = new WPInv_Subscription( $args['id'] ); |
|
490 | + $subscription = new WPInv_Subscription($args['id']); |
|
491 | 491 | |
492 | - if ( $subscription->get_id() ) { |
|
492 | + if ($subscription->get_id()) { |
|
493 | 493 | |
494 | - do_action( 'getpaid_admin_renew_subscription', $subscription ); |
|
494 | + do_action('getpaid_admin_renew_subscription', $subscription); |
|
495 | 495 | |
496 | - $args = array( 'transaction_id', $subscription->get_parent_invoice()->generate_key( 'renewal_' ) ); |
|
496 | + $args = array('transaction_id', $subscription->get_parent_invoice()->generate_key('renewal_')); |
|
497 | 497 | |
498 | - if ( ! $subscription->add_payment( $args ) ) { |
|
499 | - getpaid_admin()->show_error( __( 'We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing' ) ); |
|
498 | + if (!$subscription->add_payment($args)) { |
|
499 | + getpaid_admin()->show_error(__('We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing')); |
|
500 | 500 | } else { |
501 | 501 | $subscription->renew(); |
502 | - getpaid_admin()->show_info( __( 'This subscription has been renewed and extended.', 'invoicing' ) ); |
|
502 | + getpaid_admin()->show_info(__('This subscription has been renewed and extended.', 'invoicing')); |
|
503 | 503 | } |
504 | 504 | |
505 | 505 | wp_safe_redirect( |
@@ -522,20 +522,20 @@ discard block |
||
522 | 522 | * @param array $data |
523 | 523 | * @since 1.0.19 |
524 | 524 | */ |
525 | - public function admin_delete_single_subscription( $args ) { |
|
525 | + public function admin_delete_single_subscription($args) { |
|
526 | 526 | |
527 | 527 | // Ensure the subscription exists and that a status has been given. |
528 | - if ( empty( $args['id'] ) ) { |
|
528 | + if (empty($args['id'])) { |
|
529 | 529 | return; |
530 | 530 | } |
531 | 531 | |
532 | 532 | // Retrieve the subscriptions. |
533 | - $subscription = new WPInv_Subscription( $args['id'] ); |
|
533 | + $subscription = new WPInv_Subscription($args['id']); |
|
534 | 534 | |
535 | - if ( $subscription->delete() ) { |
|
536 | - getpaid_admin()->show_info( __( 'This subscription has been deleted.', 'invoicing' ) ); |
|
535 | + if ($subscription->delete()) { |
|
536 | + getpaid_admin()->show_info(__('This subscription has been deleted.', 'invoicing')); |
|
537 | 537 | } else { |
538 | - getpaid_admin()->show_error( __( 'We are unable to delete this subscription. Please try again.', 'invoicing' ) ); |
|
538 | + getpaid_admin()->show_error(__('We are unable to delete this subscription. Please try again.', 'invoicing')); |
|
539 | 539 | } |
540 | 540 | |
541 | 541 | $redirected = wp_safe_redirect( |
@@ -548,7 +548,7 @@ discard block |
||
548 | 548 | ) |
549 | 549 | ); |
550 | 550 | |
551 | - if ( $redirected ) { |
|
551 | + if ($redirected) { |
|
552 | 552 | exit; |
553 | 553 | } |
554 | 554 | |
@@ -561,16 +561,16 @@ discard block |
||
561 | 561 | * @param WPInv_Item $item |
562 | 562 | * @param WPInv_Invoice $invoice |
563 | 563 | */ |
564 | - public function filter_invoice_line_item_actions( $actions, $item, $invoice ) { |
|
564 | + public function filter_invoice_line_item_actions($actions, $item, $invoice) { |
|
565 | 565 | |
566 | 566 | // Abort if this invoice uses subscription groups. |
567 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
568 | - if ( ! $invoice->is_recurring() || ! is_object( $subscriptions ) ) { |
|
567 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
568 | + if (!$invoice->is_recurring() || !is_object($subscriptions)) { |
|
569 | 569 | return $actions; |
570 | 570 | } |
571 | 571 | |
572 | 572 | // Fetch item subscription. |
573 | - $args = array( |
|
573 | + $args = array( |
|
574 | 574 | 'invoice_in' => $invoice->is_parent() ? $invoice->get_id() : $invoice->get_parent_id(), |
575 | 575 | 'product_in' => $item->get_id(), |
576 | 576 | 'number' => 1, |
@@ -578,13 +578,13 @@ discard block |
||
578 | 578 | 'fields' => 'id', |
579 | 579 | ); |
580 | 580 | |
581 | - $subscription = new GetPaid_Subscriptions_Query( $args ); |
|
581 | + $subscription = new GetPaid_Subscriptions_Query($args); |
|
582 | 582 | $subscription = $subscription->get_results(); |
583 | 583 | |
584 | 584 | // In case we found a match... |
585 | - if ( ! empty( $subscription ) ) { |
|
586 | - $url = esc_url( add_query_arg( 'subscription', (int) $subscription[0], get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
587 | - $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
585 | + if (!empty($subscription)) { |
|
586 | + $url = esc_url(add_query_arg('subscription', (int) $subscription[0], get_permalink((int) wpinv_get_option('invoice_subscription_page')))); |
|
587 | + $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __('Manage Subscription', 'invoicing') . '</a>'; |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | return $actions; |