Support

Account

Home Forums ACF PRO Manually trigger acf-json sync

Solving

Manually trigger acf-json sync

  • Hey, we are using ACF in a team with git on so many sites. With version 5.3.1 and this thread the sync was improved but it’s still not perfect we think.

    If one of our teammembers commit some changes, all others have to manually sync before make any new changes.

    For us it would be perfect if we could trigger this sync with a function like acf_json_sync() or something. There are some plugins out there but it feels dirty because they all just copy large code blocks from the ACF source code because there is no standalone ACF api function for doing this.

    With such a function we could trigger this sync on every git pull for example. Alternatively an auto-sync mode would be nice to don’t need to sync at all.

    Cheers,
    Phil

  • Did some digging around in the code and it appears that could try

    acf_import_field_group($group_key);

    This is what is called by ACF if you click sync manually.

  • No, this is just a small part of the import functionality. Here is one of many examples of how to manually sync. There is much more than just call acf_import_field_group(). But unfortunately it’s 95% copied code from the ACF source and this feels unprofessional for me.

  • I hadn’t seen that and I didn’t dig that far. The second thread you pointed out might be the only way right now. I will mark this thread for the developer to look at if he finds time to get here. Maybe he’ll make an addition of create a hook to trigger it.

  • That would be perfect and an extremely timesaver for us! Thank you very much!

  • This is working well for me, with the option to make the loaded fields editable or not:

    /* Default ACF fields */
    function my_default_acf_fields() {
    	if ( ! acf_get_field_groups() ) {
    		$acf_json_data = @file_get_contents ( MY_PLUGIN_DIR . 'vendor/acf-json/my-acf-groups.json' );
    		if ( $acf_json_data && $custom_fields = json_decode( $acf_json_data, true ) ) {
    			foreach ( $custom_fields as $custom_field ) {
    				acf_import_field_group( $custom_field ); // editable
    				// acf_add_local_field_group( $custom_field ); // not editable
    			}
    		}
    	}
    }
    add_action( 'acf/init', 'my_default_acf_fields' );
  • I’m also in need for this …
    Please tell us
    I’m not fan of hacking code to do this, not sustainable.

  • Hi there,

    This is an old thread but if my code could be useful, i let it here.

    IMPORTANT : Please test it in a dev environment before.
    I adapted and didn’t test the multisite adaptations.
    It works on a single wordpress installation on ACF 5.1.5+ (tested on acf 6.2)

    Note 1 : i hooked on ‘admin_init’ but maybe there is a better hook.
    Note 2 : Tested on WP 6.2 only but i guess it would work far below as we only use get_post() and wp_update_post()

    Feel free to correct it and share it 🙂

    
    <?php
    //Modify ACF group/fields setting and save it globally (DB + local JSON)
    add_action('admin_init', 'modify_acf_settings');
    function modify_acf_settings() {
    	//Working on specific fields/posts/groups - REPLACE with your own keys
    	$acf_posts_to_update = array(1234);
    	$acf_groups_to_sync = array('group_12345678abcde'); //Used only if you're using local JSON
    	$acf_fields_to_update = array( 'field_12345678abcde' );
    	$stored_posts = array();
    	$stored_acf_groups = array();
    	
    	//You could modify any field settings from here
    	//@see https://www.advancedcustomfields.com/resources/acf-update_field/
    	foreach($acf_fields_to_update as $field) {
    		$is_field_exists = get_field_object($field);
    		if($is_field_exists !== false) {
    			$filter_identifier = strval('acf/update_field/key='.$field);
    			add_filter($filter_identifier, function($field) {
    				//$field['required'] = true;
    				//etc...
    			}, 10, 1);
    		}
    	}
    	
    	//Then we use WP native functions to modify ACF posts
    	$updated_post = false;
    	$i = 0;
    	foreach($acf_posts_to_update as $post_to_update) {
    		$post_obj = get_post($post_to_update);
    		//You could grab and modify any datas returned with get_post()
    		// Example -> $post_content = $post_obj->post_content;
    		
    		if($post_obj !== null) {
    			//Here i want to active a disable group if woocommerce is active
    			//All we have to do is change the post status (as ACF groups are basically custom post types)
    			if (&& in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    				$current_status = $post_obj->post_status;
    				if($current_status == 'acf-disabled') {
    					$post_obj->post_status = 'publish';
    				}
    				//Don't forget to update post and validate/re-serialize modified datas if needed
    				wp_update_post( $post_obj );
    				$updated_post = true;
    				$stored_post[$i] = $post_obj;
    			} else {
    				//Disable it if woocommerce is not active and the group is publish
    				$current_status = $post_obj->post_status;
    				if($current_status == 'publish') {
    					$post_obj->post_status = 'acf-disabled';
    				}
    				wp_update_post( $post_obj );
    				$updated_post = true;
    				$stored_post[$i] = $post_obj;
    			}
    		}
    	$i++;
    	}
    	
    	//Last step - Sync modified posts with JSON files
    	//note : using $updated_post but you could use any check
    	$update_modified_group = false;
    	if ( $updated_post === true ) {
    		$i = 0;
    		foreach($acf_groups_to_sync as $acf_group) {
    			$group_to_save = acf_get_field_group($acf_group);
    			$group_to_save['active'] = 1;
    			$update_modified_group = acf_write_json_field_group( $group_to_save );
    			//Add an admin notice is group was not savec correctly
    			if( boolval($update_modified_group) !== true ) {
    				add_action( 'admin_notices', 'acf_not_sync_notice' );
    			} else {
    				$stored_acf_groups[$i] = $group_to_save;
    			}
    		$i++;
    		}
    	}
    	
    	//Finally, we will sync changes in other $blog in a multisite network
    	$current_blog_id = get_current_blog_id();
    	$sites = get_sites( 
    		array( 
    			'number' => 50,
    			'site__not_in' => $current_blog_id, // exclude current site from the loop
    		) 
    	);
    	foreach( $sites as $site ) {
    		switch_to_blog( $site->blog_id );
    		if( is_array($stored_post) && !empty($stored_post) ) {
    			foreach($stored_post as $post) {
    				wp_update_post( $post );
    			}
    		}
    		
    		if( is_array($stored_acf_groups) && !empty($stored_acf_groups) ) {
    			foreach($stored_acf_groups as $group) {
    				$update_modified_group = acf_write_json_field_group( $group );
    			}
    		}
    	}
    	
    	//Don't forget to restore environnement
    	restore_current_blog();
    }
    
    function acf_not_sync_notice() {
        ?>
        <div class="notice error my-acf-notice is-dismissible" >
            <p><?php _e( 'Some ACF fields were not synced correctly. Please save the group fields manually.', 'my-text-domain' ); ?></p>
        </div>
    
        <?php
    }
Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Manually trigger acf-json sync’ is closed to new replies.