/acf-sync.php

  1. <?php  
  2. /** 
  3. Plugin Name: ACF Pro JSON Sync 
  4. Description: Sync Advanced Custom Fields fields stored in json files into the database. Ideal for using ACF in multiple environments or in a team setting. 
  5. Version: 1.0.1 
  6. Author: O3 World 
  7. Author URI: http://o3world.com 
  8. */ 
  9.  
  10.  
  11.  
  12.  
  13. function acf_sync_check_json_dirs() { 
  14. //Get all json save points 
  15. $json_dirs = acf_get_setting('load_json'); 
  16.  
  17. $missing_dirs = array(); 
  18.  
  19. foreach ( $json_dirs as $dir ) { 
  20. if (!file_exists($dir)) { 
  21. $missing_dirs[] = $dir; 
  22.  
  23. if(!empty($missing_dirs)) { 
  24. return $missing_dirs; 
  25.  
  26. return false; 
  27.  
  28. function acf_sync_import_json_folder() { 
  29. // tell ACF NOT to save to JSON 
  30. add_filter('acf/settings/save_json', 'acf_sync_import_json_no_save', 99 ); 
  31.  
  32.  
  33. // Remove previous field groups in DB 
  34. $args = array( 
  35. 'post_type' => 'acf-field-group',  
  36. 'post_status' => 'any',  
  37. 'posts_per_page' => -1 
  38. ); 
  39.  
  40. $query = new WP_Query( $args ); 
  41.  
  42. foreach ( $query->posts as $acf_group ) { 
  43. wp_delete_post( $acf_group->ID, true); 
  44.  
  45. //Get all json save points 
  46. $json_dirs = acf_get_setting('load_json'); 
  47.  
  48. foreach ( $json_dirs as $dir ) { 
  49. acf_sync_import_json_field_groups($dir); 
  50.  
  51.  
  52.  
  53. // Import field groups from local JSON to make them editable via Admin UI (dev mode) 
  54. function acf_sync_import_json_field_groups($directory) { 
  55.  
  56. $dir = new DirectoryIterator( $directory ); 
  57.  
  58. foreach( $dir as $file ) { 
  59.  
  60. if ( !$file->isDot() && $file->getExtension() == 'json' ) { 
  61.  
  62. $json = json_decode( file_get_contents( $file->getPathname() ), true ); 
  63.  
  64. // What follows is basically a copy of import() in ACF admin/settings-export.php 
  65.  
  66. // if importing an auto-json, wrap field group in array 
  67. if( isset($json['key']) ) { 
  68.  
  69. $json = array( $json ); 
  70.  
  71.  
  72. // vars 
  73. $added = array(); 
  74. $ignored = array(); 
  75. $ref = array(); 
  76. $order = array(); 
  77.  
  78. foreach( $json as $field_group ) { 
  79.  
  80. // remove fields 
  81. $fields = acf_extract_var($field_group, 'fields'); 
  82.  
  83.  
  84. // format fields 
  85. $fields = acf_prepare_fields_for_import( $fields ); 
  86.  
  87.  
  88. // save field group 
  89. $field_group = acf_update_field_group( $field_group ); 
  90.  
  91.  
  92. // add to ref 
  93. $ref[ $field_group['key'] ] = $field_group['ID']; 
  94.  
  95.  
  96. // add to order 
  97. $order[ $field_group['ID'] ] = 0; 
  98.  
  99.  
  100. // add fields 
  101. foreach( $fields as $field ) { 
  102.  
  103. // add parent 
  104. if( empty($field['parent']) ) { 
  105.  
  106. $field['parent'] = $field_group['ID']; 
  107.  
  108. } elseif( isset($ref[ $field['parent'] ]) ) { 
  109.  
  110. $field['parent'] = $ref[ $field['parent'] ]; 
  111.  
  112.  
  113. // add field menu_order 
  114. if( !isset($order[ $field['parent'] ]) ) { 
  115.  
  116. $order[ $field['parent'] ] = 0; 
  117.  
  118.  
  119. $field['menu_order'] = $order[ $field['parent'] ]; 
  120. $order[ $field['parent'] ]++; 
  121.  
  122. // save field 
  123. $field = acf_update_field( $field ); 
  124.  
  125.  
  126.  
  127.  
  128. function acf_sync_get_fields_list() { 
  129. $json_dirs = acf_get_setting('load_json'); 
  130.  
  131. $json_files = array(); 
  132.  
  133. foreach ( $json_dirs as $dir ) { 
  134. if (file_exists($dir)) { 
  135. $files = new DirectoryIterator( $dir ); 
  136. foreach( $files as $file ) { 
  137. if ( !$file->isDot() && $file->getExtension() == 'json' ) { 
  138. $path = $file->getPathname(); 
  139. $path_uri = strstr($path, '/wp-content'); 
  140.  
  141. $contents = file_get_contents($path); 
  142. $json = json_decode($contents, true); 
  143. $title = $json['title']; 
  144.  
  145. $json_files[] = array('title'=>$title, 'path'=>$path_uri); 
  146. return $json_files; 
  147.  
  148.  
  149. function acf_sync_import_json_no_save($paths) { 
  150. return null; 
  151.  
  152.  
  153. function acf_sync_admin_css() { 
  154. wp_register_style( 'acf_sync_css', plugins_url( 'css/acf-sync.css', __FILE__ ), false, '1.0.0' ); 
  155. wp_enqueue_style( 'acf_sync_css' ); 
  156. add_action( 'admin_enqueue_scripts', 'acf_sync_admin_css' ); 
  157.  
  158.  
  159. function acf_sync_menu_page() { 
  160. add_menu_page( 'Sync Advanced Custom Fields', 'Sync ACF', 'manage_options', 'sync_acf', 'acf_sync_page', '', 111 );  
  161. add_action( 'admin_menu', 'acf_sync_menu_page' ); 
  162.  
  163.  
  164. function acf_sync_submenu_page() { 
  165. add_submenu_page( 'sync_acf', 'Run ACF Sync', 'Run Sync', 'manage_options', 'run_acf_sync', 'run_acf_sync' );  
  166. add_action('admin_menu', 'acf_sync_submenu_page'); 
  167.  
  168.  
  169.  
  170. function acf_sync_page() { 
  171. include 'views/sync.php'; 
  172.  
  173.  
  174. function run_acf_sync() { 
  175. acf_sync_import_json_folder(); 
  176. include 'views/sync-complete.php'; 
.