Skip to content

Instantly share code, notes, and snippets.

@elliotcondon
Created October 4, 2016 03:51
Show Gist options
  • Save elliotcondon/ebac045330753710e4ce379d036a2185 to your computer and use it in GitHub Desktop.
Save elliotcondon/ebac045330753710e4ce379d036a2185 to your computer and use it in GitHub Desktop.
/*
* check_sync
*
* This function will check for any $_GET data to sync
*
* @type function
* @date 9/12/2014
* @since 5.1.5
*
* @param n/a
* @return n/a
*/
function check_sync() {
// message
if( $ids = acf_maybe_get($_GET, 'acfsynccomplete') ) {
// explode
$ids = explode(',', $ids);
$total = count($ids);
if( $total == 1 ) {
acf_add_admin_notice( sprintf(__('Field group synchronised. %s', 'acf'), '<a href="' . get_edit_post_link($ids[0]) . '">' . get_the_title($ids[0]) . '</a>') );
} else {
acf_add_admin_notice( sprintf(_n( '%s field group synchronised.', '%s field groups synchronised.', $total, 'acf' ), $total) );
}
}
// vars
$groups = acf_get_field_groups();
// bail early if no field groups
if( empty($groups) ) return;
// find JSON field groups which have not yet been imported
foreach( $groups as $group ) {
// vars
$local = acf_maybe_get($group, 'local', false);
$modified = acf_maybe_get($group, 'modified', 0);
$private = acf_maybe_get($group, 'private', false);
// ignore DB / PHP / private field groups
if( $local !== 'json' || $private ) {
// do nothing
} elseif( !$group['ID'] ) {
$this->sync[ $group['key'] ] = $group;
} elseif( $modified && $modified > get_post_modified_time('U', true, $group['ID'], true) ) {
$this->sync[ $group['key'] ] = $group;
}
}
// bail if no sync needed
if( empty($this->sync) ) return;
// maybe sync
$sync_keys = array();
// check single
if( $key = acf_maybe_get($_GET, 'acfsync') ) {
$sync_keys[] = $key;
// check multiple
} elseif( acf_maybe_get($_GET, 'action2') === 'acfsync' ) {
$sync_keys = acf_maybe_get($_GET, 'post');
}
// sync
if( !empty($sync_keys) ) {
// validate
check_admin_referer('bulk-posts');
// disable filters to ensure ACF loads raw data from DB
acf_disable_filters();
acf_enable_filter('local');
// disable JSON
// - this prevents a new JSON file being created and causing a 'change' to theme files - solves git anoyance
acf_update_setting('json', false);
// vars
$new_ids = array();
// loop
foreach( $sync_keys as $key ) {
// append fields
if( acf_have_local_fields($key) ) {
$this->sync[ $key ]['fields'] = acf_get_local_fields( $key );
}
// import
$field_group = acf_import_field_group( $this->sync[ $key ] );
// append
$new_ids[] = $field_group['ID'];
}
// redirect
wp_redirect( admin_url( $this->url . '&acfsynccomplete=' . implode(',', $new_ids)) );
exit;
}
// filters
add_filter('views_edit-acf-field-group', array($this, 'list_table_views'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment