Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. // Set new ACF Save and Load points
  2. add_filter('acf/settings/save_json', 'set_acf_json_save_folder');
  3. function set_acf_json_save_folder( $path ) {
  4.     $path = dirname(__FILE__) . '/acf-json';
  5.     return $path;
  6. }
  7. add_filter('acf/settings/load_json', 'add_acf_json_load_folder');
  8. function add_acf_json_load_folder( $paths ) {
  9.     unset($paths[0]);
  10.     $paths[] = dirname(__FILE__) . '/acf-json';
  11.     return $paths;
  12. }
  13.  
  14. // Automatically import fields
  15. add_action( 'admin_init', 'article_gamification_sync_acf_fields' );
  16. function article_gamification_sync_acf_fields() {
  17.     // vars
  18.     $groups = acf_get_field_groups();
  19.     $sync   = array();
  20.     // bail early if no field groups
  21.     if( empty( $groups ) )
  22.     return;
  23.     // find JSON field groups which have not yet been imported
  24.     foreach( $groups as $group ) {
  25.  
  26.         // vars
  27.         $local      = acf_maybe_get( $group, 'local', false );
  28.         $modified   = acf_maybe_get( $group, 'modified', 0 );
  29.         $private    = acf_maybe_get( $group, 'private', false );
  30.         // ignore DB / PHP / private field groups
  31.         if( $local !== 'json' || $private ) {
  32.  
  33.             // do nothing
  34.  
  35.         } elseif( ! $group[ 'ID' ] ) {
  36.  
  37.             $sync[ $group[ 'key' ] ] = $group;
  38.  
  39.         } elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {
  40.  
  41.             $sync[ $group[ 'key' ] ]  = $group;
  42.         }
  43.     }
  44.     // bail if no sync needed
  45.     if( empty( $sync ) )
  46.     return;
  47.     if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {
  48.  
  49.         // vars
  50.         $new_ids = array();
  51.  
  52.         foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {
  53.  
  54.             // append fields
  55.             if( acf_have_local_fields( $key ) ) {
  56.  
  57.                 $sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );
  58.  
  59.             }
  60.             // import
  61.             $field_group = acf_import_field_group( $sync[ $key ] );
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement