· 4 years ago · Jul 13, 2021, 01:38 PM
1<?php
2
3include_once( get_theme_file_path('./helpers/env.php') );
4
5$END_POINT = get_env_name('SAS_URL') . get_env_token('SAS_TOKEN');
6$json_data = @file_get_contents($END_POINT);
7$consultants_data = json_decode($json_data, TRUE);
8$consultant_categories = [];
9$consultant_category_skills = [];
10$update_fields = [];
11$competence_value = [];
12
13// ! Updating the field second round (The redudant part)
14foreach ($consultants_data as $key => $consultant) {
15 $consultant_id = $consultant['user_id'];
16 $consultant_email = $consultant['email'];
17
18 // Getting consultant skills based on infrastructure, programming language and producer
19 if(($consultant['cv']['technologies'])) {
20 foreach ($consultant['cv']['technologies'] as $skills) {
21 asort($skills);
22
23 // Getting consultant skills based on infrastructure, programming language, producer and other
24 if($skills['category']['no'] !== null) {
25 $category = convert_from_norwegian_character(mb_strtolower($skills['category']['no'], 'UTF-8'));
26 $category_order = $category . '_order';
27
28 if($category) {
29 foreach ($skills['technology_skills'] as $skills) {
30 foreach ($consultant_categories as $consultant_category) {
31 $consultant_category_skills[$consultant_category] = [
32 $category => $skills['tags']['no'] ?? '',
33 $category_order => $skills['order'], // this will be example infrastruktur_order. We use these number to sort ascended
34 ];
35 }
36 }
37
38 // var_dump($consultant_competence_acf_value);
39 // usort($consultant_competence_acf_value, function ($data1, $data2) {
40 // return $data1[$category_order] <=> $data2[$category_order];
41 // });
42 }
43 }
44 }
45 }
46 }
47
48
49 foreach($consultant_category_skills as $meta_key_category => $meta_category_value) {
50var_dump($meta_category_value);
51 // Set up a mapping of meta fields which might change to the values from the API
52 $update_fields = [
53
54 $meta_key_category => $meta_category_value,
55 ];
56
57 if( count( $posts ) ) {
58 // If we found a matching post, loop through the fields to be updated and update them if necessary.
59 $post_id = $posts[0]->ID;
60
61 // Looping through the repeater and sub-field of the repeater
62 foreach( $update_fields as $field_selector => $field_value ) {
63 if( $field_value !== get_field( $field_selector, $post_id, false ) ) {
64 update_field( $field_selector, $field_value, $post_id );
65 }
66 }
67 } else {
68 // If a matching post was not found, create one, and merge the meta fields to be updated with some additional static ones.
69 $post_id = wp_insert_post([
70 'post_type' => 'konsulenter',
71 'post_title' => mb_convert_case($consultant['name'], MB_CASE_TITLE, 'UTF-8'),
72 'post_content' => 'lorem ipsum',
73 'post_status' => 'publish',
74 'meta_input' => array_merge(
75 $update_fields,
76 [
77 'konsulentens_api_id' => $consultant_id,
78 'stilling' => 'Partner / Systemarkitekt',
79 '_consultant_id' => $user_id
80 ]
81 )
82 ]);
83 }
84}
85