function add_field_group($field_group)
 {
     // validate
     $field_group = acf_get_valid_field_group($field_group);
     // don't allow overrides
     if (acf_is_local_field_group($field_group['key'])) {
         return;
     }
     // remove fields
     $fields = acf_extract_var($field_group, 'fields');
     // format fields
     $fields = acf_prepare_fields_for_import($fields);
     // add field group
     $this->groups[$field_group['key']] = $field_group;
     // add fields
     foreach ($fields as $field) {
         // add parent
         if (empty($field['parent'])) {
             $field['parent'] = $field_group['key'];
         }
         // add field group reference
         //$field['field_group'] = $field_group['key'];
         // add field
         $this->add_field($field);
     }
 }
Esempio n. 2
0
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);
                }
            }
        }
    }
}
Esempio n. 3
0
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);
                }
            }
        }
    }
}
Esempio n. 4
0
 /**
  * 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'];
     }
 }
Esempio n. 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
     $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');
     }
 }
Esempio n. 6
0
 function ajax_query()
 {
     // validate
     if (!acf_verify_ajax()) {
         die;
     }
     // disable field to allow clone fields to appear selectable
     acf_disable_filter('clone');
     // options
     $options = acf_parse_args($_POST, array('post_id' => 0, 'paged' => 0, 's' => '', 'title' => '', 'fields' => array()));
     // vars
     $results = array();
     $s = false;
     $i = -1;
     $limit = 20;
     $range_start = $limit * ($options['paged'] - 1);
     //	0,	20,	40
     $range_end = $range_start + ($limit - 1);
     //	19,	39,	59
     // search
     if ($options['s'] !== '') {
         // strip slashes (search may be integer)
         $s = wp_unslash(strval($options['s']));
     }
     // load groups
     $field_groups = acf_get_field_groups();
     $field_group = false;
     // bail early if no field groups
     if (empty($field_groups)) {
         die;
     }
     // move current field group to start
     foreach (array_keys($field_groups) as $j) {
         // check ID
         if ($field_groups[$j]['ID'] !== $options['post_id']) {
             continue;
         }
         // extract field group and move to start
         $field_group = acf_extract_var($field_groups, $j);
         // field group found, stop looking
         break;
     }
     // if field group was not found, this is a new field group (not yet saved)
     if (!$field_group) {
         $field_group = array('ID' => $options['post_id'], 'title' => $options['title'], 'key' => '');
     }
     // move current field group to start of list
     array_unshift($field_groups, $field_group);
     // loop
     foreach ($field_groups as $field_group) {
         // vars
         $fields = false;
         $data = array('text' => $field_group['title'], 'children' => array());
         // get fields
         if ($field_group['ID'] == $options['post_id']) {
             $fields = $options['fields'];
         } else {
             $fields = acf_get_fields($field_group);
             $fields = acf_prepare_fields_for_import($fields);
         }
         // bail early if no fields
         if (!$fields) {
             continue;
         }
         // populate children
         $children = array();
         $children[] = $field_group['key'];
         foreach ($fields as $field) {
             $children[] = $field['key'];
         }
         // loop
         foreach ($children as $child) {
             // bail ealry if no key (fake field group or corrupt field)
             if (!$child) {
                 continue;
             }
             // vars
             $text = false;
             // bail early if is search, and $text does not contain $s
             if ($s !== false) {
                 // get early
                 $text = $this->get_clone_setting_choice($child);
                 // search
                 if (stripos($text, $s) === false) {
                     continue;
                 }
             }
             // $i
             $i++;
             // bail early if $i is out of bounds
             if ($i < $range_start || $i > $range_end) {
                 continue;
             }
             // load text
             if ($text === false) {
                 $text = $this->get_clone_setting_choice($child);
             }
             // append
             $data['children'][] = array('id' => $child, 'text' => $text);
         }
         // bail early if no children
         // - this group contained fields, but none shown on this page
         if (empty($data['children'])) {
             continue;
         }
         // append
         $results[] = $data;
         // end loop if $i is out of bounds
         // - no need to look further
         if ($i > $range_end) {
             break;
         }
     }
     // return
     acf_send_ajax_results(array('results' => $results, 'limit' => $limit));
 }
Esempio n. 7
0
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;
}
Esempio n. 8
0
 /**
  * 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);
     }
 }
Esempio n. 10
0
 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'];
         }
     }
 }
 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!');
                 }
             }
         }
     }
 }
Esempio n. 12
0
 /**
  * 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();
 }
Esempio n. 13
0
 /**
  * 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;
 }