WC_GZD_AJAX

AJAX Handler.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-gzd-ajax.php  
  1. class WC_GZD_AJAX { 
  2.  
  3. /** 
  4. * Hook in methods 
  5. */ 
  6. public static function init() { 
  7. $ajax_events = array( 
  8. 'gzd_revocation' => true,  
  9. 'gzd_json_search_delivery_time' => false,  
  10. ); 
  11. foreach ( $ajax_events as $ajax_event => $nopriv ) { 
  12. add_action( 'wp_ajax_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) ); 
  13. if ( $nopriv ) { 
  14. add_action( 'wp_ajax_nopriv_woocommerce_' . $ajax_event, array( __CLASS__, $ajax_event ) ); 
  15.  
  16. public static function gzd_json_search_delivery_time() { 
  17. ob_start(); 
  18.  
  19. check_ajax_referer( 'search-products', 'security' ); 
  20. $term = (string) wc_clean( stripslashes( $_GET['term'] ) ); 
  21. $terms = array(); 
  22.  
  23. if ( empty( $term ) ) 
  24. die(); 
  25.  
  26. $args = array( 
  27. 'hide_empty' => false,  
  28. ); 
  29.  
  30. if ( is_numeric( $term ) ) { 
  31. $args[ 'include' ] = array( absint( $term ) );  
  32. } else { 
  33. $args[ 'name__like' ] = (string) $term; 
  34.  
  35. $query = get_terms( 'product_delivery_time', $args ); 
  36. if ( ! empty( $query ) ) { 
  37. foreach ( $query as $term ) { 
  38. $terms[ $term->term_id ] = rawurldecode( $term->name ); 
  39. } else { 
  40. $terms[ rawurldecode( $term ) ] = rawurldecode( sprintf( __( "%s [new]", "woocommerce-germanized" ), $term ) ); 
  41. wp_send_json( $terms ); 
  42.  
  43. /** 
  44. * Checks revocation form and sends Email to customer and Admin 
  45. */ 
  46. public static function gzd_revocation() { 
  47.  
  48. check_ajax_referer( 'woocommerce-revocation', 'security' ); 
  49. wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-revocation' ); 
  50.  
  51. $data = array(); 
  52. $fields = WC_GZD_Revocation::get_fields(); 
  53.  
  54. if ( ! empty( $fields ) ) { 
  55. foreach ( $fields as $key => $field ) { 
  56.  
  57. if ( 'sep' !== $key ) { 
  58.  
  59. if ( true === $field[ 'required' ] ) { 
  60. if ( $key == 'address_mail' ) { 
  61. if ( ! is_email( $_POST[ $key ] ) ) 
  62. wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . _x( 'is not a valid email address.', 'revocation-form', 'woocommerce-germanized' ), 'error' ); 
  63. } elseif ( $key == 'address_postal' ) { 
  64. if ( ! WC_Validation::is_postcode( $_POST[ $key ], $_POST[ 'address_country' ] ) || empty( $_POST[ $key ] ) ) 
  65. wc_add_notice( _x( 'Please enter a valid postcode/ZIP', 'revocation-form', 'woocommerce-germanized' ), 'error' ); 
  66. } else { 
  67. if ( isset( $field[ 'required' ] ) && empty( $_POST[ $key ] ) ) 
  68. wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . _x( 'is not valid.', 'revocation-form', 'woocommerce-germanized' ), 'error' ); 
  69.  
  70. if ( isset( $_POST[ $key ] ) && ! empty( $_POST[ $key ] ) ) { 
  71. if ( $field['type'] == 'country' ) { 
  72. $countries = WC()->countries->get_countries(); 
  73. $country = wc_clean( $_POST[ $key ] ); 
  74. $data[ $key ] = ( isset( $countries[ $country ] ) ? $countries[ $country ] : '' ); 
  75. } else { 
  76. $data[ $key ] = wc_clean( $_POST[ $key ] ); 
  77.  
  78. $error = false; 
  79. if ( wc_notice_count( 'error' ) == 0 ) { 
  80.  
  81. wc_add_notice( _x( 'Thank you. We have received your Revocation Request. You will receive a conformation email within a few minutes.', 'revocation-form', 'woocommerce-germanized' ), 'success' ); 
  82.  
  83. // Send Mail 
  84. if ( $mail = WC_germanized()->emails->get_email_instance_by_id( 'customer_revocation' ) ) { 
  85.  
  86. $mail->trigger( $data ); 
  87.  
  88. // Send to Admin 
  89. $data[ 'mail' ] = get_bloginfo('admin_email'); 
  90. $mail->trigger( $data ); 
  91.  
  92. } else { 
  93. $error = true; 
  94.  
  95. ob_start(); 
  96. wc_print_notices(); 
  97. $messages = ob_get_clean(); 
  98.  
  99. if ( $error ) { 
  100. echo '<!--WC_START-->' . json_encode( 
  101. array( 
  102. 'result' => 'failure',  
  103. 'messages' => isset( $messages ) ? $messages : '',  
  104. ) . '<!--WC_END-->'; 
  105. } else { 
  106. if ( is_ajax() ) { 
  107. echo '<!--WC_START-->' . json_encode( 
  108. array( 
  109. 'result' => 'success',  
  110. 'messages' => isset ( $messages ) ? $messages : '',  
  111. ) . '<!--WC_END-->'; 
  112. exit(); 
  113.