@@ -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 | 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 ); |
|
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,21 +127,21 @@ 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() ) { |
|
140 | + if (!$subscription->exists() || $subscription->get_customer_id() != get_current_user_id()) { |
|
141 | 141 | $notice = 'perm_cancel_subscription'; |
142 | 142 | |
143 | 143 | // Can it be cancelled. |
144 | - } elseif ( ! $subscription->can_cancel() ) { |
|
144 | + } elseif (!$subscription->can_cancel()) { |
|
145 | 145 | $notice = 'cannot_cancel_subscription'; |
146 | 146 | |
147 | 147 | // Cancel it. |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | 'wpinv-notice' => $notice, |
158 | 158 | ); |
159 | 159 | |
160 | - wp_safe_redirect( add_query_arg( $redirect ) ); |
|
160 | + wp_safe_redirect(add_query_arg($redirect)); |
|
161 | 161 | exit; |
162 | 162 | |
163 | 163 | } |
@@ -169,41 +169,41 @@ discard block |
||
169 | 169 | * @param WPInv_Invoice $invoice |
170 | 170 | * @since 1.0.0 |
171 | 171 | */ |
172 | - public function maybe_create_invoice_subscription( $invoice ) { |
|
172 | + public function maybe_create_invoice_subscription($invoice) { |
|
173 | 173 | global $getpaid_subscriptions_skip_invoice_update; |
174 | 174 | |
175 | 175 | // Abort if it is not recurring. |
176 | - if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() || $invoice->is_renewal() ) { |
|
176 | + if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring() || $invoice->is_renewal()) { |
|
177 | 177 | return; |
178 | 178 | } |
179 | 179 | |
180 | 180 | // Either group the subscriptions or only process a single suscription. |
181 | - if ( getpaid_should_group_subscriptions( $invoice ) ) { |
|
181 | + if (getpaid_should_group_subscriptions($invoice)) { |
|
182 | 182 | |
183 | 183 | $subscription_groups = array(); |
184 | 184 | $is_first = true; |
185 | 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 ); |
|
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 | 188 | |
189 | - if ( $is_first ) { |
|
189 | + if ($is_first) { |
|
190 | 190 | $getpaid_subscriptions_skip_invoice_update = true; |
191 | - $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] ); |
|
191 | + $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']); |
|
192 | 192 | $invoice->save(); |
193 | 193 | $getpaid_subscriptions_skip_invoice_update = false; |
194 | 194 | } |
195 | 195 | |
196 | - $is_first = false; |
|
196 | + $is_first = false; |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | // Cache subscription groups. |
200 | - update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups ); |
|
200 | + update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups); |
|
201 | 201 | return true; |
202 | 202 | |
203 | 203 | } |
204 | 204 | |
205 | 205 | $subscription = new WPInv_Subscription(); |
206 | - return $this->update_invoice_subscription( $subscription, $invoice ); |
|
206 | + return $this->update_invoice_subscription($subscription, $invoice); |
|
207 | 207 | |
208 | 208 | } |
209 | 209 | |
@@ -218,46 +218,46 @@ discard block |
||
218 | 218 | * |
219 | 219 | * @since 2.3.0 |
220 | 220 | */ |
221 | - public function create_invoice_subscription_group( $totals, $invoice, $subscription_id = 0, $is_first = false ) { |
|
221 | + public function create_invoice_subscription_group($totals, $invoice, $subscription_id = 0, $is_first = false) { |
|
222 | 222 | |
223 | - $subscription = new WPInv_Subscription( (int) $subscription_id ); |
|
223 | + $subscription = new WPInv_Subscription((int) $subscription_id); |
|
224 | 224 | $initial_amt = $totals['initial_total']; |
225 | 225 | $recurring_amt = $totals['recurring_total']; |
226 | 226 | $fees = array(); |
227 | 227 | |
228 | 228 | // Maybe add recurring fees. |
229 | - if ( $is_first ) { |
|
229 | + if ($is_first) { |
|
230 | 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; |
|
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 | 236 | } |
237 | 237 | } |
238 | 238 | } |
239 | 239 | |
240 | - $subscription->set_customer_id( $invoice->get_user_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'] ); |
|
240 | + $subscription->set_customer_id($invoice->get_user_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 | 251 | |
252 | 252 | // Trial periods. |
253 | - if ( ! empty( $totals['trialling'] ) ) { |
|
254 | - $subscription->set_trial_period( $totals['trialling'] ); |
|
255 | - $subscription->set_status( 'trialling' ); |
|
253 | + if (!empty($totals['trialling'])) { |
|
254 | + $subscription->set_trial_period($totals['trialling']); |
|
255 | + $subscription->set_status('trialling'); |
|
256 | 256 | |
257 | 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' ); |
|
258 | + } elseif (empty($initial_amt)) { |
|
259 | + $subscription->set_trial_period($totals['interval'] . ' ' . $totals['period']); |
|
260 | + $subscription->set_status('trialling'); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | $subscription->save(); |
@@ -275,86 +275,86 @@ discard block |
||
275 | 275 | * @param WPInv_Invoice $invoice |
276 | 276 | * @since 1.0.19 |
277 | 277 | */ |
278 | - public function maybe_update_invoice_subscription( $invoice ) { |
|
278 | + public function maybe_update_invoice_subscription($invoice) { |
|
279 | 279 | global $getpaid_subscriptions_skip_invoice_update; |
280 | 280 | |
281 | 281 | // Avoid infinite loops. |
282 | - if ( ! empty( $getpaid_subscriptions_skip_invoice_update ) ) { |
|
282 | + if (!empty($getpaid_subscriptions_skip_invoice_update)) { |
|
283 | 283 | return; |
284 | 284 | } |
285 | 285 | |
286 | 286 | // Do not process renewals. |
287 | - if ( $invoice->is_renewal() ) { |
|
287 | + if ($invoice->is_renewal()) { |
|
288 | 288 | return; |
289 | 289 | } |
290 | 290 | |
291 | 291 | // Delete existing subscriptions if available and the invoice is not recurring. |
292 | - if ( ! $invoice->is_recurring() ) { |
|
293 | - $this->delete_invoice_subscriptions( $invoice ); |
|
292 | + if (!$invoice->is_recurring()) { |
|
293 | + $this->delete_invoice_subscriptions($invoice); |
|
294 | 294 | return; |
295 | 295 | } |
296 | 296 | |
297 | 297 | // Fetch existing subscriptions. |
298 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
298 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
299 | 299 | |
300 | 300 | // Create new ones if no existing subscriptions. |
301 | - if ( empty( $subscriptions ) ) { |
|
302 | - return $this->maybe_create_invoice_subscription( $invoice ); |
|
301 | + if (empty($subscriptions)) { |
|
302 | + return $this->maybe_create_invoice_subscription($invoice); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | // Abort if an invoice is paid and already has a subscription. |
306 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
306 | + if ($invoice->is_paid() || $invoice->is_refunded()) { |
|
307 | 307 | return; |
308 | 308 | } |
309 | 309 | |
310 | - $is_grouped = is_array( $subscriptions ); |
|
311 | - $should_group = getpaid_should_group_subscriptions( $invoice ); |
|
310 | + $is_grouped = is_array($subscriptions); |
|
311 | + $should_group = getpaid_should_group_subscriptions($invoice); |
|
312 | 312 | |
313 | 313 | // Ensure that the subscriptions are only grouped if there are more than 1 recurring items. |
314 | - if ( $is_grouped != $should_group ) { |
|
315 | - $this->delete_invoice_subscriptions( $invoice ); |
|
316 | - delete_post_meta( $invoice->get_id(), 'getpaid_subscription_groups' ); |
|
317 | - return $this->maybe_create_invoice_subscription( $invoice ); |
|
314 | + if ($is_grouped != $should_group) { |
|
315 | + $this->delete_invoice_subscriptions($invoice); |
|
316 | + delete_post_meta($invoice->get_id(), 'getpaid_subscription_groups'); |
|
317 | + return $this->maybe_create_invoice_subscription($invoice); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | // If there is only one recurring item... |
321 | - if ( ! $is_grouped ) { |
|
322 | - return $this->update_invoice_subscription( $subscriptions, $invoice ); |
|
321 | + if (!$is_grouped) { |
|
322 | + return $this->update_invoice_subscription($subscriptions, $invoice); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | // Process subscription groups. |
326 | - $current_groups = getpaid_get_invoice_subscription_groups( $invoice->get_id() ); |
|
326 | + $current_groups = getpaid_get_invoice_subscription_groups($invoice->get_id()); |
|
327 | 327 | $subscription_groups = array(); |
328 | 328 | $is_first = true; |
329 | 329 | |
330 | 330 | // Create new subscription groups. |
331 | - foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) { |
|
332 | - $subscription_id = isset( $current_groups[ $group_key ] ) ? $current_groups[ $group_key ]['subscription_id'] : 0; |
|
333 | - $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, $subscription_id, $is_first ); |
|
331 | + foreach (getpaid_calculate_subscription_totals($invoice) as $group_key => $totals) { |
|
332 | + $subscription_id = isset($current_groups[$group_key]) ? $current_groups[$group_key]['subscription_id'] : 0; |
|
333 | + $subscription_groups[$group_key] = $this->create_invoice_subscription_group($totals, $invoice, $subscription_id, $is_first); |
|
334 | 334 | |
335 | - if ( $is_first && $invoice->get_subscription_id() !== $subscription_groups[ $group_key ]['subscription_id'] ) { |
|
335 | + if ($is_first && $invoice->get_subscription_id() !== $subscription_groups[$group_key]['subscription_id']) { |
|
336 | 336 | $getpaid_subscriptions_skip_invoice_update = true; |
337 | - $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] ); |
|
337 | + $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']); |
|
338 | 338 | $invoice->save(); |
339 | 339 | $getpaid_subscriptions_skip_invoice_update = false; |
340 | 340 | } |
341 | 341 | |
342 | - $is_first = false; |
|
342 | + $is_first = false; |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | // Delete non-existent subscription groups. |
346 | - foreach ( $current_groups as $group_key => $data ) { |
|
347 | - if ( ! isset( $subscription_groups[ $group_key ] ) ) { |
|
348 | - $subscription = new WPInv_Subscription( (int) $data['subscription_id'] ); |
|
346 | + foreach ($current_groups as $group_key => $data) { |
|
347 | + if (!isset($subscription_groups[$group_key])) { |
|
348 | + $subscription = new WPInv_Subscription((int) $data['subscription_id']); |
|
349 | 349 | |
350 | - if ( $subscription->exists() ) { |
|
351 | - $subscription->delete( true ); |
|
350 | + if ($subscription->exists()) { |
|
351 | + $subscription->delete(true); |
|
352 | 352 | } |
353 | 353 | } |
354 | 354 | } |
355 | 355 | |
356 | 356 | // Cache subscription groups. |
357 | - update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups ); |
|
357 | + update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups); |
|
358 | 358 | return true; |
359 | 359 | |
360 | 360 | } |
@@ -364,20 +364,20 @@ discard block |
||
364 | 364 | * |
365 | 365 | * @param WPInv_Invoice $invoice |
366 | 366 | */ |
367 | - public function delete_invoice_subscriptions( $invoice ) { |
|
367 | + public function delete_invoice_subscriptions($invoice) { |
|
368 | 368 | |
369 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
369 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
370 | 370 | |
371 | - if ( empty( $subscriptions ) ) { |
|
371 | + if (empty($subscriptions)) { |
|
372 | 372 | return; |
373 | 373 | } |
374 | 374 | |
375 | - if ( ! is_array( $subscriptions ) ) { |
|
376 | - $subscriptions = array( $subscriptions ); |
|
375 | + if (!is_array($subscriptions)) { |
|
376 | + $subscriptions = array($subscriptions); |
|
377 | 377 | } |
378 | 378 | |
379 | - foreach ( $subscriptions as $subscription ) { |
|
380 | - $subscription->delete( true ); |
|
379 | + foreach ($subscriptions as $subscription) { |
|
380 | + $subscription->delete(true); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | } |
@@ -390,57 +390,57 @@ discard block |
||
390 | 390 | * @param WPInv_Invoice $invoice |
391 | 391 | * @since 1.0.19 |
392 | 392 | */ |
393 | - public function update_invoice_subscription( $subscription, $invoice ) { |
|
393 | + public function update_invoice_subscription($subscription, $invoice) { |
|
394 | 394 | |
395 | 395 | // Delete the subscription if an invoice is free or nolonger recurring. |
396 | - if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() ) { |
|
396 | + if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring()) { |
|
397 | 397 | return $subscription->delete(); |
398 | 398 | } |
399 | 399 | |
400 | - $subscription->set_customer_id( $invoice->get_user_id() ); |
|
401 | - $subscription->set_parent_invoice_id( $invoice->get_id() ); |
|
402 | - $subscription->set_initial_amount( $invoice->get_initial_total() ); |
|
403 | - $subscription->set_recurring_amount( $invoice->get_recurring_total() ); |
|
404 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
405 | - $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' ); |
|
400 | + $subscription->set_customer_id($invoice->get_user_id()); |
|
401 | + $subscription->set_parent_invoice_id($invoice->get_id()); |
|
402 | + $subscription->set_initial_amount($invoice->get_initial_total()); |
|
403 | + $subscription->set_recurring_amount($invoice->get_recurring_total()); |
|
404 | + $subscription->set_date_created(current_time('mysql')); |
|
405 | + $subscription->set_status($invoice->is_paid() ? 'active' : 'pending'); |
|
406 | 406 | |
407 | 407 | // Get the recurring item and abort if it does not exist. |
408 | - $subscription_item = $invoice->get_recurring( true ); |
|
409 | - if ( ! $subscription_item->get_id() ) { |
|
410 | - $invoice->set_subscription_id( 0 ); |
|
408 | + $subscription_item = $invoice->get_recurring(true); |
|
409 | + if (!$subscription_item->get_id()) { |
|
410 | + $invoice->set_subscription_id(0); |
|
411 | 411 | $invoice->save(); |
412 | 412 | return $subscription->delete(); |
413 | 413 | } |
414 | 414 | |
415 | - $subscription->set_product_id( $subscription_item->get_id() ); |
|
416 | - $subscription->set_period( $subscription_item->get_recurring_period( true ) ); |
|
417 | - $subscription->set_frequency( $subscription_item->get_recurring_interval() ); |
|
418 | - $subscription->set_bill_times( $subscription_item->get_recurring_limit() ); |
|
415 | + $subscription->set_product_id($subscription_item->get_id()); |
|
416 | + $subscription->set_period($subscription_item->get_recurring_period(true)); |
|
417 | + $subscription->set_frequency($subscription_item->get_recurring_interval()); |
|
418 | + $subscription->set_bill_times($subscription_item->get_recurring_limit()); |
|
419 | 419 | |
420 | 420 | // Calculate the next renewal date. |
421 | - $period = $subscription_item->get_recurring_period( true ); |
|
421 | + $period = $subscription_item->get_recurring_period(true); |
|
422 | 422 | $interval = $subscription_item->get_recurring_interval(); |
423 | 423 | |
424 | 424 | // If the subscription item has a trial period... |
425 | - if ( $subscription_item->has_free_trial() ) { |
|
426 | - $period = $subscription_item->get_trial_period( true ); |
|
425 | + if ($subscription_item->has_free_trial()) { |
|
426 | + $period = $subscription_item->get_trial_period(true); |
|
427 | 427 | $interval = $subscription_item->get_trial_interval(); |
428 | - $subscription->set_trial_period( $interval . ' ' . $period ); |
|
429 | - $subscription->set_status( 'trialling' ); |
|
428 | + $subscription->set_trial_period($interval . ' ' . $period); |
|
429 | + $subscription->set_status('trialling'); |
|
430 | 430 | } |
431 | 431 | |
432 | 432 | // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial. |
433 | - if ( $invoice->has_free_trial() ) { |
|
434 | - $subscription->set_trial_period( $interval . ' ' . $period ); |
|
435 | - $subscription->set_status( 'trialling' ); |
|
433 | + if ($invoice->has_free_trial()) { |
|
434 | + $subscription->set_trial_period($interval . ' ' . $period); |
|
435 | + $subscription->set_status('trialling'); |
|
436 | 436 | } |
437 | 437 | |
438 | 438 | // Calculate the next renewal date. |
439 | - $expiration = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", strtotime( $subscription->get_date_created() ) ) ); |
|
439 | + $expiration = date('Y-m-d H:i:s', strtotime("+$interval $period", strtotime($subscription->get_date_created()))); |
|
440 | 440 | |
441 | - $subscription->set_next_renewal_date( $expiration ); |
|
441 | + $subscription->set_next_renewal_date($expiration); |
|
442 | 442 | $subscription->save(); |
443 | - $invoice->set_subscription_id( $subscription->get_id() ); |
|
443 | + $invoice->set_subscription_id($subscription->get_id()); |
|
444 | 444 | return $subscription->get_id(); |
445 | 445 | |
446 | 446 | } |
@@ -451,34 +451,34 @@ discard block |
||
451 | 451 | * @param array $data |
452 | 452 | * @since 1.0.19 |
453 | 453 | */ |
454 | - public function admin_update_single_subscription( $args ) { |
|
454 | + public function admin_update_single_subscription($args) { |
|
455 | 455 | |
456 | 456 | // Ensure the subscription exists and that a status has been given. |
457 | - if ( empty( $args['subscription_id'] ) ) { |
|
457 | + if (empty($args['subscription_id'])) { |
|
458 | 458 | return; |
459 | 459 | } |
460 | 460 | |
461 | 461 | // Retrieve the subscriptions. |
462 | - $subscription = new WPInv_Subscription( $args['subscription_id'] ); |
|
462 | + $subscription = new WPInv_Subscription($args['subscription_id']); |
|
463 | 463 | |
464 | - if ( $subscription->get_id() ) { |
|
464 | + if ($subscription->get_id()) { |
|
465 | 465 | |
466 | 466 | $subscription->set_props( |
467 | 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 | - 'bill_times' => ! empty( $args['wpinv_subscription_max_bill_times'] ) ? $args['wpinv_subscription_max_bill_times'] : null, |
|
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 | + 'bill_times' => !empty($args['wpinv_subscription_max_bill_times']) ? $args['wpinv_subscription_max_bill_times'] : null, |
|
473 | 473 | ) |
474 | 474 | ); |
475 | 475 | |
476 | 476 | $changes = $subscription->get_changes(); |
477 | 477 | |
478 | 478 | $subscription->save(); |
479 | - getpaid_admin()->show_info( __( 'Subscription updated', 'invoicing' ) ); |
|
479 | + getpaid_admin()->show_info(__('Subscription updated', 'invoicing')); |
|
480 | 480 | |
481 | - do_action( 'getpaid_admin_updated_subscription', $subscription, $args, $changes ); |
|
481 | + do_action('getpaid_admin_updated_subscription', $subscription, $args, $changes); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | } |
@@ -489,27 +489,27 @@ discard block |
||
489 | 489 | * @param array $data |
490 | 490 | * @since 1.0.19 |
491 | 491 | */ |
492 | - public function admin_renew_single_subscription( $args ) { |
|
492 | + public function admin_renew_single_subscription($args) { |
|
493 | 493 | |
494 | 494 | // Ensure the subscription exists and that a status has been given. |
495 | - if ( empty( $args['id'] ) ) { |
|
495 | + if (empty($args['id'])) { |
|
496 | 496 | return; |
497 | 497 | } |
498 | 498 | |
499 | 499 | // Retrieve the subscriptions. |
500 | - $subscription = new WPInv_Subscription( $args['id'] ); |
|
500 | + $subscription = new WPInv_Subscription($args['id']); |
|
501 | 501 | |
502 | - if ( $subscription->get_id() ) { |
|
502 | + if ($subscription->get_id()) { |
|
503 | 503 | |
504 | - do_action( 'getpaid_admin_renew_subscription', $subscription ); |
|
504 | + do_action('getpaid_admin_renew_subscription', $subscription); |
|
505 | 505 | |
506 | - $args = array( 'transaction_id', $subscription->get_parent_invoice()->generate_key( 'renewal_' ) ); |
|
506 | + $args = array('transaction_id', $subscription->get_parent_invoice()->generate_key('renewal_')); |
|
507 | 507 | |
508 | - if ( ! $subscription->add_payment( $args ) ) { |
|
509 | - getpaid_admin()->show_error( __( 'We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing' ) ); |
|
508 | + if (!$subscription->add_payment($args)) { |
|
509 | + getpaid_admin()->show_error(__('We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing')); |
|
510 | 510 | } else { |
511 | 511 | $subscription->renew(); |
512 | - getpaid_admin()->show_info( __( 'This subscription has been renewed and extended.', 'invoicing' ) ); |
|
512 | + getpaid_admin()->show_info(__('This subscription has been renewed and extended.', 'invoicing')); |
|
513 | 513 | } |
514 | 514 | |
515 | 515 | wp_safe_redirect( |
@@ -532,20 +532,20 @@ discard block |
||
532 | 532 | * @param array $data |
533 | 533 | * @since 1.0.19 |
534 | 534 | */ |
535 | - public function admin_delete_single_subscription( $args ) { |
|
535 | + public function admin_delete_single_subscription($args) { |
|
536 | 536 | |
537 | 537 | // Ensure the subscription exists and that a status has been given. |
538 | - if ( empty( $args['id'] ) ) { |
|
538 | + if (empty($args['id'])) { |
|
539 | 539 | return; |
540 | 540 | } |
541 | 541 | |
542 | 542 | // Retrieve the subscriptions. |
543 | - $subscription = new WPInv_Subscription( $args['id'] ); |
|
543 | + $subscription = new WPInv_Subscription($args['id']); |
|
544 | 544 | |
545 | - if ( $subscription->delete() ) { |
|
546 | - getpaid_admin()->show_info( __( 'This subscription has been deleted.', 'invoicing' ) ); |
|
545 | + if ($subscription->delete()) { |
|
546 | + getpaid_admin()->show_info(__('This subscription has been deleted.', 'invoicing')); |
|
547 | 547 | } else { |
548 | - getpaid_admin()->show_error( __( 'We are unable to delete this subscription. Please try again.', 'invoicing' ) ); |
|
548 | + getpaid_admin()->show_error(__('We are unable to delete this subscription. Please try again.', 'invoicing')); |
|
549 | 549 | } |
550 | 550 | |
551 | 551 | $redirected = wp_safe_redirect( |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | ) |
559 | 559 | ); |
560 | 560 | |
561 | - if ( $redirected ) { |
|
561 | + if ($redirected) { |
|
562 | 562 | exit; |
563 | 563 | } |
564 | 564 | |
@@ -571,16 +571,16 @@ discard block |
||
571 | 571 | * @param WPInv_Item $item |
572 | 572 | * @param WPInv_Invoice $invoice |
573 | 573 | */ |
574 | - public function filter_invoice_line_item_actions( $actions, $item, $invoice ) { |
|
574 | + public function filter_invoice_line_item_actions($actions, $item, $invoice) { |
|
575 | 575 | |
576 | 576 | // Abort if this invoice uses subscription groups. |
577 | - $subscriptions = getpaid_get_invoice_subscriptions( $invoice ); |
|
578 | - if ( ! $invoice->is_recurring() || ! is_object( $subscriptions ) ) { |
|
577 | + $subscriptions = getpaid_get_invoice_subscriptions($invoice); |
|
578 | + if (!$invoice->is_recurring() || !is_object($subscriptions)) { |
|
579 | 579 | return $actions; |
580 | 580 | } |
581 | 581 | |
582 | 582 | // Fetch item subscription. |
583 | - $args = array( |
|
583 | + $args = array( |
|
584 | 584 | 'invoice_in' => $invoice->is_parent() ? $invoice->get_id() : $invoice->get_parent_id(), |
585 | 585 | 'product_in' => $item->get_id(), |
586 | 586 | 'number' => 1, |
@@ -588,13 +588,13 @@ discard block |
||
588 | 588 | 'fields' => 'id', |
589 | 589 | ); |
590 | 590 | |
591 | - $subscription = new GetPaid_Subscriptions_Query( $args ); |
|
591 | + $subscription = new GetPaid_Subscriptions_Query($args); |
|
592 | 592 | $subscription = $subscription->get_results(); |
593 | 593 | |
594 | 594 | // In case we found a match... |
595 | - if ( ! empty( $subscription ) ) { |
|
596 | - $url = esc_url( add_query_arg( 'subscription', (int) $subscription[0], get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) ); |
|
597 | - $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>'; |
|
595 | + if (!empty($subscription)) { |
|
596 | + $url = esc_url(add_query_arg('subscription', (int) $subscription[0], get_permalink((int) wpinv_get_option('invoice_subscription_page')))); |
|
597 | + $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __('Manage Subscription', 'invoicing') . '</a>'; |
|
598 | 598 | } |
599 | 599 | |
600 | 600 | return $actions; |
@@ -19,16 +19,16 @@ discard block |
||
19 | 19 | |
20 | 20 | // Define constants. |
21 | 21 | if ( ! defined( 'WPINV_PLUGIN_FILE' ) ) { |
22 | - define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
22 | + define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | if ( ! defined( 'WPINV_VERSION' ) ) { |
26 | - define( 'WPINV_VERSION', '2.8.1' ); |
|
26 | + define( 'WPINV_VERSION', '2.8.1' ); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | // Include the main Invoicing class. |
30 | 30 | if ( ! class_exists( 'WPInv_Plugin', false ) ) { |
31 | - require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
31 | + require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | $GLOBALS['invoicing'] = new WPInv_Plugin(); |
44 | 44 | } |
45 | 45 | |
46 | - return $GLOBALS['invoicing']; |
|
46 | + return $GLOBALS['invoicing']; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -15,20 +15,20 @@ discard block |
||
15 | 15 | * @package GetPaid |
16 | 16 | */ |
17 | 17 | |
18 | -defined( 'ABSPATH' ) || exit; |
|
18 | +defined('ABSPATH') || exit; |
|
19 | 19 | |
20 | 20 | // Define constants. |
21 | -if ( ! defined( 'WPINV_PLUGIN_FILE' ) ) { |
|
22 | - define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
21 | +if (!defined('WPINV_PLUGIN_FILE')) { |
|
22 | + define('WPINV_PLUGIN_FILE', __FILE__); |
|
23 | 23 | } |
24 | 24 | |
25 | -if ( ! defined( 'WPINV_VERSION' ) ) { |
|
26 | - define( 'WPINV_VERSION', '2.8.1' ); |
|
25 | +if (!defined('WPINV_VERSION')) { |
|
26 | + define('WPINV_VERSION', '2.8.1'); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | // Include the main Invoicing class. |
30 | -if ( ! class_exists( 'WPInv_Plugin', false ) ) { |
|
31 | - require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
30 | +if (!class_exists('WPInv_Plugin', false)) { |
|
31 | + require_once plugin_dir_path(WPINV_PLUGIN_FILE) . 'includes/class-wpinv.php'; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | */ |
40 | 40 | function getpaid() { |
41 | 41 | |
42 | - if ( empty( $GLOBALS['invoicing'] ) ) { |
|
42 | + if (empty($GLOBALS['invoicing'])) { |
|
43 | 43 | $GLOBALS['invoicing'] = new WPInv_Plugin(); |
44 | 44 | } |
45 | 45 | |
@@ -52,9 +52,9 @@ discard block |
||
52 | 52 | * @since 2.0.8 |
53 | 53 | */ |
54 | 54 | function getpaid_deactivation_hook() { |
55 | - update_option( 'wpinv_flush_permalinks', 1 ); |
|
55 | + update_option('wpinv_flush_permalinks', 1); |
|
56 | 56 | } |
57 | -register_deactivation_hook( __FILE__, 'getpaid_deactivation_hook' ); |
|
57 | +register_deactivation_hook(__FILE__, 'getpaid_deactivation_hook'); |
|
58 | 58 | |
59 | 59 | /** |
60 | 60 | * @deprecated |
@@ -64,4 +64,4 @@ discard block |
||
64 | 64 | } |
65 | 65 | |
66 | 66 | // Kickstart the plugin. |
67 | -add_action( 'plugins_loaded', 'getpaid', -100 ); |
|
67 | +add_action('plugins_loaded', 'getpaid', -100); |