· 4 years ago · Mar 31, 2021, 01:52 PM
1<?php
2
3function my_acf_add_local_field_groups() {
4    include_once( get_theme_file_path('./helpers/env.php') );
5
6    $END_POINT        = get_env_name('SAS_URL') . get_env_token('SAS_TOKEN');
7    $json_data        = @file_get_contents($END_POINT);
8    $consultants_data = json_decode($json_data, TRUE);
9
10    // Getting all of the categories name from API 
11    foreach ($consultants_data as $key => $consultant) {
12        if ( $consultant['cv']['technologies'] ) {
13            foreach ($consultant['cv']['technologies'] as $skills) { 
14                asort($skills);
15                $category = $skills['category']['no'];
16
17                if(!empty($category)) {
18                    $category_data[] = [ $category ];
19                }
20            }
21        }
22    }
23
24    // Remove duplicated values and keep only one unique value instead 
25    $categories_arr = array_unique([...$category_data], SORT_REGULAR );
26    sort($categories_arr);
27
28    // Creating a ACF fields of 'text'. We are going to use it to append it to produce many fields categories
29    foreach (array_flatten($categories_arr) as $category) {
30        $fields[] = [
31            'key' => 'field_' . generateRandomString(),
32            'label' => $category,
33            'name' => $category,
34            'type' => 'repeater',
35            'instructions' => '',
36			'required' => 0,
37			'conditional_logic' => 0,
38			'wrapper' => array(
39				'width' => '',
40				'class' => '',
41				'id' => '',
42			),
43            'collapsed' => '',
44			'min' => 0,
45			'max' => 0,
46			'layout' => 'table',
47			'button_label' => '',
48			'sub_fields' => array(
49                array(
50					'key' => 'field_' . generateRandomString(),
51                    // Check if there is whitespace. If it's white space, then we want to add underscore with words together
52					'label' => ctype_space($category) ?  mb_strtolower($category, 'UTF-8')  : mb_strtolower(str_replace(' ', '_', $category), 'UTF-8'),
53					'name' => mb_strtolower($category, 'UTF-8'),
54					'type' => 'text',
55					'instructions' => '',
56					'required' => 0,
57					'conditional_logic' => 0,
58					'wrapper' => array(
59						'width' => '',
60						'class' => '',
61						'id' => '',
62					),
63					'default_value' => '',
64					'placeholder' => '',
65					'prepend' => '',
66					'append' => '',
67					'maxlength' => '',
68				),
69                array(
70					'key' => 'field_' . generateRandomString(),
71					'label' => $category . ' order',
72					'name' => mb_strtolower($category, 'UTF-8') . '_order',
73					'type' => 'text',
74					'instructions' => '',
75					'required' => 0,
76					'conditional_logic' => 0,
77					'wrapper' => array(
78						'width' => '',
79						'class' => '',
80						'id' => '',
81					),
82					'default_value' => '',
83					'placeholder' => '',
84					'prepend' => '',
85					'append' => '',
86					'maxlength' => '',
87				),
88            )
89        ];
90    }
91
92
93    $fields = array_unshift($fields, array(
94        'key' => 'field_5d070126370a3',
95        'label' => 'name',
96        'name' => 'name',
97        'type' => 'text',
98        'required' => 1,
99        'conditional_logic' => 0,
100        'wrapper' => array(
101            'width' => '',
102            'class' => '',
103            'id' => '',
104        ),
105        'default_value' => '',
106        'placeholder' => '',
107        'prepend' => '',
108        'append' => '',
109        'maxlength' => '',
110        )
111    );
112
113    // Generating category fields
114    acf_add_local_field_group(array(
115        'key' => 'group_5d070117d8065',
116        'title' => 'Konsulentfelter',
117        'fields' => $fields,
118        'location' => array (
119            array (
120                array (
121                    'param' => 'post_type',
122                    'operator' => '==',
123                    'value' => 'konsulenter',
124                ),
125            ),
126        ),
127    )); 
128}
129
130add_action('acf/init', 'my_acf_add_local_field_groups');
131
132function array_flatten($array) { 
133  if (!is_array($array)) { 
134    return false; 
135  } 
136  $result = array(); 
137  foreach ($array as $key => $value) { 
138    if (is_array($value)) { 
139      $result = array_merge($result, array_flatten($value)); 
140    } else { 
141      $result = array_merge($result, array($key => $value));
142    } 
143  } 
144  return $result; 
145}
146
147function generateRandomString($length = 13) {
148    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
149    $charactersLength = strlen($characters);
150    $randomString = '';
151    for ($i = 0; $i < $length; $i++) {
152        $randomString .= $characters[rand(0, $charactersLength - 1)];
153    }
154    return $randomString;
155}