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); } } } } }
function duplicate_field($field) { // get sub fields $sub_fields = acf_extract_var($field, 'sub_fields'); // save field to get ID $field = acf_update_field($field); // duplicate sub fields acf_duplicate_fields($sub_fields, $field['ID']); // return return $field; }
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 update_value($value, $post_id, $field) { // bail early if is empty if (empty($value)) { return $value; } // select -> update_value() $value = acf_get_field_type('select')->update_value($value, $post_id, $field); // save_other_choice if ($field['save_custom']) { // get raw $field (may have been changed via repeater field) // if field is local, it won't have an ID $selector = $field['ID'] ? $field['ID'] : $field['key']; $field = acf_get_field($selector, true); // bail early if no ID (JSON only) if (!$field['ID']) { return $value; } // loop foreach ($value as $v) { // ignore if already eixsts if (isset($field['choices'][$v])) { continue; } // append $field['choices'][$v] = $v; } // save acf_update_field($field); } // return return $value; }
function _migrate_field_500($field) { // orig $orig = $field; // order_no is now menu_order $field['menu_order'] = acf_extract_var($field, 'order_no'); // correct very old field keys if (substr($field['key'], 0, 6) !== 'field_') { $field['key'] = 'field_' . str_replace('field', '', $field['key']); } // get valid field $field = acf_get_valid_field($field); // save field $field = acf_update_field($field); // sub fields if ($field['type'] == 'repeater') { // get sub fields $sub_fields = acf_extract_var($orig, 'sub_fields'); // save sub fields if (!empty($sub_fields)) { $keys = array_keys($sub_fields); foreach ($keys as $key) { $sub_field = acf_extract_var($sub_fields, $key); $sub_field['parent'] = $field['ID']; _migrate_field_500($sub_field); } } } elseif ($field['type'] == 'flexible_content') { // get layouts $layouts = acf_extract_var($orig, 'layouts'); // update layouts $field['layouts'] = array(); // save sub fields if (!empty($layouts)) { foreach ($layouts as $layout) { // vars $layout_key = uniqid(); // append layotu key $layout['key'] = $layout_key; // extract sub fields $sub_fields = acf_extract_var($layout, 'sub_fields'); // save sub fields if (!empty($sub_fields)) { $keys = array_keys($sub_fields); foreach ($keys as $key) { $sub_field = acf_extract_var($sub_fields, $key); $sub_field['parent'] = $field['ID']; $sub_field['parent_layout'] = $layout_key; _migrate_field_500($sub_field); } // foreach } // if // append layout $field['layouts'][] = $layout; } // foreach } // if // save field again with less sub field data $field = acf_update_field($field); } // return return $field; }
/** * 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 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 duplicate_field($field) { // vars $sub_fields = array(); if (!empty($field['layouts'])) { // loop through layouts foreach ($field['layouts'] as $layout) { // extract sub fields $extra = acf_extract_var($layout, 'sub_fields'); // merge if (!empty($extra)) { $sub_fields = array_merge($sub_fields, $extra); } } // foreach } // if // save field to get ID $field = acf_update_field($field); // duplicate sub fields acf_duplicate_fields($sub_fields, $field['ID']); // return return $field; }
function update_value($value, $post_id, $field) { // bail early if no value (allow 0 to be saved) if (!$value && !is_numeric($value)) { return $value; } // save_other_choice if ($field['save_other_choice']) { // value isn't in choices yet if (!isset($field['choices'][$value])) { // get raw $field (may have been changed via repeater field) // if field is local, it won't have an ID $selector = $field['ID'] ? $field['ID'] : $field['key']; $field = acf_get_field($selector, true); // bail early if no ID (JSON only) if (!$field['ID']) { return $value; } // update $field $field['choices'][$value] = $value; // save acf_update_field($field); } } // return return $value; }
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']; } } }
/** * 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_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 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!'); } } } } }
/** * 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 ajax_move_field() { // disable filters to ensure ACF loads raw data from DB acf_disable_filters(); $args = acf_parse_args($_POST, array('nonce' => '', 'post_id' => 0, 'field_id' => 0, 'field_group_id' => 0)); // verify nonce if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { die; } // confirm? if ($args['field_id'] && $args['field_group_id']) { // vars $field = acf_get_field($args['field_id']); $field_group = acf_get_field_group($args['field_group_id']); // update parent $field['parent'] = $field_group['ID']; // remove conditional logic $field['conditional_logic'] = 0; // update field acf_update_field($field); $v1 = $field['label']; $v2 = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; echo '<p><strong>' . __('Move Complete.', 'acf') . '</strong></p>'; echo '<p>' . sprintf(__('The %s field can now be found in the %s field group', 'acf'), $v1, $v2) . '</p>'; echo '<a href="#" class="button button-primary acf-close-popup">' . __("Close Window", 'acf') . '</a>'; die; } // get all field groups $field_groups = acf_get_field_groups(); $choices = array(); // check if (!empty($field_groups)) { // loop foreach ($field_groups as $field_group) { // bail early if no ID if (!$field_group['ID']) { continue; } // bail ealry if is current if ($field_group['ID'] == $args['post_id']) { continue; } // append $choices[$field_group['ID']] = $field_group['title']; } } // render options $field = acf_get_valid_field(array('type' => 'select', 'name' => 'acf_field_group', 'choices' => $choices)); echo '<p>' . __('Please select the destination for this field', 'acf') . '</p>'; echo '<form id="acf-move-field-form">'; // render acf_render_field_wrap($field); echo '<button type="submit" class="button button-primary">' . __("Move Field", 'acf') . '</button>'; echo '</form>'; // die die; }
function update_value($value, $post_id, $field) { // save_other_choice if ($field['save_other_choice']) { // value isn't in choices yet if (!isset($field['choices'][$value])) { // get ID if local if (!$field['ID']) { $field = acf_get_field($field['key'], true); } // bail early if no ID if (!$field['ID']) { return $value; } // update $field $field['choices'][$value] = $value; // save acf_update_field($field); } } // return return $value; }
function update_value($value, $post_id, $field) { // save_other_choice if ($field['save_other_choice']) { // value isn't in choices yet if (!isset($field['choices'][$value])) { // update $field $field['choices'][$value] = $value; // save acf_update_field($field); } } // return return $value; }
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); } }
function acf_duplicate_field($selector = 0, $new_parent = 0) { // disable filters to ensure ACF loads raw data from DB acf_disable_filters(); // load the origional field $field = acf_get_field($selector); // bail early if field did not load correctly if (empty($field)) { return false; } // update ID $field['ID'] = false; // try duplicate keys $field['key'] = acf_get_setting('duplicate_key_' . $field['key']); // default key if (empty($field['key'])) { $field['key'] = uniqid('field_'); } // update parent if ($new_parent) { $field['parent'] = $new_parent; } // update conditional logic references (because field keys have changed) if (!empty($field['conditional_logic'])) { // extract groups $groups = acf_extract_var($field, 'conditional_logic'); // loop over groups foreach (array_keys($groups) as $g) { // extract group $group = acf_extract_var($groups, $g); // bail early if empty if (empty($group)) { continue; } // loop over rules foreach (array_keys($group) as $r) { // extract rule $rule = acf_extract_var($group, $r); // vars $new_key = acf_get_setting('duplicate_key_' . $rule['field']); // update rule with new key if ($new_key) { $rule['field'] = $new_key; } // append to group $group[$r] = $rule; } // append to groups $groups[$g] = $group; } // update conditional logic $field['conditional_logic'] = $groups; } // filter for 3rd party customization $field = apply_filters("acf/duplicate_field", $field); $field = apply_filters("acf/duplicate_field/type={$field['type']}", $field); // save return acf_update_field($field); }
/** * 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 ajax_move_field() { // disable JSON to avoid conflicts between DB and JSON acf_disable_local(); $args = acf_parse_args($_POST, array('nonce' => '', 'field_id' => 0, 'field_group_id' => 0)); // verify nonce if (!wp_verify_nonce($args['nonce'], 'acf_nonce')) { die; } // confirm? if ($args['field_id'] && $args['field_group_id']) { // vars $field = acf_get_field($args['field_id']); $field_group = acf_get_field_group($args['field_group_id']); // update parent $field['parent'] = $field_group['ID']; // remove conditional logic $field['conditional_logic'] = 0; // update field acf_update_field($field); $v1 = $field['label']; $v2 = '<a href="' . admin_url("post.php?post={$field_group['ID']}&action=edit") . '" target="_blank">' . $field_group['title'] . '</a>'; echo '<p><strong>' . __('Move Complete.', 'acf') . '</strong></p>'; echo sprintf(__('The %s field can now be found in the %s field group', 'acf'), $v1, $v2) . '</p>'; echo '<a href="#" class="acf-button blue acf-close-popup">' . __("Close Window", 'acf') . '</a>'; die; } // get all field groups $field_groups = acf_get_field_groups(); $choices = array(); if (!empty($field_groups)) { foreach ($field_groups as $field_group) { if ($field_group['ID']) { $choices[$field_group['ID']] = $field_group['title']; } } } // render options $field = acf_get_valid_field(array('type' => 'select', 'name' => 'acf_field_group', 'choices' => $choices)); echo '<p>' . __('Please select the destination for this field', 'acf') . '</p>'; echo '<form id="acf-move-field-form">'; // render acf_render_field_wrap($field); echo '<button type="submit" class="acf-button blue">' . __("Move Field", 'acf') . '</button>'; echo '</form>'; // die die; }
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); } } } } }