Created
October 1, 2014 18:26
-
-
Save thomascharbit/54fc4296fda3de4f4f52 to your computer and use it in GitHub Desktop.
ACF field groups sync for developers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define( 'ACF_FIELDS_VERSION', '1.0.1'); | |
if ( 'development' == WP_ENV ) { | |
// save fields to JSON | |
add_filter('acf/settings/save_json', 'lc_json_save_point'); | |
// Sync fields if nececessary | |
add_action('init', 'lc_check_acf_fields_version'); | |
} | |
else { | |
// don't show ACF UI | |
add_filter('acf/settings/show_admin', '__return_false'); | |
// load fields from JSON | |
add_filter('acf/settings/load_json', 'lc_json_load_point'); | |
} | |
// Sync fields if nececessary | |
add_action('init', 'lc_check_acf_fields_version'); | |
function lc_check_acf_fields_version() { | |
$db_version = get_option( 'acf_fields_version' ); | |
if ( version_compare( ACF_FIELDS_VERSION, $db_version ) > 0 ) { | |
lc_import_json_field_groups(); | |
update_option( 'acf_fields_version', ACF_FIELDS_VERSION ); | |
} | |
} | |
// Import field groups from local JSON to make them editable via Admin UI (dev mode) | |
function lc_import_json_field_groups() { | |
// tell ACF NOT to save to JSON | |
add_filter('acf/settings/save_json', 'lc_import_json_save_no_point', 99 ); | |
// Remove previous field groups in DB | |
$args = array( | |
'post_type' => 'acf-field-group', | |
'post_status' => 'any', | |
'posts_per_page' => -1 | |
); | |
$query = new WP_Query( $args ); | |
foreach ( $query->posts as $acf_group ) { | |
wp_delete_post( $acf_group->ID, true); | |
} | |
// Find local JSON directory | |
$dir = new DirectoryIterator( dirname(__FILE__) . '/fields' ); | |
foreach( $dir as $file ) { | |
if ( !$file->isDot() && 'json' == $file->getExtension() ) { | |
$json = json_decode( file_get_contents( $file->getPathname() ), true ); | |
// What follows is basically a copy of import() in ACF admin/settings-export.php | |
// if importing an auto-json, wrap field group in array | |
if( isset($json['key']) ) { | |
$json = array( $json ); | |
} | |
// vars | |
$added = array(); | |
$ignored = array(); | |
$ref = array(); | |
$order = array(); | |
foreach( $json as $field_group ) { | |
// remove fields | |
$fields = acf_extract_var($field_group, 'fields'); | |
// format fields | |
$fields = acf_prepare_fields_for_import( $fields ); | |
// save field group | |
$field_group = acf_update_field_group( $field_group ); | |
// add to ref | |
$ref[ $field_group['key'] ] = $field_group['ID']; | |
// add to order | |
$order[ $field_group['ID'] ] = 0; | |
// add fields | |
foreach( $fields as $field ) { | |
// add parent | |
if( empty($field['parent']) ) { | |
$field['parent'] = $field_group['ID']; | |
} elseif( isset($ref[ $field['parent'] ]) ) { | |
$field['parent'] = $ref[ $field['parent'] ]; | |
} | |
// add field menu_order | |
if( !isset($order[ $field['parent'] ]) ) { | |
$order[ $field['parent'] ] = 0; | |
} | |
$field['menu_order'] = $order[ $field['parent'] ]; | |
$order[ $field['parent'] ]++; | |
// save field | |
$field = acf_update_field( $field ); | |
} | |
} | |
} | |
} | |
} | |
function lc_import_json_save_no_point( $paths) { | |
return null; | |
} |
Hi, sorry for answering late, i missed your comment !
You can save this php file and include it from a custom plugin or functions.php.
You then need to increment the ACF_FIELDS_VERSION constant each time you make changes to your ACF Fields and next time someone else get your updated code, it will automatically pick up changes from JSON files and import them back in database, so everybody will be synchronized.
I since have made improvements and built a plugin from this, i will make it public soon.
Avoid using this gist , here is the plugin version : https://github.com/FreshFlesh/ACF-Sync
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
where would this script go? is it an addon to the plugin or in functions.php?
to confirm, this solves the problem where the acf admin does not pick up the changes from the json file?