acf_sync_import_json_field_groups

The ACF Pro JSON Sync acf sync import json field groups function.

Description

acf_sync_import_json_field_groups( $directory ); 

Parameters (1)

0. $directory
The directory.

Usage

  1. if ( !function_exists( 'acf_sync_import_json_field_groups' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'acf-pro-json-sync/acf-sync.php'; 
  3.  
  4. // The directory. 
  5. $directory = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = acf_sync_import_json_field_groups($directory); 
  9.  

Defined (1)

The function is defined in the following location(s).

/acf-sync.php  
  1. function acf_sync_import_json_field_groups($directory) { 
  2.  
  3. $dir = new DirectoryIterator( $directory ); 
  4.  
  5. foreach( $dir as $file ) { 
  6.  
  7. if ( !$file->isDot() && $file->getExtension() == 'json' ) { 
  8.  
  9. $json = json_decode( file_get_contents( $file->getPathname() ), true ); 
  10.  
  11. // What follows is basically a copy of import() in ACF admin/settings-export.php 
  12.  
  13. // if importing an auto-json, wrap field group in array 
  14. if( isset($json['key']) ) { 
  15.  
  16. $json = array( $json ); 
  17.  
  18.  
  19. // vars 
  20. $added = array(); 
  21. $ignored = array(); 
  22. $ref = array(); 
  23. $order = array(); 
  24.  
  25. foreach( $json as $field_group ) { 
  26.  
  27. // remove fields 
  28. $fields = acf_extract_var($field_group, 'fields'); 
  29.  
  30.  
  31. // format fields 
  32. $fields = acf_prepare_fields_for_import( $fields ); 
  33.  
  34.  
  35. // save field group 
  36. $field_group = acf_update_field_group( $field_group ); 
  37.  
  38.  
  39. // add to ref 
  40. $ref[ $field_group['key'] ] = $field_group['ID']; 
  41.  
  42.  
  43. // add to order 
  44. $order[ $field_group['ID'] ] = 0; 
  45.  
  46.  
  47. // add fields 
  48. foreach( $fields as $field ) { 
  49.  
  50. // add parent 
  51. if( empty($field['parent']) ) { 
  52.  
  53. $field['parent'] = $field_group['ID']; 
  54.  
  55. } elseif( isset($ref[ $field['parent'] ]) ) { 
  56.  
  57. $field['parent'] = $ref[ $field['parent'] ]; 
  58.  
  59.  
  60. // add field menu_order 
  61. if( !isset($order[ $field['parent'] ]) ) { 
  62.  
  63. $order[ $field['parent'] ] = 0; 
  64.  
  65.  
  66. $field['menu_order'] = $order[ $field['parent'] ]; 
  67. $order[ $field['parent'] ]++; 
  68.  
  69. // save field 
  70. $field = acf_update_field( $field ); 
  71.  
  72.  
  73.