WC_POS_AJAX

AJAX Event Handler.

Defined (1)

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

/includes/class-wc-pos-ajax.php  
  1. class WC_POS_AJAX { 
  2.  
  3. /** 
  4. * Hook into ajax events 
  5. */ 
  6. public function __construct() { 
  7.  
  8. $ajax_events = array( 
  9. 'get_all_ids' => 'WC_POS_API',  
  10. 'get_modal' => $this,  
  11. 'get_print_template' => $this,  
  12. // 'set_product_visibilty' => $this,  
  13. 'email_receipt' => $this,  
  14. 'send_support_email' => $this,  
  15. 'test_http_methods' => $this,  
  16. // 'system_status' => $this,  
  17. 'toggle_legacy_server' => 'WC_POS_Status',  
  18. ); 
  19.  
  20. foreach ( $ajax_events as $ajax_event => $class ) { 
  21. // check_ajax_referer 
  22. add_action( 'wp_ajax_wc_pos_' . $ajax_event, array( $this, 'check_ajax_referer' ), 1 ); 
  23. // trigger method 
  24. add_action( 'wp_ajax_wc_pos_' . $ajax_event, array( $class, $ajax_event ) ); 
  25.  
  26.  
  27. public function get_modal() { 
  28.  
  29. if( isset( $_REQUEST['data']) ) 
  30. extract( $_REQUEST['data'] ); 
  31.  
  32. include_once( dirname(__FILE__) . '/views/modals/' . $_REQUEST['template'] . '.php' ); 
  33. die(); 
  34.  
  35. public function get_print_template() { 
  36.  
  37. // check for custom template 
  38. $template_path_theme = '/woocommerce-pos/'; 
  39. $template_path_plugin = WC_POS()->plugin_path. 'public/views/print/'; 
  40.  
  41. wc_get_template( $_REQUEST['template'] . '.php', null, $template_path_theme, $template_path_plugin ); 
  42.  
  43. die(); 
  44.  
  45. /** 
  46. * Update POS visibilty option 
  47. */ 
  48. // public function set_product_visibilty() { 
  49. // 
  50. // if( !isset( $_REQUEST['post_id'] ) ) 
  51. // wp_die('Product ID required'); 
  52. // 
  53. // // security 
  54. // check_ajax_referer( 'set-product-visibilty-'.$_REQUEST['post_id'], 'security' ); 
  55. // 
  56. // // set the post_meta field 
  57. // if( update_post_meta( $_REQUEST['post_id'], '_pos_visibility', $_REQUEST['_pos_visibility'] ) ) { 
  58. // $post_modified = current_time( 'mysql' ); 
  59. // $post_modified_gmt = current_time( 'mysql', 1 ); 
  60. // wp_update_post( array( 
  61. // 'ID' => $_REQUEST['post_id'],  
  62. // 'post_modified' => $post_modified,  
  63. // 'post_modified_gmt' => $post_modified_gmt 
  64. // )); 
  65. // $result = array('success' => true); 
  66. // } 
  67. // else { 
  68. // wp_die('Failed to update post meta table'); 
  69. // } 
  70. // 
  71. // $this->serve_response($result); 
  72. // } 
  73.  
  74. /** 
  75. * Send email receipt 
  76. */ 
  77. public function email_receipt() { 
  78. $order_id = isset($_REQUEST['order_id']) ? $_REQUEST['order_id'] : ''; 
  79. $email = isset($_REQUEST['email']) ? $_REQUEST['email'] : ''; 
  80. $order = wc_get_order( absint( $order_id ) ); 
  81. if( is_object( $order ) ) { 
  82. if( $email != '' ) { 
  83. $order->billing_email = $email; 
  84. WC()->mailer()->customer_invoice( $order ); 
  85. $response = array( 
  86. 'result' => 'success',  
  87. 'message' => __( 'Email sent', 'woocommerce-pos') 
  88. ); 
  89.  
  90. // hook for third party plugins 
  91. do_action( 'woocommerce_pos_email_receipt', $email, $order_id, $order ); 
  92. } else { 
  93. $response = array( 
  94. 'result' => 'error',  
  95. 'message' => __( 'There was an error sending the email', 'woocommerce-pos') 
  96. ); 
  97. WC_POS_Server::response($response); 
  98.  
  99. /** 
  100. */ 
  101. public function send_support_email() { 
  102. $headers[] = 'From: '. $_POST['name'] .' <'. $_POST['email'] .'>'; 
  103. $message = $_POST['message'] . "\n\n" . $_POST['status']; 
  104. $support = apply_filters( 'woocommerce_pos_support_email', 'support@woopos.com.au' ); 
  105. if( wp_mail( $support, 'WooCommerce POS Support', $message, $headers ) ) { 
  106. $response = array( 
  107. 'result' => 'success',  
  108. 'message' => __( 'Email sent', 'woocommerce-pos') 
  109. ); 
  110. } else { 
  111. $response = array( 
  112. 'result' => 'error',  
  113. 'message' => __( 'There was an error sending the email', 'woocommerce-pos') 
  114. ); 
  115.  
  116. WC_POS_Server::response($response); 
  117.  
  118. /** 
  119. * Returns payload for any request for testing 
  120. */ 
  121. public function test_http_methods() { 
  122. WC_POS_Server::response( array( 
  123. 'method' => strtolower($_SERVER['REQUEST_METHOD']),  
  124. 'payload' => WC_POS_Server::get_raw_data() 
  125. )); 
  126.  
  127. /** 
  128. * Returns system status JSON array 
  129. */ 
  130. public function system_status() { 
  131. $status = new WC_POS_Status(); 
  132. WC_POS_Server::response( $status->output() ); 
  133.  
  134. /** 
  135. * Verifies the AJAX request 
  136. */ 
  137. public function check_ajax_referer() { 
  138. $pass = check_ajax_referer( WC_POS_PLUGIN_NAME, 'security', false ); 
  139. if(!$pass) { 
  140. $result = new WP_Error( 
  141. 'woocommerce_pos_invalid_nonce',  
  142. __( 'Invalid security nonce', 'woocommerce-pos' ),  
  143. array( 'status' => 401 ) 
  144. ); 
  145. WC_POS_Server::response($result); 
  146.