PluginProbe ʕ •ᴥ•ʔ
Advanced Custom Fields: Extended / trunk
Advanced Custom Fields: Extended vtrunk
0.9.2.6 0.9.2.5 0.8.6 0.8.6.1 0.8.6.3 0.8.6.5 0.8.6.6 0.8.6.7 0.8.6.8 0.8.6.9 0.8.7 0.8.7.1 0.8.7.2 0.8.7.3 0.8.7.4 0.8.7.5 0.8.7.6 0.8.8 0.8.8.1 0.8.8.10 0.8.8.11 0.8.8.2 0.8.8.3 0.8.8.4 0.8.8.5 0.8.8.6 0.8.8.7 0.8.8.8 0.8.8.9 0.8.9 0.8.9.1 0.8.9.2 0.8.9.3 0.8.9.4 0.8.9.5 0.9 0.9.0.1 0.9.0.2 0.9.0.3 0.9.0.4 0.9.0.5 0.9.0.6 0.9.0.7 0.9.0.8 0.9.0.9 0.9.1 0.9.1.1 0.9.2 0.9.2.1 0.9.2.2 0.9.2.3 0.9.2.4 trunk 0.5 0.5.1 0.5.2 0.5.2.1 0.5.2.3 0.5.5 0.5.5.1 0.5.8 0.5.8.1 0.6 0.6.0.1 0.6.0.2 0.6.1 0.6.3 0.6.5 0.6.7 0.6.7.2 0.7 0.7.0.3 0.7.5 0.7.5.5 0.7.8 0.7.9 0.7.9.3 0.7.9.4 0.7.9.9.8 0.7.9.9.9 0.8 0.8.1 0.8.2 0.8.3 0.8.3.1 0.8.4 0.8.4.1 0.8.4.5 0.8.4.6 0.8.5 0.8.5.5
acf-extended / includes / modules / performance / module-performance-functions.php
acf-extended / includes / modules / performance Last commit date
module-performance-connector.php 1 month ago module-performance-functions.php 1 month ago module-performance-ui.php 3 years ago module-performance-ultra-fields.php 2 years ago module-performance-ultra-revisions.php 1 month ago module-performance-ultra.php 1 month ago module-performance-upgrades.php 2 years ago module-performance.php 1 month ago
module-performance-functions.php
874 lines
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7
8 /**
9 * acfe_get_performance_config
10 *
11 * @param $key
12 *
13 * @return mixed|null
14 */
15 function acfe_get_performance_config($key = ''){
16
17 // default config
18 $config = array(
19 'engine' => 'ultra', // ultra | hybrid
20 'mode' => 'production', // test | production | rollback
21 'ui' => false, // sidebar metabox
22 'post_types' => array(), // allowed post types (all)
23 'taxonomies' => array(), // allowed taxonomies (all)
24 'users' => false, // allowed user roles (none)
25 'options' => false, // allowed option id (none)
26 );
27
28 // deprecated single meta filters
29 if($config['engine'] === 'ultra'){
30
31 $config['post_types'] = acfe_apply_filters_deprecated('acfe/modules/single_meta/post_types', array($config['post_types']), '0.8.9.3', 'acfe/modules/performance/config');
32 $config['taxonomies'] = acfe_apply_filters_deprecated('acfe/modules/single_meta/taxonomies', array($config['taxonomies']), '0.8.9.3', 'acfe/modules/performance/config');
33 $config['users'] = acfe_apply_filters_deprecated('acfe/modules/single_meta/users', array($config['users']), '0.8.9.3', 'acfe/modules/performance/config');
34 $config['options'] = acfe_apply_filters_deprecated('acfe/modules/single_meta/options', array($config['options']), '0.8.9.3', 'acfe/modules/performance/config');
35
36 }
37
38 // use setting
39 $setting = acf_get_setting('acfe/modules/performance');
40
41 // setting = ultra | hybrid
42 if(is_string($setting) && !empty($setting)){
43 $config['engine'] = $setting;
44
45 // setting = array('engine' => 'ultra'...)
46 }elseif(is_array($setting) && !empty($setting)){
47 $config = array_merge($config, $setting);
48 }
49
50 // filter
51 $config = apply_filters('acfe/modules/performance/config', $config);
52
53 // return key
54 if(!empty($key)){
55 return acfe_get($config, $key);
56 }
57
58 // return
59 return $config;
60
61 }
62
63
64 /**
65 * acfe_do_performance_bypass
66 *
67 * @param $func
68 * @param $args
69 *
70 * @return mixed
71 */
72 function acfe_do_performance_bypass($func, $args = array()){
73
74 // get engine
75 $engine = acfe_get_performance_config('engine');
76
77 // get engine
78 $engine = acfe_get_performance_engine($engine);
79
80 // return
81 return $engine->do_bypass($func, $args);
82
83 }
84
85
86 /**
87 * acfe_is_performance_enabled
88 *
89 * @return bool
90 */
91 function acfe_is_performance_enabled(){
92 return (bool) acf_get_setting('acfe/modules/performance');
93 }
94
95
96 /**
97 * acfe_is_object_performance_enabled
98 *
99 * @param $post_id
100 *
101 * @return bool
102 */
103 function acfe_is_object_performance_enabled($post_id = 0){
104
105 // check setting
106 if(!acfe_is_performance_enabled()){
107 return false;
108 }
109
110 // check post id
111 if(!$post_id){
112 return false;
113 }
114
115 // check local post id
116 if(acfe_is_local_post_id($post_id)){
117 return false;
118 }
119
120 // get config
121 $config = acfe_get_performance_config();
122
123 // validate engine exists
124 if(!acfe_get_performance_engine($config['engine'])){
125 return false;
126 }
127
128 /**
129 * @var $type
130 * @var $id
131 */
132 extract(acf_decode_post_id($post_id));
133
134 // validate id
135 if(!$id){
136 return false;
137 }
138
139 switch($type){
140
141 // post types
142 case 'post': {
143
144 // get post type
145 $post_type = get_post_type($id);
146
147 // validate
148 if(!$post_type){
149 return false;
150 }
151
152 return acfe_is_object_type_performance_enabled('post', $post_type);
153
154 }
155
156 // taxonomies
157 case 'term': {
158
159 // get term
160 $term = get_term($id);
161
162 // validate
163 if(is_wp_error($term) || !is_a($term, 'WP_Term')){
164 return false;
165 }
166
167 // get taxonomy
168 $taxonomy = $term->taxonomy;
169
170 return acfe_is_object_type_performance_enabled('term', $taxonomy);
171
172 }
173
174 // users
175 case 'user': {
176
177 // get user
178 $user = get_userdata($id);
179
180 // validate
181 if(!($user) || !is_a($user, 'WP_User')){
182 return false;
183 }
184
185 // get roles
186 $roles = acfe_as_array($user->roles);
187
188 // array of users
189 foreach($roles as $role){
190 if(acfe_is_object_type_performance_enabled('user', $role)){
191 return true;
192 }
193 }
194
195 return false;
196
197 }
198
199 // options pages
200 case 'option': {
201
202 return acfe_is_object_type_performance_enabled('option', $id);
203
204 }
205
206 }
207
208 // type not supported
209 // block types...
210 return false;
211
212 }
213
214
215 /**
216 * acfe_is_object_type_performance_enabled
217 *
218 * @param $type
219 * @param $object
220 *
221 * @return bool
222 */
223 function acfe_is_object_type_performance_enabled($type, $object){
224
225 // get config
226 $config = acfe_get_performance_config();
227
228 switch($type){
229
230 // post types
231 case 'post': {
232
233 /**
234 * $post_types
235 *
236 * false = disallow all
237 * true = allow all
238 * array() = allow all
239 * array('post') = allow specific
240 */
241
242 // vars
243 $restricted = acfe_get_setting('reserved_post_types', array());
244 $post_types = $config['post_types'];
245
246 // no post types allowed
247 if($post_types === false){
248 return false;
249 }
250
251 // reserved post type
252 if(in_array($object, $restricted)){
253 return false;
254 }
255
256 // allow all post types
257 if($post_types === true){
258 return true;
259 }
260
261 // array of specific post types
262 if(!empty($post_types)){
263
264 // always append 'revision' to allowed post types
265 if(!in_array('revision', $post_types)){
266 $post_types[] = 'revision';
267 }
268
269 if(!in_array($object, $post_types)){
270 return false;
271 }
272
273 }
274
275 // allowed (empty array)
276 return true;
277
278 }
279
280 // taxonomies
281 case 'term': {
282
283 /**
284 * $taxonomies
285 *
286 * false = disallow all
287 * true = allow all
288 * array() = allow all
289 * array('cat') = allow specific
290 */
291
292 // vars
293 $restricted = acfe_get_setting('reserved_taxonomies', array());
294 $taxonomies = $config['taxonomies'];
295
296 // no taxonomies allowed
297 if($taxonomies === false){
298 return false;
299 }
300
301 // reserved taxonomy
302 if(in_array($object, $restricted)){
303 return false;
304 }
305
306 // allow all taxonomies
307 if($taxonomies === true){
308 return true;
309 }
310
311 // taxonomy not allowed
312 if(!empty($taxonomies) && !in_array($object, $taxonomies)){
313 return false;
314 }
315
316 // allowed (empty array)
317 return true;
318
319 }
320
321 // users
322 case 'user': {
323
324 /**
325 * $users
326 *
327 * false = disallow all
328 * true = allow all
329 * array() = allow all
330 * array('editor') = allow specific
331 */
332
333 // vars
334 $users = $config['users'];
335
336 // no users allowed
337 if($users === false){
338 return false;
339
340 // allow all users
341 }elseif($users === true){
342 return true;
343 }
344
345 // taxonomy not allowed
346 if(!empty($users) && !in_array($object, $users)){
347 return false;
348 }
349
350 // allowed (empty array)
351 return true;
352
353 }
354
355 // options pages
356 case 'option': {
357
358 /**
359 * $options
360 *
361 * false = disallow all
362 * true = allow all
363 * array() = allow all
364 * array('options') = allow specific
365 */
366
367 // vars
368 $options = $config['options'];
369
370 // no options allowed
371 if($options === false){
372 return false;
373
374 // allow all options
375 }elseif($options === true){
376 return true;
377 }
378
379 // option not allowed
380 if(!empty($options) && !in_array($object, $options)){
381 return false;
382 }
383
384 // allowed (empty array)
385 return true;
386
387 }
388
389 }
390
391 // type not supported
392 // block types...
393 return false;
394
395 }
396
397
398 /**
399 * acfe_get_object_performance_engine
400 *
401 * @param $post_id
402 *
403 * @return false|mixed|null
404 */
405 function acfe_get_object_performance_engine($post_id){
406
407 // get engine name
408 $name = acfe_get_object_performance_engine_name($post_id);
409
410 if(!$name){
411 return false;
412 }
413
414 return acfe_get_performance_engine($name);
415
416 }
417
418
419 /**
420 * acfe_get_object_performance_other_engines
421 *
422 * @param $post_id
423 *
424 * @return false|mixed
425 */
426 function acfe_get_object_performance_other_engines($post_id){
427
428 // get engine name
429 $name = acfe_get_object_performance_engine_name($post_id);
430
431 if(!$name){
432 return false;
433 }
434
435 return acfe_query_performance_engines(array('name' => $name), 'NOT');
436
437 }
438
439
440 /**
441 * acfe_get_object_performance_engine_name
442 *
443 * @param $post_id
444 *
445 * @return false|mixed|null
446 */
447 function acfe_get_object_performance_engine_name($post_id){
448
449 // check enabled
450 $enabled = acfe_is_object_performance_enabled($post_id);
451
452 if(!$enabled){
453 return false;
454 }
455
456 return acfe_get_performance_config('engine');
457
458 }
459
460
461 /**
462 * acfe_get_object_performance_mode
463 *
464 * @param $post_id
465 *
466 * @return false|mixed|null
467 */
468 function acfe_get_object_performance_mode($post_id){
469
470 // check enabled
471 $enabled = acfe_is_object_performance_enabled($post_id);
472
473 if(!$enabled){
474 return false;
475 }
476
477 return acfe_get_performance_config('mode');
478
479 }
480
481
482 /**
483 * acfe_do_performance_convert
484 *
485 * Converts Ultra/Hybrid meta to the current engine
486 *
487 * @param $post_id
488 */
489 function acfe_do_performance_convert($post_id){
490
491 // validate enabled
492 if(!acfe_is_object_performance_enabled($post_id)){
493 return;
494 }
495
496 // get current engine data
497 $engine = acfe_get_object_performance_engine($post_id);
498 $acf = $engine->get_store($post_id);
499
500 // other engines
501 $other_engines = acfe_get_object_performance_other_engines($post_id);
502
503 // loop
504 foreach($other_engines as $other_engine){
505
506 // get other meta
507 $other_acf = $other_engine->get_store($post_id);
508
509 // other meta found
510 if(!empty($other_acf)){
511
512 // loop other meta
513 foreach($other_acf as $name => $value){
514
515 // vars
516 $hidden = acfe_starts_with($name, '_');
517 $prefix = $hidden ? '_' : '';
518 $name = ltrim($name, '_');
519
520 switch($other_engine->name){
521
522 // other engine: ultra
523 case 'ultra': {
524
525 if(!isset($acf["{$prefix}{$name}"])){
526 acf_update_metadata($post_id, $name, $value, $hidden);
527 }
528
529 break;
530
531 }
532
533 // other engine: hybrid
534 case 'hybrid': {
535
536 if(!isset($acf["{$prefix}{$name}"])){
537
538 // _my_field = field_5f9f9f9f9f9f9 exists
539 // my_field = my value doesn't exist
540 if(!isset($acf[ $name ])){
541
542 // try to find in normal meta
543 $meta = $engine->do_bypass(function($post_id, $name){
544 return acf_get_metadata($post_id, $name, false);
545 }, array($post_id, $name));
546
547 // normal meta found
548 // update both meta in current engine
549 if($meta !== null){
550
551 acf_update_metadata($post_id, $name, $value, $hidden);
552 acf_update_metadata($post_id, $name, $meta, false);
553
554 }
555
556 }else{
557 acf_update_metadata($post_id, $name, $value, $hidden);
558
559 }
560
561 }
562
563 break;
564
565 }
566
567 }
568
569 // delete other engine meta
570 $other_engine->delete_meta($post_id);
571
572 }
573 }
574
575 }
576
577 }
578
579
580 /**
581 * acfe_do_performance_rollback
582 *
583 * Rollback Ultra/Hybrid meta to normal meta
584 *
585 * @param $post_id
586 *
587 * @return bool
588 */
589 function acfe_do_performance_rollback($post_id = 0){
590
591 // validate enabled
592 if(!acfe_is_object_performance_enabled($post_id)){
593 return false;
594 }
595
596 // check mode
597 if(acfe_get_performance_config('mode') !== 'rollback'){
598 return false;
599 }
600
601 // other engines
602 $engines = acfe_get_object_performance_other_engines($post_id);
603
604 // add current engine at the end
605 // this fix an issue where the current engine would be processed first
606 // and the second engine would regenerate again meta leaving residue
607 $engines[] = acfe_get_object_performance_engine($post_id);
608
609 // loop
610 foreach($engines as $engine){
611
612 // get compiled store
613 $acf = $engine->get_store($post_id);
614
615 if(!empty($acf)){
616 foreach($acf as $name => $value){
617
618 // check if _textarea => field_64148c317fcba
619 $hidden = acfe_starts_with($name, '_');
620 $name = ltrim($name, '_');
621
622 // update as normal meta
623 acf_update_metadata($post_id, $name, $value, $hidden);
624
625 }
626 }
627
628 // clean acf
629 $engine->delete_meta($post_id);
630
631 }
632
633 return true;
634
635 }
636
637
638 /**
639 * acfe_get_object_performance_status
640 *
641 * @param $post_id
642 *
643 * @return array|false
644 */
645 function acfe_get_object_performance_status($post_id){
646
647 // check enabled
648 $enabled = acfe_is_object_performance_enabled($post_id);
649
650 if(!$enabled){
651 return false;
652 }
653
654 // get meta
655 $meta = acfe_get_object_performance_meta($post_id);
656
657 // ready = meta not found
658 // active = meta found
659 $name = $meta !== null ? 'active' : 'ready';
660 $title = $meta !== null ? __('Active', 'acfe') : __('Ready', 'acfe');
661
662 $config = acfe_get_performance_config();
663 $meta_key = acfe_get_performance_engine($config['engine'])->get_meta_key($post_id);
664
665 $return = array(
666 'name' => $name,
667 'title' => $title,
668 'message' => '',
669 );
670
671 switch($name){
672
673 case 'active': {
674 $return['message'] = __('Performance Mode is active.', 'acfe') . "<br/>" . sprintf(__('The \'%s\' meta was found on this object and is effective.', 'acfe'), $meta_key);
675 break;
676 }
677
678 case 'ready': {
679 $return['message'] = __('Performance Mode is ready.', 'acfe') . "<br/>" . sprintf(__('The \'%s\' meta will be created when object will be saved.', 'acfe'), $meta_key);
680 break;
681 }
682
683 }
684
685 return $return;
686
687 }
688
689
690 /**
691 * acfe_get_object_performance_conflict
692 *
693 * @param $post_id
694 *
695 * @return false|mixed|null
696 */
697 function acfe_get_object_performance_conflict($post_id){
698
699 // check enabled
700 $enabled = acfe_is_object_performance_enabled($post_id);
701
702 if(!$enabled){
703 return false;
704 }
705
706 $return = false;
707
708 // get object engine name
709 $engine = acfe_get_object_performance_engine_name($post_id);
710
711 switch($engine){
712
713 // ultra
714 case 'ultra': {
715
716 $hybrid = acfe_get_performance_engine('hybrid');
717 if($hybrid){
718
719 // vars
720 $meta = $hybrid->get_meta($post_id);
721 $meta_key = $hybrid->get_meta_key($post_id);
722
723 // hybrid meta found
724 if($meta !== null){
725
726 $return = array(
727 'engine' => 'hybrid',
728 'meta' => $meta,
729 'title' => __('Hybrid meta found', 'acfe'),
730 'message' => sprintf(__('Hybrid engine \'%s\' meta found.', 'acfe'), $meta_key). "<br/>" . __("This meta will be converted to Ultra engine upon save.", 'acfe'),
731 );
732 }
733
734 }
735
736 break;
737 }
738
739 // hybrid
740 case 'hybrid': {
741
742 $ultra = acfe_get_performance_engine('ultra');
743 if($ultra){
744
745 // vars
746 $meta = $ultra->get_meta($post_id);
747 $meta_key = $ultra->get_meta_key($post_id);
748
749 // ultra meta found
750 if($meta !== null){
751
752 $return = array(
753 'engine' => 'ultra',
754 'meta' => $meta,
755 'title' => __('Ultra meta found', 'acfe'),
756 'message' => sprintf(__('Ultra engine \'%s\' meta found.', 'acfe'), $meta_key). "<br/>" . __("This meta will be converted to Hybrid engine upon save.", 'acfe'),
757 );
758
759 }
760
761 }
762
763
764 break;
765 }
766
767 }
768
769 return $return;
770
771 }
772
773
774 /**
775 * acfe_get_object_performance_meta
776 *
777 * @param $post_id
778 *
779 * @return false|mixed|null
780 */
781 function acfe_get_object_performance_meta($post_id){
782
783 // get engine
784 $engine = acfe_get_object_performance_engine($post_id);
785
786 // validate
787 if(empty($engine)){
788 return null;
789 }
790
791 // return
792 return $engine->get_meta($post_id);
793
794 }
795
796
797 /**
798 * acfe_delete_object_performance_meta
799 *
800 * @param $post_id
801 *
802 * @return false|mixed|null
803 */
804 function acfe_delete_object_performance_meta($post_id){
805
806 // get engine
807 $engine = acfe_get_object_performance_engine($post_id);
808
809 // validate
810 if(empty($engine)){
811 return false;
812 }
813
814 // return
815 return $engine->delete_meta($post_id);
816
817 }
818
819
820 /**
821 * acfe_is_single_meta_enabled
822 *
823 * @param $post_id
824 *
825 * @return bool
826 * @deprecated
827 */
828 function acfe_is_single_meta_enabled($post_id = 0){
829
830 // return global setting
831 if(!$post_id){
832
833 acfe_deprecated_function('acfe_is_single_meta_enabled()', '0.8.9.3', 'acfe_is_performance_enabled()');
834 return acfe_is_performance_enabled() && acfe_get_performance_config('engine') === 'ultra';
835
836 }
837
838 // return object performance setting
839 acfe_deprecated_function('acfe_is_single_meta_enabled()', '0.8.9.3', 'acfe_is_object_performance_enabled()');
840 return acfe_get_object_performance_engine_name($post_id) === 'ultra';
841
842 }
843
844
845 /**
846 * acfe_get_single_meta
847 *
848 * @param $post_id
849 *
850 * @return mixed
851 * @deprecated
852 */
853 function acfe_get_single_meta($post_id){
854
855 acfe_deprecated_function('acfe_get_single_meta()', '0.8.9.3', 'acfe_get_object_performance_meta()');
856 return acfe_get_performance_engine('ultra')->get_meta($post_id);
857
858 }
859
860
861 /**
862 * acfe_delete_single_meta
863 *
864 * @param $post_id
865 *
866 * @return bool
867 * @deprecated
868 */
869 function acfe_delete_single_meta($post_id){
870
871 acfe_deprecated_function('acfe_delete_single_meta()', '0.8.9.3', 'acfe_delete_object_performance_meta()');
872 return acfe_get_performance_engine('ultra')->delete_meta($post_id);
873
874 }