Skip to content

Instantly share code, notes, and snippets.

@amjad
Forked from superpositif/jp_sync_acf_fields.php
Last active October 1, 2021 09:42
Show Gist options
  • Save amjad/0d8bd7e9108001defc5fed546787a17b to your computer and use it in GitHub Desktop.
Save amjad/0d8bd7e9108001defc5fed546787a17b to your computer and use it in GitHub Desktop.
Automatically update Advanced Custom Fields field groups via JSON
<?php
/**
*
* Function that will automatically remove ACF field groups via JSON file update
*
*/
function acf_auto_suppr($json_dirs) {
$groups = acf_get_field_groups();
if (empty($groups)) {
return;
}
// sync groups that have been deleted
if (!is_array($json_dirs) || !$json_dirs) {
throw new \Exception('JSON dirs missing');
}
$delete = array();
foreach ($groups as $group) {
$found = false;
foreach ($json_dirs as $json_dir) {
$json_file = rtrim($json_dir, '/') . '/' . $group['key'] . '.json';
if (is_file($json_file)) {
$found = true;
break;
}
}
if (!$found) {
$delete[] = $group['key'];
}
}
if (!empty($delete)) {
foreach ($delete as $group_key) {
acf_delete_field_group($group_key);
}
}
}
/**
* Function that will automatically update ACF field groups via JSON file update.
*
* @link http://www.advancedcustomfields.com/resources/synchronized-json/
*/
function acf_auto_synch($json_dirs) {
$groups = acf_get_field_groups();
if (empty($groups)) {
return;
}
// find JSON field groups which have not yet been imported
$sync = array();
foreach ($groups as $group) {
$local = acf_maybe_get($group, 'local', false);
$modified = acf_maybe_get($group, 'modified', 0);
$private = acf_maybe_get($group, 'private', false);
// ignore DB / PHP / private field groups
if ($local !== 'json' || $private) {
// do nothing
} elseif (! $group['ID']) {
$sync[$group['key']] = $group;
} elseif ($modified && $modified > get_post_modified_time('U', true, $group['ID'], true)) {
$sync[$group['key']] = $group;
}
}
if (empty($sync)) {
return;
}
foreach ($sync as $key => $group) { //foreach ($keys as $key) {
// append fields
if (acf_have_local_fields($key)) {
$group['fields'] = acf_get_fields($key);
}
// import
$field_group = acf_import_field_group($group);
}
}
/**
* save a json when the group is validate in admin
*/
add_filter('acf/settings/save_json', function () {
$path = get_stylesheet_directory() . '/myjsonfolder';
return $path;
});
/**
* this function alerts about avalaibles synch
*/
add_filter('acf/settings/load_json', function ($paths) {
$path = get_stylesheet_directory() . '/myjsonfolder';
$paths[0] = $path;
return $paths;
});
/**
* run the auto synch
*/
add_action('init', function() {
$path = get_stylesheet_directory() . '/myjsonfolder';
$json_dirs = array($path);
acf_auto_suppr($json_dirs);
acf_auto_synch($json_dirs);
});
@mnngithub
Copy link

Hi,

Does this hook do any changes to the json file itself? My json gets modified. Shouldn't it just trigger auto sync?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment