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