Auto SyncFREE

Docs Field Groups Auto Sync

#PHP AutoSync

ACF Extended allows the automatic creation & inclusion of local PHP declaration for Field Groups. Inspired by the native ACF Json sync feature, this functionnality allow developers to take advantage of the speed of local PHP Field Groups, while maintaining field groups in the back-end.

To enable this feature, you’ll have to create a folder called /acfe-php/ in the active theme folder, then enable the “PHP Sync” setting in the targeted field group. Once activated, the field group will display “php” in the “Load” column, and the PHP Sync will be checked.

#Custom PHP AutoSync Save Path

/** 
 * acfe/settings/php_save
 * 
 * @string  $path         Save path
 * @array   $field_group  Field group settings
 */
filter('acfe/settings/php_save',                         $path);
filter('acfe/settings/php_save/all',                     $path, $field_group);
filter('acfe/settings/php_save/ID=122',                  $path, $field_group);
filter('acfe/settings/php_save/key=group_5f50bb1964cd4', $path, $field_group);
add_filter('acfe/settings/php_save/key=group_5f50bb1964cd4', 'my_acfe_php_save_point', 10, 2);
function my_acfe_php_save_point($path, $field_group){
    
    return get_stylesheet_directory() . '/my-php-folder';
    
}

#Custom PHP AutoSync Load Path

/**
 * acfe/settings/php_load
 * 
 * @array  $paths  Load paths
 */
filter('acfe/settings/php_load', $paths);
add_filter('acfe/settings/php_load', 'my_acfe_php_load_point');
function my_acfe_php_load_point($paths){
    
    // append path
    $paths[] = get_stylesheet_directory() . '/my-php-folder';
    
    // return
    return $paths;
    
}

#Should delete the PHP file

Control whenever the PHP file should be deleted when the field group is removed or the PHP Sync setting has been disabled.

/** 
 * acfe/settings/should_delete_php
 * 
 * @bool   $delete       Should delete PHP file
 * @array  $field_group  Field group settings
 */
filter('acfe/settings/should_delete_php',                         $delete, $field_group);
filter('acfe/settings/should_delete_php/ID=122',                  $delete, $field_group);
filter('acfe/settings/should_delete_php/key=group_5f50bb1964cd4', $delete, $field_group);
add_filter('acfe/settings/should_delete_php/key=group_5f50bb1964cd4', 'my_acfe_should_delete_php', 10, 2);
function my_acfe_should_delete_php($delete, $field_group){

    return false;

}

#Json AutoSync

By default, ACF save Field Groups declaration in Json format when the folder /acf-json/ has been created in the active theme. ACF Extended gives you the control over which Field Group should be automatically synced in Json. To enable the Json Sync, check the “Json Sync” setting in the targeted field group.

Note: By default, if a Json folder has been declared within the theme or the code, ACF Extended will automatically check the “Json Sync” setting on all newly created field groups, in order to stick to the native ACF behavior.

#Custom Json AutoSync Save Path

/**
 * acfe/settings/json_save
 * 
 * @string  $path         Save path
 * @array   $field_group  Field group settings
 */

filter('acfe/settings/json_save',                         $path);
filter('acfe/settings/json_save/all',                     $path, $field_group);
filter('acfe/settings/json_save/ID=122',                  $path, $field_group);
filter('acfe/settings/json_save/key=group_5f50bb1964cd4', $path, $field_group);
add_filter('acfe/settings/json_save/key=group_5f50bb1964cd4', 'my_acfe_json_save_point', 10, 2);
function my_acfe_json_save_point($path, $field_group){
    
    return get_stylesheet_directory() . '/my-json-folder';
    
}

#Custom Json AutoSync Load Path

/**
 * acfe/settings/json_load
 * 
 * @array  $paths  Load paths
 */

filter('acfe/settings/json_load', $paths);
add_filter('acfe/settings/json_load', 'my_acfe_json_load_point');
function my_acfe_json_load_point($paths){
    
    // append path
    $paths[] = get_stylesheet_directory() . '/my-json-folder';
    
    // return
    return $paths;
    
}

#Save Json files into the loading folder

Using the hooks above it is possible to automatically save the json files to their respective loading folders. This script is useful when developers have multiple directories & sub directories which load json files. Usage example:

add_filter('acfe/settings/json_save/all', 'my_acfe_save_json_where_its_loaded', 10, 2);
function my_acfe_save_json_where_its_loaded($path, $field_group){
    
    // get all local json files loaded
    $files = acf_get_local_json_files();
    
    // check if the field group is found in the local json files
    $load_path = acf_maybe_get($files, $field_group['key']);
    
    // local json file found
    if($load_path){
    
        // return the directory
        return dirname($load_path);
    
    }
    
    // return
    return $path;
    
}

#Should delete the Json file

Control whenever the Json file should be deleted when the field group is removed or the Json Sync setting has been disabled.

/**
 * acfe/settings/should_delete_json
 *
 * @bool   $delete       Should delete Json file
 * @array  $field_group  Field group settings
 */
filter('acfe/settings/should_delete_json',                         $delete, $field_group);
filter('acfe/settings/should_delete_json/ID=122',                  $delete, $field_group);
filter('acfe/settings/should_delete_json/key=group_5f50bb1964cd4', $delete, $field_group);
add_filter('acfe/settings/should_delete_json/key=group_5f50bb1964cd4', 'my_acfe_should_delete_json', 10, 2);
function my_acfe_should_delete_json($delete, $field_group){

    return false;

}

#Set Settings in PHP

It is possible to enable the PHP/Json Sync in PHP using the acf/load_field_group hook. Note that this hook is used everywhere, including in ACF admin and tools screen. Usage example:

add_filter('acf/load_field_group', 'my_acf_field_group');
function my_acf_field_group($field_group){
    
    // target a specific key
    if($field_group['key'] === 'group_5f20935b9a777'){
    
        // enable php + json sync
        $field_group['acfe_autosync'] = array('php', 'json');
    
    }
    
    // return
    return $field_group;
    
}

#Retrieve Settings

The PHP & Json AutoSync settings are saved in the Field Group array, under acfe_autosync key. It can be retrieved using acf_get_field_group(). Usage example:

$field_group = acf_get_field_group('group_5f20935b9a777');

/*
 * [instruction_placement] => label
 * [hide_on_screen] =>
 * ...
 * [acfe_autosync] => Array
 * (
 *     [0] => php
 *     [1] => json
 * )
 * ...
 */