function acf_import_field_group($field_group) { // 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']) { // disable local - important as to avoid 'acf_get_fields_by_id' returning fields with ID = 0 acf_disable_local(); // 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 - important as to allow local to find new fields and save json file acf_enable_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; }
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); } }