function acf_get_fields($parent = false)
{
    // allow $parent to be a field group ID
    if (!is_array($parent)) {
        $parent = acf_get_field_group($parent);
    }
    // bail early if no parent
    if (!$parent) {
        return false;
    }
    // vars
    $fields = array();
    // try JSON before DB to save query time
    if (acf_have_local_fields($parent['key'])) {
        $fields = acf_get_local_fields($parent['key']);
    } else {
        $fields = acf_get_fields_by_id($parent['ID']);
    }
    // filter
    $fields = apply_filters('acf/get_fields', $fields, $parent);
    // return
    return $fields;
}
Exemple #2
0
 function remove_field($key)
 {
     // get field
     $field = acf_get_field($key);
     // remove parent reference
     $this->remove_parent_reference($field['parent'], $field['key']);
     // remove field
     unset($this->fields[$key]);
     // remove children
     if (acf_have_local_fields($key)) {
         acf_remove_local_fields($key);
     }
 }
Exemple #3
0
 function remove_field($key = '')
 {
     // get field
     $field = $this->get_field($key);
     // bail early if no field
     if (!$field) {
         return false;
     }
     // remove parent reference
     $this->remove_parent_reference($field['parent'], $field['key']);
     // remove field
     unset($this->fields[$key]);
     // remove children
     if (acf_have_local_fields($key)) {
         acf_remove_local_fields($key);
     }
 }
 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;
     }
     // import field group
     if ($key = acf_maybe_get($_GET, 'acfsync')) {
         // 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);
         // validate
         check_admin_referer('bulk-posts');
         // 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]);
         // redirect
         wp_redirect(admin_url($this->url . '&acfsynccomplete=' . $field_group['ID']));
         exit;
     } elseif (acf_maybe_get($_GET, 'action2') === 'acfsync') {
         // validate
         check_admin_referer('bulk-posts');
         // get ids
         $keys = acf_maybe_get($_GET, 'post');
         if (!empty($keys)) {
             // 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();
             foreach ($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'));
 }
Exemple #5
0
 function import()
 {
     // validate
     if (empty($_FILES['acf_import_file'])) {
         acf_add_admin_notice(__("No file selected", 'acf'), 'error');
         return;
     }
     // vars
     $file = $_FILES['acf_import_file'];
     // validate error
     if ($file['error']) {
         acf_add_admin_notice(__('Error uploading file. Please try again', 'acf'), 'error');
         return;
     }
     // validate type
     if (pathinfo($file['name'], PATHINFO_EXTENSION) !== 'json') {
         acf_add_admin_notice(__('Incorrect file type', 'acf'), 'error');
         return;
     }
     // read file
     $json = file_get_contents($file['tmp_name']);
     // decode json
     $json = json_decode($json, true);
     // validate json
     if (empty($json)) {
         acf_add_admin_notice(__('Import file empty', 'acf'), 'error');
         return;
     }
     // if importing an auto-json, wrap field group in array
     if (isset($json['key'])) {
         $json = array($json);
     }
     // vars
     $ids = array();
     $keys = array();
     $imported = array();
     // populate keys
     foreach ($json as $field_group) {
         // append key
         $keys[] = $field_group['key'];
     }
     // look for existing ids
     foreach ($keys as $key) {
         // attempt find ID
         $field_group = _acf_get_field_group_by_key($key);
         // bail early if no field group
         if (!$field_group) {
             continue;
         }
         // append
         $ids[$key] = $field_group['ID'];
     }
     // enable local
     acf_enable_local();
     // reset local (JSON class has already included .json field groups which may conflict)
     acf_reset_local();
     // add local field groups
     foreach ($json as $field_group) {
         // add field group
         acf_add_local_field_group($field_group);
     }
     // loop over keys
     foreach ($keys as $key) {
         // vars
         $field_group = acf_get_local_field_group($key);
         // attempt get id
         $id = acf_maybe_get($ids, $key);
         if ($id) {
             $field_group['ID'] = $id;
         }
         // append fields
         if (acf_have_local_fields($key)) {
             $field_group['fields'] = acf_get_local_fields($key);
         }
         // import
         $field_group = acf_import_field_group($field_group);
         // append message
         $imported[] = array('ID' => $field_group['ID'], 'title' => $field_group['title'], 'updated' => $id ? 1 : 0);
     }
     // messages
     if (!empty($imported)) {
         // vars
         $links = array();
         $count = count($imported);
         $message = sprintf(_n('Imported 1 field group', 'Imported %s field groups', $count, 'acf'), $count) . '.';
         // populate links
         foreach ($imported as $import) {
             $links[] = '<a href="' . admin_url("post.php?post={$import['ID']}&action=edit") . '" target="_blank">' . $import['title'] . '</a>';
         }
         // append links
         $message .= ' ' . implode(', ', $links);
         // add notice
         acf_add_admin_notice($message);
     }
 }