Passed
Push — master ( bbe6ac...d1758c )
by Brian
05:12
created
includes/class-wpinv-ajax.php 1 patch
Spacing   +206 added lines, -206 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
6 6
  * @package Invoicing
7 7
  */
8 8
  
9
-defined( 'ABSPATH' ) || exit;
9
+defined('ABSPATH') || exit;
10 10
 
11 11
 /**
12 12
  * WPInv_Ajax class.
@@ -17,8 +17,8 @@  discard block
 block discarded – undo
17 17
 	 * Hook in ajax handlers.
18 18
 	 */
19 19
 	public static function init() {
20
-		add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
21
-		add_action( 'template_redirect', array( __CLASS__, 'do_wpinv_ajax' ), 0 );
20
+		add_action('init', array(__CLASS__, 'define_ajax'), 0);
21
+		add_action('template_redirect', array(__CLASS__, 'do_wpinv_ajax'), 0);
22 22
 		self::add_ajax_events();
23 23
     }
24 24
 
@@ -27,11 +27,11 @@  discard block
 block discarded – undo
27 27
 	 */
28 28
 	public static function define_ajax() {
29 29
 
30
-		if ( ! empty( $_GET['wpinv-ajax'] ) ) {
31
-			getpaid_maybe_define_constant( 'DOING_AJAX', true );
32
-			getpaid_maybe_define_constant( 'WPInv_DOING_AJAX', true );
33
-			if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
34
-				/** @scrutinizer ignore-unhandled */ @ini_set( 'display_errors', 0 );
30
+		if (!empty($_GET['wpinv-ajax'])) {
31
+			getpaid_maybe_define_constant('DOING_AJAX', true);
32
+			getpaid_maybe_define_constant('WPInv_DOING_AJAX', true);
33
+			if (!WP_DEBUG || (WP_DEBUG && !WP_DEBUG_DISPLAY)) {
34
+				/** @scrutinizer ignore-unhandled */ @ini_set('display_errors', 0);
35 35
 			}
36 36
 			$GLOBALS['wpdb']->hide_errors();
37 37
 		}
@@ -44,13 +44,13 @@  discard block
 block discarded – undo
44 44
 	 * @since 1.0.18
45 45
 	 */
46 46
 	private static function wpinv_ajax_headers() {
47
-		if ( ! headers_sent() ) {
47
+		if (!headers_sent()) {
48 48
 			send_origin_headers();
49 49
 			send_nosniff_header();
50 50
 			nocache_headers();
51
-			header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
52
-			header( 'X-Robots-Tag: noindex' );
53
-			status_header( 200 );
51
+			header('Content-Type: text/html; charset=' . get_option('blog_charset'));
52
+			header('X-Robots-Tag: noindex');
53
+			status_header(200);
54 54
 		}
55 55
     }
56 56
     
@@ -60,16 +60,16 @@  discard block
 block discarded – undo
60 60
 	public static function do_wpinv_ajax() {
61 61
 		global $wp_query;
62 62
 
63
-		if ( ! empty( $_GET['wpinv-ajax'] ) ) {
64
-			$wp_query->set( 'wpinv-ajax', sanitize_text_field( wp_unslash( $_GET['wpinv-ajax'] ) ) );
63
+		if (!empty($_GET['wpinv-ajax'])) {
64
+			$wp_query->set('wpinv-ajax', sanitize_text_field(wp_unslash($_GET['wpinv-ajax'])));
65 65
 		}
66 66
 
67
-		$action = $wp_query->get( 'wpinv-ajax' );
67
+		$action = $wp_query->get('wpinv-ajax');
68 68
 
69
-		if ( $action ) {
69
+		if ($action) {
70 70
 			self::wpinv_ajax_headers();
71
-			$action = sanitize_text_field( $action );
72
-			do_action( 'wpinv_ajax_' . $action );
71
+			$action = sanitize_text_field($action);
72
+			do_action('wpinv_ajax_' . $action);
73 73
 			wp_die();
74 74
 		}
75 75
 
@@ -101,36 +101,36 @@  discard block
 block discarded – undo
101 101
             'payment_form_refresh_prices' => true,
102 102
         );
103 103
 
104
-        foreach ( $ajax_events as $ajax_event => $nopriv ) {
105
-            add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) );
106
-            add_action( 'wp_ajax_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) );
104
+        foreach ($ajax_events as $ajax_event => $nopriv) {
105
+            add_action('wp_ajax_wpinv_' . $ajax_event, array(__CLASS__, $ajax_event));
106
+            add_action('wp_ajax_getpaid_' . $ajax_event, array(__CLASS__, $ajax_event));
107 107
 
108
-            if ( $nopriv ) {
109
-                add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) );
110
-                add_action( 'wp_ajax_nopriv_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) );
111
-                add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
108
+            if ($nopriv) {
109
+                add_action('wp_ajax_nopriv_wpinv_' . $ajax_event, array(__CLASS__, $ajax_event));
110
+                add_action('wp_ajax_nopriv_getpaid_' . $ajax_event, array(__CLASS__, $ajax_event));
111
+                add_action('wpinv_ajax_' . $ajax_event, array(__CLASS__, $ajax_event));
112 112
             }
113 113
         }
114 114
     }
115 115
     
116 116
     public static function add_note() {
117
-        check_ajax_referer( 'add-invoice-note', '_nonce' );
117
+        check_ajax_referer('add-invoice-note', '_nonce');
118 118
 
119
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
119
+        if (!wpinv_current_user_can_manage_invoicing()) {
120 120
             die(-1);
121 121
         }
122 122
 
123
-        $post_id   = absint( $_POST['post_id'] );
124
-        $note      = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) );
125
-        $note_type = sanitize_text_field( $_POST['note_type'] );
123
+        $post_id   = absint($_POST['post_id']);
124
+        $note      = wp_kses_post(trim(stripslashes($_POST['note'])));
125
+        $note_type = sanitize_text_field($_POST['note_type']);
126 126
 
127 127
         $is_customer_note = $note_type == 'customer' ? 1 : 0;
128 128
 
129
-        if ( $post_id > 0 ) {
130
-            $note_id = wpinv_insert_payment_note( $post_id, $note, $is_customer_note );
129
+        if ($post_id > 0) {
130
+            $note_id = wpinv_insert_payment_note($post_id, $note, $is_customer_note);
131 131
 
132
-            if ( $note_id > 0 && !is_wp_error( $note_id ) ) {
133
-                wpinv_get_invoice_note_line_item( $note_id );
132
+            if ($note_id > 0 && !is_wp_error($note_id)) {
133
+                wpinv_get_invoice_note_line_item($note_id);
134 134
             }
135 135
         }
136 136
 
@@ -138,16 +138,16 @@  discard block
 block discarded – undo
138 138
     }
139 139
 
140 140
     public static function delete_note() {
141
-        check_ajax_referer( 'delete-invoice-note', '_nonce' );
141
+        check_ajax_referer('delete-invoice-note', '_nonce');
142 142
 
143
-        if ( !wpinv_current_user_can_manage_invoicing() ) {
143
+        if (!wpinv_current_user_can_manage_invoicing()) {
144 144
             die(-1);
145 145
         }
146 146
 
147
-        $note_id = (int)$_POST['note_id'];
147
+        $note_id = (int) $_POST['note_id'];
148 148
 
149
-        if ( $note_id > 0 ) {
150
-            wp_delete_comment( $note_id, true );
149
+        if ($note_id > 0) {
150
+            wp_delete_comment($note_id, true);
151 151
         }
152 152
 
153 153
         die();
@@ -165,34 +165,34 @@  discard block
 block discarded – undo
165 165
     public static function get_billing_details() {
166 166
 
167 167
         // Verify nonce.
168
-        check_ajax_referer( 'wpinv-nonce' );
168
+        check_ajax_referer('wpinv-nonce');
169 169
 
170 170
         // Can the user manage the plugin?
171
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
171
+        if (!wpinv_current_user_can_manage_invoicing()) {
172 172
             die(-1);
173 173
         }
174 174
 
175 175
         // Do we have a user id?
176 176
         $user_id = $_GET['user_id'];
177 177
 
178
-        if ( empty( $user_id ) || ! is_numeric( $user_id ) ) {
178
+        if (empty($user_id) || !is_numeric($user_id)) {
179 179
             die(-1);
180 180
         }
181 181
 
182 182
         // Fetch the billing details.
183
-        $billing_details    = wpinv_get_user_address( $user_id );
184
-        $billing_details    = apply_filters( 'wpinv_ajax_billing_details', $billing_details, $user_id );
183
+        $billing_details    = wpinv_get_user_address($user_id);
184
+        $billing_details    = apply_filters('wpinv_ajax_billing_details', $billing_details, $user_id);
185 185
 
186 186
         // unset the user id and email.
187
-        $to_ignore = array( 'user_id', 'email' );
187
+        $to_ignore = array('user_id', 'email');
188 188
 
189
-        foreach ( $to_ignore as $key ) {
190
-            if ( isset( $billing_details[ $key ] ) ) {
191
-                unset( $billing_details[ $key ] );
189
+        foreach ($to_ignore as $key) {
190
+            if (isset($billing_details[$key])) {
191
+                unset($billing_details[$key]);
192 192
             }
193 193
         }
194 194
 
195
-        wp_send_json_success( $billing_details );
195
+        wp_send_json_success($billing_details);
196 196
 
197 197
     }
198 198
 
@@ -202,47 +202,47 @@  discard block
 block discarded – undo
202 202
     public static function check_new_user_email() {
203 203
 
204 204
         // Verify nonce.
205
-        check_ajax_referer( 'wpinv-nonce' );
205
+        check_ajax_referer('wpinv-nonce');
206 206
 
207 207
         // Can the user manage the plugin?
208
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
208
+        if (!wpinv_current_user_can_manage_invoicing()) {
209 209
             die(-1);
210 210
         }
211 211
 
212 212
         // We need an email address.
213
-        if ( empty( $_GET['email'] ) ) {
214
-            _e( "Provide the new user's email address", 'invoicing' );
213
+        if (empty($_GET['email'])) {
214
+            _e("Provide the new user's email address", 'invoicing');
215 215
             exit;
216 216
         }
217 217
 
218 218
         // Ensure the email is valid.
219
-        $email = sanitize_text_field( $_GET['email'] );
220
-        if ( ! is_email( $email ) ) {
221
-            _e( 'Invalid email address', 'invoicing' );
219
+        $email = sanitize_text_field($_GET['email']);
220
+        if (!is_email($email)) {
221
+            _e('Invalid email address', 'invoicing');
222 222
             exit;
223 223
         }
224 224
 
225 225
         // And it does not exist.
226
-        if ( email_exists( $email ) ) {
227
-            _e( 'A user with this email address already exists', 'invoicing' );
226
+        if (email_exists($email)) {
227
+            _e('A user with this email address already exists', 'invoicing');
228 228
             exit;
229 229
         }
230 230
 
231
-        wp_send_json_success( true );
231
+        wp_send_json_success(true);
232 232
     }
233 233
     
234 234
     public static function run_tool() {
235
-        check_ajax_referer( 'wpinv-nonce', '_nonce' );
236
-        if ( !wpinv_current_user_can_manage_invoicing() ) {
235
+        check_ajax_referer('wpinv-nonce', '_nonce');
236
+        if (!wpinv_current_user_can_manage_invoicing()) {
237 237
             die(-1);
238 238
         }
239 239
         
240
-        $tool = sanitize_text_field( $_POST['tool'] );
240
+        $tool = sanitize_text_field($_POST['tool']);
241 241
         
242
-        do_action( 'wpinv_run_tool' );
242
+        do_action('wpinv_run_tool');
243 243
         
244
-        if ( !empty( $tool ) ) {
245
-            do_action( 'wpinv_tool_' . $tool );
244
+        if (!empty($tool)) {
245
+            do_action('wpinv_tool_' . $tool);
246 246
         }
247 247
     }
248 248
 
@@ -252,27 +252,27 @@  discard block
 block discarded – undo
252 252
     public static function get_payment_form() {
253 253
 
254 254
         // Check nonce.
255
-        check_ajax_referer( 'getpaid_form_nonce' );
255
+        check_ajax_referer('getpaid_form_nonce');
256 256
 
257 257
         // Is the request set up correctly?
258
-		if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) {
258
+		if (empty($_GET['form']) && empty($_GET['item'])) {
259 259
 			echo aui()->alert(
260 260
 				array(
261 261
 					'type'    => 'warning',
262
-					'content' => __( 'No payment form or item provided', 'invoicing' ),
262
+					'content' => __('No payment form or item provided', 'invoicing'),
263 263
 				)
264 264
             );
265 265
             exit;
266 266
         }
267 267
 
268 268
         // Payment form or button?
269
-		if ( ! empty( $_GET['form'] ) ) {
270
-            getpaid_display_payment_form( urldecode( $_GET['form'] ) );
271
-		} else if( ! empty( $_GET['invoice'] ) ) {
272
-		    getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) );
269
+		if (!empty($_GET['form'])) {
270
+            getpaid_display_payment_form(urldecode($_GET['form']));
271
+		} else if (!empty($_GET['invoice'])) {
272
+		    getpaid_display_invoice_payment_form(urldecode($_GET['invoice']));
273 273
         } else {
274
-			$items = getpaid_convert_items_to_array( urldecode( $_GET['item'] ) );
275
-		    getpaid_display_item_payment_form( $items );
274
+			$items = getpaid_convert_items_to_array(urldecode($_GET['item']));
275
+		    getpaid_display_item_payment_form($items);
276 276
         }
277 277
 
278 278
         exit;
@@ -287,17 +287,17 @@  discard block
 block discarded – undo
287 287
     public static function payment_form() {
288 288
 
289 289
         // Check nonce.
290
-        check_ajax_referer( 'getpaid_form_nonce' );
290
+        check_ajax_referer('getpaid_form_nonce');
291 291
 
292 292
         // ... form fields...
293
-        if ( empty( $_POST['getpaid_payment_form_submission'] ) ) {
294
-            _e( 'Error: Reload the page and try again.', 'invoicing' );
293
+        if (empty($_POST['getpaid_payment_form_submission'])) {
294
+            _e('Error: Reload the page and try again.', 'invoicing');
295 295
             exit;
296 296
         }
297 297
 
298 298
         // Process the payment form.
299
-        $checkout_class = apply_filters( 'getpaid_checkout_class', 'GetPaid_Checkout' );
300
-        $checkout       = new $checkout_class( new GetPaid_Payment_Form_Submission() );
299
+        $checkout_class = apply_filters('getpaid_checkout_class', 'GetPaid_Checkout');
300
+        $checkout       = new $checkout_class(new GetPaid_Payment_Form_Submission());
301 301
         $checkout->process_checkout();
302 302
 
303 303
         exit;
@@ -310,55 +310,55 @@  discard block
 block discarded – undo
310 310
      */
311 311
     public static function get_payment_form_states_field() {
312 312
 
313
-        if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) {
313
+        if (empty($_GET['country']) || empty($_GET['form'])) {
314 314
             exit;
315 315
         }
316 316
 
317
-        $elements = getpaid_get_payment_form_elements( $_GET['form'] );
317
+        $elements = getpaid_get_payment_form_elements($_GET['form']);
318 318
 
319
-        if ( empty( $elements ) ) {
319
+        if (empty($elements)) {
320 320
             exit;
321 321
         }
322 322
 
323 323
         $address_fields = array();
324
-        foreach ( $elements as $element ) {
325
-            if ( 'address' === $element['type'] ) {
324
+        foreach ($elements as $element) {
325
+            if ('address' === $element['type']) {
326 326
                 $address_fields = $element;
327 327
                 break;
328 328
             }
329 329
         }
330 330
 
331
-        if ( empty( $address_fields ) ) {
331
+        if (empty($address_fields)) {
332 332
             exit;
333 333
         }
334 334
 
335
-        foreach ( $address_fields['fields'] as $address_field ) {
335
+        foreach ($address_fields['fields'] as $address_field) {
336 336
 
337
-            if ( 'wpinv_state' == $address_field['name'] ) {
337
+            if ('wpinv_state' == $address_field['name']) {
338 338
 
339
-                $wrap_class  = getpaid_get_form_element_grid_class( $address_field );
340
-                $wrap_class  = esc_attr( "$wrap_class getpaid-address-field-wrapper" );
341
-                $placeholder = empty( $address_field['placeholder'] ) ? '' : esc_attr( $address_field['placeholder'] );
342
-                $description = empty( $address_field['description'] ) ? '' : wp_kses_post( $address_field['description'] );
343
-                $value       = is_user_logged_in() ? get_user_meta( get_current_user_id(), '_wpinv_state', true ) : '';
344
-                $label       = empty( $address_field['label'] ) ? '' : wp_kses_post( $address_field['label'] );
339
+                $wrap_class  = getpaid_get_form_element_grid_class($address_field);
340
+                $wrap_class  = esc_attr("$wrap_class getpaid-address-field-wrapper");
341
+                $placeholder = empty($address_field['placeholder']) ? '' : esc_attr($address_field['placeholder']);
342
+                $description = empty($address_field['description']) ? '' : wp_kses_post($address_field['description']);
343
+                $value       = is_user_logged_in() ? get_user_meta(get_current_user_id(), '_wpinv_state', true) : '';
344
+                $label       = empty($address_field['label']) ? '' : wp_kses_post($address_field['label']);
345 345
 
346
-                if ( ! empty( $address_field['required'] ) ) {
346
+                if (!empty($address_field['required'])) {
347 347
                     $label .= "<span class='text-danger'> *</span>";
348 348
                 }
349 349
 
350
-                $html = getpaid_get_states_select_markup (
351
-                    sanitize_text_field( $_GET['country'] ),
350
+                $html = getpaid_get_states_select_markup(
351
+                    sanitize_text_field($_GET['country']),
352 352
                     $value,
353 353
                     $placeholder,
354 354
                     $label,
355 355
                     $description,
356
-                    ! empty( $address_field['required'] ),
356
+                    !empty($address_field['required']),
357 357
                     $wrap_class,
358
-                    wpinv_clean( $_GET['name'] )
358
+                    wpinv_clean($_GET['name'])
359 359
                 );
360 360
 
361
-                wp_send_json_success( $html );
361
+                wp_send_json_success($html);
362 362
                 exit;
363 363
 
364 364
             }
@@ -374,66 +374,66 @@  discard block
 block discarded – undo
374 374
     public static function recalculate_invoice_totals() {
375 375
 
376 376
         // Verify nonce.
377
-        check_ajax_referer( 'wpinv-nonce' );
377
+        check_ajax_referer('wpinv-nonce');
378 378
 
379
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
379
+        if (!wpinv_current_user_can_manage_invoicing()) {
380 380
             exit;
381 381
         }
382 382
 
383 383
         // We need an invoice.
384
-        if ( empty( $_POST['post_id'] ) ) {
384
+        if (empty($_POST['post_id'])) {
385 385
             exit;
386 386
         }
387 387
 
388 388
         // Fetch the invoice.
389
-        $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) );
389
+        $invoice = new WPInv_Invoice(trim($_POST['post_id']));
390 390
 
391 391
         // Ensure it exists.
392
-        if ( ! $invoice->get_id() ) {
392
+        if (!$invoice->get_id()) {
393 393
             exit;
394 394
         }
395 395
 
396 396
         // Maybe set the country, state, currency.
397
-        foreach ( array( 'country', 'state', 'currency', 'vat_number', 'discount_code' ) as $key ) {
398
-            if ( isset( $_POST[ $key ] ) ) {
397
+        foreach (array('country', 'state', 'currency', 'vat_number', 'discount_code') as $key) {
398
+            if (isset($_POST[$key])) {
399 399
                 $method = "set_$key";
400
-                $invoice->$method( sanitize_text_field( $_POST[ $key ] ) );
400
+                $invoice->$method(sanitize_text_field($_POST[$key]));
401 401
             }
402 402
         }
403 403
 
404 404
         // Maybe disable taxes.
405
-        $invoice->set_disable_taxes( ! empty( $_POST['taxes'] ) );
405
+        $invoice->set_disable_taxes(!empty($_POST['taxes']));
406 406
 
407 407
         // Discount code.
408
-        if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) {
409
-            $discount = new WPInv_Discount( $invoice->get_discount_code() );
410
-            if ( $discount->exists() ) {
411
-                $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) );
408
+        if (!$invoice->is_paid() && !$invoice->is_refunded()) {
409
+            $discount = new WPInv_Discount($invoice->get_discount_code());
410
+            if ($discount->exists()) {
411
+                $invoice->add_discount(getpaid_calculate_invoice_discount($invoice, $discount));
412 412
             } else {
413
-                $invoice->remove_discount( 'discount_code' );
413
+                $invoice->remove_discount('discount_code');
414 414
             }
415 415
         }
416 416
 
417 417
         // Recalculate totals.
418 418
         $invoice->recalculate_total();
419 419
 
420
-        $total = wpinv_price( $invoice->get_total(), $invoice->get_currency() );
420
+        $total = wpinv_price($invoice->get_total(), $invoice->get_currency());
421 421
 
422
-        if ( $invoice->is_recurring() && $invoice->is_parent() && $invoice->get_total() != $invoice->get_recurring_total() ) {
423
-            $recurring_total = wpinv_price( $invoice->get_recurring_total(), $invoice->get_currency() );
424
-            $total          .= '<small class="form-text text-muted">' . sprintf( __( 'Recurring Price: %s', 'invoicing' ), $recurring_total ) . '</small>';
422
+        if ($invoice->is_recurring() && $invoice->is_parent() && $invoice->get_total() != $invoice->get_recurring_total()) {
423
+            $recurring_total = wpinv_price($invoice->get_recurring_total(), $invoice->get_currency());
424
+            $total          .= '<small class="form-text text-muted">' . sprintf(__('Recurring Price: %s', 'invoicing'), $recurring_total) . '</small>';
425 425
         }
426 426
 
427 427
         $totals = array(
428
-            'subtotal' => wpinv_price( $invoice->get_subtotal(), $invoice->get_currency() ),
429
-            'discount' => wpinv_price( $invoice->get_total_discount(), $invoice->get_currency() ),
430
-            'tax'      => wpinv_price( $invoice->get_total_tax(), $invoice->get_currency() ),
428
+            'subtotal' => wpinv_price($invoice->get_subtotal(), $invoice->get_currency()),
429
+            'discount' => wpinv_price($invoice->get_total_discount(), $invoice->get_currency()),
430
+            'tax'      => wpinv_price($invoice->get_total_tax(), $invoice->get_currency()),
431 431
             'total'    => $total,
432 432
         );
433 433
 
434
-        $totals = apply_filters( 'getpaid_invoice_totals', $totals, $invoice );
434
+        $totals = apply_filters('getpaid_invoice_totals', $totals, $invoice);
435 435
 
436
-        wp_send_json_success( compact( 'totals' ) );
436
+        wp_send_json_success(compact('totals'));
437 437
     }
438 438
 
439 439
     /**
@@ -442,33 +442,33 @@  discard block
 block discarded – undo
442 442
     public static function get_invoice_items() {
443 443
 
444 444
         // Verify nonce.
445
-        check_ajax_referer( 'wpinv-nonce' );
445
+        check_ajax_referer('wpinv-nonce');
446 446
 
447
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
447
+        if (!wpinv_current_user_can_manage_invoicing()) {
448 448
             exit;
449 449
         }
450 450
 
451 451
         // We need an invoice and items.
452
-        if ( empty( $_POST['post_id'] ) ) {
452
+        if (empty($_POST['post_id'])) {
453 453
             exit;
454 454
         }
455 455
 
456 456
         // Fetch the invoice.
457
-        $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) );
457
+        $invoice = new WPInv_Invoice(trim($_POST['post_id']));
458 458
 
459 459
         // Ensure it exists.
460
-        if ( ! $invoice->get_id() ) {
460
+        if (!$invoice->get_id()) {
461 461
             exit;
462 462
         }
463 463
 
464 464
         // Return an array of invoice items.
465 465
         $items = array();
466 466
 
467
-        foreach ( $invoice->get_items() as $item_id => $item ) {
468
-            $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax(  $invoice->get_currency(), $invoice->is_renewal()  );
467
+        foreach ($invoice->get_items() as $item_id => $item) {
468
+            $items[$item_id] = $item->prepare_data_for_invoice_edit_ajax($invoice->get_currency(), $invoice->is_renewal());
469 469
         }
470 470
 
471
-        wp_send_json_success( compact( 'items' ) );
471
+        wp_send_json_success(compact('items'));
472 472
     }
473 473
 
474 474
     /**
@@ -477,50 +477,50 @@  discard block
 block discarded – undo
477 477
     public static function edit_invoice_item() {
478 478
 
479 479
         // Verify nonce.
480
-        check_ajax_referer( 'wpinv-nonce' );
480
+        check_ajax_referer('wpinv-nonce');
481 481
 
482
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
482
+        if (!wpinv_current_user_can_manage_invoicing()) {
483 483
             exit;
484 484
         }
485 485
 
486 486
         // We need an invoice and item details.
487
-        if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) {
487
+        if (empty($_POST['post_id']) || empty($_POST['data'])) {
488 488
             exit;
489 489
         }
490 490
 
491 491
         // Fetch the invoice.
492
-        $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) );
492
+        $invoice = new WPInv_Invoice(trim($_POST['post_id']));
493 493
 
494 494
         // Ensure it exists and its not been paid for.
495
-        if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) {
495
+        if (!$invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded()) {
496 496
             exit;
497 497
         }
498 498
 
499 499
         // Format the data.
500
-        $data = wp_unslash( wp_list_pluck( $_POST['data'], 'value', 'field' ) );
500
+        $data = wp_unslash(wp_list_pluck($_POST['data'], 'value', 'field'));
501 501
 
502 502
         // Ensure that we have an item id.
503
-        if ( empty( $data['id'] ) ) {
503
+        if (empty($data['id'])) {
504 504
             exit;
505 505
         }
506 506
 
507 507
         // Abort if the invoice does not have the specified item.
508
-        $item = $invoice->get_item( (int) $data['id'] );
508
+        $item = $invoice->get_item((int) $data['id']);
509 509
 
510
-        if ( empty( $item ) ) {
510
+        if (empty($item)) {
511 511
             exit;
512 512
         }
513 513
 
514 514
         // Update the item.
515
-        $item->set_price( floatval( $data['price'] ) );
516
-        $item->set_name( sanitize_text_field( $data['name'] ) );
517
-        $item->set_description( wp_kses_post( $data['description'] ) );
518
-        $item->set_quantity( intval( $data['quantity'] ) );
515
+        $item->set_price(floatval($data['price']));
516
+        $item->set_name(sanitize_text_field($data['name']));
517
+        $item->set_description(wp_kses_post($data['description']));
518
+        $item->set_quantity(intval($data['quantity']));
519 519
 
520 520
         // Add it to the invoice.
521
-        $error = $invoice->add_item( $item );
521
+        $error = $invoice->add_item($item);
522 522
         $alert = false;
523
-        if ( is_wp_error( $error ) ) {
523
+        if (is_wp_error($error)) {
524 524
             $alert = $error->get_error_message();
525 525
         }
526 526
 
@@ -533,11 +533,11 @@  discard block
 block discarded – undo
533 533
         // Return an array of invoice items.
534 534
         $items = array();
535 535
 
536
-        foreach ( $invoice->get_items() as $item_id => $item ) {
537
-            $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax(  $invoice->get_currency()  );
536
+        foreach ($invoice->get_items() as $item_id => $item) {
537
+            $items[$item_id] = $item->prepare_data_for_invoice_edit_ajax($invoice->get_currency());
538 538
         }
539 539
 
540
-        wp_send_json_success( compact( 'items', 'alert' ) );
540
+        wp_send_json_success(compact('items', 'alert'));
541 541
     }
542 542
 
543 543
     /**
@@ -546,33 +546,33 @@  discard block
 block discarded – undo
546 546
     public static function remove_invoice_item() {
547 547
 
548 548
         // Verify nonce.
549
-        check_ajax_referer( 'wpinv-nonce' );
549
+        check_ajax_referer('wpinv-nonce');
550 550
 
551
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
551
+        if (!wpinv_current_user_can_manage_invoicing()) {
552 552
             exit;
553 553
         }
554 554
 
555 555
         // We need an invoice and an item.
556
-        if ( empty( $_POST['post_id'] ) || empty( $_POST['item_id'] ) ) {
556
+        if (empty($_POST['post_id']) || empty($_POST['item_id'])) {
557 557
             exit;
558 558
         }
559 559
 
560 560
         // Fetch the invoice.
561
-        $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) );
561
+        $invoice = new WPInv_Invoice(trim($_POST['post_id']));
562 562
 
563 563
         // Ensure it exists and its not been paid for.
564
-        if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) {
564
+        if (!$invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded()) {
565 565
             exit;
566 566
         }
567 567
 
568 568
         // Abort if the invoice does not have the specified item.
569
-        $item = $invoice->get_item( (int) $_POST['item_id'] );
569
+        $item = $invoice->get_item((int) $_POST['item_id']);
570 570
 
571
-        if ( empty( $item ) ) {
571
+        if (empty($item)) {
572 572
             exit;
573 573
         }
574 574
 
575
-        $invoice->remove_item( (int) $_POST['item_id'] );
575
+        $invoice->remove_item((int) $_POST['item_id']);
576 576
 
577 577
         // Update totals.
578 578
         $invoice->recalculate_total();
@@ -583,11 +583,11 @@  discard block
 block discarded – undo
583 583
         // Return an array of invoice items.
584 584
         $items = array();
585 585
 
586
-        foreach ( $invoice->get_items() as $item_id => $item ) {
587
-            $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax(  $invoice->get_currency()  );
586
+        foreach ($invoice->get_items() as $item_id => $item) {
587
+            $items[$item_id] = $item->prepare_data_for_invoice_edit_ajax($invoice->get_currency());
588 588
         }
589 589
 
590
-        wp_send_json_success( compact( 'items' ) );
590
+        wp_send_json_success(compact('items'));
591 591
     }
592 592
 
593 593
     /**
@@ -596,39 +596,39 @@  discard block
 block discarded – undo
596 596
     public static function add_invoice_items() {
597 597
 
598 598
         // Verify nonce.
599
-        check_ajax_referer( 'wpinv-nonce' );
599
+        check_ajax_referer('wpinv-nonce');
600 600
 
601
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
601
+        if (!wpinv_current_user_can_manage_invoicing()) {
602 602
             exit;
603 603
         }
604 604
 
605 605
         // We need an invoice and items.
606
-        if ( empty( $_POST['post_id'] ) || empty( $_POST['items'] ) ) {
606
+        if (empty($_POST['post_id']) || empty($_POST['items'])) {
607 607
             exit;
608 608
         }
609 609
 
610 610
         // Fetch the invoice.
611
-        $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) );
611
+        $invoice = new WPInv_Invoice(trim($_POST['post_id']));
612 612
         $alert   = false;
613 613
 
614 614
         // Ensure it exists and its not been paid for.
615
-        if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) {
615
+        if (!$invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded()) {
616 616
             exit;
617 617
         }
618 618
 
619 619
         // Add the items.
620
-        foreach ( $_POST['items'] as $data ) {
620
+        foreach ($_POST['items'] as $data) {
621 621
 
622
-            $item = new GetPaid_Form_Item( $data[ 'id' ] );
622
+            $item = new GetPaid_Form_Item($data['id']);
623 623
 
624
-            if ( is_numeric( $data[ 'qty' ] ) && (float) $data[ 'qty' ] > 0 ) {
625
-                $item->set_quantity( $data[ 'qty' ] );
624
+            if (is_numeric($data['qty']) && (float) $data['qty'] > 0) {
625
+                $item->set_quantity($data['qty']);
626 626
             }
627 627
 
628
-            if ( $item->get_id() > 0 ) {
629
-                $error = $invoice->add_item( $item );
628
+            if ($item->get_id() > 0) {
629
+                $error = $invoice->add_item($item);
630 630
 
631
-                if ( is_wp_error( $error ) ) {
631
+                if (is_wp_error($error)) {
632 632
                     $alert = $error->get_error_message();
633 633
                 }
634 634
 
@@ -643,11 +643,11 @@  discard block
 block discarded – undo
643 643
         // Return an array of invoice items.
644 644
         $items = array();
645 645
 
646
-        foreach ( $invoice->get_items() as $item_id => $item ) {
647
-            $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() );
646
+        foreach ($invoice->get_items() as $item_id => $item) {
647
+            $items[$item_id] = $item->prepare_data_for_invoice_edit_ajax($invoice->get_currency());
648 648
         }
649 649
 
650
-        wp_send_json_success( compact( 'items', 'alert' ) );
650
+        wp_send_json_success(compact('items', 'alert'));
651 651
     }
652 652
 
653 653
     /**
@@ -656,15 +656,15 @@  discard block
 block discarded – undo
656 656
     public static function get_invoicing_items() {
657 657
 
658 658
         // Verify nonce.
659
-        check_ajax_referer( 'wpinv-nonce' );
659
+        check_ajax_referer('wpinv-nonce');
660 660
 
661
-        if ( ! wpinv_current_user_can_manage_invoicing() ) {
661
+        if (!wpinv_current_user_can_manage_invoicing()) {
662 662
             exit;
663 663
         }
664 664
 
665 665
         // We need a search term.
666
-        if ( empty( $_GET['search'] ) ) {
667
-            wp_send_json_success( array() );
666
+        if (empty($_GET['search'])) {
667
+            wp_send_json_success(array());
668 668
         }
669 669
 
670 670
         // Retrieve items.
@@ -673,8 +673,8 @@  discard block
 block discarded – undo
673 673
             'orderby'        => 'title',
674 674
             'order'          => 'ASC',
675 675
             'posts_per_page' => -1,
676
-            'post_status'    => array( 'publish' ),
677
-            's'              => trim( $_GET['search'] ),
676
+            'post_status'    => array('publish'),
677
+            's'              => trim($_GET['search']),
678 678
             'meta_query'     => array(
679 679
                 array(
680 680
                     'key'       => '_wpinv_type',
@@ -684,22 +684,22 @@  discard block
 block discarded – undo
684 684
             )
685 685
         );
686 686
 
687
-        $items = get_posts( apply_filters( 'getpaid_ajax_invoice_items_query_args', $item_args ) );
687
+        $items = get_posts(apply_filters('getpaid_ajax_invoice_items_query_args', $item_args));
688 688
         $data  = array();
689 689
 
690 690
 
691
-        $is_payment_form = ( ! empty( $_GET['post_id'] ) && 'wpi_payment_form' == get_post_type( $_GET['post_id'] ) );
691
+        $is_payment_form = (!empty($_GET['post_id']) && 'wpi_payment_form' == get_post_type($_GET['post_id']));
692 692
 
693
-        foreach ( $items as $item ) {
694
-            $item      = new GetPaid_Form_Item( $item );
693
+        foreach ($items as $item) {
694
+            $item = new GetPaid_Form_Item($item);
695 695
             $data[] = array(
696 696
                 'id'        => (int) $item->get_id(),
697
-                'text'      => strip_tags( $item->get_name() ),
698
-                'form_data' => $is_payment_form ? $item->prepare_data_for_use( false ) : '',
697
+                'text'      => strip_tags($item->get_name()),
698
+                'form_data' => $is_payment_form ? $item->prepare_data_for_use(false) : '',
699 699
             );
700 700
         }
701 701
 
702
-        wp_send_json_success( $data );
702
+        wp_send_json_success($data);
703 703
 
704 704
     }
705 705
 
@@ -709,28 +709,28 @@  discard block
 block discarded – undo
709 709
     public static function get_aui_states_field() {
710 710
 
711 711
         // Verify nonce.
712
-        check_ajax_referer( 'wpinv-nonce' );
712
+        check_ajax_referer('wpinv-nonce');
713 713
 
714 714
         // We need a country.
715
-        if ( empty( $_GET['country'] ) ) {
715
+        if (empty($_GET['country'])) {
716 716
             exit;
717 717
         }
718 718
 
719
-        $states = wpinv_get_country_states( sanitize_text_field( $_GET['country'] ) );
720
-        $state  = isset( $_GET['state'] ) ? sanitize_text_field( $_GET['state'] ) : wpinv_get_default_state();
721
-        $name   = isset( $_GET['name'] ) ? sanitize_text_field( $_GET['name'] ) : 'wpinv_state';
722
-        $class  = isset( $_GET['class'] ) ? sanitize_text_field( $_GET['class'] ) : 'form-control-sm';
719
+        $states = wpinv_get_country_states(sanitize_text_field($_GET['country']));
720
+        $state  = isset($_GET['state']) ? sanitize_text_field($_GET['state']) : wpinv_get_default_state();
721
+        $name   = isset($_GET['name']) ? sanitize_text_field($_GET['name']) : 'wpinv_state';
722
+        $class  = isset($_GET['class']) ? sanitize_text_field($_GET['class']) : 'form-control-sm';
723 723
 
724
-        if ( empty( $states ) ) {
724
+        if (empty($states)) {
725 725
 
726 726
             $html = aui()->input(
727 727
                 array(
728 728
                     'type'        => 'text',
729 729
                     'id'          => 'wpinv_state',
730 730
                     'name'        => $name,
731
-                    'label'       => __( 'State', 'invoicing' ),
731
+                    'label'       => __('State', 'invoicing'),
732 732
                     'label_type'  => 'vertical',
733
-                    'placeholder' => __( 'State', 'invoicing' ),
733
+                    'placeholder' => __('State', 'invoicing'),
734 734
                     'class'       => $class,
735 735
                     'value'       => $state,
736 736
                 )
@@ -742,9 +742,9 @@  discard block
 block discarded – undo
742 742
                 array(
743 743
                     'id'          => 'wpinv_state',
744 744
                     'name'        => $name,
745
-                    'label'       => __( 'State', 'invoicing' ),
745
+                    'label'       => __('State', 'invoicing'),
746 746
                     'label_type'  => 'vertical',
747
-                    'placeholder' => __( 'Select a state', 'invoicing' ),
747
+                    'placeholder' => __('Select a state', 'invoicing'),
748 748
                     'class'       => $class,
749 749
                     'value'       => $state,
750 750
                     'options'     => $states,
@@ -758,7 +758,7 @@  discard block
 block discarded – undo
758 758
         wp_send_json_success(
759 759
             array(
760 760
                 'html'   => $html,
761
-                'select' => ! empty ( $states )
761
+                'select' => !empty ($states)
762 762
             )
763 763
         );
764 764
 
@@ -772,11 +772,11 @@  discard block
 block discarded – undo
772 772
     public static function payment_form_refresh_prices() {
773 773
 
774 774
         // Check nonce.
775
-        check_ajax_referer( 'getpaid_form_nonce' );
775
+        check_ajax_referer('getpaid_form_nonce');
776 776
 
777 777
         // ... form fields...
778
-        if ( empty( $_POST['getpaid_payment_form_submission'] ) ) {
779
-            _e( 'Error: Reload the page and try again.', 'invoicing' );
778
+        if (empty($_POST['getpaid_payment_form_submission'])) {
779
+            _e('Error: Reload the page and try again.', 'invoicing');
780 780
             exit;
781 781
         }
782 782
 
@@ -784,18 +784,18 @@  discard block
 block discarded – undo
784 784
         $submission = new GetPaid_Payment_Form_Submission();
785 785
 
786 786
         // Do we have an error?
787
-        if ( ! empty( $submission->last_error ) ) {
787
+        if (!empty($submission->last_error)) {
788 788
             echo $submission->last_error;
789 789
             exit;
790 790
         }
791 791
 
792 792
         // Prepare the response.
793
-        $response = new GetPaid_Payment_Form_Submission_Refresh_Prices( $submission );
793
+        $response = new GetPaid_Payment_Form_Submission_Refresh_Prices($submission);
794 794
         
795 795
         // Filter the response.
796
-        $response = apply_filters( 'getpaid_payment_form_ajax_refresh_prices', $response->response, $submission );
796
+        $response = apply_filters('getpaid_payment_form_ajax_refresh_prices', $response->response, $submission);
797 797
 
798
-        wp_send_json_success( $response );
798
+        wp_send_json_success($response);
799 799
     }
800 800
 
801 801
 }
Please login to merge, or discard this patch.