function __construct() { // register filter acf_enable_filter('local'); // actions add_action('acf/delete_field', array($this, 'acf_delete_field'), 20, 1); // filters add_filter('acf/get_field_groups', array($this, 'acf_get_field_groups'), 20, 1); }
function __construct() { // vars $this->name = 'clone'; $this->label = _x('Clone', 'noun', 'acf'); $this->category = 'layout'; $this->defaults = array('clone' => '', 'prefix_label' => 0, 'prefix_name' => 0, 'display' => 'seamless', 'layout' => 'block'); $this->cloning = array(); $this->replace = array(); // register filter acf_enable_filter('clone'); // ajax add_action('wp_ajax_acf/fields/clone/query', array($this, 'ajax_query')); // filters add_filter('acf/get_fields', array($this, 'acf_get_fields'), 5, 2); add_filter('acf/clone_field/type=clone', array($this, 'acf_clone_field'), 10, 2); // do not delete! parent::__construct(); }
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')); }
function acf_import_field_group($field_group) { // disable filters to ensure ACF loads raw data from DB acf_disable_filters(); // vars $ref = array(); $order = array(); // extract fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // remove old fields if ($field_group['ID']) { // load fields $db_fields = acf_get_fields_by_id($field_group['ID']); $db_fields = acf_prepare_fields_for_import($db_fields); // get field keys $keys = array(); foreach ($fields as $field) { $keys[] = $field['key']; } // loop over db fields foreach ($db_fields as $field) { // add to ref $ref[$field['key']] = $field['ID']; if (!in_array($field['key'], $keys)) { acf_delete_field($field['ID']); } } } // enable local filter for JSON to be created acf_enable_filter('local'); // save field group $field_group = acf_update_field_group($field_group); // add to ref $ref[$field_group['key']] = $field_group['ID']; // add to order $order[$field_group['ID']] = 0; // add fields foreach ($fields as $field) { // add ID if (!$field['ID'] && isset($ref[$field['key']])) { $field['ID'] = $ref[$field['key']]; } // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } elseif (isset($ref[$field['parent']])) { $field['parent'] = $ref[$field['parent']]; } // add field menu_order if (!isset($order[$field['parent']])) { $order[$field['parent']] = 0; } $field['menu_order'] = $order[$field['parent']]; $order[$field['parent']]++; // save field $field = acf_update_field($field); // add to ref $ref[$field['key']] = $field['ID']; } // return new field group return $field_group; }