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