PluginProbe
ShiftController Employee Shift Scheduling / 4.9.66
ShiftController Employee Shift Scheduling v4.9.66
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / crud / wordpress / user.php

user.php in ShiftController Employee Shift Scheduling 4.9.66, at hc3/crud/wordpress/user.php

312 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 class HC3_Crud_Wordpress_User implements HC3_ICrud
3 {
4 protected $wp_type = NULL;
5 protected $core_fields = array();
6 protected $idField = 'id';
7 protected $withMeta = FALSE;
8
9 protected $fields_to_me = array('ID' => 'id');
10 protected $fields_to_wp = array('id' => 'ID');
11
12 static $read_query_cache = array();
13 protected $read_use_cache = TRUE;
14
15 protected $default_sort = array();
16
17 protected function _convertFromWp( $userdata )
18 {
19 $return = array(
20 'id' => $userdata->ID,
21 'email' => $userdata->user_email,
22 'display_name' => $userdata->display_name,
23 'username' => $userdata->user_login,
24 );
25 return $return;
26 }
27
28 public function withMeta()
29 {
30 $this->withMeta = TRUE;
31 return $this;
32 }
33
34 protected function _convertToWp( $values )
35 {
36 $core = array();
37 $meta = array();
38
39 if( array_key_exists('id', $values) ){
40 $core['ID'] = $values['id'];
41 unset( $values['id'] );
42 }
43 if( array_key_exists('email', $values) ){
44 $core['user_email'] = $values['email'];
45 unset( $values['email'] );
46 }
47 if( array_key_exists('display_name', $values) ){
48 $core['display_name'] = $values['display_name'];
49 unset( $values['display_name'] );
50 }
51 if( array_key_exists('username', $values) ){
52 $core['user_login'] = $values['username'];
53 unset( $values['username'] );
54 }
55
56 if( array_key_exists('role', $values) ){
57 $meta['role'] = $values['role'];
58 unset( $values['role'] );
59 }
60
61 if( $values ){
62 $meta = $meta + $values;
63 }
64
65 $return = array( $core, $meta );
66 return $return;
67 }
68
69 public function set_search_in( $fields = array() ){
70 $this->search_in = $fields;
71 return $this;
72 }
73
74 // READ
75 public function read_prepare_query( $args = array() )
76 {
77 $q = array();
78 $meta_query = array();
79
80 foreach( $args['WHERE'] as $w ){
81 list( $k, $compare, $v ) = $w;
82 if( 'NOTIN' == $compare ){
83 $compare = 'NOT IN';
84 }
85
86 $test = array($k => array($compare, $v));
87 list( $core_where, $meta_where ) = $this->_convertToWp( $test );
88
89 // _print_r( $args['WHERE'] );
90 // echo 'CORE WHERE';
91 // _print_r( $core_where );
92 // echo 'META WHERE';
93 // _print_r( $meta_where );
94 // exit;
95 // don't query main table except ID, post_name, post_title as it's too difficult at the moment
96 if( $core_where ){
97 foreach( $core_where as $k => $more ){
98 list( $compare, $v ) = $more;
99 switch( $k ){
100 case 'ID':
101 switch( $compare ){
102 case '=':
103 case 'IN':
104 if( (! is_array($v)) && (! $v) ){
105 $return = NULL;
106 return $return;
107 }
108 $q['include'] = $v;
109 break;
110
111 case '<>':
112 case 'NOTIN':
113 case 'NOT IN':
114 $q['exclude'] = $v;
115 break;
116 }
117 break;
118 }
119 }
120 }
121
122 if( $meta_where ){
123 foreach( $meta_where as $k => $more ){
124 list( $compare, $v ) = $more;
125
126 switch( $k ){
127 case 'role':
128 switch( $compare ){
129 case '=':
130 case 'IN':
131 $q['role__in'] = is_array($v) ? $v : array($v);
132 break;
133
134 case '<>':
135 case 'NOTIN':
136 case 'NOT IN':
137 $q['role__not_in'] = is_array($v) ? $v : array($v);
138 break;
139 }
140 break;
141
142 default:
143 $meta_query[] = array(
144 'key' => $k,
145 'compare' => $compare,
146 'value' => $v,
147 );
148 break;
149 }
150 }
151 }
152 }
153
154 if( $meta_query ){
155 $meta_query['relation'] = 'AND';
156 }
157 // _print_r( $q );
158 // exit;
159
160 if( $args['SORT'] ){
161 $wp_orderby = array();
162
163 $sort = array();
164 foreach( $args['SORT'] as $s ){
165 $sort[$s[0]] = $s[1];
166 }
167
168 list( $sort_core, $sort_meta ) = $this->_convertToWp( $sort );
169
170 foreach( $sort_core as $k => $ascdesc ){
171 $q['orderby'] = $k;
172 $q['order'] = $ascdesc;
173 }
174
175 if( $sort_meta ){
176 foreach( $sort_meta as $k => $ascdesc ){
177 $q['orderby'] = 'meta_value';
178 $q['order'] = $ascdesc;
179 $q['meta_key'] = $k;
180 }
181 }
182 }
183
184 if( $args['LIMIT'] ){
185 $q['number'] = $args['LIMIT'][0];
186 if( $args['LIMIT'][1] ){
187 $q['offset'] = $args['LIMIT'][1];
188 }
189 }
190 else {
191 // $q['posts_per_page'] = -1;
192 }
193
194 if( $args['SEARCH'] ){
195 $q['search'] = $args['SEARCH'];
196 }
197
198 if( $meta_query ){
199 $q['meta_query'] = $meta_query;
200 }
201
202 return $q;
203 }
204
205 public function count( $args = array() )
206 {
207 $return = NULL;
208
209 if( NULL === $args ){
210 return $return;
211 }
212
213 $args = HC3_Functions::tempCrudPrepareArgs( $args, $this->idField );
214
215 if( $this->read_use_cache ){
216 $cache_key = $this->wp_type . ':count' . json_encode($args);
217 if( array_key_exists($cache_key, self::$read_query_cache) ){
218 // echo "ON CACHE: '$sql'<br>";
219 $return = self::$read_query_cache[$cache_key];
220 return $return;
221 }
222 }
223
224 $q = $this->read_prepare_query( $args );
225
226 if( $q !== NULL ){
227 $q['number'] = 1;
228 $wp_users_query = new WP_User_Query( $q );
229 $return = $wp_users_query->get_total();
230 }
231
232 return $return;
233 }
234
235 public function read( $args = array() )
236 {
237 $return = array();
238
239 if( NULL === $args ){
240 return $return;
241 }
242
243 $args = array_merge( $this->default_sort, $args );
244
245 $args = HC3_Functions::tempCrudPrepareArgs( $args, $this->idField );
246
247 // _print_r( $args );
248
249 if( $this->read_use_cache ){
250 $cache_key = $this->wp_type . ':' . json_encode($args);
251 if( isset(self::$read_query_cache[$cache_key]) ){
252 // echo "ON CACHE: '$cache_key'<br>";
253 $return = self::$read_query_cache[$cache_key];
254 return $return;
255 }
256 }
257
258 // $args = array();
259 $q = $this->read_prepare_query( $args );
260
261 if( $q !== NULL ){
262 // echo 'QUERY<br>';
263 // _print_r( $q );
264 // exit;
265 $wp_users_query = new WP_User_Query( $q );
266 $wp_users = $wp_users_query->get_results();
267
268 $return = array();
269 $count = count($wp_users);
270 for( $ii = 0; $ii < $count; $ii++ ){
271 // $meta = get_metadata( 'post', $posts[$ii]->ID );
272 // $values = array_map( function($n){return $n[0];}, $meta );
273
274 $values = $this->_convertFromWp( $wp_users[$ii] );
275 $key_id = $values['id'];
276 $return[ $key_id ] = $values;
277 }
278 }
279
280 if( $this->read_use_cache ){
281 self::$read_query_cache[$cache_key] = $return;
282 }
283
284 return $return;
285 }
286
287 // CREATE
288 public function create( $values )
289 {
290 }
291
292 // UPDATE
293 public function update( $id, $values )
294 {
295 }
296
297 public function updateMeta( $id, $values )
298 {
299 }
300
301 // DELETE
302 public function delete( $id )
303 {
304 }
305
306 public function deleteAll()
307 {
308 $return = TRUE;
309 return $return;
310 }
311 }
312