Passed
Push — master ( 3873c7...dd948d )
by Kiran
14:02 queued 08:35
created
includes/class-wpinv-subscriptions.php 1 patch
Spacing   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -4,7 +4,7 @@  discard block
 block discarded – undo
4 4
  *
5 5
  */
6 6
 
7
-defined( 'ABSPATH' ) || exit;
7
+defined('ABSPATH') || exit;
8 8
 /**
9 9
  * Main Subscriptions class.
10 10
  *
@@ -17,25 +17,25 @@  discard block
 block discarded – undo
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_changed', array( $this, 'maybe_deactivate_invoice_subscription' ), 20, 1 );
23
+        add_action('getpaid_invoice_status_changed', array($this, 'maybe_deactivate_invoice_subscription'), 20, 1);
24 24
 
25 25
         // Handles subscription cancelations.
26
-        add_action( 'getpaid_authenticated_action_subscription_cancel', array( $this, 'user_cancel_single_subscription' ) );
26
+        add_action('getpaid_authenticated_action_subscription_cancel', array($this, 'user_cancel_single_subscription'));
27 27
 
28 28
         // Create a subscription whenever an invoice is created, (and update it when it is updated).
29
-        add_action( 'wpinv_invoice_metabox_saved', array( $this, 'maybe_update_invoice_subscription' ), 5 );
30
-        add_action( 'getpaid_checkout_invoice_updated', array( $this, 'maybe_update_invoice_subscription' ), 5 );
29
+        add_action('wpinv_invoice_metabox_saved', array($this, 'maybe_update_invoice_subscription'), 5);
30
+        add_action('getpaid_checkout_invoice_updated', array($this, 'maybe_update_invoice_subscription'), 5);
31 31
 
32 32
         // Handles admin subscription update actions.
33
-        add_action( 'getpaid_authenticated_admin_action_update_single_subscription', array( $this, 'admin_update_single_subscription' ) );
34
-        add_action( 'getpaid_authenticated_admin_action_subscription_manual_renew', array( $this, 'admin_renew_single_subscription' ) );
35
-        add_action( 'getpaid_authenticated_admin_action_subscription_manual_delete', array( $this, 'admin_delete_single_subscription' ) );
33
+        add_action('getpaid_authenticated_admin_action_update_single_subscription', array($this, 'admin_update_single_subscription'));
34
+        add_action('getpaid_authenticated_admin_action_subscription_manual_renew', array($this, 'admin_renew_single_subscription'));
35
+        add_action('getpaid_authenticated_admin_action_subscription_manual_delete', array($this, 'admin_delete_single_subscription'));
36 36
 
37 37
         // Filter invoice item row actions.
38
-        add_action( 'getpaid-invoice-page-line-item-actions', array( $this, 'filter_invoice_line_item_actions' ), 10, 3 );
38
+        add_action('getpaid-invoice-page-line-item-actions', array($this, 'filter_invoice_line_item_actions'), 10, 3);
39 39
     }
40 40
 
41 41
     /**
@@ -44,19 +44,19 @@  discard block
 block discarded – undo
44 44
      * @param WPInv_Invoice $invoice
45 45
      * @return WPInv_Subscription|bool
46 46
      */
