Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

  • is_pos
  • is_pos_admin
  • wc_pos_get_option
  • wc_pos_json_encode
  • wc_pos_locate_template
  • wc_pos_trim_html_string
  • wc_pos_update_option
  • wc_pos_url
 1 <?php
 2 
 3 /**
 4  * AJAX Class
 5  *
 6  * @class    WC_POS_Ajax
 7  * @package  WooCommerce POS
 8  * @author   Paul Kilmurray <paul@kilbot.com.au>
 9  * @link     http://www.woopos.com.au
10  */
11 
12 namespace WC_POS;
13 
14 class Ajax {
15 
16   /**
17    * Constructor
18    */
19   public function __construct() {
20 
21     $ajax_events = array(
22       'toggle_legacy_server'  => '\WC_POS\Status'
23     );
24 
25     foreach ( $ajax_events as $ajax_event => $class ) {
26       // check_ajax_referer
27       add_action( 'wp_ajax_wc_pos_' . $ajax_event, array( $this, 'check_ajax_referer' ), 1 );
28       // trigger method
29       add_action( 'wp_ajax_wc_pos_' . $ajax_event, array( $class, $ajax_event ) );
30     }
31   }
32 
33   /**
34    * Verifies the AJAX request
35    */
36   static public function check_ajax_referer(){
37     $pass = check_ajax_referer( PLUGIN_NAME, 'security', false );
38     if(!$pass){
39       $result = new \WP_Error(
40         'woocommerce_pos_invalid_nonce',
41         __( 'Invalid security nonce', 'woocommerce-pos' ),
42         array( 'status' => 401 )
43       );
44       self::response($result);
45     }
46   }
47 
48 
49   /**
50    * The below functions closely resemble output from the WC REST API
51    * This keeps response handling in the POS somewhat consistent
52    * between API and AJAX calls
53    *
54    * Output the result
55    * @param $result
56    */
57   static public function response($result){
58     header( 'Content-Type: application/json; charset=utf-8' );
59     if (is_wp_error($result)) {
60       $data = $result->get_error_data();
61       if ( is_array( $data ) && isset( $data['status'] ) ) {
62         status_header( $data['status'] );
63       }
64       $result = self::error_to_array( $result );
65     }
66     echo json_encode( $result );
67     die();
68   }
69   /**
70    * Convert wp_error to array
71    * @param $error
72    * @return array
73    */
74   static private function error_to_array( $error ) {
75     $errors = array();
76     foreach ( (array) $error->errors as $code => $messages ) {
77       foreach ( (array) $messages as $message ) {
78         $errors[] = array( 'code' => $code, 'message' => $message );
79       }
80     }
81     return array( 'errors' => $errors );
82   }
83 
84 }
API documentation generated by ApiGen