Пример #1
0
function acf_get_field_group($selector = false, $search_trash = false)
{
    // vars
    $field_group = false;
    $k = 'ID';
    $v = 0;
    // $post_id or $key
    if (is_numeric($selector)) {
        $v = $selector;
    } elseif (is_string($selector)) {
        $k = 'key';
        $v = $selector;
    } elseif (is_object($selector)) {
        $v = $selector->ID;
    } else {
        return false;
    }
    // get cache key
    $cache_key = "get_field_group/{$k}={$v}";
    // get cache
    $found = false;
    $cache = wp_cache_get($cache_key, 'acf', false, $found);
    if ($found) {
        return $cache;
    }
    // get field group from ID or key
    if ($k == 'ID') {
        $field_group = _acf_get_field_group_by_id($v);
    } else {
        $field_group = _acf_get_field_group_by_key($v, $search_trash);
    }
    // filter for 3rd party customization
    $field_group = apply_filters('acf/get_field_group', $field_group);
    // set cache
    wp_cache_set($cache_key, $field_group, 'acf');
    // return
    return $field_group;
}
 function load_field($field)
 {
     $group = _acf_get_field_group_by_key($field["group_key"]);
     $fields = acf_get_fields($group);
     $field['sub_fields'] = $fields;
     return $field;
 }
Пример #3
0
 /**
  * Get a field group by its key
  *
  * @param string $key ACF field group key (slug)
  * @return array|bool ACF field group, or false or empty array (depending on the ACF version) if the key doesn't exist
  */
 public static function get_field_group_by_key($key)
 {
     if (self::is_pro_installed()) {
         return _acf_get_field_group_by_key($key);
     } else {
         // keys are not supported with ACF 4, fall back to ID
         return self::get_field_group_by_id($key);
     }
 }
Пример #4
0
function acf_get_field_group($selector = null)
{
    // vars
    $field_group = false;
    $type = 'ID';
    // ID
    if (is_numeric($selector)) {
        // do nothing
        // object
    } elseif (is_object($selector)) {
        $selector = $selector->ID;
        // string
    } elseif (is_string($selector)) {
        $type = 'key';
        // other
    } else {
        return false;
    }
    // return early if cache is found
    $cache_key = "get_field_group/{$type}={$selector}";
    if (acf_isset_cache($cache_key)) {
        return acf_get_cache($cache_key);
    }
    // ID
    if ($type == 'ID') {
        $field_group = _acf_get_field_group_by_id($selector);
        // key
    } else {
        $field_group = _acf_get_field_group_by_key($selector);
    }
    // bail early if no field
    if (!$field_group) {
        return false;
    }
    // filter for 3rd party customization
    $field_group = apply_filters('acf/get_field_group', $field_group);
    // update cache
    // - Use key instead of ID for best compatibility (not all field groups exist in the DB)
    $cache_key = acf_set_cache("get_field_group/key={$field_group['key']}", $field_group);
    // update cache reference
    // - allow cache to return if using an ID selector
    acf_set_cache_reference("get_field_group/ID={$field_group['ID']}", $cache_key);
    // return
    return $field_group;
}
Пример #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
     $ids = array();
     $keys = array();
     $imported = array();
     // populate keys
     foreach ($json as $field_group) {
         // append key
         $keys[] = $field_group['key'];
     }
     // look for existing ids
     foreach ($keys as $key) {
         // attempt find ID
         $field_group = _acf_get_field_group_by_key($key);
         // bail early if no field group
         if (!$field_group) {
             continue;
         }
         // append
         $ids[$key] = $field_group['ID'];
     }
     // enable local
     acf_enable_local();
     // reset local (JSON class has already included .json field groups which may conflict)
     acf_reset_local();
     // add local field groups
     foreach ($json as $field_group) {
         // add field group
         acf_add_local_field_group($field_group);
     }
     // loop over keys
     foreach ($keys as $key) {
         // vars
         $field_group = acf_get_local_field_group($key);
         // attempt get id
         $id = acf_maybe_get($ids, $key);
         if ($id) {
             $field_group['ID'] = $id;
         }
         // append fields
         if (acf_have_local_fields($key)) {
             $field_group['fields'] = acf_get_local_fields($key);
         }
         // import
         $field_group = acf_import_field_group($field_group);
         // append message
         $imported[] = array('ID' => $field_group['ID'], 'title' => $field_group['title'], 'updated' => $id ? 1 : 0);
     }
     // messages
     if (!empty($imported)) {
         // vars
         $links = array();
         $count = count($imported);
         $message = sprintf(_n('Imported 1 field group', 'Imported %s field groups', $count, 'acf'), $count) . '.';
         // populate links
         foreach ($imported as $import) {
             $links[] = '<a href="' . admin_url("post.php?post={$import['ID']}&action=edit") . '" target="_blank">' . $import['title'] . '</a>';
         }
         // append links
         $message .= ' ' . implode(', ', $links);
         // add notice
         acf_add_admin_notice($message);
     }
 }