<?php if (!defined('ABSPATH')) { exit; } if (!class_exists('ACF_Local_JSON')) { class ACF_Local_JSON { private $files = array(); public function __construct() { acf_update_setting('save_json', get_stylesheet_directory() . '/acf-json'); acf_append_setting('load_json', get_stylesheet_directory() . '/acf-json'); add_action('acf/update_field_group', array($this, 'update_field_group')); add_action('acf/untrash_field_group', array($this, 'update_field_group')); add_action('acf/trash_field_group', array($this, 'delete_field_group')); add_action('acf/delete_field_group', array($this, 'delete_field_group')); add_action('acf/include_fields', array($this, 'include_fields')); } public function is_enabled() { return (bool) acf_get_setting('json'); } public function update_field_group($field_group) { if (!$this->is_enabled()) { return false; } $field_group['fields'] = acf_get_fields($field_group); $this->save_file($field_group['key'], $field_group); } public function delete_field_group($field_group) { if (!$this->is_enabled()) { return false; } $key = str_replace('__trashed', '', $field_group['key']); $this->delete_file($key); } public function include_fields() { if (!$this->is_enabled()) { return false; } $files = $this->scan_field_groups(); foreach ($files as $key => $file) { $json = json_decode(file_get_contents($file), true); $json['local'] = 'json'; $json['local_file'] = $file; acf_add_local_field_group($json); } } function scan_field_groups() { $json_files = array(); $paths = (array) acf_get_setting('load_json'); foreach ($paths as $path) { if (is_dir($path)) { $files = scandir($path); if ($files) { foreach ($files as $filename) { if ($filename[0] === '.') { continue; } $file = untrailingslashit($path) . '/' . $filename; if (is_dir($file)) { continue; } $ext = pathinfo($filename, PATHINFO_EXTENSION); if ($ext !== 'json') { continue; } $json = json_decode(file_get_contents($file), true); if (!is_array($json) || !isset($json['key'])) { continue; } $json_files[$json['key']] = $file; } } } } $this->files = $json_files; return $json_files; } public function get_files() { return $this->files; } public function save_file($key, $field_group) { $path = acf_get_setting('save_json'); $file = untrailingslashit($path) . '/' . $key . '.json'; if (!is_writable($path)) { return false; } if ($field_group['ID']) { $field_group['modified'] = get_post_modified_time('U', true, $field_group['ID']); } else { $field_group['modified'] = strtotime('now'); } $field_group = acf_prepare_field_group_for_export($field_group); $result = file_put_contents($file, acf_json_encode($field_group)); return is_int($result); } public function delete_file($key) { $path = acf_get_setting('save_json'); $file = untrailingslashit($path) . '/' . $key . '.json'; if (is_readable($file)) { unlink($file); return true; } return false; } public function include_json_folders() { _deprecated_function(__METHOD__, '5.9.0', 'ACF_Local_JSON::include_fields()'); $this->include_fields(); } public function include_json_folder($path = '') { _deprecated_function(__METHOD__, '5.9.0'); } } acf_new_instance('ACF_Local_JSON'); } function acf_get_local_json_files() { return acf_get_instance('ACF_Local_JSON')->get_files(); } function acf_write_json_field_group($field_group) { return acf_get_instance('ACF_Local_JSON')->save_file($field_group['key'], $field_group); } function acf_delete_json_field_group($key) { return acf_get_instance('ACF_Local_JSON')->delete_file($key); }