Passed
Pull Request — master (#240)
by
unknown
03:32
created
includes/wpinv-invoice-functions.php 1 patch
Spacing   +758 added lines, -758 removed lines patch added patch discarded remove patch
@@ -7,14 +7,14 @@  discard block
 block discarded – undo
7 7
  */
8 8
  
9 9
 // MUST have WordPress.
10
-if ( !defined( 'WPINC' ) ) {
11
-    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
10
+if (!defined('WPINC')) {
11
+    exit('Do NOT access this file directly: ' . basename(__FILE__));
12 12
 }
13 13
 
14 14
 function wpinv_get_invoice_cart_id() {
15 15
     $wpinv_checkout = wpinv_get_checkout_session();
16 16
     
17
-    if ( !empty( $wpinv_checkout['invoice_id'] ) ) {
17
+    if (!empty($wpinv_checkout['invoice_id'])) {
18 18
         return $wpinv_checkout['invoice_id'];
19 19
     }
20 20
     
@@ -28,27 +28,27 @@  discard block
 block discarded – undo
28 28
  * @param  bool          $wp_error       Whether to return false or WP_Error on failure.
29 29
  * @return mixed         The value 0 or WP_Error on failure. The WPInv_Invoice object on success.
30 30
  */
31
-function wpinv_insert_invoice( $invoice_data = array(), $wp_error = false ) {
32
-    if ( empty( $invoice_data ) ) {
31
+function wpinv_insert_invoice($invoice_data = array(), $wp_error = false) {
32
+    if (empty($invoice_data)) {
33 33
         return false;
34 34
     }
35 35
     
36
-    if ( !( !empty( $invoice_data['cart_details'] ) && is_array( $invoice_data['cart_details'] ) ) ) {
37
-        return $wp_error ? new WP_Error( 'wpinv_invalid_items', __( 'Invoice must have atleast one item.', 'invoicing' ) ) : 0;
36
+    if (!(!empty($invoice_data['cart_details']) && is_array($invoice_data['cart_details']))) {
37
+        return $wp_error ? new WP_Error('wpinv_invalid_items', __('Invoice must have atleast one item.', 'invoicing')) : 0;
38 38
     }
39 39
     
40 40
     // If no user id is provided, default to the current user id
41
-    if ( empty( $invoice_data['user_id'] ) ) {
41
+    if (empty($invoice_data['user_id'])) {
42 42
         $invoice_data['user_id'] = get_current_user_id();
43 43
     }
44 44
     
45
-    $invoice_data['invoice_id'] = !empty( $invoice_data['invoice_id'] ) ? (int)$invoice_data['invoice_id'] : 0;
45
+    $invoice_data['invoice_id'] = !empty($invoice_data['invoice_id']) ? (int)$invoice_data['invoice_id'] : 0;
46 46
     
47
-    if ( empty( $invoice_data['status'] ) ) {
47
+    if (empty($invoice_data['status'])) {
48 48
         $invoice_data['status'] = 'wpi-pending';
49 49
     }
50 50
     
51
-    if ( empty( $invoice_data['ip'] ) ) {
51
+    if (empty($invoice_data['ip'])) {
52 52
         $invoice_data['ip'] = wpinv_get_ip();
53 53
     }
54 54
 
@@ -59,12 +59,12 @@  discard block
 block discarded – undo
59 59
         'status'        => $invoice_data['status'],
60 60
     );
61 61
 
62
-    $invoice = wpinv_create_invoice( $default_args, $invoice_data, true );
63
-    if ( is_wp_error( $invoice ) ) {
62
+    $invoice = wpinv_create_invoice($default_args, $invoice_data, true);
63
+    if (is_wp_error($invoice)) {
64 64
         return $wp_error ? $invoice : 0;
65 65
     }
66 66
     
67
-    if ( empty( $invoice_data['invoice_id'] ) ) {
67
+    if (empty($invoice_data['invoice_id'])) {
68 68
         //$invoice->add_note( wp_sprintf( __( 'Invoice is created with status %s.', 'invoicing' ), wpinv_status_nicename( $invoice->status ) ) );
69 69
     }
70 70
     
@@ -87,24 +87,24 @@  discard block
 block discarded – undo
87 87
         'discount'              => array(),
88 88
     );
89 89
 
90
-    if ( $user_id = (int)$invoice->get_user_id() ) {
91
-        if ( $user_address = wpinv_get_user_address( $user_id ) ) {
92
-            $default_user_info = wp_parse_args( $user_address, $default_user_info );
90
+    if ($user_id = (int)$invoice->get_user_id()) {
91
+        if ($user_address = wpinv_get_user_address($user_id)) {
92
+            $default_user_info = wp_parse_args($user_address, $default_user_info);
93 93
         }
94 94
     }
95 95
     
96
-    if ( empty( $invoice_data['user_info'] ) ) {
96
+    if (empty($invoice_data['user_info'])) {
97 97
         $invoice_data['user_info'] = array();
98 98
     }
99 99
     
100
-    $user_info = wp_parse_args( $invoice_data['user_info'], $default_user_info );
100
+    $user_info = wp_parse_args($invoice_data['user_info'], $default_user_info);
101 101
     
102
-    if ( empty( $user_info['first_name'] ) ) {
102
+    if (empty($user_info['first_name'])) {
103 103
         $user_info['first_name'] = $default_user_info['first_name'];
104 104
         $user_info['last_name'] = $default_user_info['last_name'];
105 105
     }
106 106
     
107
-    if ( empty( $user_info['country'] ) ) {
107
+    if (empty($user_info['country'])) {
108 108
         $user_info['country'] = $default_user_info['country'];
109 109
         $user_info['state'] = $default_user_info['state'];
110 110
         $user_info['city'] = $default_user_info['city'];
@@ -113,13 +113,13 @@  discard block
 block discarded – undo
113 113
         $user_info['phone'] = $default_user_info['phone'];
114 114
     }
115 115
     
116
-    if ( !empty( $user_info['discount'] ) && !is_array( $user_info['discount'] ) ) {
116
+    if (!empty($user_info['discount']) && !is_array($user_info['discount'])) {
117 117
         $user_info['discount'] = (array)$user_info['discount'];
118 118
     }
119 119
 
120 120
     // Payment details
121 121
     $payment_details = array();
122
-    if ( !empty( $invoice_data['payment_details'] ) ) {
122
+    if (!empty($invoice_data['payment_details'])) {
123 123
         $default_payment_details = array(
124 124
             'gateway'           => 'manual',
125 125
             'gateway_title'     => '',
@@ -127,56 +127,56 @@  discard block
 block discarded – undo
127 127
             'transaction_id'    => '',
128 128
         );
129 129
         
130
-        $payment_details = wp_parse_args( $invoice_data['payment_details'], $default_payment_details );
130
+        $payment_details = wp_parse_args($invoice_data['payment_details'], $default_payment_details);
131 131
         
132
-        if ( empty( $payment_details['gateway'] ) ) {
132
+        if (empty($payment_details['gateway'])) {
133 133
             $payment_details['gateway'] = 'manual';
134 134
         }
135 135
         
136
-        if ( empty( $payment_details['currency'] ) ) {
136
+        if (empty($payment_details['currency'])) {
137 137
             $payment_details['currency'] = wpinv_get_default_country();
138 138
         }
139 139
         
140
-        if ( empty( $payment_details['gateway_title'] ) ) {
141
-            $payment_details['gateway_title'] = wpinv_get_gateway_checkout_label( $payment_details['gateway'] );
140
+        if (empty($payment_details['gateway_title'])) {
141
+            $payment_details['gateway_title'] = wpinv_get_gateway_checkout_label($payment_details['gateway']);
142 142
         }
143 143
     }
144 144
     
145
-    $invoice->set( 'status', ( !empty( $invoice_data['status'] ) ? $invoice_data['status'] : 'wpi-pending' ) );
146
-    
147
-    if ( !empty( $payment_details ) ) {
148
-        $invoice->set( 'currency', $payment_details['currency'] );
149
-        $invoice->set( 'gateway', $payment_details['gateway'] );
150
-        $invoice->set( 'gateway_title', $payment_details['gateway_title'] );
151
-        $invoice->set( 'transaction_id', $payment_details['transaction_id'] );
152
-    }
153
-    
154
-    $invoice->set( 'user_info', $user_info );
155
-    $invoice->set( 'first_name', $user_info['first_name'] );
156
-    $invoice->set( 'last_name', $user_info['last_name'] );
157
-    $invoice->set( 'address', $user_info['address'] );
158
-    $invoice->set( 'company', $user_info['company'] );
159
-    $invoice->set( 'vat_number', $user_info['vat_number'] );
160
-    $invoice->set( 'phone', $user_info['phone'] );
161
-    $invoice->set( 'city', $user_info['city'] );
162
-    $invoice->set( 'country', $user_info['country'] );
163
-    $invoice->set( 'state', $user_info['state'] );
164
-    $invoice->set( 'zip', $user_info['zip'] );
165
-    $invoice->set( 'discounts', $user_info['discount'] );
166
-    $invoice->set( 'ip', ( !empty( $invoice_data['ip'] ) ? $invoice_data['ip'] : wpinv_get_ip() ) );
167
-    $invoice->set( 'mode', ( wpinv_is_test_mode() ? 'test' : 'live' ) );
168
-    $invoice->set( 'parent_invoice', ( !empty( $invoice_data['parent'] ) ? absint( $invoice_data['parent'] ) : '' ) );
169
-    
170
-    if ( !empty( $invoice_data['cart_details'] ) && is_array( $invoice_data['cart_details'] ) ) {
171
-        foreach ( $invoice_data['cart_details'] as $key => $item ) {
172
-            $item_id        = !empty( $item['id'] ) ? $item['id'] : 0;
173
-            $quantity       = !empty( $item['quantity'] ) ? $item['quantity'] : 1;
174
-            $name           = !empty( $item['name'] ) ? $item['name'] : '';
175
-            $item_price     = isset( $item['item_price'] ) ? $item['item_price'] : '';
145
+    $invoice->set('status', (!empty($invoice_data['status']) ? $invoice_data['status'] : 'wpi-pending'));
146
+    
147
+    if (!empty($payment_details)) {
148
+        $invoice->set('currency', $payment_details['currency']);
149
+        $invoice->set('gateway', $payment_details['gateway']);
150
+        $invoice->set('gateway_title', $payment_details['gateway_title']);
151
+        $invoice->set('transaction_id', $payment_details['transaction_id']);
152
+    }
153
+    
154
+    $invoice->set('user_info', $user_info);
155
+    $invoice->set('first_name', $user_info['first_name']);
156
+    $invoice->set('last_name', $user_info['last_name']);
157
+    $invoice->set('address', $user_info['address']);
158
+    $invoice->set('company', $user_info['company']);
159
+    $invoice->set('vat_number', $user_info['vat_number']);
160
+    $invoice->set('phone', $user_info['phone']);
161
+    $invoice->set('city', $user_info['city']);
162
+    $invoice->set('country', $user_info['country']);
163
+    $invoice->set('state', $user_info['state']);
164
+    $invoice->set('zip', $user_info['zip']);
165
+    $invoice->set('discounts', $user_info['discount']);
166
+    $invoice->set('ip', (!empty($invoice_data['ip']) ? $invoice_data['ip'] : wpinv_get_ip()));
167
+    $invoice->set('mode', (wpinv_is_test_mode() ? 'test' : 'live'));
168
+    $invoice->set('parent_invoice', (!empty($invoice_data['parent']) ? absint($invoice_data['parent']) : ''));
169
+    
170
+    if (!empty($invoice_data['cart_details']) && is_array($invoice_data['cart_details'])) {
171
+        foreach ($invoice_data['cart_details'] as $key => $item) {
172
+            $item_id        = !empty($item['id']) ? $item['id'] : 0;
173
+            $quantity       = !empty($item['quantity']) ? $item['quantity'] : 1;
174
+            $name           = !empty($item['name']) ? $item['name'] : '';
175
+            $item_price     = isset($item['item_price']) ? $item['item_price'] : '';
176 176
             
177
-            $post_item  = new WPInv_Item( $item_id );
178
-            if ( !empty( $post_item ) ) {
179
-                $name       = !empty( $name ) ? $name : $post_item->get_name();
177
+            $post_item = new WPInv_Item($item_id);
178
+            if (!empty($post_item)) {
179
+                $name       = !empty($name) ? $name : $post_item->get_name();
180 180
                 $item_price = $item_price !== '' ? $item_price : $post_item->get_price();
181 181
             } else {
182 182
                 continue;
@@ -186,253 +186,253 @@  discard block
 block discarded – undo
186 186
                 'name'          => $name,
187 187
                 'quantity'      => $quantity,
188 188
                 'item_price'    => $item_price,
189
-                'custom_price'  => isset( $item['custom_price'] ) ? $item['custom_price'] : '',
190
-                'tax'           => !empty( $item['tax'] ) ? $item['tax'] : 0.00,
191
-                'discount'      => isset( $item['discount'] ) ? $item['discount'] : 0,
192
-                'meta'          => isset( $item['meta'] ) ? $item['meta'] : array(),
193
-                'fees'          => isset( $item['fees'] ) ? $item['fees'] : array(),
189
+                'custom_price'  => isset($item['custom_price']) ? $item['custom_price'] : '',
190
+                'tax'           => !empty($item['tax']) ? $item['tax'] : 0.00,
191
+                'discount'      => isset($item['discount']) ? $item['discount'] : 0,
192
+                'meta'          => isset($item['meta']) ? $item['meta'] : array(),
193
+                'fees'          => isset($item['fees']) ? $item['fees'] : array(),
194 194
             );
195 195
 
196
-            $invoice->add_item( $item_id, $args );
196
+            $invoice->add_item($item_id, $args);
197 197
         }
198 198
     }
199 199
 
200
-    $invoice->increase_tax( wpinv_get_cart_fee_tax() );
200
+    $invoice->increase_tax(wpinv_get_cart_fee_tax());
201 201
 
202
-    if ( isset( $invoice_data['post_date'] ) ) {
203
-        $invoice->set( 'date', $invoice_data['post_date'] );
202
+    if (isset($invoice_data['post_date'])) {
203
+        $invoice->set('date', $invoice_data['post_date']);
204 204
     }
205 205
     
206 206
     // Invoice due date
207
-    if ( isset( $invoice_data['due_date'] ) ) {
208
-        $invoice->set( 'due_date', $invoice_data['due_date'] );
207
+    if (isset($invoice_data['due_date'])) {
208
+        $invoice->set('due_date', $invoice_data['due_date']);
209 209
     }
210 210
     
211 211
     $invoice->save();
212 212
     
213 213
     // Add notes
214
-    if ( !empty( $invoice_data['private_note'] ) ) {
215
-        $invoice->add_note( $invoice_data['private_note'] );
214
+    if (!empty($invoice_data['private_note'])) {
215
+        $invoice->add_note($invoice_data['private_note']);
216 216
     }
217
-    if ( !empty( $invoice_data['user_note'] ) ) {
218
-        $invoice->add_note( $invoice_data['user_note'], true );
217
+    if (!empty($invoice_data['user_note'])) {
218
+        $invoice->add_note($invoice_data['user_note'], true);
219 219
     }
220 220
     
221
-    do_action( 'wpinv_insert_invoice', $invoice->ID, $invoice_data );
221
+    do_action('wpinv_insert_invoice', $invoice->ID, $invoice_data);
222 222
 
223
-    if ( ! empty( $invoice->ID ) ) {
223
+    if (!empty($invoice->ID)) {
224 224
         global $wpi_userID, $wpinv_ip_address_country;
225 225
         
226 226
         $checkout_session = wpinv_get_checkout_session();
227 227
         
228 228
         $data_session                   = array();
229 229
         $data_session['invoice_id']     = $invoice->ID;
230
-        $data_session['cart_discounts'] = $invoice->get_discounts( true );
230
+        $data_session['cart_discounts'] = $invoice->get_discounts(true);
231 231
         
232
-        wpinv_set_checkout_session( $data_session );
232
+        wpinv_set_checkout_session($data_session);
233 233
         
234 234
         $wpi_userID         = (int)$invoice->get_user_id();
235 235
         
236
-        $_POST['country']   = !empty( $invoice->country ) ? $invoice->country : wpinv_get_default_country();
236
+        $_POST['country']   = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country();
237 237
         $_POST['state']     = $invoice->state;
238 238
 
239
-        $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) );
240
-        $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) );
239
+        $invoice->set('country', sanitize_text_field($_POST['country']));
240
+        $invoice->set('state', sanitize_text_field($_POST['state']));
241 241
         
242 242
         $wpinv_ip_address_country = $invoice->country;
243 243
         
244
-        $invoice = $invoice->recalculate_totals( true );
244
+        $invoice = $invoice->recalculate_totals(true);
245 245
         
246
-        wpinv_set_checkout_session( $checkout_session );
246
+        wpinv_set_checkout_session($checkout_session);
247 247
                     
248 248
         return $invoice;
249 249
     }
250 250
     
251
-    if ( $wp_error ) {
252
-        if ( is_wp_error( $invoice ) ) {
251
+    if ($wp_error) {
252
+        if (is_wp_error($invoice)) {
253 253
             return $invoice;
254 254
         } else {
255
-            return new WP_Error( 'wpinv_insert_invoice_error', __( 'Error in insert invoice.', 'invoicing' ) );
255
+            return new WP_Error('wpinv_insert_invoice_error', __('Error in insert invoice.', 'invoicing'));
256 256
         }
257 257
     } else {
258 258
         return 0;
259 259
     }
260 260
 }
261 261
 
262
-function wpinv_update_invoice( $invoice_data = array(), $wp_error = false ) {
263
-    $invoice_ID = !empty( $invoice_data['ID'] ) ? absint( $invoice_data['ID'] ) : NULL;
262
+function wpinv_update_invoice($invoice_data = array(), $wp_error = false) {
263
+    $invoice_ID = !empty($invoice_data['ID']) ? absint($invoice_data['ID']) : NULL;
264 264
 
265
-    if ( !$invoice_ID ) {
266
-        if ( $wp_error ) {
267
-            return new WP_Error( 'invalid_invoice_id', __( 'Invalid invoice ID.', 'invoicing' ) );
265
+    if (!$invoice_ID) {
266
+        if ($wp_error) {
267
+            return new WP_Error('invalid_invoice_id', __('Invalid invoice ID.', 'invoicing'));
268 268
         }
269 269
         return 0;
270 270
     }
271 271
 
272
-    $invoice = wpinv_get_invoice( $invoice_ID );
272
+    $invoice = wpinv_get_invoice($invoice_ID);
273 273
 
274
-    $recurring_item = $invoice->is_recurring() ? $invoice->get_recurring( true ) : NULL;
274
+    $recurring_item = $invoice->is_recurring() ? $invoice->get_recurring(true) : NULL;
275 275
 
276
-    if ( empty( $invoice->ID ) ) {
277
-        if ( $wp_error ) {
278
-            return new WP_Error( 'invalid_invoice', __( 'Invalid invoice.', 'invoicing' ) );
276
+    if (empty($invoice->ID)) {
277
+        if ($wp_error) {
278
+            return new WP_Error('invalid_invoice', __('Invalid invoice.', 'invoicing'));
279 279
         }
280 280
         return 0;
281 281
     }
282 282
 
283
-    if ( !$invoice->has_status( array( 'wpi-pending' ) ) ) {
284
-        if ( $wp_error ) {
285
-            return new WP_Error( 'invalid_invoice_status', __( 'Only invoice with pending payment is allowed to update.', 'invoicing' ) );
283
+    if (!$invoice->has_status(array('wpi-pending'))) {
284
+        if ($wp_error) {
285
+            return new WP_Error('invalid_invoice_status', __('Only invoice with pending payment is allowed to update.', 'invoicing'));
286 286
         }
287 287
         return 0;
288 288
     }
289 289
 
290 290
     // Invoice status
291
-    if ( !empty( $invoice_data['status'] ) ) {
292
-        $invoice->set( 'status', $invoice_data['status'] );
291
+    if (!empty($invoice_data['status'])) {
292
+        $invoice->set('status', $invoice_data['status']);
293 293
     }
294 294
 
295 295
     // Invoice date
296
-    if ( !empty( $invoice_data['post_date'] ) ) {
297
-        $invoice->set( 'date', $invoice_data['post_date'] );
296
+    if (!empty($invoice_data['post_date'])) {
297
+        $invoice->set('date', $invoice_data['post_date']);
298 298
     }
299 299
 
300 300
     // Invoice due date
301
-    if ( isset( $invoice_data['due_date'] ) ) {
302
-        $invoice->set( 'due_date', $invoice_data['due_date'] );
301
+    if (isset($invoice_data['due_date'])) {
302
+        $invoice->set('due_date', $invoice_data['due_date']);
303 303
     }
304 304
 
305 305
     // Invoice IP address
306
-    if ( !empty( $invoice_data['ip'] ) ) {
307
-        $invoice->set( 'ip', $invoice_data['ip'] );
306
+    if (!empty($invoice_data['ip'])) {
307
+        $invoice->set('ip', $invoice_data['ip']);
308 308
     }
309 309
     
310 310
     // User info
311
-    if ( !empty( $invoice_data['user_info'] ) && is_array( $invoice_data['user_info'] ) ) {
312
-        $user_info = wp_parse_args( $invoice_data['user_info'], $invoice->user_info );
311
+    if (!empty($invoice_data['user_info']) && is_array($invoice_data['user_info'])) {
312
+        $user_info = wp_parse_args($invoice_data['user_info'], $invoice->user_info);
313 313
 
314
-        if ( $discounts = $invoice->get_discounts() ) {
314
+        if ($discounts = $invoice->get_discounts()) {
315 315
             $set_discount = $discounts;
316 316
         } else {
317 317
             $set_discount = '';
318 318
         }
319 319
 
320 320
         // Manage discount
321
-        if ( !empty( $invoice_data['user_info']['discount'] ) ) {
321
+        if (!empty($invoice_data['user_info']['discount'])) {
322 322
             // Remove discount
323
-            if ( $invoice_data['user_info']['discount'] == 'none' ) {
323
+            if ($invoice_data['user_info']['discount'] == 'none') {
324 324
                 $set_discount = '';
325 325
             } else {
326 326
                 $set_discount = $invoice_data['user_info']['discount'];
327 327
             }
328 328
 
329
-            $invoice->set( 'discounts', $set_discount );
329
+            $invoice->set('discounts', $set_discount);
330 330
         }
331 331
 
332 332
         $user_info['discount'] = $set_discount;
333 333
 
334
-        $invoice->set( 'user_info', $user_info );
334
+        $invoice->set('user_info', $user_info);
335 335
     }
336 336
 
337
-    if ( !empty( $invoice_data['cart_details'] ) && is_array( $invoice_data['cart_details'] ) && $cart_details = $invoice_data['cart_details'] ) {
338
-        $remove_items = !empty( $cart_details['remove_items'] ) && is_array( $cart_details['remove_items'] ) ? $cart_details['remove_items'] : array();
337
+    if (!empty($invoice_data['cart_details']) && is_array($invoice_data['cart_details']) && $cart_details = $invoice_data['cart_details']) {
338
+        $remove_items = !empty($cart_details['remove_items']) && is_array($cart_details['remove_items']) ? $cart_details['remove_items'] : array();
339 339
 
340
-        if ( !empty( $remove_items[0]['id'] ) ) {
341
-            foreach ( $remove_items as $item ) {
342
-                $item_id        = !empty( $item['id'] ) ? $item['id'] : 0;
343
-                $quantity       = !empty( $item['quantity'] ) ? $item['quantity'] : 1;
344
-                if ( empty( $item_id ) ) {
340
+        if (!empty($remove_items[0]['id'])) {
341
+            foreach ($remove_items as $item) {
342
+                $item_id        = !empty($item['id']) ? $item['id'] : 0;
343
+                $quantity       = !empty($item['quantity']) ? $item['quantity'] : 1;
344
+                if (empty($item_id)) {
345 345
                     continue;
346 346
                 }
347 347
 
348
-                foreach ( $invoice->cart_details as $cart_index => $cart_item ) {
349
-                    if ( $item_id == $cart_item['id'] ) {
348
+                foreach ($invoice->cart_details as $cart_index => $cart_item) {
349
+                    if ($item_id == $cart_item['id']) {
350 350
                         $args = array(
351 351
                             'id'         => $item_id,
352 352
                             'quantity'   => $quantity,
353 353
                             'cart_index' => $cart_index
354 354
                         );
355 355
 
356
-                        $invoice->remove_item( $item_id, $args );
356
+                        $invoice->remove_item($item_id, $args);
357 357
                         break;
358 358
                     }
359 359
                 }
360 360
             }
361 361
         }
362 362
 
363
-        $add_items = !empty( $cart_details['add_items'] ) && is_array( $cart_details['add_items'] ) ? $cart_details['add_items'] : array();
363
+        $add_items = !empty($cart_details['add_items']) && is_array($cart_details['add_items']) ? $cart_details['add_items'] : array();
364 364
 
365
-        if ( !empty( $add_items[0]['id'] ) ) {
366
-            foreach ( $add_items as $item ) {
367
-                $item_id        = !empty( $item['id'] ) ? $item['id'] : 0;
368
-                $post_item      = new WPInv_Item( $item_id );
369
-                if ( empty( $post_item ) ) {
365
+        if (!empty($add_items[0]['id'])) {
366
+            foreach ($add_items as $item) {
367
+                $item_id        = !empty($item['id']) ? $item['id'] : 0;
368
+                $post_item      = new WPInv_Item($item_id);
369
+                if (empty($post_item)) {
370 370
                     continue;
371 371
                 }
372 372
 
373 373
                 $valid_item = true;
374
-                if ( !empty( $recurring_item ) ) {
375
-                    if ( $recurring_item->ID != $item_id ) {
374
+                if (!empty($recurring_item)) {
375
+                    if ($recurring_item->ID != $item_id) {
376 376
                         $valid_item = false;
377 377
                     }
378
-                } else if ( wpinv_is_recurring_item( $item_id ) ) {
378
+                } else if (wpinv_is_recurring_item($item_id)) {
379 379
                     $valid_item = false;
380 380
                 }
381 381
                 
382
-                if ( !$valid_item ) {
383
-                    if ( $wp_error ) {
384
-                        return new WP_Error( 'invalid_invoice_item', __( 'You can not add item because recurring item must be paid individually!', 'invoicing' ) );
382
+                if (!$valid_item) {
383
+                    if ($wp_error) {
384
+                        return new WP_Error('invalid_invoice_item', __('You can not add item because recurring item must be paid individually!', 'invoicing'));
385 385
                     }
386 386
                     return 0;
387 387
                 }
388 388
 
389
-                $quantity       = !empty( $item['quantity'] ) ? $item['quantity'] : 1;
390
-                $name           = !empty( $item['name'] ) ? $item['name'] : $post_item->get_name();
391
-                $item_price     = isset( $item['item_price'] ) ? $item['item_price'] : $post_item->get_price();
389
+                $quantity       = !empty($item['quantity']) ? $item['quantity'] : 1;
390
+                $name           = !empty($item['name']) ? $item['name'] : $post_item->get_name();
391
+                $item_price     = isset($item['item_price']) ? $item['item_price'] : $post_item->get_price();
392 392
 
393 393
                 $args = array(
394 394
                     'name'          => $name,
395 395
                     'quantity'      => $quantity,
396 396
                     'item_price'    => $item_price,
397
-                    'custom_price'  => isset( $item['custom_price'] ) ? $item['custom_price'] : '',
398
-                    'tax'           => !empty( $item['tax'] ) ? $item['tax'] : 0,
399
-                    'discount'      => isset( $item['discount'] ) ? $item['discount'] : 0,
400
-                    'meta'          => isset( $item['meta'] ) ? $item['meta'] : array(),
401
-                    'fees'          => isset( $item['fees'] ) ? $item['fees'] : array(),
397
+                    'custom_price'  => isset($item['custom_price']) ? $item['custom_price'] : '',
398
+                    'tax'           => !empty($item['tax']) ? $item['tax'] : 0,
399
+                    'discount'      => isset($item['discount']) ? $item['discount'] : 0,
400
+                    'meta'          => isset($item['meta']) ? $item['meta'] : array(),
401
+                    'fees'          => isset($item['fees']) ? $item['fees'] : array(),
402 402
                 );
403 403
 
404
-                $invoice->add_item( $item_id, $args );
404
+                $invoice->add_item($item_id, $args);
405 405
             }
406 406
         }
407 407
     }
408 408
     
409 409
     // Payment details
410
-    if ( !empty( $invoice_data['payment_details'] ) && $payment_details = $invoice_data['payment_details'] ) {
411
-        if ( !empty( $payment_details['gateway'] ) ) {
412
-            $invoice->set( 'gateway', $payment_details['gateway'] );
410
+    if (!empty($invoice_data['payment_details']) && $payment_details = $invoice_data['payment_details']) {
411
+        if (!empty($payment_details['gateway'])) {
412
+            $invoice->set('gateway', $payment_details['gateway']);
413 413
         }
414 414
 
415
-        if ( !empty( $payment_details['transaction_id'] ) ) {
416
-            $invoice->set( 'transaction_id', $payment_details['transaction_id'] );
415
+        if (!empty($payment_details['transaction_id'])) {
416
+            $invoice->set('transaction_id', $payment_details['transaction_id']);
417 417
         }
418 418
     }
419 419
 
420
-    do_action( 'wpinv_pre_update_invoice', $invoice->ID, $invoice_data );
420
+    do_action('wpinv_pre_update_invoice', $invoice->ID, $invoice_data);
421 421
 
422 422
     // Parent invoice
423
-    if ( !empty( $invoice_data['parent'] ) ) {
424
-        $invoice->set( 'parent_invoice', $invoice_data['parent'] );
423
+    if (!empty($invoice_data['parent'])) {
424
+        $invoice->set('parent_invoice', $invoice_data['parent']);
425 425
     }
426 426
 
427 427
     // Save invoice data.
428 428
     $invoice->save();
429 429
     
430
-    if ( empty( $invoice->ID ) || is_wp_error( $invoice ) ) {
431
-        if ( $wp_error ) {
432
-            if ( is_wp_error( $invoice ) ) {
430
+    if (empty($invoice->ID) || is_wp_error($invoice)) {
431
+        if ($wp_error) {
432
+            if (is_wp_error($invoice)) {
433 433
                 return $invoice;
434 434
             } else {
435
-                return new WP_Error( 'wpinv_update_invoice_error', __( 'Error in update invoice.', 'invoicing' ) );
435
+                return new WP_Error('wpinv_update_invoice_error', __('Error in update invoice.', 'invoicing'));
436 436
             }
437 437
         } else {
438 438
             return 0;
@@ -440,13 +440,13 @@  discard block
 block discarded – undo
440 440
     }
441 441
 
442 442
     // Add private note
443
-    if ( !empty( $invoice_data['private_note'] ) ) {
444
-        $invoice->add_note( $invoice_data['private_note'] );
443
+    if (!empty($invoice_data['private_note'])) {
444
+        $invoice->add_note($invoice_data['private_note']);
445 445
     }
446 446
 
447 447
     // Add user note
448
-    if ( !empty( $invoice_data['user_note'] ) ) {
449
-        $invoice->add_note( $invoice_data['user_note'], true );
448
+    if (!empty($invoice_data['user_note'])) {
449
+        $invoice->add_note($invoice_data['user_note'], true);
450 450
     }
451 451
 
452 452
     global $wpi_userID, $wpinv_ip_address_country;
@@ -455,450 +455,450 @@  discard block
 block discarded – undo
455 455
 
456 456
     $data_session                   = array();
457 457
     $data_session['invoice_id']     = $invoice->ID;
458
-    $data_session['cart_discounts'] = $invoice->get_discounts( true );
458
+    $data_session['cart_discounts'] = $invoice->get_discounts(true);
459 459
 
460
-    wpinv_set_checkout_session( $data_session );
460
+    wpinv_set_checkout_session($data_session);
461 461
 
462 462
     $wpi_userID         = (int)$invoice->get_user_id();
463 463
 
464
-    $_POST['country']   = !empty( $invoice->country ) ? $invoice->country : wpinv_get_default_country();
464
+    $_POST['country']   = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country();
465 465
     $_POST['state']     = $invoice->state;
466 466
 
467
-    $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) );
468
-    $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) );
467
+    $invoice->set('country', sanitize_text_field($_POST['country']));
468
+    $invoice->set('state', sanitize_text_field($_POST['state']));
469 469
 
470 470
     $wpinv_ip_address_country = $invoice->country;
471 471
 
472
-    $invoice = $invoice->recalculate_totals( true );
472
+    $invoice = $invoice->recalculate_totals(true);
473 473
 
474
-    do_action( 'wpinv_post_update_invoice', $invoice->ID, $invoice_data );
474
+    do_action('wpinv_post_update_invoice', $invoice->ID, $invoice_data);
475 475
 
476
-    wpinv_set_checkout_session( $checkout_session );
476
+    wpinv_set_checkout_session($checkout_session);
477 477
 
478 478
     return $invoice;
479 479
 }
480 480
 
481
-function wpinv_get_invoice( $invoice_id = 0, $cart = false ) {
482
-    if ( $cart && empty( $invoice_id ) ) {
481
+function wpinv_get_invoice($invoice_id = 0, $cart = false) {
482
+    if ($cart && empty($invoice_id)) {
483 483
         $invoice_id = (int)wpinv_get_invoice_cart_id();
484 484
     }
485 485
 
486
-    $invoice = new WPInv_Invoice( $invoice_id );
486
+    $invoice = new WPInv_Invoice($invoice_id);
487 487
 
488
-    if ( ! empty( $invoice ) && ! empty( $invoice->ID ) ) {
488
+    if (!empty($invoice) && !empty($invoice->ID)) {
489 489
         return $invoice;
490 490
     }
491 491
     
492 492
     return NULL;
493 493
 }
494 494
 
495
-function wpinv_get_invoice_cart( $invoice_id = 0 ) {
496
-    return wpinv_get_invoice( $invoice_id, true );
495
+function wpinv_get_invoice_cart($invoice_id = 0) {
496
+    return wpinv_get_invoice($invoice_id, true);
497 497
 }
498 498
 
499
-function wpinv_get_invoice_description( $invoice_id = 0 ) {
500
-    $invoice = new WPInv_Invoice( $invoice_id );
499
+function wpinv_get_invoice_description($invoice_id = 0) {
500
+    $invoice = new WPInv_Invoice($invoice_id);
501 501
     return $invoice->get_description();
502 502
 }
503 503
 
504
-function wpinv_get_invoice_currency_code( $invoice_id = 0 ) {
505
-    $invoice = new WPInv_Invoice( $invoice_id );
504
+function wpinv_get_invoice_currency_code($invoice_id = 0) {
505
+    $invoice = new WPInv_Invoice($invoice_id);
506 506
     return $invoice->get_currency();
507 507
 }
508 508
 
509
-function wpinv_get_payment_user_email( $invoice_id ) {
510
-    $invoice = new WPInv_Invoice( $invoice_id );
509
+function wpinv_get_payment_user_email($invoice_id) {
510
+    $invoice = new WPInv_Invoice($invoice_id);
511 511
     return $invoice->get_email();
512 512
 }
513 513
 
514
-function wpinv_get_user_id( $invoice_id ) {
515
-    $invoice = new WPInv_Invoice( $invoice_id );
514
+function wpinv_get_user_id($invoice_id) {
515
+    $invoice = new WPInv_Invoice($invoice_id);
516 516
     return $invoice->get_user_id();
517 517
 }
518 518
 
519
-function wpinv_get_invoice_status( $invoice_id, $return_label = false ) {
520
-    $invoice = new WPInv_Invoice( $invoice_id );
519
+function wpinv_get_invoice_status($invoice_id, $return_label = false) {
520
+    $invoice = new WPInv_Invoice($invoice_id);
521 521
     
522
-    return $invoice->get_status( $return_label );
522
+    return $invoice->get_status($return_label);
523 523
 }
524 524
 
525
-function wpinv_get_payment_gateway( $invoice_id, $return_label = false ) {
526
-    $invoice = new WPInv_Invoice( $invoice_id );
525
+function wpinv_get_payment_gateway($invoice_id, $return_label = false) {
526
+    $invoice = new WPInv_Invoice($invoice_id);
527 527
     
528
-    return $invoice->get_gateway( $return_label );
528
+    return $invoice->get_gateway($return_label);
529 529
 }
530 530
 
531
-function wpinv_get_payment_gateway_name( $invoice_id ) {
532
-    $invoice = new WPInv_Invoice( $invoice_id );
531
+function wpinv_get_payment_gateway_name($invoice_id) {
532
+    $invoice = new WPInv_Invoice($invoice_id);
533 533
     
534 534
     return $invoice->get_gateway_title();
535 535
 }
536 536
 
537
-function wpinv_get_payment_transaction_id( $invoice_id ) {
538
-    $invoice = new WPInv_Invoice( $invoice_id );
537
+function wpinv_get_payment_transaction_id($invoice_id) {
538
+    $invoice = new WPInv_Invoice($invoice_id);
539 539
     
540 540
     return $invoice->get_transaction_id();
541 541
 }
542 542
 
543
-function wpinv_get_id_by_transaction_id( $key ) {
543
+function wpinv_get_id_by_transaction_id($key) {
544 544
     global $wpdb;
545 545
 
546
-    $invoice_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpinv_transaction_id' AND meta_value = %s LIMIT 1", $key ) );
546
+    $invoice_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpinv_transaction_id' AND meta_value = %s LIMIT 1", $key));
547 547
 
548
-    if ( $invoice_id != NULL )
548
+    if ($invoice_id != NULL)
549 549
         return $invoice_id;
550 550
 
551 551
     return 0;
552 552
 }
553 553
 
554
-function wpinv_get_invoice_meta( $invoice_id = 0, $meta_key = '_wpinv_payment_meta', $single = true ) {
555
-    $invoice = new WPInv_Invoice( $invoice_id );
554
+function wpinv_get_invoice_meta($invoice_id = 0, $meta_key = '_wpinv_payment_meta', $single = true) {
555
+    $invoice = new WPInv_Invoice($invoice_id);
556 556
 
557
-    return $invoice->get_meta( $meta_key, $single );
557
+    return $invoice->get_meta($meta_key, $single);
558 558
 }
559 559
 
560
-function wpinv_update_invoice_meta( $invoice_id = 0, $meta_key = '', $meta_value = '', $prev_value = '' ) {
561
-    $invoice = new WPInv_Invoice( $invoice_id );
560
+function wpinv_update_invoice_meta($invoice_id = 0, $meta_key = '', $meta_value = '', $prev_value = '') {
561
+    $invoice = new WPInv_Invoice($invoice_id);
562 562
     
563
-    return $invoice->update_meta( $meta_key, $meta_value, $prev_value );
563
+    return $invoice->update_meta($meta_key, $meta_value, $prev_value);
564 564
 }
565 565
 
566
-function wpinv_get_items( $invoice_id = 0 ) {
567
-    $invoice            = wpinv_get_invoice( $invoice_id );
566
+function wpinv_get_items($invoice_id = 0) {
567
+    $invoice            = wpinv_get_invoice($invoice_id);
568 568
     
569 569
     $items              = $invoice->get_items();
570 570
     $invoice_currency   = $invoice->get_currency();
571 571
 
572
-    if ( !empty( $items ) && is_array( $items ) ) {
573
-        foreach ( $items as $key => $item ) {
572
+    if (!empty($items) && is_array($items)) {
573
+        foreach ($items as $key => $item) {
574 574
             $items[$key]['currency'] = $invoice_currency;
575 575
 
576
-            if ( !isset( $item['subtotal'] ) ) {
576
+            if (!isset($item['subtotal'])) {
577 577
                 $items[$key]['subtotal'] = $items[$key]['amount'] * 1;
578 578
             }
579 579
         }
580 580
     }
581 581
 
582
-    return apply_filters( 'wpinv_get_items', $items, $invoice_id );
582
+    return apply_filters('wpinv_get_items', $items, $invoice_id);
583 583
 }
584 584
 
585
-function wpinv_get_fees( $invoice_id = 0 ) {
586
-    $invoice           = wpinv_get_invoice( $invoice_id );
585
+function wpinv_get_fees($invoice_id = 0) {
586
+    $invoice           = wpinv_get_invoice($invoice_id);
587 587
     $fees              = $invoice->get_fees();
588 588
 
589
-    return apply_filters( 'wpinv_get_fees', $fees, $invoice_id );
589
+    return apply_filters('wpinv_get_fees', $fees, $invoice_id);
590 590
 }
591 591
 
592
-function wpinv_get_invoice_ip( $invoice_id ) {
593
-    $invoice = new WPInv_Invoice( $invoice_id );
592
+function wpinv_get_invoice_ip($invoice_id) {
593
+    $invoice = new WPInv_Invoice($invoice_id);
594 594
     return $invoice->get_ip();
595 595
 }
596 596
 
597
-function wpinv_get_invoice_user_info( $invoice_id ) {
598
-    $invoice = new WPInv_Invoice( $invoice_id );
597
+function wpinv_get_invoice_user_info($invoice_id) {
598
+    $invoice = new WPInv_Invoice($invoice_id);
599 599
     return $invoice->get_user_info();
600 600
 }
601 601
 
602
-function wpinv_subtotal( $invoice_id = 0, $currency = false ) {
603
-    $invoice = new WPInv_Invoice( $invoice_id );
602
+function wpinv_subtotal($invoice_id = 0, $currency = false) {
603
+    $invoice = new WPInv_Invoice($invoice_id);
604 604
 
605
-    return $invoice->get_subtotal( $currency );
605
+    return $invoice->get_subtotal($currency);
606 606
 }
607 607
 
608
-function wpinv_tax( $invoice_id = 0, $currency = false ) {
609
-    $invoice = new WPInv_Invoice( $invoice_id );
608
+function wpinv_tax($invoice_id = 0, $currency = false) {
609
+    $invoice = new WPInv_Invoice($invoice_id);
610 610
 
611
-    return $invoice->get_tax( $currency );
611
+    return $invoice->get_tax($currency);
612 612
 }
613 613
 
614
-function wpinv_discount( $invoice_id = 0, $currency = false, $dash = false ) {
615
-    $invoice = wpinv_get_invoice( $invoice_id );
614
+function wpinv_discount($invoice_id = 0, $currency = false, $dash = false) {
615
+    $invoice = wpinv_get_invoice($invoice_id);
616 616
 
617
-    return $invoice->get_discount( $currency, $dash );
617
+    return $invoice->get_discount($currency, $dash);
618 618
 }
619 619
 
620
-function wpinv_discount_code( $invoice_id = 0 ) {
621
-    $invoice = new WPInv_Invoice( $invoice_id );
620
+function wpinv_discount_code($invoice_id = 0) {
621
+    $invoice = new WPInv_Invoice($invoice_id);
622 622
 
623 623
     return $invoice->get_discount_code();
624 624
 }
625 625
 
626
-function wpinv_payment_total( $invoice_id = 0, $currency = false ) {
627
-    $invoice = new WPInv_Invoice( $invoice_id );
626
+function wpinv_payment_total($invoice_id = 0, $currency = false) {
627
+    $invoice = new WPInv_Invoice($invoice_id);
628 628
 
629
-    return $invoice->get_total( $currency );
629
+    return $invoice->get_total($currency);
630 630
 }
631 631
 
632
-function wpinv_get_date_created( $invoice_id = 0, $format = '' ) {
633
-    $invoice = new WPInv_Invoice( $invoice_id );
632
+function wpinv_get_date_created($invoice_id = 0, $format = '') {
633
+    $invoice = new WPInv_Invoice($invoice_id);
634 634
 
635
-    $format         = !empty( $format ) ? $format : get_option( 'date_format' );
635
+    $format         = !empty($format) ? $format : get_option('date_format');
636 636
     $date_created   = $invoice->get_created_date();
637
-    $date_created   = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n( $format, strtotime( $date_created ) ) : '';
637
+    $date_created   = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? date_i18n($format, strtotime($date_created)) : '';
638 638
 
639 639
     return $date_created;
640 640
 }
641 641
 
642
-function wpinv_get_invoice_date( $invoice_id = 0, $format = '', $default = true ) {
643
-    $invoice = new WPInv_Invoice( $invoice_id );
642
+function wpinv_get_invoice_date($invoice_id = 0, $format = '', $default = true) {
643
+    $invoice = new WPInv_Invoice($invoice_id);
644 644
     
645
-    $format         = !empty( $format ) ? $format : get_option( 'date_format' );
645
+    $format         = !empty($format) ? $format : get_option('date_format');
646 646
     $date_completed = $invoice->get_completed_date();
647
-    $invoice_date   = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? date_i18n( $format, strtotime( $date_completed ) ) : '';
648
-    if ( $invoice_date == '' && $default ) {
649
-        $invoice_date   = wpinv_get_date_created( $invoice_id, $format );
647
+    $invoice_date   = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? date_i18n($format, strtotime($date_completed)) : '';
648
+    if ($invoice_date == '' && $default) {
649
+        $invoice_date = wpinv_get_date_created($invoice_id, $format);
650 650
     }
651 651
 
652 652
     return $invoice_date;
653 653
 }
654 654
 
655
-function wpinv_get_invoice_vat_number( $invoice_id = 0 ) {
656
-    $invoice = new WPInv_Invoice( $invoice_id );
655
+function wpinv_get_invoice_vat_number($invoice_id = 0) {
656
+    $invoice = new WPInv_Invoice($invoice_id);
657 657
     
658 658
     return $invoice->vat_number;
659 659
 }
660 660
 
661
-function wpinv_insert_payment_note( $invoice_id = 0, $note = '', $user_type = false, $added_by_user = false, $system = false ) {
662
-    $invoice = new WPInv_Invoice( $invoice_id );
661
+function wpinv_insert_payment_note($invoice_id = 0, $note = '', $user_type = false, $added_by_user = false, $system = false) {
662
+    $invoice = new WPInv_Invoice($invoice_id);
663 663
 
664
-    return $invoice->add_note( $note, $user_type, $added_by_user, $system );
664
+    return $invoice->add_note($note, $user_type, $added_by_user, $system);
665 665
 }
666 666
 
667
-function wpinv_get_invoice_notes( $invoice_id = 0, $type = '' ) {
667
+function wpinv_get_invoice_notes($invoice_id = 0, $type = '') {
668 668
     global $invoicing;
669 669
     
670
-    if ( empty( $invoice_id ) ) {
670
+    if (empty($invoice_id)) {
671 671
         return NULL;
672 672
     }
673 673
     
674
-    $notes = $invoicing->notes->get_invoice_notes( $invoice_id, $type );
674
+    $notes = $invoicing->notes->get_invoice_notes($invoice_id, $type);
675 675
     
676
-    return apply_filters( 'wpinv_invoice_notes', $notes, $invoice_id, $type );
676
+    return apply_filters('wpinv_invoice_notes', $notes, $invoice_id, $type);
677 677
 }
678 678
 
679
-function wpinv_get_payment_key( $invoice_id = 0 ) {
680
-	$invoice = new WPInv_Invoice( $invoice_id );
679
+function wpinv_get_payment_key($invoice_id = 0) {
680
+	$invoice = new WPInv_Invoice($invoice_id);
681 681
     return $invoice->get_key();
682 682
 }
683 683
 
684
-function wpinv_get_invoice_number( $invoice_id = 0 ) {
685
-    $invoice = new WPInv_Invoice( $invoice_id );
684
+function wpinv_get_invoice_number($invoice_id = 0) {
685
+    $invoice = new WPInv_Invoice($invoice_id);
686 686
     return $invoice->get_number();
687 687
 }
688 688
 
689
-function wpinv_get_cart_discountable_subtotal( $code_id ) {
689
+function wpinv_get_cart_discountable_subtotal($code_id) {
690 690
     $cart_items = wpinv_get_cart_content_details();
691 691
     $items      = array();
692 692
 
693
-    $excluded_items = wpinv_get_discount_excluded_items( $code_id );
693
+    $excluded_items = wpinv_get_discount_excluded_items($code_id);
694 694
 
695
-    if( $cart_items ) {
695
+    if ($cart_items) {
696 696
 
697
-        foreach( $cart_items as $item ) {
697
+        foreach ($cart_items as $item) {
698 698
 
699
-            if( ! in_array( $item['id'], $excluded_items ) ) {
700
-                $items[] =  $item;
699
+            if (!in_array($item['id'], $excluded_items)) {
700
+                $items[] = $item;
701 701
             }
702 702
         }
703 703
     }
704 704
 
705
-    $subtotal = wpinv_get_cart_items_subtotal( $items );
705
+    $subtotal = wpinv_get_cart_items_subtotal($items);
706 706
 
707
-    return apply_filters( 'wpinv_get_cart_discountable_subtotal', $subtotal );
707
+    return apply_filters('wpinv_get_cart_discountable_subtotal', $subtotal);
708 708
 }
709 709
 
710
-function wpinv_get_cart_items_subtotal( $items ) {
710
+function wpinv_get_cart_items_subtotal($items) {
711 711
     $subtotal = 0.00;
712 712
 
713
-    if ( is_array( $items ) && ! empty( $items ) ) {
714
-        $prices = wp_list_pluck( $items, 'subtotal' );
713
+    if (is_array($items) && !empty($items)) {
714
+        $prices = wp_list_pluck($items, 'subtotal');
715 715
 
716
-        if( is_array( $prices ) ) {
717
-            $subtotal = array_sum( $prices );
716
+        if (is_array($prices)) {
717
+            $subtotal = array_sum($prices);
718 718
         } else {
719 719
             $subtotal = 0.00;
720 720
         }
721 721
 
722
-        if( $subtotal < 0 ) {
722
+        if ($subtotal < 0) {
723 723
             $subtotal = 0.00;
724 724
         }
725 725
     }
726 726
 
727
-    return apply_filters( 'wpinv_get_cart_items_subtotal', $subtotal );
727
+    return apply_filters('wpinv_get_cart_items_subtotal', $subtotal);
728 728
 }
729 729
 
730
-function wpinv_get_cart_subtotal( $items = array() ) {
731
-    $items    = !empty( $items ) ? $items : wpinv_get_cart_content_details();
732
-    $subtotal = wpinv_get_cart_items_subtotal( $items );
730
+function wpinv_get_cart_subtotal($items = array()) {
731
+    $items    = !empty($items) ? $items : wpinv_get_cart_content_details();
732
+    $subtotal = wpinv_get_cart_items_subtotal($items);
733 733
 
734
-    return apply_filters( 'wpinv_get_cart_subtotal', $subtotal );
734
+    return apply_filters('wpinv_get_cart_subtotal', $subtotal);
735 735
 }
736 736
 
737
-function wpinv_cart_subtotal( $items = array() ) {
738
-    $price = wpinv_price( wpinv_format_amount( wpinv_get_cart_subtotal( $items ) ) );
737
+function wpinv_cart_subtotal($items = array()) {
738
+    $price = wpinv_price(wpinv_format_amount(wpinv_get_cart_subtotal($items)));
739 739
 
740 740
     return $price;
741 741
 }
742 742
 
743
-function wpinv_get_cart_total( $items = array(), $discounts = false, $invoice = array() ) {
744
-    $subtotal  = (float)wpinv_get_cart_subtotal( $items );
745
-    $discounts = (float)wpinv_get_cart_discounted_amount( $items );
746
-    $cart_tax  = (float)wpinv_get_cart_tax( $items );
743
+function wpinv_get_cart_total($items = array(), $discounts = false, $invoice = array()) {
744
+    $subtotal  = (float)wpinv_get_cart_subtotal($items);
745
+    $discounts = (float)wpinv_get_cart_discounted_amount($items);
746
+    $cart_tax  = (float)wpinv_get_cart_tax($items);
747 747
     $fees      = (float)wpinv_get_cart_fee_total();
748
-    if ( !empty( $invoice ) && $invoice->is_free_trial() ) {
748
+    if (!empty($invoice) && $invoice->is_free_trial()) {
749 749
         $total = 0;
750 750
     } else {
751
-        $total     = $subtotal - $discounts + $cart_tax + $fees;
751
+        $total = $subtotal - $discounts + $cart_tax + $fees;
752 752
     }
753 753
 
754
-    if ( $total < 0 ) {
754
+    if ($total < 0) {
755 755
         $total = 0.00;
756 756
     }
757 757
     
758
-    $total = (float)apply_filters( 'wpinv_get_cart_total', $total, $items );
758
+    $total = (float)apply_filters('wpinv_get_cart_total', $total, $items);
759 759
 
760
-    return wpinv_sanitize_amount( $total );
760
+    return wpinv_sanitize_amount($total);
761 761
 }
762 762
 
763
-function wpinv_cart_total( $cart_items = array(), $echo = true, $invoice = array() ) {
763
+function wpinv_cart_total($cart_items = array(), $echo = true, $invoice = array()) {
764 764
     global $cart_total;
765
-    $total = wpinv_price( wpinv_format_amount( wpinv_get_cart_total( $cart_items, NULL, $invoice ) ) );
766
-    $total = apply_filters( 'wpinv_cart_total', $total, $cart_items, $invoice );
765
+    $total = wpinv_price(wpinv_format_amount(wpinv_get_cart_total($cart_items, NULL, $invoice)));
766
+    $total = apply_filters('wpinv_cart_total', $total, $cart_items, $invoice);
767 767
     
768 768
     $cart_total = $total;
769 769
 
770
-    if ( !$echo ) {
770
+    if (!$echo) {
771 771
         return $total;
772 772
     }
773 773
 
774 774
     echo $total;
775 775
 }
776 776
 
777
-function wpinv_get_cart_tax( $items = array() ) {
777
+function wpinv_get_cart_tax($items = array()) {
778 778
     $cart_tax = 0;
779
-    $items    = !empty( $items ) ? $items : wpinv_get_cart_content_details();
779
+    $items    = !empty($items) ? $items : wpinv_get_cart_content_details();
780 780
 
781
-    if ( $items ) {
782
-        $taxes = wp_list_pluck( $items, 'tax' );
781
+    if ($items) {
782
+        $taxes = wp_list_pluck($items, 'tax');
783 783
 
784
-        if( is_array( $taxes ) ) {
785
-            $cart_tax = array_sum( $taxes );
784
+        if (is_array($taxes)) {
785
+            $cart_tax = array_sum($taxes);
786 786
         }
787 787
     }
788 788
 
789 789
     $cart_tax += wpinv_get_cart_fee_tax();
790 790
 
791
-    return apply_filters( 'wpinv_get_cart_tax', wpinv_sanitize_amount( $cart_tax ) );
791
+    return apply_filters('wpinv_get_cart_tax', wpinv_sanitize_amount($cart_tax));
792 792
 }
793 793
 
794
-function wpinv_cart_tax( $items = array(), $echo = false ) {
795
-    $cart_tax = wpinv_get_cart_tax( $items );
796
-    $cart_tax = wpinv_price( wpinv_format_amount( $cart_tax ) );
794
+function wpinv_cart_tax($items = array(), $echo = false) {
795
+    $cart_tax = wpinv_get_cart_tax($items);
796
+    $cart_tax = wpinv_price(wpinv_format_amount($cart_tax));
797 797
 
798
-    $tax = apply_filters( 'wpinv_cart_tax', $cart_tax, $items );
798
+    $tax = apply_filters('wpinv_cart_tax', $cart_tax, $items);
799 799
 
800
-    if ( !$echo ) {
800
+    if (!$echo) {
801 801
         return $tax;
802 802
     }
803 803
 
804 804
     echo $tax;
805 805
 }
806 806
 
807
-function wpinv_get_cart_discount_code( $items = array() ) {
807
+function wpinv_get_cart_discount_code($items = array()) {
808 808
     $invoice = wpinv_get_invoice_cart();
809
-    $cart_discount_code = !empty( $invoice ) ? $invoice->get_discount_code() : '';
809
+    $cart_discount_code = !empty($invoice) ? $invoice->get_discount_code() : '';
810 810
     
811
-    return apply_filters( 'wpinv_get_cart_discount_code', $cart_discount_code );
811
+    return apply_filters('wpinv_get_cart_discount_code', $cart_discount_code);
812 812
 }
813 813
 
814
-function wpinv_cart_discount_code( $items = array(), $echo = false ) {
815
-    $cart_discount_code = wpinv_get_cart_discount_code( $items );
814
+function wpinv_cart_discount_code($items = array(), $echo = false) {
815
+    $cart_discount_code = wpinv_get_cart_discount_code($items);
816 816
 
817
-    if ( $cart_discount_code != '' ) {
817
+    if ($cart_discount_code != '') {
818 818
         $cart_discount_code = ' (' . $cart_discount_code . ')';
819 819
     }
820 820
     
821
-    $discount_code = apply_filters( 'wpinv_cart_discount_code', $cart_discount_code, $items );
821
+    $discount_code = apply_filters('wpinv_cart_discount_code', $cart_discount_code, $items);
822 822
 
823
-    if ( !$echo ) {
823
+    if (!$echo) {
824 824
         return $discount_code;
825 825
     }
826 826
 
827 827
     echo $discount_code;
828 828
 }
829 829
 
830
-function wpinv_get_cart_discount( $items = array() ) {
830
+function wpinv_get_cart_discount($items = array()) {
831 831
     $invoice = wpinv_get_invoice_cart();
832
-    $cart_discount = !empty( $invoice ) ? $invoice->get_discount() : 0;
832
+    $cart_discount = !empty($invoice) ? $invoice->get_discount() : 0;
833 833
     
834
-    return apply_filters( 'wpinv_get_cart_discount', wpinv_sanitize_amount( $cart_discount ), $items );
834
+    return apply_filters('wpinv_get_cart_discount', wpinv_sanitize_amount($cart_discount), $items);
835 835
 }
836 836
 
837
-function wpinv_cart_discount( $items = array(), $echo = false ) {
838
-    $cart_discount = wpinv_get_cart_discount( $items );
839
-    $cart_discount = wpinv_price( wpinv_format_amount( $cart_discount ) );
837
+function wpinv_cart_discount($items = array(), $echo = false) {
838
+    $cart_discount = wpinv_get_cart_discount($items);
839
+    $cart_discount = wpinv_price(wpinv_format_amount($cart_discount));
840 840
 
841
-    $discount = apply_filters( 'wpinv_cart_discount', $cart_discount, $items );
841
+    $discount = apply_filters('wpinv_cart_discount', $cart_discount, $items);
842 842
 
843
-    if ( !$echo ) {
843
+    if (!$echo) {
844 844
         return $discount;
845 845
     }
846 846
 
847 847
     echo $discount;
848 848
 }
849 849
 
850
-function wpinv_get_cart_fees( $type = 'all', $item_id = 0 ) {
851
-    $item = new WPInv_Item( $item_id );
850
+function wpinv_get_cart_fees($type = 'all', $item_id = 0) {
851
+    $item = new WPInv_Item($item_id);
852 852
     
853
-    return $item->get_fees( $type, $item_id );
853
+    return $item->get_fees($type, $item_id);
854 854
 }
855 855
 
856 856
 function wpinv_get_cart_fee_total() {
857
-    $total  = 0;
857
+    $total = 0;
858 858
     $fees = wpinv_get_cart_fees();
859 859
     
860
-    if ( $fees ) {
861
-        foreach ( $fees as $fee_id => $fee ) {
860
+    if ($fees) {
861
+        foreach ($fees as $fee_id => $fee) {
862 862
             $total += $fee['amount'];
863 863
         }
864 864
     }
865 865
 
866
-    return apply_filters( 'wpinv_get_cart_fee_total', $total );
866
+    return apply_filters('wpinv_get_cart_fee_total', $total);
867 867
 }
868 868
 
869 869
 function wpinv_get_cart_fee_tax() {
870 870
     $tax  = 0;
871 871
     $fees = wpinv_get_cart_fees();
872 872
 
873
-    if ( $fees ) {
874
-        foreach ( $fees as $fee_id => $fee ) {
875
-            if( ! empty( $fee['no_tax'] ) ) {
873
+    if ($fees) {
874
+        foreach ($fees as $fee_id => $fee) {
875
+            if (!empty($fee['no_tax'])) {
876 876
                 continue;
877 877
             }
878 878
 
879
-            $tax += wpinv_calculate_tax( $fee['amount'] );
879
+            $tax += wpinv_calculate_tax($fee['amount']);
880 880
         }
881 881
     }
882 882
 
883
-    return apply_filters( 'wpinv_get_cart_fee_tax', $tax );
883
+    return apply_filters('wpinv_get_cart_fee_tax', $tax);
884 884
 }
885 885
 
886 886
 function wpinv_cart_has_recurring_item() {
887 887
     $cart_items = wpinv_get_cart_contents();
888 888
     
889
-    if ( empty( $cart_items ) ) {
889
+    if (empty($cart_items)) {
890 890
         return false;
891 891
     }
892 892
     
893 893
     $has_subscription = false;
894
-    foreach( $cart_items as $cart_item ) {
895
-        if ( !empty( $cart_item['id'] ) && wpinv_is_recurring_item( $cart_item['id'] )  ) {
894
+    foreach ($cart_items as $cart_item) {
895
+        if (!empty($cart_item['id']) && wpinv_is_recurring_item($cart_item['id'])) {
896 896
             $has_subscription = true;
897 897
             break;
898 898
         }
899 899
     }
900 900
     
901
-    return apply_filters( 'wpinv_cart_has_recurring_item', $has_subscription, $cart_items );
901
+    return apply_filters('wpinv_cart_has_recurring_item', $has_subscription, $cart_items);
902 902
 }
903 903
 
904 904
 function wpinv_cart_has_free_trial() {
@@ -906,97 +906,97 @@  discard block
 block discarded – undo
906 906
     
907 907
     $free_trial = false;
908 908
     
909
-    if ( !empty( $invoice ) && $invoice->is_free_trial() ) {
909
+    if (!empty($invoice) && $invoice->is_free_trial()) {
910 910
         $free_trial = true;
911 911
     }
912 912
     
913
-    return apply_filters( 'wpinv_cart_has_free_trial', $free_trial, $invoice );
913
+    return apply_filters('wpinv_cart_has_free_trial', $free_trial, $invoice);
914 914
 }
915 915
 
916 916
 function wpinv_get_cart_contents() {
917 917
     $cart_details = wpinv_get_cart_details();
918 918
     
919
-    return apply_filters( 'wpinv_get_cart_contents', $cart_details );
919
+    return apply_filters('wpinv_get_cart_contents', $cart_details);
920 920
 }
921 921
 
922 922
 function wpinv_get_cart_content_details() {
923 923
     global $wpinv_euvat, $wpi_current_id, $wpi_item_id, $wpinv_is_last_cart_item, $wpinv_flat_discount_total;
924 924
     $cart_items = wpinv_get_cart_contents();
925 925
     
926
-    if ( empty( $cart_items ) ) {
926
+    if (empty($cart_items)) {
927 927
         return false;
928 928
     }
929 929
     $invoice = wpinv_get_invoice_cart();
930
-	if ( empty( $invoice ) ) {
930
+	if (empty($invoice)) {
931 931
         return false;
932 932
     }
933 933
 
934 934
     $details = array();
935
-    $length  = count( $cart_items ) - 1;
935
+    $length  = count($cart_items) - 1;
936 936
     
937
-    if ( empty( $_POST['country'] ) ) {
937
+    if (empty($_POST['country'])) {
938 938
         $_POST['country'] = $invoice->country;
939 939
     }
940
-    if ( !isset( $_POST['state'] ) ) {
940
+    if (!isset($_POST['state'])) {
941 941
         $_POST['state'] = $invoice->state;
942 942
     }
943 943
 
944
-    foreach( $cart_items as $key => $item ) {
945
-        $item_id            = isset( $item['id'] ) ? sanitize_text_field( $item['id'] ) : '';
946
-        if ( empty( $item_id ) ) {
944
+    foreach ($cart_items as $key => $item) {
945
+        $item_id = isset($item['id']) ? sanitize_text_field($item['id']) : '';
946
+        if (empty($item_id)) {
947 947
             continue;
948 948
         }
949 949
         
950 950
         $wpi_current_id         = $invoice->ID;
951 951
         $wpi_item_id            = $item_id;
952 952
         
953
-        if ( isset( $item['custom_price'] ) && $item['custom_price'] !== '' ) {
953
+        if (isset($item['custom_price']) && $item['custom_price'] !== '') {
954 954
             $item_price = $item['custom_price'];
955 955
         } else {
956
-            if ( isset( $item['item_price'] ) && $item['item_price'] !== '' && $item['item_price'] !== false ) {
956
+            if (isset($item['item_price']) && $item['item_price'] !== '' && $item['item_price'] !== false) {
957 957
                 $item_price = $item['item_price'];
958 958
             } else {
959
-                $item_price = wpinv_get_item_price( $item_id );
959
+                $item_price = wpinv_get_item_price($item_id);
960 960
             }
961 961
         }
962
-        $discount           = wpinv_get_cart_item_discount_amount( $item );
963
-        $discount           = apply_filters( 'wpinv_get_cart_content_details_item_discount_amount', $discount, $item );
964
-        $quantity           = wpinv_get_cart_item_quantity( $item );
965
-        $fees               = wpinv_get_cart_fees( 'fee', $item_id );
962
+        $discount           = wpinv_get_cart_item_discount_amount($item);
963
+        $discount           = apply_filters('wpinv_get_cart_content_details_item_discount_amount', $discount, $item);
964
+        $quantity           = wpinv_get_cart_item_quantity($item);
965
+        $fees               = wpinv_get_cart_fees('fee', $item_id);
966 966
         
967 967
         $subtotal           = $item_price * $quantity;
968
-        $tax_rate           = wpinv_get_tax_rate( $_POST['country'], $_POST['state'], $wpi_item_id );
969
-        $tax_class          = $wpinv_euvat->get_item_class( $item_id );
970
-        $tax                = wpinv_get_cart_item_tax( $item_id, $subtotal - $discount );
968
+        $tax_rate           = wpinv_get_tax_rate($_POST['country'], $_POST['state'], $wpi_item_id);
969
+        $tax_class          = $wpinv_euvat->get_item_class($item_id);
970
+        $tax                = wpinv_get_cart_item_tax($item_id, $subtotal - $discount);
971 971
         
972
-        if ( wpinv_prices_include_tax() ) {
973
-            $subtotal -= wpinv_round_amount( $tax );
972
+        if (wpinv_prices_include_tax()) {
973
+            $subtotal -= wpinv_round_amount($tax);
974 974
         }
975 975
         
976
-        $total              = $subtotal - $discount + $tax;
976
+        $total = $subtotal - $discount + $tax;
977 977
         
978 978
         // Do not allow totals to go negatve
979
-        if( $total < 0 ) {
979
+        if ($total < 0) {
980 980
             $total = 0;
981 981
         }
982 982
         
983
-        $details[ $key ]  = array(
983
+        $details[$key] = array(
984 984
             'id'                => $item_id,
985
-            'name'              => !empty($item['name']) ? $item['name'] : get_the_title( $item_id ),
986
-            'item_price'        => wpinv_round_amount( $item_price ),
987
-            'custom_price'      => isset( $item['custom_price'] ) ? $item['custom_price'] : '',
985
+            'name'              => !empty($item['name']) ? $item['name'] : get_the_title($item_id),
986
+            'item_price'        => wpinv_round_amount($item_price),
987
+            'custom_price'      => isset($item['custom_price']) ? $item['custom_price'] : '',
988 988
             'quantity'          => $quantity,
989
-            'discount'          => wpinv_round_amount( $discount ),
990
-            'subtotal'          => wpinv_round_amount( $subtotal ),
991
-            'tax'               => wpinv_round_amount( $tax ),
992
-            'price'             => wpinv_round_amount( $total ),
989
+            'discount'          => wpinv_round_amount($discount),
990
+            'subtotal'          => wpinv_round_amount($subtotal),
991
+            'tax'               => wpinv_round_amount($tax),
992
+            'price'             => wpinv_round_amount($total),
993 993
             'vat_rates_class'   => $tax_class,
994 994
             'vat_rate'          => $tax_rate,
995
-            'meta'              => isset( $item['meta'] ) ? $item['meta'] : array(),
995
+            'meta'              => isset($item['meta']) ? $item['meta'] : array(),
996 996
             'fees'              => $fees,
997 997
         );
998 998
         
999
-        if ( $wpinv_is_last_cart_item ) {
999
+        if ($wpinv_is_last_cart_item) {
1000 1000
             $wpinv_is_last_cart_item   = false;
1001 1001
             $wpinv_flat_discount_total = 0.00;
1002 1002
         }
@@ -1005,67 +1005,67 @@  discard block
 block discarded – undo
1005 1005
     return $details;
1006 1006
 }
1007 1007
 
1008
-function wpinv_get_cart_details( $invoice_id = 0 ) {
1008
+function wpinv_get_cart_details($invoice_id = 0) {
1009 1009
     global $ajax_cart_details;
1010 1010
 
1011
-    $invoice      = wpinv_get_invoice_cart( $invoice_id );
1011
+    $invoice      = wpinv_get_invoice_cart($invoice_id);
1012 1012
     $cart_details = $ajax_cart_details;
1013
-    if ( empty( $cart_details ) && ! empty( $invoice->cart_details ) ) {
1013
+    if (empty($cart_details) && !empty($invoice->cart_details)) {
1014 1014
         $cart_details = $invoice->cart_details;
1015 1015
     }
1016 1016
 
1017
-    if ( ! empty( $cart_details ) && is_array( $cart_details ) ) {
1018
-        $invoice_currency = ! empty( $invoice->currency ) ? $invoice->currency : wpinv_get_default_country();
1017
+    if (!empty($cart_details) && is_array($cart_details)) {
1018
+        $invoice_currency = !empty($invoice->currency) ? $invoice->currency : wpinv_get_default_country();
1019 1019
 
1020
-        foreach ( $cart_details as $key => $cart_item ) {
1021
-            $cart_details[ $key ]['currency'] = $invoice_currency;
1020
+        foreach ($cart_details as $key => $cart_item) {
1021
+            $cart_details[$key]['currency'] = $invoice_currency;
1022 1022
 
1023
-            if ( ! isset( $cart_item['subtotal'] ) ) {
1024
-                $cart_details[ $key ]['subtotal'] = $cart_item['price'];
1023
+            if (!isset($cart_item['subtotal'])) {
1024
+                $cart_details[$key]['subtotal'] = $cart_item['price'];
1025 1025
             }
1026 1026
         }
1027 1027
     }
1028 1028
 
1029
-    return apply_filters( 'wpinv_get_cart_details', $cart_details, $invoice_id );
1029
+    return apply_filters('wpinv_get_cart_details', $cart_details, $invoice_id);
1030 1030
 }
1031 1031
 
1032
-function wpinv_record_status_change( $invoice_id, $new_status, $old_status ) {
1033
-    if ( 'wpi_invoice' != get_post_type( $invoice_id ) ) {
1032
+function wpinv_record_status_change($invoice_id, $new_status, $old_status) {
1033
+    if ('wpi_invoice' != get_post_type($invoice_id)) {
1034 1034
         return;
1035 1035
     }
1036 1036
 
1037
-    if ( ( $old_status == 'wpi-pending' && $new_status == 'draft' ) || ( $old_status == 'draft' && $new_status == 'wpi-pending' ) ) {
1037
+    if (($old_status == 'wpi-pending' && $new_status == 'draft') || ($old_status == 'draft' && $new_status == 'wpi-pending')) {
1038 1038
         return;
1039 1039
     }
1040 1040
 
1041
-    $invoice    = wpinv_get_invoice( $invoice_id );
1041
+    $invoice    = wpinv_get_invoice($invoice_id);
1042 1042
     
1043
-    $old_status = wpinv_status_nicename( $old_status );
1044
-    $new_status = wpinv_status_nicename( $new_status );
1043
+    $old_status = wpinv_status_nicename($old_status);
1044
+    $new_status = wpinv_status_nicename($new_status);
1045 1045
 
1046
-    $status_change = sprintf( __( 'Invoice status changed from %s to %s', 'invoicing' ), $old_status, $new_status );
1046
+    $status_change = sprintf(__('Invoice status changed from %s to %s', 'invoicing'), $old_status, $new_status);
1047 1047
     
1048 1048
     // Add note
1049
-    return $invoice->add_note( $status_change, false, false, true );
1049
+    return $invoice->add_note($status_change, false, false, true);
1050 1050
 }
1051
-add_action( 'wpinv_update_status', 'wpinv_record_status_change', 100, 3 );
1051
+add_action('wpinv_update_status', 'wpinv_record_status_change', 100, 3);
1052 1052
 
1053
-function wpinv_complete_payment( $invoice_id, $new_status, $old_status ) {
1053
+function wpinv_complete_payment($invoice_id, $new_status, $old_status) {
1054 1054
     global $wpi_has_free_trial;
1055 1055
     
1056 1056
     $wpi_has_free_trial = false;
1057 1057
     
1058
-    if ( $old_status == 'publish' ) {
1058
+    if ($old_status == 'publish') {
1059 1059
         return; // Make sure that payments are only paid once
1060 1060
     }
1061 1061
 
1062 1062
     // Make sure the payment completion is only processed when new status is paid
1063
-    if ( $new_status != 'publish' ) {
1063
+    if ($new_status != 'publish') {
1064 1064
         return;
1065 1065
     }
1066 1066
 
1067
-    $invoice = new WPInv_Invoice( $invoice_id );
1068
-    if ( empty( $invoice ) ) {
1067
+    $invoice = new WPInv_Invoice($invoice_id);
1068
+    if (empty($invoice)) {
1069 1069
         return;
1070 1070
     }
1071 1071
 
@@ -1073,58 +1073,58 @@  discard block
 block discarded – undo
1073 1073
     $completed_date = $invoice->completed_date;
1074 1074
     $cart_details   = $invoice->cart_details;
1075 1075
 
1076
-    do_action( 'wpinv_pre_complete_payment', $invoice_id );
1076
+    do_action('wpinv_pre_complete_payment', $invoice_id);
1077 1077
 
1078
-    if ( is_array( $cart_details ) ) {
1078
+    if (is_array($cart_details)) {
1079 1079
         // Increase purchase count and earnings
1080
-        foreach ( $cart_details as $cart_index => $item ) {
1080
+        foreach ($cart_details as $cart_index => $item) {
1081 1081
             // Ensure these actions only run once, ever
1082
-            if ( empty( $completed_date ) ) {
1083
-                do_action( 'wpinv_complete_item_payment', $item['id'], $invoice_id, $item, $cart_index );
1082
+            if (empty($completed_date)) {
1083
+                do_action('wpinv_complete_item_payment', $item['id'], $invoice_id, $item, $cart_index);
1084 1084
             }
1085 1085
         }
1086 1086
     }
1087 1087
     
1088 1088
     // Check for discount codes and increment their use counts
1089
-    if ( $discounts = $invoice->get_discounts( true ) ) {
1090
-        if( ! empty( $discounts ) ) {
1091
-            foreach( $discounts as $code ) {
1092
-                wpinv_increase_discount_usage( $code );
1089
+    if ($discounts = $invoice->get_discounts(true)) {
1090
+        if (!empty($discounts)) {
1091
+            foreach ($discounts as $code) {
1092
+                wpinv_increase_discount_usage($code);
1093 1093
             }
1094 1094
         }
1095 1095
     }
1096 1096
     
1097 1097
     // Ensure this action only runs once ever
1098
-    if( empty( $completed_date ) ) {
1098
+    if (empty($completed_date)) {
1099 1099
         // Save the completed date
1100
-        $invoice->set( 'completed_date', current_time( 'mysql', 0 ) );
1100
+        $invoice->set('completed_date', current_time('mysql', 0));
1101 1101
         $invoice->save();
1102 1102
 
1103
-        do_action( 'wpinv_complete_payment', $invoice_id );
1103
+        do_action('wpinv_complete_payment', $invoice_id);
1104 1104
     }
1105 1105
 
1106 1106
     // Empty the shopping cart
1107 1107
     wpinv_empty_cart();
1108 1108
 }
1109
-add_action( 'wpinv_update_status', 'wpinv_complete_payment', 100, 3 );
1109
+add_action('wpinv_update_status', 'wpinv_complete_payment', 100, 3);
1110 1110
 
1111
-function wpinv_update_payment_status( $invoice_id, $new_status = 'publish' ) {    
1112
-    $invoice = !empty( $invoice_id ) && is_object( $invoice_id ) ? $invoice_id : wpinv_get_invoice( (int)$invoice_id );
1111
+function wpinv_update_payment_status($invoice_id, $new_status = 'publish') {    
1112
+    $invoice = !empty($invoice_id) && is_object($invoice_id) ? $invoice_id : wpinv_get_invoice((int)$invoice_id);
1113 1113
     
1114
-    if ( empty( $invoice ) ) {
1114
+    if (empty($invoice)) {
1115 1115
         return false;
1116 1116
     }
1117 1117
     
1118
-    return $invoice->update_status( $new_status );
1118
+    return $invoice->update_status($new_status);
1119 1119
 }
1120 1120
 
1121
-function wpinv_cart_has_fees( $type = 'all' ) {
1121
+function wpinv_cart_has_fees($type = 'all') {
1122 1122
     return false;
1123 1123
 }
1124 1124
 
1125 1125
 function wpinv_validate_checkout_fields() {    
1126 1126
     // Check if there is $_POST
1127
-    if ( empty( $_POST ) ) {
1127
+    if (empty($_POST)) {
1128 1128
         return false;
1129 1129
     }
1130 1130
     
@@ -1136,11 +1136,11 @@  discard block
 block discarded – undo
1136 1136
     );
1137 1137
     
1138 1138
     // Validate agree to terms
1139
-    $page = wpinv_get_option( 'tandc_page' );
1140
-    if(isset($page) && (int)$page > 0 && apply_filters( 'wpinv_checkout_show_terms', true )){
1139
+    $page = wpinv_get_option('tandc_page');
1140
+    if (isset($page) && (int)$page > 0 && apply_filters('wpinv_checkout_show_terms', true)) {
1141 1141
         // Validate agree to terms
1142
-        if ( ! isset( $_POST['wpi_terms'] ) || !$_POST['wpi_terms'] ) {
1143
-            wpinv_set_error( 'accept_terms', apply_filters( 'wpinv_accept_terms_error_text', __( 'You must accept terms and conditions', 'invoicing' ) ) );
1142
+        if (!isset($_POST['wpi_terms']) || !$_POST['wpi_terms']) {
1143
+            wpinv_set_error('accept_terms', apply_filters('wpinv_accept_terms_error_text', __('You must accept terms and conditions', 'invoicing')));
1144 1144
         }
1145 1145
     }
1146 1146
     
@@ -1156,26 +1156,26 @@  discard block
 block discarded – undo
1156 1156
     
1157 1157
     $invoice = wpinv_get_invoice_cart();
1158 1158
     $has_subscription = $invoice->is_recurring();
1159
-    if ( empty( $invoice ) ) {
1160
-        wpinv_set_error( 'invalid_invoice', __( 'Your cart is empty.', 'invoicing' ) );
1159
+    if (empty($invoice)) {
1160
+        wpinv_set_error('invalid_invoice', __('Your cart is empty.', 'invoicing'));
1161 1161
         return $gateway;
1162 1162
     }
1163 1163
 
1164 1164
     // Check if a gateway value is present
1165
-    if ( !empty( $_REQUEST['wpi-gateway'] ) ) {
1166
-        $gateway = sanitize_text_field( $_REQUEST['wpi-gateway'] );
1165
+    if (!empty($_REQUEST['wpi-gateway'])) {
1166
+        $gateway = sanitize_text_field($_REQUEST['wpi-gateway']);
1167 1167
 
1168
-        if ( $invoice->is_free() ) {
1168
+        if ($invoice->is_free()) {
1169 1169
             $gateway = 'manual';
1170
-        } elseif ( !wpinv_is_gateway_active( $gateway ) ) {
1171
-            wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not enabled', 'invoicing' ) );
1172
-        } elseif ( $has_subscription && !wpinv_gateway_support_subscription( $gateway ) ) {
1173
-            wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway doesnot support subscription payment', 'invoicing' ) );
1170
+        } elseif (!wpinv_is_gateway_active($gateway)) {
1171
+            wpinv_set_error('invalid_gateway', __('The selected payment gateway is not enabled', 'invoicing'));
1172
+        } elseif ($has_subscription && !wpinv_gateway_support_subscription($gateway)) {
1173
+            wpinv_set_error('invalid_gateway', __('The selected payment gateway doesnot support subscription payment', 'invoicing'));
1174 1174
         }
1175 1175
     }
1176 1176
 
1177
-    if ( $has_subscription && count( wpinv_get_cart_contents() ) > 1 ) {
1178
-        wpinv_set_error( 'subscription_invalid', __( 'Only one subscription may be purchased through payment per checkout.', 'invoicing' ) );
1177
+    if ($has_subscription && count(wpinv_get_cart_contents()) > 1) {
1178
+        wpinv_set_error('subscription_invalid', __('Only one subscription may be purchased through payment per checkout.', 'invoicing'));
1179 1179
     }
1180 1180
 
1181 1181
     return $gateway;
@@ -1189,10 +1189,10 @@  discard block
 block discarded – undo
1189 1189
     
1190 1190
     $error = false;
1191 1191
     // If we have discounts, loop through them
1192
-    if ( ! empty( $discounts ) ) {
1193
-        foreach ( $discounts as $discount ) {
1192
+    if (!empty($discounts)) {
1193
+        foreach ($discounts as $discount) {
1194 1194
             // Check if valid
1195
-            if (  !wpinv_is_discount_valid( $discount, (int)$wpi_cart->get_user_id() ) ) {
1195
+            if (!wpinv_is_discount_valid($discount, (int)$wpi_cart->get_user_id())) {
1196 1196
                 // Discount is not valid
1197 1197
                 $error = true;
1198 1198
             }
@@ -1202,20 +1202,20 @@  discard block
 block discarded – undo
1202 1202
         return NULL;
1203 1203
     }
1204 1204
 
1205
-    if ( $error && !wpinv_get_errors() ) {
1206
-        wpinv_set_error( 'invalid_discount', __( 'Discount code you entered is invalid', 'invoicing' ) );
1205
+    if ($error && !wpinv_get_errors()) {
1206
+        wpinv_set_error('invalid_discount', __('Discount code you entered is invalid', 'invoicing'));
1207 1207
     }
1208 1208
 
1209
-    return implode( ',', $discounts );
1209
+    return implode(',', $discounts);
1210 1210
 }
1211 1211
 
1212 1212
 function wpinv_checkout_validate_cc() {
1213 1213
     $card_data = wpinv_checkout_get_cc_info();
1214 1214
 
1215 1215
     // Validate the card zip
1216
-    if ( !empty( $card_data['wpinv_zip'] ) ) {
1217
-        if ( !wpinv_checkout_validate_cc_zip( $card_data['wpinv_zip'], $card_data['wpinv_country'] ) ) {
1218
-            wpinv_set_error( 'invalid_cc_zip', __( 'The zip / postcode you entered for your billing address is invalid', 'invoicing' ) );
1216
+    if (!empty($card_data['wpinv_zip'])) {
1217
+        if (!wpinv_checkout_validate_cc_zip($card_data['wpinv_zip'], $card_data['wpinv_country'])) {
1218
+            wpinv_set_error('invalid_cc_zip', __('The zip / postcode you entered for your billing address is invalid', 'invoicing'));
1219 1219
         }
1220 1220
     }
1221 1221
 
@@ -1225,28 +1225,28 @@  discard block
 block discarded – undo
1225 1225
 
1226 1226
 function wpinv_checkout_get_cc_info() {
1227 1227
 	$cc_info = array();
1228
-	$cc_info['card_name']      = isset( $_POST['card_name'] )       ? sanitize_text_field( $_POST['card_name'] )       : '';
1229
-	$cc_info['card_number']    = isset( $_POST['card_number'] )     ? sanitize_text_field( $_POST['card_number'] )     : '';
1230
-	$cc_info['card_cvc']       = isset( $_POST['card_cvc'] )        ? sanitize_text_field( $_POST['card_cvc'] )        : '';
1231
-	$cc_info['card_exp_month'] = isset( $_POST['card_exp_month'] )  ? sanitize_text_field( $_POST['card_exp_month'] )  : '';
1232
-	$cc_info['card_exp_year']  = isset( $_POST['card_exp_year'] )   ? sanitize_text_field( $_POST['card_exp_year'] )   : '';
1233
-	$cc_info['card_address']   = isset( $_POST['wpinv_address'] )  ? sanitize_text_field( $_POST['wpinv_address'] ) : '';
1234
-	$cc_info['card_city']      = isset( $_POST['wpinv_city'] )     ? sanitize_text_field( $_POST['wpinv_city'] )    : '';
1235
-	$cc_info['card_state']     = isset( $_POST['wpinv_state'] )    ? sanitize_text_field( $_POST['wpinv_state'] )   : '';
1236
-	$cc_info['card_country']   = isset( $_POST['wpinv_country'] )  ? sanitize_text_field( $_POST['wpinv_country'] ) : '';
1237
-	$cc_info['card_zip']       = isset( $_POST['wpinv_zip'] )      ? sanitize_text_field( $_POST['wpinv_zip'] )     : '';
1228
+	$cc_info['card_name']      = isset($_POST['card_name']) ? sanitize_text_field($_POST['card_name']) : '';
1229
+	$cc_info['card_number']    = isset($_POST['card_number']) ? sanitize_text_field($_POST['card_number']) : '';
1230
+	$cc_info['card_cvc']       = isset($_POST['card_cvc']) ? sanitize_text_field($_POST['card_cvc']) : '';
1231
+	$cc_info['card_exp_month'] = isset($_POST['card_exp_month']) ? sanitize_text_field($_POST['card_exp_month']) : '';
1232
+	$cc_info['card_exp_year']  = isset($_POST['card_exp_year']) ? sanitize_text_field($_POST['card_exp_year']) : '';
1233
+	$cc_info['card_address']   = isset($_POST['wpinv_address']) ? sanitize_text_field($_POST['wpinv_address']) : '';
1234
+	$cc_info['card_city']      = isset($_POST['wpinv_city']) ? sanitize_text_field($_POST['wpinv_city']) : '';
1235
+	$cc_info['card_state']     = isset($_POST['wpinv_state']) ? sanitize_text_field($_POST['wpinv_state']) : '';
1236
+	$cc_info['card_country']   = isset($_POST['wpinv_country']) ? sanitize_text_field($_POST['wpinv_country']) : '';
1237
+	$cc_info['card_zip']       = isset($_POST['wpinv_zip']) ? sanitize_text_field($_POST['wpinv_zip']) : '';
1238 1238
 
1239 1239
 	// Return cc info
1240 1240
 	return $cc_info;
1241 1241
 }
1242 1242
 
1243
-function wpinv_checkout_validate_cc_zip( $zip = 0, $country_code = '' ) {
1243
+function wpinv_checkout_validate_cc_zip($zip = 0, $country_code = '') {
1244 1244
     $ret = false;
1245 1245
 
1246
-    if ( empty( $zip ) || empty( $country_code ) )
1246
+    if (empty($zip) || empty($country_code))
1247 1247
         return $ret;
1248 1248
 
1249
-    $country_code = strtoupper( $country_code );
1249
+    $country_code = strtoupper($country_code);
1250 1250
 
1251 1251
     $zip_regex = array(
1252 1252
         "AD" => "AD\d{3}",
@@ -1406,24 +1406,24 @@  discard block
 block discarded – undo
1406 1406
         "ZM" => "\d{5}"
1407 1407
     );
1408 1408
 
1409
-    if ( ! isset ( $zip_regex[ $country_code ] ) || preg_match( "/" . $zip_regex[ $country_code ] . "/i", $zip ) )
1409
+    if (!isset ($zip_regex[$country_code]) || preg_match("/" . $zip_regex[$country_code] . "/i", $zip))
1410 1410
         $ret = true;
1411 1411
 
1412
-    return apply_filters( 'wpinv_is_zip_valid', $ret, $zip, $country_code );
1412
+    return apply_filters('wpinv_is_zip_valid', $ret, $zip, $country_code);
1413 1413
 }
1414 1414
 
1415 1415
 function wpinv_checkout_validate_agree_to_terms() {
1416 1416
     // Validate agree to terms
1417
-    if ( ! isset( $_POST['wpi_agree_to_terms'] ) || $_POST['wpi_agree_to_terms'] != 1 ) {
1417
+    if (!isset($_POST['wpi_agree_to_terms']) || $_POST['wpi_agree_to_terms'] != 1) {
1418 1418
         // User did not agree
1419
-        wpinv_set_error( 'agree_to_terms', apply_filters( 'wpinv_agree_to_terms_text', __( 'You must agree to the terms of use', 'invoicing' ) ) );
1419
+        wpinv_set_error('agree_to_terms', apply_filters('wpinv_agree_to_terms_text', __('You must agree to the terms of use', 'invoicing')));
1420 1420
     }
1421 1421
 }
1422 1422
 
1423 1423
 function wpinv_checkout_validate_invoice_user() {
1424 1424
     global $wpi_cart, $user_ID;
1425 1425
 
1426
-    if(empty($wpi_cart)){
1426
+    if (empty($wpi_cart)) {
1427 1427
         $wpi_cart = wpinv_get_invoice_cart();
1428 1428
     }
1429 1429
 
@@ -1433,45 +1433,45 @@  discard block
 block discarded – undo
1433 1433
     );
1434 1434
 
1435 1435
     // If guest checkout allowed
1436
-    if ( !wpinv_require_login_to_checkout() ) {
1436
+    if (!wpinv_require_login_to_checkout()) {
1437 1437
         return $valid_user_data;
1438 1438
     }
1439 1439
     
1440 1440
     // Verify there is a user_ID
1441
-    if ( $user_ID == $invoice_user ) {
1441
+    if ($user_ID == $invoice_user) {
1442 1442
         // Get the logged in user data
1443
-        $user_data = get_userdata( $user_ID );
1444
-        $required_fields  = wpinv_checkout_required_fields();
1443
+        $user_data = get_userdata($user_ID);
1444
+        $required_fields = wpinv_checkout_required_fields();
1445 1445
 
1446 1446
         // Loop through required fields and show error messages
1447
-         if ( !empty( $required_fields ) ) {
1448
-            foreach ( $required_fields as $field_name => $value ) {
1449
-                if ( in_array( $value, $required_fields ) && empty( $_POST[ 'wpinv_' . $field_name ] ) ) {
1450
-                    wpinv_set_error( $value['error_id'], $value['error_message'] );
1447
+         if (!empty($required_fields)) {
1448
+            foreach ($required_fields as $field_name => $value) {
1449
+                if (in_array($value, $required_fields) && empty($_POST['wpinv_' . $field_name])) {
1450
+                    wpinv_set_error($value['error_id'], $value['error_message']);
1451 1451
                 }
1452 1452
             }
1453 1453
         }
1454 1454
 
1455 1455
         // Verify data
1456
-        if ( $user_data ) {
1456
+        if ($user_data) {
1457 1457
             // Collected logged in user data
1458 1458
             $valid_user_data = array(
1459 1459
                 'user_id'     => $user_ID,
1460
-                'email'       => isset( $_POST['wpinv_email'] ) ? sanitize_email( $_POST['wpinv_email'] ) : $user_data->user_email,
1461
-                'first_name'  => isset( $_POST['wpinv_first_name'] ) && ! empty( $_POST['wpinv_first_name'] ) ? sanitize_text_field( $_POST['wpinv_first_name'] ) : $user_data->first_name,
1462
-                'last_name'   => isset( $_POST['wpinv_last_name'] ) && ! empty( $_POST['wpinv_last_name']  ) ? sanitize_text_field( $_POST['wpinv_last_name']  ) : $user_data->last_name,
1460
+                'email'       => isset($_POST['wpinv_email']) ? sanitize_email($_POST['wpinv_email']) : $user_data->user_email,
1461
+                'first_name'  => isset($_POST['wpinv_first_name']) && !empty($_POST['wpinv_first_name']) ? sanitize_text_field($_POST['wpinv_first_name']) : $user_data->first_name,
1462
+                'last_name'   => isset($_POST['wpinv_last_name']) && !empty($_POST['wpinv_last_name']) ? sanitize_text_field($_POST['wpinv_last_name']) : $user_data->last_name,
1463 1463
             );
1464 1464
 
1465
-            if ( !empty( $_POST[ 'wpinv_email' ] ) && !is_email( $_POST[ 'wpinv_email' ] ) ) {
1466
-                wpinv_set_error( 'invalid_email', __( 'Please enter a valid email address', 'invoicing' ) );
1465
+            if (!empty($_POST['wpinv_email']) && !is_email($_POST['wpinv_email'])) {
1466
+                wpinv_set_error('invalid_email', __('Please enter a valid email address', 'invoicing'));
1467 1467
             }
1468 1468
         } else {
1469 1469
             // Set invalid user error
1470
-            wpinv_set_error( 'invalid_user', __( 'The user billing information is invalid', 'invoicing' ) );
1470
+            wpinv_set_error('invalid_user', __('The user billing information is invalid', 'invoicing'));
1471 1471
         }
1472 1472
     } else {
1473 1473
         // Set invalid user error
1474
-        wpinv_set_error( 'invalid_user_id', __( 'The invalid invoice user id', 'invoicing' ) );
1474
+        wpinv_set_error('invalid_user_id', __('The invalid invoice user id', 'invoicing'));
1475 1475
     }
1476 1476
 
1477 1477
     // Return user data
@@ -1483,27 +1483,27 @@  discard block
 block discarded – undo
1483 1483
 
1484 1484
     $data = array();
1485 1485
     
1486
-    if ( is_user_logged_in() ) {
1487
-        if ( !wpinv_require_login_to_checkout() || ( wpinv_require_login_to_checkout() && (int)$wpi_cart->get_user_id() === (int)get_current_user_id() ) ) {
1486
+    if (is_user_logged_in()) {
1487
+        if (!wpinv_require_login_to_checkout() || (wpinv_require_login_to_checkout() && (int)$wpi_cart->get_user_id() === (int)get_current_user_id())) {
1488 1488
             $data['user_id'] = (int)get_current_user_id();
1489 1489
         } else {
1490
-            wpinv_set_error( 'logged_in_only', __( 'You are not allowed to pay for this invoice', 'invoicing' ) );
1490
+            wpinv_set_error('logged_in_only', __('You are not allowed to pay for this invoice', 'invoicing'));
1491 1491
         }
1492 1492
     } else {
1493 1493
         // If guest checkout allowed
1494
-        if ( !wpinv_require_login_to_checkout() ) {
1494
+        if (!wpinv_require_login_to_checkout()) {
1495 1495
             $data['user_id'] = 0;
1496 1496
         } else {
1497
-            wpinv_set_error( 'logged_in_only', __( 'You must be logged in to pay for this invoice', 'invoicing' ) );
1497
+            wpinv_set_error('logged_in_only', __('You must be logged in to pay for this invoice', 'invoicing'));
1498 1498
         }
1499 1499
     }
1500 1500
 
1501 1501
     return $data;
1502 1502
 }
1503 1503
 
1504
-function wpinv_checkout_form_get_user( $valid_data = array() ) {
1504
+function wpinv_checkout_form_get_user($valid_data = array()) {
1505 1505
 
1506
-    if ( !empty( $valid_data['current_user']['user_id'] ) ) {
1506
+    if (!empty($valid_data['current_user']['user_id'])) {
1507 1507
         $user = $valid_data['current_user'];
1508 1508
     } else {
1509 1509
         // Set the valid invoice user
@@ -1511,7 +1511,7 @@  discard block
 block discarded – undo
1511 1511
     }
1512 1512
 
1513 1513
     // Verify invoice have an user
1514
-    if ( false === $user || empty( $user ) ) {
1514
+    if (false === $user || empty($user)) {
1515 1515
         return false;
1516 1516
     }
1517 1517
 
@@ -1528,11 +1528,11 @@  discard block
 block discarded – undo
1528 1528
         'zip',
1529 1529
     );
1530 1530
     
1531
-    foreach ( $address_fields as $field ) {
1532
-        $user[$field]  = !empty( $_POST['wpinv_' . $field] ) ? sanitize_text_field( $_POST['wpinv_' . $field] ) : false;
1531
+    foreach ($address_fields as $field) {
1532
+        $user[$field] = !empty($_POST['wpinv_' . $field]) ? sanitize_text_field($_POST['wpinv_' . $field]) : false;
1533 1533
         
1534
-        if ( !empty( $user['user_id'] ) && !empty( $valid_data['current_user']['user_id'] ) && $valid_data['current_user']['user_id'] == $valid_data['invoice_user']['user_id'] ) {
1535
-            update_user_meta( $user['user_id'], '_wpinv_' . $field, $user[$field] );
1534
+        if (!empty($user['user_id']) && !empty($valid_data['current_user']['user_id']) && $valid_data['current_user']['user_id'] == $valid_data['invoice_user']['user_id']) {
1535
+            update_user_meta($user['user_id'], '_wpinv_' . $field, $user[$field]);
1536 1536
         }
1537 1537
     }
1538 1538
 
@@ -1540,28 +1540,28 @@  discard block
 block discarded – undo
1540 1540
     return $user;
1541 1541
 }
1542 1542
 
1543
-function wpinv_set_checkout_session( $invoice_data = array() ) {
1543
+function wpinv_set_checkout_session($invoice_data = array()) {
1544 1544
     global $wpi_session;
1545 1545
     
1546
-    return $wpi_session->set( 'wpinv_checkout', $invoice_data );
1546
+    return $wpi_session->set('wpinv_checkout', $invoice_data);
1547 1547
 }
1548 1548
 
1549 1549
 function wpinv_get_checkout_session() {
1550 1550
 	global $wpi_session;
1551 1551
     
1552
-    return $wpi_session->get( 'wpinv_checkout' );
1552
+    return $wpi_session->get('wpinv_checkout');
1553 1553
 }
1554 1554
 
1555 1555
 function wpinv_empty_cart() {
1556 1556
     global $wpi_session;
1557 1557
 
1558 1558
     // Remove cart contents
1559
-    $wpi_session->set( 'wpinv_checkout', NULL );
1559
+    $wpi_session->set('wpinv_checkout', NULL);
1560 1560
 
1561 1561
     // Remove all cart fees
1562
-    $wpi_session->set( 'wpi_cart_fees', NULL );
1562
+    $wpi_session->set('wpi_cart_fees', NULL);
1563 1563
 
1564
-    do_action( 'wpinv_empty_cart' );
1564
+    do_action('wpinv_empty_cart');
1565 1565
 }
1566 1566
 
1567 1567
 function wpinv_process_checkout() {
@@ -1570,7 +1570,7 @@  discard block
 block discarded – undo
1570 1570
     wpinv_clear_errors();
1571 1571
     
1572 1572
     $invoice = wpinv_get_invoice_cart();
1573
-    if ( empty( $invoice ) ) {
1573
+    if (empty($invoice)) {
1574 1574
         return false;
1575 1575
     }
1576 1576
     
@@ -1578,42 +1578,42 @@  discard block
 block discarded – undo
1578 1578
     
1579 1579
     $wpi_checkout_id = $invoice->ID;
1580 1580
     
1581
-    do_action( 'wpinv_pre_process_checkout' );
1581
+    do_action('wpinv_pre_process_checkout');
1582 1582
     
1583
-    if ( !wpinv_get_cart_contents() ) { // Make sure the cart isn't empty
1583
+    if (!wpinv_get_cart_contents()) { // Make sure the cart isn't empty
1584 1584
         $valid_data = false;
1585
-        wpinv_set_error( 'empty_cart', __( 'Your cart is empty', 'invoicing' ) );
1585
+        wpinv_set_error('empty_cart', __('Your cart is empty', 'invoicing'));
1586 1586
     } else {
1587 1587
         // Validate the form $_POST data
1588 1588
         $valid_data = wpinv_validate_checkout_fields();
1589 1589
         
1590 1590
         // Allow themes and plugins to hook to errors
1591
-        do_action( 'wpinv_checkout_error_checks', $valid_data, $_POST );
1591
+        do_action('wpinv_checkout_error_checks', $valid_data, $_POST);
1592 1592
     }
1593 1593
     
1594
-    $is_ajax    = defined( 'DOING_AJAX' ) && DOING_AJAX;
1594
+    $is_ajax = defined('DOING_AJAX') && DOING_AJAX;
1595 1595
     
1596 1596
     // Validate the user
1597
-    $user = wpinv_checkout_form_get_user( $valid_data );
1597
+    $user = wpinv_checkout_form_get_user($valid_data);
1598 1598
 
1599 1599
     // Let extensions validate fields after user is logged in if user has used login/registration form
1600
-    do_action( 'wpinv_checkout_user_error_checks', $user, $valid_data, $_POST );
1600
+    do_action('wpinv_checkout_user_error_checks', $user, $valid_data, $_POST);
1601 1601
     
1602
-    if ( false === $valid_data || wpinv_get_errors() || ! $user ) {
1603
-        if ( $is_ajax ) {
1604
-            do_action( 'wpinv_ajax_checkout_errors' );
1602
+    if (false === $valid_data || wpinv_get_errors() || !$user) {
1603
+        if ($is_ajax) {
1604
+            do_action('wpinv_ajax_checkout_errors');
1605 1605
             die();
1606 1606
         } else {
1607 1607
             return false;
1608 1608
         }
1609 1609
     }
1610 1610
 
1611
-    if ( $is_ajax ) {
1611
+    if ($is_ajax) {
1612 1612
         // Save address fields.
1613
-        $address_fields = array( 'first_name', 'last_name', 'phone', 'address', 'city', 'country', 'state', 'zip', 'company' );
1614
-        foreach ( $address_fields as $field ) {
1615
-            if ( isset( $user[$field] ) ) {
1616
-                $invoice->set( $field, $user[$field] );
1613
+        $address_fields = array('first_name', 'last_name', 'phone', 'address', 'city', 'country', 'state', 'zip', 'company');
1614
+        foreach ($address_fields as $field) {
1615
+            if (isset($user[$field])) {
1616
+                $invoice->set($field, $user[$field]);
1617 1617
             }
1618 1618
 
1619 1619
             $invoice->save();
@@ -1621,16 +1621,16 @@  discard block
 block discarded – undo
1621 1621
 
1622 1622
         $response['success']            = true;
1623 1623
         $response['data']['subtotal']   = $invoice->get_subtotal();
1624
-        $response['data']['subtotalf']  = $invoice->get_subtotal( true );
1624
+        $response['data']['subtotalf']  = $invoice->get_subtotal(true);
1625 1625
         $response['data']['discount']   = $invoice->get_discount();
1626
-        $response['data']['discountf']  = $invoice->get_discount( true );
1626
+        $response['data']['discountf']  = $invoice->get_discount(true);
1627 1627
         $response['data']['tax']        = $invoice->get_tax();
1628
-        $response['data']['taxf']       = $invoice->get_tax( true );
1628
+        $response['data']['taxf']       = $invoice->get_tax(true);
1629 1629
         $response['data']['total']      = $invoice->get_total();
1630
-        $response['data']['totalf']     = $invoice->get_total( true );
1631
-	    $response['data']['free']       = $invoice->is_free() && ( ! ( (float) $response['data']['total'] > 0 ) || $invoice->is_free_trial() ) ? true : false;
1630
+        $response['data']['totalf']     = $invoice->get_total(true);
1631
+	    $response['data']['free'] = $invoice->is_free() && (!((float)$response['data']['total'] > 0) || $invoice->is_free_trial()) ? true : false;
1632 1632
 
1633
-        wp_send_json( $response );
1633
+        wp_send_json($response);
1634 1634
     }
1635 1635
     
1636 1636
     $user_info = array(
@@ -1652,42 +1652,42 @@  discard block
 block discarded – undo
1652 1652
     
1653 1653
     // Setup invoice information
1654 1654
     $invoice_data = array(
1655
-        'invoice_id'        => !empty( $invoice ) ? $invoice->ID : 0,
1655
+        'invoice_id'        => !empty($invoice) ? $invoice->ID : 0,
1656 1656
         'items'             => $cart_items,
1657 1657
         'cart_discounts'    => $discounts,
1658
-        'fees'              => wpinv_get_cart_fees(),        // Any arbitrary fees that have been added to the cart
1659
-        'subtotal'          => wpinv_get_cart_subtotal( $cart_items ),    // Amount before taxes and discounts
1660
-        'discount'          => wpinv_get_cart_items_discount_amount( $cart_items, $discounts ), // Discounted amount
1661
-        'tax'               => wpinv_get_cart_tax( $cart_items ),               // Taxed amount
1662
-        'price'             => wpinv_get_cart_total( $cart_items, $discounts ),    // Amount after taxes
1658
+        'fees'              => wpinv_get_cart_fees(), // Any arbitrary fees that have been added to the cart
1659
+        'subtotal'          => wpinv_get_cart_subtotal($cart_items), // Amount before taxes and discounts
1660
+        'discount'          => wpinv_get_cart_items_discount_amount($cart_items, $discounts), // Discounted amount
1661
+        'tax'               => wpinv_get_cart_tax($cart_items), // Taxed amount
1662
+        'price'             => wpinv_get_cart_total($cart_items, $discounts), // Amount after taxes
1663 1663
         'invoice_key'       => $invoice->get_key() ? $invoice->get_key() : $invoice->generate_key(),
1664 1664
         'user_email'        => $invoice->get_email(),
1665
-        'date'              => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
1666
-        'user_info'         => stripslashes_deep( $user_info ),
1665
+        'date'              => date('Y-m-d H:i:s', current_time('timestamp')),
1666
+        'user_info'         => stripslashes_deep($user_info),
1667 1667
         'post_data'         => $_POST,
1668 1668
         'cart_details'      => $cart_items,
1669 1669
         'gateway'           => $valid_data['gateway'],
1670 1670
         'card_info'         => $valid_data['cc_info']
1671 1671
     );
1672 1672
     
1673
-    $vat_info   = $wpinv_euvat->current_vat_data();
1674
-    if ( is_array( $vat_info ) ) {
1673
+    $vat_info = $wpinv_euvat->current_vat_data();
1674
+    if (is_array($vat_info)) {
1675 1675
         $invoice_data['user_info']['vat_number']        = $vat_info['number'];
1676 1676
         $invoice_data['user_info']['vat_rate']          = wpinv_get_tax_rate($invoice_data['user_info']['country'], $invoice_data['user_info']['state']);
1677
-        $invoice_data['user_info']['adddress_confirmed']    = isset($vat_info['adddress_confirmed']) ? $vat_info['adddress_confirmed'] : false;
1677
+        $invoice_data['user_info']['adddress_confirmed'] = isset($vat_info['adddress_confirmed']) ? $vat_info['adddress_confirmed'] : false;
1678 1678
 
1679 1679
         // Add the VAT rate to each item in the cart
1680
-        foreach( $invoice_data['cart_details'] as $key => $item_data) {
1680
+        foreach ($invoice_data['cart_details'] as $key => $item_data) {
1681 1681
             $rate = wpinv_get_tax_rate($invoice_data['user_info']['country'], $invoice_data['user_info']['state'], $item_data['id']);
1682
-            $invoice_data['cart_details'][$key]['vat_rate'] = wpinv_round_amount( $rate, 4 );
1682
+            $invoice_data['cart_details'][$key]['vat_rate'] = wpinv_round_amount($rate, 4);
1683 1683
         }
1684 1684
     }
1685 1685
     
1686 1686
     // Save vat fields.
1687
-    $address_fields = array( 'vat_number', 'vat_rate', 'adddress_confirmed' );
1688
-    foreach ( $address_fields as $field ) {
1689
-        if ( isset( $invoice_data['user_info'][$field] ) ) {
1690
-            $invoice->set( $field, $invoice_data['user_info'][$field] );
1687
+    $address_fields = array('vat_number', 'vat_rate', 'adddress_confirmed');
1688
+    foreach ($address_fields as $field) {
1689
+        if (isset($invoice_data['user_info'][$field])) {
1690
+            $invoice->set($field, $invoice_data['user_info'][$field]);
1691 1691
         }
1692 1692
 
1693 1693
         $invoice->save();
@@ -1697,55 +1697,55 @@  discard block
 block discarded – undo
1697 1697
     $valid_data['user'] = $user;
1698 1698
     
1699 1699
     // Allow themes and plugins to hook before the gateway
1700
-    do_action( 'wpinv_checkout_before_gateway', $_POST, $user_info, $valid_data );
1700
+    do_action('wpinv_checkout_before_gateway', $_POST, $user_info, $valid_data);
1701 1701
     
1702 1702
     // If the total amount in the cart is 0, send to the manual gateway. This emulates a free invoice
1703
-    if ( !$invoice_data['price'] ) {
1703
+    if (!$invoice_data['price']) {
1704 1704
         // Revert to manual
1705 1705
         $invoice_data['gateway'] = 'manual';
1706 1706
         $_POST['wpi-gateway'] = 'manual';
1707 1707
     }
1708 1708
     
1709 1709
     // Allow the invoice data to be modified before it is sent to the gateway
1710
-    $invoice_data = apply_filters( 'wpinv_data_before_gateway', $invoice_data, $valid_data );
1710
+    $invoice_data = apply_filters('wpinv_data_before_gateway', $invoice_data, $valid_data);
1711 1711
     
1712
-    if ( $invoice_data['price'] && $invoice_data['gateway'] == 'manual' ) {
1712
+    if ($invoice_data['price'] && $invoice_data['gateway'] == 'manual') {
1713 1713
         $mode = 'test';
1714 1714
     } else {
1715
-        $mode = wpinv_is_test_mode( $invoice_data['gateway'] ) ? 'test' : 'live';
1715
+        $mode = wpinv_is_test_mode($invoice_data['gateway']) ? 'test' : 'live';
1716 1716
     }
1717 1717
     
1718 1718
     // Setup the data we're storing in the purchase session
1719 1719
     $session_data = $invoice_data;
1720 1720
     // Make sure credit card numbers are never stored in sessions
1721
-    if ( !empty( $session_data['card_info']['card_number'] ) ) {
1722
-        unset( $session_data['card_info']['card_number'] );
1721
+    if (!empty($session_data['card_info']['card_number'])) {
1722
+        unset($session_data['card_info']['card_number']);
1723 1723
     }
1724 1724
     
1725 1725
     // Used for showing item links to non logged-in users after purchase, and for other plugins needing purchase data.
1726
-    wpinv_set_checkout_session( $invoice_data );
1726
+    wpinv_set_checkout_session($invoice_data);
1727 1727
     
1728 1728
     // Set gateway
1729
-    $invoice->update_meta( '_wpinv_gateway', $invoice_data['gateway'] );
1730
-    $invoice->update_meta( '_wpinv_mode', $mode );
1731
-    $invoice->update_meta( '_wpinv_checkout', date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) );
1729
+    $invoice->update_meta('_wpinv_gateway', $invoice_data['gateway']);
1730
+    $invoice->update_meta('_wpinv_mode', $mode);
1731
+    $invoice->update_meta('_wpinv_checkout', date_i18n('Y-m-d H:i:s', current_time('timestamp')));
1732 1732
     
1733
-    do_action( 'wpinv_checkout_before_send_to_gateway', $invoice, $invoice_data );
1733
+    do_action('wpinv_checkout_before_send_to_gateway', $invoice, $invoice_data);
1734 1734
 
1735 1735
     // Send info to the gateway for payment processing
1736
-    wpinv_send_to_gateway( $invoice_data['gateway'], $invoice_data );
1736
+    wpinv_send_to_gateway($invoice_data['gateway'], $invoice_data);
1737 1737
     die();
1738 1738
 }
1739
-add_action( 'wpinv_payment', 'wpinv_process_checkout' );
1739
+add_action('wpinv_payment', 'wpinv_process_checkout');
1740 1740
 
1741
-function wpinv_get_invoices( $args ) {
1742
-    $args = wp_parse_args( $args, array(
1743
-        'status'   => array_keys( wpinv_get_invoice_statuses() ),
1741
+function wpinv_get_invoices($args) {
1742
+    $args = wp_parse_args($args, array(
1743
+        'status'   => array_keys(wpinv_get_invoice_statuses()),
1744 1744
         'type'     => 'wpi_invoice',
1745 1745
         'parent'   => null,
1746 1746
         'user'     => null,
1747 1747
         'email'    => '',
1748
-        'limit'    => get_option( 'posts_per_page' ),
1748
+        'limit'    => get_option('posts_per_page'),
1749 1749
         'offset'   => null,
1750 1750
         'page'     => 1,
1751 1751
         'exclude'  => array(),
@@ -1753,7 +1753,7 @@  discard block
 block discarded – undo
1753 1753
         'order'    => 'DESC',
1754 1754
         'return'   => 'objects',
1755 1755
         'paginate' => false,
1756
-    ) );
1756
+    ));
1757 1757
     
1758 1758
     // Handle some BW compatibility arg names where wp_query args differ in naming.
1759 1759
     $map_legacy = array(
@@ -1766,18 +1766,18 @@  discard block
 block discarded – undo
1766 1766
         'paged'          => 'page',
1767 1767
     );
1768 1768
 
1769
-    foreach ( $map_legacy as $from => $to ) {
1770
-        if ( isset( $args[ $from ] ) ) {
1771
-            $args[ $to ] = $args[ $from ];
1769
+    foreach ($map_legacy as $from => $to) {
1770
+        if (isset($args[$from])) {
1771
+            $args[$to] = $args[$from];
1772 1772
         }
1773 1773
     }
1774 1774
 
1775
-    if ( get_query_var( 'paged' ) )
1775
+    if (get_query_var('paged'))
1776 1776
         $args['page'] = get_query_var('paged');
1777
-    else if ( get_query_var( 'page' ) )
1778
-        $args['page'] = get_query_var( 'page' );
1779
-    else if ( !empty( $args[ 'page' ] ) )
1780
-        $args['page'] = $args[ 'page' ];
1777
+    else if (get_query_var('page'))
1778
+        $args['page'] = get_query_var('page');
1779
+    else if (!empty($args['page']))
1780
+        $args['page'] = $args['page'];
1781 1781
     else
1782 1782
         $args['page'] = 1;
1783 1783
 
@@ -1790,49 +1790,49 @@  discard block
 block discarded – undo
1790 1790
         'post_status'    => $args['status'],
1791 1791
         'posts_per_page' => $args['limit'],
1792 1792
         'meta_query'     => array(),
1793
-        'date_query'     => !empty( $args['date_query'] ) ? $args['date_query'] : array(),
1793
+        'date_query'     => !empty($args['date_query']) ? $args['date_query'] : array(),
1794 1794
         'fields'         => 'ids',
1795 1795
         'orderby'        => $args['orderby'],
1796 1796
         'order'          => $args['order'],
1797 1797
     );
1798 1798
     
1799
-    if ( !empty( $args['user'] ) ) {
1800
-        $wp_query_args['author'] = absint( $args['user'] );
1799
+    if (!empty($args['user'])) {
1800
+        $wp_query_args['author'] = absint($args['user']);
1801 1801
     }
1802 1802
 
1803
-    if ( ! is_null( $args['parent'] ) ) {
1804
-        $wp_query_args['post_parent'] = absint( $args['parent'] );
1803
+    if (!is_null($args['parent'])) {
1804
+        $wp_query_args['post_parent'] = absint($args['parent']);
1805 1805
     }
1806 1806
 
1807
-    if ( ! is_null( $args['offset'] ) ) {
1808
-        $wp_query_args['offset'] = absint( $args['offset'] );
1807
+    if (!is_null($args['offset'])) {
1808
+        $wp_query_args['offset'] = absint($args['offset']);
1809 1809
     } else {
1810
-        $wp_query_args['paged'] = absint( $args['page'] );
1810
+        $wp_query_args['paged'] = absint($args['page']);
1811 1811
     }
1812 1812
 
1813
-    if ( ! empty( $args['exclude'] ) ) {
1814
-        $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] );
1813
+    if (!empty($args['exclude'])) {
1814
+        $wp_query_args['post__not_in'] = array_map('absint', $args['exclude']);
1815 1815
     }
1816 1816
 
1817
-    if ( ! $args['paginate' ] ) {
1817
+    if (!$args['paginate']) {
1818 1818
         $wp_query_args['no_found_rows'] = true;
1819 1819
     }
1820 1820
 
1821 1821
     $wp_query_args = apply_filters('wpinv_get_invoices_args', $wp_query_args, $args);
1822 1822
 
1823 1823
     // Get results.
1824
-    $invoices = new WP_Query( $wp_query_args );
1824
+    $invoices = new WP_Query($wp_query_args);
1825 1825
 
1826
-    if ( 'objects' === $args['return'] ) {
1827
-        $return = array_map( 'wpinv_get_invoice', $invoices->posts );
1828
-    } elseif ( 'self' === $args['return'] ) {
1826
+    if ('objects' === $args['return']) {
1827
+        $return = array_map('wpinv_get_invoice', $invoices->posts);
1828
+    } elseif ('self' === $args['return']) {
1829 1829
         return $invoices;
1830 1830
     } else {
1831 1831
         $return = $invoices->posts;
1832 1832
     }
1833 1833
 
1834
-    if ( $args['paginate' ] ) {
1835
-        return (object) array(
1834
+    if ($args['paginate']) {
1835
+        return (object)array(
1836 1836
             'invoices'      => $return,
1837 1837
             'total'         => $invoices->found_posts,
1838 1838
             'max_num_pages' => $invoices->max_num_pages,
@@ -1844,22 +1844,22 @@  discard block
 block discarded – undo
1844 1844
 
1845 1845
 function wpinv_get_user_invoices_columns() {
1846 1846
     $columns = array(
1847
-            'invoice-number'  => array( 'title' => __( 'ID', 'invoicing' ), 'class' => 'text-left' ),
1848
-            'created-date'    => array( 'title' => __( 'Created Date', 'invoicing' ), 'class' => 'text-left' ),
1849
-            'payment-date'    => array( 'title' => __( 'Payment Date', 'invoicing' ), 'class' => 'text-left' ),
1850
-            'invoice-status'  => array( 'title' => __( 'Status', 'invoicing' ), 'class' => 'text-center' ),
1851
-            'invoice-total'   => array( 'title' => __( 'Total', 'invoicing' ), 'class' => 'text-right' ),
1852
-            'invoice-actions' => array( 'title' => '&nbsp;', 'class' => 'text-center' ),
1847
+            'invoice-number'  => array('title' => __('ID', 'invoicing'), 'class' => 'text-left'),
1848
+            'created-date'    => array('title' => __('Created Date', 'invoicing'), 'class' => 'text-left'),
1849
+            'payment-date'    => array('title' => __('Payment Date', 'invoicing'), 'class' => 'text-left'),
1850
+            'invoice-status'  => array('title' => __('Status', 'invoicing'), 'class' => 'text-center'),
1851
+            'invoice-total'   => array('title' => __('Total', 'invoicing'), 'class' => 'text-right'),
1852
+            'invoice-actions' => array('title' => '&nbsp;', 'class' => 'text-center'),
1853 1853
         );
1854 1854
 
1855
-    return apply_filters( 'wpinv_user_invoices_columns', $columns );
1855
+    return apply_filters('wpinv_user_invoices_columns', $columns);
1856 1856
 }
1857 1857
 
1858
-function wpinv_payment_receipt( $atts, $content = null ) {
1858
+function wpinv_payment_receipt($atts, $content = null) {
1859 1859
     global $wpinv_receipt_args;
1860 1860
 
1861
-    $wpinv_receipt_args = shortcode_atts( array(
1862
-        'error'           => __( 'Sorry, trouble retrieving payment receipt.', 'invoicing' ),
1861
+    $wpinv_receipt_args = shortcode_atts(array(
1862
+        'error'           => __('Sorry, trouble retrieving payment receipt.', 'invoicing'),
1863 1863
         'price'           => true,
1864 1864
         'discount'        => true,
1865 1865
         'items'           => true,
@@ -1868,197 +1868,197 @@  discard block
 block discarded – undo
1868 1868
         'invoice_key'     => false,
1869 1869
         'payment_method'  => true,
1870 1870
         'invoice_id'      => true
1871
-    ), $atts, 'wpinv_receipt' );
1871
+    ), $atts, 'wpinv_receipt');
1872 1872
 
1873 1873
     $session = wpinv_get_checkout_session();
1874
-    if ( isset( $_GET['invoice_key'] ) ) {
1875
-        $invoice_key = urldecode( $_GET['invoice_key'] );
1876
-    } else if ( $session && isset( $session['invoice_key'] ) ) {
1874
+    if (isset($_GET['invoice_key'])) {
1875
+        $invoice_key = urldecode($_GET['invoice_key']);
1876
+    } else if ($session && isset($session['invoice_key'])) {
1877 1877
         $invoice_key = $session['invoice_key'];
1878
-    } elseif ( isset( $wpinv_receipt_args['invoice_key'] ) && $wpinv_receipt_args['invoice_key'] ) {
1878
+    } elseif (isset($wpinv_receipt_args['invoice_key']) && $wpinv_receipt_args['invoice_key']) {
1879 1879
         $invoice_key = $wpinv_receipt_args['invoice_key'];
1880
-    } else if ( isset( $_GET['invoice-id'] ) ) {
1881
-        $invoice_key = wpinv_get_payment_key( (int)$_GET['invoice-id'] );
1880
+    } else if (isset($_GET['invoice-id'])) {
1881
+        $invoice_key = wpinv_get_payment_key((int)$_GET['invoice-id']);
1882 1882
     }
1883 1883
 
1884 1884
     // No key found
1885
-    if ( ! isset( $invoice_key ) ) {
1885
+    if (!isset($invoice_key)) {
1886 1886
         return '<p class="alert alert-error">' . $wpinv_receipt_args['error'] . '</p>';
1887 1887
     }
1888 1888
 
1889
-    $invoice_id    = wpinv_get_invoice_id_by_key( $invoice_key );
1890
-    $user_can_view = wpinv_can_view_receipt( $invoice_key );
1891
-    if ( $user_can_view && isset( $_GET['invoice-id'] ) ) {
1889
+    $invoice_id    = wpinv_get_invoice_id_by_key($invoice_key);
1890
+    $user_can_view = wpinv_can_view_receipt($invoice_key);
1891
+    if ($user_can_view && isset($_GET['invoice-id'])) {
1892 1892
         $invoice_id     = (int)$_GET['invoice-id'];
1893
-        $user_can_view  = $invoice_key == wpinv_get_payment_key( (int)$_GET['invoice-id'] ) ? true : false;
1893
+        $user_can_view  = $invoice_key == wpinv_get_payment_key((int)$_GET['invoice-id']) ? true : false;
1894 1894
     }
1895 1895
 
1896 1896
     // Key was provided, but user is logged out. Offer them the ability to login and view the receipt
1897
-    if ( ! $user_can_view && ! empty( $invoice_key ) && ! is_user_logged_in() ) {
1897
+    if (!$user_can_view && !empty($invoice_key) && !is_user_logged_in()) {
1898 1898
         // login redirect
1899
-        return '<p class="alert alert-error">' . __( 'You are not allowed to access this section', 'invoicing' ) . '</p>';
1899
+        return '<p class="alert alert-error">' . __('You are not allowed to access this section', 'invoicing') . '</p>';
1900 1900
     }
1901 1901
 
1902
-    if ( ! apply_filters( 'wpinv_user_can_view_receipt', $user_can_view, $wpinv_receipt_args ) ) {
1902
+    if (!apply_filters('wpinv_user_can_view_receipt', $user_can_view, $wpinv_receipt_args)) {
1903 1903
         return '<p class="alert alert-error">' . $wpinv_receipt_args['error'] . '</p>';
1904 1904
     }
1905 1905
 
1906 1906
     ob_start();
1907 1907
 
1908
-    wpinv_get_template_part( 'wpinv-invoice-receipt' );
1908
+    wpinv_get_template_part('wpinv-invoice-receipt');
1909 1909
 
1910 1910
     $display = ob_get_clean();
1911 1911
 
1912 1912
     return $display;
1913 1913
 }
1914 1914
 
1915
-function wpinv_get_invoice_id_by_key( $key ) {
1915
+function wpinv_get_invoice_id_by_key($key) {
1916 1916
 	global $wpdb;
1917 1917
 
1918
-	$invoice_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpinv_key' AND meta_value = %s LIMIT 1", $key ) );
1918
+	$invoice_id = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wpinv_key' AND meta_value = %s LIMIT 1", $key));
1919 1919
 
1920
-	if ( $invoice_id != NULL )
1920
+	if ($invoice_id != NULL)
1921 1921
 		return $invoice_id;
1922 1922
 
1923 1923
 	return 0;
1924 1924
 }
1925 1925
 
1926
-function wpinv_can_view_receipt( $invoice_key = '' ) {
1926
+function wpinv_can_view_receipt($invoice_key = '') {
1927 1927
 	$return = false;
1928 1928
 
1929
-	if ( empty( $invoice_key ) ) {
1929
+	if (empty($invoice_key)) {
1930 1930
 		return $return;
1931 1931
 	}
1932 1932
 
1933 1933
 	global $wpinv_receipt_args;
1934 1934
 
1935
-	$wpinv_receipt_args['id'] = wpinv_get_invoice_id_by_key( $invoice_key );
1936
-	if ( isset( $_GET['invoice-id'] ) ) {
1937
-		$wpinv_receipt_args['id'] = $invoice_key == wpinv_get_payment_key( (int)$_GET['invoice-id'] ) ? (int)$_GET['invoice-id'] : 0;
1935
+	$wpinv_receipt_args['id'] = wpinv_get_invoice_id_by_key($invoice_key);
1936
+	if (isset($_GET['invoice-id'])) {
1937
+		$wpinv_receipt_args['id'] = $invoice_key == wpinv_get_payment_key((int)$_GET['invoice-id']) ? (int)$_GET['invoice-id'] : 0;
1938 1938
 	}
1939 1939
 
1940
-	if ( empty( $wpinv_receipt_args['id'] ) ) {
1940
+	if (empty($wpinv_receipt_args['id'])) {
1941 1941
 		return $return;
1942 1942
 	}
1943 1943
 
1944
-	$invoice = wpinv_get_invoice( $wpinv_receipt_args['id'] );
1945
-	if ( !( !empty( $invoice->ID ) && $invoice->get_key() === $invoice_key ) ) {
1944
+	$invoice = wpinv_get_invoice($wpinv_receipt_args['id']);
1945
+	if (!(!empty($invoice->ID) && $invoice->get_key() === $invoice_key)) {
1946 1946
 		return $return;
1947 1947
 	}
1948 1948
 
1949
-	if ( is_user_logged_in() ) {
1950
-		if ( (int)$invoice->get_user_id() === (int) get_current_user_id() ) {
1949
+	if (is_user_logged_in()) {
1950
+		if ((int)$invoice->get_user_id() === (int)get_current_user_id()) {
1951 1951
 			$return = true;
1952 1952
 		}
1953 1953
 	}
1954 1954
 
1955 1955
 	$session = wpinv_get_checkout_session();
1956
-	if ( isset( $_GET['invoice_key'] ) || ( $session && isset( $session['invoice_key'] ) ) ) {
1957
-		$check_key = isset( $_GET['invoice_key'] ) ? $_GET['invoice_key'] : $session['invoice_key'];
1956
+	if (isset($_GET['invoice_key']) || ($session && isset($session['invoice_key']))) {
1957
+		$check_key = isset($_GET['invoice_key']) ? $_GET['invoice_key'] : $session['invoice_key'];
1958 1958
 
1959
-		if ( wpinv_require_login_to_checkout() ) {
1959
+		if (wpinv_require_login_to_checkout()) {
1960 1960
 			$return = $return && $check_key === $invoice_key;
1961 1961
 		} else {
1962 1962
 			$return = $check_key === $invoice_key;
1963 1963
 		}
1964 1964
 	}
1965 1965
 
1966
-	return (bool) apply_filters( 'wpinv_can_view_receipt', $return, $invoice_key );
1966
+	return (bool)apply_filters('wpinv_can_view_receipt', $return, $invoice_key);
1967 1967
 }
1968 1968
 
1969 1969
 function wpinv_pay_for_invoice() {
1970 1970
     global $wpinv_euvat;
1971 1971
     
1972
-    if ( isset( $_GET['invoice_key'] ) ) {
1972
+    if (isset($_GET['invoice_key'])) {
1973 1973
         $checkout_uri   = wpinv_get_checkout_uri();
1974
-        $invoice_key    = sanitize_text_field( $_GET['invoice_key'] );
1974
+        $invoice_key    = sanitize_text_field($_GET['invoice_key']);
1975 1975
         
1976
-        if ( empty( $invoice_key ) ) {
1977
-            wpinv_set_error( 'invalid_invoice', __( 'Invoice not found', 'invoicing' ) );
1978
-            wp_redirect( $checkout_uri );
1976
+        if (empty($invoice_key)) {
1977
+            wpinv_set_error('invalid_invoice', __('Invoice not found', 'invoicing'));
1978
+            wp_redirect($checkout_uri);
1979 1979
             exit();
1980 1980
         }
1981 1981
         
1982
-        do_action( 'wpinv_check_pay_for_invoice', $invoice_key );
1982
+        do_action('wpinv_check_pay_for_invoice', $invoice_key);
1983 1983
 
1984
-        $invoice_id    = wpinv_get_invoice_id_by_key( $invoice_key );
1985
-        $user_can_view = wpinv_can_view_receipt( $invoice_key );
1986
-        if ( $user_can_view && isset( $_GET['invoice-id'] ) ) {
1984
+        $invoice_id    = wpinv_get_invoice_id_by_key($invoice_key);
1985
+        $user_can_view = wpinv_can_view_receipt($invoice_key);
1986
+        if ($user_can_view && isset($_GET['invoice-id'])) {
1987 1987
             $invoice_id     = (int)$_GET['invoice-id'];
1988
-            $user_can_view  = $invoice_key == wpinv_get_payment_key( (int)$_GET['invoice-id'] ) ? true : false;
1988
+            $user_can_view  = $invoice_key == wpinv_get_payment_key((int)$_GET['invoice-id']) ? true : false;
1989 1989
         }
1990 1990
         
1991
-        if ( $invoice_id && $user_can_view && ( $invoice = wpinv_get_invoice( $invoice_id ) ) ) {
1992
-            if ( $invoice->needs_payment() ) {
1991
+        if ($invoice_id && $user_can_view && ($invoice = wpinv_get_invoice($invoice_id))) {
1992
+            if ($invoice->needs_payment()) {
1993 1993
                 $data                   = array();
1994 1994
                 $data['invoice_id']     = $invoice_id;
1995
-                $data['cart_discounts'] = $invoice->get_discounts( true );
1995
+                $data['cart_discounts'] = $invoice->get_discounts(true);
1996 1996
                 
1997
-                wpinv_set_checkout_session( $data );
1997
+                wpinv_set_checkout_session($data);
1998 1998
                 
1999
-                if ( wpinv_get_option( 'vat_ip_country_default' ) ) {
1999
+                if (wpinv_get_option('vat_ip_country_default')) {
2000 2000
                     $_POST['country']   = $wpinv_euvat->get_country_by_ip();
2001 2001
                     $_POST['state']     = $_POST['country'] == $invoice->country ? $invoice->state : '';
2002 2002
                     
2003
-                    wpinv_recalculate_tax( true );
2003
+                    wpinv_recalculate_tax(true);
2004 2004
                 }
2005 2005
                 
2006 2006
             } else {
2007 2007
                 $checkout_uri = $invoice->get_view_url();
2008 2008
             }
2009 2009
         } else {
2010
-            wpinv_set_error( 'invalid_invoice', __( 'You are not allowed to view this invoice', 'invoicing' ) );
2010
+            wpinv_set_error('invalid_invoice', __('You are not allowed to view this invoice', 'invoicing'));
2011 2011
             
2012
-            $checkout_uri = is_user_logged_in() ? wpinv_get_history_page_uri() : wp_login_url( get_permalink() );
2012
+            $checkout_uri = is_user_logged_in() ? wpinv_get_history_page_uri() : wp_login_url(get_permalink());
2013 2013
         }
2014 2014
         
2015
-        if(wp_redirect( $checkout_uri )){
2015
+        if (wp_redirect($checkout_uri)) {
2016 2016
             exit;
2017 2017
         };
2018 2018
         wpinv_die();
2019 2019
     }
2020 2020
 }
2021
-add_action( 'wpinv_pay_for_invoice', 'wpinv_pay_for_invoice' );
2021
+add_action('wpinv_pay_for_invoice', 'wpinv_pay_for_invoice');
2022 2022
 
2023
-function wpinv_handle_pay_via_invoice_link( $invoice_key ) {
2024
-    if ( !empty( $invoice_key ) && !empty( $_REQUEST['_wpipay'] ) && !is_user_logged_in() && $invoice_id = wpinv_get_invoice_id_by_key( $invoice_key ) ) {
2025
-        if ( $invoice = wpinv_get_invoice( $invoice_id ) ) {
2023
+function wpinv_handle_pay_via_invoice_link($invoice_key) {
2024
+    if (!empty($invoice_key) && !empty($_REQUEST['_wpipay']) && !is_user_logged_in() && $invoice_id = wpinv_get_invoice_id_by_key($invoice_key)) {
2025
+        if ($invoice = wpinv_get_invoice($invoice_id)) {
2026 2026
             $user_id = $invoice->get_user_id();
2027
-            $secret = sanitize_text_field( $_GET['_wpipay'] );
2027
+            $secret = sanitize_text_field($_GET['_wpipay']);
2028 2028
             
2029
-            if ( $secret === md5( $user_id . '::' . $invoice->get_email() . '::' . $invoice_key ) ) { // valid invoice link
2030
-                $redirect_to = remove_query_arg( '_wpipay', get_permalink() );
2029
+            if ($secret === md5($user_id . '::' . $invoice->get_email() . '::' . $invoice_key)) { // valid invoice link
2030
+                $redirect_to = remove_query_arg('_wpipay', get_permalink());
2031 2031
                 
2032
-                wpinv_guest_redirect( $redirect_to, $user_id );
2032
+                wpinv_guest_redirect($redirect_to, $user_id);
2033 2033
                 exit();
2034 2034
             }
2035 2035
         }
2036 2036
     }
2037 2037
 }
2038
-add_action( 'wpinv_check_pay_for_invoice', 'wpinv_handle_pay_via_invoice_link' );
2038
+add_action('wpinv_check_pay_for_invoice', 'wpinv_handle_pay_via_invoice_link');
2039 2039
 
2040
-function wpinv_set_payment_transaction_id( $invoice_id = 0, $transaction_id = '' ) {
2041
-    $invoice_id = is_object( $invoice_id ) && !empty( $invoice_id->ID ) ? $invoice_id : $invoice_id;
2040
+function wpinv_set_payment_transaction_id($invoice_id = 0, $transaction_id = '') {
2041
+    $invoice_id = is_object($invoice_id) && !empty($invoice_id->ID) ? $invoice_id : $invoice_id;
2042 2042
     
2043
-    if ( empty( $invoice_id ) && $invoice_id > 0 ) {
2043
+    if (empty($invoice_id) && $invoice_id > 0) {
2044 2044
         return false;
2045 2045
     }
2046 2046
     
2047
-    if ( empty( $transaction_id ) ) {
2047
+    if (empty($transaction_id)) {
2048 2048
         $transaction_id = $invoice_id;
2049 2049
     }
2050 2050
 
2051
-    $transaction_id = apply_filters( 'wpinv_set_payment_transaction_id', $transaction_id, $invoice_id );
2051
+    $transaction_id = apply_filters('wpinv_set_payment_transaction_id', $transaction_id, $invoice_id);
2052 2052
     
2053
-    return wpinv_update_invoice_meta( $invoice_id, '_wpinv_transaction_id', $transaction_id );
2053
+    return wpinv_update_invoice_meta($invoice_id, '_wpinv_transaction_id', $transaction_id);
2054 2054
 }
2055 2055
 
2056
-function wpinv_invoice_status_label( $status, $status_display = '' ) {
2057
-    if ( empty( $status_display ) ) {
2058
-        $status_display = wpinv_status_nicename( $status );
2056
+function wpinv_invoice_status_label($status, $status_display = '') {
2057
+    if (empty($status_display)) {
2058
+        $status_display = wpinv_status_nicename($status);
2059 2059
     }
2060 2060
     
2061
-    switch ( $status ) {
2061
+    switch ($status) {
2062 2062
         case 'publish' :
2063 2063
         case 'wpi-renewal' :
2064 2064
             $class = 'label-success';
@@ -2083,201 +2083,201 @@  discard block
 block discarded – undo
2083 2083
     
2084 2084
     $label = '<span class="label label-inv-' . $status . ' ' . $class . '">' . $status_display . '</span>';
2085 2085
     
2086
-    return apply_filters( 'wpinv_invoice_status_label', $label, $status, $status_display );
2086
+    return apply_filters('wpinv_invoice_status_label', $label, $status, $status_display);
2087 2087
 }
2088 2088
 
2089
-function wpinv_format_invoice_number( $number, $type = '' ) {
2090
-    $check = apply_filters( 'wpinv_pre_format_invoice_number', null, $number, $type );
2091
-    if ( null !== $check ) {
2089
+function wpinv_format_invoice_number($number, $type = '') {
2090
+    $check = apply_filters('wpinv_pre_format_invoice_number', null, $number, $type);
2091
+    if (null !== $check) {
2092 2092
         return $check;
2093 2093
     }
2094 2094
 
2095
-    if ( !empty( $number ) && !is_numeric( $number ) ) {
2095
+    if (!empty($number) && !is_numeric($number)) {
2096 2096
         return $number;
2097 2097
     }
2098 2098
 
2099
-    $padd  = wpinv_get_option( 'invoice_number_padd' );
2100
-    $prefix  = wpinv_get_option( 'invoice_number_prefix' );
2101
-    $postfix = wpinv_get_option( 'invoice_number_postfix' );
2099
+    $padd = wpinv_get_option('invoice_number_padd');
2100
+    $prefix  = wpinv_get_option('invoice_number_prefix');
2101
+    $postfix = wpinv_get_option('invoice_number_postfix');
2102 2102
     
2103
-    $padd = absint( $padd );
2104
-    $formatted_number = absint( $number );
2103
+    $padd = absint($padd);
2104
+    $formatted_number = absint($number);
2105 2105
     
2106
-    if ( $padd > 0 ) {
2107
-        $formatted_number = zeroise( $formatted_number, $padd );
2106
+    if ($padd > 0) {
2107
+        $formatted_number = zeroise($formatted_number, $padd);
2108 2108
     }    
2109 2109
 
2110 2110
     $formatted_number = $prefix . $formatted_number . $postfix;
2111 2111
 
2112
-    return apply_filters( 'wpinv_format_invoice_number', $formatted_number, $number, $prefix, $postfix, $padd );
2112
+    return apply_filters('wpinv_format_invoice_number', $formatted_number, $number, $prefix, $postfix, $padd);
2113 2113
 }
2114 2114
 
2115
-function wpinv_get_next_invoice_number( $type = '' ) {
2116
-    $check = apply_filters( 'wpinv_get_pre_next_invoice_number', null, $type );
2117
-    if ( null !== $check ) {
2115
+function wpinv_get_next_invoice_number($type = '') {
2116
+    $check = apply_filters('wpinv_get_pre_next_invoice_number', null, $type);
2117
+    if (null !== $check) {
2118 2118
         return $check;
2119 2119
     }
2120 2120
     
2121
-    if ( !wpinv_sequential_number_active() ) {
2121
+    if (!wpinv_sequential_number_active()) {
2122 2122
         return false;
2123 2123
     }
2124 2124
 
2125
-    $number = $last_number = get_option( 'wpinv_last_invoice_number', 0 );
2126
-    $start  = wpinv_get_option( 'invoice_sequence_start', 1 );
2127
-    if ( !absint( $start ) > 0 ) {
2125
+    $number = $last_number = get_option('wpinv_last_invoice_number', 0);
2126
+    $start  = wpinv_get_option('invoice_sequence_start', 1);
2127
+    if (!absint($start) > 0) {
2128 2128
         $start = 1;
2129 2129
     }
2130 2130
     $increment_number = true;
2131 2131
     $save_number = false;
2132 2132
 
2133
-    if ( !empty( $number ) && !is_numeric( $number ) && $number == wpinv_format_invoice_number( $number ) ) {
2134
-        $number = wpinv_clean_invoice_number( $number );
2133
+    if (!empty($number) && !is_numeric($number) && $number == wpinv_format_invoice_number($number)) {
2134
+        $number = wpinv_clean_invoice_number($number);
2135 2135
     }
2136 2136
 
2137
-    if ( empty( $number ) ) {
2138
-        if ( !( $last_number === 0 || $last_number === '0' ) ) {
2139
-            $last_invoice = wpinv_get_invoices( array( 'limit' => 1, 'order' => 'DESC', 'orderby' => 'ID', 'return' => 'posts', 'fields' => 'ids', 'status' => array_keys( wpinv_get_invoice_statuses( true, true ) ) ) );
2137
+    if (empty($number)) {
2138
+        if (!($last_number === 0 || $last_number === '0')) {
2139
+            $last_invoice = wpinv_get_invoices(array('limit' => 1, 'order' => 'DESC', 'orderby' => 'ID', 'return' => 'posts', 'fields' => 'ids', 'status' => array_keys(wpinv_get_invoice_statuses(true, true))));
2140 2140
 
2141
-            if ( !empty( $last_invoice[0] ) && $invoice_number = wpinv_get_invoice_number( $last_invoice[0] ) ) {
2142
-                if ( is_numeric( $invoice_number ) ) {
2141
+            if (!empty($last_invoice[0]) && $invoice_number = wpinv_get_invoice_number($last_invoice[0])) {
2142
+                if (is_numeric($invoice_number)) {
2143 2143
                     $number = $invoice_number;
2144 2144
                 } else {
2145
-                    $number = wpinv_clean_invoice_number( $invoice_number );
2145
+                    $number = wpinv_clean_invoice_number($invoice_number);
2146 2146
                 }
2147 2147
             }
2148 2148
 
2149
-            if ( empty( $number ) ) {
2149
+            if (empty($number)) {
2150 2150
                 $increment_number = false;
2151 2151
                 $number = $start;
2152
-                $save_number = ( $number - 1 );
2152
+                $save_number = ($number - 1);
2153 2153
             } else {
2154 2154
                 $save_number = $number;
2155 2155
             }
2156 2156
         }
2157 2157
     }
2158 2158
 
2159
-    if ( $start > $number ) {
2159
+    if ($start > $number) {
2160 2160
         $increment_number = false;
2161 2161
         $number = $start;
2162
-        $save_number = ( $number - 1 );
2162
+        $save_number = ($number - 1);
2163 2163
     }
2164 2164
 
2165
-    if ( $save_number !== false ) {
2166
-        update_option( 'wpinv_last_invoice_number', $save_number );
2165
+    if ($save_number !== false) {
2166
+        update_option('wpinv_last_invoice_number', $save_number);
2167 2167
     }
2168 2168
     
2169
-    $increment_number = apply_filters( 'wpinv_increment_payment_number', $increment_number, $number );
2169
+    $increment_number = apply_filters('wpinv_increment_payment_number', $increment_number, $number);
2170 2170
 
2171
-    if ( $increment_number ) {
2171
+    if ($increment_number) {
2172 2172
         $number++;
2173 2173
     }
2174 2174
 
2175
-    return apply_filters( 'wpinv_get_next_invoice_number', $number );
2175
+    return apply_filters('wpinv_get_next_invoice_number', $number);
2176 2176
 }
2177 2177
 
2178
-function wpinv_clean_invoice_number( $number, $type = '' ) {
2179
-    $check = apply_filters( 'wpinv_pre_clean_invoice_number', null, $number, $type );
2180
-    if ( null !== $check ) {
2178
+function wpinv_clean_invoice_number($number, $type = '') {
2179
+    $check = apply_filters('wpinv_pre_clean_invoice_number', null, $number, $type);
2180
+    if (null !== $check) {
2181 2181
         return $check;
2182 2182
     }
2183 2183
     
2184
-    $prefix  = wpinv_get_option( 'invoice_number_prefix' );
2185
-    $postfix = wpinv_get_option( 'invoice_number_postfix' );
2184
+    $prefix  = wpinv_get_option('invoice_number_prefix');
2185
+    $postfix = wpinv_get_option('invoice_number_postfix');
2186 2186
 
2187
-    $number = preg_replace( '/' . $prefix . '/', '', $number, 1 );
2187
+    $number = preg_replace('/' . $prefix . '/', '', $number, 1);
2188 2188
 
2189
-    $length      = strlen( $number );
2190
-    $postfix_pos = strrpos( $number, $postfix );
2189
+    $length      = strlen($number);
2190
+    $postfix_pos = strrpos($number, $postfix);
2191 2191
     
2192
-    if ( false !== $postfix_pos ) {
2193
-        $number      = substr_replace( $number, '', $postfix_pos, $length );
2192
+    if (false !== $postfix_pos) {
2193
+        $number = substr_replace($number, '', $postfix_pos, $length);
2194 2194
     }
2195 2195
 
2196
-    $number = intval( $number );
2196
+    $number = intval($number);
2197 2197
 
2198
-    return apply_filters( 'wpinv_clean_invoice_number', $number, $prefix, $postfix );
2198
+    return apply_filters('wpinv_clean_invoice_number', $number, $prefix, $postfix);
2199 2199
 }
2200 2200
 
2201
-function wpinv_save_number_post_saved( $post_ID, $post, $update ) {
2201
+function wpinv_save_number_post_saved($post_ID, $post, $update) {
2202 2202
     global $wpdb;
2203 2203
 
2204
-    if ( !$update && !get_post_meta( $post_ID, '_wpinv_number', true ) ) {
2205
-        wpinv_update_invoice_number( $post_ID, $post->post_status != 'auto-draft', $post->post_type );
2204
+    if (!$update && !get_post_meta($post_ID, '_wpinv_number', true)) {
2205
+        wpinv_update_invoice_number($post_ID, $post->post_status != 'auto-draft', $post->post_type);
2206 2206
     }
2207 2207
 
2208
-    if ( !$update ) {
2209
-        $wpdb->update( $wpdb->posts, array( 'post_name' => wpinv_generate_post_name( $post_ID ) ), array( 'ID' => $post_ID ) );
2210
-        clean_post_cache( $post_ID );
2208
+    if (!$update) {
2209
+        $wpdb->update($wpdb->posts, array('post_name' => wpinv_generate_post_name($post_ID)), array('ID' => $post_ID));
2210
+        clean_post_cache($post_ID);
2211 2211
     }
2212 2212
 }
2213
-add_action( 'save_post_wpi_invoice', 'wpinv_save_number_post_saved', 1, 3 );
2213
+add_action('save_post_wpi_invoice', 'wpinv_save_number_post_saved', 1, 3);
2214 2214
 
2215
-function wpinv_save_number_post_updated( $post_ID, $post_after, $post_before ) {
2216
-    if ( !empty( $post_after->post_type ) && $post_after->post_type == 'wpi_invoice' && $post_before->post_status == 'auto-draft' && $post_after->post_status != $post_before->post_status ) {
2217
-        wpinv_update_invoice_number( $post_ID, true, $post_after->post_type );
2215
+function wpinv_save_number_post_updated($post_ID, $post_after, $post_before) {
2216
+    if (!empty($post_after->post_type) && $post_after->post_type == 'wpi_invoice' && $post_before->post_status == 'auto-draft' && $post_after->post_status != $post_before->post_status) {
2217
+        wpinv_update_invoice_number($post_ID, true, $post_after->post_type);
2218 2218
     }
2219 2219
 }
2220
-add_action( 'post_updated', 'wpinv_save_number_post_updated', 1, 3 );
2220
+add_action('post_updated', 'wpinv_save_number_post_updated', 1, 3);
2221 2221
 
2222
-function wpinv_update_invoice_number( $post_ID, $save_sequential = false, $type = '' ) {
2222
+function wpinv_update_invoice_number($post_ID, $save_sequential = false, $type = '') {
2223 2223
     global $wpdb;
2224 2224
     
2225
-    $check = apply_filters( 'wpinv_pre_update_invoice_number', null, $post_ID, $save_sequential, $type );
2226
-    if ( null !== $check ) {
2225
+    $check = apply_filters('wpinv_pre_update_invoice_number', null, $post_ID, $save_sequential, $type);
2226
+    if (null !== $check) {
2227 2227
         return $check;
2228 2228
     }
2229 2229
 
2230
-    if ( wpinv_sequential_number_active() ) {
2230
+    if (wpinv_sequential_number_active()) {
2231 2231
         $number = wpinv_get_next_invoice_number();
2232 2232
 
2233
-        if ( $save_sequential ) {
2234
-            update_option( 'wpinv_last_invoice_number', $number );
2233
+        if ($save_sequential) {
2234
+            update_option('wpinv_last_invoice_number', $number);
2235 2235
         }
2236 2236
     } else {
2237 2237
         $number = $post_ID;
2238 2238
     }
2239 2239
 
2240
-    $number = wpinv_format_invoice_number( $number );
2240
+    $number = wpinv_format_invoice_number($number);
2241 2241
 
2242
-    update_post_meta( $post_ID, '_wpinv_number', $number );
2242
+    update_post_meta($post_ID, '_wpinv_number', $number);
2243 2243
 
2244
-    $wpdb->update( $wpdb->posts, array( 'post_title' => $number ), array( 'ID' => $post_ID ) );
2244
+    $wpdb->update($wpdb->posts, array('post_title' => $number), array('ID' => $post_ID));
2245 2245
 
2246
-    clean_post_cache( $post_ID );
2246
+    clean_post_cache($post_ID);
2247 2247
 
2248 2248
     return $number;
2249 2249
 }
2250 2250
 
2251
-function wpinv_post_name_prefix( $post_type = 'wpi_invoice' ) {
2252
-    return apply_filters( 'wpinv_post_name_prefix', 'inv-', $post_type );
2251
+function wpinv_post_name_prefix($post_type = 'wpi_invoice') {
2252
+    return apply_filters('wpinv_post_name_prefix', 'inv-', $post_type);
2253 2253
 }
2254 2254
 
2255
-function wpinv_generate_post_name( $post_ID ) {
2256
-    $prefix = wpinv_post_name_prefix( get_post_type( $post_ID ) );
2257
-    $post_name = sanitize_title( $prefix . $post_ID );
2255
+function wpinv_generate_post_name($post_ID) {
2256
+    $prefix = wpinv_post_name_prefix(get_post_type($post_ID));
2257
+    $post_name = sanitize_title($prefix . $post_ID);
2258 2258
 
2259
-    return apply_filters( 'wpinv_generate_post_name', $post_name, $post_ID, $prefix );
2259
+    return apply_filters('wpinv_generate_post_name', $post_name, $post_ID, $prefix);
2260 2260
 }
2261 2261
 
2262
-function wpinv_is_invoice_viewed( $invoice_id ) {
2263
-    if ( empty( $invoice_id ) ) {
2262
+function wpinv_is_invoice_viewed($invoice_id) {
2263
+    if (empty($invoice_id)) {
2264 2264
         return false;
2265 2265
     }
2266 2266
 
2267
-    $viewed_meta = get_post_meta( $invoice_id, '_wpinv_is_viewed', true );
2267
+    $viewed_meta = get_post_meta($invoice_id, '_wpinv_is_viewed', true);
2268 2268
 
2269
-    return apply_filters( 'wpinv_is_invoice_viewed', 1 === (int)$viewed_meta, $invoice_id );
2269
+    return apply_filters('wpinv_is_invoice_viewed', 1 === (int)$viewed_meta, $invoice_id);
2270 2270
 }
2271 2271
 
2272 2272
 function wpinv_mark_invoice_viewed() {
2273 2273
 
2274
-    if ( isset( $_GET['invoice_key'] ) || is_singular( 'wpi_invoice' ) || is_singular( 'wpi_quote' ) ) {
2275
-        $invoice_key = isset( $_GET['invoice_key'] ) ? urldecode($_GET['invoice_key']) : '';
2274
+    if (isset($_GET['invoice_key']) || is_singular('wpi_invoice') || is_singular('wpi_quote')) {
2275
+        $invoice_key = isset($_GET['invoice_key']) ? urldecode($_GET['invoice_key']) : '';
2276 2276
 	    global $post;
2277 2277
 
2278
-        if(!empty($invoice_key)){
2278
+        if (!empty($invoice_key)) {
2279 2279
 	        $invoice_id = wpinv_get_invoice_id_by_key($invoice_key);
2280
-        } else if(!empty( $post ) && ($post->post_type == 'wpi_invoice' || $post->post_type == 'wpi_quote')) {
2280
+        } else if (!empty($post) && ($post->post_type == 'wpi_invoice' || $post->post_type == 'wpi_quote')) {
2281 2281
 			$invoice_id = $post->ID;
2282 2282
         } else {
2283 2283
         	return;
@@ -2285,60 +2285,60 @@  discard block
 block discarded – undo
2285 2285
 
2286 2286
         $invoice = new WPInv_Invoice($invoice_id);
2287 2287
 
2288
-        if(!$invoice_id){
2288
+        if (!$invoice_id) {
2289 2289
             return;
2290 2290
         }
2291 2291
 
2292
-	    if ( is_user_logged_in() ) {
2293
-		    if ( (int)$invoice->get_user_id() === get_current_user_id() ) {
2294
-			    update_post_meta($invoice_id,'_wpinv_is_viewed', 1);
2295
-		    } else if ( !wpinv_require_login_to_checkout() && isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2296
-			    update_post_meta($invoice_id,'_wpinv_is_viewed', 1);
2292
+	    if (is_user_logged_in()) {
2293
+		    if ((int)$invoice->get_user_id() === get_current_user_id()) {
2294
+			    update_post_meta($invoice_id, '_wpinv_is_viewed', 1);
2295
+		    } else if (!wpinv_require_login_to_checkout() && isset($_GET['invoice_key']) && $_GET['invoice_key'] === $invoice->get_key()) {
2296
+			    update_post_meta($invoice_id, '_wpinv_is_viewed', 1);
2297 2297
 		    }
2298 2298
 	    } else {
2299
-		    if ( !wpinv_require_login_to_checkout() && isset( $_GET['invoice_key'] ) && $_GET['invoice_key'] === $invoice->get_key() ) {
2300
-			    update_post_meta($invoice_id,'_wpinv_is_viewed', 1);
2299
+		    if (!wpinv_require_login_to_checkout() && isset($_GET['invoice_key']) && $_GET['invoice_key'] === $invoice->get_key()) {
2300
+			    update_post_meta($invoice_id, '_wpinv_is_viewed', 1);
2301 2301
 		    }
2302 2302
 	    }
2303 2303
     }
2304 2304
 
2305 2305
 }
2306
-add_action( 'template_redirect', 'wpinv_mark_invoice_viewed' );
2306
+add_action('template_redirect', 'wpinv_mark_invoice_viewed');
2307 2307
 
2308
-function wpinv_get_subscription( $invoice, $by_parent = false ) {
2309
-    if ( empty( $invoice ) ) {
2308
+function wpinv_get_subscription($invoice, $by_parent = false) {
2309
+    if (empty($invoice)) {
2310 2310
         return false;
2311 2311
     }
2312 2312
     
2313
-    if ( ! is_object( $invoice ) && is_scalar( $invoice ) ) {
2314
-        $invoice = wpinv_get_invoice( $invoice );
2313
+    if (!is_object($invoice) && is_scalar($invoice)) {
2314
+        $invoice = wpinv_get_invoice($invoice);
2315 2315
     }
2316 2316
     
2317
-    if ( !( is_object( $invoice ) && ! empty( $invoice->ID ) && $invoice->is_recurring() ) ) {
2317
+    if (!(is_object($invoice) && !empty($invoice->ID) && $invoice->is_recurring())) {
2318 2318
         return false;
2319 2319
     }
2320 2320
     
2321
-    $invoice_id = ! $by_parent && ! empty( $invoice->parent_invoice ) ? $invoice->parent_invoice : $invoice->ID;
2321
+    $invoice_id = !$by_parent && !empty($invoice->parent_invoice) ? $invoice->parent_invoice : $invoice->ID;
2322 2322
     
2323 2323
     $subs_db    = new WPInv_Subscriptions_DB;
2324
-    $subs       = $subs_db->get_subscriptions( array( 'parent_payment_id' => $invoice_id, 'number' => 1 ) );
2324
+    $subs       = $subs_db->get_subscriptions(array('parent_payment_id' => $invoice_id, 'number' => 1));
2325 2325
     
2326
-    if ( ! empty( $subs ) ) {
2327
-        return reset( $subs );
2326
+    if (!empty($subs)) {
2327
+        return reset($subs);
2328 2328
     }
2329 2329
     
2330 2330
     return false;
2331 2331
 }
2332 2332
 
2333
-function wpinv_filter_posts_clauses( $clauses, $wp_query ) {
2333
+function wpinv_filter_posts_clauses($clauses, $wp_query) {
2334 2334
     global $wpdb;
2335 2335
 
2336
-    if ( ! empty( $wp_query->query_vars['orderby'] ) && $wp_query->query_vars['orderby'] == 'invoice_date' ) {
2337
-        if ( !empty( $clauses['join'] ) ) {
2336
+    if (!empty($wp_query->query_vars['orderby']) && $wp_query->query_vars['orderby'] == 'invoice_date') {
2337
+        if (!empty($clauses['join'])) {
2338 2338
             $clauses['join'] .= " ";
2339 2339
         }
2340 2340
 
2341
-        if ( !empty( $clauses['fields'] ) ) {
2341
+        if (!empty($clauses['fields'])) {
2342 2342
             $clauses['fields'] .= ", ";
2343 2343
         }
2344 2344
 
@@ -2349,4 +2349,4 @@  discard block
 block discarded – undo
2349 2349
 
2350 2350
     return $clauses;
2351 2351
 }
2352
-add_filter( 'posts_clauses', 'wpinv_filter_posts_clauses', 10, 2 );
2353 2352
\ No newline at end of file
2353
+add_filter('posts_clauses', 'wpinv_filter_posts_clauses', 10, 2);
2354 2354
\ No newline at end of file
Please login to merge, or discard this patch.
includes/class-wpinv-cli.php 1 patch
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  */
8 8
  
9 9
 // MUST have WordPress.
10
-if ( !defined( 'WPINC' ) ) {
10
+if (!defined('WPINC')) {
11 11
     exit;
12 12
 }
13 13
 
@@ -24,40 +24,40 @@  discard block
 block discarded – undo
24 24
      *  @param Array $assoc_args Key value arguments stored in associated array format.
25 25
      *  @since 1.0.13
26 26
      */
27
-    public function insert_invoice( $args, $assoc_args ) {
27
+    public function insert_invoice($args, $assoc_args) {
28 28
 
29 29
         // Fetch invoice data from the args
30
-        $invoice_data = wp_unslash( $assoc_args );
30
+        $invoice_data = wp_unslash($assoc_args);
31 31
 
32 32
         // Abort if no invoice data is provided
33
-        if( empty( $invoice_data ) ) {
34
-            return WP_CLI::error( __( 'Invoice data not provided', 'invoicing' ) );
33
+        if (empty($invoice_data)) {
34
+            return WP_CLI::error(__('Invoice data not provided', 'invoicing'));
35 35
         }
36 36
 
37 37
         //Cart details
38
-        if( !empty( $invoice_data['cart_details'] ) ) {
39
-            $invoice_data['cart_details'] = json_decode( $invoice_data['cart_details'], true );
38
+        if (!empty($invoice_data['cart_details'])) {
39
+            $invoice_data['cart_details'] = json_decode($invoice_data['cart_details'], true);
40 40
         }
41 41
 
42 42
         //User details
43
-        if( !empty( $invoice_data['user_info'] ) ) {
44
-            $invoice_data['user_info'] = json_decode( $invoice_data['user_info'], true );
43
+        if (!empty($invoice_data['user_info'])) {
44
+            $invoice_data['user_info'] = json_decode($invoice_data['user_info'], true);
45 45
         }
46 46
 
47 47
         //Payment info
48
-        if( !empty( $invoice_data['payment_details'] ) ) {
49
-            $invoice_data['payment_details'] = json_decode( $invoice_data['payment_details'], true );
48
+        if (!empty($invoice_data['payment_details'])) {
49
+            $invoice_data['payment_details'] = json_decode($invoice_data['payment_details'], true);
50 50
         }
51 51
 
52 52
         // Try creating the invoice
53
-        $invoice = wpinv_insert_invoice( $invoice_data, true );
53
+        $invoice = wpinv_insert_invoice($invoice_data, true);
54 54
 
55
-        if ( is_wp_error( $invoice ) ) {
56
-            return WP_CLI::error( $invoice->get_error_message() );
55
+        if (is_wp_error($invoice)) {
56
+            return WP_CLI::error($invoice->get_error_message());
57 57
         }
58 58
 
59
-        $message = sprintf( __( 'Invoice %s created', 'invoicing' ), $invoice->ID );
60
-        WP_CLI::success( $message );
59
+        $message = sprintf(__('Invoice %s created', 'invoicing'), $invoice->ID);
60
+        WP_CLI::success($message);
61 61
     }
62 62
     
63 63
     
Please login to merge, or discard this patch.
includes/class-wpinv-api.php 2 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -46,15 +46,15 @@
 block discarded – undo
46 46
     }
47 47
 
48 48
 
49
-	/**
50
-	 * Registers routes
51
-	 *
49
+    /**
50
+     * Registers routes
51
+     *
52 52
      * @since 1.0.13
53
-	 */
54
-	public function register_rest_routes() {
53
+     */
54
+    public function register_rest_routes() {
55 55
 
56
-		//Invoices
57
-		$this->invoices_controller->register_routes();
56
+        //Invoices
57
+        $this->invoices_controller->register_routes();
58 58
         
59 59
     }
60 60
 
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
  */
8 8
  
9 9
 // MUST have WordPress.
10
-if ( !defined( 'WPINC' ) ) {
10
+if (!defined('WPINC')) {
11 11
     exit;
12 12
 }
13 13
 
@@ -19,7 +19,7 @@  discard block
 block discarded – undo
19 19
     /**
20 20
      * @param string A prefix for our REST routes
21 21
      */
22
-    public $api_namespace    = '';
22
+    public $api_namespace = '';
23 23
 
24 24
     /**
25 25
      * @param WPInv_REST_Invoice_Controller Invoices controller
@@ -32,17 +32,17 @@  discard block
 block discarded – undo
32 32
      * @since 1.0.13
33 33
      * Sets the API namespace and inits hooks
34 34
      */
35
-    public function __construct( $api_namespace = 'invoicing/v1' ) {
35
+    public function __construct($api_namespace = 'invoicing/v1') {
36 36
 
37 37
         // Include controllers and related files
38 38
         $this->includes();
39 39
 
40 40
         // Set up class variables
41
-        $this->api_namespace       = apply_filters( 'wpinv_rest_api_namespace', $api_namespace );
42
-        $this->invoices_controller = new WPInv_REST_Invoice_Controller( $this->api_namespace );
41
+        $this->api_namespace       = apply_filters('wpinv_rest_api_namespace', $api_namespace);
42
+        $this->invoices_controller = new WPInv_REST_Invoice_Controller($this->api_namespace);
43 43
 
44 44
         //Register REST routes
45
-        add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
45
+        add_action('rest_api_init', array($this, 'register_rest_routes'));
46 46
     }
47 47
 
48 48
 
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
     protected function includes() {
68 68
         
69 69
         // Invoices
70
-        require_once( WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php' );
70
+        require_once(WPINV_PLUGIN_DIR . 'includes/api/class-wpinv-rest-invoice-controller.php');
71 71
 
72 72
     }
73 73
     
Please login to merge, or discard this patch.
includes/class-wpinv.php 1 patch
Spacing   +203 added lines, -203 removed lines patch added patch discarded remove patch
@@ -7,15 +7,15 @@  discard block
 block discarded – undo
7 7
  */
8 8
  
9 9
 // MUST have WordPress.
10
-if ( !defined( 'WPINC' ) ) {
11
-    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
10
+if (!defined('WPINC')) {
11
+    exit('Do NOT access this file directly: ' . basename(__FILE__));
12 12
 }
13 13
 
14 14
 class WPInv_Plugin {
15 15
     private static $instance;
16 16
     
17 17
     public static function run() {
18
-        if ( !isset( self::$instance ) && !( self::$instance instanceof WPInv_Plugin ) ) {
18
+        if (!isset(self::$instance) && !(self::$instance instanceof WPInv_Plugin)) {
19 19
             self::$instance = new WPInv_Plugin;
20 20
             self::$instance->includes();
21 21
             self::$instance->actions();
@@ -32,31 +32,31 @@  discard block
 block discarded – undo
32 32
     }
33 33
     
34 34
     public function define_constants() {
35
-        define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) );
36
-        define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) );
35
+        define('WPINV_PLUGIN_DIR', plugin_dir_path(WPINV_PLUGIN_FILE));
36
+        define('WPINV_PLUGIN_URL', plugin_dir_url(WPINV_PLUGIN_FILE));
37 37
     }
38 38
     
39 39
     private function actions() {
40 40
         /* Internationalize the text strings used. */
41
-        add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
41
+        add_action('plugins_loaded', array(&$this, 'plugins_loaded'));
42 42
         
43 43
         /* Perform actions on admin initialization. */
44
-        add_action( 'admin_init', array( &$this, 'admin_init') );
45
-        add_action( 'init', array( &$this, 'init' ), 3 );
46
-        add_action( 'init', array( &$this, 'wpinv_actions' ) );
44
+        add_action('admin_init', array(&$this, 'admin_init'));
45
+        add_action('init', array(&$this, 'init'), 3);
46
+        add_action('init', array(&$this, 'wpinv_actions'));
47 47
         
48
-        if ( class_exists( 'BuddyPress' ) ) {
49
-            add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) );
48
+        if (class_exists('BuddyPress')) {
49
+            add_action('bp_include', array(&$this, 'bp_invoicing_init'));
50 50
         }
51 51
 
52
-        add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
53
-        add_action( 'widgets_init', array( &$this, 'register_widgets' ) );
52
+        add_action('wp_enqueue_scripts', array(&$this, 'enqueue_scripts'));
53
+        add_action('widgets_init', array(&$this, 'register_widgets'));
54 54
 
55
-        if ( is_admin() ) {
56
-            add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) );
57
-            add_action( 'admin_body_class', array( &$this, 'admin_body_class' ) );
55
+        if (is_admin()) {
56
+            add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts'));
57
+            add_action('admin_body_class', array(&$this, 'admin_body_class'));
58 58
         } else {
59
-            add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) );
59
+            add_filter('pre_get_posts', array(&$this, 'pre_get_posts'));
60 60
         }
61 61
         
62 62
         /**
@@ -66,19 +66,19 @@  discard block
 block discarded – undo
66 66
          *
67 67
          * @param WPInv_Plugin $this. Current WPInv_Plugin instance. Passed by reference.
68 68
          */
69
-        do_action_ref_array( 'wpinv_actions', array( &$this ) );
69
+        do_action_ref_array('wpinv_actions', array(&$this));
70 70
 
71
-        add_action( 'admin_init', array( &$this, 'activation_redirect') );
71
+        add_action('admin_init', array(&$this, 'activation_redirect'));
72 72
     }
73 73
     
74 74
     public function plugins_loaded() {
75 75
         /* Internationalize the text strings used. */
76 76
         $this->load_textdomain();
77 77
 
78
-        do_action( 'wpinv_loaded' );
78
+        do_action('wpinv_loaded');
79 79
 
80 80
         // Fix oxygen page builder conflict
81
-        if ( function_exists( 'ct_css_output' ) ) {
81
+        if (function_exists('ct_css_output')) {
82 82
             wpinv_oxygen_fix_conflict();
83 83
         }
84 84
     }
@@ -88,228 +88,228 @@  discard block
 block discarded – undo
88 88
      *
89 89
      * @since 1.0
90 90
      */
91
-    public function load_textdomain( $locale = NULL ) {
92
-        if ( empty( $locale ) ) {
93
-            $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
91
+    public function load_textdomain($locale = NULL) {
92
+        if (empty($locale)) {
93
+            $locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale();
94 94
         }
95 95
 
96
-        $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' );
96
+        $locale = apply_filters('plugin_locale', $locale, 'invoicing');
97 97
         
98
-        unload_textdomain( 'invoicing' );
99
-        load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' );
100
-        load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' );
98
+        unload_textdomain('invoicing');
99
+        load_textdomain('invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo');
100
+        load_plugin_textdomain('invoicing', false, WPINV_PLUGIN_DIR . 'languages');
101 101
         
102 102
         /**
103 103
          * Define language constants.
104 104
          */
105
-        require_once( WPINV_PLUGIN_DIR . 'language.php' );
105
+        require_once(WPINV_PLUGIN_DIR . 'language.php');
106 106
     }
107 107
         
108 108
     public function includes() {
109 109
         global $wpinv_options;
110 110
         
111
-        require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' );
111
+        require_once(WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php');
112 112
         $wpinv_options = wpinv_get_settings();
113 113
         
114
-        require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' );
115
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-post-types.php' );
116
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' );
117
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' );
118
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' );
119
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' );
120
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' );
121
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' );
122
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-invoice-functions.php' );
123
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' );
124
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' );
125
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' );
126
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' );
127
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' );
128
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-error-functions.php' );
129
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-invoice.php' );
130
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-item.php' );
131
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-notes.php' );
132
-        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' );
133
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' );
134
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' );
135
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' );
136
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' );
137
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' );
138
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' );
139
-        require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
140
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' );
141
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions.php' );
142
-        require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' );
143
-        require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-subscriptions-list-table.php' );
144
-        require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' );
145
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' );
146
-        require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' );
147
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' );
148
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' );
149
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' );
150
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' );
151
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' );
152
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' );
153
-	    require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' );
154
-
155
-        if ( !class_exists( 'WPInv_EUVat' ) ) {
156
-            require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' );
114
+        require_once(WPINV_PLUGIN_DIR . 'vendor/autoload.php');
115
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-post-types.php');
116
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php');
117
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php');
118
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php');
119
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php');
120
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php');
121
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php');
122
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-invoice-functions.php');
123
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php');
124
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php');
125
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php');
126
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php');
127
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php');
128
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-error-functions.php');
129
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-invoice.php');
130
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-item.php');
131
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-notes.php');
132
+        require_once(WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php');
133
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php');
134
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php');
135
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php');
136
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php');
137
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php');
138
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php');
139
+        require_once(WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php');
140
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php');
141
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions.php');
142
+        require_once(WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php');
143
+        require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-subscriptions-list-table.php');
144
+        require_once(WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php');
145
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php');
146
+        require_once(WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php');
147
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php');
148
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/checkout.php');
149
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-history.php');
150
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php');
151
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php');
152
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/subscriptions.php');
153
+	    require_once(WPINV_PLUGIN_DIR . 'widgets/buy-item.php');
154
+
155
+        if (!class_exists('WPInv_EUVat')) {
156
+            require_once(WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php');
157 157
         }
158 158
         
159
-        $gateways = array_keys( wpinv_get_enabled_payment_gateways() );
160
-        if ( !empty( $gateways ) ) {
161
-            foreach ( $gateways as $gateway ) {
162
-                if ( $gateway == 'manual' ) {
159
+        $gateways = array_keys(wpinv_get_enabled_payment_gateways());
160
+        if (!empty($gateways)) {
161
+            foreach ($gateways as $gateway) {
162
+                if ($gateway == 'manual') {
163 163
                     continue;
164 164
                 }
165 165
                 
166 166
                 $gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php';
167 167
                 
168
-                if ( file_exists( $gateway_file ) ) {
169
-                    require_once( $gateway_file );
168
+                if (file_exists($gateway_file)) {
169
+                    require_once($gateway_file);
170 170
                 }
171 171
             }
172 172
         }
173
-        require_once( WPINV_PLUGIN_DIR . 'includes/gateways/manual.php' );
173
+        require_once(WPINV_PLUGIN_DIR . 'includes/gateways/manual.php');
174 174
         
175
-        if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
176
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' );
177
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' );
178
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-meta-boxes.php' );
175
+        if (is_admin() || (defined('WP_CLI') && WP_CLI)) {
176
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php');
177
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php');
178
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/admin-meta-boxes.php');
179 179
             //require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-recurring-admin.php' );
180
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-details.php' );
181
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-items.php' );
182
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' );
183
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-address.php' );
184
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' );
185
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' );
186
-            require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' );
180
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-details.php');
181
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-items.php');
182
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php');
183
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-address.php');
184
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php');
185
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php');
186
+            require_once(WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php');
187 187
             //require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' );
188 188
             // load the user class only on the users.php page
189 189
             global $pagenow;
190
-            if($pagenow=='users.php'){
190
+            if ($pagenow == 'users.php') {
191 191
                 new WPInv_Admin_Users();
192 192
             }
193 193
         }
194 194
 
195 195
         // Register cli commands
196
-        if ( defined( 'WP_CLI' ) && WP_CLI ) {
197
-            require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' );
198
-            WP_CLI::add_command( 'invoicing', 'WPInv_CLI' );
196
+        if (defined('WP_CLI') && WP_CLI) {
197
+            require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php');
198
+            WP_CLI::add_command('invoicing', 'WPInv_CLI');
199 199
         }
200 200
         
201 201
         // include css inliner
202
-        if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
203
-            include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' );
202
+        if (!class_exists('Emogrifier') && class_exists('DOMDocument')) {
203
+            include_once(WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php');
204 204
         }
205 205
         
206
-        require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' );
206
+        require_once(WPINV_PLUGIN_DIR . 'includes/admin/install.php');
207 207
     }
208 208
     
209 209
     public function init() {
210 210
     }
211 211
     
212 212
     public function admin_init() {
213
-        add_action( 'admin_print_scripts-edit.php', array( &$this, 'admin_print_scripts_edit_php' ) );
213
+        add_action('admin_print_scripts-edit.php', array(&$this, 'admin_print_scripts_edit_php'));
214 214
     }
215 215
 
216 216
     public function activation_redirect() {
217 217
         // Bail if no activation redirect
218
-        if ( !get_transient( '_wpinv_activation_redirect' ) ) {
218
+        if (!get_transient('_wpinv_activation_redirect')) {
219 219
             return;
220 220
         }
221 221
 
222 222
         // Delete the redirect transient
223
-        delete_transient( '_wpinv_activation_redirect' );
223
+        delete_transient('_wpinv_activation_redirect');
224 224
 
225 225
         // Bail if activating from network, or bulk
226
-        if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
226
+        if (is_network_admin() || isset($_GET['activate-multi'])) {
227 227
             return;
228 228
         }
229 229
 
230
-        wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) );
230
+        wp_safe_redirect(admin_url('admin.php?page=wpinv-settings&tab=general'));
231 231
         exit;
232 232
     }
233 233
     
234 234
     public function enqueue_scripts() {
235
-        $suffix       = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
235
+        $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
236 236
         
237
-        wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), WPINV_VERSION );
238
-        wp_enqueue_style( 'wpinv_front_style' );
237
+        wp_register_style('wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), WPINV_VERSION);
238
+        wp_enqueue_style('wpinv_front_style');
239 239
                
240 240
         // Register scripts
241
-        wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
242
-        wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ),  WPINV_VERSION );
241
+        wp_register_script('jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array('jquery'), '2.70', true);
242
+        wp_register_script('wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array('jquery'), WPINV_VERSION);
243 243
 
244 244
         $localize                         = array();
245
-        $localize['ajax_url']             = admin_url( 'admin-ajax.php' );
246
-        $localize['nonce']                = wp_create_nonce( 'wpinv-nonce' );
245
+        $localize['ajax_url']             = admin_url('admin-ajax.php');
246
+        $localize['nonce']                = wp_create_nonce('wpinv-nonce');
247 247
         $localize['currency_symbol']      = wpinv_currency_symbol();
248 248
         $localize['currency_pos']         = wpinv_currency_position();
249 249
         $localize['thousand_sep']         = wpinv_thousands_separator();
250 250
         $localize['decimal_sep']          = wpinv_decimal_separator();
251 251
         $localize['decimals']             = wpinv_decimals();
252
-        $localize['txtComplete']          = __( 'Complete', 'invoicing' );
252
+        $localize['txtComplete']          = __('Complete', 'invoicing');
253 253
         $localize['UseTaxes']             = wpinv_use_taxes();
254
-        $localize['checkoutNonce']        = wp_create_nonce( 'wpinv_checkout_nonce' );
254
+        $localize['checkoutNonce']        = wp_create_nonce('wpinv_checkout_nonce');
255 255
 
256
-        $localize = apply_filters( 'wpinv_front_js_localize', $localize );
256
+        $localize = apply_filters('wpinv_front_js_localize', $localize);
257 257
         
258
-        wp_enqueue_script( 'jquery-blockui' );
258
+        wp_enqueue_script('jquery-blockui');
259 259
         $autofill_api = wpinv_get_option('address_autofill_api');
260 260
         $autofill_active = wpinv_get_option('address_autofill_active');
261
-        if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) {
262
-            if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) {
263
-                wp_dequeue_script( 'google-maps-api' );
261
+        if (isset($autofill_active) && 1 == $autofill_active && !empty($autofill_api) && wpinv_is_checkout()) {
262
+            if (wp_script_is('google-maps-api', 'enqueued')) {
263
+                wp_dequeue_script('google-maps-api');
264 264
             }
265
-            wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false );
266
-            wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true );
265
+            wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array('jquery'), '', false);
266
+            wp_enqueue_script('google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array('jquery', 'google-maps-api'), '', true);
267 267
         }
268 268
 
269
-        wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
270
-        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
269
+        wp_enqueue_style("select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all');
270
+        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array('jquery'), WPINV_VERSION);
271 271
 
272
-        wp_enqueue_script( 'wpinv-front-script' );
273
-        wp_localize_script( 'wpinv-front-script', 'WPInv', $localize );
272
+        wp_enqueue_script('wpinv-front-script');
273
+        wp_localize_script('wpinv-front-script', 'WPInv', $localize);
274 274
     }
275 275
 
276 276
     public function admin_enqueue_scripts() {
277 277
         global $post, $pagenow;
278 278
         
279 279
         $post_type  = wpinv_admin_post_type();
280
-        $suffix     = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
281
-        $page       = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : '';
280
+        $suffix     = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
281
+        $page       = isset($_GET['page']) ? strtolower($_GET['page']) : '';
282 282
 
283 283
         $jquery_ui_css = false;
284
-        if ( ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $post_type == 'wpi_discount' ) && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) {
284
+        if (($post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $post_type == 'wpi_discount') && ($pagenow == 'post-new.php' || $pagenow == 'post.php')) {
285 285
             $jquery_ui_css = true;
286
-        } else if ( $page == 'wpinv-settings' || $page == 'wpinv-reports' ) {
286
+        } else if ($page == 'wpinv-settings' || $page == 'wpinv-reports') {
287 287
             $jquery_ui_css = true;
288 288
         }
289
-        if ( $jquery_ui_css ) {
290
-            wp_register_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16' );
291
-            wp_enqueue_style( 'jquery-ui-css' );
289
+        if ($jquery_ui_css) {
290
+            wp_register_style('jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16');
291
+            wp_enqueue_style('jquery-ui-css');
292 292
         }
293 293
 
294
-        wp_register_style( 'wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION );
295
-        wp_enqueue_style( 'wpinv_meta_box_style' );
294
+        wp_register_style('wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION);
295
+        wp_enqueue_style('wpinv_meta_box_style');
296 296
         
297
-        wp_register_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array(), WPINV_VERSION );
298
-        wp_enqueue_style( 'wpinv_admin_style' );
297
+        wp_register_style('wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array(), WPINV_VERSION);
298
+        wp_enqueue_style('wpinv_admin_style');
299 299
 
300
-        $enqueue = ( $post_type == 'wpi_discount' || $post_type == 'wpi_invoice' && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) );
301
-        if ( $page == 'wpinv-subscriptions' ) {
302
-            wp_enqueue_script( 'jquery-ui-datepicker' );
300
+        $enqueue = ($post_type == 'wpi_discount' || $post_type == 'wpi_invoice' && ($pagenow == 'post-new.php' || $pagenow == 'post.php'));
301
+        if ($page == 'wpinv-subscriptions') {
302
+            wp_enqueue_script('jquery-ui-datepicker');
303 303
         }
304 304
         
305
-        if ( $enqueue_datepicker = apply_filters( 'wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue ) ) {
306
-            wp_enqueue_script( 'jquery-ui-datepicker' );
305
+        if ($enqueue_datepicker = apply_filters('wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue)) {
306
+            wp_enqueue_script('jquery-ui-datepicker');
307 307
         }
308 308
 
309
-        wp_enqueue_style( 'wp-color-picker' );
310
-        wp_enqueue_script( 'wp-color-picker' );
309
+        wp_enqueue_style('wp-color-picker');
310
+        wp_enqueue_script('wp-color-picker');
311 311
         
312
-        wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true );
312
+        wp_register_script('jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array('jquery'), '2.70', true);
313 313
 
314 314
         if (($post_type == 'wpi_invoice' || $post_type == 'wpi_quote') && ($pagenow == 'post-new.php' || $pagenow == 'post.php')) {
315 315
             $autofill_api = wpinv_get_option('address_autofill_api');
@@ -320,20 +320,20 @@  discard block
 block discarded – undo
320 320
             }
321 321
         }
322 322
 
323
-        wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' );
324
-        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION );
323
+        wp_enqueue_style("select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all');
324
+        wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array('jquery'), WPINV_VERSION);
325 325
 
326
-        wp_register_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-blockui','jquery-ui-tooltip' ),  WPINV_VERSION );
327
-        wp_enqueue_script( 'wpinv-admin-script' );
326
+        wp_register_script('wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array('jquery', 'jquery-blockui', 'jquery-ui-tooltip'), WPINV_VERSION);
327
+        wp_enqueue_script('wpinv-admin-script');
328 328
         
329 329
         $localize                               = array();
330
-        $localize['ajax_url']                   = admin_url( 'admin-ajax.php' );
331
-        $localize['post_ID']                    = isset( $post->ID ) ? $post->ID : '';
332
-        $localize['wpinv_nonce']                = wp_create_nonce( 'wpinv-nonce' );
333
-        $localize['add_invoice_note_nonce']     = wp_create_nonce( 'add-invoice-note' );
334
-        $localize['delete_invoice_note_nonce']  = wp_create_nonce( 'delete-invoice-note' );
335
-        $localize['invoice_item_nonce']         = wp_create_nonce( 'invoice-item' );
336
-        $localize['billing_details_nonce']      = wp_create_nonce( 'get-billing-details' );
330
+        $localize['ajax_url']                   = admin_url('admin-ajax.php');
331
+        $localize['post_ID']                    = isset($post->ID) ? $post->ID : '';
332
+        $localize['wpinv_nonce']                = wp_create_nonce('wpinv-nonce');
333
+        $localize['add_invoice_note_nonce']     = wp_create_nonce('add-invoice-note');
334
+        $localize['delete_invoice_note_nonce']  = wp_create_nonce('delete-invoice-note');
335
+        $localize['invoice_item_nonce']         = wp_create_nonce('invoice-item');
336
+        $localize['billing_details_nonce']      = wp_create_nonce('get-billing-details');
337 337
         $localize['tax']                        = wpinv_tax_amount();
338 338
         $localize['discount']                   = wpinv_discount_amount();
339 339
         $localize['currency_symbol']            = wpinv_currency_symbol();
@@ -341,69 +341,69 @@  discard block
 block discarded – undo
341 341
         $localize['thousand_sep']               = wpinv_thousands_separator();
342 342
         $localize['decimal_sep']                = wpinv_decimal_separator();
343 343
         $localize['decimals']                   = wpinv_decimals();
344
-        $localize['save_invoice']               = __( 'Save Invoice', 'invoicing' );
345
-        $localize['status_publish']             = wpinv_status_nicename( 'publish' );
346
-        $localize['status_pending']             = wpinv_status_nicename( 'wpi-pending' );
347
-        $localize['delete_tax_rate']            = __( 'Are you sure you wish to delete this tax rate?', 'invoicing' );
348
-        $localize['OneItemMin']                 = __( 'Invoice must contain at least one item', 'invoicing' );
349
-        $localize['DeleteInvoiceItem']          = __( 'Are you sure you wish to delete this item?', 'invoicing' );
350
-        $localize['FillBillingDetails']         = __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' );
351
-        $localize['confirmCalcTotals']          = __( 'Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing' );
352
-        $localize['AreYouSure']                 = __( 'Are you sure?', 'invoicing' );
353
-        $localize['emptyInvoice']               = __( 'Add at least one item to save invoice!', 'invoicing' );
354
-        $localize['errDeleteItem']              = __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' );
355
-        $localize['delete_subscription']        = __( 'Are you sure you want to delete this subscription?', 'invoicing' );
356
-        $localize['action_edit']                = __( 'Edit', 'invoicing' );
357
-        $localize['action_cancel']              = __( 'Cancel', 'invoicing' );
358
-
359
-        $localize = apply_filters( 'wpinv_admin_js_localize', $localize );
360
-
361
-        wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', $localize );
362
-
363
-        if ( $page == 'wpinv-subscriptions' ) {
364
-            wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ),  WPINV_VERSION );
365
-            wp_enqueue_script( 'wpinv-sub-admin-script' );
344
+        $localize['save_invoice']               = __('Save Invoice', 'invoicing');
345
+        $localize['status_publish']             = wpinv_status_nicename('publish');
346
+        $localize['status_pending']             = wpinv_status_nicename('wpi-pending');
347
+        $localize['delete_tax_rate']            = __('Are you sure you wish to delete this tax rate?', 'invoicing');
348
+        $localize['OneItemMin']                 = __('Invoice must contain at least one item', 'invoicing');
349
+        $localize['DeleteInvoiceItem']          = __('Are you sure you wish to delete this item?', 'invoicing');
350
+        $localize['FillBillingDetails']         = __('Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing');
351
+        $localize['confirmCalcTotals']          = __('Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing');
352
+        $localize['AreYouSure']                 = __('Are you sure?', 'invoicing');
353
+        $localize['emptyInvoice']               = __('Add at least one item to save invoice!', 'invoicing');
354
+        $localize['errDeleteItem']              = __('This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing');
355
+        $localize['delete_subscription']        = __('Are you sure you want to delete this subscription?', 'invoicing');
356
+        $localize['action_edit']                = __('Edit', 'invoicing');
357
+        $localize['action_cancel']              = __('Cancel', 'invoicing');
358
+
359
+        $localize = apply_filters('wpinv_admin_js_localize', $localize);
360
+
361
+        wp_localize_script('wpinv-admin-script', 'WPInv_Admin', $localize);
362
+
363
+        if ($page == 'wpinv-subscriptions') {
364
+            wp_register_script('wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array('wpinv-admin-script'), WPINV_VERSION);
365
+            wp_enqueue_script('wpinv-sub-admin-script');
366 366
         }
367 367
     }
368 368
     
369
-    public function admin_body_class( $classes ) {
369
+    public function admin_body_class($classes) {
370 370
         global $pagenow, $post, $current_screen;
371 371
         
372
-        if ( !empty( $current_screen->post_type ) && ( $current_screen->post_type == 'wpi_invoice' || $current_screen->post_type == 'wpi_quote' ) ) {
372
+        if (!empty($current_screen->post_type) && ($current_screen->post_type == 'wpi_invoice' || $current_screen->post_type == 'wpi_quote')) {
373 373
             $classes .= ' wpinv-cpt';
374 374
         }
375 375
         
376
-        $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false;
376
+        $page = isset($_GET['page']) ? strtolower($_GET['page']) : false;
377 377
 
378
-        $add_class = $page && $pagenow == 'admin.php' && strpos( $page, 'wpinv-' ) === 0 ? true : false;
379
-        if ( $add_class ) {
380
-            $classes .= ' wpi-' . wpinv_sanitize_key( $page );
378
+        $add_class = $page && $pagenow == 'admin.php' && strpos($page, 'wpinv-') === 0 ? true : false;
379
+        if ($add_class) {
380
+            $classes .= ' wpi-' . wpinv_sanitize_key($page);
381 381
         }
382 382
         
383 383
         $settings_class = array();
384
-        if ( $page == 'wpinv-settings' ) {
385
-            if ( !empty( $_REQUEST['tab'] ) ) {
386
-                $settings_class[] = sanitize_text_field( $_REQUEST['tab'] );
384
+        if ($page == 'wpinv-settings') {
385
+            if (!empty($_REQUEST['tab'])) {
386
+                $settings_class[] = sanitize_text_field($_REQUEST['tab']);
387 387
             }
388 388
             
389
-            if ( !empty( $_REQUEST['section'] ) ) {
390
-                $settings_class[] = sanitize_text_field( $_REQUEST['section'] );
389
+            if (!empty($_REQUEST['section'])) {
390
+                $settings_class[] = sanitize_text_field($_REQUEST['section']);
391 391
             }
392 392
             
393
-            $settings_class[] = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' ? sanitize_text_field( $_REQUEST['wpi_sub'] ) : 'main';
393
+            $settings_class[] = isset($_REQUEST['wpi_sub']) && $_REQUEST['wpi_sub'] !== '' ? sanitize_text_field($_REQUEST['wpi_sub']) : 'main';
394 394
         }
395 395
         
396
-        if ( !empty( $settings_class ) ) {
397
-            $classes .= ' wpi-' . wpinv_sanitize_key( implode( $settings_class, '-' ) );
396
+        if (!empty($settings_class)) {
397
+            $classes .= ' wpi-' . wpinv_sanitize_key(implode($settings_class, '-'));
398 398
         }
399 399
         
400 400
         $post_type = wpinv_admin_post_type();
401 401
 
402
-        if ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $add_class !== false ) {
402
+        if ($post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $add_class !== false) {
403 403
             return $classes .= ' wpinv';
404 404
         }
405 405
         
406
-        if ( $pagenow == 'post.php' && $post_type == 'wpi_item' && !empty( $post ) && !wpinv_item_is_editable( $post ) ) {
406
+        if ($pagenow == 'post.php' && $post_type == 'wpi_item' && !empty($post) && !wpinv_item_is_editable($post)) {
407 407
             $classes .= ' wpi-editable-n';
408 408
         }
409 409
 
@@ -415,21 +415,21 @@  discard block
 block discarded – undo
415 415
     }
416 416
     
417 417
     public function wpinv_actions() {
418
-        if ( isset( $_REQUEST['wpi_action'] ) ) {
419
-            do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST );
418
+        if (isset($_REQUEST['wpi_action'])) {
419
+            do_action('wpinv_' . wpinv_sanitize_key($_REQUEST['wpi_action']), $_REQUEST);
420 420
         }
421 421
     }
422 422
     
423
-    public function pre_get_posts( $wp_query ) {
424
-        if ( !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) {
425
-            $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() );
423
+    public function pre_get_posts($wp_query) {
424
+        if (!empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query()) {
425
+            $wp_query->query_vars['post_status'] = array_keys(wpinv_get_invoice_statuses());
426 426
         }
427 427
         
428 428
         return $wp_query;
429 429
     }
430 430
     
431 431
     public function bp_invoicing_init() {
432
-        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' );
432
+        require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php');
433 433
     }
434 434
 
435 435
 	/**
@@ -437,11 +437,11 @@  discard block
 block discarded – undo
437 437
 	 *
438 438
 	 */
439 439
 	public function register_widgets() {
440
-		register_widget( "WPInv_Checkout_Widget" );
441
-		register_widget( "WPInv_History_Widget" );
442
-		register_widget( "WPInv_Receipt_Widget" );
443
-		register_widget( "WPInv_Subscriptions_Widget" );
444
-		register_widget( "WPInv_Buy_Item_Widget" );
445
-		register_widget( "WPInv_Messages_Widget" );
440
+		register_widget("WPInv_Checkout_Widget");
441
+		register_widget("WPInv_History_Widget");
442
+		register_widget("WPInv_Receipt_Widget");
443
+		register_widget("WPInv_Subscriptions_Widget");
444
+		register_widget("WPInv_Buy_Item_Widget");
445
+		register_widget("WPInv_Messages_Widget");
446 446
 	}
447 447
 }
448 448
\ No newline at end of file
Please login to merge, or discard this patch.
includes/api/class-wpinv-rest-invoice-controller.php 2 patches
Indentation   +1041 added lines, -1041 removed lines patch added patch discarded remove patch
@@ -20,23 +20,23 @@  discard block
 block discarded – undo
20 20
 class WPInv_REST_Invoice_Controller extends WP_REST_Posts_Controller {
21 21
 
22 22
     /**
23
-	 * Post type.
24
-	 *
25
-	 * @var string
26
-	 */
23
+     * Post type.
24
+     *
25
+     * @var string
26
+     */
27 27
     protected $post_type = 'wpi_invoice';
28 28
 
29 29
     /**
30
-	 * Constructor.
31
-	 *
32
-	 * @since 1.0.13
33
-	 *
34
-	 * @param string $namespace Api Namespace
35
-	 */
36
-	public function __construct( $namespace ) {
30
+     * Constructor.
31
+     *
32
+     * @since 1.0.13
33
+     *
34
+     * @param string $namespace Api Namespace
35
+     */
36
+    public function __construct( $namespace ) {
37 37
         
38 38
         // Set api namespace...
39
-		$this->namespace = $namespace;
39
+        $this->namespace = $namespace;
40 40
 
41 41
         // ... and the rest base
42 42
         $this->rest_base = 'invoices';
@@ -44,39 +44,39 @@  discard block
 block discarded – undo
44 44
     }
45 45
     
46 46
     /**
47
-	 * Checks if a given request has access to read invoices.
47
+     * Checks if a given request has access to read invoices.
48 48
      * 
49
-	 *
50
-	 * @since 1.0.13
51
-	 *
52
-	 * @param WP_REST_Request $request Full details about the request.
53
-	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
54
-	 */
55
-	public function get_items_permissions_check( $request ) {
49
+     *
50
+     * @since 1.0.13
51
+     *
52
+     * @param WP_REST_Request $request Full details about the request.
53
+     * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
54
+     */
55
+    public function get_items_permissions_check( $request ) {
56 56
 	
57 57
         $post_type = get_post_type_object( $this->post_type );
58 58
 
59
-		if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
60
-			return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
61
-		}
59
+        if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
60
+            return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
61
+        }
62 62
 
63
-		// Read checks will be evaluated on a per invoice basis
63
+        // Read checks will be evaluated on a per invoice basis
64 64
 
65
-		return true;
65
+        return true;
66 66
 
67 67
     }
68 68
     
69 69
     /**
70
-	 * Retrieves a collection of invoices.
71
-	 *
72
-	 * @since 1.0.13
73
-	 *
74
-	 * @param WP_REST_Request $request Full details about the request.
75
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
76
-	 */
77
-	public function get_items( $request ) {
70
+     * Retrieves a collection of invoices.
71
+     *
72
+     * @since 1.0.13
73
+     *
74
+     * @param WP_REST_Request $request Full details about the request.
75
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
76
+     */
77
+    public function get_items( $request ) {
78 78
 		
79
-		// Retrieve the list of registered invoice query parameters.
79
+        // Retrieve the list of registered invoice query parameters.
80 80
         $registered = $this->get_collection_params();
81 81
         
82 82
         $args       = array();
@@ -89,54 +89,54 @@  discard block
 block discarded – undo
89 89
 
90 90
         }
91 91
 
92
-		/**
93
-		 * Filters the wpinv_get_invoices arguments for invoices requests.
94
-		 *
95
-		 *
96
-		 * @since 1.0.13
97
-		 *
98
-		 *
99
-		 * @param array           $args    Key value array of query var to query value.
100
-		 * @param WP_REST_Request $request The request used.
101
-		 */
92
+        /**
93
+         * Filters the wpinv_get_invoices arguments for invoices requests.
94
+         *
95
+         *
96
+         * @since 1.0.13
97
+         *
98
+         *
99
+         * @param array           $args    Key value array of query var to query value.
100
+         * @param WP_REST_Request $request The request used.
101
+         */
102 102
         $args       = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this );
103 103
 		
104
-		// Special args
105
-		$args[ 'return' ]   = 'objects';
106
-		$args[ 'paginate' ] = true;
104
+        // Special args
105
+        $args[ 'return' ]   = 'objects';
106
+        $args[ 'paginate' ] = true;
107 107
 
108 108
         // Run the query.
109
-		$query = wpinv_get_invoices( $args );
109
+        $query = wpinv_get_invoices( $args );
110 110
 		
111
-		// Prepare the retrieved invoices
112
-		$invoices = array();
113
-		foreach( $query->invoices as $invoice ) {
114
-
115
-			if ( ! $this->check_read_permission( $invoice ) ) {
116
-				continue;
117
-			}
118
-
119
-			$data       = $this->prepare_item_for_response( $invoice, $request );
120
-			$invoices[] = $this->prepare_response_for_collection( $data );
121
-
122
-		}
123
-
124
-		// Prepare the response.
125
-		$response = rest_ensure_response( $invoices );
126
-		$response->header( 'X-WP-Total', (int) $query->total );
127
-		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
128
-
129
-		/**
130
-		 * Filters the responses for invoices requests.
131
-		 *
132
-		 *
133
-		 * @since 1.0.13
134
-		 *
135
-		 *
136
-		 * @param arrWP_REST_Response $response    Response object.
137
-		 * @param WP_REST_Request     $request The request used.
111
+        // Prepare the retrieved invoices
112
+        $invoices = array();
113
+        foreach( $query->invoices as $invoice ) {
114
+
115
+            if ( ! $this->check_read_permission( $invoice ) ) {
116
+                continue;
117
+            }
118
+
119
+            $data       = $this->prepare_item_for_response( $invoice, $request );
120
+            $invoices[] = $this->prepare_response_for_collection( $data );
121
+
122
+        }
123
+
124
+        // Prepare the response.
125
+        $response = rest_ensure_response( $invoices );
126
+        $response->header( 'X-WP-Total', (int) $query->total );
127
+        $response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
128
+
129
+        /**
130
+         * Filters the responses for invoices requests.
131
+         *
132
+         *
133
+         * @since 1.0.13
134
+         *
135
+         *
136
+         * @param arrWP_REST_Response $response    Response object.
137
+         * @param WP_REST_Request     $request The request used.
138 138
          * @param array               $args Array of args used to retrieve the invoices
139
-		 */
139
+         */
140 140
         $response       = apply_filters( "rest_wpinv_invoices_response", $response, $request, $args );
141 141
 
142 142
         return rest_ensure_response( $response );
@@ -144,25 +144,25 @@  discard block
 block discarded – undo
144 144
     }
145 145
 
146 146
     /**
147
-	 * Get the post, if the ID is valid.
148
-	 *
149
-	 * @since 1.0.13
150
-	 *
151
-	 * @param int $invoice_id Supplied ID.
152
-	 * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise.
153
-	 */
154
-	protected function get_post( $invoice_id ) {
147
+     * Get the post, if the ID is valid.
148
+     *
149
+     * @since 1.0.13
150
+     *
151
+     * @param int $invoice_id Supplied ID.
152
+     * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise.
153
+     */
154
+    protected function get_post( $invoice_id ) {
155 155
 		
156
-		$error     = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) );
156
+        $error     = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) );
157 157
 
158 158
         // Ids start from 1
159 159
         if ( (int) $invoice_id <= 0 ) {
160
-			return $error;
161
-		}
160
+            return $error;
161
+        }
162 162
 
163
-		$invoice = wpinv_get_invoice( (int) $invoice_id );
164
-		if ( empty( $invoice ) ) {
165
-			return $error;
163
+        $invoice = wpinv_get_invoice( (int) $invoice_id );
164
+        if ( empty( $invoice ) ) {
165
+            return $error;
166 166
         }
167 167
 
168 168
         return $invoice;
@@ -170,88 +170,88 @@  discard block
 block discarded – undo
170 170
     }
171 171
 
172 172
     /**
173
-	 * Checks if a given request has access to read an invoice.
174
-	 *
175
-	 * @since 1.0.13
176
-	 *
177
-	 * @param WP_REST_Request $request Full details about the request.
178
-	 * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise.
179
-	 */
180
-	public function get_item_permissions_check( $request ) {
173
+     * Checks if a given request has access to read an invoice.
174
+     *
175
+     * @since 1.0.13
176
+     *
177
+     * @param WP_REST_Request $request Full details about the request.
178
+     * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise.
179
+     */
180
+    public function get_item_permissions_check( $request ) {
181 181
 
182 182
         // Retrieve the invoice object.
183 183
         $invoice = $this->get_post( $request['id'] );
184 184
         
185 185
         // Ensure it is valid.
186
-		if ( is_wp_error( $invoice ) ) {
187
-			return $invoice;
188
-		}
186
+        if ( is_wp_error( $invoice ) ) {
187
+            return $invoice;
188
+        }
189 189
 
190
-		if ( $invoice ) {
191
-			return $this->check_read_permission( $invoice );
192
-		}
190
+        if ( $invoice ) {
191
+            return $this->check_read_permission( $invoice );
192
+        }
193 193
 
194
-		return true;
194
+        return true;
195 195
     }
196 196
     
197 197
     /**
198
-	 * Checks if an invoice can be read.
199
-	 * 
200
-	 * An invoice can be read by site admins and owners of the invoice
201
-	 *
202
-	 *
203
-	 * @since 1.0.13
204
-	 *
205
-	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
206
-	 * @return bool Whether the post can be read.
207
-	 */
208
-	public function check_read_permission( $invoice ) {
209
-
210
-		// An invoice can be read by an admin...
211
-		if ( current_user_can( 'manage_options' ) ||  current_user_can( 'view_invoices' ) ) {
212
-			return true;
213
-		}
198
+     * Checks if an invoice can be read.
199
+     * 
200
+     * An invoice can be read by site admins and owners of the invoice
201
+     *
202
+     *
203
+     * @since 1.0.13
204
+     *
205
+     * @param WPInv_Invoice $invoice WPInv_Invoice object.
206
+     * @return bool Whether the post can be read.
207
+     */
208
+    public function check_read_permission( $invoice ) {
209
+
210
+        // An invoice can be read by an admin...
211
+        if ( current_user_can( 'manage_options' ) ||  current_user_can( 'view_invoices' ) ) {
212
+            return true;
213
+        }
214 214
 
215 215
         // ... and the owner of the invoice
216
-		if( get_current_user_id() ===(int) $invoice->get_user_id() ) {
217
-			return true;
218
-		}
216
+        if( get_current_user_id() ===(int) $invoice->get_user_id() ) {
217
+            return true;
218
+        }
219 219
 
220
-		return false;
220
+        return false;
221 221
     }
222 222
     
223 223
     /**
224
-	 * Retrieves a single invoice.
225
-	 *
226
-	 * @since 1.0.13
227
-	 *
228
-	 * @param WP_REST_Request $request Full details about the request.
229
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
230
-	 */
231
-	public function get_item( $request ) {
224
+     * Retrieves a single invoice.
225
+     *
226
+     * @since 1.0.13
227
+     *
228
+     * @param WP_REST_Request $request Full details about the request.
229
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
230
+     */
231
+    public function get_item( $request ) {
232 232
 
233 233
         // Fetch the invoice.
234 234
         $invoice = $this->get_post( $request['id'] );
235 235
         
236 236
         // Abort early if it does not exist
237
-		if ( is_wp_error( $invoice ) ) {
238
-			return $invoice;
239
-		}
240
-
241
-		// Prepare the response
242
-		$response = $this->prepare_item_for_response( $invoice, $request );
243
-		$response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) );
244
-
245
-		/**
246
-		 * Filters the responses for single invoice requests.
247
-		 *
248
-		 *
249
-		 * @since 1.0.13
250
-		 * @var WP_HTTP_Response
251
-		 *
252
-		 * @param WP_HTTP_Response $response Response.
253
-		 * @param WP_REST_Request  $request The request used.
254
-		 */
237
+        if ( is_wp_error( $invoice ) ) {
238
+            return $invoice;
239
+        }
240
+
241
+        // Prepare the response
242
+        $response = $this->prepare_item_for_response( $invoice, $request );
243
+        $response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) );
244
+
245
+        /**
246
+         * Filters the responses for single invoice requests.
247
+         *
248
+         *
249
+         * @since 1.0.13
250
+         * @var WP_HTTP_Response
251
+         *
252
+         * @param WP_HTTP_Response $response Response.
253
+         * @param WP_REST_Request  $request The request used.
254
+         */
255 255
         $response       = apply_filters( "rest_wpinv_get_invoice_response", $response, $request );
256 256
 
257 257
         return rest_ensure_response( $response );
@@ -259,23 +259,23 @@  discard block
 block discarded – undo
259 259
     }
260 260
     
261 261
     /**
262
-	 * Checks if a given request has access to create an invoice.
263
-	 *
264
-	 * @since 1.0.13
265
-	 *
266
-	 * @param WP_REST_Request $request Full details about the request.
267
-	 * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
268
-	 */
269
-	public function create_item_permissions_check( $request ) {
262
+     * Checks if a given request has access to create an invoice.
263
+     *
264
+     * @since 1.0.13
265
+     *
266
+     * @param WP_REST_Request $request Full details about the request.
267
+     * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
268
+     */
269
+    public function create_item_permissions_check( $request ) {
270 270
 	
271
-		if ( ! empty( $request['id'] ) ) {
272
-			return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
273
-		}
271
+        if ( ! empty( $request['id'] ) ) {
272
+            return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
273
+        }
274 274
 
275
-		$post_type = get_post_type_object( $this->post_type );
275
+        $post_type = get_post_type_object( $this->post_type );
276 276
 
277
-		if ( ! current_user_can( $post_type->cap->create_posts ) ) {
278
-			return new WP_Error( 
277
+        if ( ! current_user_can( $post_type->cap->create_posts ) ) {
278
+            return new WP_Error( 
279 279
                 'rest_cannot_create', 
280 280
                 __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), 
281 281
                 array( 
@@ -284,88 +284,88 @@  discard block
 block discarded – undo
284 284
             );
285 285
         }
286 286
 
287
-		return true;
287
+        return true;
288 288
     }
289 289
     
290 290
     /**
291
-	 * Creates a single invoice.
292
-	 *
293
-	 * @since 1.0.13
294
-	 *
295
-	 * @param WP_REST_Request $request Full details about the request.
296
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
297
-	 */
298
-	public function create_item( $request ) {
299
-
300
-		if ( ! empty( $request['id'] ) ) {
301
-			return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
302
-		}
291
+     * Creates a single invoice.
292
+     *
293
+     * @since 1.0.13
294
+     *
295
+     * @param WP_REST_Request $request Full details about the request.
296
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
297
+     */
298
+    public function create_item( $request ) {
299
+
300
+        if ( ! empty( $request['id'] ) ) {
301
+            return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
302
+        }
303 303
 
304
-		$request->set_param( 'context', 'edit' );
304
+        $request->set_param( 'context', 'edit' );
305 305
 
306
-		// Prepare the updated data.
307
-		$invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) );
306
+        // Prepare the updated data.
307
+        $invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) );
308 308
 
309
-		if ( is_wp_error( $invoice_data ) ) {
310
-			return $invoice_data;
311
-		}
309
+        if ( is_wp_error( $invoice_data ) ) {
310
+            return $invoice_data;
311
+        }
312 312
 
313
-		// Try creating the invoice
313
+        // Try creating the invoice
314 314
         $invoice = wpinv_insert_invoice( $invoice_data, true );
315 315
 
316
-		if ( is_wp_error( $invoice ) ) {
316
+        if ( is_wp_error( $invoice ) ) {
317 317
             return $invoice;
318
-		}
319
-
320
-		// Prepare the response
321
-		$response = $this->prepare_item_for_response( $invoice, $request );
322
-
323
-		/**
324
-		 * Fires after a single invoice is created or updated via the REST API.
325
-		 *
326
-		 * @since 1.0.13
327
-		 *
328
-		 * @param WPinv_Invoice   $invoice  Inserted or updated invoice object.
329
-		 * @param WP_REST_Request $request  Request object.
330
-		 * @param bool            $creating True when creating a post, false when updating.
331
-		 */
332
-		do_action( "wpinv_rest_insert_invoice", $invoice, $request, true );
333
-
334
-		/**
335
-		 * Filters the responses for creating single invoice requests.
336
-		 *
337
-		 *
338
-		 * @since 1.0.13
339
-		 *
340
-		 *
341
-		 * @param array           $invoice_data Invoice properties.
342
-		 * @param WP_REST_Request $request The request used.
343
-		 */
318
+        }
319
+
320
+        // Prepare the response
321
+        $response = $this->prepare_item_for_response( $invoice, $request );
322
+
323
+        /**
324
+         * Fires after a single invoice is created or updated via the REST API.
325
+         *
326
+         * @since 1.0.13
327
+         *
328
+         * @param WPinv_Invoice   $invoice  Inserted or updated invoice object.
329
+         * @param WP_REST_Request $request  Request object.
330
+         * @param bool            $creating True when creating a post, false when updating.
331
+         */
332
+        do_action( "wpinv_rest_insert_invoice", $invoice, $request, true );
333
+
334
+        /**
335
+         * Filters the responses for creating single invoice requests.
336
+         *
337
+         *
338
+         * @since 1.0.13
339
+         *
340
+         *
341
+         * @param array           $invoice_data Invoice properties.
342
+         * @param WP_REST_Request $request The request used.
343
+         */
344 344
         $response       = apply_filters( "rest_wpinv_create_invoice_response", $response, $request );
345 345
 
346 346
         return rest_ensure_response( $response );
347
-	}
348
-
349
-	/**
350
-	 * Checks if a given request has access to update an invoice.
351
-	 *
352
-	 * @since 1.0.13
353
-	 *
354
-	 * @param WP_REST_Request $request Full details about the request.
355
-	 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
356
-	 */
357
-	public function update_item_permissions_check( $request ) {
358
-
359
-		// Retrieve the invoice.
360
-		$invoice = $this->get_post( $request['id'] );
361
-		if ( is_wp_error( $invoice ) ) {
362
-			return $invoice;
363
-		}
364
-
365
-		$post_type = get_post_type_object( $this->post_type );
366
-
367
-		if ( ! current_user_can(  $post_type->cap->edit_post, $invoice->ID  ) ) {
368
-			return new WP_Error( 
347
+    }
348
+
349
+    /**
350
+     * Checks if a given request has access to update an invoice.
351
+     *
352
+     * @since 1.0.13
353
+     *
354
+     * @param WP_REST_Request $request Full details about the request.
355
+     * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
356
+     */
357
+    public function update_item_permissions_check( $request ) {
358
+
359
+        // Retrieve the invoice.
360
+        $invoice = $this->get_post( $request['id'] );
361
+        if ( is_wp_error( $invoice ) ) {
362
+            return $invoice;
363
+        }
364
+
365
+        $post_type = get_post_type_object( $this->post_type );
366
+
367
+        if ( ! current_user_can(  $post_type->cap->edit_post, $invoice->ID  ) ) {
368
+            return new WP_Error( 
369 369
                 'rest_cannot_edit', 
370 370
                 __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), 
371 371
                 array( 
@@ -374,162 +374,162 @@  discard block
 block discarded – undo
374 374
             );
375 375
         }
376 376
 
377
-		return true;
378
-	}
379
-
380
-	/**
381
-	 * Updates a single invoice.
382
-	 *
383
-	 * @since 1.0.13
384
-	 *
385
-	 * @param WP_REST_Request $request Full details about the request.
386
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
387
-	 */
388
-	public function update_item( $request ) {
377
+        return true;
378
+    }
379
+
380
+    /**
381
+     * Updates a single invoice.
382
+     *
383
+     * @since 1.0.13
384
+     *
385
+     * @param WP_REST_Request $request Full details about the request.
386
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
387
+     */
388
+    public function update_item( $request ) {
389 389
 		
390
-		// Ensure the invoice exists.
390
+        // Ensure the invoice exists.
391 391
         $valid_check = $this->get_post( $request['id'] );
392 392
         
393 393
         // Abort early if it does not exist
394
-		if ( is_wp_error( $valid_check ) ) {
395
-			return $valid_check;
396
-		}
394
+        if ( is_wp_error( $valid_check ) ) {
395
+            return $valid_check;
396
+        }
397 397
 
398
-		$request->set_param( 'context', 'edit' );
398
+        $request->set_param( 'context', 'edit' );
399 399
 
400
-		// Prepare the updated data.
401
-		$data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) );
400
+        // Prepare the updated data.
401
+        $data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) );
402 402
 
403
-		if ( is_wp_error( $data_to_update ) ) {
404
-			return $data_to_update;
405
-		}
403
+        if ( is_wp_error( $data_to_update ) ) {
404
+            return $data_to_update;
405
+        }
406 406
 
407
-		// Abort if no invoice data is provided
407
+        // Abort if no invoice data is provided
408 408
         if( empty( $data_to_update ) ) {
409 409
             return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) );
410 410
         }
411 411
 
412
-		// Include the invoice ID
413
-		$data_to_update['ID'] = $request['id'];
414
-
415
-		// Update the invoice
416
-		$updated_invoice = wpinv_update_invoice( $data_to_update, true );
417
-
418
-		// Incase the update operation failed...
419
-		if ( is_wp_error( $updated_invoice ) ) {
420
-			return $updated_invoice;
421
-		}
422
-
423
-		// Prepare the response
424
-		$response = $this->prepare_item_for_response( $updated_invoice, $request );
425
-
426
-		/** This action is documented in includes/class-wpinv-rest-invoice-controller.php */
427
-		do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false );
428
-
429
-		/**
430
-		 * Filters the responses for updating single invoice requests.
431
-		 *
432
-		 *
433
-		 * @since 1.0.13
434
-		 *
435
-		 *
436
-		 * @param array           $invoice_data Invoice properties.
437
-		 * @param WP_REST_Request $request The request used.
438
-		 */
412
+        // Include the invoice ID
413
+        $data_to_update['ID'] = $request['id'];
414
+
415
+        // Update the invoice
416
+        $updated_invoice = wpinv_update_invoice( $data_to_update, true );
417
+
418
+        // Incase the update operation failed...
419
+        if ( is_wp_error( $updated_invoice ) ) {
420
+            return $updated_invoice;
421
+        }
422
+
423
+        // Prepare the response
424
+        $response = $this->prepare_item_for_response( $updated_invoice, $request );
425
+
426
+        /** This action is documented in includes/class-wpinv-rest-invoice-controller.php */
427
+        do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false );
428
+
429
+        /**
430
+         * Filters the responses for updating single invoice requests.
431
+         *
432
+         *
433
+         * @since 1.0.13
434
+         *
435
+         *
436
+         * @param array           $invoice_data Invoice properties.
437
+         * @param WP_REST_Request $request The request used.
438
+         */
439 439
         $response       = apply_filters( "wpinv_rest_update_invoice_response", $response, $request );
440 440
 
441 441
         return rest_ensure_response( $response );
442
-	}
443
-
444
-	/**
445
-	 * Checks if a given request has access to delete an invoice.
446
-	 *
447
-	 * @since 1.0.13
448
-	 *
449
-	 * @param WP_REST_Request $request Full details about the request.
450
-	 * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise.
451
-	 */
452
-	public function delete_item_permissions_check( $request ) {
453
-
454
-		// Retrieve the invoice.
455
-		$invoice = $this->get_post( $request['id'] );
456
-		if ( is_wp_error( $invoice ) ) {
457
-			return $invoice;
458
-		}
459
-
460
-		// Ensure the current user can delete invoices
461
-		if ( current_user_can( 'manage_options' ) ||  current_user_can( 'delete_invoices' ) ) {
462
-			return new WP_Error( 
442
+    }
443
+
444
+    /**
445
+     * Checks if a given request has access to delete an invoice.
446
+     *
447
+     * @since 1.0.13
448
+     *
449
+     * @param WP_REST_Request $request Full details about the request.
450
+     * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise.
451
+     */
452
+    public function delete_item_permissions_check( $request ) {
453
+
454
+        // Retrieve the invoice.
455
+        $invoice = $this->get_post( $request['id'] );
456
+        if ( is_wp_error( $invoice ) ) {
457
+            return $invoice;
458
+        }
459
+
460
+        // Ensure the current user can delete invoices
461
+        if ( current_user_can( 'manage_options' ) ||  current_user_can( 'delete_invoices' ) ) {
462
+            return new WP_Error( 
463 463
                 'rest_cannot_delete', 
464 464
                 __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), 
465 465
                 array( 
466 466
                     'status' => rest_authorization_required_code(),
467 467
                 )
468 468
             );
469
-		}
470
-
471
-		return true;
472
-	}
473
-
474
-	/**
475
-	 * Deletes a single invoice.
476
-	 *
477
-	 * @since 1.0.13
478
-	 *
479
-	 * @param WP_REST_Request $request Full details about the request.
480
-	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
481
-	 */
482
-	public function delete_item( $request ) {
469
+        }
470
+
471
+        return true;
472
+    }
473
+
474
+    /**
475
+     * Deletes a single invoice.
476
+     *
477
+     * @since 1.0.13
478
+     *
479
+     * @param WP_REST_Request $request Full details about the request.
480
+     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
481
+     */
482
+    public function delete_item( $request ) {
483 483
 		
484
-		// Retrieve the invoice.
485
-		$invoice = $this->get_post( $request['id'] );
486
-		if ( is_wp_error( $invoice ) ) {
487
-			return $invoice;
488
-		}
484
+        // Retrieve the invoice.
485
+        $invoice = $this->get_post( $request['id'] );
486
+        if ( is_wp_error( $invoice ) ) {
487
+            return $invoice;
488
+        }
489 489
 
490
-		$request->set_param( 'context', 'edit' );
490
+        $request->set_param( 'context', 'edit' );
491 491
 
492
-		// Prepare the invoice id
493
-		$id    = $invoice->ID;
492
+        // Prepare the invoice id
493
+        $id    = $invoice->ID;
494 494
 
495
-		// Prepare the response
496
-		$response = $this->prepare_item_for_response( $invoice, $request );
495
+        // Prepare the response
496
+        $response = $this->prepare_item_for_response( $invoice, $request );
497 497
 
498
-		// Check if the user wants to bypass the trash...
499
-		$force_delete = (bool) $request['force'];
498
+        // Check if the user wants to bypass the trash...
499
+        $force_delete = (bool) $request['force'];
500 500
 
501
-		// Try deleting the invoice.
502
-		$deleted = wp_delete_post( $id, $force_delete );
501
+        // Try deleting the invoice.
502
+        $deleted = wp_delete_post( $id, $force_delete );
503 503
 
504
-		// Abort early if we can't delete the invoice.
505
-		if ( ! $deleted ) {
506
-			return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) );
507
-		}
504
+        // Abort early if we can't delete the invoice.
505
+        if ( ! $deleted ) {
506
+            return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) );
507
+        }
508 508
 
509
-		/**
510
-		 * Fires immediately after a single invoice is deleted or trashed via the REST API.
511
-		 *
512
-		 *
513
-		 * @since 1.0.13
514
-		 *
515
-		 * @param WPInv_Invoice    $invoice  The deleted or trashed invoice.
516
-		 * @param WP_REST_Request  $request  The request sent to the API.
517
-		 */
518
-		do_action( "wpinv_rest_delete_invoice", $invoice, $request );
509
+        /**
510
+         * Fires immediately after a single invoice is deleted or trashed via the REST API.
511
+         *
512
+         *
513
+         * @since 1.0.13
514
+         *
515
+         * @param WPInv_Invoice    $invoice  The deleted or trashed invoice.
516
+         * @param WP_REST_Request  $request  The request sent to the API.
517
+         */
518
+        do_action( "wpinv_rest_delete_invoice", $invoice, $request );
519 519
 
520
-		return $response;
520
+        return $response;
521 521
 
522
-	}
522
+    }
523 523
     
524 524
     
525 525
     /**
526
-	 * Retrieves the query params for the invoices collection.
527
-	 *
528
-	 * @since 1.0.13
529
-	 *
530
-	 * @return array Collection parameters.
531
-	 */
532
-	public function get_collection_params() {
526
+     * Retrieves the query params for the invoices collection.
527
+     *
528
+     * @since 1.0.13
529
+     *
530
+     * @return array Collection parameters.
531
+     */
532
+    public function get_collection_params() {
533 533
         
534 534
         $query_params               = array(
535 535
 
@@ -547,22 +547,22 @@  discard block
 block discarded – undo
547 547
 
548 548
             // User.
549 549
             'user'                  => array(
550
-				'description'       => __( 'Limit result set to invoices for a specif user.', 'invoicing' ),
551
-				'type'              => 'integer',
550
+                'description'       => __( 'Limit result set to invoices for a specif user.', 'invoicing' ),
551
+                'type'              => 'integer',
552 552
             ),
553 553
             
554 554
             // Number of results per page
555 555
             'limit'                 => array(
556
-				'description'       => __( 'Number of invoices to fetch.', 'invoicing' ),
557
-				'type'              => 'integer',
558
-				'default'           => (int) get_option( 'posts_per_page' ),
556
+                'description'       => __( 'Number of invoices to fetch.', 'invoicing' ),
557
+                'type'              => 'integer',
558
+                'default'           => (int) get_option( 'posts_per_page' ),
559 559
             ),
560 560
 
561 561
             // Pagination
562 562
             'page'     => array(
563
-				'description'       => __( 'Current page to fetch.', 'invoicing' ),
564
-				'type'              => 'integer',
565
-				'default'           => 1,
563
+                'description'       => __( 'Current page to fetch.', 'invoicing' ),
564
+                'type'              => 'integer',
565
+                'default'           => 1,
566 566
             ),
567 567
 
568 568
             // Exclude certain invoices
@@ -598,721 +598,721 @@  discard block
 block discarded – undo
598 598
             ),
599 599
         );
600 600
 
601
-		/**
602
-		 * Filter collection parameters for the invoices controller.
603
-		 *
604
-		 *
605
-		 * @since 1.0.13
606
-		 *
607
-		 * @param array        $query_params JSON Schema-formatted collection parameters.
608
-		 * @param WP_Post_Type $post_type    Post type object.
609
-		 */
610
-		return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' );
601
+        /**
602
+         * Filter collection parameters for the invoices controller.
603
+         *
604
+         *
605
+         * @since 1.0.13
606
+         *
607
+         * @param array        $query_params JSON Schema-formatted collection parameters.
608
+         * @param WP_Post_Type $post_type    Post type object.
609
+         */
610
+        return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' );
611 611
     }
612 612
     
613 613
     /**
614
-	 * Checks if a given post type can be viewed or managed.
615
-	 *
616
-	 * @since 1.0.13
617
-	 *
618
-	 * @param object|string $post_type Post type name or object.
619
-	 * @return bool Whether the post type is allowed in REST.
620
-	 */
621
-	protected function check_is_post_type_allowed( $post_type ) {
622
-		return true;
623
-	}
624
-
625
-	/**
626
-	 * Prepares a single invoice for create or update.
627
-	 *
628
-	 * @since 1.0.13
629
-	 *
630
-	 * @param WP_REST_Request $request Request object.
631
-	 * @return array|WP_Error Invoice Properties or WP_Error.
632
-	 */
633
-	protected function prepare_item_for_database( $request ) {
634
-		$prepared_invoice = new stdClass();
635
-
636
-		// Post ID.
637
-		if ( isset( $request['id'] ) ) {
638
-			$existing_invoice = $this->get_post( $request['id'] );
639
-			if ( is_wp_error( $existing_invoice ) ) {
640
-				return $existing_invoice;
641
-			}
642
-
643
-			$prepared_invoice->ID 		  = $existing_invoice->ID;
644
-			$prepared_invoice->invoice_id = $existing_invoice->ID;
645
-		}
646
-
647
-		$schema = $this->get_item_schema();
648
-
649
-		// Invoice owner.
650
-		if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) {
651
-			$prepared_invoice->user_id = (int) $request['user_id'];
652
-		}
653
-
654
-		// Cart details.
655
-		if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) {
656
-			$prepared_invoice->cart_details = (array) $request['cart_details'];
657
-		}
658
-
659
-		// Invoice status.
660
-		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
661
-
662
-			if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) {
663
-				$prepared_invoice->status = $request['status'];
664
-			}
665
-
666
-		}
667
-
668
-		// User info
669
-		if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) {
670
-			$prepared_invoice->user_info = array();
671
-			$user_info = (array) $request['user_info'];
672
-
673
-			foreach( $user_info as $prop => $value ) {
674
-
675
-				if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) {
676
-
677
-					$prepared_invoice->user_info[$prop] = $value;
614
+     * Checks if a given post type can be viewed or managed.
615
+     *
616
+     * @since 1.0.13
617
+     *
618
+     * @param object|string $post_type Post type name or object.
619
+     * @return bool Whether the post type is allowed in REST.
620
+     */
621
+    protected function check_is_post_type_allowed( $post_type ) {
622
+        return true;
623
+    }
624
+
625
+    /**
626
+     * Prepares a single invoice for create or update.
627
+     *
628
+     * @since 1.0.13
629
+     *
630
+     * @param WP_REST_Request $request Request object.
631
+     * @return array|WP_Error Invoice Properties or WP_Error.
632
+     */
633
+    protected function prepare_item_for_database( $request ) {
634
+        $prepared_invoice = new stdClass();
635
+
636
+        // Post ID.
637
+        if ( isset( $request['id'] ) ) {
638
+            $existing_invoice = $this->get_post( $request['id'] );
639
+            if ( is_wp_error( $existing_invoice ) ) {
640
+                return $existing_invoice;
641
+            }
642
+
643
+            $prepared_invoice->ID 		  = $existing_invoice->ID;
644
+            $prepared_invoice->invoice_id = $existing_invoice->ID;
645
+        }
646
+
647
+        $schema = $this->get_item_schema();
648
+
649
+        // Invoice owner.
650
+        if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) {
651
+            $prepared_invoice->user_id = (int) $request['user_id'];
652
+        }
653
+
654
+        // Cart details.
655
+        if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) {
656
+            $prepared_invoice->cart_details = (array) $request['cart_details'];
657
+        }
658
+
659
+        // Invoice status.
660
+        if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
661
+
662
+            if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) {
663
+                $prepared_invoice->status = $request['status'];
664
+            }
665
+
666
+        }
667
+
668
+        // User info
669
+        if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) {
670
+            $prepared_invoice->user_info = array();
671
+            $user_info = (array) $request['user_info'];
672
+
673
+            foreach( $user_info as $prop => $value ) {
674
+
675
+                if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) {
676
+
677
+                    $prepared_invoice->user_info[$prop] = $value;
678 678
 		
679
-				}
679
+                }
680 680
 
681
-			}
681
+            }
682 682
 
683
-		}
683
+        }
684 684
 
685
-		// IP
686
-		if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) {
687
-			$prepared_invoice->ip = $request['ip'];
688
-		}
685
+        // IP
686
+        if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) {
687
+            $prepared_invoice->ip = $request['ip'];
688
+        }
689 689
 
690
-		// Payment details
691
-		$prepared_invoice->payment_details = array();
690
+        // Payment details
691
+        $prepared_invoice->payment_details = array();
692 692
 
693
-		if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) {
694
-			$prepared_invoice->payment_details['gateway'] = $request['gateway'];
695
-		}
693
+        if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) {
694
+            $prepared_invoice->payment_details['gateway'] = $request['gateway'];
695
+        }
696 696
 
697
-		if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) {
698
-			$prepared_invoice->payment_details['gateway_title'] = $request['gateway_title'];
699
-		}
697
+        if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) {
698
+            $prepared_invoice->payment_details['gateway_title'] = $request['gateway_title'];
699
+        }
700 700
 
701
-		if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) {
702
-			$prepared_invoice->payment_details['currency'] = $request['currency'];
703
-		}
701
+        if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) {
702
+            $prepared_invoice->payment_details['currency'] = $request['currency'];
703
+        }
704 704
 
705
-		if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) {
706
-			$prepared_invoice->payment_details['transaction_id'] = $request['transaction_id'];
707
-		}
705
+        if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) {
706
+            $prepared_invoice->payment_details['transaction_id'] = $request['transaction_id'];
707
+        }
708 708
 
709
-		// Dates
710
-		if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) {
711
-			$post_date = rest_get_date_with_gmt( $request['date'] );
709
+        // Dates
710
+        if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) {
711
+            $post_date = rest_get_date_with_gmt( $request['date'] );
712 712
 
713
-			if ( ! empty( $post_date ) ) {
714
-				$prepared_invoice->post_date = $post_date[0];
715
-			}
713
+            if ( ! empty( $post_date ) ) {
714
+                $prepared_invoice->post_date = $post_date[0];
715
+            }
716 716
 			
717
-		}
717
+        }
718
+
719
+        if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) {
720
+            $due_date = rest_get_date_with_gmt( $request['due_date'] );
721
+
722
+            if ( ! empty( $due_date ) ) {
723
+                $prepared_invoice->due_date = $due_date[0];
724
+            }
725
+
726
+        }
727
+
728
+        $invoice_data = (array) $prepared_invoice;
729
+
730
+        /**
731
+         * Filters an invoice before it is inserted via the REST API.
732
+         *
733
+         * @since 1.0.13
734
+         *
735
+         * @param array        $invoice_data An array of invoice data
736
+         * @param WP_REST_Request $request       Request object.
737
+         */
738
+        return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request );
739
+
740
+    }
741
+
742
+    /**
743
+     * Prepares a single invoice output for response.
744
+     *
745
+     * @since 1.0.13
746
+     *
747
+     * @param WPInv_Invoice   $invoice    Invoice object.
748
+     * @param WP_REST_Request $request Request object.
749
+     * @return WP_REST_Response Response object.
750
+     */
751
+    public function prepare_item_for_response( $invoice, $request ) {
718 752
 
719
-		if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) {
720
-			$due_date = rest_get_date_with_gmt( $request['due_date'] );
753
+        $GLOBALS['post'] = get_post( $invoice->ID );
721 754
 
722
-			if ( ! empty( $due_date ) ) {
723
-				$prepared_invoice->due_date = $due_date[0];
724
-			}
755
+        setup_postdata( $invoice->ID );
725 756
 
726
-		}
757
+        // Fetch the fields to include in this response.
758
+        $fields = $this->get_fields_for_response( $request );
727 759
 
728
-		$invoice_data = (array) $prepared_invoice;
760
+        // Base fields for every invoice.
761
+        $data = array();
729 762
 
730
-		/**
731
-		 * Filters an invoice before it is inserted via the REST API.
732
-		 *
733
-		 * @since 1.0.13
734
-		 *
735
-		 * @param array        $invoice_data An array of invoice data
736
-		 * @param WP_REST_Request $request       Request object.
737
-		 */
738
-		return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request );
763
+        // Set up ID
764
+        if ( rest_is_field_included( 'id', $fields ) ) {
765
+            $data['id'] = $invoice->ID;
766
+        }
767
+
768
+
769
+        // Basic properties
770
+        $invoice_properties = array(
771
+            'title', 'email', 'ip', 
772
+            'key', 'number', 'transaction_id', 'mode',
773
+            'gateway', 'gateway_title',
774
+            'total', 'discount', 'discount_code', 
775
+            'tax', 'fees_total', 'subtotal', 'currency',
776
+            'status', 'status_nicename', 'post_type'
777
+        );
739 778
 
740
-	}
779
+        foreach( $invoice_properties as $property ) {
741 780
 
742
-	/**
743
-	 * Prepares a single invoice output for response.
744
-	 *
745
-	 * @since 1.0.13
746
-	 *
747
-	 * @param WPInv_Invoice   $invoice    Invoice object.
748
-	 * @param WP_REST_Request $request Request object.
749
-	 * @return WP_REST_Response Response object.
750
-	 */
751
-	public function prepare_item_for_response( $invoice, $request ) {
781
+            if ( rest_is_field_included( $property, $fields ) ) {
782
+                $data[$property] = $invoice->get( $property );
783
+            }
784
+
785
+        }
752 786
 
753
-		$GLOBALS['post'] = get_post( $invoice->ID );
787
+        // Cart details
788
+        if ( rest_is_field_included( 'cart_details', $fields ) ) {
789
+            $data['cart_details'] = $invoice->get( 'cart_details' );
790
+        }
754 791
 
755
-		setup_postdata( $invoice->ID );
792
+        //Dates
793
+        $invoice_properties = array( 'date', 'due_date', 'completed_date' );
756 794
 
757
-		// Fetch the fields to include in this response.
758
-		$fields = $this->get_fields_for_response( $request );
795
+        foreach( $invoice_properties as $property ) {
759 796
 
760
-		// Base fields for every invoice.
761
-		$data = array();
797
+            if ( rest_is_field_included( $property, $fields ) ) {
798
+                $data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) );
799
+            }
762 800
 
763
-		// Set up ID
764
-		if ( rest_is_field_included( 'id', $fields ) ) {
765
-			$data['id'] = $invoice->ID;
766
-		}
801
+        }
767 802
 
803
+        // User id
804
+        if ( rest_is_field_included( 'user_id', $fields ) ) {
805
+            $data['user_id'] = (int) $invoice->get( 'user_id' );
806
+        }
768 807
 
769
-		// Basic properties
770
-		$invoice_properties = array(
771
-			'title', 'email', 'ip', 
772
-			'key', 'number', 'transaction_id', 'mode',
773
-			'gateway', 'gateway_title',
774
-			'total', 'discount', 'discount_code', 
775
-			'tax', 'fees_total', 'subtotal', 'currency',
776
-			'status', 'status_nicename', 'post_type'
777
-		);
808
+        // User info
809
+        $user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' );
778 810
 
779
-		foreach( $invoice_properties as $property ) {
811
+        foreach( $user_info as $property ) {
780 812
 
781
-			if ( rest_is_field_included( $property, $fields ) ) {
782
-				$data[$property] = $invoice->get( $property );
783
-			}
813
+            if ( rest_is_field_included( "user_info.$property", $fields ) ) {
814
+                $data['user_info'][$property] = $invoice->get( $property );
815
+            }
784 816
 
785
-		}
817
+        }
786 818
 
787
-		// Cart details
788
-		if ( rest_is_field_included( 'cart_details', $fields ) ) {
789
-			$data['cart_details'] = $invoice->get( 'cart_details' );
790
-		}
819
+        // Slug
820
+        if ( rest_is_field_included( 'slug', $fields ) ) {
821
+            $data['slug'] = $invoice->get( 'post_name' );
822
+        }
791 823
 
792
-		//Dates
793
-		$invoice_properties = array( 'date', 'due_date', 'completed_date' );
794
-
795
-		foreach( $invoice_properties as $property ) {
796
-
797
-			if ( rest_is_field_included( $property, $fields ) ) {
798
-				$data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) );
799
-			}
800
-
801
-		}
802
-
803
-		// User id
804
-		if ( rest_is_field_included( 'user_id', $fields ) ) {
805
-			$data['user_id'] = (int) $invoice->get( 'user_id' );
806
-		}
807
-
808
-		// User info
809
-		$user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' );
810
-
811
-		foreach( $user_info as $property ) {
812
-
813
-			if ( rest_is_field_included( "user_info.$property", $fields ) ) {
814
-				$data['user_info'][$property] = $invoice->get( $property );
815
-			}
816
-
817
-		}
818
-
819
-		// Slug
820
-		if ( rest_is_field_included( 'slug', $fields ) ) {
821
-			$data['slug'] = $invoice->get( 'post_name' );
822
-		}
823
-
824
-		// View invoice link
825
-		if ( rest_is_field_included( 'link', $fields ) ) {
826
-			$data['link'] = esc_url( $invoice->get_view_url() );
827
-		}
828
-
829
-
830
-		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
831
-		$data    = $this->add_additional_fields_to_object( $data, $request );
832
-		$data    = $this->filter_response_by_context( $data, $context );
833
-
834
-		// Wrap the data in a response object.
835
-		$response = rest_ensure_response( $data );
836
-
837
-		$links = $this->prepare_links( $invoice );
838
-		$response->add_links( $links );
839
-
840
-		if ( ! empty( $links['self']['href'] ) ) {
841
-			$actions = $this->get_available_actions( $invoice, $request );
842
-
843
-			$self = $links['self']['href'];
844
-
845
-			foreach ( $actions as $rel ) {
846
-				$response->add_link( $rel, $self );
847
-			}
848
-		}
849
-
850
-		/**
851
-		 * Filters the invoice data for a response.
852
-		 *
853
-		 * @since 1.0.13
854
-		 *
855
-		 * @param WP_REST_Response $response The response object.
856
-		 * @param WPInv_Invoice    $invoice  The invoice object.
857
-		 * @param WP_REST_Request  $request  Request object.
858
-		 */
859
-		return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request );
860
-	}
861
-
862
-	/**
863
-	 * Retrieves the invoice's schema, conforming to JSON Schema.
864
-	 *
865
-	 * @since 1.0.13
866
-	 *
867
-	 * @return array Invoice schema data.
868
-	 */
869
-	public function get_item_schema() {
870
-
871
-		// Maybe retrieve the schema from cache.
872
-		if ( $this->schema ) {
873
-			return $this->add_additional_fields_schema( $this->schema );
874
-		}
875
-
876
-		$schema = array(
877
-			'$schema'    => 'http://json-schema.org/draft-04/schema#',
878
-			'title'      => $this->post_type,
879
-			'type'       => 'object',
880
-
881
-			// Base properties for every Invoice.
882
-			'properties' 		  => array(
883
-
884
-				'title'			  => array(
885
-					'description' => __( 'The title for the invoice.', 'invoicing' ),
886
-					'type'        => 'string',
887
-					'context'     => array( 'view', 'edit', 'embed' ),
888
-					'readonly'    => true,
889
-				),
890
-
891
-				'user_id'		  => array(
892
-					'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ),
893
-					'type'        => 'integer',
894
-					'context'     => array( 'view', 'edit', 'embed' ),
895
-				),
896
-
897
-				'email'		  	  => array(
898
-					'description' => __( 'The email of the owner of the invoice.', 'invoicing' ),
899
-					'type'        => 'string',
900
-					'context'     => array( 'view', 'edit', 'embed' ),
901
-					'readonly'    => true,
902
-				),
903
-
904
-				'ip'			  => array(
905
-					'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ),
906
-					'type'        => 'string',
907
-					'context'     => array( 'view', 'edit', 'embed' ),
908
-				),
909
-
910
-				'user_info'       => array(
911
-					'description' => __( 'Information about the owner of the invoice.', 'invoicing' ),
912
-					'type'        => 'object',
913
-					'context'     => array( 'view', 'edit', 'embed' ),
914
-					'properties'  => array(
915
-
916
-						'first_name'      => array(
917
-							'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ),
918
-							'type'        => 'string',
919
-							'context'     => array( 'view', 'edit', 'embed' ),
920
-						),
921
-
922
-						'last_name'       => array(
923
-							'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ),
924
-							'type'        => 'string',
925
-							'context'     => array( 'view', 'edit', 'embed' ),
926
-						),
927
-
928
-						'company'         => array(
929
-							'description' => __( 'The company of the owner of the invoice.', 'invoicing' ),
930
-							'type'        => 'string',
931
-							'context'     => array( 'view', 'edit', 'embed' ),
932
-						),
933
-
934
-						'vat_number'      => array(
935
-							'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ),
936
-							'type'        => 'string',
937
-							'context'     => array( 'view', 'edit', 'embed' ),
938
-						),
939
-
940
-						'vat_rate'        => array(
941
-							'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ),
942
-							'type'        => 'string',
943
-							'context'     => array( 'view', 'edit', 'embed' ),
944
-						),
945
-
946
-						'address'        => array(
947
-							'description' => __( 'The address of the invoice owner.', 'invoicing' ),
948
-							'type'        => 'string',
949
-							'context'     => array( 'view', 'edit', 'embed' ),
950
-						),
951
-
952
-						'city'            => array(
953
-							'description' => __( 'The city of the invoice owner.', 'invoicing' ),
954
-							'type'        => 'string',
955
-							'context'     => array( 'view', 'edit', 'embed' ),
956
-						),
957
-
958
-						'country'         => array(
959
-							'description' => __( 'The country of the invoice owner.', 'invoicing' ),
960
-							'type'        => 'string',
961
-							'context'     => array( 'view', 'edit', 'embed' ),
962
-						),
963
-
964
-						'state'           => array(
965
-							'description' => __( 'The state of the invoice owner.', 'invoicing' ),
966
-							'type'        => 'string',
967
-							'context'     => array( 'view', 'edit', 'embed' ),
968
-						),
969
-
970
-						'zip'             => array(
971
-							'description' => __( 'The zip code of the invoice owner.', 'invoicing' ),
972
-							'type'        => 'string',
973
-							'context'     => array( 'view', 'edit', 'embed' ),
974
-						),
975
-
976
-						'phone'             => array(
977
-							'description' => __( 'The phone number of the invoice owner.', 'invoicing' ),
978
-							'type'        => 'string',
979
-							'context'     => array( 'view', 'edit', 'embed' ),
980
-						),
981
-					),
982
-				),
983
-
984
-				'id'           => array(
985
-					'description' => __( 'Unique identifier for the invoice.', 'invoicing' ),
986
-					'type'        => 'integer',
987
-					'context'     => array( 'view', 'edit', 'embed' ),
988
-					'readonly'    => true,
989
-				),
990
-
991
-				'key'			  => array(
992
-					'description' => __( 'A unique key for the invoice.', 'invoicing' ),
993
-					'type'        => 'string',
994
-					'context'     => array( 'view', 'edit', 'embed' ),
995
-					'readonly'    => true,
996
-				),
997
-
998
-				'number'		  => array(
999
-					'description' => __( 'The invoice number.', 'invoicing' ),
1000
-					'type'        => 'string',
1001
-					'context'     => array( 'view', 'edit', 'embed' ),
1002
-					'readonly'    => true,
1003
-				),
1004
-
1005
-				'transaction_id'  => array(
1006
-					'description' => __( 'The transaction id of the invoice.', 'invoicing' ),
1007
-					'type'        => 'string',
1008
-					'context'     => array( 'view', 'edit', 'embed' ),
1009
-				),
1010
-
1011
-				'gateway'		  => array(
1012
-					'description' => __( 'The gateway used to process the invoice.', 'invoicing' ),
1013
-					'type'        => 'string',
1014
-					'context'     => array( 'view', 'edit', 'embed' ),
1015
-				),
1016
-
1017
-				'gateway_title'	  => array(
1018
-					'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ),
1019
-					'type'        => 'string',
1020
-					'context'     => array( 'view', 'edit', 'embed' ),
1021
-				),
1022
-
1023
-				'total'	  		  => array(
1024
-					'description' => __( 'The total amount of the invoice.', 'invoicing' ),
1025
-					'type'        => 'number',
1026
-					'context'     => array( 'view', 'edit', 'embed' ),
1027
-					'readonly'    => true,
1028
-				),
1029
-
1030
-				'discount'		  => array(
1031
-					'description' => __( 'The discount applied to the invoice.', 'invoicing' ),
1032
-					'type'        => 'number',
1033
-					'context'     => array( 'view', 'edit', 'embed' ),
1034
-					'readonly'    => true,
1035
-				),
1036
-
1037
-				'discount_code'	  => array(
1038
-					'description' => __( 'The discount code applied to the invoice.', 'invoicing' ),
1039
-					'type'        => 'string',
1040
-					'context'     => array( 'view', 'edit', 'embed' ),
1041
-					'readonly'    => true,
1042
-				),
1043
-
1044
-				'tax'	  		  => array(
1045
-					'description' => __( 'The tax applied to the invoice.', 'invoicing' ),
1046
-					'type'        => 'number',
1047
-					'context'     => array( 'view', 'edit', 'embed' ),
1048
-					'readonly'    => true,
1049
-				),
1050
-
1051
-				'fees_total'	  => array(
1052
-					'description' => __( 'The total fees applied to the invoice.', 'invoicing' ),
1053
-					'type'        => 'number',
1054
-					'context'     => array( 'view', 'edit', 'embed' ),
1055
-					'readonly'    => true,
1056
-				),
1057
-
1058
-				'subtotal'	  	  => array(
1059
-					'description' => __( 'The sub-total for the invoice.', 'invoicing' ),
1060
-					'type'        => 'number',
1061
-					'context'     => array( 'view', 'edit', 'embed' ),
1062
-					'readonly'    => true,
1063
-				),
1064
-
1065
-				'currency'	  	  => array(
1066
-					'description' => __( 'The currency used to process the invoice.', 'invoicing' ),
1067
-					'type'        => 'string',
1068
-					'context'     => array( 'view', 'edit', 'embed' ),
1069
-				),
1070
-
1071
-				'cart_details'	  => array(
1072
-					'description' => __( 'The cart details for invoice.', 'invoicing' ),
1073
-					'type'        => 'array',
1074
-					'context'     => array( 'view', 'edit', 'embed' ),
1075
-					'required'	  => true,
1076
-				),
1077
-
1078
-				'date'         => array(
1079
-					'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ),
1080
-					'type'        => array( 'string', 'null' ),
1081
-					'format'      => 'date-time',
1082
-					'context'     => array( 'view', 'edit', 'embed' ),
1083
-				),
1084
-
1085
-				'due_date'     => array(
1086
-					'description' => __( 'The due date for the invoice.', 'invoicing' ),
1087
-					'type'        => array( 'string', 'null' ),
1088
-					'format'      => 'date-time',
1089
-					'context'     => array( 'view', 'edit', 'embed' ),
1090
-				),
1091
-
1092
-				'completed_date'  => array(
1093
-					'description' => __( 'The completed date for the invoice.', 'invoicing' ),
1094
-					'type'        => array( 'string', 'null' ),
1095
-					'format'      => 'date-time',
1096
-					'context'     => array( 'view', 'edit', 'embed' ),
1097
-					'readonly'    => true,
1098
-				),
824
+        // View invoice link
825
+        if ( rest_is_field_included( 'link', $fields ) ) {
826
+            $data['link'] = esc_url( $invoice->get_view_url() );
827
+        }
828
+
829
+
830
+        $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
831
+        $data    = $this->add_additional_fields_to_object( $data, $request );
832
+        $data    = $this->filter_response_by_context( $data, $context );
833
+
834
+        // Wrap the data in a response object.
835
+        $response = rest_ensure_response( $data );
836
+
837
+        $links = $this->prepare_links( $invoice );
838
+        $response->add_links( $links );
839
+
840
+        if ( ! empty( $links['self']['href'] ) ) {
841
+            $actions = $this->get_available_actions( $invoice, $request );
842
+
843
+            $self = $links['self']['href'];
844
+
845
+            foreach ( $actions as $rel ) {
846
+                $response->add_link( $rel, $self );
847
+            }
848
+        }
849
+
850
+        /**
851
+         * Filters the invoice data for a response.
852
+         *
853
+         * @since 1.0.13
854
+         *
855
+         * @param WP_REST_Response $response The response object.
856
+         * @param WPInv_Invoice    $invoice  The invoice object.
857
+         * @param WP_REST_Request  $request  Request object.
858
+         */
859
+        return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request );
860
+    }
861
+
862
+    /**
863
+     * Retrieves the invoice's schema, conforming to JSON Schema.
864
+     *
865
+     * @since 1.0.13
866
+     *
867
+     * @return array Invoice schema data.
868
+     */
869
+    public function get_item_schema() {
870
+
871
+        // Maybe retrieve the schema from cache.
872
+        if ( $this->schema ) {
873
+            return $this->add_additional_fields_schema( $this->schema );
874
+        }
875
+
876
+        $schema = array(
877
+            '$schema'    => 'http://json-schema.org/draft-04/schema#',
878
+            'title'      => $this->post_type,
879
+            'type'       => 'object',
880
+
881
+            // Base properties for every Invoice.
882
+            'properties' 		  => array(
883
+
884
+                'title'			  => array(
885
+                    'description' => __( 'The title for the invoice.', 'invoicing' ),
886
+                    'type'        => 'string',
887
+                    'context'     => array( 'view', 'edit', 'embed' ),
888
+                    'readonly'    => true,
889
+                ),
890
+
891
+                'user_id'		  => array(
892
+                    'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ),
893
+                    'type'        => 'integer',
894
+                    'context'     => array( 'view', 'edit', 'embed' ),
895
+                ),
896
+
897
+                'email'		  	  => array(
898
+                    'description' => __( 'The email of the owner of the invoice.', 'invoicing' ),
899
+                    'type'        => 'string',
900
+                    'context'     => array( 'view', 'edit', 'embed' ),
901
+                    'readonly'    => true,
902
+                ),
903
+
904
+                'ip'			  => array(
905
+                    'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ),
906
+                    'type'        => 'string',
907
+                    'context'     => array( 'view', 'edit', 'embed' ),
908
+                ),
909
+
910
+                'user_info'       => array(
911
+                    'description' => __( 'Information about the owner of the invoice.', 'invoicing' ),
912
+                    'type'        => 'object',
913
+                    'context'     => array( 'view', 'edit', 'embed' ),
914
+                    'properties'  => array(
915
+
916
+                        'first_name'      => array(
917
+                            'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ),
918
+                            'type'        => 'string',
919
+                            'context'     => array( 'view', 'edit', 'embed' ),
920
+                        ),
921
+
922
+                        'last_name'       => array(
923
+                            'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ),
924
+                            'type'        => 'string',
925
+                            'context'     => array( 'view', 'edit', 'embed' ),
926
+                        ),
927
+
928
+                        'company'         => array(
929
+                            'description' => __( 'The company of the owner of the invoice.', 'invoicing' ),
930
+                            'type'        => 'string',
931
+                            'context'     => array( 'view', 'edit', 'embed' ),
932
+                        ),
933
+
934
+                        'vat_number'      => array(
935
+                            'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ),
936
+                            'type'        => 'string',
937
+                            'context'     => array( 'view', 'edit', 'embed' ),
938
+                        ),
939
+
940
+                        'vat_rate'        => array(
941
+                            'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ),
942
+                            'type'        => 'string',
943
+                            'context'     => array( 'view', 'edit', 'embed' ),
944
+                        ),
945
+
946
+                        'address'        => array(
947
+                            'description' => __( 'The address of the invoice owner.', 'invoicing' ),
948
+                            'type'        => 'string',
949
+                            'context'     => array( 'view', 'edit', 'embed' ),
950
+                        ),
951
+
952
+                        'city'            => array(
953
+                            'description' => __( 'The city of the invoice owner.', 'invoicing' ),
954
+                            'type'        => 'string',
955
+                            'context'     => array( 'view', 'edit', 'embed' ),
956
+                        ),
957
+
958
+                        'country'         => array(
959
+                            'description' => __( 'The country of the invoice owner.', 'invoicing' ),
960
+                            'type'        => 'string',
961
+                            'context'     => array( 'view', 'edit', 'embed' ),
962
+                        ),
963
+
964
+                        'state'           => array(
965
+                            'description' => __( 'The state of the invoice owner.', 'invoicing' ),
966
+                            'type'        => 'string',
967
+                            'context'     => array( 'view', 'edit', 'embed' ),
968
+                        ),
969
+
970
+                        'zip'             => array(
971
+                            'description' => __( 'The zip code of the invoice owner.', 'invoicing' ),
972
+                            'type'        => 'string',
973
+                            'context'     => array( 'view', 'edit', 'embed' ),
974
+                        ),
975
+
976
+                        'phone'             => array(
977
+                            'description' => __( 'The phone number of the invoice owner.', 'invoicing' ),
978
+                            'type'        => 'string',
979
+                            'context'     => array( 'view', 'edit', 'embed' ),
980
+                        ),
981
+                    ),
982
+                ),
983
+
984
+                'id'           => array(
985
+                    'description' => __( 'Unique identifier for the invoice.', 'invoicing' ),
986
+                    'type'        => 'integer',
987
+                    'context'     => array( 'view', 'edit', 'embed' ),
988
+                    'readonly'    => true,
989
+                ),
990
+
991
+                'key'			  => array(
992
+                    'description' => __( 'A unique key for the invoice.', 'invoicing' ),
993
+                    'type'        => 'string',
994
+                    'context'     => array( 'view', 'edit', 'embed' ),
995
+                    'readonly'    => true,
996
+                ),
997
+
998
+                'number'		  => array(
999
+                    'description' => __( 'The invoice number.', 'invoicing' ),
1000
+                    'type'        => 'string',
1001
+                    'context'     => array( 'view', 'edit', 'embed' ),
1002
+                    'readonly'    => true,
1003
+                ),
1004
+
1005
+                'transaction_id'  => array(
1006
+                    'description' => __( 'The transaction id of the invoice.', 'invoicing' ),
1007
+                    'type'        => 'string',
1008
+                    'context'     => array( 'view', 'edit', 'embed' ),
1009
+                ),
1010
+
1011
+                'gateway'		  => array(
1012
+                    'description' => __( 'The gateway used to process the invoice.', 'invoicing' ),
1013
+                    'type'        => 'string',
1014
+                    'context'     => array( 'view', 'edit', 'embed' ),
1015
+                ),
1016
+
1017
+                'gateway_title'	  => array(
1018
+                    'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ),
1019
+                    'type'        => 'string',
1020
+                    'context'     => array( 'view', 'edit', 'embed' ),
1021
+                ),
1022
+
1023
+                'total'	  		  => array(
1024
+                    'description' => __( 'The total amount of the invoice.', 'invoicing' ),
1025
+                    'type'        => 'number',
1026
+                    'context'     => array( 'view', 'edit', 'embed' ),
1027
+                    'readonly'    => true,
1028
+                ),
1029
+
1030
+                'discount'		  => array(
1031
+                    'description' => __( 'The discount applied to the invoice.', 'invoicing' ),
1032
+                    'type'        => 'number',
1033
+                    'context'     => array( 'view', 'edit', 'embed' ),
1034
+                    'readonly'    => true,
1035
+                ),
1036
+
1037
+                'discount_code'	  => array(
1038
+                    'description' => __( 'The discount code applied to the invoice.', 'invoicing' ),
1039
+                    'type'        => 'string',
1040
+                    'context'     => array( 'view', 'edit', 'embed' ),
1041
+                    'readonly'    => true,
1042
+                ),
1043
+
1044
+                'tax'	  		  => array(
1045
+                    'description' => __( 'The tax applied to the invoice.', 'invoicing' ),
1046
+                    'type'        => 'number',
1047
+                    'context'     => array( 'view', 'edit', 'embed' ),
1048
+                    'readonly'    => true,
1049
+                ),
1050
+
1051
+                'fees_total'	  => array(
1052
+                    'description' => __( 'The total fees applied to the invoice.', 'invoicing' ),
1053
+                    'type'        => 'number',
1054
+                    'context'     => array( 'view', 'edit', 'embed' ),
1055
+                    'readonly'    => true,
1056
+                ),
1057
+
1058
+                'subtotal'	  	  => array(
1059
+                    'description' => __( 'The sub-total for the invoice.', 'invoicing' ),
1060
+                    'type'        => 'number',
1061
+                    'context'     => array( 'view', 'edit', 'embed' ),
1062
+                    'readonly'    => true,
1063
+                ),
1064
+
1065
+                'currency'	  	  => array(
1066
+                    'description' => __( 'The currency used to process the invoice.', 'invoicing' ),
1067
+                    'type'        => 'string',
1068
+                    'context'     => array( 'view', 'edit', 'embed' ),
1069
+                ),
1070
+
1071
+                'cart_details'	  => array(
1072
+                    'description' => __( 'The cart details for invoice.', 'invoicing' ),
1073
+                    'type'        => 'array',
1074
+                    'context'     => array( 'view', 'edit', 'embed' ),
1075
+                    'required'	  => true,
1076
+                ),
1077
+
1078
+                'date'         => array(
1079
+                    'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ),
1080
+                    'type'        => array( 'string', 'null' ),
1081
+                    'format'      => 'date-time',
1082
+                    'context'     => array( 'view', 'edit', 'embed' ),
1083
+                ),
1084
+
1085
+                'due_date'     => array(
1086
+                    'description' => __( 'The due date for the invoice.', 'invoicing' ),
1087
+                    'type'        => array( 'string', 'null' ),
1088
+                    'format'      => 'date-time',
1089
+                    'context'     => array( 'view', 'edit', 'embed' ),
1090
+                ),
1091
+
1092
+                'completed_date'  => array(
1093
+                    'description' => __( 'The completed date for the invoice.', 'invoicing' ),
1094
+                    'type'        => array( 'string', 'null' ),
1095
+                    'format'      => 'date-time',
1096
+                    'context'     => array( 'view', 'edit', 'embed' ),
1097
+                    'readonly'    => true,
1098
+                ),
1099 1099
 				
1100
-				'link'         => array(
1101
-					'description' => __( 'URL to the invoice.', 'invoicing' ),
1102
-					'type'        => 'string',
1103
-					'format'      => 'uri',
1104
-					'context'     => array( 'view', 'edit', 'embed' ),
1105
-					'readonly'    => true,
1106
-				),
1107
-
1108
-				'mode'       	  => array(
1109
-					'description' => __( 'The mode used to process the invoice.', 'invoicing' ),
1110
-					'type'        => 'string',
1111
-					'enum'        => array( 'live', 'test' ),
1112
-					'context'     => array( 'view', 'edit', 'embed' ),
1113
-					'readonly'    => true,
1114
-				),
1115
-
1116
-				'slug'       	  => array(
1117
-					'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ),
1118
-					'type'        => 'string',
1119
-					'context'     => array( 'view', 'edit', 'embed' ),
1120
-					'arg_options' => array(
1121
-						'sanitize_callback' => array( $this, 'sanitize_slug' ),
1122
-					),
1123
-					'readonly'    => true,
1124
-				),
1125
-
1126
-				'status'       	  => array(
1127
-					'description' => __( 'A named status for the invoice.', 'invoicing' ),
1128
-					'type'        => 'string',
1129
-					'enum'        => array_keys( wpinv_get_invoice_statuses( true, true ) ),
1130
-					'context'     => array( 'view', 'edit' ),
1131
-					'default'	  => 'wpi-pending',
1132
-				),
1133
-
1134
-				'status_nicename' => array(
1135
-					'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ),
1136
-					'type'        => 'string',
1137
-					'context'     => array( 'view', 'embed' ),
1138
-					'readonly'    => true,
1139
-				),
1140
-
1141
-				'post_type'       => array(
1142
-					'description' => __( 'The post type for the invoice.', 'invoicing' ),
1143
-					'type'        => 'string',
1144
-					'context'     => array( 'view' ),
1145
-					'readonly'    => true,
1146
-				),
1147
-			),
1148
-		);
1149
-
1150
-		// Add helpful links to the invoice schem.
1151
-		$schema['links'] = $this->get_schema_links();
1152
-
1153
-		/**
1154
-		 * Filters the invoice schema for the REST API.
1155
-		 *
1156
-		 * Enables adding extra properties to invoices.
1157
-		 *
1158
-		 * @since 1.0.13
1159
-		 *
1160
-		 * @param array   $schema    The invoice schema.
1161
-		 */
1100
+                'link'         => array(
1101
+                    'description' => __( 'URL to the invoice.', 'invoicing' ),
1102
+                    'type'        => 'string',
1103
+                    'format'      => 'uri',
1104
+                    'context'     => array( 'view', 'edit', 'embed' ),
1105
+                    'readonly'    => true,
1106
+                ),
1107
+
1108
+                'mode'       	  => array(
1109
+                    'description' => __( 'The mode used to process the invoice.', 'invoicing' ),
1110
+                    'type'        => 'string',
1111
+                    'enum'        => array( 'live', 'test' ),
1112
+                    'context'     => array( 'view', 'edit', 'embed' ),
1113
+                    'readonly'    => true,
1114
+                ),
1115
+
1116
+                'slug'       	  => array(
1117
+                    'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ),
1118
+                    'type'        => 'string',
1119
+                    'context'     => array( 'view', 'edit', 'embed' ),
1120
+                    'arg_options' => array(
1121
+                        'sanitize_callback' => array( $this, 'sanitize_slug' ),
1122
+                    ),
1123
+                    'readonly'    => true,
1124
+                ),
1125
+
1126
+                'status'       	  => array(
1127
+                    'description' => __( 'A named status for the invoice.', 'invoicing' ),
1128
+                    'type'        => 'string',
1129
+                    'enum'        => array_keys( wpinv_get_invoice_statuses( true, true ) ),
1130
+                    'context'     => array( 'view', 'edit' ),
1131
+                    'default'	  => 'wpi-pending',
1132
+                ),
1133
+
1134
+                'status_nicename' => array(
1135
+                    'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ),
1136
+                    'type'        => 'string',
1137
+                    'context'     => array( 'view', 'embed' ),
1138
+                    'readonly'    => true,
1139
+                ),
1140
+
1141
+                'post_type'       => array(
1142
+                    'description' => __( 'The post type for the invoice.', 'invoicing' ),
1143
+                    'type'        => 'string',
1144
+                    'context'     => array( 'view' ),
1145
+                    'readonly'    => true,
1146
+                ),
1147
+            ),
1148
+        );
1149
+
1150
+        // Add helpful links to the invoice schem.
1151
+        $schema['links'] = $this->get_schema_links();
1152
+
1153
+        /**
1154
+         * Filters the invoice schema for the REST API.
1155
+         *
1156
+         * Enables adding extra properties to invoices.
1157
+         *
1158
+         * @since 1.0.13
1159
+         *
1160
+         * @param array   $schema    The invoice schema.
1161
+         */
1162 1162
         $schema = apply_filters( "wpinv_rest_invoice_schema", $schema );
1163 1163
 
1164
-		// Cache the invoice schema.
1165
-		$this->schema = $schema;
1164
+        // Cache the invoice schema.
1165
+        $this->schema = $schema;
1166 1166
 		
1167
-		return $this->add_additional_fields_schema( $this->schema );
1168
-	}
1169
-
1170
-	/**
1171
-	 * Retrieve Link Description Objects that should be added to the Schema for the invoices collection.
1172
-	 *
1173
-	 * @since 1.0.13
1174
-	 *
1175
-	 * @return array
1176
-	 */
1177
-	protected function get_schema_links() {
1178
-
1179
-		$href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1180
-
1181
-		$links = array();
1182
-
1183
-		$links[] = array(
1184
-			'rel'          => 'https://api.w.org/action-publish',
1185
-			'title'        => __( 'The current user can mark this invoice as completed.', 'invoicing' ),
1186
-			'href'         => $href,
1187
-			'targetSchema' => array(
1188
-				'type'       => 'object',
1189
-				'properties' => array(
1190
-					'status' => array(
1191
-						'type' => 'string',
1192
-						'enum' => array( 'publish', 'wpi-renewal' ),
1193
-					),
1194
-				),
1195
-			),
1196
-		);
1197
-
1198
-		$links[] = array(
1199
-			'rel'          => 'https://api.w.org/action-assign-author',
1200
-			'title'        => __( 'The current user can change the owner of this invoice.', 'invoicing' ),
1201
-			'href'         => $href,
1202
-			'targetSchema' => array(
1203
-				'type'       => 'object',
1204
-				'properties'   => array(
1205
-					'user_id'  => array(
1206
-						'type' => 'integer',
1207
-					),
1208
-				),
1209
-			),
1210
-		);
1211
-
1212
-		return $links;
1213
-	}
1214
-
1215
-	/**
1216
-	 * Prepares links for the request.
1217
-	 *
1218
-	 * @since 1.0.13
1219
-	 *
1220
-	 * @param WPInv_Invoice $invoice Invoice Object.
1221
-	 * @return array Links for the given invoice.
1222
-	 */
1223
-	protected function prepare_links( $invoice ) {
1224
-
1225
-		// Prepare the base REST API endpoint for invoices.
1226
-		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1227
-
1228
-		// Entity meta.
1229
-		$links = array(
1230
-			'self'       => array(
1231
-				'href' => rest_url( trailingslashit( $base ) . $invoice->ID ),
1232
-			),
1233
-			'collection' => array(
1234
-				'href' => rest_url( $base ),
1235
-			),
1236
-		);
1237
-
1238
-		if ( ! empty( $invoice->user_id ) ) {
1239
-			$links['user'] = array(
1240
-				'href'       => rest_url( 'wp/v2/users/' . $invoice->user_id ),
1241
-				'embeddable' => true,
1242
-			);
1243
-		}
1244
-
1245
-		/**
1246
-		 * Filters the returned invoice links for the REST API.
1247
-		 *
1248
-		 * Enables adding extra links to invoice API responses.
1249
-		 *
1250
-		 * @since 1.0.13
1251
-		 *
1252
-		 * @param array   $links    Rest links.
1253
-		 */
1254
-		return apply_filters( "wpinv_invoice_rest_links", $links );
1255
-
1256
-	}
1257
-
1258
-	/**
1259
-	 * Get the link relations available for the post and current user.
1260
-	 *
1261
-	 * @since 1.0.13
1262
-	 *
1263
-	 * @param WPInv_Invoice   $invoice    Invoice object.
1264
-	 * @param WP_REST_Request $request Request object.
1265
-	 * @return array List of link relations.
1266
-	 */
1267
-	protected function get_available_actions( $invoice, $request ) {
1268
-
1269
-		if ( 'edit' !== $request['context'] ) {
1270
-			return array();
1271
-		}
1272
-
1273
-		$rels = array();
1274
-
1275
-		// Retrieve the post type object.
1276
-		$post_type = get_post_type_object( $invoice->post_type );
1277
-
1278
-		// Mark invoice as completed.
1279
-		if ( current_user_can( $post_type->cap->publish_posts ) ) {
1280
-			$rels[] = 'https://api.w.org/action-publish';
1281
-		}
1282
-
1283
-		// Change the owner of the invoice.
1284
-		if ( current_user_can( $post_type->cap->edit_others_posts ) ) {
1285
-			$rels[] = 'https://api.w.org/action-assign-author';
1286
-		}
1287
-
1288
-		/**
1289
-		 * Filters the available invoice link relations for the REST API.
1290
-		 *
1291
-		 * Enables adding extra link relation for the current user and request to invoice responses.
1292
-		 *
1293
-		 * @since 1.0.13
1294
-		 *
1295
-		 * @param array   $rels    Available link relations.
1296
-		 */
1297
-		return apply_filters( "wpinv_invoice_rest_link_relations", $rels );
1298
-	}
1299
-
1300
-	/**
1301
-	 * Sanitizes and validates the list of post statuses.
1302
-	 *
1303
-	 * @since 1.0.13
1304
-	 *
1305
-	 * @param string|array    $statuses  One or more post statuses.
1306
-	 * @param WP_REST_Request $request   Full details about the request.
1307
-	 * @param string          $parameter Additional parameter to pass to validation.
1308
-	 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
1309
-	 */
1310
-	public function sanitize_post_statuses( $statuses, $request, $parameter ) {
1311
-
1312
-		$statuses 	  = wp_parse_slug_list( $statuses );
1313
-		$valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) );
1314
-		return array_intersect( $statuses, $valid_statuses );
1167
+        return $this->add_additional_fields_schema( $this->schema );
1168
+    }
1169
+
1170
+    /**
1171
+     * Retrieve Link Description Objects that should be added to the Schema for the invoices collection.
1172
+     *
1173
+     * @since 1.0.13
1174
+     *
1175
+     * @return array
1176
+     */
1177
+    protected function get_schema_links() {
1178
+
1179
+        $href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1180
+
1181
+        $links = array();
1182
+
1183
+        $links[] = array(
1184
+            'rel'          => 'https://api.w.org/action-publish',
1185
+            'title'        => __( 'The current user can mark this invoice as completed.', 'invoicing' ),
1186
+            'href'         => $href,
1187
+            'targetSchema' => array(
1188
+                'type'       => 'object',
1189
+                'properties' => array(
1190
+                    'status' => array(
1191
+                        'type' => 'string',
1192
+                        'enum' => array( 'publish', 'wpi-renewal' ),
1193
+                    ),
1194
+                ),
1195
+            ),
1196
+        );
1197
+
1198
+        $links[] = array(
1199
+            'rel'          => 'https://api.w.org/action-assign-author',
1200
+            'title'        => __( 'The current user can change the owner of this invoice.', 'invoicing' ),
1201
+            'href'         => $href,
1202
+            'targetSchema' => array(
1203
+                'type'       => 'object',
1204
+                'properties'   => array(
1205
+                    'user_id'  => array(
1206
+                        'type' => 'integer',
1207
+                    ),
1208
+                ),
1209
+            ),
1210
+        );
1211
+
1212
+        return $links;
1213
+    }
1214
+
1215
+    /**
1216
+     * Prepares links for the request.
1217
+     *
1218
+     * @since 1.0.13
1219
+     *
1220
+     * @param WPInv_Invoice $invoice Invoice Object.
1221
+     * @return array Links for the given invoice.
1222
+     */
1223
+    protected function prepare_links( $invoice ) {
1224
+
1225
+        // Prepare the base REST API endpoint for invoices.
1226
+        $base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1227
+
1228
+        // Entity meta.
1229
+        $links = array(
1230
+            'self'       => array(
1231
+                'href' => rest_url( trailingslashit( $base ) . $invoice->ID ),
1232
+            ),
1233
+            'collection' => array(
1234
+                'href' => rest_url( $base ),
1235
+            ),
1236
+        );
1237
+
1238
+        if ( ! empty( $invoice->user_id ) ) {
1239
+            $links['user'] = array(
1240
+                'href'       => rest_url( 'wp/v2/users/' . $invoice->user_id ),
1241
+                'embeddable' => true,
1242
+            );
1243
+        }
1244
+
1245
+        /**
1246
+         * Filters the returned invoice links for the REST API.
1247
+         *
1248
+         * Enables adding extra links to invoice API responses.
1249
+         *
1250
+         * @since 1.0.13
1251
+         *
1252
+         * @param array   $links    Rest links.
1253
+         */
1254
+        return apply_filters( "wpinv_invoice_rest_links", $links );
1255
+
1256
+    }
1257
+
1258
+    /**
1259
+     * Get the link relations available for the post and current user.
1260
+     *
1261
+     * @since 1.0.13
1262
+     *
1263
+     * @param WPInv_Invoice   $invoice    Invoice object.
1264
+     * @param WP_REST_Request $request Request object.
1265
+     * @return array List of link relations.
1266
+     */
1267
+    protected function get_available_actions( $invoice, $request ) {
1268
+
1269
+        if ( 'edit' !== $request['context'] ) {
1270
+            return array();
1271
+        }
1272
+
1273
+        $rels = array();
1274
+
1275
+        // Retrieve the post type object.
1276
+        $post_type = get_post_type_object( $invoice->post_type );
1277
+
1278
+        // Mark invoice as completed.
1279
+        if ( current_user_can( $post_type->cap->publish_posts ) ) {
1280
+            $rels[] = 'https://api.w.org/action-publish';
1281
+        }
1282
+
1283
+        // Change the owner of the invoice.
1284
+        if ( current_user_can( $post_type->cap->edit_others_posts ) ) {
1285
+            $rels[] = 'https://api.w.org/action-assign-author';
1286
+        }
1287
+
1288
+        /**
1289
+         * Filters the available invoice link relations for the REST API.
1290
+         *
1291
+         * Enables adding extra link relation for the current user and request to invoice responses.
1292
+         *
1293
+         * @since 1.0.13
1294
+         *
1295
+         * @param array   $rels    Available link relations.
1296
+         */
1297
+        return apply_filters( "wpinv_invoice_rest_link_relations", $rels );
1298
+    }
1299
+
1300
+    /**
1301
+     * Sanitizes and validates the list of post statuses.
1302
+     *
1303
+     * @since 1.0.13
1304
+     *
1305
+     * @param string|array    $statuses  One or more post statuses.
1306
+     * @param WP_REST_Request $request   Full details about the request.
1307
+     * @param string          $parameter Additional parameter to pass to validation.
1308
+     * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
1309
+     */
1310
+    public function sanitize_post_statuses( $statuses, $request, $parameter ) {
1311
+
1312
+        $statuses 	  = wp_parse_slug_list( $statuses );
1313
+        $valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) );
1314
+        return array_intersect( $statuses, $valid_statuses );
1315 1315
 		
1316
-	}
1316
+    }
1317 1317
     
1318 1318
 }
1319 1319
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +283 added lines, -283 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  * @since    1.0.13
9 9
  */
10 10
 
11
-if ( !defined( 'WPINC' ) ) {
11
+if (!defined('WPINC')) {
12 12
     exit;
13 13
 }
14 14
 
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
 	 *
34 34
 	 * @param string $namespace Api Namespace
35 35
 	 */
36
-	public function __construct( $namespace ) {
36
+	public function __construct($namespace) {
37 37
         
38 38
         // Set api namespace...
39 39
 		$this->namespace = $namespace;
@@ -52,12 +52,12 @@  discard block
 block discarded – undo
52 52
 	 * @param WP_REST_Request $request Full details about the request.
53 53
 	 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
54 54
 	 */
55
-	public function get_items_permissions_check( $request ) {
55
+	public function get_items_permissions_check($request) {
56 56
 	
57
-        $post_type = get_post_type_object( $this->post_type );
57
+        $post_type = get_post_type_object($this->post_type);
58 58
 
59
-		if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
60
-			return new WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit invoices.', 'invoicing' ), array( 'status' => rest_authorization_required_code() ) );
59
+		if ('edit' === $request['context'] && !current_user_can($post_type->cap->edit_posts)) {
60
+			return new WP_Error('rest_forbidden_context', __('Sorry, you are not allowed to edit invoices.', 'invoicing'), array('status' => rest_authorization_required_code()));
61 61
 		}
62 62
 
63 63
 		// Read checks will be evaluated on a per invoice basis
@@ -74,17 +74,17 @@  discard block
 block discarded – undo
74 74
 	 * @param WP_REST_Request $request Full details about the request.
75 75
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
76 76
 	 */
77
-	public function get_items( $request ) {
77
+	public function get_items($request) {
78 78
 		
79 79
 		// Retrieve the list of registered invoice query parameters.
80 80
         $registered = $this->get_collection_params();
81 81
         
82 82
         $args       = array();
83 83
 
84
-        foreach( array_keys( $registered ) as $key ) {
84
+        foreach (array_keys($registered) as $key) {
85 85
 
86
-            if( isset( $request[ $key] ) ) {
87
-                $args[ $key ] = $request[ $key];
86
+            if (isset($request[$key])) {
87
+                $args[$key] = $request[$key];
88 88
             }
89 89
 
90 90
         }
@@ -99,32 +99,32 @@  discard block
 block discarded – undo
99 99
 		 * @param array           $args    Key value array of query var to query value.
100 100
 		 * @param WP_REST_Request $request The request used.
101 101
 		 */
102
-        $args       = apply_filters( "wpinv_rest_get_invoices_arguments", $args, $request, $this );
102
+        $args = apply_filters("wpinv_rest_get_invoices_arguments", $args, $request, $this);
103 103
 		
104 104
 		// Special args
105
-		$args[ 'return' ]   = 'objects';
106
-		$args[ 'paginate' ] = true;
105
+		$args['return']   = 'objects';
106
+		$args['paginate'] = true;
107 107
 
108 108
         // Run the query.
109
-		$query = wpinv_get_invoices( $args );
109
+		$query = wpinv_get_invoices($args);
110 110
 		
111 111
 		// Prepare the retrieved invoices
112 112
 		$invoices = array();
113
-		foreach( $query->invoices as $invoice ) {
113
+		foreach ($query->invoices as $invoice) {
114 114
 
115
-			if ( ! $this->check_read_permission( $invoice ) ) {
115
+			if (!$this->check_read_permission($invoice)) {
116 116
 				continue;
117 117
 			}
118 118
 
119
-			$data       = $this->prepare_item_for_response( $invoice, $request );
120
-			$invoices[] = $this->prepare_response_for_collection( $data );
119
+			$data       = $this->prepare_item_for_response($invoice, $request);
120
+			$invoices[] = $this->prepare_response_for_collection($data);
121 121
 
122 122
 		}
123 123
 
124 124
 		// Prepare the response.
125
-		$response = rest_ensure_response( $invoices );
126
-		$response->header( 'X-WP-Total', (int) $query->total );
127
-		$response->header( 'X-WP-TotalPages', (int) $query->max_num_pages );
125
+		$response = rest_ensure_response($invoices);
126
+		$response->header('X-WP-Total', (int)$query->total);
127
+		$response->header('X-WP-TotalPages', (int)$query->max_num_pages);
128 128
 
129 129
 		/**
130 130
 		 * Filters the responses for invoices requests.
@@ -137,9 +137,9 @@  discard block
 block discarded – undo
137 137
 		 * @param WP_REST_Request     $request The request used.
138 138
          * @param array               $args Array of args used to retrieve the invoices
139 139
 		 */
140
-        $response       = apply_filters( "rest_wpinv_invoices_response", $response, $request, $args );
140
+        $response = apply_filters("rest_wpinv_invoices_response", $response, $request, $args);
141 141
 
142
-        return rest_ensure_response( $response );
142
+        return rest_ensure_response($response);
143 143
         
144 144
     }
145 145
 
@@ -151,17 +151,17 @@  discard block
 block discarded – undo
151 151
 	 * @param int $invoice_id Supplied ID.
152 152
 	 * @return WPInv_Invoice|WP_Error Invoice object if ID is valid, WP_Error otherwise.
153 153
 	 */
154
-	protected function get_post( $invoice_id ) {
154
+	protected function get_post($invoice_id) {
155 155
 		
156
-		$error     = new WP_Error( 'rest_invoice_invalid_id', __( 'Invalid invoice ID.', 'invoicing' ), array( 'status' => 404 ) );
156
+		$error = new WP_Error('rest_invoice_invalid_id', __('Invalid invoice ID.', 'invoicing'), array('status' => 404));
157 157
 
158 158
         // Ids start from 1
159
-        if ( (int) $invoice_id <= 0 ) {
159
+        if ((int)$invoice_id <= 0) {
160 160
 			return $error;
161 161
 		}
162 162
 
163
-		$invoice = wpinv_get_invoice( (int) $invoice_id );
164
-		if ( empty( $invoice ) ) {
163
+		$invoice = wpinv_get_invoice((int)$invoice_id);
164
+		if (empty($invoice)) {
165 165
 			return $error;
166 166
         }
167 167
 
@@ -177,18 +177,18 @@  discard block
 block discarded – undo
177 177
 	 * @param WP_REST_Request $request Full details about the request.
178 178
 	 * @return bool|WP_Error True if the request has read access for the invoice, WP_Error object otherwise.
179 179
 	 */
180
-	public function get_item_permissions_check( $request ) {
180
+	public function get_item_permissions_check($request) {
181 181
 
182 182
         // Retrieve the invoice object.
183
-        $invoice = $this->get_post( $request['id'] );
183
+        $invoice = $this->get_post($request['id']);
184 184
         
185 185
         // Ensure it is valid.
186
-		if ( is_wp_error( $invoice ) ) {
186
+		if (is_wp_error($invoice)) {
187 187
 			return $invoice;
188 188
 		}
189 189
 
190
-		if ( $invoice ) {
191
-			return $this->check_read_permission( $invoice );
190
+		if ($invoice) {
191
+			return $this->check_read_permission($invoice);
192 192
 		}
193 193
 
194 194
 		return true;
@@ -205,15 +205,15 @@  discard block
 block discarded – undo
205 205
 	 * @param WPInv_Invoice $invoice WPInv_Invoice object.
206 206
 	 * @return bool Whether the post can be read.
207 207
 	 */
208
-	public function check_read_permission( $invoice ) {
208
+	public function check_read_permission($invoice) {
209 209
 
210 210
 		// An invoice can be read by an admin...
211
-		if ( current_user_can( 'manage_options' ) ||  current_user_can( 'view_invoices' ) ) {
211
+		if (current_user_can('manage_options') || current_user_can('view_invoices')) {
212 212
 			return true;
213 213
 		}
214 214
 
215 215
         // ... and the owner of the invoice
216
-		if( get_current_user_id() ===(int) $invoice->get_user_id() ) {
216
+		if (get_current_user_id() === (int)$invoice->get_user_id()) {
217 217
 			return true;
218 218
 		}
219 219
 
@@ -228,19 +228,19 @@  discard block
 block discarded – undo
228 228
 	 * @param WP_REST_Request $request Full details about the request.
229 229
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
230 230
 	 */
231
-	public function get_item( $request ) {
231
+	public function get_item($request) {
232 232
 
233 233
         // Fetch the invoice.
234
-        $invoice = $this->get_post( $request['id'] );
234
+        $invoice = $this->get_post($request['id']);
235 235
         
236 236
         // Abort early if it does not exist
237
-		if ( is_wp_error( $invoice ) ) {
237
+		if (is_wp_error($invoice)) {
238 238
 			return $invoice;
239 239
 		}
240 240
 
241 241
 		// Prepare the response
242
-		$response = $this->prepare_item_for_response( $invoice, $request );
243
-		$response->link_header( 'alternate', esc_url( $invoice->get_view_url() ), array( 'type' => 'text/html' ) );
242
+		$response = $this->prepare_item_for_response($invoice, $request);
243
+		$response->link_header('alternate', esc_url($invoice->get_view_url()), array('type' => 'text/html'));
244 244
 
245 245
 		/**
246 246
 		 * Filters the responses for single invoice requests.
@@ -252,9 +252,9 @@  discard block
 block discarded – undo
252 252
 		 * @param WP_HTTP_Response $response Response.
253 253
 		 * @param WP_REST_Request  $request The request used.
254 254
 		 */
255
-        $response       = apply_filters( "rest_wpinv_get_invoice_response", $response, $request );
255
+        $response = apply_filters("rest_wpinv_get_invoice_response", $response, $request);
256 256
 
257
-        return rest_ensure_response( $response );
257
+        return rest_ensure_response($response);
258 258
 
259 259
     }
260 260
     
@@ -266,18 +266,18 @@  discard block
 block discarded – undo
266 266
 	 * @param WP_REST_Request $request Full details about the request.
267 267
 	 * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
268 268
 	 */
269
-	public function create_item_permissions_check( $request ) {
269
+	public function create_item_permissions_check($request) {
270 270
 	
271
-		if ( ! empty( $request['id'] ) ) {
272
-			return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
271
+		if (!empty($request['id'])) {
272
+			return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400));
273 273
 		}
274 274
 
275
-		$post_type = get_post_type_object( $this->post_type );
275
+		$post_type = get_post_type_object($this->post_type);
276 276
 
277
-		if ( ! current_user_can( $post_type->cap->create_posts ) ) {
277
+		if (!current_user_can($post_type->cap->create_posts)) {
278 278
 			return new WP_Error( 
279 279
                 'rest_cannot_create', 
280
-                __( 'Sorry, you are not allowed to create invoices as this user.', 'invoicing' ), 
280
+                __('Sorry, you are not allowed to create invoices as this user.', 'invoicing'), 
281 281
                 array( 
282 282
                     'status' => rest_authorization_required_code(),
283 283
                 )
@@ -295,30 +295,30 @@  discard block
 block discarded – undo
295 295
 	 * @param WP_REST_Request $request Full details about the request.
296 296
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
297 297
 	 */
298
-	public function create_item( $request ) {
298
+	public function create_item($request) {
299 299
 
300
-		if ( ! empty( $request['id'] ) ) {
301
-			return new WP_Error( 'rest_invoice_exists', __( 'Cannot create existing invoice.', 'invoicing' ), array( 'status' => 400 ) );
300
+		if (!empty($request['id'])) {
301
+			return new WP_Error('rest_invoice_exists', __('Cannot create existing invoice.', 'invoicing'), array('status' => 400));
302 302
 		}
303 303
 
304
-		$request->set_param( 'context', 'edit' );
304
+		$request->set_param('context', 'edit');
305 305
 
306 306
 		// Prepare the updated data.
307
-		$invoice_data = wp_unslash( $this->prepare_item_for_database( $request ) );
307
+		$invoice_data = wp_unslash($this->prepare_item_for_database($request));
308 308
 
309
-		if ( is_wp_error( $invoice_data ) ) {
309
+		if (is_wp_error($invoice_data)) {
310 310
 			return $invoice_data;
311 311
 		}
312 312
 
313 313
 		// Try creating the invoice
314
-        $invoice = wpinv_insert_invoice( $invoice_data, true );
314
+        $invoice = wpinv_insert_invoice($invoice_data, true);
315 315
 
316
-		if ( is_wp_error( $invoice ) ) {
316
+		if (is_wp_error($invoice)) {
317 317
             return $invoice;
318 318
 		}
319 319
 
320 320
 		// Prepare the response
321
-		$response = $this->prepare_item_for_response( $invoice, $request );
321
+		$response = $this->prepare_item_for_response($invoice, $request);
322 322
 
323 323
 		/**
324 324
 		 * Fires after a single invoice is created or updated via the REST API.
@@ -329,7 +329,7 @@  discard block
 block discarded – undo
329 329
 		 * @param WP_REST_Request $request  Request object.
330 330
 		 * @param bool            $creating True when creating a post, false when updating.
331 331
 		 */
332
-		do_action( "wpinv_rest_insert_invoice", $invoice, $request, true );
332
+		do_action("wpinv_rest_insert_invoice", $invoice, $request, true);
333 333
 
334 334
 		/**
335 335
 		 * Filters the responses for creating single invoice requests.
@@ -341,9 +341,9 @@  discard block
 block discarded – undo
341 341
 		 * @param array           $invoice_data Invoice properties.
342 342
 		 * @param WP_REST_Request $request The request used.
343 343
 		 */
344
-        $response       = apply_filters( "rest_wpinv_create_invoice_response", $response, $request );
344
+        $response = apply_filters("rest_wpinv_create_invoice_response", $response, $request);
345 345
 
346
-        return rest_ensure_response( $response );
346
+        return rest_ensure_response($response);
347 347
 	}
348 348
 
349 349
 	/**
@@ -354,20 +354,20 @@  discard block
 block discarded – undo
354 354
 	 * @param WP_REST_Request $request Full details about the request.
355 355
 	 * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
356 356
 	 */
357
-	public function update_item_permissions_check( $request ) {
357
+	public function update_item_permissions_check($request) {
358 358
 
359 359
 		// Retrieve the invoice.
360
-		$invoice = $this->get_post( $request['id'] );
361
-		if ( is_wp_error( $invoice ) ) {
360
+		$invoice = $this->get_post($request['id']);
361
+		if (is_wp_error($invoice)) {
362 362
 			return $invoice;
363 363
 		}
364 364
 
365
-		$post_type = get_post_type_object( $this->post_type );
365
+		$post_type = get_post_type_object($this->post_type);
366 366
 
367
-		if ( ! current_user_can(  $post_type->cap->edit_post, $invoice->ID  ) ) {
367
+		if (!current_user_can($post_type->cap->edit_post, $invoice->ID)) {
368 368
 			return new WP_Error( 
369 369
                 'rest_cannot_edit', 
370
-                __( 'Sorry, you are not allowed to update this invoice.', 'invoicing' ), 
370
+                __('Sorry, you are not allowed to update this invoice.', 'invoicing'), 
371 371
                 array( 
372 372
                     'status' => rest_authorization_required_code(),
373 373
                 )
@@ -385,46 +385,46 @@  discard block
 block discarded – undo
385 385
 	 * @param WP_REST_Request $request Full details about the request.
386 386
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
387 387
 	 */
388
-	public function update_item( $request ) {
388
+	public function update_item($request) {
389 389
 		
390 390
 		// Ensure the invoice exists.
391
-        $valid_check = $this->get_post( $request['id'] );
391
+        $valid_check = $this->get_post($request['id']);
392 392
         
393 393
         // Abort early if it does not exist
394
-		if ( is_wp_error( $valid_check ) ) {
394
+		if (is_wp_error($valid_check)) {
395 395
 			return $valid_check;
396 396
 		}
397 397
 
398
-		$request->set_param( 'context', 'edit' );
398
+		$request->set_param('context', 'edit');
399 399
 
400 400
 		// Prepare the updated data.
401
-		$data_to_update = wp_unslash( $this->prepare_item_for_database( $request ) );
401
+		$data_to_update = wp_unslash($this->prepare_item_for_database($request));
402 402
 
403
-		if ( is_wp_error( $data_to_update ) ) {
403
+		if (is_wp_error($data_to_update)) {
404 404
 			return $data_to_update;
405 405
 		}
406 406
 
407 407
 		// Abort if no invoice data is provided
408
-        if( empty( $data_to_update ) ) {
409
-            return new WP_Error( 'missing_data', __( 'An update request cannot be empty.', 'invoicing' ) );
408
+        if (empty($data_to_update)) {
409
+            return new WP_Error('missing_data', __('An update request cannot be empty.', 'invoicing'));
410 410
         }
411 411
 
412 412
 		// Include the invoice ID
413 413
 		$data_to_update['ID'] = $request['id'];
414 414
 
415 415
 		// Update the invoice
416
-		$updated_invoice = wpinv_update_invoice( $data_to_update, true );
416
+		$updated_invoice = wpinv_update_invoice($data_to_update, true);
417 417
 
418 418
 		// Incase the update operation failed...
419
-		if ( is_wp_error( $updated_invoice ) ) {
419
+		if (is_wp_error($updated_invoice)) {
420 420
 			return $updated_invoice;
421 421
 		}
422 422
 
423 423
 		// Prepare the response
424
-		$response = $this->prepare_item_for_response( $updated_invoice, $request );
424
+		$response = $this->prepare_item_for_response($updated_invoice, $request);
425 425
 
426 426
 		/** This action is documented in includes/class-wpinv-rest-invoice-controller.php */
427
-		do_action( "wpinv_rest_insert_invoice", $updated_invoice, $request, false );
427
+		do_action("wpinv_rest_insert_invoice", $updated_invoice, $request, false);
428 428
 
429 429
 		/**
430 430
 		 * Filters the responses for updating single invoice requests.
@@ -436,9 +436,9 @@  discard block
 block discarded – undo
436 436
 		 * @param array           $invoice_data Invoice properties.
437 437
 		 * @param WP_REST_Request $request The request used.
438 438
 		 */
439
-        $response       = apply_filters( "wpinv_rest_update_invoice_response", $response, $request );
439
+        $response = apply_filters("wpinv_rest_update_invoice_response", $response, $request);
440 440
 
441
-        return rest_ensure_response( $response );
441
+        return rest_ensure_response($response);
442 442
 	}
443 443
 
444 444
 	/**
@@ -449,19 +449,19 @@  discard block
 block discarded – undo
449 449
 	 * @param WP_REST_Request $request Full details about the request.
450 450
 	 * @return true|WP_Error True if the request has access to delete the invoice, WP_Error object otherwise.
451 451
 	 */
452
-	public function delete_item_permissions_check( $request ) {
452
+	public function delete_item_permissions_check($request) {
453 453
 
454 454
 		// Retrieve the invoice.
455
-		$invoice = $this->get_post( $request['id'] );
456
-		if ( is_wp_error( $invoice ) ) {
455
+		$invoice = $this->get_post($request['id']);
456
+		if (is_wp_error($invoice)) {
457 457
 			return $invoice;
458 458
 		}
459 459
 
460 460
 		// Ensure the current user can delete invoices
461
-		if ( current_user_can( 'manage_options' ) ||  current_user_can( 'delete_invoices' ) ) {
461
+		if (current_user_can('manage_options') || current_user_can('delete_invoices')) {
462 462
 			return new WP_Error( 
463 463
                 'rest_cannot_delete', 
464
-                __( 'Sorry, you are not allowed to delete this invoice.', 'invoicing' ), 
464
+                __('Sorry, you are not allowed to delete this invoice.', 'invoicing'), 
465 465
                 array( 
466 466
                     'status' => rest_authorization_required_code(),
467 467
                 )
@@ -479,31 +479,31 @@  discard block
 block discarded – undo
479 479
 	 * @param WP_REST_Request $request Full details about the request.
480 480
 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
481 481
 	 */
482
-	public function delete_item( $request ) {
482
+	public function delete_item($request) {
483 483
 		
484 484
 		// Retrieve the invoice.
485
-		$invoice = $this->get_post( $request['id'] );
486
-		if ( is_wp_error( $invoice ) ) {
485
+		$invoice = $this->get_post($request['id']);
486
+		if (is_wp_error($invoice)) {
487 487
 			return $invoice;
488 488
 		}
489 489
 
490
-		$request->set_param( 'context', 'edit' );
490
+		$request->set_param('context', 'edit');
491 491
 
492 492
 		// Prepare the invoice id
493
-		$id    = $invoice->ID;
493
+		$id = $invoice->ID;
494 494
 
495 495
 		// Prepare the response
496
-		$response = $this->prepare_item_for_response( $invoice, $request );
496
+		$response = $this->prepare_item_for_response($invoice, $request);
497 497
 
498 498
 		// Check if the user wants to bypass the trash...
499
-		$force_delete = (bool) $request['force'];
499
+		$force_delete = (bool)$request['force'];
500 500
 
501 501
 		// Try deleting the invoice.
502
-		$deleted = wp_delete_post( $id, $force_delete );
502
+		$deleted = wp_delete_post($id, $force_delete);
503 503
 
504 504
 		// Abort early if we can't delete the invoice.
505
-		if ( ! $deleted ) {
506
-			return new WP_Error( 'rest_cannot_delete', __( 'The invoice cannot be deleted.', 'invoicing' ), array( 'status' => 500 ) );
505
+		if (!$deleted) {
506
+			return new WP_Error('rest_cannot_delete', __('The invoice cannot be deleted.', 'invoicing'), array('status' => 500));
507 507
 		}
508 508
 
509 509
 		/**
@@ -515,7 +515,7 @@  discard block
 block discarded – undo
515 515
 		 * @param WPInv_Invoice    $invoice  The deleted or trashed invoice.
516 516
 		 * @param WP_REST_Request  $request  The request sent to the API.
517 517
 		 */
518
-		do_action( "wpinv_rest_delete_invoice", $invoice, $request );
518
+		do_action("wpinv_rest_delete_invoice", $invoice, $request);
519 519
 
520 520
 		return $response;
521 521
 
@@ -531,43 +531,43 @@  discard block
 block discarded – undo
531 531
 	 */
532 532
 	public function get_collection_params() {
533 533
         
534
-        $query_params               = array(
534
+        $query_params = array(
535 535
 
536 536
             // Invoice status.
537 537
             'status'                => array(
538 538
                 'default'           => 'publish',
539
-                'description'       => __( 'Limit result set to invoices assigned one or more statuses.', 'invoicing' ),
539
+                'description'       => __('Limit result set to invoices assigned one or more statuses.', 'invoicing'),
540 540
                 'type'              => 'array',
541 541
                 'items'             => array(
542
-                    'enum'          => array_keys( wpinv_get_invoice_statuses( true, true ) ),
542
+                    'enum'          => array_keys(wpinv_get_invoice_statuses(true, true)),
543 543
                     'type'          => 'string',
544 544
                 ),
545
-                'sanitize_callback' => array( $this, 'sanitize_post_statuses' ),
545
+                'sanitize_callback' => array($this, 'sanitize_post_statuses'),
546 546
             ),
547 547
 
548 548
             // User.
549 549
             'user'                  => array(
550
-				'description'       => __( 'Limit result set to invoices for a specif user.', 'invoicing' ),
550
+				'description'       => __('Limit result set to invoices for a specif user.', 'invoicing'),
551 551
 				'type'              => 'integer',
552 552
             ),
553 553
             
554 554
             // Number of results per page
555 555
             'limit'                 => array(
556
-				'description'       => __( 'Number of invoices to fetch.', 'invoicing' ),
556
+				'description'       => __('Number of invoices to fetch.', 'invoicing'),
557 557
 				'type'              => 'integer',
558
-				'default'           => (int) get_option( 'posts_per_page' ),
558
+				'default'           => (int)get_option('posts_per_page'),
559 559
             ),
560 560
 
561 561
             // Pagination
562 562
             'page'     => array(
563
-				'description'       => __( 'Current page to fetch.', 'invoicing' ),
563
+				'description'       => __('Current page to fetch.', 'invoicing'),
564 564
 				'type'              => 'integer',
565 565
 				'default'           => 1,
566 566
             ),
567 567
 
568 568
             // Exclude certain invoices
569 569
             'exclude'  => array(
570
-                'description' => __( 'Ensure result set excludes specific IDs.', 'invoicing' ),
570
+                'description' => __('Ensure result set excludes specific IDs.', 'invoicing'),
571 571
                 'type'        => 'array',
572 572
                 'items'       => array(
573 573
                     'type' => 'integer',
@@ -577,7 +577,7 @@  discard block
 block discarded – undo
577 577
 
578 578
             // Order invoices by
579 579
             'orderby'  => array(
580
-                'description' => __( 'Sort invoices by object attribute.', 'invoicing' ),
580
+                'description' => __('Sort invoices by object attribute.', 'invoicing'),
581 581
                 'type'        => 'string',
582 582
                 'default'     => 'date',
583 583
                 'enum'        => array(
@@ -591,10 +591,10 @@  discard block
 block discarded – undo
591 591
 
592 592
             // How to order
593 593
             'order'    => array(
594
-                'description' => __( 'Order sort attribute ascending or descending.', 'invoicing' ),
594
+                'description' => __('Order sort attribute ascending or descending.', 'invoicing'),
595 595
                 'type'        => 'string',
596 596
                 'default'     => 'DESC',
597
-                'enum'        => array( 'ASC', 'DESC' ),
597
+                'enum'        => array('ASC', 'DESC'),
598 598
             ),
599 599
         );
600 600
 
@@ -607,7 +607,7 @@  discard block
 block discarded – undo
607 607
 		 * @param array        $query_params JSON Schema-formatted collection parameters.
608 608
 		 * @param WP_Post_Type $post_type    Post type object.
609 609
 		 */
610
-		return apply_filters( "rest_invoices_collection_params", $query_params, 'wpi-invoice' );
610
+		return apply_filters("rest_invoices_collection_params", $query_params, 'wpi-invoice');
611 611
     }
612 612
     
613 613
     /**
@@ -618,7 +618,7 @@  discard block
 block discarded – undo
618 618
 	 * @param object|string $post_type Post type name or object.
619 619
 	 * @return bool Whether the post type is allowed in REST.
620 620
 	 */
621
-	protected function check_is_post_type_allowed( $post_type ) {
621
+	protected function check_is_post_type_allowed($post_type) {
622 622
 		return true;
623 623
 	}
624 624
 
@@ -630,49 +630,49 @@  discard block
 block discarded – undo
630 630
 	 * @param WP_REST_Request $request Request object.
631 631
 	 * @return array|WP_Error Invoice Properties or WP_Error.
632 632
 	 */
633
-	protected function prepare_item_for_database( $request ) {
633
+	protected function prepare_item_for_database($request) {
634 634
 		$prepared_invoice = new stdClass();
635 635
 
636 636
 		// Post ID.
637
-		if ( isset( $request['id'] ) ) {
638
-			$existing_invoice = $this->get_post( $request['id'] );
639
-			if ( is_wp_error( $existing_invoice ) ) {
637
+		if (isset($request['id'])) {
638
+			$existing_invoice = $this->get_post($request['id']);
639
+			if (is_wp_error($existing_invoice)) {
640 640
 				return $existing_invoice;
641 641
 			}
642 642
 
643
-			$prepared_invoice->ID 		  = $existing_invoice->ID;
643
+			$prepared_invoice->ID = $existing_invoice->ID;
644 644
 			$prepared_invoice->invoice_id = $existing_invoice->ID;
645 645
 		}
646 646
 
647 647
 		$schema = $this->get_item_schema();
648 648
 
649 649
 		// Invoice owner.
650
-		if ( ! empty( $schema['properties']['user_id'] ) && isset( $request['user_id'] ) ) {
651
-			$prepared_invoice->user_id = (int) $request['user_id'];
650
+		if (!empty($schema['properties']['user_id']) && isset($request['user_id'])) {
651
+			$prepared_invoice->user_id = (int)$request['user_id'];
652 652
 		}
653 653
 
654 654
 		// Cart details.
655
-		if ( ! empty( $schema['properties']['cart_details'] ) && isset( $request['cart_details'] ) ) {
656
-			$prepared_invoice->cart_details = (array) $request['cart_details'];
655
+		if (!empty($schema['properties']['cart_details']) && isset($request['cart_details'])) {
656
+			$prepared_invoice->cart_details = (array)$request['cart_details'];
657 657
 		}
658 658
 
659 659
 		// Invoice status.
660
-		if ( ! empty( $schema['properties']['status'] ) && isset( $request['status'] ) ) {
660
+		if (!empty($schema['properties']['status']) && isset($request['status'])) {
661 661
 
662
-			if ( in_array( $request['status'], array_keys( wpinv_get_invoice_statuses( true, true ) ), true ) ) {
662
+			if (in_array($request['status'], array_keys(wpinv_get_invoice_statuses(true, true)), true)) {
663 663
 				$prepared_invoice->status = $request['status'];
664 664
 			}
665 665
 
666 666
 		}
667 667
 
668 668
 		// User info
669
-		if ( ! empty( $schema['properties']['user_info'] ) && isset( $request['user_info'] ) ) {
669
+		if (!empty($schema['properties']['user_info']) && isset($request['user_info'])) {
670 670
 			$prepared_invoice->user_info = array();
671
-			$user_info = (array) $request['user_info'];
671
+			$user_info = (array)$request['user_info'];
672 672
 
673
-			foreach( $user_info as $prop => $value ) {
673
+			foreach ($user_info as $prop => $value) {
674 674
 
675
-				if ( ! empty( $schema['properties']['user_info']['properties'][$prop] ) ) {
675
+				if (!empty($schema['properties']['user_info']['properties'][$prop])) {
676 676
 
677 677
 					$prepared_invoice->user_info[$prop] = $value;
678 678
 		
@@ -683,49 +683,49 @@  discard block
 block discarded – undo
683 683
 		}
684 684
 
685 685
 		// IP
686
-		if ( ! empty( $schema['properties']['ip'] ) && isset( $request['ip'] ) ) {
686
+		if (!empty($schema['properties']['ip']) && isset($request['ip'])) {
687 687
 			$prepared_invoice->ip = $request['ip'];
688 688
 		}
689 689
 
690 690
 		// Payment details
691 691
 		$prepared_invoice->payment_details = array();
692 692
 
693
-		if ( ! empty( $schema['properties']['gateway'] ) && isset( $request['gateway'] ) ) {
693
+		if (!empty($schema['properties']['gateway']) && isset($request['gateway'])) {
694 694
 			$prepared_invoice->payment_details['gateway'] = $request['gateway'];
695 695
 		}
696 696
 
697
-		if ( ! empty( $schema['properties']['gateway_title'] ) && isset( $request['gateway_title'] ) ) {
697
+		if (!empty($schema['properties']['gateway_title']) && isset($request['gateway_title'])) {
698 698
 			$prepared_invoice->payment_details['gateway_title'] = $request['gateway_title'];
699 699
 		}
700 700
 
701
-		if ( ! empty( $schema['properties']['currency'] ) && isset( $request['currency'] ) ) {
701
+		if (!empty($schema['properties']['currency']) && isset($request['currency'])) {
702 702
 			$prepared_invoice->payment_details['currency'] = $request['currency'];
703 703
 		}
704 704
 
705
-		if ( ! empty( $schema['properties']['transaction_id'] ) && isset( $request['transaction_id'] ) ) {
705
+		if (!empty($schema['properties']['transaction_id']) && isset($request['transaction_id'])) {
706 706
 			$prepared_invoice->payment_details['transaction_id'] = $request['transaction_id'];
707 707
 		}
708 708
 
709 709
 		// Dates
710
-		if ( ! empty( $schema['properties']['date'] ) && isset( $request['date'] ) ) {
711
-			$post_date = rest_get_date_with_gmt( $request['date'] );
710
+		if (!empty($schema['properties']['date']) && isset($request['date'])) {
711
+			$post_date = rest_get_date_with_gmt($request['date']);
712 712
 
713
-			if ( ! empty( $post_date ) ) {
713
+			if (!empty($post_date)) {
714 714
 				$prepared_invoice->post_date = $post_date[0];
715 715
 			}
716 716
 			
717 717
 		}
718 718
 
719
-		if ( ! empty( $schema['properties']['due_date'] ) && isset( $request['due_date'] ) ) {
720
-			$due_date = rest_get_date_with_gmt( $request['due_date'] );
719
+		if (!empty($schema['properties']['due_date']) && isset($request['due_date'])) {
720
+			$due_date = rest_get_date_with_gmt($request['due_date']);
721 721
 
722
-			if ( ! empty( $due_date ) ) {
722
+			if (!empty($due_date)) {
723 723
 				$prepared_invoice->due_date = $due_date[0];
724 724
 			}
725 725
 
726 726
 		}
727 727
 
728
-		$invoice_data = (array) $prepared_invoice;
728
+		$invoice_data = (array)$prepared_invoice;
729 729
 
730 730
 		/**
731 731
 		 * Filters an invoice before it is inserted via the REST API.
@@ -735,7 +735,7 @@  discard block
 block discarded – undo
735 735
 		 * @param array        $invoice_data An array of invoice data
736 736
 		 * @param WP_REST_Request $request       Request object.
737 737
 		 */
738
-		return apply_filters( "wpinv_rest_pre_insert_invoice", $invoice_data, $request );
738
+		return apply_filters("wpinv_rest_pre_insert_invoice", $invoice_data, $request);
739 739
 
740 740
 	}
741 741
 
@@ -748,20 +748,20 @@  discard block
 block discarded – undo
748 748
 	 * @param WP_REST_Request $request Request object.
749 749
 	 * @return WP_REST_Response Response object.
750 750
 	 */
751
-	public function prepare_item_for_response( $invoice, $request ) {
751
+	public function prepare_item_for_response($invoice, $request) {
752 752
 
753
-		$GLOBALS['post'] = get_post( $invoice->ID );
753
+		$GLOBALS['post'] = get_post($invoice->ID);
754 754
 
755
-		setup_postdata( $invoice->ID );
755
+		setup_postdata($invoice->ID);
756 756
 
757 757
 		// Fetch the fields to include in this response.
758
-		$fields = $this->get_fields_for_response( $request );
758
+		$fields = $this->get_fields_for_response($request);
759 759
 
760 760
 		// Base fields for every invoice.
761 761
 		$data = array();
762 762
 
763 763
 		// Set up ID
764
-		if ( rest_is_field_included( 'id', $fields ) ) {
764
+		if (rest_is_field_included('id', $fields)) {
765 765
 			$data['id'] = $invoice->ID;
766 766
 		}
767 767
 
@@ -776,74 +776,74 @@  discard block
 block discarded – undo
776 776
 			'status', 'status_nicename', 'post_type'
777 777
 		);
778 778
 
779
-		foreach( $invoice_properties as $property ) {
779
+		foreach ($invoice_properties as $property) {
780 780
 
781
-			if ( rest_is_field_included( $property, $fields ) ) {
782
-				$data[$property] = $invoice->get( $property );
781
+			if (rest_is_field_included($property, $fields)) {
782
+				$data[$property] = $invoice->get($property);
783 783
 			}
784 784
 
785 785
 		}
786 786
 
787 787
 		// Cart details
788
-		if ( rest_is_field_included( 'cart_details', $fields ) ) {
789
-			$data['cart_details'] = $invoice->get( 'cart_details' );
788
+		if (rest_is_field_included('cart_details', $fields)) {
789
+			$data['cart_details'] = $invoice->get('cart_details');
790 790
 		}
791 791
 
792 792
 		//Dates
793
-		$invoice_properties = array( 'date', 'due_date', 'completed_date' );
793
+		$invoice_properties = array('date', 'due_date', 'completed_date');
794 794
 
795
-		foreach( $invoice_properties as $property ) {
795
+		foreach ($invoice_properties as $property) {
796 796
 
797
-			if ( rest_is_field_included( $property, $fields ) ) {
798
-				$data[$property] = $this->prepare_date_response( '0000-00-00 00:00:00', $invoice->get( $property ) );
797
+			if (rest_is_field_included($property, $fields)) {
798
+				$data[$property] = $this->prepare_date_response('0000-00-00 00:00:00', $invoice->get($property));
799 799
 			}
800 800
 
801 801
 		}
802 802
 
803 803
 		// User id
804
-		if ( rest_is_field_included( 'user_id', $fields ) ) {
805
-			$data['user_id'] = (int) $invoice->get( 'user_id' );
804
+		if (rest_is_field_included('user_id', $fields)) {
805
+			$data['user_id'] = (int)$invoice->get('user_id');
806 806
 		}
807 807
 
808 808
 		// User info
809
-		$user_info = array( 'first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone' );
809
+		$user_info = array('first_name', 'last_name', 'company', 'vat_number', 'vat_rate', 'address', 'city', 'country', 'state', 'zip', 'phone');
810 810
 
811
-		foreach( $user_info as $property ) {
811
+		foreach ($user_info as $property) {
812 812
 
813
-			if ( rest_is_field_included( "user_info.$property", $fields ) ) {
814
-				$data['user_info'][$property] = $invoice->get( $property );
813
+			if (rest_is_field_included("user_info.$property", $fields)) {
814
+				$data['user_info'][$property] = $invoice->get($property);
815 815
 			}
816 816
 
817 817
 		}
818 818
 
819 819
 		// Slug
820
-		if ( rest_is_field_included( 'slug', $fields ) ) {
821
-			$data['slug'] = $invoice->get( 'post_name' );
820
+		if (rest_is_field_included('slug', $fields)) {
821
+			$data['slug'] = $invoice->get('post_name');
822 822
 		}
823 823
 
824 824
 		// View invoice link
825
-		if ( rest_is_field_included( 'link', $fields ) ) {
826
-			$data['link'] = esc_url( $invoice->get_view_url() );
825
+		if (rest_is_field_included('link', $fields)) {
826
+			$data['link'] = esc_url($invoice->get_view_url());
827 827
 		}
828 828
 
829 829
 
830
-		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
831
-		$data    = $this->add_additional_fields_to_object( $data, $request );
832
-		$data    = $this->filter_response_by_context( $data, $context );
830
+		$context = !empty($request['context']) ? $request['context'] : 'view';
831
+		$data    = $this->add_additional_fields_to_object($data, $request);
832
+		$data    = $this->filter_response_by_context($data, $context);
833 833
 
834 834
 		// Wrap the data in a response object.
835
-		$response = rest_ensure_response( $data );
835
+		$response = rest_ensure_response($data);
836 836
 
837
-		$links = $this->prepare_links( $invoice );
838
-		$response->add_links( $links );
837
+		$links = $this->prepare_links($invoice);
838
+		$response->add_links($links);
839 839
 
840
-		if ( ! empty( $links['self']['href'] ) ) {
841
-			$actions = $this->get_available_actions( $invoice, $request );
840
+		if (!empty($links['self']['href'])) {
841
+			$actions = $this->get_available_actions($invoice, $request);
842 842
 
843 843
 			$self = $links['self']['href'];
844 844
 
845
-			foreach ( $actions as $rel ) {
846
-				$response->add_link( $rel, $self );
845
+			foreach ($actions as $rel) {
846
+				$response->add_link($rel, $self);
847 847
 			}
848 848
 		}
849 849
 
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
 		 * @param WPInv_Invoice    $invoice  The invoice object.
857 857
 		 * @param WP_REST_Request  $request  Request object.
858 858
 		 */
859
-		return apply_filters( "wpinv_rest_prepare_invoice", $response, $invoice, $request );
859
+		return apply_filters("wpinv_rest_prepare_invoice", $response, $invoice, $request);
860 860
 	}
861 861
 
862 862
 	/**
@@ -869,8 +869,8 @@  discard block
 block discarded – undo
869 869
 	public function get_item_schema() {
870 870
 
871 871
 		// Maybe retrieve the schema from cache.
872
-		if ( $this->schema ) {
873
-			return $this->add_additional_fields_schema( $this->schema );
872
+		if ($this->schema) {
873
+			return $this->add_additional_fields_schema($this->schema);
874 874
 		}
875 875
 
876 876
 		$schema = array(
@@ -882,266 +882,266 @@  discard block
 block discarded – undo
882 882
 			'properties' 		  => array(
883 883
 
884 884
 				'title'			  => array(
885
-					'description' => __( 'The title for the invoice.', 'invoicing' ),
885
+					'description' => __('The title for the invoice.', 'invoicing'),
886 886
 					'type'        => 'string',
887
-					'context'     => array( 'view', 'edit', 'embed' ),
887
+					'context'     => array('view', 'edit', 'embed'),
888 888
 					'readonly'    => true,
889 889
 				),
890 890
 
891 891
 				'user_id'		  => array(
892
-					'description' => __( 'The ID of the owner of the invoice.', 'invoicing' ),
892
+					'description' => __('The ID of the owner of the invoice.', 'invoicing'),
893 893
 					'type'        => 'integer',
894
-					'context'     => array( 'view', 'edit', 'embed' ),
894
+					'context'     => array('view', 'edit', 'embed'),
895 895
 				),
896 896
 
897 897
 				'email'		  	  => array(
898
-					'description' => __( 'The email of the owner of the invoice.', 'invoicing' ),
898
+					'description' => __('The email of the owner of the invoice.', 'invoicing'),
899 899
 					'type'        => 'string',
900
-					'context'     => array( 'view', 'edit', 'embed' ),
900
+					'context'     => array('view', 'edit', 'embed'),
901 901
 					'readonly'    => true,
902 902
 				),
903 903
 
904 904
 				'ip'			  => array(
905
-					'description' => __( 'The IP of the owner of the invoice.', 'invoicing' ),
905
+					'description' => __('The IP of the owner of the invoice.', 'invoicing'),
906 906
 					'type'        => 'string',
907
-					'context'     => array( 'view', 'edit', 'embed' ),
907
+					'context'     => array('view', 'edit', 'embed'),
908 908
 				),
909 909
 
910 910
 				'user_info'       => array(
911
-					'description' => __( 'Information about the owner of the invoice.', 'invoicing' ),
911
+					'description' => __('Information about the owner of the invoice.', 'invoicing'),
912 912
 					'type'        => 'object',
913
-					'context'     => array( 'view', 'edit', 'embed' ),
913
+					'context'     => array('view', 'edit', 'embed'),
914 914
 					'properties'  => array(
915 915
 
916 916
 						'first_name'      => array(
917
-							'description' => __( 'The first name of the owner of the invoice.', 'invoicing' ),
917
+							'description' => __('The first name of the owner of the invoice.', 'invoicing'),
918 918
 							'type'        => 'string',
919
-							'context'     => array( 'view', 'edit', 'embed' ),
919
+							'context'     => array('view', 'edit', 'embed'),
920 920
 						),
921 921
 
922 922
 						'last_name'       => array(
923
-							'description' => __( 'The last name of the owner of the invoice.', 'invoicing' ),
923
+							'description' => __('The last name of the owner of the invoice.', 'invoicing'),
924 924
 							'type'        => 'string',
925
-							'context'     => array( 'view', 'edit', 'embed' ),
925
+							'context'     => array('view', 'edit', 'embed'),
926 926
 						),
927 927
 
928 928
 						'company'         => array(
929
-							'description' => __( 'The company of the owner of the invoice.', 'invoicing' ),
929
+							'description' => __('The company of the owner of the invoice.', 'invoicing'),
930 930
 							'type'        => 'string',
931
-							'context'     => array( 'view', 'edit', 'embed' ),
931
+							'context'     => array('view', 'edit', 'embed'),
932 932
 						),
933 933
 
934 934
 						'vat_number'      => array(
935
-							'description' => __( 'The VAT number of the owner of the invoice.', 'invoicing' ),
935
+							'description' => __('The VAT number of the owner of the invoice.', 'invoicing'),
936 936
 							'type'        => 'string',
937
-							'context'     => array( 'view', 'edit', 'embed' ),
937
+							'context'     => array('view', 'edit', 'embed'),
938 938
 						),
939 939
 
940 940
 						'vat_rate'        => array(
941
-							'description' => __( 'The VAT rate applied on the invoice.', 'invoicing' ),
941
+							'description' => __('The VAT rate applied on the invoice.', 'invoicing'),
942 942
 							'type'        => 'string',
943
-							'context'     => array( 'view', 'edit', 'embed' ),
943
+							'context'     => array('view', 'edit', 'embed'),
944 944
 						),
945 945
 
946 946
 						'address'        => array(
947
-							'description' => __( 'The address of the invoice owner.', 'invoicing' ),
947
+							'description' => __('The address of the invoice owner.', 'invoicing'),
948 948
 							'type'        => 'string',
949
-							'context'     => array( 'view', 'edit', 'embed' ),
949
+							'context'     => array('view', 'edit', 'embed'),
950 950
 						),
951 951
 
952 952
 						'city'            => array(
953
-							'description' => __( 'The city of the invoice owner.', 'invoicing' ),
953
+							'description' => __('The city of the invoice owner.', 'invoicing'),
954 954
 							'type'        => 'string',
955
-							'context'     => array( 'view', 'edit', 'embed' ),
955
+							'context'     => array('view', 'edit', 'embed'),
956 956
 						),
957 957
 
958 958
 						'country'         => array(
959
-							'description' => __( 'The country of the invoice owner.', 'invoicing' ),
959
+							'description' => __('The country of the invoice owner.', 'invoicing'),
960 960
 							'type'        => 'string',
961
-							'context'     => array( 'view', 'edit', 'embed' ),
961
+							'context'     => array('view', 'edit', 'embed'),
962 962
 						),
963 963
 
964 964
 						'state'           => array(
965
-							'description' => __( 'The state of the invoice owner.', 'invoicing' ),
965
+							'description' => __('The state of the invoice owner.', 'invoicing'),
966 966
 							'type'        => 'string',
967
-							'context'     => array( 'view', 'edit', 'embed' ),
967
+							'context'     => array('view', 'edit', 'embed'),
968 968
 						),
969 969
 
970 970
 						'zip'             => array(
971
-							'description' => __( 'The zip code of the invoice owner.', 'invoicing' ),
971
+							'description' => __('The zip code of the invoice owner.', 'invoicing'),
972 972
 							'type'        => 'string',
973
-							'context'     => array( 'view', 'edit', 'embed' ),
973
+							'context'     => array('view', 'edit', 'embed'),
974 974
 						),
975 975
 
976 976
 						'phone'             => array(
977
-							'description' => __( 'The phone number of the invoice owner.', 'invoicing' ),
977
+							'description' => __('The phone number of the invoice owner.', 'invoicing'),
978 978
 							'type'        => 'string',
979
-							'context'     => array( 'view', 'edit', 'embed' ),
979
+							'context'     => array('view', 'edit', 'embed'),
980 980
 						),
981 981
 					),
982 982
 				),
983 983
 
984 984
 				'id'           => array(
985
-					'description' => __( 'Unique identifier for the invoice.', 'invoicing' ),
985
+					'description' => __('Unique identifier for the invoice.', 'invoicing'),
986 986
 					'type'        => 'integer',
987
-					'context'     => array( 'view', 'edit', 'embed' ),
987
+					'context'     => array('view', 'edit', 'embed'),
988 988
 					'readonly'    => true,
989 989
 				),
990 990
 
991 991
 				'key'			  => array(
992
-					'description' => __( 'A unique key for the invoice.', 'invoicing' ),
992
+					'description' => __('A unique key for the invoice.', 'invoicing'),
993 993
 					'type'        => 'string',
994
-					'context'     => array( 'view', 'edit', 'embed' ),
994
+					'context'     => array('view', 'edit', 'embed'),
995 995
 					'readonly'    => true,
996 996
 				),
997 997
 
998 998
 				'number'		  => array(
999
-					'description' => __( 'The invoice number.', 'invoicing' ),
999
+					'description' => __('The invoice number.', 'invoicing'),
1000 1000
 					'type'        => 'string',
1001
-					'context'     => array( 'view', 'edit', 'embed' ),
1001
+					'context'     => array('view', 'edit', 'embed'),
1002 1002
 					'readonly'    => true,
1003 1003
 				),
1004 1004
 
1005 1005
 				'transaction_id'  => array(
1006
-					'description' => __( 'The transaction id of the invoice.', 'invoicing' ),
1006
+					'description' => __('The transaction id of the invoice.', 'invoicing'),
1007 1007
 					'type'        => 'string',
1008
-					'context'     => array( 'view', 'edit', 'embed' ),
1008
+					'context'     => array('view', 'edit', 'embed'),
1009 1009
 				),
1010 1010
 
1011 1011
 				'gateway'		  => array(
1012
-					'description' => __( 'The gateway used to process the invoice.', 'invoicing' ),
1012
+					'description' => __('The gateway used to process the invoice.', 'invoicing'),
1013 1013
 					'type'        => 'string',
1014
-					'context'     => array( 'view', 'edit', 'embed' ),
1014
+					'context'     => array('view', 'edit', 'embed'),
1015 1015
 				),
1016 1016
 
1017 1017
 				'gateway_title'	  => array(
1018
-					'description' => __( 'The title of the gateway used to process the invoice.', 'invoicing' ),
1018
+					'description' => __('The title of the gateway used to process the invoice.', 'invoicing'),
1019 1019
 					'type'        => 'string',
1020
-					'context'     => array( 'view', 'edit', 'embed' ),
1020
+					'context'     => array('view', 'edit', 'embed'),
1021 1021
 				),
1022 1022
 
1023 1023
 				'total'	  		  => array(
1024
-					'description' => __( 'The total amount of the invoice.', 'invoicing' ),
1024
+					'description' => __('The total amount of the invoice.', 'invoicing'),
1025 1025
 					'type'        => 'number',
1026
-					'context'     => array( 'view', 'edit', 'embed' ),
1026
+					'context'     => array('view', 'edit', 'embed'),
1027 1027
 					'readonly'    => true,
1028 1028
 				),
1029 1029
 
1030 1030
 				'discount'		  => array(
1031
-					'description' => __( 'The discount applied to the invoice.', 'invoicing' ),
1031
+					'description' => __('The discount applied to the invoice.', 'invoicing'),
1032 1032
 					'type'        => 'number',
1033
-					'context'     => array( 'view', 'edit', 'embed' ),
1033
+					'context'     => array('view', 'edit', 'embed'),
1034 1034
 					'readonly'    => true,
1035 1035
 				),
1036 1036
 
1037 1037
 				'discount_code'	  => array(
1038
-					'description' => __( 'The discount code applied to the invoice.', 'invoicing' ),
1038
+					'description' => __('The discount code applied to the invoice.', 'invoicing'),
1039 1039
 					'type'        => 'string',
1040
-					'context'     => array( 'view', 'edit', 'embed' ),
1040
+					'context'     => array('view', 'edit', 'embed'),
1041 1041
 					'readonly'    => true,
1042 1042
 				),
1043 1043
 
1044 1044
 				'tax'	  		  => array(
1045
-					'description' => __( 'The tax applied to the invoice.', 'invoicing' ),
1045
+					'description' => __('The tax applied to the invoice.', 'invoicing'),
1046 1046
 					'type'        => 'number',
1047
-					'context'     => array( 'view', 'edit', 'embed' ),
1047
+					'context'     => array('view', 'edit', 'embed'),
1048 1048
 					'readonly'    => true,
1049 1049
 				),
1050 1050
 
1051 1051
 				'fees_total'	  => array(
1052
-					'description' => __( 'The total fees applied to the invoice.', 'invoicing' ),
1052
+					'description' => __('The total fees applied to the invoice.', 'invoicing'),
1053 1053
 					'type'        => 'number',
1054
-					'context'     => array( 'view', 'edit', 'embed' ),
1054
+					'context'     => array('view', 'edit', 'embed'),
1055 1055
 					'readonly'    => true,
1056 1056
 				),
1057 1057
 
1058 1058
 				'subtotal'	  	  => array(
1059
-					'description' => __( 'The sub-total for the invoice.', 'invoicing' ),
1059
+					'description' => __('The sub-total for the invoice.', 'invoicing'),
1060 1060
 					'type'        => 'number',
1061
-					'context'     => array( 'view', 'edit', 'embed' ),
1061
+					'context'     => array('view', 'edit', 'embed'),
1062 1062
 					'readonly'    => true,
1063 1063
 				),
1064 1064
 
1065 1065
 				'currency'	  	  => array(
1066
-					'description' => __( 'The currency used to process the invoice.', 'invoicing' ),
1066
+					'description' => __('The currency used to process the invoice.', 'invoicing'),
1067 1067
 					'type'        => 'string',
1068
-					'context'     => array( 'view', 'edit', 'embed' ),
1068
+					'context'     => array('view', 'edit', 'embed'),
1069 1069
 				),
1070 1070
 
1071 1071
 				'cart_details'	  => array(
1072
-					'description' => __( 'The cart details for invoice.', 'invoicing' ),
1072
+					'description' => __('The cart details for invoice.', 'invoicing'),
1073 1073
 					'type'        => 'array',
1074
-					'context'     => array( 'view', 'edit', 'embed' ),
1074
+					'context'     => array('view', 'edit', 'embed'),
1075 1075
 					'required'	  => true,
1076 1076
 				),
1077 1077
 
1078 1078
 				'date'         => array(
1079
-					'description' => __( "The date the invoice was published, in the site's timezone.", 'invoicing' ),
1080
-					'type'        => array( 'string', 'null' ),
1079
+					'description' => __("The date the invoice was published, in the site's timezone.", 'invoicing'),
1080
+					'type'        => array('string', 'null'),
1081 1081
 					'format'      => 'date-time',
1082
-					'context'     => array( 'view', 'edit', 'embed' ),
1082
+					'context'     => array('view', 'edit', 'embed'),
1083 1083
 				),
1084 1084
 
1085 1085
 				'due_date'     => array(
1086
-					'description' => __( 'The due date for the invoice.', 'invoicing' ),
1087
-					'type'        => array( 'string', 'null' ),
1086
+					'description' => __('The due date for the invoice.', 'invoicing'),
1087
+					'type'        => array('string', 'null'),
1088 1088
 					'format'      => 'date-time',
1089
-					'context'     => array( 'view', 'edit', 'embed' ),
1089
+					'context'     => array('view', 'edit', 'embed'),
1090 1090
 				),
1091 1091
 
1092 1092
 				'completed_date'  => array(
1093
-					'description' => __( 'The completed date for the invoice.', 'invoicing' ),
1094
-					'type'        => array( 'string', 'null' ),
1093
+					'description' => __('The completed date for the invoice.', 'invoicing'),
1094
+					'type'        => array('string', 'null'),
1095 1095
 					'format'      => 'date-time',
1096
-					'context'     => array( 'view', 'edit', 'embed' ),
1096
+					'context'     => array('view', 'edit', 'embed'),
1097 1097
 					'readonly'    => true,
1098 1098
 				),
1099 1099
 				
1100 1100
 				'link'         => array(
1101
-					'description' => __( 'URL to the invoice.', 'invoicing' ),
1101
+					'description' => __('URL to the invoice.', 'invoicing'),
1102 1102
 					'type'        => 'string',
1103 1103
 					'format'      => 'uri',
1104
-					'context'     => array( 'view', 'edit', 'embed' ),
1104
+					'context'     => array('view', 'edit', 'embed'),
1105 1105
 					'readonly'    => true,
1106 1106
 				),
1107 1107
 
1108 1108
 				'mode'       	  => array(
1109
-					'description' => __( 'The mode used to process the invoice.', 'invoicing' ),
1109
+					'description' => __('The mode used to process the invoice.', 'invoicing'),
1110 1110
 					'type'        => 'string',
1111
-					'enum'        => array( 'live', 'test' ),
1112
-					'context'     => array( 'view', 'edit', 'embed' ),
1111
+					'enum'        => array('live', 'test'),
1112
+					'context'     => array('view', 'edit', 'embed'),
1113 1113
 					'readonly'    => true,
1114 1114
 				),
1115 1115
 
1116 1116
 				'slug'       	  => array(
1117
-					'description' => __( 'An alphanumeric identifier for the invoice.', 'invoicing' ),
1117
+					'description' => __('An alphanumeric identifier for the invoice.', 'invoicing'),
1118 1118
 					'type'        => 'string',
1119
-					'context'     => array( 'view', 'edit', 'embed' ),
1119
+					'context'     => array('view', 'edit', 'embed'),
1120 1120
 					'arg_options' => array(
1121
-						'sanitize_callback' => array( $this, 'sanitize_slug' ),
1121
+						'sanitize_callback' => array($this, 'sanitize_slug'),
1122 1122
 					),
1123 1123
 					'readonly'    => true,
1124 1124
 				),
1125 1125
 
1126 1126
 				'status'       	  => array(
1127
-					'description' => __( 'A named status for the invoice.', 'invoicing' ),
1127
+					'description' => __('A named status for the invoice.', 'invoicing'),
1128 1128
 					'type'        => 'string',
1129
-					'enum'        => array_keys( wpinv_get_invoice_statuses( true, true ) ),
1130
-					'context'     => array( 'view', 'edit' ),
1129
+					'enum'        => array_keys(wpinv_get_invoice_statuses(true, true)),
1130
+					'context'     => array('view', 'edit'),
1131 1131
 					'default'	  => 'wpi-pending',
1132 1132
 				),
1133 1133
 
1134 1134
 				'status_nicename' => array(
1135
-					'description' => __( 'A human-readable status name for the invoice.', 'invoicing' ),
1135
+					'description' => __('A human-readable status name for the invoice.', 'invoicing'),
1136 1136
 					'type'        => 'string',
1137
-					'context'     => array( 'view', 'embed' ),
1137
+					'context'     => array('view', 'embed'),
1138 1138
 					'readonly'    => true,
1139 1139
 				),
1140 1140
 
1141 1141
 				'post_type'       => array(
1142
-					'description' => __( 'The post type for the invoice.', 'invoicing' ),
1142
+					'description' => __('The post type for the invoice.', 'invoicing'),
1143 1143
 					'type'        => 'string',
1144
-					'context'     => array( 'view' ),
1144
+					'context'     => array('view'),
1145 1145
 					'readonly'    => true,
1146 1146
 				),
1147 1147
 			),
@@ -1159,12 +1159,12 @@  discard block
 block discarded – undo
1159 1159
 		 *
1160 1160
 		 * @param array   $schema    The invoice schema.
1161 1161
 		 */
1162
-        $schema = apply_filters( "wpinv_rest_invoice_schema", $schema );
1162
+        $schema = apply_filters("wpinv_rest_invoice_schema", $schema);
1163 1163
 
1164 1164
 		// Cache the invoice schema.
1165 1165
 		$this->schema = $schema;
1166 1166
 		
1167
-		return $this->add_additional_fields_schema( $this->schema );
1167
+		return $this->add_additional_fields_schema($this->schema);
1168 1168
 	}
1169 1169
 
1170 1170
 	/**
@@ -1176,20 +1176,20 @@  discard block
 block discarded – undo
1176 1176
 	 */
1177 1177
 	protected function get_schema_links() {
1178 1178
 
1179
-		$href = rest_url( "{$this->namespace}/{$this->rest_base}/{id}" );
1179
+		$href = rest_url("{$this->namespace}/{$this->rest_base}/{id}");
1180 1180
 
1181 1181
 		$links = array();
1182 1182
 
1183 1183
 		$links[] = array(
1184 1184
 			'rel'          => 'https://api.w.org/action-publish',
1185
-			'title'        => __( 'The current user can mark this invoice as completed.', 'invoicing' ),
1185
+			'title'        => __('The current user can mark this invoice as completed.', 'invoicing'),
1186 1186
 			'href'         => $href,
1187 1187
 			'targetSchema' => array(
1188 1188
 				'type'       => 'object',
1189 1189
 				'properties' => array(
1190 1190
 					'status' => array(
1191 1191
 						'type' => 'string',
1192
-						'enum' => array( 'publish', 'wpi-renewal' ),
1192
+						'enum' => array('publish', 'wpi-renewal'),
1193 1193
 					),
1194 1194
 				),
1195 1195
 			),
@@ -1197,7 +1197,7 @@  discard block
 block discarded – undo
1197 1197
 
1198 1198
 		$links[] = array(
1199 1199
 			'rel'          => 'https://api.w.org/action-assign-author',
1200
-			'title'        => __( 'The current user can change the owner of this invoice.', 'invoicing' ),
1200
+			'title'        => __('The current user can change the owner of this invoice.', 'invoicing'),
1201 1201
 			'href'         => $href,
1202 1202
 			'targetSchema' => array(
1203 1203
 				'type'       => 'object',
@@ -1220,24 +1220,24 @@  discard block
 block discarded – undo
1220 1220
 	 * @param WPInv_Invoice $invoice Invoice Object.
1221 1221
 	 * @return array Links for the given invoice.
1222 1222
 	 */
1223
-	protected function prepare_links( $invoice ) {
1223
+	protected function prepare_links($invoice) {
1224 1224
 
1225 1225
 		// Prepare the base REST API endpoint for invoices.
1226
-		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
1226
+		$base = sprintf('%s/%s', $this->namespace, $this->rest_base);
1227 1227
 
1228 1228
 		// Entity meta.
1229 1229
 		$links = array(
1230 1230
 			'self'       => array(
1231
-				'href' => rest_url( trailingslashit( $base ) . $invoice->ID ),
1231
+				'href' => rest_url(trailingslashit($base) . $invoice->ID),
1232 1232
 			),
1233 1233
 			'collection' => array(
1234
-				'href' => rest_url( $base ),
1234
+				'href' => rest_url($base),
1235 1235
 			),
1236 1236
 		);
1237 1237
 
1238
-		if ( ! empty( $invoice->user_id ) ) {
1238
+		if (!empty($invoice->user_id)) {
1239 1239
 			$links['user'] = array(
1240
-				'href'       => rest_url( 'wp/v2/users/' . $invoice->user_id ),
1240
+				'href'       => rest_url('wp/v2/users/' . $invoice->user_id),
1241 1241
 				'embeddable' => true,
1242 1242
 			);
1243 1243
 		}
@@ -1251,7 +1251,7 @@  discard block
 block discarded – undo
1251 1251
 		 *
1252 1252
 		 * @param array   $links    Rest links.
1253 1253
 		 */
1254
-		return apply_filters( "wpinv_invoice_rest_links", $links );
1254
+		return apply_filters("wpinv_invoice_rest_links", $links);
1255 1255
 
1256 1256
 	}
1257 1257
 
@@ -1264,24 +1264,24 @@  discard block
 block discarded – undo
1264 1264
 	 * @param WP_REST_Request $request Request object.
1265 1265
 	 * @return array List of link relations.
1266 1266
 	 */
1267
-	protected function get_available_actions( $invoice, $request ) {
1267
+	protected function get_available_actions($invoice, $request) {
1268 1268
 
1269
-		if ( 'edit' !== $request['context'] ) {
1269
+		if ('edit' !== $request['context']) {
1270 1270
 			return array();
1271 1271
 		}
1272 1272
 
1273 1273
 		$rels = array();
1274 1274
 
1275 1275
 		// Retrieve the post type object.
1276
-		$post_type = get_post_type_object( $invoice->post_type );
1276
+		$post_type = get_post_type_object($invoice->post_type);
1277 1277
 
1278 1278
 		// Mark invoice as completed.
1279
-		if ( current_user_can( $post_type->cap->publish_posts ) ) {
1279
+		if (current_user_can($post_type->cap->publish_posts)) {
1280 1280
 			$rels[] = 'https://api.w.org/action-publish';
1281 1281
 		}
1282 1282
 
1283 1283
 		// Change the owner of the invoice.
1284
-		if ( current_user_can( $post_type->cap->edit_others_posts ) ) {
1284
+		if (current_user_can($post_type->cap->edit_others_posts)) {
1285 1285
 			$rels[] = 'https://api.w.org/action-assign-author';
1286 1286
 		}
1287 1287
 
@@ -1294,7 +1294,7 @@  discard block
 block discarded – undo
1294 1294
 		 *
1295 1295
 		 * @param array   $rels    Available link relations.
1296 1296
 		 */
1297
-		return apply_filters( "wpinv_invoice_rest_link_relations", $rels );
1297
+		return apply_filters("wpinv_invoice_rest_link_relations", $rels);
1298 1298
 	}
1299 1299
 
1300 1300
 	/**
@@ -1307,11 +1307,11 @@  discard block
 block discarded – undo
1307 1307
 	 * @param string          $parameter Additional parameter to pass to validation.
1308 1308
 	 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
1309 1309
 	 */
1310
-	public function sanitize_post_statuses( $statuses, $request, $parameter ) {
1310
+	public function sanitize_post_statuses($statuses, $request, $parameter) {
1311 1311
 
1312
-		$statuses 	  = wp_parse_slug_list( $statuses );
1313
-		$valid_statuses = array_keys( wpinv_get_invoice_statuses( true, true ) );
1314
-		return array_intersect( $statuses, $valid_statuses );
1312
+		$statuses = wp_parse_slug_list($statuses);
1313
+		$valid_statuses = array_keys(wpinv_get_invoice_statuses(true, true));
1314
+		return array_intersect($statuses, $valid_statuses);
1315 1315
 		
1316 1316
 	}
1317 1317
     
Please login to merge, or discard this patch.