Total Complexity | 128 |
Total Lines | 825 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like WPInv_Ajax often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPInv_Ajax, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WPInv_Ajax { |
||
15 | |||
16 | /** |
||
17 | * Hook in ajax handlers. |
||
18 | */ |
||
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 ); |
||
22 | self::add_ajax_events(); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Set GetPaid AJAX constant and headers. |
||
27 | */ |
||
28 | public static function define_ajax() { |
||
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 ); |
||
35 | } |
||
36 | $GLOBALS['wpdb']->hide_errors(); |
||
37 | } |
||
38 | |||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Send headers for GetPaid Ajax Requests. |
||
43 | * |
||
44 | * @since 1.0.18 |
||
45 | */ |
||
46 | private static function wpinv_ajax_headers() { |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Check for GetPaid Ajax request and fire action. |
||
59 | */ |
||
60 | public static function do_wpinv_ajax() { |
||
74 | } |
||
75 | |||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Hook in ajax methods. |
||
80 | */ |
||
81 | public static function add_ajax_events() { |
||
82 | |||
83 | // array( 'event' => is_frontend ) |
||
84 | $ajax_events = array( |
||
85 | 'add_note' => false, |
||
86 | 'delete_note' => false, |
||
87 | 'get_states_field' => true, |
||
88 | 'get_aui_states_field' => true, |
||
89 | 'payment_form' => true, |
||
90 | 'get_payment_form' => true, |
||
91 | 'get_payment_form_states_field' => true, |
||
92 | 'get_invoicing_items' => false, |
||
93 | 'get_customers' => false, |
||
94 | 'get_invoice_items' => false, |
||
95 | 'add_invoice_items' => false, |
||
96 | 'edit_invoice_item' => false, |
||
97 | 'remove_invoice_item' => false, |
||
98 | 'get_billing_details' => false, |
||
99 | 'recalculate_invoice_totals' => false, |
||
100 | 'check_new_user_email' => false, |
||
101 | 'run_tool' => false, |
||
102 | 'payment_form_refresh_prices' => true, |
||
103 | ); |
||
104 | |||
105 | foreach ( $ajax_events as $ajax_event => $nopriv ) { |
||
106 | add_action( 'wp_ajax_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
107 | add_action( 'wp_ajax_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
108 | |||
109 | if ( $nopriv ) { |
||
110 | add_action( 'wp_ajax_nopriv_wpinv_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
111 | add_action( 'wp_ajax_nopriv_getpaid_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
112 | add_action( 'wpinv_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) ); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | public static function add_note() { |
||
139 | } |
||
140 | |||
141 | public static function delete_note() { |
||
155 | } |
||
156 | |||
157 | public static function get_states_field() { |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Retrieves a given user's billing address. |
||
165 | */ |
||
166 | public static function get_billing_details() { |
||
167 | |||
168 | // Verify nonce. |
||
169 | check_ajax_referer( 'wpinv-nonce' ); |
||
170 | |||
171 | // Can the user manage the plugin? |
||
172 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
173 | die(-1); |
||
174 | } |
||
175 | |||
176 | // Do we have a user id? |
||
177 | $user_id = $_GET['user_id']; |
||
178 | |||
179 | if ( empty( $user_id ) || ! is_numeric( $user_id ) ) { |
||
180 | die(-1); |
||
181 | } |
||
182 | |||
183 | // Fetch the billing details. |
||
184 | $billing_details = wpinv_get_user_address( $user_id ); |
||
185 | $billing_details = apply_filters( 'wpinv_ajax_billing_details', $billing_details, $user_id ); |
||
186 | |||
187 | // unset the user id and email. |
||
188 | $to_ignore = array( 'user_id', 'email' ); |
||
189 | |||
190 | foreach ( $to_ignore as $key ) { |
||
191 | if ( isset( $billing_details[ $key ] ) ) { |
||
192 | unset( $billing_details[ $key ] ); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | wp_send_json_success( $billing_details ); |
||
197 | |||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Checks if a new users email is valid. |
||
202 | */ |
||
203 | public static function check_new_user_email() { |
||
204 | |||
205 | // Verify nonce. |
||
206 | check_ajax_referer( 'wpinv-nonce' ); |
||
207 | |||
208 | // Can the user manage the plugin? |
||
209 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
210 | die(-1); |
||
211 | } |
||
212 | |||
213 | // We need an email address. |
||
214 | if ( empty( $_GET['email'] ) ) { |
||
215 | _e( "Provide the new user's email address", 'invoicing' ); |
||
216 | exit; |
||
217 | } |
||
218 | |||
219 | // Ensure the email is valid. |
||
220 | $email = sanitize_text_field( $_GET['email'] ); |
||
221 | if ( ! is_email( $email ) ) { |
||
222 | _e( 'Invalid email address', 'invoicing' ); |
||
223 | exit; |
||
224 | } |
||
225 | |||
226 | // And it does not exist. |
||
227 | if ( email_exists( $email ) ) { |
||
228 | _e( 'A user with this email address already exists', 'invoicing' ); |
||
229 | exit; |
||
230 | } |
||
231 | |||
232 | wp_send_json_success( true ); |
||
233 | } |
||
234 | |||
235 | public static function run_tool() { |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Retrieves the markup for a payment form. |
||
252 | */ |
||
253 | public static function get_payment_form() { |
||
254 | |||
255 | // Check nonce. |
||
256 | check_ajax_referer( 'getpaid_form_nonce' ); |
||
257 | |||
258 | // Is the request set up correctly? |
||
259 | if ( empty( $_GET['form'] ) && empty( $_GET['item'] ) ) { |
||
260 | echo aui()->alert( |
||
261 | array( |
||
262 | 'type' => 'warning', |
||
263 | 'content' => __( 'No payment form or item provided', 'invoicing' ), |
||
264 | ) |
||
265 | ); |
||
266 | exit; |
||
267 | } |
||
268 | |||
269 | // Payment form or button? |
||
270 | if ( ! empty( $_GET['form'] ) ) { |
||
271 | getpaid_display_payment_form( urldecode( $_GET['form'] ) ); |
||
272 | } else if( ! empty( $_GET['invoice'] ) ) { |
||
273 | getpaid_display_invoice_payment_form( urldecode( $_GET['invoice'] ) ); |
||
274 | } else { |
||
275 | $items = getpaid_convert_items_to_array( urldecode( $_GET['item'] ) ); |
||
276 | getpaid_display_item_payment_form( $items ); |
||
277 | } |
||
278 | |||
279 | exit; |
||
280 | |||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Payment forms. |
||
285 | * |
||
286 | * @since 1.0.18 |
||
287 | */ |
||
288 | public static function payment_form() { |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Payment forms. |
||
309 | * |
||
310 | * @since 1.0.18 |
||
311 | */ |
||
312 | public static function get_payment_form_states_field() { |
||
313 | |||
314 | if ( empty( $_GET['country'] ) || empty( $_GET['form'] ) ) { |
||
315 | exit; |
||
316 | } |
||
317 | |||
318 | $elements = getpaid_get_payment_form_elements( $_GET['form'] ); |
||
319 | |||
320 | if ( empty( $elements ) ) { |
||
321 | exit; |
||
322 | } |
||
323 | |||
324 | $address_fields = array(); |
||
325 | foreach ( $elements as $element ) { |
||
326 | if ( 'address' === $element['type'] ) { |
||
327 | $address_fields = $element; |
||
328 | break; |
||
329 | } |
||
330 | } |
||
331 | |||
332 | if ( empty( $address_fields ) ) { |
||
333 | exit; |
||
334 | } |
||
335 | |||
336 | foreach ( $address_fields['fields'] as $address_field ) { |
||
337 | |||
338 | if ( 'wpinv_state' == $address_field['name'] ) { |
||
339 | |||
340 | $wrap_class = getpaid_get_form_element_grid_class( $address_field ); |
||
341 | $wrap_class = esc_attr( "$wrap_class getpaid-address-field-wrapper" ); |
||
342 | $placeholder = empty( $address_field['placeholder'] ) ? '' : esc_attr( $address_field['placeholder'] ); |
||
343 | $description = empty( $address_field['description'] ) ? '' : wp_kses_post( $address_field['description'] ); |
||
344 | $value = is_user_logged_in() ? get_user_meta( get_current_user_id(), '_wpinv_state', true ) : ''; |
||
345 | $label = empty( $address_field['label'] ) ? '' : wp_kses_post( $address_field['label'] ); |
||
346 | |||
347 | if ( ! empty( $address_field['required'] ) ) { |
||
348 | $label .= "<span class='text-danger'> *</span>"; |
||
349 | } |
||
350 | |||
351 | $html = getpaid_get_states_select_markup ( |
||
352 | sanitize_text_field( $_GET['country'] ), |
||
353 | $value, |
||
354 | $placeholder, |
||
355 | $label, |
||
356 | $description, |
||
357 | ! empty( $address_field['required'] ), |
||
358 | $wrap_class, |
||
359 | wpinv_clean( $_GET['name'] ) |
||
360 | ); |
||
361 | |||
362 | wp_send_json_success( $html ); |
||
363 | exit; |
||
364 | |||
365 | } |
||
366 | |||
367 | } |
||
368 | |||
369 | exit; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Recalculates invoice totals. |
||
374 | */ |
||
375 | public static function recalculate_invoice_totals() { |
||
376 | |||
377 | // Verify nonce. |
||
378 | check_ajax_referer( 'wpinv-nonce' ); |
||
379 | |||
380 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
381 | exit; |
||
382 | } |
||
383 | |||
384 | // We need an invoice. |
||
385 | if ( empty( $_POST['post_id'] ) ) { |
||
386 | exit; |
||
387 | } |
||
388 | |||
389 | // Fetch the invoice. |
||
390 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
391 | |||
392 | // Ensure it exists. |
||
393 | if ( ! $invoice->get_id() ) { |
||
394 | exit; |
||
395 | } |
||
396 | |||
397 | // Maybe set the country, state, currency. |
||
398 | foreach ( array( 'country', 'state', 'currency', 'vat_number', 'discount_code' ) as $key ) { |
||
399 | if ( isset( $_POST[ $key ] ) ) { |
||
400 | $method = "set_$key"; |
||
401 | $invoice->$method( sanitize_text_field( $_POST[ $key ] ) ); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | // Maybe disable taxes. |
||
406 | $invoice->set_disable_taxes( ! empty( $_POST['taxes'] ) ); |
||
407 | |||
408 | // Discount code. |
||
409 | if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
||
410 | $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
||
411 | if ( $discount->exists() ) { |
||
412 | $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
||
413 | } else { |
||
414 | $invoice->remove_discount( 'discount_code' ); |
||
415 | } |
||
416 | } |
||
417 | |||
418 | // Recalculate totals. |
||
419 | $invoice->recalculate_total(); |
||
420 | |||
421 | $total = wpinv_price( $invoice->get_total(), $invoice->get_currency() ); |
||
422 | |||
423 | if ( $invoice->is_recurring() && $invoice->is_parent() && $invoice->get_total() != $invoice->get_recurring_total() ) { |
||
424 | $recurring_total = wpinv_price( $invoice->get_recurring_total(), $invoice->get_currency() ); |
||
425 | $total .= '<small class="form-text text-muted">' . sprintf( __( 'Recurring Price: %s', 'invoicing' ), $recurring_total ) . '</small>'; |
||
426 | } |
||
427 | |||
428 | $totals = array( |
||
429 | 'subtotal' => wpinv_price( $invoice->get_subtotal(), $invoice->get_currency() ), |
||
430 | 'discount' => wpinv_price( $invoice->get_total_discount(), $invoice->get_currency() ), |
||
431 | 'tax' => wpinv_price( $invoice->get_total_tax(), $invoice->get_currency() ), |
||
432 | 'total' => $total, |
||
433 | ); |
||
434 | |||
435 | $totals = apply_filters( 'getpaid_invoice_totals', $totals, $invoice ); |
||
436 | |||
437 | wp_send_json_success( compact( 'totals' ) ); |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Get items belonging to a given invoice. |
||
442 | */ |
||
443 | public static function get_invoice_items() { |
||
444 | |||
445 | // Verify nonce. |
||
446 | check_ajax_referer( 'wpinv-nonce' ); |
||
447 | |||
448 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
449 | exit; |
||
450 | } |
||
451 | |||
452 | // We need an invoice and items. |
||
453 | if ( empty( $_POST['post_id'] ) ) { |
||
454 | exit; |
||
455 | } |
||
456 | |||
457 | // Fetch the invoice. |
||
458 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
459 | |||
460 | // Ensure it exists. |
||
461 | if ( ! $invoice->get_id() ) { |
||
462 | exit; |
||
463 | } |
||
464 | |||
465 | // Return an array of invoice items. |
||
466 | $items = array(); |
||
467 | |||
468 | foreach ( $invoice->get_items() as $item_id => $item ) { |
||
469 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency(), $invoice->is_renewal() ); |
||
470 | } |
||
471 | |||
472 | wp_send_json_success( compact( 'items' ) ); |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Edits an invoice item. |
||
477 | */ |
||
478 | public static function edit_invoice_item() { |
||
479 | |||
480 | // Verify nonce. |
||
481 | check_ajax_referer( 'wpinv-nonce' ); |
||
482 | |||
483 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
484 | exit; |
||
485 | } |
||
486 | |||
487 | // We need an invoice and item details. |
||
488 | if ( empty( $_POST['post_id'] ) || empty( $_POST['data'] ) ) { |
||
489 | exit; |
||
490 | } |
||
491 | |||
492 | // Fetch the invoice. |
||
493 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
494 | |||
495 | // Ensure it exists and its not been paid for. |
||
496 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
497 | exit; |
||
498 | } |
||
499 | |||
500 | // Format the data. |
||
501 | $data = wp_unslash( wp_list_pluck( $_POST['data'], 'value', 'field' ) ); |
||
502 | |||
503 | // Ensure that we have an item id. |
||
504 | if ( empty( $data['id'] ) ) { |
||
505 | exit; |
||
506 | } |
||
507 | |||
508 | // Abort if the invoice does not have the specified item. |
||
509 | $item = $invoice->get_item( (int) $data['id'] ); |
||
510 | |||
511 | if ( empty( $item ) ) { |
||
512 | exit; |
||
513 | } |
||
514 | |||
515 | // Update the item. |
||
516 | $item->set_price( floatval( $data['price'] ) ); |
||
517 | $item->set_name( sanitize_text_field( $data['name'] ) ); |
||
518 | $item->set_description( wp_kses_post( $data['description'] ) ); |
||
519 | $item->set_quantity( intval( $data['quantity'] ) ); |
||
520 | |||
521 | // Add it to the invoice. |
||
522 | $error = $invoice->add_item( $item ); |
||
523 | $alert = false; |
||
524 | if ( is_wp_error( $error ) ) { |
||
525 | $alert = $error->get_error_message(); |
||
526 | } |
||
527 | |||
528 | // Update totals. |
||
529 | $invoice->recalculate_total(); |
||
530 | |||
531 | // Save the invoice. |
||
532 | $invoice->save(); |
||
533 | |||
534 | // Return an array of invoice items. |
||
535 | $items = array(); |
||
536 | |||
537 | foreach ( $invoice->get_items() as $item_id => $item ) { |
||
538 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
539 | } |
||
540 | |||
541 | wp_send_json_success( compact( 'items', 'alert' ) ); |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Deletes an invoice item. |
||
546 | */ |
||
547 | public static function remove_invoice_item() { |
||
548 | |||
549 | // Verify nonce. |
||
550 | check_ajax_referer( 'wpinv-nonce' ); |
||
551 | |||
552 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
553 | exit; |
||
554 | } |
||
555 | |||
556 | // We need an invoice and an item. |
||
557 | if ( empty( $_POST['post_id'] ) || empty( $_POST['item_id'] ) ) { |
||
558 | exit; |
||
559 | } |
||
560 | |||
561 | // Fetch the invoice. |
||
562 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
563 | |||
564 | // Ensure it exists and its not been paid for. |
||
565 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
566 | exit; |
||
567 | } |
||
568 | |||
569 | // Abort if the invoice does not have the specified item. |
||
570 | $item = $invoice->get_item( (int) $_POST['item_id'] ); |
||
571 | |||
572 | if ( empty( $item ) ) { |
||
573 | exit; |
||
574 | } |
||
575 | |||
576 | $invoice->remove_item( (int) $_POST['item_id'] ); |
||
577 | |||
578 | // Update totals. |
||
579 | $invoice->recalculate_total(); |
||
580 | |||
581 | // Save the invoice. |
||
582 | $invoice->save(); |
||
583 | |||
584 | // Return an array of invoice items. |
||
585 | $items = array(); |
||
586 | |||
587 | foreach ( $invoice->get_items() as $item_id => $item ) { |
||
588 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
589 | } |
||
590 | |||
591 | wp_send_json_success( compact( 'items' ) ); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * Adds a items to an invoice. |
||
596 | */ |
||
597 | public static function add_invoice_items() { |
||
598 | |||
599 | // Verify nonce. |
||
600 | check_ajax_referer( 'wpinv-nonce' ); |
||
601 | |||
602 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
603 | exit; |
||
604 | } |
||
605 | |||
606 | // We need an invoice and items. |
||
607 | if ( empty( $_POST['post_id'] ) || empty( $_POST['items'] ) ) { |
||
608 | exit; |
||
609 | } |
||
610 | |||
611 | // Fetch the invoice. |
||
612 | $invoice = new WPInv_Invoice( trim( $_POST['post_id'] ) ); |
||
613 | $alert = false; |
||
614 | |||
615 | // Ensure it exists and its not been paid for. |
||
616 | if ( ! $invoice->get_id() || $invoice->is_paid() || $invoice->is_refunded() ) { |
||
617 | exit; |
||
618 | } |
||
619 | |||
620 | // Add the items. |
||
621 | foreach ( $_POST['items'] as $data ) { |
||
622 | |||
623 | $item = new GetPaid_Form_Item( $data[ 'id' ] ); |
||
624 | |||
625 | if ( is_numeric( $data[ 'qty' ] ) && (float) $data[ 'qty' ] > 0 ) { |
||
626 | $item->set_quantity( $data[ 'qty' ] ); |
||
627 | } |
||
628 | |||
629 | if ( $item->get_id() > 0 ) { |
||
630 | $error = $invoice->add_item( $item ); |
||
631 | |||
632 | if ( is_wp_error( $error ) ) { |
||
633 | $alert = $error->get_error_message(); |
||
634 | } |
||
635 | |||
636 | } |
||
637 | |||
638 | } |
||
639 | |||
640 | // Save the invoice. |
||
641 | $invoice->recalculate_total(); |
||
642 | $invoice->save(); |
||
643 | |||
644 | // Return an array of invoice items. |
||
645 | $items = array(); |
||
646 | |||
647 | foreach ( $invoice->get_items() as $item_id => $item ) { |
||
648 | $items[ $item_id ] = $item->prepare_data_for_invoice_edit_ajax( $invoice->get_currency() ); |
||
649 | } |
||
650 | |||
651 | wp_send_json_success( compact( 'items', 'alert' ) ); |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * Retrieves items that should be added to an invoice. |
||
656 | */ |
||
657 | public static function get_invoicing_items() { |
||
658 | |||
659 | // Verify nonce. |
||
660 | check_ajax_referer( 'wpinv-nonce' ); |
||
661 | |||
662 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
663 | exit; |
||
664 | } |
||
665 | |||
666 | // We need a search term. |
||
667 | if ( empty( $_GET['search'] ) ) { |
||
668 | wp_send_json_success( array() ); |
||
669 | } |
||
670 | |||
671 | // Retrieve items. |
||
672 | $item_args = array( |
||
673 | 'post_type' => 'wpi_item', |
||
674 | 'orderby' => 'title', |
||
675 | 'order' => 'ASC', |
||
676 | 'posts_per_page' => -1, |
||
677 | 'post_status' => array( 'publish' ), |
||
678 | 's' => trim( $_GET['search'] ), |
||
679 | 'meta_query' => array( |
||
680 | array( |
||
681 | 'key' => '_wpinv_type', |
||
682 | 'compare' => '!=', |
||
683 | 'value' => 'package' |
||
684 | ) |
||
685 | ) |
||
686 | ); |
||
687 | |||
688 | $items = get_posts( apply_filters( 'getpaid_ajax_invoice_items_query_args', $item_args ) ); |
||
689 | $data = array(); |
||
690 | |||
691 | |||
692 | $is_payment_form = ( ! empty( $_GET['post_id'] ) && 'wpi_payment_form' == get_post_type( $_GET['post_id'] ) ); |
||
693 | |||
694 | foreach ( $items as $item ) { |
||
695 | $item = new GetPaid_Form_Item( $item ); |
||
696 | $data[] = array( |
||
697 | 'id' => (int) $item->get_id(), |
||
698 | 'text' => strip_tags( $item->get_name() ), |
||
699 | 'form_data' => $is_payment_form ? $item->prepare_data_for_use( false ) : '', |
||
700 | ); |
||
701 | } |
||
702 | |||
703 | wp_send_json_success( $data ); |
||
704 | |||
705 | } |
||
706 | |||
707 | /** |
||
708 | * Retrieves items that should be added to an invoice. |
||
709 | */ |
||
710 | public static function get_customers() { |
||
711 | |||
712 | // Verify nonce. |
||
713 | check_ajax_referer( 'wpinv-nonce' ); |
||
714 | |||
715 | if ( ! wpinv_current_user_can_manage_invoicing() ) { |
||
716 | exit; |
||
717 | } |
||
718 | |||
719 | // We need a search term. |
||
720 | if ( empty( $_GET['search'] ) ) { |
||
721 | wp_send_json_success( array() ); |
||
722 | } |
||
723 | |||
724 | // Retrieve customers. |
||
725 | |||
726 | $customer_args = array( |
||
727 | 'fields' => array( 'ID', 'user_email', 'display_name' ), |
||
728 | 'orderby' => 'display_name', |
||
729 | 'search' => sanitize_text_field( $_GET['search'] ), |
||
730 | ); |
||
731 | |||
732 | $customers = get_users( apply_filters( 'getpaid_ajax_invoice_customers_query_args', $customer_args ) ); |
||
733 | $data = array(); |
||
734 | |||
735 | foreach ( $customers as $customer ) { |
||
736 | $data[] = array( |
||
737 | 'id' => (int) $customer->ID, |
||
738 | 'text' => strip_tags( sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ) ), |
||
739 | ); |
||
740 | } |
||
741 | |||
742 | wp_send_json_success( $data ); |
||
743 | |||
744 | } |
||
745 | |||
746 | /** |
||
747 | * Retrieves the states field for AUI forms. |
||
748 | */ |
||
749 | public static function get_aui_states_field() { |
||
750 | |||
751 | // Verify nonce. |
||
752 | check_ajax_referer( 'wpinv-nonce' ); |
||
753 | |||
754 | // We need a country. |
||
755 | if ( empty( $_GET['country'] ) ) { |
||
756 | exit; |
||
757 | } |
||
758 | |||
759 | $states = wpinv_get_country_states( sanitize_text_field( $_GET['country'] ) ); |
||
760 | $state = isset( $_GET['state'] ) ? sanitize_text_field( $_GET['state'] ) : wpinv_get_default_state(); |
||
761 | $name = isset( $_GET['name'] ) ? sanitize_text_field( $_GET['name'] ) : 'wpinv_state'; |
||
762 | $class = isset( $_GET['class'] ) ? sanitize_text_field( $_GET['class'] ) : 'form-control-sm'; |
||
763 | |||
764 | if ( empty( $states ) ) { |
||
765 | |||
766 | $html = aui()->input( |
||
767 | array( |
||
768 | 'type' => 'text', |
||
769 | 'id' => 'wpinv_state', |
||
770 | 'name' => $name, |
||
771 | 'label' => __( 'State', 'invoicing' ), |
||
772 | 'label_type' => 'vertical', |
||
773 | 'placeholder' => __( 'State', 'invoicing' ), |
||
774 | 'class' => $class, |
||
775 | 'value' => $state, |
||
776 | ) |
||
777 | ); |
||
778 | |||
779 | } else { |
||
780 | |||
781 | $html = aui()->select( |
||
782 | array( |
||
783 | 'id' => 'wpinv_state', |
||
784 | 'name' => $name, |
||
785 | 'label' => __( 'State', 'invoicing' ), |
||
786 | 'label_type' => 'vertical', |
||
787 | 'placeholder' => __( 'Select a state', 'invoicing' ), |
||
788 | 'class' => $class, |
||
789 | 'value' => $state, |
||
790 | 'options' => $states, |
||
791 | 'data-allow-clear' => 'false', |
||
792 | 'select2' => true, |
||
793 | ) |
||
794 | ); |
||
795 | |||
796 | } |
||
797 | |||
798 | wp_send_json_success( |
||
799 | array( |
||
800 | 'html' => $html, |
||
801 | 'select' => ! empty ( $states ) |
||
802 | ) |
||
803 | ); |
||
804 | |||
805 | } |
||
806 | |||
807 | /** |
||
808 | * Refresh prices. |
||
809 | * |
||
810 | * @since 1.0.19 |
||
811 | */ |
||
812 | public static function payment_form_refresh_prices() { |
||
839 | } |
||
840 | |||
841 | } |
||
842 | |||
843 | WPInv_Ajax::init(); |