StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
790 stars 61 forks source link

Transform acf-json to acf-builder #131

Open jnaklaas opened 3 years ago

jnaklaas commented 3 years ago

Is there by any chance a way to transform acf-json to acf-builder? I'm about to do a major update on a website with a ton of acf fields. Perhaps it might save some time if I could transform the existing fields to acf-builder.

I would write some nodejs to achieve this myself... but then I'm most likely not saving time :)

stevep commented 3 years ago

Thats a pretty interesting idea. There is no way for a FieldsBuilder class to import JSON currently. Depending on how much work you have to do to the existing fields, I would probably just convert the ones you really needed as you went, especially if you feel like you're gonna have to change them often.

davidwebca commented 2 years ago

Oh my the number of times I wish this existed. I'm leaving a comment to show my interest, but also might end up doing it 😂

voyou-sbeaulieu commented 1 year ago

Is there an other way around ? I noticed that with huge number of field, acf-builder is slower then acf-json. Maybe there's a way that instead of using "acf_add_local_field_group" we create / update .json from local-json from build() to speed up the TTFB/server time?

voyou-sbeaulieu commented 1 year ago

Hi just to add if this can help anyone, we change the acf_add_local_field_group and we now create a new json file from the build(). We do this only when options page is saved (as trigger) and not on all page load and this has really upgrade performance.

Here's the function

$local_json_path = function ($path) {
    // update path
    $path = config('theme.dir')  . '/app/acf-json';
    // return
    return $path;
};

add_filter('acf/settings/load_json', $local_json_path);

add_action('acf/save_post', function () {
    $screen = get_current_screen();
    if ($screen->id == $option_page_ID) { //This ID need to be you options page custom ID
        collect(glob(config('theme.dir') . '/app/fields/*.php'))->map(function ($field) {
            return require_once($field);
        })->map(function ($field) {
            if ($field instanceof FieldsBuilder) {
                $export = $field->build();
                if ($export && !empty($export)) {
                    $path = config('theme.dir')  . '/app/acf-json/';
                    file_put_contents($path . $export['key'] . '.json', json_encode($export, JSON_PRETTY_PRINT));
                }
            }
        });
    }
}, 20);