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  * POS Customer Class
  5  * duck punches the WC REST API
  6  *
  7  * @class    WC_POS_API_Customers
  8  * @package  WooCommerce POS
  9  * @author   Paul Kilmurray <paul@kilbot.com.au>
 10  * @link     http://www.woopos.com.au
 11  */
 12 
 13 namespace WC_POS\API;
 14 
 15 use WC_API_Resource;
 16 use WC_API_Server;
 17 use WP_User_Query;
 18 
 19 class Customers extends WC_API_Resource {
 20 
 21   /** @var string $base the route base */
 22   protected $base = '/customers';
 23 
 24 
 25   /**
 26    * @param WC_API_Server $server
 27    */
 28   public function __construct( WC_API_Server $server ) {
 29     parent::__construct( $server );
 30     add_filter( 'woocommerce_api_customer_response', array( $this, 'customer_response' ), 10, 4 );
 31 
 32     if( $server->path === $this->base || $server->path === $this->base . '/ids' ){
 33       add_action( 'pre_get_users', array( $this, 'pre_get_users' ), 5 );
 34       add_action( 'pre_user_query', array( $this, 'pre_user_query' ) );
 35     }
 36   }
 37 
 38 
 39   /**
 40    * Register routes for POS Customers
 41    *
 42    * @param array $routes
 43    * @return array
 44    */
 45   public function register_routes( $routes ) {
 46 
 47     # GET /customers/ids
 48     $routes[ $this->base . '/ids'] = array(
 49       array( array( $this, 'get_all_ids' ), WC_API_Server::READABLE ),
 50     );
 51 
 52     return $routes;
 53   }
 54 
 55 
 56   /**
 57    * - add `updated_at` to customer data
 58    *
 59    * @param $data
 60    * @param $customer
 61    * @param $fields
 62    * @param $server
 63    * @return
 64    */
 65   public function customer_response( $data, $customer, $fields, $server ){
 66     $timestamp = get_user_meta( $customer->ID , '_user_modified_gmt', true);
 67     $data['updated_at'] = $server->format_datetime( $timestamp );
 68     return $data;
 69   }
 70 
 71 
 72   /**
 73    *
 74    * @param $wp_user_query
 75    */
 76   public function pre_get_users( $wp_user_query ) {
 77     global $wp_version;
 78 
 79     $wp_user_query->query_vars[ 'role' ] = '';
 80 
 81     // WordPress 4.4 allows role__in and role__not_in
 82     if ( version_compare( $wp_version, '4.4', '>=' ) ) {
 83       $roles = wc_pos_get_option( 'customers', 'customer_roles' );
 84 
 85       if ( is_array( $roles ) && !in_array( 'all', $roles ) ) {
 86         $wp_user_query->query_vars[ 'role__in' ] = $roles;
 87       }
 88 
 89       // special case: no customer roles
 90       if ( is_null( $roles ) ) {
 91         // ?
 92       }
 93     }
 94 
 95     if ( isset( $_GET[ 'filter' ] ) ) {
 96 
 97       // add support for filter[in]
 98       if ( isset( $_GET[ 'filter' ][ 'in' ] ) ) {
 99         $wp_user_query->query_vars[ 'include' ] = explode( ',', $_GET[ 'filter' ][ 'in' ] );
100       }
101 
102       // add support for filter[not_in]
103       if ( isset( $_GET[ 'filter' ][ 'not_in' ] ) ) {
104         $wp_user_query->query_vars[ 'exclude' ] = explode( ',', $_GET[ 'filter' ][ 'not_in' ] );
105       }
106 
107       // wildcard by default
108       if ( isset( $_GET[ 'filter' ][ 'q' ] ) ) {
109         $query = $_GET[ 'filter' ][ 'q' ];
110         if(is_string($query)){
111           $wp_user_query->query_vars[ 'search' ] = '*' . trim( $_GET[ 'filter' ][ 'q' ], '*' ) . '*';
112         }
113         if(is_array($query)){
114           $wp_user_query->query_vars[ 'search' ] = '';
115           $wp_user_query->query_vars[ '_pos_query' ] = $query;
116         }
117       }
118 
119       // search columns: 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename'
120       if ( isset( $_GET[ 'filter' ][ 'fields' ] ) ) {
121         $fields = $_GET[ 'filter' ][ 'fields' ];
122         $fields = is_string($fields) ? explode(',', $fields) : $fields;
123         $search_columns = array();
124         $translate = array(
125           'id' => 'ID',
126           'email' => 'user_email',
127           'username' => 'user_login'
128         );
129         foreach( $fields as $field ) {
130           $search_columns[] = isset($translate[$field]) ? $translate[$field] : $field;
131         }
132         if(!in_array('user_login', $search_columns)){
133           $search_columns[] = 'user_login'; // required
134         }
135         $wp_user_query->query_vars[ 'search_columns' ] = $search_columns;
136       }
137 
138     }
139 
140   }
141 
142   /**
143    *
144    *
145    * @param $wp_user_query
146    */
147   public function pre_user_query( $wp_user_query ) {
148 
149     if(!isset($wp_user_query->query_vars[ 'search' ]))
150       return;
151 
152     $term = trim( $wp_user_query->query_vars[ 'search' ], '*' );
153 
154     if ( !empty( $term ) ) {
155       $this->simple_search( $term, $wp_user_query );
156     }
157 
158     if( isset($wp_user_query->query_vars[ '_pos_query' ]) ){
159       $queries = $wp_user_query->query_vars[ '_pos_query' ];
160       $this->complex_search( $queries, $wp_user_query );
161     }
162 
163   }
164 
165   /**
166    * Extends customer search, allows more fields
167    *
168    * @param $term
169    * @param $wp_user_query
170    */
171   private function simple_search($term, $wp_user_query) {
172     global $wpdb;
173     $meta_keys = array();
174     $ids = array();
175 
176     foreach ( $wp_user_query->query_vars[ 'search_columns' ] as $field ) {
177       if ( $field == 'first_name' ) $meta_keys[] = "meta_key='$field'";
178       if ( $field == 'last_name' ) $meta_keys[] = "meta_key='$field'";
179       if ( substr( $field, 0, 16 ) == 'billing_address.' ) {
180         $field = str_replace( 'billing_address.', 'billing_', $field );
181         $meta_keys[] = "meta_key='$field'";
182       }
183       if ( substr( $field, 0, 17 ) == 'shipping_address.' ) {
184         $field = str_replace( 'shipping_address.', 'shipping_', $field );
185         $meta_keys[] = "meta_key='$field'";
186       }
187     }
188 
189     // search usermeta table
190     if ( !empty( $meta_keys ) ) {
191       $ids = $wpdb->get_col( "
192         SELECT DISTINCT user_id
193         FROM $wpdb->usermeta
194         WHERE (" . implode( ' OR ', $meta_keys ) . ")
195         AND LOWER(meta_value)
196         LIKE '%" . $term . "%'
197       " );
198     }
199 
200 
201     if ( !empty( $ids ) ) {
202       $wp_user_query->query_where = str_replace(
203         "user_login LIKE '%$term%'",
204         "user_login LIKE '%$term%' OR ID IN(" . implode( ',', $ids ) . ")",
205         $wp_user_query->query_where
206       );
207     }
208   }
209 
210   /**
211    * @param array $queries
212    * @param $wp_user_query
213    */
214   private function complex_search(array $queries, $wp_user_query){
215     $ORs = array();
216 
217     foreach($queries as $query){
218       $type = isset($query['type']) ? $query['type'] : '';
219       $term = isset($query['query']) ? $query['query'] : '';
220       if($type == 'prefix'){
221         $prefix = isset($query['prefix']) ? $query['prefix'] : '';
222         if($prefix == 'id'){
223           $ORs[] = 'ID = ' . $term;
224         }
225       }
226     };
227 
228     if(!empty($ORs)){
229       $wp_user_query->query_where .= ' AND (' . implode(' OR ', $ORs) .') ';
230     }
231 
232   }
233 
234   /**
235    * Returns array of all user ids
236    *
237    * @param array $filter
238    * @return array|void
239    */
240   public function get_all_ids( $filter = array() ){
241     $args = array(
242       'fields' => 'ID',
243       'orderby' => 'ID'
244     );
245 
246     if( isset( $filter['updated_at_min'] ) ){
247       $args['meta_key']      = '_user_modified_gmt';
248       $args['meta_value']    = $this->server->parse_datetime( $filter['updated_at_min'] );
249       $args['meta_compare']  = '>';
250     }
251 
252     $query = new \WP_User_Query( $args );
253     $this->server->add_pagination_headers($query);
254     return array( 'customers' => array_map( array( $this, 'format_id' ), $query->results ) );
255   }
256 
257 
258   /**
259    * @param $id
260    * @return array
261    */
262   private function format_id( $id ) {
263     return array( 'id' => (int) $id );
264   }
265 
266 
267 }
API documentation generated by ApiGen