PluginProbe
Advanced Custom Fields: Extended / 0.9.1
Advanced Custom Fields: Extended v0.9.1
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 / modules / autosync-json.php
autosync-json.php
608 lines 15.3 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_AutoSync_Json')):
8
9 class ACFE_AutoSync_Json{
10
11 // vars
12 private $json_files = array();
13 public $old_path = false;
14
15 /**
16 * construct
17 */
18 function __construct(){
19
20 // hooks
21 add_action('acf/update_field_group', array($this, 'pre_update_field_group'), 9);
22 add_action('acf/untrash_field_group', array($this, 'pre_update_field_group'), 9);
23 add_action('acf/update_field_group', array($this, 'post_update_field_group'), 11);
24 add_action('acf/untrash_field_group', array($this, 'post_update_field_group'), 11);
25
26 add_action('acf/trash_field_group', array($this, 'pre_delete_field_group'), 9);
27 add_action('acf/delete_field_group', array($this, 'pre_delete_field_group'), 9);
28 add_action('acf/trash_field_group', array($this, 'post_delete_field_group'), 11);
29 add_action('acf/delete_field_group', array($this, 'post_delete_field_group'), 11);
30
31 // override settings
32 add_filter('acf/settings/json', array($this, 'override_json'), 5);
33 add_filter('acf/settings/save_json', array($this, 'override_json_save'), 5);
34 add_filter('acf/settings/load_json', array($this, 'override_json_load'), 5);
35
36 // setup
37 $this->setup_json();
38
39 }
40
41
42 /**
43 * override_json
44 *
45 * @param $value
46 *
47 * @return bool
48 */
49 function override_json($value){
50
51 // not very elegant, but it allows
52 // acf_update_setting('json', false) to work mid-page request
53 if($value === false){
54 return false;
55 }
56
57 return (bool) acf_get_setting('acfe/json', $value);
58 }
59
60
61 /**
62 * override_json_save
63 *
64 * @param $path
65 *
66 * @return mixed|null
67 */
68 function override_json_save($path){
69 return apply_filters('acfe/settings/json_save', $path);
70 }
71
72
73 /**
74 * override_json_load
75 *
76 * @param $paths
77 *
78 * @return array
79 */
80 function override_json_load($paths){
81 return (array) apply_filters('acfe/settings/json_load', $paths);
82 }
83
84
85 /**
86 * is_json_enabled
87 *
88 * @return bool
89 */
90 function is_json_enabled(){
91 return (bool) acf_get_setting('acfe/json');
92 }
93
94
95 /**
96 * setup_json
97 */
98 function setup_json(){
99
100 if($this->is_json_enabled()){
101 $this->scan_json_folders();
102 }
103
104 }
105
106
107 /**
108 * scan_json_folders
109 */
110 function scan_json_folders(){
111
112 // normalize paths
113 $paths = (array) acf_get_setting('load_json');
114
115 // loop paths
116 foreach($paths as $path){
117
118 // update setting
119 if(is_dir($path)){
120 acf_update_setting('acfe/json_found', true);
121 break;
122 }
123
124 }
125
126 // acf < 5.9 compatibility
127 if(acf_version_compare(acf_get_setting('version'), '<', '5.9')){
128 $this->scan_json_folders_compatibility();
129 }
130
131 }
132
133
134 /**
135 * scan_json_folders_compatibility
136 *
137 * ACF < 5.9 compatibility
138 *
139 * @return array
140 */
141 function scan_json_folders_compatibility(){
142
143 $json_files = array();
144
145 // get paths
146 $paths = (array) acf_get_setting('load_json');
147
148 foreach($paths as $path){
149
150 // validate folder
151 if(!is_dir($path)){
152 continue;
153 }
154
155 // validate files
156 $files = scandir($path);
157 if(!$files){
158 continue;
159 }
160
161 // loop files
162 foreach($files as $filename){
163
164 // ignore hidden files
165 if($filename[0] === '.'){
166 continue;
167 }
168
169 // ignore sub directories
170 $file = untrailingslashit( $path ) . '/' . $filename;
171 if(is_dir($file)){
172 continue;
173 }
174
175 // ignore non json files
176 $ext = pathinfo($filename, PATHINFO_EXTENSION);
177 if($ext !== 'json'){
178 continue;
179 }
180
181 // read json data
182 $json = json_decode(file_get_contents($file), true);
183 if(!is_array($json) || !isset($json['key'])){
184 continue;
185 }
186
187 // append data
188 $json_files[ $json['key'] ] = $file;
189
190 }
191
192 }
193
194 // store data and return
195 $this->json_files = $json_files;
196
197 // return
198 return $json_files;
199
200 }
201
202
203 /**
204 * get_json_files
205 *
206 * @return array
207 */
208 function get_json_files(){
209 return $this->json_files;
210 }
211
212
213 /**
214 * pre_update_field_group
215 *
216 * @param $field_group
217 */
218 function pre_update_field_group($field_group){
219
220 $this->old_path = false;
221
222 // json enabled
223 if(!$this->is_json_enabled()){
224 return;
225 }
226
227 // save file
228 if(acfe_has_json_sync($field_group)){
229
230 $old_path = untrailingslashit(acf_get_setting('save_json'));
231 $new_path = $this->get_json_save_path($field_group);
232
233 $this->old_path = $old_path;
234
235 // set setting for native acf/update_field_group:10
236 acf_update_setting('save_json', $new_path);
237
238
239 // delete file
240 }else{
241
242 // delete file
243 $this->delete_file($field_group);
244
245 // do not save json file
246 add_filter('acf/settings/json', array($this, '__return_false'));
247
248 }
249
250 }
251
252
253 /**
254 * post_update_field_group
255 *
256 * @param $field_group
257 */
258 function post_update_field_group($field_group){
259
260 // json enabled
261 if(!$this->is_json_enabled()){
262 return;
263 }
264
265 // save file
266 if(acfe_has_json_sync($field_group)){
267
268 // set setting back to native
269 acf_update_setting('save_json', $this->old_path);
270
271 // reset old path
272 $this->old_path = false;
273
274
275 // delete file
276 }else{
277
278 // reset json setting
279 remove_filter('acf/settings/json', array($this, '__return_false'));
280
281 }
282
283 }
284
285
286 /**
287 * pre_delete_field_group
288 *
289 * @param $field_group
290 *
291 * @return void
292 */
293 function pre_delete_field_group($field_group){
294
295 $this->old_path = false;
296
297 // json enabled
298 if(!$this->is_json_enabled()){
299 return;
300 }
301
302 // wp appends '__trashed' to end of 'key' (post_name)
303 $field_group['key'] = str_replace('__trashed', '', $field_group['key']);
304
305 // filters
306 $delete = true;
307 $delete = apply_filters("acfe/settings/should_delete_json", $delete, $field_group);
308 $delete = apply_filters("acfe/settings/should_delete_json/ID={$field_group['ID']}", $delete, $field_group);
309 $delete = apply_filters("acfe/settings/should_delete_json/key={$field_group['key']}", $delete, $field_group);
310
311 // do not delete json file
312 if(!$delete){
313 return add_filter('acf/settings/json', array($this, '__return_false'));
314 }
315
316 $old_path = untrailingslashit(acf_get_setting('save_json'));
317 $new_path = $this->get_json_save_path($field_group);
318
319 $this->old_path = $old_path;
320
321 // set setting for native acf/update_field_group:10
322 acf_update_setting('save_json', $new_path);
323
324
325 }
326
327
328 /**
329 * post_delete_field_group
330 *
331 * @param $field_group
332 *
333 * @return void
334 */
335 function post_delete_field_group($field_group){
336
337 // json enabled
338 if(!$this->is_json_enabled()){
339 return;
340 }
341
342 // reset json setting
343 remove_filter('acf/settings/json', array($this, '__return_false'));
344
345 if(!empty($this->old_path)){
346
347 // set setting back to native
348 acf_update_setting('save_json', $this->old_path);
349
350 // reset old path
351 $this->old_path = false;
352
353 }
354
355 }
356
357
358 /**
359 * delete_file
360 *
361 * @param $field_group
362 *
363 * @return void
364 */
365 function delete_file($field_group){
366
367 // validate
368 if(!$this->is_json_enabled()){
369 return;
370 }
371
372 // filters
373 $delete = true;
374 $delete = apply_filters("acfe/settings/should_delete_json", $delete, $field_group);
375 $delete = apply_filters("acfe/settings/should_delete_json/ID={$field_group['ID']}", $delete, $field_group);
376 $delete = apply_filters("acfe/settings/should_delete_json/key={$field_group['key']}", $delete, $field_group);
377
378 if(!$delete){
379 return;
380 }
381
382 // vars
383 $path = $this->get_json_save_path($field_group);
384 $filename = $this->get_filename($field_group);
385
386 if(!$filename){
387 return;
388 }
389
390 $file = untrailingslashit($path) . '/' . $filename;
391
392 // delete
393 if(is_readable($file)){
394 unlink($file);
395 }
396
397 }
398
399
400 /**
401 * get_filename
402 *
403 * @param $field_group
404 *
405 * @return false|string
406 */
407 function get_filename($field_group){
408
409 $version = acf_get_setting('version');
410
411 // acf 6.2 compatibility
412 if(acf_version_compare($version, '>=', '6.2') && acf_version_compare($version, '<', '6.3')){
413
414 $instance = acf_get_instance('ACF_Local_JSON');
415
416 if(method_exists($instance, 'get_files')){
417
418 $load_path = '';
419 $files = $instance->get_files();
420
421 if(is_array($files) && isset($files[ $field_group['key'] ])){
422 $load_path = $files[ $field_group['key'] ];
423 }
424
425 $filename = apply_filters('acf/json/save_file_name', $field_group['key'] . '.json', $field_group, $load_path);
426
427 if(!is_string($filename)){
428 return false;
429 }
430
431 $filename = sanitize_file_name($filename);
432
433 // sanitize_file_name() can potentially remove all characters.
434 if(empty($filename)){
435 return false;
436 }
437
438 return $filename;
439
440 }
441
442 // acf 6.3 compatibility
443 }elseif(acf_version_compare($version, '>=', '6.3')){
444
445 $instance = acf_get_instance('ACF_Local_JSON');
446
447 if(method_exists($instance, 'get_filename')){
448 return $instance->get_filename($field_group['key'], $field_group);
449 }
450
451 }
452
453 // default
454 return $field_group['key'] . '.json';
455
456 }
457
458
459 /**
460 * __return_false
461 *
462 * @return false
463 */
464 function __return_false(){
465 return false;
466 }
467
468
469 /**
470 * get_json_save_path
471 *
472 * @param $field_group
473 *
474 * @return mixed|null
475 */
476 function get_json_save_path($field_group){
477
478 // default
479 $path = untrailingslashit(acf_get_setting('acfe/json_save'));
480
481 // acf 6.2 compatibility
482 // added save_json variations
483 $path = acf_get_setting("save_json/type=acf-field-group", $path);
484 $path = acf_get_setting("save_json/name={$field_group['title']}", $path);
485 $path = acf_get_setting("save_json/key={$field_group['key']}", $path);
486
487 // filters
488 $path = apply_filters("acfe/settings/json_save/all", $path, $field_group);
489 $path = apply_filters("acfe/settings/json_save/ID={$field_group['ID']}", $path, $field_group);
490 $path = apply_filters("acfe/settings/json_save/key={$field_group['key']}", $path, $field_group);
491
492 return $path;
493
494 }
495
496 }
497
498 acf_new_instance('ACFE_AutoSync_Json');
499
500 endif;
501
502
503 /**
504 * acf_get_local_json_files
505 *
506 * @return mixed
507 */
508 if(!function_exists('acf_get_local_json_files') && acf_version_compare(acf_get_setting('version'), '<', '5.9')){
509
510 function acf_get_local_json_files(){
511 return acf_get_instance('ACFE_AutoSync_Json')->get_json_files();
512 }
513
514 }
515
516
517 /**
518 * acfe_is_sync_available
519 *
520 * @param $field_group
521 *
522 * @return bool
523 */
524 function acfe_is_sync_available($field_group){
525
526 $key = acf_maybe_get($field_group, 'key');
527 $id = acf_maybe_get($field_group, 'ID');
528
529 // bail early
530 if(empty($key) || empty($id)){
531 return false;
532 }
533
534 acf_enable_filter('local');
535
536 $field_group = acf_get_local_field_group($key);
537
538 acf_disable_filter('local');
539
540 if(!$field_group){
541 return false;
542 }
543
544 $private = acf_maybe_get($field_group, 'private', false);
545 $local = acf_maybe_get($field_group, 'local', false);
546 $modified = acf_maybe_get($field_group, 'modified', 0);
547
548 if($private || $local !== 'json'){
549 return false;
550 }
551
552 if($modified && $modified > get_post_modified_time('U', true, $id, true)){
553 return true;
554 }
555
556 return false;
557
558 }
559
560
561 /**
562 * acfe_has_json_sync
563 *
564 * @param $item
565 *
566 * @return bool
567 */
568 function acfe_has_json_sync($item){
569 return in_array('json', (array) acf_maybe_get($item, 'acfe_autosync', array()));
570 }
571
572
573 /**
574 * acfe_get_local_json_file
575 *
576 * @param $field_group
577 *
578 * @return false
579 */
580 function acfe_get_local_json_file($field_group){
581
582 $key = $field_group;
583
584 if(is_array($field_group) && isset($field_group['key'])){
585 $key = $field_group['key'];
586 }
587
588 $json_files = acf_get_local_json_files();
589
590 if(isset($json_files[ $key ])){
591 return $json_files[ $key ];
592 }
593
594 return false;
595
596 }
597
598
599 /**
600 * acfe_get_json_save_path
601 *
602 * @param $field_group
603 *
604 * @return mixed
605 */
606 function acfe_get_json_save_path($field_group){
607 return acf_get_instance('ACFE_AutoSync_Json')->get_json_save_path($field_group);
608 }