autosync.php
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')) |
| 4 | exit; |
| 5 | |
| 6 | if(!class_exists('ACFE_AutoSync')): |
| 7 | |
| 8 | class ACFE_AutoSync{ |
| 9 | |
| 10 | private $php_files = array(); |
| 11 | private $json_files = array(); |
| 12 | |
| 13 | function __construct(){ |
| 14 | |
| 15 | // PHP Save |
| 16 | add_action('acf/update_field_group', array($this, 'update_field_group')); |
| 17 | add_action('acf/untrash_field_group', array($this, 'update_field_group')); |
| 18 | |
| 19 | // PHP Delete |
| 20 | add_action('acf/trash_field_group', array($this, 'delete_field_group')); |
| 21 | add_action('acf/delete_field_group', array($this, 'delete_field_group')); |
| 22 | |
| 23 | |
| 24 | // Json Save |
| 25 | add_action('acf/update_field_group', array($this, 'pre_update_field_group_json'), 9); |
| 26 | add_action('acf/untrash_field_group', array($this, 'pre_update_field_group_json'), 9); |
| 27 | |
| 28 | add_action('acf/update_field_group', array($this, 'post_update_field_group_json'), 11); |
| 29 | add_action('acf/untrash_field_group', array($this, 'post_update_field_group_json'), 11); |
| 30 | |
| 31 | // Override Json |
| 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_php(); |
| 38 | $this->setup_json(); |
| 39 | |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Override: Json |
| 44 | */ |
| 45 | function override_json($value){ |
| 46 | return apply_filters('acfe/settings/json', $value); |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Override: Json Save |
| 51 | */ |
| 52 | function override_json_save($path){ |
| 53 | return apply_filters('acfe/settings/json_save', $path); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Override: Json Load |
| 58 | */ |
| 59 | function override_json_load($paths){ |
| 60 | return (array) apply_filters('acfe/settings/json_load', $paths); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Json Enabled |
| 65 | */ |
| 66 | function is_json_enabled(){ |
| 67 | return (bool) acf_get_setting('json'); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Json: Setup |
| 72 | */ |
| 73 | function setup_json(){ |
| 74 | |
| 75 | if(!$this->is_json_enabled()) |
| 76 | return; |
| 77 | |
| 78 | $this->scan_json_folders(); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Json: Scan |
| 84 | */ |
| 85 | function scan_json_folders(){ |
| 86 | |
| 87 | $paths = (array) acf_get_setting('load_json'); |
| 88 | |
| 89 | foreach($paths as $path){ |
| 90 | |
| 91 | if(!is_dir($path)) |
| 92 | continue; |
| 93 | |
| 94 | acf_update_setting('acfe/json_found', true); |
| 95 | break; |
| 96 | |
| 97 | } |
| 98 | |
| 99 | if(acf_version_compare(acf_get_setting('version'), '<', '5.9')){ |
| 100 | |
| 101 | $this->scan_json_folders_compatibility(); |
| 102 | |
| 103 | } |
| 104 | |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Json: Scan - Compatibility (ACF < 5.9) |
| 109 | */ |
| 110 | function scan_json_folders_compatibility(){ |
| 111 | |
| 112 | $json_files = array(); |
| 113 | |
| 114 | // Get paths |
| 115 | $paths = (array) acf_get_setting('load_json'); |
| 116 | |
| 117 | foreach($paths as $path){ |
| 118 | |
| 119 | if(!is_dir($path)) |
| 120 | continue; |
| 121 | |
| 122 | $files = scandir($path); |
| 123 | if(!$files) |
| 124 | continue; |
| 125 | |
| 126 | foreach($files as $filename){ |
| 127 | |
| 128 | // Ignore hidden files. |
| 129 | if($filename[0] === '.') |
| 130 | continue; |
| 131 | |
| 132 | // Ignore sub directories. |
| 133 | $file = untrailingslashit( $path ) . '/' . $filename; |
| 134 | if(is_dir($file)) |
| 135 | continue; |
| 136 | |
| 137 | // Ignore non JSON files. |
| 138 | $ext = pathinfo($filename, PATHINFO_EXTENSION); |
| 139 | if($ext !== 'json') |
| 140 | continue; |
| 141 | |
| 142 | // Read JSON data. |
| 143 | $json = json_decode(file_get_contents($file), true); |
| 144 | if(!is_array($json) || !isset($json['key'])) |
| 145 | continue; |
| 146 | |
| 147 | // Append data. |
| 148 | $json_files[$json['key']] = $file; |
| 149 | |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | // Store data and return. |
| 155 | $this->json_files = $json_files; |
| 156 | |
| 157 | return $json_files; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Json: Get Files |
| 163 | */ |
| 164 | function get_json_files(){ |
| 165 | return $this->json_files; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * PHP Enabled |
| 170 | */ |
| 171 | function is_php_enabled(){ |
| 172 | return (bool) acf_get_setting('acfe/php'); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * PHP: Setup |
| 177 | */ |
| 178 | function setup_php(){ |
| 179 | |
| 180 | if(!$this->is_php_enabled()) |
| 181 | return; |
| 182 | |
| 183 | global $pagenow; |
| 184 | $files = $this->scan_php_folders(); |
| 185 | |
| 186 | // Do not include PHP files in ACF Admin |
| 187 | if(($pagenow === 'edit.php' && acf_maybe_get_GET('post_type') === 'acf-field-group') || ($pagenow === 'post.php' && get_post_type(acf_maybe_get_GET('post')) === 'acf-field-group')) |
| 188 | return; |
| 189 | |
| 190 | foreach($files as $key => $file){ |
| 191 | |
| 192 | require_once($file); |
| 193 | |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * PHP: Scan |
| 200 | */ |
| 201 | function scan_php_folders(){ |
| 202 | |
| 203 | $php_files = array(); |
| 204 | $paths = (array) acf_get_setting('acfe/php_load'); |
| 205 | |
| 206 | foreach($paths as $path){ |
| 207 | |
| 208 | $path = untrailingslashit($path); |
| 209 | |
| 210 | if(!is_dir($path)) |
| 211 | continue; |
| 212 | |
| 213 | acf_update_setting('acfe/php_found', true); |
| 214 | |
| 215 | $files = glob($path . '/group_*.php'); |
| 216 | |
| 217 | if(!$files) |
| 218 | continue; |
| 219 | |
| 220 | foreach($files as $file){ |
| 221 | |
| 222 | $key = pathinfo($file, PATHINFO_FILENAME); |
| 223 | |
| 224 | // Append data. |
| 225 | $php_files[$key] = $file; |
| 226 | |
| 227 | } |
| 228 | |
| 229 | } |
| 230 | |
| 231 | // Store data and return. |
| 232 | $this->php_files = $php_files; |
| 233 | |
| 234 | return $php_files; |
| 235 | |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * PHP: Get Files |
| 240 | */ |
| 241 | public function get_php_files(){ |
| 242 | return $this->php_files; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * PHP: Update Field Group |
| 247 | */ |
| 248 | function update_field_group($field_group){ |
| 249 | |
| 250 | // Bail early |
| 251 | if(!$this->is_php_enabled()) |
| 252 | return; |
| 253 | |
| 254 | // Bail early |
| 255 | if(!acfe_has_php_sync($field_group)) |
| 256 | return; |
| 257 | |
| 258 | // Vars |
| 259 | $id = $field_group['ID']; |
| 260 | $key = $field_group['key']; |
| 261 | $path = untrailingslashit(acf_get_setting('acfe/php_save')); |
| 262 | |
| 263 | // Backup path |
| 264 | $new_path = $path; |
| 265 | |
| 266 | // Filters |
| 267 | $new_path = apply_filters("acfe/settings/php_save/all", $new_path, $field_group); |
| 268 | $new_path = apply_filters("acfe/settings/php_save/ID={$id}", $new_path, $field_group); |
| 269 | $new_path = apply_filters("acfe/settings/php_save/key={$key}", $new_path, $field_group); |
| 270 | |
| 271 | $diff = $path !== $new_path; |
| 272 | |
| 273 | // Custom path |
| 274 | if($diff) |
| 275 | acf_update_setting('acfe/php_save', $new_path); |
| 276 | |
| 277 | // Save File |
| 278 | $this->save_file($key, $field_group); |
| 279 | |
| 280 | // Restore |
| 281 | if($diff) |
| 282 | acf_update_setting('acfe/php_save', $path); |
| 283 | |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * PHP: Delete Field Group |
| 288 | */ |
| 289 | function delete_field_group($field_group){ |
| 290 | |
| 291 | // Bail early |
| 292 | if(!$this->is_php_enabled()) |
| 293 | return; |
| 294 | |
| 295 | // Bail early |
| 296 | if(!acfe_has_php_sync($field_group)) |
| 297 | return; |
| 298 | |
| 299 | // WP appends '__trashed' to end of 'key' (post_name). |
| 300 | $key = str_replace('__trashed', '', $field_group['key']); |
| 301 | |
| 302 | // Delete file. |
| 303 | $this->delete_file($key); |
| 304 | |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * PHP: Save File |
| 309 | */ |
| 310 | function save_file($key, $field_group){ |
| 311 | |
| 312 | $path = acf_get_setting('acfe/php_save'); |
| 313 | $file = untrailingslashit($path) . '/' . $key . '.php'; |
| 314 | |
| 315 | if(!is_writable($path)) |
| 316 | return false; |
| 317 | |
| 318 | // Translation |
| 319 | $l10n = acf_get_setting('l10n'); |
| 320 | $l10n_textdomain = acf_get_setting('l10n_textdomain'); |
| 321 | |
| 322 | if(!$l10n || !$l10n_textdomain){ |
| 323 | |
| 324 | $field_group['fields'] = acf_get_fields($field_group); |
| 325 | |
| 326 | }else{ |
| 327 | |
| 328 | acf_update_setting('l10n_var_export', true); |
| 329 | |
| 330 | $field_group = acf_translate_field_group($field_group); |
| 331 | |
| 332 | // Reset store to allow fields translation |
| 333 | $store = acf_get_store('fields'); |
| 334 | $store->reset(); |
| 335 | |
| 336 | $field_group['fields'] = acf_get_fields($field_group); |
| 337 | |
| 338 | acf_update_setting('l10n_var_export', false); |
| 339 | |
| 340 | } |
| 341 | |
| 342 | // prepare for export |
| 343 | $id = acf_extract_var($field_group, 'ID'); |
| 344 | $field_group = acf_prepare_field_group_for_export($field_group); |
| 345 | |
| 346 | // add modified time |
| 347 | $field_group['modified'] = get_post_modified_time('U', true, $id, true); |
| 348 | |
| 349 | // Prepare |
| 350 | $str_replace = array( |
| 351 | " " => "\t", |
| 352 | "'!!__(!!\'" => "__('", |
| 353 | "!!\', !!\'" => "', '", |
| 354 | "!!\')!!'" => "')", |
| 355 | "array (" => "array(" |
| 356 | ); |
| 357 | |
| 358 | $preg_replace = array( |
| 359 | '/([\t\r\n]+?)array/' => 'array', |
| 360 | '/[0-9]+ => array/' => 'array' |
| 361 | ); |
| 362 | |
| 363 | ob_start(); |
| 364 | |
| 365 | echo "<?php " . "\r\n" . "\r\n"; |
| 366 | echo "if( function_exists('acf_add_local_field_group') ):" . "\r\n" . "\r\n"; |
| 367 | |
| 368 | // code |
| 369 | $code = var_export($field_group, true); |
| 370 | |
| 371 | // change double spaces to tabs |
| 372 | $code = str_replace( array_keys($str_replace), array_values($str_replace), $code ); |
| 373 | |
| 374 | // correctly formats "=> array(" |
| 375 | $code = preg_replace( array_keys($preg_replace), array_values($preg_replace), $code ); |
| 376 | |
| 377 | // echo |
| 378 | echo "acf_add_local_field_group({$code});" . "\r\n" . "\r\n"; |
| 379 | |
| 380 | echo "endif;"; |
| 381 | |
| 382 | $output = ob_get_clean(); |
| 383 | |
| 384 | // Save and return true if bytes were written. |
| 385 | $result = file_put_contents($file, $output); |
| 386 | |
| 387 | return is_int($result); |
| 388 | |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * PHP: Delete File |
| 393 | */ |
| 394 | function delete_file($key){ |
| 395 | |
| 396 | $path = acf_get_setting('acfe/php_save'); |
| 397 | $file = untrailingslashit($path) . '/' . $key . '.php'; |
| 398 | |
| 399 | if(is_readable($file)){ |
| 400 | |
| 401 | unlink($file); |
| 402 | return true; |
| 403 | |
| 404 | } |
| 405 | |
| 406 | return false; |
| 407 | |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Json: Pre update Field Group |
| 412 | */ |
| 413 | function pre_update_field_group_json($field_group){ |
| 414 | |
| 415 | if(!$this->is_json_enabled()) |
| 416 | return; |
| 417 | |
| 418 | if(!acfe_has_json_sync($field_group)){ |
| 419 | |
| 420 | // Do not save json |
| 421 | add_filter('acf/settings/json', array($this, '__return_false')); |
| 422 | return; |
| 423 | |
| 424 | } |
| 425 | |
| 426 | // Vars |
| 427 | $id = $field_group['ID']; |
| 428 | $key = $field_group['key']; |
| 429 | $path = untrailingslashit(acf_get_setting('save_json')); |
| 430 | |
| 431 | // Backup |
| 432 | $new_path = $path; |
| 433 | |
| 434 | // Filters |
| 435 | $new_path = apply_filters("acfe/settings/json_save/all", $new_path, $field_group); |
| 436 | $new_path = apply_filters("acfe/settings/json_save/ID={$id}", $new_path, $field_group); |
| 437 | $new_path = apply_filters("acfe/settings/json_save/key={$key}", $new_path, $field_group); |
| 438 | |
| 439 | // Set custom saving point |
| 440 | if($path !== $new_path){ |
| 441 | |
| 442 | // Backup original path |
| 443 | $GLOBALS['acfe_json_original_path'] = $path; |
| 444 | |
| 445 | // Set custom path |
| 446 | acf_update_setting('save_json', $new_path); |
| 447 | |
| 448 | } |
| 449 | |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Json: Post Update Field Group |
| 454 | */ |
| 455 | function post_update_field_group_json($field_group){ |
| 456 | |
| 457 | if(!$this->is_json_enabled()) |
| 458 | return; |
| 459 | |
| 460 | // Json |
| 461 | if(!acfe_has_json_sync($field_group)){ |
| 462 | |
| 463 | // Original json setting |
| 464 | remove_filter('acf/settings/json', array($this, '__return_false')); |
| 465 | return; |
| 466 | |
| 467 | } |
| 468 | |
| 469 | if(isset($GLOBALS['acfe_json_original_path']) && !empty($GLOBALS['acfe_json_original_path'])){ |
| 470 | |
| 471 | // Restore original path |
| 472 | acf_update_setting('save_json', $GLOBALS['acfe_json_original_path']); |
| 473 | |
| 474 | // Remove backup |
| 475 | unset($GLOBALS['acfe_json_original_path']); |
| 476 | |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Custom Return False |
| 483 | */ |
| 484 | function __return_false(){ |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | |
| 490 | acf_new_instance('ACFE_AutoSync'); |
| 491 | |
| 492 | endif; |
| 493 | |
| 494 | /* |
| 495 | * Helper: Get PHP Files |
| 496 | */ |
| 497 | function acfe_get_local_php_files(){ |
| 498 | return acf_get_instance('ACFE_AutoSync')->get_php_files(); |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Helper: Get Json Files |
| 503 | */ |
| 504 | if(!function_exists('acf_get_local_json_files') && acf_version_compare(acf_get_setting('version'), '<', '5.9')){ |
| 505 | |
| 506 | function acf_get_local_json_files(){ |
| 507 | return acf_get_instance('ACFE_AutoSync')->get_json_files(); |
| 508 | } |
| 509 | |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Helper: Sync available |
| 514 | */ |
| 515 | function acfe_is_sync_available($field_group){ |
| 516 | |
| 517 | $key = acf_maybe_get($field_group, 'key'); |
| 518 | $id = acf_maybe_get($field_group, 'ID'); |
| 519 | |
| 520 | // Bail early |
| 521 | if(empty($key) || empty($id)) |
| 522 | return false; |
| 523 | |
| 524 | acf_enable_filter('local'); |
| 525 | |
| 526 | $field_group = acf_get_local_field_group($key); |
| 527 | |
| 528 | acf_disable_filter('local'); |
| 529 | |
| 530 | if(!$field_group) |
| 531 | return false; |
| 532 | |
| 533 | $private = acf_maybe_get($field_group, 'private', false); |
| 534 | $local = acf_maybe_get($field_group, 'local', false); |
| 535 | $modified = acf_maybe_get($field_group, 'modified', 0); |
| 536 | |
| 537 | if($private || $local !== 'json') |
| 538 | return false; |
| 539 | |
| 540 | if($modified && $modified > get_post_modified_time('U', true, $id, true)) |
| 541 | return true; |
| 542 | |
| 543 | return false; |
| 544 | |
| 545 | } |
| 546 | |
| 547 | function acfe_has_json_sync($field_group){ |
| 548 | return in_array('json', (array) acf_maybe_get($field_group, 'acfe_autosync', array())); |
| 549 | } |
| 550 | |
| 551 | function acfe_has_php_sync($field_group){ |
| 552 | return in_array('php', (array) acf_maybe_get($field_group, 'acfe_autosync', array())); |
| 553 | } |
| 554 | |
| 555 | function acfe_get_local_php_file($field_group){ |
| 556 | |
| 557 | $key = $field_group; |
| 558 | |
| 559 | if(is_array($field_group) && isset($field_group['key'])) |
| 560 | $key = $field_group['key']; |
| 561 | |
| 562 | $php_files = acfe_get_local_php_files(); |
| 563 | |
| 564 | if(isset($php_files[$key])){ |
| 565 | |
| 566 | return $php_files[$key]; |
| 567 | |
| 568 | } |
| 569 | |
| 570 | return false; |
| 571 | |
| 572 | } |
| 573 | |
| 574 | function acfe_get_local_json_file($field_group){ |
| 575 | |
| 576 | $key = $field_group; |
| 577 | |
| 578 | if(is_array($field_group) && isset($field_group['key'])) |
| 579 | $key = $field_group['key']; |
| 580 | |
| 581 | $json_files = acf_get_local_json_files(); |
| 582 | |
| 583 | if(isset($json_files[$key])){ |
| 584 | |
| 585 | return $json_files[$key]; |
| 586 | |
| 587 | } |
| 588 | |
| 589 | return false; |
| 590 | |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | function acfe_is_local_json($field_group){ |
| 595 | |
| 596 | $key = $field_group; |
| 597 | |
| 598 | if(is_array($field_group) && isset($field_group['key'])) |
| 599 | $key = $field_group['key']; |
| 600 | |
| 601 | if(!acf_is_local_field_group($key)) |
| 602 | return false; |
| 603 | |
| 604 | $field_group = acf_get_local_field_group($key); |
| 605 | |
| 606 | if(acf_maybe_get($field_group, 'local') === 'json') |
| 607 | return true; |
| 608 | |
| 609 | return false; |
| 610 | |
| 611 | } |
| 612 | |
| 613 | function acfe_is_local_php($field_group){ |
| 614 | |
| 615 | $key = $field_group; |
| 616 | |
| 617 | if(is_array($field_group) && isset($field_group['key'])) |
| 618 | $key = $field_group['key']; |
| 619 | |
| 620 | if(!acf_is_local_field_group($key)) |
| 621 | return false; |
| 622 | |
| 623 | $field_group = acf_get_local_field_group($key); |
| 624 | |
| 625 | if(acf_maybe_get($field_group, 'local') === 'php') |
| 626 | return true; |
| 627 | |
| 628 | return false; |
| 629 | |
| 630 | } |
| 631 | |
| 632 | function acfe_has_field_group_autosync($field_group, $type = false){ |
| 633 | |
| 634 | $acfe_autosync = (array) acf_maybe_get($field_group, 'acfe_autosync', array()); |
| 635 | |
| 636 | // Has something |
| 637 | if(!$type){ |
| 638 | |
| 639 | return !empty($acfe_autosync); |
| 640 | |
| 641 | } |
| 642 | |
| 643 | // Has Json |
| 644 | elseif($type === 'json'){ |
| 645 | |
| 646 | return in_array('json', $acfe_autosync); |
| 647 | |
| 648 | } |
| 649 | |
| 650 | // Has PHP |
| 651 | elseif($type === 'php'){ |
| 652 | |
| 653 | return in_array('php', $acfe_autosync); |
| 654 | |
| 655 | } |
| 656 | |
| 657 | return false; |
| 658 | |
| 659 | } |
| 660 | |
| 661 | function acfe_has_field_group_autosync_file($field_group, $type = 'json'){ |
| 662 | |
| 663 | if($type === 'json'){ |
| 664 | |
| 665 | $found = false; |
| 666 | $paths = (array) acf_get_setting('load_json', array()); |
| 667 | |
| 668 | // True if json file found |
| 669 | if(acf_is_local_field_group($field_group['key'])){ |
| 670 | |
| 671 | $local_field_group = acf_get_local_field_group($field_group['key']); |
| 672 | $get_local = acf_maybe_get($local_field_group, 'local', false); |
| 673 | |
| 674 | if($get_local === 'json') |
| 675 | $found = true; |
| 676 | |
| 677 | } |
| 678 | |
| 679 | if(!$found){ |
| 680 | |
| 681 | foreach($paths as $path){ |
| 682 | |
| 683 | $path = untrailingslashit($path); |
| 684 | $file = $field_group['key'] . '.json'; |
| 685 | |
| 686 | if(!is_readable("{$path}/{$file}")) |
| 687 | continue; |
| 688 | |
| 689 | $found = true; |
| 690 | break; |
| 691 | |
| 692 | } |
| 693 | |
| 694 | } |
| 695 | |
| 696 | return $found; |
| 697 | |
| 698 | } |
| 699 | |
| 700 | elseif($type === 'php'){ |
| 701 | |
| 702 | $php_files = acfe_get_local_php_files(); |
| 703 | |
| 704 | if(isset($php_files[$field_group['key']])){ |
| 705 | |
| 706 | return $php_files[$field_group['key']]; |
| 707 | |
| 708 | } |
| 709 | |
| 710 | return false; |
| 711 | |
| 712 | } |
| 713 | |
| 714 | return false; |
| 715 | |
| 716 | } |
| 717 | */ |