function get_field_groups($field_groups) { // validate if (!acf_have_local_field_groups()) { return $field_groups; } // vars $ignore = array(); // overrride field groups and populate ignore list if (!empty($field_groups)) { foreach ($field_groups as $k => $group) { // override if (acf_is_local_field_group($group['key'])) { $field_groups[$k] = acf_get_local_field_group($group['key']); } $ignore[] = $group['key']; } } // append field groups $groups = acf_get_local_field_groups(); foreach ($groups as $group) { if (!in_array($group['key'], $ignore)) { $field_groups[] = $group; } } // order field groups based on menu_order, title $menu_order = array(); $title = array(); foreach ($field_groups as $key => $row) { $menu_order[$key] = $row['menu_order']; $title[$key] = $row['title']; } // sort the array with menu_order ascending array_multisort($menu_order, SORT_ASC, $title, SORT_ASC, $field_groups); // return return $field_groups; }
function _acf_get_field_group_by_key($key = '', $search_trash = false) { // vars $field_group = false; // try JSON before DB to save query time if (acf_is_local_field_group($key)) { $field_group = acf_get_local_field_group($key); // validate $field_group = acf_get_valid_field_group($field_group); // return return $field_group; } // vars $args = array('posts_per_page' => 1, 'post_type' => 'acf-field-group', 'orderby' => 'menu_order title', 'order' => 'ASC', 'suppress_filters' => false, 'acf_group_key' => $key); // search trash? if ($search_trash) { $args['post_status'] = 'publish, trash'; } // load posts $posts = get_posts($args); // validate if (empty($posts[0])) { return $field_group; } // load from ID $field_group = _acf_get_field_group_by_id($posts[0]->ID); // return return $field_group; }
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); } }