PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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 / custompost.php

custompost.php in ShiftController Employee Shift Scheduling 4.9.26, at hc3/crud/wordpress/custompost.php

509 lines 10.8 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_CustomPost implements HC3_ICrud
3 {
4 protected $wp_type = NULL;
5 protected $core_fields = array();
6 protected $withMeta = FALSE;
7
8 protected $fields_to_me = array('ID' => 'id', 'post_status' => 'status');
9 protected $fields_to_wp = array('id' => 'ID', 'status' => 'post_status');
10
11 protected $idField = 'id';
12
13 static $read_query_cache = array();
14 protected $read_use_cache = TRUE;
15
16 protected $default_sort = array();
17
18 public function __construct( $postType, $convertToMe = array(), $convertToWp = array() )
19 {
20 $this->wp_type = $postType;
21
22 if( ! post_type_exists($this->wp_type) ){
23 // echo "REGISTERING POST TYPE '" . $this->wp_type . "'<br>";
24 register_post_type(
25 $this->wp_type,
26 array(
27 // 'public' => TRUE,
28 'public' => FALSE,
29 'publicly_queryable' => TRUE,
30 'has_archive' => FALSE,
31 'exclude_from_search' => TRUE,
32 'show_in_menu' => FALSE,
33 'show_in_nav_menus' => FALSE,
34 'show_in_rest' => TRUE,
35
36 // 'show_in_menu' => TRUE,
37 // 'show_in_nav_menus' => TRUE,
38 // 'show_in_rest' => TRUE,
39 // 'show_ui' => TRUE,
40 // 'menu_position' => 5,
41 )
42 );
43
44 add_filter( 'user_has_cap', array($this, 'wpAllowEditPost'), 10, 3 );
45 }
46
47 if( $convertToMe ){
48 foreach( $convertToMe as $k => $v ){
49 $this->fields_to_me[ $k ] = $v;
50 }
51 }
52
53 if( $convertToWp ){
54 foreach( $convertToWp as $k => $v ){
55 $this->fields_to_wp[ $k ] = $v;
56 }
57 }
58 }
59
60 public function withMeta()
61 {
62 $this->withMeta = TRUE;
63 return $this;
64 }
65
66 public function wpAllowEditPost( $allcaps, $cap, $args )
67 {
68 if ( 'edit_post' != $args[0] ){
69 return $allcaps;
70 }
71
72 if( ! isset($args[2]) ){
73 return $allcaps;
74 }
75
76 // Load the post data:
77 $post = get_post( $args[2] );
78
79 if( ! ($post && isset($post->post_type)) ){
80 return $allcaps;
81 }
82
83 if( $post->post_type != $this->wp_type ){
84 return $allcaps;
85 }
86
87 $allcaps[ $cap[0] ] = TRUE;
88 return $allcaps;
89 }
90
91 public function convert_to_wp( $values )
92 {
93 $core_values = array();
94 $meta_values = array();
95
96 foreach( $values as $k => $v ){
97 if( isset($this->fields_to_wp[$k]) ){
98 $wp_k = $this->fields_to_wp[$k];
99 $core_values[$wp_k] = $v;
100 }
101 else {
102 $meta_values[$k] = $v;
103 }
104 }
105
106 $return = array( $core_values, $meta_values );
107 return $return;
108 }
109
110 public function set_search_in( $fields = array() ){
111 $this->search_in = $fields;
112 return $this;
113 }
114
115 // READ
116 public function read_prepare_query( $args = array() )
117 {
118 $q = array(
119 'post_type' => $this->wp_type,
120 'post_status' => array('any', 'trash', 'draft'),
121 'perm' => 'readable',
122 // 'no_found_rows' => FALSE,
123 );
124
125 $meta_query = array();
126
127 foreach( $args['WHERE'] as $w ){
128 list( $k, $compare, $v ) = $w;
129 if( 'NOTIN' == $compare ){
130 $compare = 'NOT IN';
131 }
132
133 $test = array($k => array($compare, $v));
134 list( $core_where, $meta_where ) = $this->convert_to_wp( $test );
135
136 // echo 'CORE WHERE';
137 // _print_r( $core_where );
138 // echo 'META WHERE';
139 // _print_r( $meta_where );
140
141 // don't query main table except ID, post_name, post_title as it's too difficult at the moment
142 if( $core_where ){
143 foreach( $core_where as $k => $more ){
144 list( $compare, $v ) = $more;
145 switch( $k ){
146 case 'ID':
147 switch( $compare ){
148 case '=':
149 $q['p'] = $v;
150 break;
151 case '<>':
152 $q['post__not_in'] = array($v);
153 break;
154 case 'IN':
155 $q['post__in'] = $v;
156 break;
157 case 'NOTIN':
158 case 'NOT IN':
159 if( ! is_array($v) ){
160 $v = array($v);
161 }
162 $q['post__not_in'] = $v;
163 break;
164 }
165 break;
166
167 case 'post_name':
168 switch( $compare ){
169 case '=':
170 $q['post_name__in'] = array($v);
171 break;
172 case 'IN':
173 $q['post_name__in'] = $v;
174 break;
175 }
176 break;
177
178 case 'post_title':
179 switch( $compare ){
180 case '=':
181 $q['title'] = $v;
182 break;
183 }
184 break;
185
186 case 'post_status':
187 switch( $compare ){
188 case '=':
189 case 'IN':
190 $q['post_status'] = $v;
191 break;
192 }
193 break;
194 }
195 }
196 }
197
198 if( $meta_where ){
199 foreach( $meta_where as $k => $more ){
200 list( $compare, $v ) = $more;
201 $meta_query[] = array(
202 'key' => $k,
203 'compare' => $compare,
204 'value' => $v,
205 );
206 }
207 }
208 }
209 if( $meta_query ){
210 $meta_query['relation'] = 'AND';
211 }
212 // _print_r( $q );
213 // exit;
214
215 if( $args['SORT'] ){
216 $wp_orderby = array();
217
218 $sort = array();
219 foreach( $args['SORT'] as $s ){
220 $sort[$s[0]] = $s[1];
221 }
222
223 list( $sort_core, $sort_meta ) = $this->convert_to_wp( $sort );
224
225 if( $sort_core ){
226 $wp_orderby = array_merge( $wp_orderby, $sort_core );
227 }
228
229 if( $sort_meta ){
230 $meta_orderby = array();
231 foreach( $sort_meta as $k => $v ){
232 $meta_query[ $k . '_clause' ] = array(
233 'key' => $k,
234 );
235 $meta_orderby[ $k . '_clause' ] = $v;
236 }
237 $wp_orderby = array_merge( $wp_orderby, $meta_orderby );
238 }
239
240 $q['orderby'] = $wp_orderby;
241 }
242
243 if( $args['LIMIT'] ){
244 $q['posts_per_page'] = $args['LIMIT'][0];
245 if( $args['LIMIT'][1] ){
246 $q['offset'] = $args['LIMIT'][1];
247 }
248 }
249 else {
250 $q['posts_per_page'] = -1;
251 }
252
253 if( $args['SEARCH'] && $this->search_in ){
254 $search = array();
255 reset( $this->search_in );
256 foreach( $this->search_in as $k ){
257 $search[$k] = $args['SEARCH'];
258 }
259 list( $core_search, $meta_search ) = $this->convert_to_wp( $search );
260
261 if( $core_search ){
262 $q['s'] = $args['SEARCH'];
263 }
264
265 if( $meta_search ){
266 $submeta_query = array();
267 $submeta_query['relation'] = 'OR';
268
269 foreach( $meta_search as $k2 => $v2 ){
270 $submeta_query[] = array(
271 'key' => $k2,
272 'value' => $v2,
273 'compare' => 'LIKE',
274 );
275 }
276
277 $meta_query[] = $submeta_query;
278 }
279 }
280
281 if( $meta_query ){
282 $q['meta_query'] = $meta_query;
283 }
284
285 return $q;
286 }
287
288 public function count( $args = array() )
289 {
290 $return = NULL;
291
292 if( NULL === $args ){
293 return $return;
294 }
295
296 $args = HC3_Functions::tempCrudPrepareArgs( $args, $this->idField );
297
298 if( $this->read_use_cache ){
299 $cache_key = $this->wp_type . ':count' . json_encode($args);
300 if( array_key_exists($cache_key, self::$read_query_cache) ){
301 // echo "ON CACHE: '$sql'<br>";
302 $return = self::$read_query_cache[$cache_key];
303 return $return;
304 }
305 }
306
307 $q = $this->read_prepare_query( $args );
308
309 if( $q !== NULL ){
310 $wp_query = new WP_Query( $q );
311 $return = $wp_query->found_posts;
312 }
313
314 return $return;
315 }
316
317 public function read( $args = array() )
318 {
319 $return = array();
320
321 if( NULL === $args ){
322 return $return;
323 }
324
325 $args = array_merge( $this->default_sort, $args );
326
327 $args = HC3_Functions::tempCrudPrepareArgs( $args, $this->idField );
328
329 if( $this->read_use_cache ){
330 $cache_key = $this->wp_type . ':' . json_encode($args);
331 if( isset(self::$read_query_cache[$cache_key]) ){
332 // echo "ON CACHE: '$cache_key'<br>";
333 $return = self::$read_query_cache[$cache_key];
334 return $return;
335 }
336 }
337
338 $q = $this->read_prepare_query( $args );
339
340 // $args = array(
341 // 'post_type' => 'your_post_type',
342 // 'post_status' => 'publish',
343
344 // 'meta_query' => array(
345 // 'relation' => 'AND',
346 // array(
347 // 'key' => 'longitude-key',
348 // 'value' => '',
349 // 'compare' => 'NOT'
350 // ),
351 // array(
352 // 'key' => 'latitude-key',
353 // 'value' => '',
354 // 'compare' => 'NOT'
355 // ),
356 // array(
357 // 'key' => 'name-key',
358 // 'value' => '',
359 // 'compare' => 'NOT'
360 // ),
361 // )
362 // );
363 if( $q !== NULL ){
364 // echo 'QUERY<br>';
365 // _print_r( $q );
366 // exit;
367
368 // _print_r( $q );
369 $wp_query = new WP_Query( $q );
370 $posts = $wp_query->get_posts();
371
372 // echo "POSTS";
373 // _print_r( $posts );
374 // global $wpdb;
375 // _print_r( $wpdb->queries );
376 // exit;
377 $return = array();
378 $count = count($posts);
379 for( $ii = 0; $ii < $count; $ii++ ){
380 $meta = get_metadata( 'post', $posts[$ii]->ID );
381 $values = array_map( function($n){return $n[0];}, $meta );
382 $keys = array_keys( $values );
383 foreach( $keys as $k ){
384 $metaK = 'meta_' . $k;
385 $values[ $metaK ] = $values[$k];
386 }
387 reset( $this->fields_to_me );
388 foreach( $this->fields_to_me as $wp_field => $my_field ){
389 $values[ $my_field ] = $posts[$ii]->{$wp_field};
390 }
391
392 $key_id = $values[ $this->idField ];
393 $return[ $key_id ] = $values;
394 }
395 }
396
397 if( $this->read_use_cache ){
398 self::$read_query_cache[$cache_key] = $return;
399 }
400
401 return $return;
402 }
403
404 // CREATE
405 public function create( $values )
406 {
407 $return = $values;
408
409 list( $core_values, $meta_values ) = $this->convert_to_wp( $values );
410
411 $postarr = $core_values;
412 $postarr['post_type'] = $this->wp_type;
413
414 if( ! array_key_exists('post_status', $postarr) ){
415 $postarr['post_status'] = 'publish';
416 }
417
418 if( $meta_values ){
419 $postarr['meta_input'] = $meta_values;
420 }
421
422 $id = wp_insert_post( $postarr, TRUE );
423
424 if( is_wp_error($id) ){
425 $error = '__Database Error__' . ': ' . $id->get_error_message();
426 throw new Exception( $error );
427 }
428
429 $return['id'] = $id;
430 return $return;
431 }
432
433 // UPDATE
434 public function update( $id, $values )
435 {
436 $return = TRUE;
437
438 if( ! $id ){
439 return;
440 }
441
442 $values[$this->idField] = $id;
443 $return = $values;
444
445 unset( $values['id'] );
446 list( $core_values, $meta_values ) = $this->convert_to_wp( $values );
447
448 if( $core_values ){
449 $core_values['ID'] = $id;
450 wp_update_post( $core_values );
451 }
452
453 if( $meta_values ){
454 foreach( $meta_values as $k => $v ){
455 update_post_meta( $id, $k, $v );
456 }
457 }
458
459 return $return;
460 }
461
462 public function updateMeta( $id, $values )
463 {
464 $return = TRUE;
465
466 if( ! $id ){
467 return;
468 }
469
470 if( $values ){
471 foreach( $values as $k => $v ){
472 update_post_meta( $id, $k, $v );
473 }
474 }
475
476 return $return;
477 }
478
479
480 // DELETE
481 public function delete( $id )
482 {
483 $return = FALSE;
484
485 if( ! $id ){
486 return $return;
487 }
488
489 $return = wp_delete_post( $id, TRUE );
490 return $return;
491 }
492
493 public function deleteAll()
494 {
495 global $wpdb;
496
497 $sql = '
498 DELETE `posts`, `pm`
499 FROM `' . $wpdb->prefix . 'posts` AS `posts`
500 LEFT JOIN `' . $wpdb->prefix . 'postmeta` AS `pm` ON `pm`.`post_id` = `posts`.`ID`
501 WHERE `posts`.`post_type` = \'' . $this->wp_type . '\'';
502
503 $result = $wpdb->query($sql);
504
505 $return = TRUE;
506 return $return;
507 }
508 }
509