1 <?php
2
3 4 5 6 7 8 9 10 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
22 protected $base = '/customers';
23
24
25 26 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 41 42 43 44
45 public function register_routes( $routes ) {
46
47
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 58 59 60 61 62 63 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 75
76 public function pre_get_users( $wp_user_query ) {
77 global $wp_version;
78
79 $wp_user_query->query_vars[ 'role' ] = '';
80
81
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
90 if ( is_null( $roles ) ) {
91
92 }
93 }
94
95 if ( isset( $_GET[ 'filter' ] ) ) {
96
97
98 if ( isset( $_GET[ 'filter' ][ 'in' ] ) ) {
99 $wp_user_query->query_vars[ 'include' ] = explode( ',', $_GET[ 'filter' ][ 'in' ] );
100 }
101
102
103 if ( isset( $_GET[ 'filter' ][ 'not_in' ] ) ) {
104 $wp_user_query->query_vars[ 'exclude' ] = explode( ',', $_GET[ 'filter' ][ 'not_in' ] );
105 }
106
107
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
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';
134 }
135 $wp_user_query->query_vars[ 'search_columns' ] = $search_columns;
136 }
137
138 }
139
140 }
141
142 143 144 145 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 167 168 169 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
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 212 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 236 237 238 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 260 261
262 private function format_id( $id ) {
263 return array( 'id' => (int) $id );
264 }
265
266
267 }