· 4 years ago · Mar 31, 2021, 03:42 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_index => $category) {
30        $fields_repeater[] = [
31            'key' => 'field_' . generateRandomString() . $category_index,
32            'label' => 'Ferdigheter ' . $category,
33            'name' => ctype_space($category) ? 'ferdigheter_' . mb_strtolower($category, 'UTF-8') : 'ferdigheter_' . mb_strtolower(str_replace(' ', '_', $category), 'UTF-8'),
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() . $category_index,
51                    // Check if there is whitespace. If it's white space, then we want to add underscore with words together
52					'label' => $category,
53					'name' => ctype_space($category) ? mb_strtolower($category, 'UTF-8') : mb_strtolower(str_replace(' ', '_', $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() . $category_index,
71					'label' =>  $category . ' Order',
72					'name' => ctype_space($category) ? mb_strtolower($category, 'UTF-8') . '_order' : mb_strtolower(str_replace(' ', '_', $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    var_dump($fields_repeater[0]);
93
94    $fields = [
95        array(
96            'key' => 'field_5d070126370a3',
97            'label' => 'fornavn',
98            'name' => 'fornavn',
99            'type' => 'text',
100            'required' => 1,
101            'conditional_logic' => 0,
102            'wrapper' => array(
103                'width' => '',
104                'class' => '',
105                'id' => '',
106            ),
107            'default_value' => '',
108            'placeholder' => '',
109            'prepend' => '',
110            'append' => '',
111            'maxlength' => '',
112        ),
113        array(
114			'key' => 'field_5d0701a5370a4',
115			'label' => 'Etternavn',
116			'name' => 'etternavn',
117			'type' => 'text',
118			'instructions' => 'Skriv inn etternavnet til konsulenten',
119			'required' => 1,
120			'conditional_logic' => 0,
121			'wrapper' => array(
122				'width' => '',
123				'class' => '',
124				'id' => '',
125			),
126			'default_value' => '',
127			'placeholder' => '',
128			'prepend' => '',
129			'append' => '',
130			'maxlength' => '',
131		),
132		array(
133			'key' => 'field_5d6e3067afbc2',
134			'label' => 'Stilling',
135			'name' => 'stilling',
136			'type' => 'text',
137			'instructions' => 'Skriv inn stillingen konsulenten innehar',
138			'required' => 1,
139			'conditional_logic' => 0,
140			'wrapper' => array(
141				'width' => '',
142				'class' => '',
143				'id' => '',
144			),
145			'default_value' => '',
146			'placeholder' => '',
147			'prepend' => '',
148			'append' => '',
149			'maxlength' => '',
150		),
151		array(
152			'key' => 'field_5d667bfd893a9',
153			'label' => 'Konsulentens epost',
154			'name' => 'konsulentens_epost',
155			'type' => 'text',
156			'instructions' => '',
157			'required' => 0,
158			'conditional_logic' => 0,
159			'wrapper' => array(
160				'width' => '',
161				'class' => '',
162				'id' => '',
163			),
164			'default_value' => '',
165			'placeholder' => '',
166			'prepend' => '',
167			'append' => '',
168			'maxlength' => '',
169		),
170		array(
171			'key' => 'field_5d667c2c893aa',
172			'label' => 'Konsulentens telefonnummer',
173			'name' => 'konsulentens_telefonnummer',
174			'type' => 'number',
175			'instructions' => '',
176			'required' => 0,
177			'conditional_logic' => 0,
178			'wrapper' => array(
179				'width' => '',
180				'class' => '',
181				'id' => '',
182			),
183			'default_value' => '',
184			'placeholder' => '',
185			'prepend' => '',
186			'append' => '',
187			'min' => '',
188			'max' => '',
189			'step' => '',
190		),
191		array(
192			'key' => 'field_5d0701b7370a5',
193			'label' => 'Kort om konsulenten',
194			'name' => 'kort_om_konsulenten',
195			'type' => 'wysiwyg',
196			'instructions' => 'Skriv kort om konsulentens inngang til Sicra eller relevante temaer',
197			'required' => 1,
198			'conditional_logic' => 0,
199			'wrapper' => array(
200				'width' => '',
201				'class' => '',
202				'id' => '',
203			),
204			'default_value' => '',
205			'tabs' => 'all',
206			'toolbar' => 'full',
207			'media_upload' => 1,
208			'delay' => 0,
209		),
210		array(
211			'key' => 'field_6025362361d9c',
212			'label' => 'Sammendrag av konsulent',
213			'name' => 'sammendrag_av_konsulent',
214			'type' => 'wysiwyg',
215			'instructions' => '',
216			'required' => 0,
217			'conditional_logic' => 0,
218			'wrapper' => array(
219				'width' => '',
220				'class' => '',
221				'id' => '',
222			),
223			'default_value' => '',
224			'tabs' => 'all',
225			'toolbar' => 'full',
226			'media_upload' => 1,
227			'delay' => 0,
228		),
229		array(
230			'key' => 'field_5fe1b331eb56f',
231			'label' => 'Tittel for kundesitat',
232			'name' => 'tittel_for_kundesitat',
233			'type' => 'text',
234			'instructions' => '',
235			'required' => 0,
236			'conditional_logic' => 0,
237			'wrapper' => array(
238				'width' => '',
239				'class' => '',
240				'id' => '',
241			),
242			'default_value' => '',
243			'placeholder' => '',
244			'prepend' => '',
245			'append' => '',
246			'maxlength' => '',
247		),
248		array(
249			'key' => 'field_5d664e0779aa5',
250			'label' => 'Sitat fra kunde',
251			'name' => 'sitat_fra_kunde',
252			'type' => 'textarea',
253			'instructions' => 'Legg inn et sitat fra kunde',
254			'required' => 0,
255			'conditional_logic' => 0,
256			'wrapper' => array(
257				'width' => '',
258				'class' => '',
259				'id' => '',
260			),
261			'default_value' => '',
262			'placeholder' => '',
263			'maxlength' => '',
264			'rows' => '',
265			'new_lines' => '',
266		),
267		array(
268			'key' => 'field_5d666dcf8b16e',
269			'label' => 'Navn på kunde',
270			'name' => 'navn_kunde',
271			'type' => 'text',
272			'instructions' => 'Legg inn et sitat fra kunde',
273			'required' => 0,
274			'conditional_logic' => 0,
275			'wrapper' => array(
276				'width' => '',
277				'class' => '',
278				'id' => '',
279			),
280			'default_value' => '',
281			'placeholder' => '',
282			'prepend' => '<p class="quote-customer">',
283			'append' => '</p>',
284			'maxlength' => '',
285		),
286		array(
287			'key' => 'field_5d0702b40d144',
288			'label' => 'Frigjort bilde av konsulenten',
289			'name' => 'frigjort_bilde_av_konsulenten',
290			'type' => 'image',
291			'instructions' => '',
292			'required' => 1,
293			'conditional_logic' => 0,
294			'wrapper' => array(
295				'width' => '',
296				'class' => '',
297				'id' => '',
298			),
299			'return_format' => 'array',
300			'preview_size' => 'medium',
301			'library' => 'all',
302			'min_width' => '',
303			'min_height' => '',
304			'min_size' => '',
305			'max_width' => '',
306			'max_height' => '',
307			'max_size' => '',
308			'mime_types' => '',
309		),
310		array(
311			'key' => 'field_5fdc6bcb16bf1',
312			'label' => 'Fagområder',
313			'name' => 'fagomrader',
314			'type' => 'repeater',
315			'instructions' => '',
316			'required' => 0,
317			'conditional_logic' => 0,
318			'wrapper' => array(
319				'width' => '',
320				'class' => '',
321				'id' => '',
322			),
323			'collapsed' => '',
324			'min' => 0,
325			'max' => 0,
326			'layout' => 'table',
327			'button_label' => '',
328			'sub_fields' => array(
329				array(
330					'key' => 'field_5fdc6bdb16bf2',
331					'label' => 'Fagområder tittel',
332					'name' => 'fagomrader_tittel',
333					'type' => 'text',
334					'instructions' => '',
335					'required' => 0,
336					'conditional_logic' => 0,
337					'wrapper' => array(
338						'width' => '',
339						'class' => '',
340						'id' => '',
341					),
342					'default_value' => '',
343					'placeholder' => '',
344					'prepend' => '',
345					'append' => '',
346					'maxlength' => '',
347				),
348			),
349		),
350        	array(
351			'key' => 'field_5fdca429c4259',
352			'label' => 'Prosjekt erfaring',
353			'name' => 'prosjekt_erfaring',
354			'type' => 'repeater',
355			'instructions' => '',
356			'required' => 0,
357			'conditional_logic' => 0,
358			'wrapper' => array(
359				'width' => '',
360				'class' => '',
361				'id' => '',
362			),
363			'collapsed' => '',
364			'min' => 0,
365			'max' => 0,
366			'layout' => 'row',
367			'button_label' => '',
368			'sub_fields' => array(
369				array(
370					'key' => 'field_5fdca434c425a',
371					'label' => 'Prosjekt kunde',
372					'name' => 'prosjekt_kunde',
373					'type' => 'text',
374					'instructions' => '',
375					'required' => 0,
376					'conditional_logic' => 0,
377					'wrapper' => array(
378						'width' => '',
379						'class' => '',
380						'id' => '',
381					),
382					'default_value' => '',
383					'placeholder' => '',
384					'prepend' => '',
385					'append' => '',
386					'maxlength' => '',
387				),
388				array(
389					'key' => 'field_5fdca482c425d',
390					'label' => 'Prosjekt sektor',
391					'name' => 'prosjekt_sektor',
392					'type' => 'text',
393					'instructions' => '',
394					'required' => 0,
395					'conditional_logic' => 0,
396					'wrapper' => array(
397						'width' => '',
398						'class' => '',
399						'id' => '',
400					),
401					'default_value' => '',
402					'placeholder' => '',
403					'prepend' => '',
404					'append' => '',
405					'maxlength' => '',
406				),
407				array(
408					'key' => 'field_5fdca4e3dffcd',
409					'label' => 'Prosjekt beskrivelse',
410					'name' => 'prosjekt_beskrivelse',
411					'type' => 'text',
412					'instructions' => '',
413					'required' => 0,
414					'conditional_logic' => 0,
415					'wrapper' => array(
416						'width' => '',
417						'class' => '',
418						'id' => '',
419					),
420					'default_value' => '',
421					'placeholder' => '',
422					'prepend' => '',
423					'append' => '',
424					'maxlength' => '',
425				),
426				array(
427					'key' => 'field_5fdca48cc425e',
428					'label' => 'Prosjekt oppgaver',
429					'name' => 'prosjekt_oppgaver',
430					'type' => 'text',
431					'instructions' => '',
432					'required' => 0,
433					'conditional_logic' => 0,
434					'wrapper' => array(
435						'width' => '',
436						'class' => '',
437						'id' => '',
438					),
439					'default_value' => '',
440					'placeholder' => '',
441					'prepend' => '',
442					'append' => '',
443					'maxlength' => '',
444				),
445				array(
446					'key' => 'field_5fdca51a9023f',
447					'label' => 'Prosjekt rolle',
448					'name' => 'prosjekt_rolle',
449					'type' => 'text',
450					'instructions' => '',
451					'required' => 0,
452					'conditional_logic' => 0,
453					'wrapper' => array(
454						'width' => '',
455						'class' => '',
456						'id' => '',
457					),
458					'default_value' => '',
459					'placeholder' => '',
460					'prepend' => '',
461					'append' => '',
462					'maxlength' => '',
463				),
464				array(
465					'key' => 'field_602a731a2a525',
466					'label' => 'Prosjekt Order',
467					'name' => 'prosjekt_order',
468					'type' => 'text',
469					'instructions' => '',
470					'required' => 0,
471					'conditional_logic' => 0,
472					'wrapper' => array(
473						'width' => '',
474						'class' => '',
475						'id' => '',
476					),
477					'default_value' => '',
478					'placeholder' => '',
479					'prepend' => '',
480					'append' => '',
481					'maxlength' => '',
482				),
483				array(
484					'key' => 'field_5fdca45ec425b',
485					'label' => 'prosjekt periode fra',
486					'name' => 'prosjekt_periode_fra',
487					'type' => 'text',
488					'instructions' => '',
489					'required' => 0,
490					'conditional_logic' => 0,
491					'wrapper' => array(
492						'width' => '',
493						'class' => '',
494						'id' => '',
495					),
496					'default_value' => '',
497					'placeholder' => '',
498					'prepend' => '',
499					'append' => '',
500					'maxlength' => '',
501				),
502				array(
503					'key' => 'field_5fdca478c425c',
504					'label' => 'Prosjekt periode til',
505					'name' => 'prosjekt_periode_til',
506					'type' => 'text',
507					'instructions' => '',
508					'required' => 0,
509					'conditional_logic' => 0,
510					'wrapper' => array(
511						'width' => '',
512						'class' => '',
513						'id' => '',
514					),
515					'default_value' => '',
516					'placeholder' => '',
517					'prepend' => '',
518					'append' => '',
519					'maxlength' => '',
520				),
521			),
522		),
523        array(
524			'key' => 'field_5fdc942561c62',
525			'label' => 'Sertifisering',
526			'name' => 'sertifisering',
527			'type' => 'repeater',
528			'instructions' => '',
529			'required' => 0,
530			'conditional_logic' => 0,
531			'wrapper' => array(
532				'width' => '',
533				'class' => '',
534				'id' => '',
535			),
536			'collapsed' => '',
537			'min' => 0,
538			'max' => 0,
539			'layout' => 'table',
540			'button_label' => '',
541			'sub_fields' => array(
542				array(
543					'key' => 'field_5fdc943061c63',
544					'label' => 'Sertifiserings navn',
545					'name' => 'sertifiserings_navn',
546					'type' => 'text',
547					'instructions' => '',
548					'required' => 0,
549					'conditional_logic' => 0,
550					'wrapper' => array(
551						'width' => '',
552						'class' => '',
553						'id' => '',
554					),
555					'default_value' => '',
556					'placeholder' => '',
557					'prepend' => '',
558					'append' => '',
559					'maxlength' => '',
560				),
561				array(
562					'key' => 'field_5fdc944861c64',
563					'label' => 'Sertifiserings organiser',
564					'name' => 'sertifiserings_organiser',
565					'type' => 'text',
566					'instructions' => '',
567					'required' => 0,
568					'conditional_logic' => 0,
569					'wrapper' => array(
570						'width' => '',
571						'class' => '',
572						'id' => '',
573					),
574					'default_value' => '',
575					'placeholder' => '',
576					'prepend' => '',
577					'append' => '',
578					'maxlength' => '',
579				),
580				array(
581					'key' => 'field_5fdc944f61c65',
582					'label' => 'Sertifiserings gjennomføringsår',
583					'name' => 'sertifiserings_gjennomforingsar',
584					'type' => 'text',
585					'instructions' => '',
586					'required' => 0,
587					'conditional_logic' => 0,
588					'wrapper' => array(
589						'width' => '',
590						'class' => '',
591						'id' => '',
592					),
593					'default_value' => '',
594					'placeholder' => '',
595					'prepend' => '',
596					'append' => '',
597					'maxlength' => '',
598				),
599				array(
600					'key' => 'field_602ae65415624',
601					'label' => 'Sertifisering Order',
602					'name' => 'sertifisering_order',
603					'type' => 'text',
604					'instructions' => '',
605					'required' => 0,
606					'conditional_logic' => 0,
607					'wrapper' => array(
608						'width' => '',
609						'class' => '',
610						'id' => '',
611					),
612					'default_value' => '',
613					'placeholder' => '',
614					'prepend' => '',
615					'append' => '',
616					'maxlength' => '',
617				),
618			),
619		),
620		array(
621			'key' => 'field_5fdc9e2b49cea',
622			'label' => 'Kurs',
623			'name' => 'kurs',
624			'type' => 'repeater',
625			'instructions' => '',
626			'required' => 0,
627			'conditional_logic' => 0,
628			'wrapper' => array(
629				'width' => '',
630				'class' => '',
631				'id' => '',
632			),
633			'collapsed' => '',
634			'min' => 0,
635			'max' => 0,
636			'layout' => 'row',
637			'button_label' => '',
638			'sub_fields' => array(
639				array(
640					'key' => 'field_5fdc9e3549ceb',
641					'label' => 'Kurs navn',
642					'name' => 'kurs_navn',
643					'type' => 'text',
644					'instructions' => '',
645					'required' => 0,
646					'conditional_logic' => 0,
647					'wrapper' => array(
648						'width' => '',
649						'class' => '',
650						'id' => '',
651					),
652					'default_value' => '',
653					'placeholder' => '',
654					'prepend' => '',
655					'append' => '',
656					'maxlength' => '',
657				),
658				array(
659					'key' => 'field_5fdc9e4049cec',
660					'label' => 'Kurs beskrivelse',
661					'name' => 'kurs_beskrivelse',
662					'type' => 'text',
663					'instructions' => '',
664					'required' => 0,
665					'conditional_logic' => 0,
666					'wrapper' => array(
667						'width' => '',
668						'class' => '',
669						'id' => '',
670					),
671					'default_value' => '',
672					'placeholder' => '',
673					'prepend' => '',
674					'append' => '',
675					'maxlength' => '',
676				),
677				array(
678					'key' => 'field_5fdc9e5049ced',
679					'label' => 'Kurs program',
680					'name' => 'kurs_program',
681					'type' => 'text',
682					'instructions' => '',
683					'required' => 0,
684					'conditional_logic' => 0,
685					'wrapper' => array(
686						'width' => '',
687						'class' => '',
688						'id' => '',
689					),
690					'default_value' => '',
691					'placeholder' => '',
692					'prepend' => '',
693					'append' => '',
694					'maxlength' => '',
695				),
696				array(
697					'key' => 'field_5fdc9e5749cee',
698					'label' => 'Kurs gjennomføringsår',
699					'name' => 'kurs_gjennomforingsar',
700					'type' => 'text',
701					'instructions' => '',
702					'required' => 0,
703					'conditional_logic' => 0,
704					'wrapper' => array(
705						'width' => '',
706						'class' => '',
707						'id' => '',
708					),
709					'default_value' => '',
710					'placeholder' => '',
711					'prepend' => '',
712					'append' => '',
713					'maxlength' => '',
714				),
715			),
716		),
717    ];
718
719    foreach($fields_repeater as $field_repeater) $fields[] = $field_repeater;
720
721    acf_add_local_field_group(array(
722            'key' => 'group_5d070117d8065',
723            'title' => 'Konsulentfelter',
724            'fields' => $fields, 
725            'location' => array (
726                array (
727                    array (
728                        'param' => 'post_type',
729                        'operator' => '==',
730                        'value' => 'konsulenter',
731                    ),
732                ),
733            ),
734        )); 
735    }
736
737add_action('acf/init', 'my_acf_add_local_field_groups');
738
739function array_flatten($array) { 
740  if (!is_array($array)) { 
741    return false; 
742  } 
743  $result = array(); 
744  foreach ($array as $key => $value) { 
745    if (is_array($value)) { 
746      $result = array_merge($result, array_flatten($value)); 
747    } else { 
748      $result = array_merge($result, array($key => $value));
749    } 
750  } 
751  return $result; 
752}
753
754function generateRandomString($length = 12) {
755    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
756    $charactersLength = strlen($characters);
757    $randomString = '';
758    for ($i = 0; $i < $length; $i++) {
759        $randomString .= $characters[rand(0, $charactersLength - 1)];
760    }
761    return $randomString;
762}