PluginProbe
Advanced Custom Fields: Extended / 0.9.2.4
Advanced Custom Fields: Extended v0.9.2.4
0.9.2.7 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 All 92 releases
acf-extended / includes / module.php
module.php
1,444 lines 35.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if(!defined('ABSPATH')){
4 exit;
5 }
6
7 if(!class_exists('acfe_module')):
8
9 class acfe_module{
10
11 // vars
12 public $name = '',
13 $plural = '',
14 $post_type = '',
15 $args = array(),
16
17 $setting = '',
18 $settings = '',
19 $view = '',
20 $register = '',
21
22 $export_tool = '',
23 $import_tool = '',
24 $export_actions = array('php', 'json'),
25 $export_files = array(),
26 $messages = array(),
27
28 $item = array(),
29 $validate = array(),
30 $columns = array(),
31 $alias = array(),
32 $l10n = array(),
33
34 $modified = null;
35
36 /**
37 * construct
38 */
39 function __construct(){
40
41 // setup
42 $this->initialize();
43
44 // register stores
45 // todo move 'local-any-post_type' store outside module
46 acf_register_store("local-{$this->post_type}");
47 acf_register_store("local-any-{$this->post_type}");
48 acf_register_store("{$this->post_type}:db")->prop('multisite', true);
49 acf_register_store("{$this->post_type}:local")->prop('multisite', true);
50
51 $this->add_module_filter('acfe/module/register_field_groups', array($this, 'register_field_groups'), 9);
52 $this->add_module_filter('acfe/module/register_items', array($this, 'register_items'), 9);
53 $this->add_module_filter('acfe/module/register_item_args', array($this, 'register_item_args'), 9);
54 $this->add_module_action('acfe/module/register_item', array($this, 'register_item'), 9);
55 $this->add_module_action('acfe/module/load_post', array($this, 'load_post'), 9);
56 $this->add_module_action('acfe/module/load_posts', array($this, 'load_posts'), 9);
57 $this->add_module_filter('acfe/module/edit_columns', array($this, 'edit_columns'), 9);
58 $this->add_module_action('acfe/module/edit_columns_html', array($this, 'edit_columns_html'), 9, 2);
59 $this->add_module_filter('acfe/module/validate_save_item', array($this, 'validate_save_item'), 9);
60 $this->add_module_filter('acfe/module/prepare_load_item', array($this, 'prepare_load_item'), 9);
61 $this->add_module_filter('acfe/module/prepare_save_item', array($this, 'prepare_save_item'), 9);
62 $this->add_module_action('acfe/module/updated_item', array($this, 'updated_item'), 9);
63 $this->add_module_action('acfe/module/trashed_item', array($this, 'trashed_item'), 9);
64 $this->add_module_action('acfe/module/untrashed_item', array($this, 'untrashed_item'), 9);
65 $this->add_module_action('acfe/module/deleted_item', array($this, 'deleted_item'), 9);
66 $this->add_module_action('acfe/module/imported_item', array($this, 'imported_item'), 9);
67 $this->add_module_filter('acfe/module/pre_validate_item', array($this, 'pre_validate_item'), 9);
68 $this->add_module_filter('acfe/module/validate_item', array($this, 'validated_item'), 9);
69 $this->add_module_filter('acfe/module/load_item', array($this, 'load_item'), 9);
70 $this->add_module_filter('acfe/module/load_items', array($this, 'load_items'), 9);
71
72 $this->add_action('acfe/do_reset', array($this, 'reset'));
73
74 // add to reserved post types
75 acfe_append_setting('reserved_post_types', $this->post_type);
76
77 }
78
79
80 /**
81 * initialize
82 */
83 function initialize(){
84 // ...
85 }
86
87
88 /**
89 * is_active
90 * @return bool
91 */
92 function is_active(){
93
94 if(!$this->setting){
95 return true;
96 }
97
98 return (bool) acfe_get_setting($this->setting);
99
100 }
101
102
103 /**
104 * get_field_groups
105 *
106 * @return mixed
107 */
108 function get_field_groups(){
109 return $this->apply_module_filters('acfe/module/register_field_groups', array());
110 }
111
112
113 /**
114 * validate_item
115 *
116 * @param $item
117 *
118 * @return array|mixed
119 */
120 function validate_item($item = array()){
121
122 // already valid
123 if(is_array($item) && !empty($item['_valid'])){
124 return $item;
125 }
126
127 // convert
128 $item['ID'] = (int) acf_maybe_get($item, 'ID', 0);
129 $item['active'] = (bool) acf_maybe_get($item, 'active', true);
130 $item['_valid'] = true;
131
132 // filters
133 $item = $this->apply_module_filters('acfe/module/pre_validate_item', $item);
134
135 // default item
136 $defaults = wp_parse_args($this->item, array(
137 'ID' => 0,
138 'name' => '',
139 'label' => '',
140 ));
141
142 // parse defaults
143 $item = acfe_parse_args_r($item, $defaults);
144
145 // process alias
146 foreach($this->alias as $k => $alias){
147 if(!empty($item[ $alias ])){
148
149 // set 'page_title' = 'label'
150 $item[ $k ] = $item[ $alias ];
151
152 }
153 }
154
155 // filters
156 $item = $this->apply_module_filters('acfe/module/validate_item', $item);
157
158 return $item;
159
160 }
161
162
163 /**
164 * update_item
165 *
166 * @param $item
167 *
168 * @return array
169 */
170 function update_item($item){
171
172 // make sure name is unique in db
173 $name = wp_unique_post_slug($item['name'], $item['ID'], (!empty($item['active']) ? 'publish' : 'acf-disabled'), $this->post_type, 0);
174
175 if($item['name'] !== $name){
176 $item['name'] = $name;
177 acf_enable_filter('acfe/module/update_unique_name');
178 }
179
180 // validate
181 $item = $this->validate_item($item);
182
183 // prepare item
184 $item = wp_unslash($item);
185 $item = acfe_parse_types($item);
186 // todo: do not parse types on 'values' => array()
187
188 // modified as global var
189 $this->modified = acf_extract_var($item, 'modified');
190
191 // cleanup keys
192 $export = $this->prepare_item_for_export($item);
193
194 // array of data
195 $save = array(
196 'ID' => $item['ID'],
197 'post_status' => $item['active'] ? 'publish' : 'acf-disabled',
198 'post_type' => $this->post_type,
199 'post_title' => $item['label'],
200 'post_name' => $item['name'],
201 'post_content' => maybe_serialize($export),
202 'comment_status' => 'closed',
203 'ping_status' => 'closed',
204 );
205
206 // bypass post modified
207 add_filter('wp_insert_post_data', array($this, 'bypass_post_modified'), 1, 2);
208
209 // remove filter to avoid serialized data corruption
210 remove_filter('content_save_pre', 'wp_targeted_link_rel');
211
212
213 // slash
214 $save = wp_slash($save);
215
216 // update or insert
217 if($item['ID']){
218 wp_update_post($save);
219 }else{
220 $item['ID'] = wp_insert_post($save);
221 }
222
223 // remove bypass post modified
224 remove_filter('wp_insert_post_data', array($this, 'bypass_post_modified'), 1);
225
226 // flush cache
227 $this->flush_cache($item);
228
229 // actions
230 $this->do_module_action('acfe/module/updated_item', $item);
231
232 // delete _wp_old_slug meta
233 delete_post_meta($item['ID'], '_wp_old_slug');
234
235 // return
236 return $item;
237
238 }
239
240
241 /**
242 * bypass_post_modified
243 *
244 * bypass post_modified automatically set to current date by wp_update_post
245 * https://brogramo.com/how-to-update-a-wordpress-post-without-updating-the-modified-date-using-wp_update_post/
246 *
247 * @param $data
248 * @param $array
249 *
250 * @return mixed
251 */
252 function bypass_post_modified($data, $array){
253
254 if(!isset($data['post_modified'], $data['post_modified_gmt']) || !$this->modified){
255 return $data;
256 }
257
258 $data['post_modified'] = wp_date('Y-m-d H:i:s', $this->modified);
259 $data['post_modified_gmt'] = get_gmt_from_date($data['post_modified']);
260
261 return $data;
262
263 }
264
265
266 /**
267 * trash_item
268 *
269 * @param $id
270 *
271 * @return bool
272 */
273 function trash_item($id){
274
275 // get
276 $item = $this->get_item($id, 'db');
277
278 // bail early
279 if(!$item || !$item['ID']){
280 return false;
281 }
282
283 // trash
284 wp_trash_post($item['ID']);
285
286 // flush
287 $this->flush_cache($item);
288
289 // actions
290 $this->do_module_action('acfe/module/trashed_item', $item);
291
292 // return
293 return true;
294
295 }
296
297
298 /**
299 * untrash_item
300 *
301 * @param $id
302 *
303 * @return bool
304 */
305 function untrash_item($id){
306
307 // get db item
308 $item = $this->get_item($id, 'db');
309
310 // bail early
311 if(!$item || !$item['ID']){
312 return false;
313 }
314
315 // untrash
316 wp_untrash_post($item['ID']);
317
318 // update item (new status etc...)
319 $this->update_item($item);
320
321 // already flushed in update_item
322 // $this->flush_cache($item);
323
324 // actions
325 $this->do_module_action('acfe/module/untrashed_item', $item);
326
327 // return
328 return true;
329 }
330
331
332 /**
333 * delete_item
334 *
335 * @param $id
336 *
337 * @return bool
338 */
339 function delete_item($id){
340
341 // get
342 $item = $this->get_item($id, 'db');
343
344 // bail early
345 if(!$item || !$item['ID']){
346 return false;
347 }
348
349 // delete post
350 wp_delete_post($item['ID'], true);
351
352 // flush
353 $this->flush_cache($item);
354
355 // actions
356 $this->do_module_action('acfe/module/deleted_item', $item);
357
358 // return
359 return true;
360
361 }
362
363
364 /**
365 * import_item
366 *
367 * @param $item
368 *
369 * @return array
370 */
371 function import_item($item){
372
373 // disable filters to ensure data is not modified by local, clone, etc.
374 $filters = acf_disable_filters();
375
376 // validate item (ensures all settings exist).
377 $item = $this->validate_item($item);
378
379 // prepare item for import (modifies settings).
380 $item = $this->prepare_item_for_import($item);
381
382 // save item
383 $item = $this->update_item($item);
384
385 // enable filters again.
386 acf_enable_filters($filters);
387
388 // actions
389 $this->do_module_action('acfe/module/imported_item', $item);
390
391 // return
392 return $item;
393
394 }
395
396
397 /**
398 * duplicate_item
399 *
400 * @param $id
401 * @param $new_post_id
402 *
403 * @return array|false
404 */
405 function duplicate_item($id = 0, $new_post_id = 0){
406
407 // get db item
408 $item = $this->get_item($id, 'db');
409
410 // bail early if item was not found
411 if(!$item || !$item['ID']){
412 return false;
413 }
414
415 // update attributes
416 $item['ID'] = $new_post_id;
417
418 // Add (copy) to title when apropriate
419 if(!$new_post_id){
420 $item['label'] .= ' (' . __('copy', 'acf') . ')';
421 }
422
423 // save item
424 $item = $this->update_item($item);
425
426 // actions
427 $this->do_module_action('acfe/module/duplicated_item', $item);
428
429 // return
430 return $item;
431
432 }
433
434
435 /**
436 * get_item
437 *
438 * Get db or local item
439 *
440 * @param $id
441 *
442 * @return array|false|mixed|null
443 */
444 function get_item($id, $source = 'any'){
445
446 // object
447 if(is_object($id)){
448 $id = $id->ID;
449
450 // array
451 }elseif(is_array($id)){
452 $id = !empty($id['name']) ? $id['name'] : acf_maybe_get($id, 'ID', 0);
453 }
454
455 // source: any
456 if($source === 'any'){
457 $source = $this->is_local_item($id) ? 'local' : 'db';
458 }
459
460 // check store
461 $store = acf_get_store("{$this->post_type}:{$source}");
462 if($store->has($id)){
463 return $store->get($id);
464 }
465
466 // get raw item
467 $item = $this->get_raw_item($id, $source);
468 if(!$item){
469 return false;
470 }
471
472 // validate
473 $item = $this->validate_item($item);
474
475 // filters
476 $item = $this->apply_module_filters('acfe/module/load_item', $item);
477
478 // store
479 $store->set($item['name'], $item);
480 $store->alias($item['name'], $item['ID']);
481
482 // return
483 return $item;
484
485 }
486
487
488 /**
489 * get_raw_item
490 *
491 * Get raw item
492 *
493 * @param $id
494 * @param $source
495 *
496 * @return array|false
497 */
498 function get_raw_item($id, $source = 'any'){
499
500 // object
501 if(is_object($id)){
502 $id = $id->ID;
503
504 // array
505 }elseif(is_array($id)){
506 $id = !empty($id['name']) ? $id['name'] : acf_maybe_get($id, 'ID', 0);
507 }
508
509 // source: any
510 if($source === 'any'){
511 $source = $this->is_local_item($id) ? 'local' : 'db';
512 }
513
514 // source: local
515 if($source === 'local'){
516
517 $item = acf_get_local_store($this->post_type)->get($id);
518 if(!$item){
519 return false;
520 }
521
522 return $item;
523
524 }
525
526 // source: db
527 $post = $this->get_item_post($id);
528 if(!$post){
529 return false;
530 }
531
532 // bail early if incorrect post type
533 if($post->post_type !== $this->post_type){
534 return false;
535 }
536
537 // unserialize post content
538 $item = maybe_unserialize($post->post_content);
539 $item = acf_get_array($item); // cast as array
540
541 // prepend id
542 $item = wp_parse_args($item, array(
543 'ID' => $post->ID,
544 'name' => $post->post_name,
545 'label' => $post->post_title,
546 ));
547
548 // return
549 return $item;
550
551 }
552
553
554 /**
555 * get_item_post
556 *
557 * @param $id
558 *
559 * @return array|false|WP_Post|null
560 */
561 function get_item_post($id = 0){
562
563 // get post if numeric
564 if(is_numeric($id)){
565
566 return get_post($id);
567
568 // search posts if string
569 }elseif(is_string($id)){
570
571 // try cache
572 $cache_key = acf_cache_key("{$this->post_type}_post:name:$id");
573 $post_id = wp_cache_get($cache_key, 'acfe');
574
575 if($post_id === false){
576
577 // query posts
578 $posts = get_posts(array(
579 'name' => $id,
580 'posts_per_page' => 1,
581 'post_type' => $this->post_type,
582 'post_status' => array('publish', 'acf-disabled', 'trash'),
583 'orderby' => 'menu_order title',
584 'order' => 'ASC',
585 'suppress_filters' => false,
586 'cache_results' => true,
587 'update_post_meta_cache' => false,
588 'update_post_term_cache' => false,
589 ));
590
591 // update post with non false value
592 $post_id = $posts ? $posts[0]->ID : 0;
593
594 // update cache
595 wp_cache_set($cache_key, $post_id, 'acfe');
596
597 }
598
599 // check post id and return post when possible
600 if($post_id){
601 return get_post($post_id);
602 }
603 }
604
605 // return
606 return false;
607
608 }
609
610
611 /**
612 * get_items
613 *
614 * @param $source
615 *
616 * retrieve db + local items
617 *
618 * @return mixed
619 */
620 function get_items($source = 'any'){
621
622 // vars
623 $items = array();
624
625 // loop raw items and validate
626 foreach($this->get_raw_items($source) as $raw_item){
627
628 $item = $this->get_item($raw_item, $source);
629 if($item){
630
631 if(!empty($raw_item['ID'])){
632 $item['ID'] = $raw_item['ID'];
633 }
634
635 $items[] = $item;
636
637 }
638
639 }
640
641 // filters
642 $items = $this->apply_module_filters('acfe/module/load_items', $items);
643
644 // return
645 return $items;
646
647 }
648
649
650 /**
651 * get_raw_items
652 *
653 * @return array
654 */
655 function get_raw_items($source = 'any'){
656
657 // vars
658 $items = array();
659
660 // source: any or db
661 if($source === 'any' || $source === 'db'){
662
663 // try cache
664 $cache_key = acf_cache_key($this->post_type);
665 $post_ids = wp_cache_get($cache_key, 'acfe');
666
667 if($post_ids === false){
668
669 // query
670 $posts = get_posts(array(
671 'posts_per_page' => -1,
672 'post_type' => $this->post_type,
673 'orderby' => 'menu_order title',
674 'order' => 'ASC',
675 'suppress_filters' => false, // Allow WPML to modify the query
676 'cache_results' => true,
677 'update_post_meta_cache' => false,
678 'update_post_term_cache' => false,
679 'post_status' => array('publish', 'acf-disabled'),
680 ));
681
682 // update post ids with non false values
683 $post_ids = array();
684 foreach($posts as $post){
685 $post_ids[] = $post->ID;
686 }
687
688 // update cache
689 wp_cache_set($cache_key, $post_ids, 'acfe');
690
691 }
692
693 // loop post ids
694 foreach($post_ids as $post_id){
695
696 // get raw item
697 $raw_item = $this->get_raw_item($post_id, 'db');
698
699 // append
700 if($raw_item){
701 $items[] = $raw_item;
702 }
703
704 }
705
706 }
707
708 // source: any or local
709 if($source === 'any' || $source === 'local'){
710
711 // check local filter
712 $is_local = acf_is_filter_enabled('local');
713
714 // enable filter
715 if(!$is_local){
716 acf_enable_filter('local');
717 }
718
719 // vars
720 $local_items = acf_get_local_store($this->post_type)->get();
721
722 if($local_items){
723
724 // generate map of 'index' => 'name' data.
725 $map = wp_list_pluck($items, 'name');
726
727 // loop over items and update/append local
728 foreach($local_items as $local_item){
729
730 // update
731 $i = array_search($local_item['name'], $map);
732 if($i !== false){
733 unset($local_item['ID']);
734 $items[ $i ] = array_merge($items[ $i ], $local_item);
735
736 // append
737 }else{
738 $items[] = $local_item;
739 }
740
741 }
742
743 // sort list via name
744 $items = wp_list_sort($items, array(
745 'name' => 'ASC',
746 ));
747
748 }
749
750 // disable filter
751 if(!$is_local){
752 acf_disable_filter('local');
753 }
754
755 }
756
757 // return
758 return $items;
759
760 }
761
762
763 /**
764 * flush_cache
765 *
766 * @param $item
767 */
768 function flush_cache($item){
769
770 // delete stored data.
771 acf_get_store("{$this->post_type}:db")->remove($item['name']);
772 acf_get_store("{$this->post_type}:local")->remove($item['name']);
773
774 // flush cached post_id for this item name
775 wp_cache_delete(acf_cache_key("{$this->post_type}_post:name:{$item['name']}"), 'acfe');
776
777 // flush cached array of post_ids for collection of items
778 wp_cache_delete(acf_cache_key($this->post_type), 'acfe');
779
780 }
781
782
783 /**
784 * prepare_item_for_export
785 *
786 * @param $item
787 *
788 * @return mixed
789 */
790 function prepare_item_for_export($item = array()){
791
792 // todo: different export for acf function such as acf_register_block_type() with no custom args like acfe_autosync
793
794 // remove args
795 acf_extract_vars($item, array('ID', 'local', 'local_file', '_valid'));
796
797 // remove alias keys
798 acf_extract_vars($item, $this->alias);
799
800 // filters
801 $item = $this->apply_module_filters('acfe/module/prepare_item_for_export', $item);
802
803 return $item;
804
805 }
806
807
808 /**
809 * prepare_item_for_import
810 *
811 * @param $item
812 *
813 * @return mixed
814 */
815 function prepare_item_for_import($item){
816
817 // process alias
818 foreach($this->alias as $k => $alias){
819
820 if(!empty($item[ $k ])){
821
822 // set 'label' = 'page_title'
823 $item[ $alias ] = $item[ $k ];
824
825 }
826 }
827
828 // filters
829 $item = $this->apply_module_filters('acfe/module/prepare_item_for_import', $item);
830
831 return $item;
832
833 }
834
835
836 /**
837 * translate_item
838 *
839 * @param $item
840 *
841 * @return mixed
842 */
843 function translate_item($item = array()){
844
845 foreach($this->l10n as $k){
846 acfe_array_set($item, $k, acf_translate(acfe_array_get($item, $k)));
847 }
848
849 return $item;
850 }
851
852
853 /**
854 * is_local_item
855 *
856 * @param $name
857 *
858 * @return bool
859 */
860 function is_local_item($name = ''){
861 return acf_get_local_store($this->post_type)->has($name);
862 }
863
864
865 /**
866 * add_local_item
867 *
868 * @param $item
869 */
870 function add_local_item($item){
871
872 // apply default properties
873 $item = wp_parse_args($item, array(
874 'name' => '',
875 'label' => '',
876 'local' => 'php',
877 ));
878
879 // append local file for php
880 if($item['local'] === 'php'){
881
882 // get php local file path
883 $backtrace = debug_backtrace();
884
885 // append php local file
886 if(isset($backtrace[1]['file'])){
887 $item['local_file'] = wp_normalize_path($backtrace[1]['file']);
888 }
889
890 }
891
892 // prepare item keys (alias etc...)
893 $item = $this->prepare_item_for_import($item);
894
895 // generate name if not provided
896 if(!$item['name']){
897 $item['name'] = acf_slugify($item['label']);
898 }
899
900 // add item to store if it doesn't exist
901 if(!$this->is_local_item($item['name'])){
902 acf_get_local_store($this->post_type)->set($item['name'], $item);
903 }
904
905 // action
906 $this->do_module_action('acfe/module/add_local_item', $item);
907
908 }
909
910
911 /**
912 * export_code
913 *
914 * @param $code
915 * @param $args
916 *
917 * @return mixed
918 */
919 function export_code($code, $args){
920 return '';
921 }
922
923
924 /**
925 * export_local_code
926 *
927 * @param $code
928 * @param $args
929 *
930 * @return mixed
931 */
932 function export_local_code($code, $args){
933 return $this->export_code($code, $args);
934 }
935
936
937 /**
938 * get_export_tool
939 *
940 * @return string
941 */
942 function get_export_tool(){
943 return !empty($this->export_tool) ? $this->export_tool : "acfe_module_{$this->name}_export";
944 }
945
946
947 /**
948 * get_import_tool
949 *
950 * @return string
951 */
952 function get_import_tool(){
953 return !empty($this->import_tool) ? $this->import_tool : "acfe_module_{$this->name}_import";
954 }
955
956
957 /**
958 * get_export_url
959 *
960 * @param $action
961 * @param $item
962 *
963 * @return string|void
964 */
965 function get_export_url($action, $item){
966
967 $name = acf_maybe_get($item, 'name', $item);
968 $tool = $this->get_export_tool();
969 return admin_url("edit.php?post_type=acf-field-group&page=acf-tools&tool={$tool}&action={$action}&keys={$name}");
970
971 }
972
973
974 /**
975 * get_export_link
976 *
977 * @param $action
978 * @param $item
979 * @param $text
980 *
981 * @return string
982 */
983 function get_export_link($action, $item, $text = ''){
984
985 $name = acf_maybe_get($item, 'name', $item);
986 $text = !empty($text) ? $text : $this->get_export_action_label($action);
987 return '<a href="' . $this->get_export_url($action, $name) . '">' . $text . '</a>';
988
989 }
990
991
992 /**
993 * get_export_links
994 *
995 * @param $item
996 *
997 * @return array
998 */
999 function get_export_links($item){
1000
1001 $links = array();
1002 foreach($this->export_actions as $action){
1003 $links[] = $this->get_export_link($action, $item['name']);
1004 }
1005
1006 return $links;
1007
1008 }
1009
1010
1011 /**
1012 * get_export_action_label
1013 *
1014 * @param $action
1015 *
1016 * @return string
1017 */
1018 function get_export_action_label($action){
1019 return $action === 'php' ? 'PHP' : 'Json';
1020 }
1021
1022
1023 /**
1024 * get_label
1025 *
1026 * @param $type
1027 *
1028 * @return mixed|null
1029 */
1030 function get_label($type = 'singular_name'){
1031
1032 $labels = acfe_maybe_get($this->args, 'labels');
1033
1034 if(!empty($labels)){
1035 return acf_maybe_get($labels, $type);
1036 }
1037
1038 return acfe_maybe_get($this->args, 'label');
1039
1040 }
1041
1042
1043 /**
1044 * get_message
1045 *
1046 * @param $name
1047 *
1048 * @return mixed|null
1049 */
1050 function get_message($name){
1051 return acf_maybe_get($this->messages, $name);
1052 }
1053
1054
1055 /**
1056 * reset
1057 */
1058 function reset(){
1059
1060 // disable sync to avoid generating files
1061 acfe_update_setting('php', false);
1062 acfe_update_setting('json', false);
1063
1064 // get db items
1065 $items = $this->get_items('db');
1066
1067 if(!empty($items)){
1068
1069 foreach($items as $item){
1070
1071 // update db local
1072 // do not use module->update_item() to avoid changing post date
1073 $this->do_module_action('acfe/module/updated_item', $item);
1074
1075 }
1076
1077 // Log
1078 $message = '[ACF Extended] ' . __('Reset', 'acfe') . ': ' . $this->get_label('name');
1079 acf_log($message);
1080
1081 }
1082
1083 }
1084
1085
1086 /**
1087 * do_action
1088 *
1089 * @param $tag
1090 * @param ...$args
1091 */
1092 function do_action($tag, ...$args){
1093
1094 $args[] = $this;
1095 do_action_ref_array($tag, $args);
1096
1097 }
1098
1099
1100 /**
1101 * apply_filters
1102 *
1103 * @param $tag
1104 * @param ...$args
1105 *
1106 * @return mixed
1107 */
1108 function apply_filters($tag, ...$args){
1109
1110 $args[] = $this;
1111 return apply_filters_ref_array($tag, $args);
1112
1113 }
1114
1115
1116 /**
1117 * do_module_action
1118 *
1119 * @param $tag
1120 * @param ...$args
1121 */
1122 function do_module_action($tag, ...$args){
1123
1124 $args[] = $this;
1125
1126 do_action_ref_array("{$tag}/module={$this->name}", $args);
1127 do_action_ref_array($tag, $args);
1128
1129 }
1130
1131
1132 /**
1133 * apply_module_filters
1134 *
1135 * @param $tag
1136 * @param ...$args
1137 *
1138 * @return mixed
1139 */
1140 function apply_module_filters($tag, ...$args){
1141
1142 $args[] = $this;
1143
1144 $args[0] = apply_filters_ref_array("{$tag}/module={$this->name}", $args);
1145 $args[0] = apply_filters_ref_array($tag, $args);
1146
1147 return $args[0];
1148
1149 }
1150
1151
1152 /**
1153 * add_action
1154 *
1155 * @param $tag
1156 * @param $function_to_add
1157 * @param $priority
1158 * @param $accepted_args
1159 */
1160 function add_action($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
1161
1162 if(is_callable($function_to_add)){
1163 add_action($tag, $function_to_add, $priority, $accepted_args);
1164 }
1165
1166 }
1167
1168
1169 /**
1170 * add_filter
1171 *
1172 * @param $tag
1173 * @param $function_to_add
1174 * @param $priority
1175 * @param $accepted_args
1176 */
1177 function add_filter($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
1178
1179 if(is_callable($function_to_add)){
1180 add_filter($tag, $function_to_add, $priority, $accepted_args);
1181 }
1182
1183 }
1184
1185
1186 /**
1187 * add_module_action
1188 *
1189 * @param $tag
1190 * @param $function_to_add
1191 * @param $priority
1192 * @param $accepted_args
1193 */
1194 function add_module_action($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
1195
1196 $tag .= "/module={$this->name}";
1197 $this->add_action($tag, $function_to_add, $priority, $accepted_args);
1198
1199 }
1200
1201
1202 /**
1203 * add_module_filter
1204 *
1205 * @param $tag
1206 * @param $function_to_add
1207 * @param $priority
1208 * @param $accepted_args
1209 */
1210 function add_module_filter($tag = '', $function_to_add = '', $priority = 10, $accepted_args = 1){
1211
1212 $tag .= "/module={$this->name}";
1213 $this->add_filter($tag, $function_to_add, $priority, $accepted_args);
1214
1215 }
1216
1217
1218 /**
1219 * get_local_item
1220 *
1221 * @param $name
1222 *
1223 * @deprecated
1224 *
1225 * @return array|false|mixed
1226 */
1227 function get_local_item($name = ''){
1228
1229 acfe_deprecated_function(__METHOD__, '0.9.2.4', '$module->get_item($name, \'local\')');
1230 return $this->get_item($name, 'local');
1231
1232 }
1233
1234
1235 /**
1236 * get_local_items
1237 *
1238 * @deprecated
1239 *
1240 * @return array
1241 */
1242 function get_local_items(){
1243
1244 acfe_deprecated_function(__METHOD__, '0.9.2.4', '$module->get_items(\'local\')');
1245 return $this->get_items('local');
1246
1247 }
1248
1249 }
1250
1251 endif;
1252
1253 // register store
1254 acf_register_store('acfe-modules');
1255
1256
1257 /**
1258 * acfe_register_module
1259 *
1260 * @param $class
1261 *
1262 * @return bool
1263 */
1264 function acfe_register_module($class){
1265
1266 // instantiate
1267 $module = new $class();
1268
1269 // add to store
1270 acf_get_store('acfe-modules')->set($module->name, $module);
1271
1272 // return
1273 return true;
1274
1275 }
1276
1277
1278 /**
1279 * acfe_get_modules
1280 * @return array|mixed|null
1281 */
1282 function acfe_get_modules(){
1283 return acf_get_store('acfe-modules')->get();
1284 }
1285
1286
1287 /**
1288 * acfe_get_module
1289 *
1290 * @param $module
1291 *
1292 * @return acfe_module|array|mixed|null
1293 */
1294 function acfe_get_module($module){
1295
1296 if($module instanceof acfe_module){
1297 return $module;
1298 }
1299
1300 return acf_get_store('acfe-modules')->get($module);
1301 }
1302
1303
1304 /**
1305 * acfe_query_module
1306 *
1307 * @param array $query
1308 *
1309 * @return false|mixed
1310 */
1311 function acfe_query_module($query = array()){
1312
1313 $modules = acfe_query_modules($query);
1314
1315 return current($modules);
1316
1317 }
1318
1319
1320 /**
1321 * acfe_query_modules
1322 *
1323 * @param $query
1324 *
1325 * @return false|mixed
1326 */
1327 function acfe_query_modules($query = array()){
1328 return acf_get_store('acfe-modules')->query($query);
1329 }
1330
1331
1332 /**
1333 * acfe_get_module_by_post_type
1334 *
1335 * @param $post_type
1336 *
1337 * @return false|mixed
1338 */
1339 function acfe_get_module_by_post_type($post_type){
1340 return acfe_query_module(array('post_type' => $post_type));
1341 }
1342
1343
1344 /**
1345 * acfe_get_module_by_item
1346 *
1347 * @param $id
1348 *
1349 * @return false|mixed
1350 */
1351 function acfe_get_module_by_item($id){
1352
1353 // check array/object
1354 if(is_array($id) || is_object($id)){
1355 $id = acfe_maybe_get($id, 'ID');
1356 }
1357
1358 $id = absint($id);
1359 if(!$id){
1360 return false;
1361 }
1362
1363 return acfe_get_module_by_post_type(get_post_type($id));
1364 }
1365
1366 /**
1367 * acfe_is_module_v2_item
1368 *
1369 * @param $post_id
1370 *
1371 * @return bool
1372 */
1373 function acfe_is_module_v2_item($post_id){
1374
1375 $post = get_post($post_id);
1376 $meta_name = false;
1377
1378 // validate post
1379 if(!$post){
1380 return false;
1381 }
1382
1383 // define meta name
1384 switch($post->post_type){
1385
1386 case 'acfe-dbt': {
1387 $meta_name = 'name';
1388 break;
1389 }
1390
1391 case 'acfe-form': {
1392 $meta_name = 'acfe_form_name';
1393 break;
1394 }
1395
1396 case 'acfe-dop': {
1397 $meta_name = 'menu_slug';
1398 break;
1399 }
1400
1401 case 'acfe-dpt': {
1402 $meta_name = 'acfe_dpt_name';
1403 break;
1404 }
1405
1406 case 'acfe-dt': {
1407 $meta_name = 'acfe_dt_name';
1408 break;
1409 }
1410
1411 case 'acfe-template': {
1412 $meta_name = 'acfe_template_active';
1413 break;
1414 }
1415
1416 }
1417
1418 // validate meta name
1419 if(!$meta_name){
1420 return false;
1421 }
1422
1423 // get post meta
1424 $post_meta = get_post_meta($post_id, $meta_name, true);
1425
1426 // validate old meta name
1427 // empty and not 0
1428 if(acf_is_empty($post_meta)){
1429 return false;
1430 }
1431
1432 // get post content
1433 $post_content = maybe_unserialize($post->post_content);
1434 $post_content = acf_get_array($post_content);
1435
1436 // post content already set
1437 if($post_content){
1438 return false;
1439 }
1440
1441 // is module v2 item
1442 return true;
1443
1444 }