47
-    public function get_invoice_subscription( $invoice ) {
47
+    public function get_invoice_subscription($invoice) {
48 48
         $subscription_id = $invoice->get_subscription_id();
49 49
 
50 50
         // Fallback to the parent invoice if the child invoice has no subscription id.
51
-        if ( empty( $subscription_id ) && $invoice->is_renewal() ) {
51
+        if (empty($subscription_id) && $invoice->is_renewal()) {
52 52
             $subscription_id = $invoice->get_parent_payment()->get_subscription_id();
53 53
         }
54 54
 
55 55
         // Fetch the subscription.
56
-        $subscription = new WPInv_Subscription( $subscription_id );
56
+        $subscription = new WPInv_Subscription($subscription_id);
57 57
 
58 58
         // Return subscription or use a fallback for backwards compatibility.
59
-        return $subscription->exists() ? $subscription : wpinv_get_invoice_subscription( $invoice );
59
+        return $subscription->exists() ? $subscription : wpinv_get_invoice_subscription($invoice);
60 60
     }
61 61
 
62 62
     /**
@@ -65,7 +65,7 @@  discard block
 block discarded – undo
65 65
      * @param string $invoice_status
66 66
      * @return string
67 67
      */
68
-    protected function get_subscription_status_from_invoice_status( $invoice_status ) {
68
+    protected function get_subscription_status_from_invoice_status($invoice_status) {
69 69
         $status_mapping = array(
70 70
             'draft'            => 'pending',
71 71
             'wpi-failed'       => 'failing',
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
             'wpi-refunded'     => 'cancelled',
79 79
         );
80 80
 
81
-        return isset( $status_mapping[ $invoice_status ] ) ? $status_mapping[ $invoice_status ] : 'pending';
81
+        return isset($status_mapping[$invoice_status]) ? $status_mapping[$invoice_status] : 'pending';
82 82
     }
83 83
 
84 84
     /**
@@ -86,22 +86,22 @@  discard block
 block discarded – undo
86 86
      *
87 87
      * @param WPInv_Invoice $invoice
88 88
      */
89
-    public function maybe_deactivate_invoice_subscription( $invoice ) {
89
+    public function maybe_deactivate_invoice_subscription($invoice) {
90 90
 
91
-        $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
91
+        $subscriptions = getpaid_get_invoice_subscriptions($invoice);
92 92
 
93
-        if ( empty( $subscriptions ) ) {
93
+        if (empty($subscriptions)) {
94 94
             return;
95 95
         }
96 96
 
97
-        if ( ! is_array( $subscriptions ) ) {
98
-            $subscriptions = array( $subscriptions );
97
+        if (!is_array($subscriptions)) {
98
+            $subscriptions = array($subscriptions);
99 99
         }
100 100
 
101
-        $new_status = $this->get_subscription_status_from_invoice_status( $invoice->get_status() );
101
+        $new_status = $this->get_subscription_status_from_invoice_status($invoice->get_status());
102 102
 
103
-        foreach ( $subscriptions as $subscription ) {
104
-            $subscription->set_status( $new_status );
103
+        foreach ($subscriptions as $subscription) {
104
+            $subscription->set_status($new_status);
105 105
             $subscription->save();
106 106
         }
107 107
 
@@ -114,15 +114,15 @@  discard block
 block discarded – undo
114 114
      * @param string $from
115 115
      * @param string $to
116 116
 	 */
117
-    public function process_subscription_status_change( $subscription, $from, $to ) {
117
+    public function process_subscription_status_change($subscription, $from, $to) {
118 118
 
119 119
         $gateway = $subscription->get_gateway();
120 120
 
121
-        if ( ! empty( $gateway ) ) {
122
-            $gateway = sanitize_key( $gateway );
123
-            $from    = sanitize_key( $from );
124
-            $to      = sanitize_key( $to );
125
-            do_action( "getpaid_{$gateway}_subscription_$to", $subscription, $from );
121
+        if (!empty($gateway)) {
122
+            $gateway = sanitize_key($gateway);
123
+            $from    = sanitize_key($from);
124
+            $to      = sanitize_key($to);
125
+            do_action("getpaid_{$gateway}_subscription_$to", $subscription, $from);
126 126
         }
127 127
 
128 128
     }
@@ -135,8 +135,8 @@  discard block
 block discarded – undo
135 135
      * @deprecated
136 136
      * @return mixed|string|void
137 137
      */
138
-    public static function wpinv_get_pretty_subscription_frequency( $period, $frequency_count = 1 ) {
139
-        return getpaid_get_subscription_period_label( $period, $frequency_count );
138
+    public static function wpinv_get_pretty_subscription_frequency($period, $frequency_count = 1) {
139
+        return getpaid_get_subscription_period_label($period, $frequency_count);
140 140
     }
141 141
 
142 142
     /**
@@ -146,21 +146,21 @@  discard block
 block discarded – undo
146 146
      * @since       1.0.0
147 147
      * @return      void
148 148
      */
149
-    public function user_cancel_single_subscription( $data ) {
149
+    public function user_cancel_single_subscription($data) {
150 150
 
151 151
         // Ensure there is a subscription to cancel.
152
-        if ( empty( $data['subscription'] ) ) {
152
+        if (empty($data['subscription'])) {
153 153
             return;
154 154
         }
155 155
 
156
-        $subscription = new WPInv_Subscription( (int) $data['subscription'] );
156
+        $subscription = new WPInv_Subscription((int) $data['subscription']);
157 157
 
158 158
         // Ensure that it exists and that it belongs to the current user.
159
-        if ( ! $subscription->exists() || $subscription->get_customer_id() != get_current_user_id() ) {
159
+        if (!$subscription->exists() || $subscription->get_customer_id() != get_current_user_id()) {
160 160
             $notice = 'perm_cancel_subscription';
161 161
 
162 162
         // Can it be cancelled.
163
-        } elseif ( ! $subscription->can_cancel() ) {
163
+        } elseif (!$subscription->can_cancel()) {
164 164
             $notice = 'cannot_cancel_subscription';
165 165
 
166 166
         // Cancel it.
@@ -176,7 +176,7 @@  discard block
 block discarded – undo
176 176
             'wpinv-notice'   => $notice,
177 177
         );
178 178
 
179
-        wp_safe_redirect( add_query_arg( $redirect ) );
179
+        wp_safe_redirect(add_query_arg($redirect));
180 180
         exit;
181 181
 
182 182
     }
@@ -188,41 +188,41 @@  discard block
 block discarded – undo
188 188
      * @param       WPInv_Invoice $invoice
189 189
      * @since       1.0.0
190 190
      */
191
-    public function maybe_create_invoice_subscription( $invoice ) {
191
+    public function maybe_create_invoice_subscription($invoice) {
192 192
         global $getpaid_subscriptions_skip_invoice_update;
193 193
 
194 194
         // Abort if it is not recurring.
195
-        if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() || $invoice->is_renewal() ) {
195
+        if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring() || $invoice->is_renewal()) {
196 196
             return;
197 197
         }
198 198
 
199 199
         // Either group the subscriptions or only process a single suscription.
200
-        if ( getpaid_should_group_subscriptions( $invoice ) ) {
200
+        if (getpaid_should_group_subscriptions($invoice)) {
201 201
 
202 202
             $subscription_groups = array();
203 203
             $is_first            = true;
204 204
 
205
-            foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) {
206
-                $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, 0, $is_first );
205
+            foreach (getpaid_calculate_subscription_totals($invoice) as $group_key => $totals) {
206
+                $subscription_groups[$group_key] = $this->create_invoice_subscription_group($totals, $invoice, 0, $is_first);
207 207
 
208
-                if ( $is_first ) {
208
+                if ($is_first) {
209 209
                     $getpaid_subscriptions_skip_invoice_update = true;
210
-                    $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] );
210
+                    $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']);
211 211
                     $invoice->save();
212 212
                     $getpaid_subscriptions_skip_invoice_update = false;
213 213
                 }
214 214
 
215
-                $is_first                          = false;
215
+                $is_first = false;
216 216
             }
217 217
 
218 218
             // Cache subscription groups.
219
-            update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups );
219
+            update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups);
220 220
             return true;
221 221
 
222 222
         }
223 223
 
224 224
         $subscription = new WPInv_Subscription();
225
-        return $this->update_invoice_subscription( $subscription, $invoice );
225
+        return $this->update_invoice_subscription($subscription, $invoice);
226 226
 
227 227
     }
228 228
 
@@ -237,46 +237,46 @@  discard block
 block discarded – undo
237 237
      *
238 238
      * @since       2.3.0
239 239
      */
240
-    public function create_invoice_subscription_group( $totals, $invoice, $subscription_id = 0, $is_first = false ) {
240
+    public function create_invoice_subscription_group($totals, $invoice, $subscription_id = 0, $is_first = false) {
241 241
 
242
-        $subscription  = new WPInv_Subscription( (int) $subscription_id );
242
+        $subscription  = new WPInv_Subscription((int) $subscription_id);
243 243
         $initial_amt   = $totals['initial_total'];
244 244
         $recurring_amt = $totals['recurring_total'];
245 245
         $fees          = array();
246 246
 
247 247
         // Maybe add recurring fees.
248
-        if ( $is_first ) {
248
+        if ($is_first) {
249 249
 
250
-            foreach ( $invoice->get_fees() as $i => $fee ) {
251
-                if ( ! empty( $fee['recurring_fee'] ) ) {
252
-                    $initial_amt   += wpinv_sanitize_amount( $fee['initial_fee'] );
253
-                    $recurring_amt += wpinv_sanitize_amount( $fee['recurring_fee'] );
254
-                    $fees[ $i ]       = $fee;
250
+            foreach ($invoice->get_fees() as $i => $fee) {
251
+                if (!empty($fee['recurring_fee'])) {
252
+                    $initial_amt   += wpinv_sanitize_amount($fee['initial_fee']);
253
+                    $recurring_amt += wpinv_sanitize_amount($fee['recurring_fee']);
254
+                    $fees[$i] = $fee;
255 255
                 }
256 256
             }
257 257
         }
258 258
 
259
-        $subscription->set_customer_id( $invoice->get_user_id() );
260
-        $subscription->set_parent_invoice_id( $invoice->get_id() );
261
-        $subscription->set_initial_amount( $initial_amt );
262
-        $subscription->set_recurring_amount( $recurring_amt );
263
-        $subscription->set_date_created( current_time( 'mysql' ) );
264
-        $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' );
265
-        $subscription->set_product_id( $totals['item_id'] );
266
-        $subscription->set_period( $totals['period'] );
267
-        $subscription->set_frequency( $totals['interval'] );
268
-        $subscription->set_bill_times( $totals['recurring_limit'] );
269
-        $subscription->set_next_renewal_date( $totals['renews_on'] );
259
+        $subscription->set_customer_id($invoice->get_user_id());
260
+        $subscription->set_parent_invoice_id($invoice->get_id());
261
+        $subscription->set_initial_amount($initial_amt);
262
+        $subscription->set_recurring_amount($recurring_amt);
263
+        $subscription->set_date_created(current_time('mysql'));
264
+        $subscription->set_status($invoice->is_paid() ? 'active' : 'pending');
265
+        $subscription->set_product_id($totals['item_id']);
266
+        $subscription->set_period($totals['period']);
267
+        $subscription->set_frequency($totals['interval']);
268
+        $subscription->set_bill_times($totals['recurring_limit']);
269
+        $subscription->set_next_renewal_date($totals['renews_on']);
270 270
 
271 271
         // Trial periods.
272
-        if ( ! empty( $totals['trialling'] ) ) {
273
-            $subscription->set_trial_period( $totals['trialling'] );
274
-            $subscription->set_status( 'trialling' );
272
+        if (!empty($totals['trialling'])) {
273
+            $subscription->set_trial_period($totals['trialling']);
274
+            $subscription->set_status('trialling');
275 275
 
276 276
         // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial.
277
-        } elseif ( empty( $initial_amt ) ) {
278
-            $subscription->set_trial_period( $totals['interval'] . ' ' . $totals['period'] );
279
-            $subscription->set_status( 'trialling' );
277
+        } elseif (empty($initial_amt)) {
278
+            $subscription->set_trial_period($totals['interval'] . ' ' . $totals['period']);
279
+            $subscription->set_status('trialling');
280 280
         }
281 281
 
282 282
         $subscription->save();
@@ -294,86 +294,86 @@  discard block
 block discarded – undo
294 294
      * @param       WPInv_Invoice $invoice
295 295
      * @since       1.0.19
296 296
      */
297
-    public function maybe_update_invoice_subscription( $invoice ) {
297
+    public function maybe_update_invoice_subscription($invoice) {
298 298
         global $getpaid_subscriptions_skip_invoice_update;
299 299
 
300 300
         // Avoid infinite loops.
301
-        if ( ! empty( $getpaid_subscriptions_skip_invoice_update ) ) {
301
+        if (!empty($getpaid_subscriptions_skip_invoice_update)) {
302 302
             return;
303 303
         }
304 304
 
305 305
         // Do not process renewals.
306
-        if ( $invoice->is_renewal() ) {
306
+        if ($invoice->is_renewal()) {
307 307
             return;
308 308
         }
309 309
 
310 310
         // Delete existing subscriptions if available and the invoice is not recurring.
311
-        if ( ! $invoice->is_recurring() ) {
312
-            $this->delete_invoice_subscriptions( $invoice );
311
+        if (!$invoice->is_recurring()) {
312
+            $this->delete_invoice_subscriptions($invoice);
313 313
             return;
314 314
         }
315 315
 
316 316
         // Fetch existing subscriptions.
317
-        $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
317
+        $subscriptions = getpaid_get_invoice_subscriptions($invoice);
318 318
 
319 319
         // Create new ones if no existing subscriptions.
320
-        if ( empty( $subscriptions ) ) {
321
-            return $this->maybe_create_invoice_subscription( $invoice );
320
+        if (empty($subscriptions)) {
321
+            return $this->maybe_create_invoice_subscription($invoice);
322 322
         }
323 323
 
324 324
         // Abort if an invoice is paid and already has a subscription.
325
-        if ( $invoice->is_paid() || $invoice->is_refunded() ) {
325
+        if ($invoice->is_paid() || $invoice->is_refunded()) {
326 326
             return;
327 327
         }
328 328
 
329
-        $is_grouped   = is_array( $subscriptions );
330
-        $should_group = getpaid_should_group_subscriptions( $invoice );
329
+        $is_grouped   = is_array($subscriptions);
330
+        $should_group = getpaid_should_group_subscriptions($invoice);
331 331
 
332 332
         // Ensure that the subscriptions are only grouped if there are more than 1 recurring items.
333
-        if ( $is_grouped != $should_group ) {
334
-            $this->delete_invoice_subscriptions( $invoice );
335
-            delete_post_meta( $invoice->get_id(), 'getpaid_subscription_groups' );
336
-            return $this->maybe_create_invoice_subscription( $invoice );
333
+        if ($is_grouped != $should_group) {
334
+            $this->delete_invoice_subscriptions($invoice);
335
+            delete_post_meta($invoice->get_id(), 'getpaid_subscription_groups');
336
+            return $this->maybe_create_invoice_subscription($invoice);
337 337
         }
338 338
 
339 339
         // If there is only one recurring item...
340
-        if ( ! $is_grouped ) {
341
-            return $this->update_invoice_subscription( $subscriptions, $invoice );
340
+        if (!$is_grouped) {
341
+            return $this->update_invoice_subscription($subscriptions, $invoice);
342 342
         }
343 343
 
344 344
         // Process subscription groups.
345
-        $current_groups      = getpaid_get_invoice_subscription_groups( $invoice->get_id() );
345
+        $current_groups      = getpaid_get_invoice_subscription_groups($invoice->get_id());
346 346
         $subscription_groups = array();
347 347
         $is_first            = true;
348 348
 
349 349
         // Create new subscription groups.
350
-        foreach ( getpaid_calculate_subscription_totals( $invoice ) as $group_key => $totals ) {
351
-            $subscription_id                   = isset( $current_groups[ $group_key ] ) ? $current_groups[ $group_key ]['subscription_id'] : 0;
352
-            $subscription_groups[ $group_key ] = $this->create_invoice_subscription_group( $totals, $invoice, $subscription_id, $is_first );
350
+        foreach (getpaid_calculate_subscription_totals($invoice) as $group_key => $totals) {
351
+            $subscription_id                   = isset($current_groups[$group_key]) ? $current_groups[$group_key]['subscription_id'] : 0;
352
+            $subscription_groups[$group_key] = $this->create_invoice_subscription_group($totals, $invoice, $subscription_id, $is_first);
353 353
 
354
-            if ( $is_first && $invoice->get_subscription_id() !== $subscription_groups[ $group_key ]['subscription_id'] ) {
354
+            if ($is_first && $invoice->get_subscription_id() !== $subscription_groups[$group_key]['subscription_id']) {
355 355
                 $getpaid_subscriptions_skip_invoice_update = true;
356
-                $invoice->set_subscription_id( $subscription_groups[ $group_key ]['subscription_id'] );
356
+                $invoice->set_subscription_id($subscription_groups[$group_key]['subscription_id']);
357 357
                 $invoice->save();
358 358
                 $getpaid_subscriptions_skip_invoice_update = false;
359 359
             }
360 360
 
361
-            $is_first                          = false;
361
+            $is_first = false;
362 362
         }
363 363
 
364 364
         // Delete non-existent subscription groups.
365
-        foreach ( $current_groups as $group_key => $data ) {
366
-            if ( ! isset( $subscription_groups[ $group_key ] ) ) {
367
-                $subscription = new WPInv_Subscription( (int) $data['subscription_id'] );
365
+        foreach ($current_groups as $group_key => $data) {
366
+            if (!isset($subscription_groups[$group_key])) {
367
+                $subscription = new WPInv_Subscription((int) $data['subscription_id']);
368 368
 
369
-                if ( $subscription->exists() ) {
370
-                    $subscription->delete( true );
369
+                if ($subscription->exists()) {
370
+                    $subscription->delete(true);
371 371
                 }
372 372
 }
373 373
         }
374 374
 
375 375
         // Cache subscription groups.
376
-        update_post_meta( $invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups );
376
+        update_post_meta($invoice->get_id(), 'getpaid_subscription_groups', $subscription_groups);
377 377
         return true;
378 378
 
379 379
     }
@@ -383,20 +383,20 @@  discard block
 block discarded – undo
383 383
      *
384 384
      * @param WPInv_Invoice $invoice
385 385
      */
386
-    public function delete_invoice_subscriptions( $invoice ) {
386
+    public function delete_invoice_subscriptions($invoice) {
387 387
 
388
-        $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
388
+        $subscriptions = getpaid_get_invoice_subscriptions($invoice);
389 389
 
390
-        if ( empty( $subscriptions ) ) {
390
+        if (empty($subscriptions)) {
391 391
             return;
392 392
         }
393 393
 
394
-        if ( ! is_array( $subscriptions ) ) {
395
-            $subscriptions = array( $subscriptions );
394
+        if (!is_array($subscriptions)) {
395
+            $subscriptions = array($subscriptions);
396 396
         }
397 397
 
398
-        foreach ( $subscriptions as $subscription ) {
399
-            $subscription->delete( true );
398
+        foreach ($subscriptions as $subscription) {
399
+            $subscription->delete(true);
400 400
         }
401 401
 
402 402
     }
@@ -409,57 +409,57 @@  discard block
 block discarded – undo
409 409
      * @param       WPInv_Invoice $invoice
410 410
      * @since       1.0.19
411 411
      */
412
-    public function update_invoice_subscription( $subscription, $invoice ) {
412
+    public function update_invoice_subscription($subscription, $invoice) {
413 413
 
414 414
         // Delete the subscription if an invoice is free or nolonger recurring.
415
-        if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_free() || ! $invoice->is_recurring() ) {
415
+        if (!$invoice->is_type('invoice') || $invoice->is_free() || !$invoice->is_recurring()) {
416 416
             return $subscription->delete();
417 417
         }
418 418
 
419
-        $subscription->set_customer_id( $invoice->get_user_id() );
420
-        $subscription->set_parent_invoice_id( $invoice->get_id() );
421
-        $subscription->set_initial_amount( $invoice->get_initial_total() );
422
-        $subscription->set_recurring_amount( $invoice->get_recurring_total() );
423
-        $subscription->set_date_created( current_time( 'mysql' ) );
424
-        $subscription->set_status( $invoice->is_paid() ? 'active' : 'pending' );
419
+        $subscription->set_customer_id($invoice->get_user_id());
420
+        $subscription->set_parent_invoice_id($invoice->get_id());
421
+        $subscription->set_initial_amount($invoice->get_initial_total());
422
+        $subscription->set_recurring_amount($invoice->get_recurring_total());
423
+        $subscription->set_date_created(current_time('mysql'));
424
+        $subscription->set_status($invoice->is_paid() ? 'active' : 'pending');
425 425
 
426 426
         // Get the recurring item and abort if it does not exist.
427
-        $subscription_item = $invoice->get_recurring( true );
428
-        if ( ! $subscription_item->get_id() ) {
429
-            $invoice->set_subscription_id( 0 );
427
+        $subscription_item = $invoice->get_recurring(true);
428
+        if (!$subscription_item->get_id()) {
429
+            $invoice->set_subscription_id(0);
430 430
             $invoice->save();
431 431
             return $subscription->delete();
432 432
         }
433 433
 
434
-        $subscription->set_product_id( $subscription_item->get_id() );
435
-        $subscription->set_period( $subscription_item->get_recurring_period( true ) );
436
-        $subscription->set_frequency( $subscription_item->get_recurring_interval() );
437
-        $subscription->set_bill_times( $subscription_item->get_recurring_limit() );
434
+        $subscription->set_product_id($subscription_item->get_id());
435
+        $subscription->set_period($subscription_item->get_recurring_period(true));
436
+        $subscription->set_frequency($subscription_item->get_recurring_interval());
437
+        $subscription->set_bill_times($subscription_item->get_recurring_limit());
438 438
 
439 439
         // Calculate the next renewal date.
440
-        $period       = $subscription_item->get_recurring_period( true );
440
+        $period       = $subscription_item->get_recurring_period(true);
441 441
         $interval     = $subscription_item->get_recurring_interval();
442 442
 
443 443
         // If the subscription item has a trial period...
444
-        if ( $subscription_item->has_free_trial() ) {
445
-            $period   = $subscription_item->get_trial_period( true );
444
+        if ($subscription_item->has_free_trial()) {
445
+            $period   = $subscription_item->get_trial_period(true);
446 446
             $interval = $subscription_item->get_trial_interval();
447
-            $subscription->set_trial_period( $interval . ' ' . $period );
448
-            $subscription->set_status( 'trialling' );
447
+            $subscription->set_trial_period($interval . ' ' . $period);
448
+            $subscription->set_status('trialling');
449 449
         }
450 450
 
451 451
         // If initial amount is free, treat it as a free trial even if the subscription item does not have a free trial.
452
-        if ( $invoice->has_free_trial() ) {
453
-            $subscription->set_trial_period( $interval . ' ' . $period );
454
-            $subscription->set_status( 'trialling' );
452
+        if ($invoice->has_free_trial()) {
453
+            $subscription->set_trial_period($interval . ' ' . $period);
454
+            $subscription->set_status('trialling');
455 455
         }
456 456
 
457 457
         // Calculate the next renewal date.
458
-        $expiration = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", strtotime( $subscription->get_date_created() ) ) );
458
+        $expiration = date('Y-m-d H:i:s', strtotime("+$interval $period", strtotime($subscription->get_date_created())));
459 459
 
460
-        $subscription->set_next_renewal_date( $expiration );
460
+        $subscription->set_next_renewal_date($expiration);
461 461
         $subscription->save();
462
-        $invoice->set_subscription_id( $subscription->get_id() );
462
+        $invoice->set_subscription_id($subscription->get_id());
463 463
         return $subscription->get_id();
464 464
 
465 465
     }
@@ -470,34 +470,34 @@  discard block
 block discarded – undo
470 470
      * @param       array $data
471 471
      * @since       1.0.19
472 472
      */
473
-    public function admin_update_single_subscription( $args ) {
473
+    public function admin_update_single_subscription($args) {
474 474
 
475 475
         // Ensure the subscription exists and that a status has been given.
476
-        if ( empty( $args['subscription_id'] ) ) {
476
+        if (empty($args['subscription_id'])) {
477 477
             return;
478 478
         }
479 479
 
480 480
         // Retrieve the subscriptions.
481
-        $subscription = new WPInv_Subscription( $args['subscription_id'] );
481
+        $subscription = new WPInv_Subscription($args['subscription_id']);
482 482
 
483
-        if ( $subscription->get_id() ) {
483
+        if ($subscription->get_id()) {
484 484
 
485 485
             $subscription->set_props(
486 486
                 array(
487
-                    'status'       => isset( $args['subscription_status'] ) ? $args['subscription_status'] : null,
488
-                    'profile_id'   => isset( $args['wpinv_subscription_profile_id'] ) ? $args['wpinv_subscription_profile_id'] : null,
489
-                    'date_created' => ! empty( $args['wpinv_subscription_date_created'] ) ? $args['wpinv_subscription_date_created'] : null,
490
-                    'expiration'   => ! empty( $args['wpinv_subscription_expiration'] ) ? $args['wpinv_subscription_expiration'] : null,
491
-                    'bill_times'   => ! empty( $args['wpinv_subscription_max_bill_times'] ) ? $args['wpinv_subscription_max_bill_times'] : null,
487
+                    'status'       => isset($args['subscription_status']) ? $args['subscription_status'] : null,
488
+                    'profile_id'   => isset($args['wpinv_subscription_profile_id']) ? $args['wpinv_subscription_profile_id'] : null,
489
+                    'date_created' => !empty($args['wpinv_subscription_date_created']) ? $args['wpinv_subscription_date_created'] : null,
490
+                    'expiration'   => !empty($args['wpinv_subscription_expiration']) ? $args['wpinv_subscription_expiration'] : null,
491
+                    'bill_times'   => !empty($args['wpinv_subscription_max_bill_times']) ? $args['wpinv_subscription_max_bill_times'] : null,
492 492
                 )
493 493
             );
494 494
 
495 495
             $changes = $subscription->get_changes();
496 496
 
497 497
             $subscription->save();
498
-            getpaid_admin()->show_info( __( 'Subscription updated', 'invoicing' ) );
498
+            getpaid_admin()->show_info(__('Subscription updated', 'invoicing'));
499 499
 
500
-            do_action( 'getpaid_admin_updated_subscription', $subscription, $args, $changes );
500
+            do_action('getpaid_admin_updated_subscription', $subscription, $args, $changes);
501 501
         }
502 502
 
503 503
     }
@@ -508,27 +508,27 @@  discard block
 block discarded – undo
508 508
      * @param       array $data
509 509
      * @since       1.0.19
510 510
      */
511
-    public function admin_renew_single_subscription( $args ) {
511
+    public function admin_renew_single_subscription($args) {
512 512
 
513 513
         // Ensure the subscription exists and that a status has been given.
514
-        if ( empty( $args['id'] ) ) {
514
+        if (empty($args['id'])) {
515 515
             return;
516 516
         }
517 517
 
518 518
         // Retrieve the subscriptions.
519
-        $subscription = new WPInv_Subscription( $args['id'] );
519
+        $subscription = new WPInv_Subscription($args['id']);
520 520
 
521
-        if ( $subscription->get_id() ) {
521
+        if ($subscription->get_id()) {
522 522
 
523
-            do_action( 'getpaid_admin_renew_subscription', $subscription );
523
+            do_action('getpaid_admin_renew_subscription', $subscription);
524 524
 
525
-            $args = array( 'transaction_id', $subscription->get_parent_invoice()->generate_key( 'renewal_' ) );
525
+            $args = array('transaction_id', $subscription->get_parent_invoice()->generate_key('renewal_'));
526 526
 
527
-            if ( ! $subscription->add_payment( $args ) ) {
528
-                getpaid_admin()->show_error( __( 'We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing' ) );
527
+            if (!$subscription->add_payment($args)) {
528
+                getpaid_admin()->show_error(__('We are unable to renew this subscription as the parent invoice does not exist.', 'invoicing'));
529 529
             } else {
530 530
                 $subscription->renew();
531
-                getpaid_admin()->show_info( __( 'This subscription has been renewed and extended.', 'invoicing' ) );
531
+                getpaid_admin()->show_info(__('This subscription has been renewed and extended.', 'invoicing'));
532 532
             }
533 533
 
534 534
             wp_safe_redirect(
@@ -551,20 +551,20 @@  discard block
 block discarded – undo
551 551
      * @param       array $data
552 552
      * @since       1.0.19
553 553
      */
554
-    public function admin_delete_single_subscription( $args ) {
554
+    public function admin_delete_single_subscription($args) {
555 555
 
556 556
         // Ensure the subscription exists and that a status has been given.
557
-        if ( empty( $args['id'] ) ) {
557
+        if (empty($args['id'])) {
558 558
             return;
559 559
         }
560 560
 
561 561
         // Retrieve the subscriptions.
562
-        $subscription = new WPInv_Subscription( $args['id'] );
562
+        $subscription = new WPInv_Subscription($args['id']);
563 563
 
564
-        if ( $subscription->delete() ) {
565
-            getpaid_admin()->show_info( __( 'This subscription has been deleted.', 'invoicing' ) );
564
+        if ($subscription->delete()) {
565
+            getpaid_admin()->show_info(__('This subscription has been deleted.', 'invoicing'));
566 566
         } else {
567
-            getpaid_admin()->show_error( __( 'We are unable to delete this subscription. Please try again.', 'invoicing' ) );
567
+            getpaid_admin()->show_error(__('We are unable to delete this subscription. Please try again.', 'invoicing'));
568 568
         }
569 569
 
570 570
         $redirected = wp_safe_redirect(
@@ -577,7 +577,7 @@  discard block
 block discarded – undo
577 577
             )
578 578
         );
579 579
 
580
-        if ( $redirected ) {
580
+        if ($redirected) {
581 581
             exit;
582 582
         }
583 583
 
@@ -590,16 +590,16 @@  discard block
 block discarded – undo
590 590
      * @param WPInv_Item $item
591 591
      * @param WPInv_Invoice $invoice
592 592
      */
593
-    public function filter_invoice_line_item_actions( $actions, $item, $invoice ) {
593
+    public function filter_invoice_line_item_actions($actions, $item, $invoice) {
594 594
 
595 595
         // Abort if this invoice uses subscription groups.
596
-        $subscriptions = getpaid_get_invoice_subscriptions( $invoice );
597
-        if ( ! $invoice->is_recurring() || ! is_object( $subscriptions ) ) {
596
+        $subscriptions = getpaid_get_invoice_subscriptions($invoice);
597
+        if (!$invoice->is_recurring() || !is_object($subscriptions)) {
598 598
             return $actions;
599 599
         }
600 600
 
601 601
         // Fetch item subscription.
602
-        $args  = array(
602
+        $args = array(
603 603
             'invoice_in'  => $invoice->is_parent() ? $invoice->get_id() : $invoice->get_parent_id(),
604 604
             'product_in'  => $item->get_id(),
605 605
             'number'      => 1,
@@ -607,13 +607,13 @@  discard block
 block discarded – undo
607 607
             'fields'      => 'id',
608 608
         );
609 609
 
610
-        $subscription = new GetPaid_Subscriptions_Query( $args );
610
+        $subscription = new GetPaid_Subscriptions_Query($args);
611 611
         $subscription = $subscription->get_results();
612 612
 
613 613
         // In case we found a match...
614
-        if ( ! empty( $subscription ) ) {
615
-            $url                     = esc_url( add_query_arg( 'subscription', (int) $subscription[0], get_permalink( (int) wpinv_get_option( 'invoice_subscription_page' ) ) ) );
616
-            $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __( 'Manage Subscription', 'invoicing' ) . '</a>';
614
+        if (!empty($subscription)) {
615
+            $url                     = esc_url(add_query_arg('subscription', (int) $subscription[0], get_permalink((int) wpinv_get_option('invoice_subscription_page'))));
616
+            $actions['subscription'] = "<a href='$url' class='text-decoration-none'>" . __('Manage Subscription', 'invoicing') . '</a>';
617 617
         }
618 618
 
619 619
         return $actions;
Please login to merge, or discard this patch.