function relationships_import_json_field_groups() { // tell ACF NOT to save to JSON add_filter('acf/settings/save_json', 'lc_import_json_save_no_point', 99); // Remove previous field groups in DB $args = array('post_type' => 'acf-field-group', 'post_status' => 'any', 'posts_per_page' => -1); $query = new WP_Query($args); foreach ($query->posts as $acf_group) { wp_delete_post($acf_group->ID, true); } // Find local JSON directory $dir = new DirectoryIterator(dirname(__FILE__) . '/json'); foreach ($dir as $file) { if (!$file->isDot() && 'json' == $file->getExtension()) { $json = json_decode(file_get_contents($file->getPathname()), true); // What follows is basically a copy of import() in ACF admin/settings-export.php // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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); } } } } }
function acf_sync_import_json_field_groups($directory) { $dir = new DirectoryIterator($directory); foreach ($dir as $file) { if (!$file->isDot() && $file->getExtension() == 'json') { $json = json_decode(file_get_contents($file->getPathname()), true); // What follows is basically a copy of import() in ACF admin/settings-export.php // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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); } } } } }
/** * Import (overwrite) field groups into DB * @param string $params['name'] * @param string $params['group'] * @param string $params['old_value'] The previous settings data * @param string $params['new_value'] The new settings data */ function acf_pull($params) { $field_group = $params['new_value']; if ($existing_group = acf_get_field_group($field_group['key'])) { $field_group['ID'] = $existing_group['ID']; $existing_fields = acf_get_fields($existing_group); // Remove fields foreach ($existing_fields as $field) { wp_delete_post($field['ID'], true); } } // extract fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 $index => $field) { // add parent if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } else { if (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']; } }
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']) { $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']); } } } // 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 _migrate_field_group_500($ofg) { // global global $wpdb; // get post status $post_status = $ofg->post_status; // create new field group $nfg = array('ID' => 0, 'title' => $ofg->post_title, 'menu_order' => $ofg->menu_order); // location rules $groups = array(); // get all rules $rules = get_post_meta($ofg->ID, 'rule', false); if (is_array($rules)) { $group_no = 0; foreach ($rules as $rule) { // if field group was duplicated, it may now be a serialized string! $rule = maybe_unserialize($rule); // does this rule have a group? // + groups were added in 4.0.4 if (!isset($rule['group_no'])) { $rule['group_no'] = $group_no; // sperate groups? if (get_post_meta($ofg->ID, 'allorany', true) == 'any') { $group_no++; } } // extract vars $group = acf_extract_var($rule, 'group_no'); $order = acf_extract_var($rule, 'order_no'); // add to group $groups[$group][$order] = $rule; // sort rules ksort($groups[$group]); } // sort groups ksort($groups); } $nfg['location'] = $groups; // settings if ($position = get_post_meta($ofg->ID, 'position', true)) { $nfg['position'] = $position; } if ($layout = get_post_meta($ofg->ID, 'layout', true)) { $nfg['layout'] = $layout; } if ($hide_on_screen = get_post_meta($ofg->ID, 'hide_on_screen', true)) { $nfg['hide_on_screen'] = maybe_unserialize($hide_on_screen); } // Note: acf_update_field_group will call the acf_get_valid_field_group function and apply 'compatibility' changes // add old ID reference $nfg['old_ID'] = $ofg->ID; // save field group $nfg = acf_update_field_group($nfg); // trash? if ($post_status == 'trash') { acf_trash_field_group($nfg['ID']); } // return return $nfg; }
function acf_duplicate_field_group($selector = 0, $new_post_id = 0) { // disable JSON to avoid conflicts between DB and JSON acf_disable_local(); // load the origional field gorup $field_group = acf_get_field_group($selector); // bail early if field group did not load correctly if (empty($field_group)) { return false; } // keep backup of field group $orig_field_group = $field_group; // update ID $field_group['ID'] = $new_post_id; $field_group['key'] = uniqid('group_'); // add (copy) if (!$new_post_id) { $field_group['title'] .= ' (' . __("copy", 'acf') . ')'; } // save $field_group = acf_update_field_group($field_group); // get fields $fields = acf_get_fields($orig_field_group); // duplicate fields acf_duplicate_fields($fields, $field_group['ID']); // action for 3rd party customization do_action('acf/duplicate_field_group', $field_group); // return return $field_group; }
/** * An altered version of acf/admin/settings-tool.php::import() * * ACF's import function takes care of most things, but this * function makes up for what it lacks: * * - Removes fields that are in the DB, but not in the import file * - Removes field groups that are in the DB, but not in the import file * - Updates fields that have changed * - Displays command-line messages * * @return null */ protected function importFields() { // 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 $added = array(); $deletedgroups = array(); $deletedfields = array(); $ref = array(); $order = array(); $allgroups = $this->getFieldGroups(); $allfields = $this->getFields(); foreach ($json as $field_group) { $update = false; // check if field group exists if ($post = acf_get_field_group($field_group['key'], true)) { // \WP_CLI::log($field_group['title'] . " group already exists. Updating."); // add ID to trigger update instead of insert $field_group["ID"] = $post["ID"]; $update = true; // } else { // \WP_CLI::log($field_group['title'] . " group is new. Adding."); } // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // save field group $field_group = acf_update_field_group($field_group); // remove group from $allgroups array if (isset($allgroups[$field_group['ID']])) { unset($allgroups[$field_group['ID']]); } // 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 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']]++; // add ID if the field already exists if ($post = acf_get_field($field['key'], true)) { // add ID to trigger update instead of insert $field["ID"] = $post["ID"]; // \WP_CLI::log($field_group['title'] . "->" . $field['label'] . " field already exists. Updating."); // } else { // \WP_CLI::log($field_group['title'] . "->" . $field['label'] . " field is new. Adding."); } // save field $field = acf_update_field($field); // remove field from allfields array if (isset($allfields[$field['ID']])) { unset($allfields[$field['ID']]); } // add to ref $ref[$field['key']] = $field['ID']; } if ($update) { \WP_CLI::success($field_group['title'] . " field group updated."); } else { \WP_CLI::success($field_group['title'] . " field group added."); } } if (!empty($allgroups)) { foreach ($allgroups as $post) { \WP_CLI::success($post->post_title . " field group deleted."); wp_delete_post($post->ID); } } if (!empty($allfields)) { foreach ($allfields as $post) { \WP_CLI::success($post->post_title . " field deleted."); wp_delete_post($post->ID); } } }
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 $added = array(); $deletedgroups = array(); $deletedfields = array(); $ref = array(); $order = array(); $allgroups = $this->getFieldGroups(); $allfields = $this->getFields(); foreach ($json as $field_group) { $upate = false; // check if field group exists if ($post = acf_get_field_group($field_group['key'], true)) { // add ID to trigger update instead of insert $field_group["ID"] = $post["ID"]; $update = true; } // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // save field group $field_group = acf_update_field_group($field_group); // remove group from $allgroups array if (isset($allgroups[$field_group['ID']])) { unset($allgroups[$field_group['ID']]); } // 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 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']]++; // add ID if the field already exists if ($post = acf_get_field($field['key'], true)) { // add ID to trigger update instead of insert $field["ID"] = $post["ID"]; } // save field $field = acf_update_field($field); // remove group from allgroups array if (isset($allfields[$field['ID']])) { unset($allfields[$field['ID']]); } // add to ref $ref[$field['key']] = $field['ID']; } if ($update) { // append to updated $updated[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; } else { // append to added $added[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; } } if (!empty($allgroups)) { foreach ($allgroups as $post) { $deletedgroups[] = $post->post_title; wp_delete_post($post->ID); } } if (!empty($allfields)) { foreach ($allfields as $post) { $deletedfields[] = $post->post_title; wp_delete_post($post->ID); } } // messages if (!empty($added)) { $message = __('<b>Success</b>. Import tool added %s field groups: %s', 'acf'); $message = sprintf($message, count($added), implode(', ', $added)); acf_add_admin_notice($message); } if (!empty($updated)) { $message = __('<b>Success</b>. Import tool updated %s field group(s): %s', 'acf'); $message = sprintf($message, count($updated), implode(', ', $updated)); acf_add_admin_notice($message); } }
/** * Revert action for ACF Commits * * Delete all existing field groups and fields, and import a backup */ public function acf_commit_import() { $post = get_post($_POST['post_id']); // decode json $json = json_decode($post->post_content, true); // validate json if (empty($json)) { return; } // get all previous acf posts $posts = get_posts(array('post_type' => array('acf-field-group', 'acf-field', 'acf'))); // delete al previous acf posts if (!empty($posts)) { foreach ($posts as $post) { wp_delete_post($post); } } // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // check if field group exists if (acf_get_field_group($field_group['key'])) { // append to ignored $ignored[] = $field_group['title']; continue; } // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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']; } } $this->create_commit('[Reverted] ' . get_field('commit_message', $post->ID)); die; }
function acf_php_recovery_page() { global $wpdb; $acf_local = acf_local(); // process the form if (isset($_POST['acf_php_recovery_action']) && $_POST['acf_php_recovery_action'] == 'import' && isset($_POST['fieldsets']) && check_admin_referer('acf_php_recovery')) { $import_fieldsets = $_POST['fieldsets']; $imported = array(); // Keep track of the imported $key_to_post_id = array(); // Group or field key to post id // Now we can import the groups foreach ($acf_local->groups as $key => $group) { $group['title'] = $group['title'] . ' (Recovered)'; // Only import those that were selected if (in_array($key, $import_fieldsets)) { $saved_group = acf_update_field_group($group); $key_to_post_id[$key] = $saved_group['ID']; // For displaying the success message $imported[] = array('title' => $group['title'], 'id' => $saved_group['ID']); } } // This requires multipile runs to handle sub-fields that have their parent set to the parent field instead of the group $field_parents = $import_fieldsets; // The groups and fields $imported_fields = array(); // Keep track of the already imported do { $num_import = 0; foreach ($acf_local->fields as $key => $field) { if (!in_array($key, $imported_fields) && in_array($field['parent'], $field_parents)) { $num_import = $num_import + 1; $field_parents[] = $key; $imported_fields[] = $key; $field['parent'] = $key_to_post_id[$field['parent']]; // Convert the key into the post_parent $saved_field = acf_update_field($field); $key_to_post_id[$key] = $saved_field['ID']; } } } while ($num_import > 0); } // output ?> <div class="wrap"> <h2>ACF PHP Recovery Tool</h2> <?php // Check the version of ACF $acf_version = explode('.', acf_get_setting('version')); if ($acf_version[0] != '5') { ?> <div id="message" class="error below-h2"> <p><?php printf(__('This tool was built for ACF version 5 and you have version %s.'), $acf_version[0]); ?> </p> </div> <?php } ?> <?php if (!empty($imported)) { ?> <div id="message" class="updated below-h2"><p><?php _e('Fieldsets recovered'); ?> .</p> <ul> <?php foreach ($imported as $import) { ?> <li><?php edit_post_link($import['title'], '', '', $import['id']); ?> </li> <?php } ?> <li><strong><?php _e('Remove the PHP defined fields! The duplicate field IDs interfer with the editing of the fields.'); ?> </strong></li> </ul> </div> <?php } ?> <p><strong>This is a recovery tool. Do not use this as part of your workflow for importing and exporting ACF fieldsets.</strong></p> <form method="POST"> <table class="widefat"> <thead> <th>Import</th> <th>Name</th> <th>Possible Existing Matches</th> </thead> <tbody> <?php foreach ($acf_local->groups as $key => $field_group) { ?> <tr> <td><input type="checkbox" name="fieldsets[]" value="<?php echo esc_attr($key); ?> " /></td> <td><?php echo $field_group['title']; ?> </td> <td><?php $sql = "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_title LIKE '%{$field_group['title']}%' AND post_type='" . ACFPR_GROUP_POST_TYPE . "'"; // Set post status $post_status = apply_filters('acf_recovery\\query\\post_status', ''); if (!empty($post_status)) { $sql .= ' AND post_status="' . esc_sql($post_status) . '"'; } $matches = $wpdb->get_results($sql); if (empty($matches)) { echo '<em>none</em>'; } else { $links = array(); foreach ($matches as $match) { $links[] = '<a href="' . get_edit_post_link($match->ID) . '">' . $match->post_title . '</a>'; } echo implode(', ', $links); } ?> </td> </tr> <?php } ?> </tbody> </table> <?php wp_nonce_field('acf_php_recovery'); ?> <input type="hidden" name="acf_php_recovery_action" value="import" /> <p class="submit"> <input type="submit" value="Import" class="button-primary" /> </p> </form> <h3>Registered Field Groups</h3> <pre class=""> <?php echo var_export($acf_local->groups); ?> </pre> </div> <?php }
function save_post($post_id, $post) { // do not save if this is an auto save routine if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // bail early if not acf-field-group if ($post->post_type !== 'acf-field-group') { return $post_id; } // only save once! WordPress save's a revision as well. if (wp_is_post_revision($post_id)) { return $post_id; } // verify nonce if (!acf_verify_nonce('field_group')) { return $post_id; } // disable filters to ensure ACF loads raw data from DB acf_disable_filters(); // save fields if (!empty($_POST['acf_fields'])) { foreach ($_POST['acf_fields'] as $field) { // vars $specific = false; $save = acf_extract_var($field, 'save'); // only saved field if has changed if ($save == 'meta') { $specific = array('menu_order', 'post_parent'); } // set field parent if (empty($field['parent'])) { $field['parent'] = $post_id; } // save field acf_update_field($field, $specific); } } // delete fields if ($_POST['_acf_delete_fields']) { // clean $ids = explode('|', $_POST['_acf_delete_fields']); $ids = array_map('intval', $ids); // loop foreach ($ids as $id) { // bai early if no id if (!$id) { continue; } // delete acf_delete_field($id); } } // add args $_POST['acf_field_group']['ID'] = $post_id; $_POST['acf_field_group']['title'] = $_POST['post_title']; // save field group acf_update_field_group($_POST['acf_field_group']); // return return $post_id; }
function import($args, $assoc_args) { if (is_multisite()) { $choice = $this->select_blog(); switch_to_blog($choice); if (!isset($args[0])) { $choices = array(); $choices['all'] = 'all'; foreach ($this->paths as $path) { if (!file_exists($path)) { continue; } if ($dir = opendir($path)) { while (false !== ($folder = readdir($dir))) { if ($folder != '.' && $folder != '..') { $key = trailingslashit($path . $folder); $choices[$key] = $folder; } } } } while (true) { $choice = \cli\menu($choices, null, __('Choose a fieldgroup to import', 'acf-wpcli')); \cli\line(); break; } } $patterns = array(); if ($choice == 'all') { foreach ($this->paths as $key => $value) { $patterns[$key] = trailingslashit($value) . '*/data.json'; } } else { $patterns[] = $choice . 'data.json'; } foreach ($patterns as $pattern) { $i = 0; //echo $pattern."\n"; foreach (glob($pattern) as $file) { //Start acf 5 import // read file $json = file_get_contents($file); // decode json $json = json_decode($json, true); // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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']; } WP_CLI::success('imported the data.json for field_group ' . $field_group['title'] . '" into the dabatase!'); } } $i++; if ($i === 1) { break; } } } else { if (!isset($args[0])) { $choices = array(); $choices['all'] = 'all'; foreach ($this->paths as $path) { if (!file_exists($path)) { continue; } if ($dir = opendir($path)) { while (false !== ($folder = readdir($dir))) { if ($folder != '.' && $folder != '..') { $key = trailingslashit($path . $folder); $choices[$key] = $folder; } } } } while (true) { $choice = \cli\menu($choices, null, 'Pick a fieldgroup to import'); \cli\line(); break; } } $patterns = array(); if ($choice == 'all') { foreach ($this->paths as $key => $value) { $patterns[$key] = trailingslashit($value) . '*/data.json'; } } else { $patterns[] = $choice . 'data.json'; } foreach ($patterns as $pattern) { foreach (glob($pattern) as $file) { //Start acf 5 import // read file $json = file_get_contents($file); // decode json $json = json_decode($json, true); // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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']; } WP_CLI::success('imported the data.json for field_group ' . $field_group['title'] . '" into the dabatase!'); } } } } }
function acf_update_500_field_group($ofg) { // global global $wpdb; // create new field group $nfg = array('ID' => 0, 'title' => $ofg->post_title, 'menu_order' => $ofg->menu_order); // location rules $groups = array(); // get all rules $rules = get_post_meta($ofg->ID, 'rule', false); if (is_array($rules)) { $group_no = 0; foreach ($rules as $rule) { // if field group was duplicated, it may now be a serialized string! $rule = maybe_unserialize($rule); // does this rule have a group? // + groups were added in 4.0.4 if (!isset($rule['group_no'])) { $rule['group_no'] = $group_no; // sperate groups? if (get_post_meta($ofg->ID, 'allorany', true) == 'any') { $group_no++; } } // extract vars $group = acf_extract_var($rule, 'group_no'); $order = acf_extract_var($rule, 'order_no'); // add to group $groups[$group][$order] = $rule; // sort rules ksort($groups[$group]); } // sort groups ksort($groups); } $nfg['location'] = $groups; // settings if ($position = get_post_meta($ofg->ID, 'position', true)) { $nfg['position'] = $position; } if ($layout = get_post_meta($ofg->ID, 'layout', true)) { $nfg['layout'] = $layout; } if ($hide_on_screen = get_post_meta($ofg->ID, 'hide_on_screen', true)) { $nfg['hide_on_screen'] = maybe_unserialize($hide_on_screen); } // Note: acf_update_field_group will call the acf_get_valid_field_group function and apply 'compatibility' changes // save field group $nfg = acf_update_field_group($nfg); // action for 3rd party do_action('acf/update_500_field_group', $nfg, $ofg); // trash? if ($ofg->post_status == 'trash') { acf_trash_field_group($nfg['ID']); } // global global $wpdb; // get field from postmeta $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key LIKE %s", $ofg->ID, 'field_%'), ARRAY_A); // check if ($rows) { // loop foreach ($rows as $row) { // bail early if key already migrated (potential duplicates in DB) if (acf_has_done('update_500_field_group_' . $ofg->ID . '_' . $row['meta_key'])) { continue; } // vars $field = $row['meta_value']; $field = maybe_unserialize($field); $field = maybe_unserialize($field); // run again for WPML // add parent $field['parent'] = $nfg['ID']; // migrate field $field = acf_update_500_field($field); } } // return return $nfg; }
/** * Synchronize Acf from json files * * @throws \Exception */ public static function synchronize() { global $wpdb; $path = self::get_configuration_path(); $sql = "DELETE FROM {$wpdb->posts} WHERE post_type='acf-field' OR post_type='acf-field-group';"; $wpdb->query($sql); // Just because we love implicit eval $files = glob("{$path}/*.json"); if (is_array($files)) { foreach ($files as $file) { $json = file_get_contents($file); $json = json_decode($json, true); // copied from acf settings-export.php / import function $fields = acf_extract_var($json, 'fields'); $fields = acf_prepare_fields_for_import($fields); $field_group = acf_update_field_group($json); $ref[$field_group['key']] = $field_group['ID']; $order[$field_group['ID']] = 0; foreach ($fields as $field) { if (empty($field['parent'])) { $field['parent'] = $field_group['ID']; } elseif (isset($ref[$field['parent']])) { $field['parent'] = $ref[$field['parent']]; } if (!isset($order[$field['parent']])) { $order[$field['parent']] = 0; } $field['menu_order'] = $order[$field['parent']]; $order[$field['parent']]++; $field = acf_update_field($field); $ref[$field['key']] = $field['ID']; } // /copied } } ChecksumHandler::stamp(); }
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 $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // check if field group exists if (acf_get_field_group($field_group['key'], true)) { // append to ignored $ignored[] = $field_group['title']; continue; } // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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']; } // append to added $added[] = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; } // messages if (!empty($added)) { $message = __('<b>Success</b>. Import tool added %s field groups: %s', 'acf'); $message = sprintf($message, count($added), implode(', ', $added)); acf_add_admin_notice($message); } if (!empty($ignored)) { $message = __('<b>Warning</b>. Import tool detected %s field groups already exist and have been ignored: %s', 'acf'); $message = sprintf($message, count($ignored), implode(', ', $ignored)); acf_add_admin_notice($message, 'error'); } }
function save_post($post_id) { // do not save if this is an auto save routine if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; } // only save once! WordPress save's a revision as well. if (wp_is_post_revision($post_id)) { return $post_id; } // verify nonce if (!acf_verify_nonce('field_group')) { return $post_id; } // disable local to avoid conflicts between DB and local acf_disable_local(); // save fields unset($_POST['acf_fields']['acfcloneindex']); if (!empty($_POST['acf_fields'])) { foreach ($_POST['acf_fields'] as $field) { // vars $specific = false; $save = acf_extract_var($field, 'save'); // only saved field if has changed if ($save == 'meta') { $specific = array('menu_order', 'post_parent'); } // set field parent if (empty($field['parent'])) { $field['parent'] = $post_id; } // save field acf_update_field($field, $specific); } } // delete fields if ($_POST['_acf_delete_fields']) { $ids = explode('|', $_POST['_acf_delete_fields']); $ids = array_map('intval', $ids); foreach ($ids as $id) { if ($id != 0) { acf_delete_field($id); } } } // add args $_POST['acf_field_group']['ID'] = $post_id; $_POST['acf_field_group']['title'] = $_POST['post_title']; // save field group acf_update_field_group($_POST['acf_field_group']); // return return $post_id; }
private function import_json_field_group($json) { // What follows is basically a copy of import() in ACF admin/settings-export.php // if importing an auto-json, wrap field group in array if (isset($json['key'])) { $json = array($json); } // vars $added = array(); $ignored = array(); $ref = array(); $order = array(); foreach ($json as $field_group) { // remove fields $fields = acf_extract_var($field_group, 'fields'); // format fields $fields = acf_prepare_fields_for_import($fields); // 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 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']; } } }