Support

Account

Home Forums ACF PRO Manually trigger acf-json sync Reply To: Manually trigger acf-json sync

  • 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
    }