· 4 years ago · Mar 31, 2021, 01:34 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 $index => $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    // Generating category fields
93    acf_add_local_field_group(array(
94        'key' => 'group_5d070117d8065',
95        'title' => 'Konsulentfelter',
96        'fields' => [
97            array(
98                'key' => 'field_5d070126370a3',
99                'label' => 'name',
100                'name' => 'name',
101                'type' => 'text',
102                'required' => 1,
103                'conditional_logic' => 0,
104                'wrapper' => array(
105                    'width' => '',
106                    'class' => '',
107                    'id' => '',
108                ),
109                'default_value' => '',
110                'placeholder' => '',
111                'prepend' => '',
112                'append' => '',
113                'maxlength' => '',
114            ),
115            $fields
116        ], 
117        'location' => array (
118            array (
119                array (
120                    'param' => 'post_type',
121                    'operator' => '==',
122                    'value' => 'konsulenter',
123                ),
124            ),
125        ),
126    )); 
127}
128
129add_action('acf/init', 'my_acf_add_local_field_groups');
130
131function array_flatten($array) { 
132  if (!is_array($array)) { 
133    return false; 
134  } 
135  $result = array(); 
136  foreach ($array as $key => $value) { 
137    if (is_array($value)) { 
138      $result = array_merge($result, array_flatten($value)); 
139    } else { 
140      $result = array_merge($result, array($key => $value));
141    } 
142  } 
143  return $result; 
144}
145
146function generateRandomString($length = 13) {
147    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
148    $charactersLength = strlen($characters);
149    $randomString = '';
150    for ($i = 0; $i < $length; $i++) {
151        $randomString .= $characters[rand(0, $charactersLength - 1)];
152    }
153    return $randomString;
154}