· 4 years ago · Jun 30, 2021, 10:30 AM
1<?php
2include_once( get_theme_file_path('./helpers/env.php') );
3
4$END_POINT = get_env_name('SAS_URL') . get_env_token('SAS_TOKEN');
5$json_data = @file_get_contents($END_POINT);
6$consultants_data = json_decode($json_data, TRUE);
7$consultant_categories = [];
8$update_fields = [];
9
10// ! Updating the field first round
11foreach ($consultants_data as $key => $consultant) {
12 $consultant_id = $consultant['user_id'];
13 $consultant_email = $consultant['email'];
14
15 // Retrieve WP users with the API ID
16 $users = get_users([
17 'meta_key' => 'konsulentens_api_id',
18 'meta_value' => $consultant_id,
19 ]);
20
21 if(count($users)) {
22 $user = $users[0];
23 } else {
24 // If no users were found, try to look up the user by email address
25 $user = get_user_by( 'email', $consultant_email );
26
27 // If a user was located by email, add their API ID to their user meta
28 if($user) {
29 update_user_meta( $user -> ID, 'konsulentens_api_id', $consultant_id );
30 }
31 }
32
33 if($user) {
34 $user_id = $user -> ID;
35 } else {
36 // If a user wasn't found by API ID or email address, create a new one.
37 $user_id = wp_create_user( $consultant_email, wp_generate_password(), $consultant_email );
38 $user = get_user_by('ID', $user_id );
39
40 update_user_meta( $user_id, 'konsulentens_api_id', $consultant_id );
41 }
42
43 // Now that we have a user, we'll update any data which is not be set or which might have changed.
44
45 $name_parts = explode(' ', $consultant['name']);
46 $first_name = ucfirst( array_shift( $name_parts ) ); // Let's consider the first word in the name string to be the first name,
47 $last_name = ucfirst( array_pop( $name_parts ) ); // the last word to be the last name,
48 $middle_name = ucfirst( implode( ' ', $name_parts ) ); // and everything (if anything) in-between to be the middle name.
49 $user_slug = strtolower( str_replace( ' ', '-', $consultant['name'] ) );
50
51 $consultant_bio_description = '';
52 $consultant_subject_area = [];
53
54 $consultant_competance_infrastructure = [];
55 $consultant_competance_coding_language = [];
56 $consultant_competance_producer = [];
57 $consultant_competence_network = [];
58 $consultant_competence_firewall = [];
59 $consultant_competence_identidy = [];
60 $consultant_competence_acf_value = [];
61
62 $consultant_certification = [];
63 $consultant_course = [];
64 $consultant_project_experience = [];
65 // Getting the Consultants biography values from the API
66 if($consultant['cv']['key_qualifications']) {
67 foreach( $consultant['cv']['key_qualifications'] as $description ) {
68 $consultant_bio_description .= $description['long_description']['no'];
69 }
70 }
71
72 // Getting the "fag område" values from the API
73 if($consultant['cv']['key_qualifications']) {
74 foreach ($consultant['cv']['key_qualifications'] as $qualification) {
75 foreach($qualification['key_points'] as $key_value) {
76 $consultant_subject_area[] = [
77 'fagomrader_tittel' => $key_value['name']['no']
78 ];
79 }
80 }
81 }
82
83 // Getting consultant skills based on infrastructure, programming language and producer
84 if(($consultant['cv']['technologies'])) {
85 foreach ($consultant['cv']['technologies'] as $skills) {
86 asort($skills);
87 if($skills['category']['no'] !== null) {
88 $category = convert_from_norwegian_character(mb_strtolower($skills['category']['no'], 'UTF-8'));
89
90 if($category) {
91 foreach ($skills['technology_skills'] as $skills) {
92 $consultant_competence_acf_value[] = [
93 $category => rand(1, 2021),
94 $category . '_order' => rand(1, 2021), // this will be example infrastruktur_order. We use these number to sort ascended
95 ];
96 }
97 }
98 }
99 }
100 }
101
102 if($consultant['cv']['certifications']){
103
104
105 // Getting every certifications from the consultant they have achieved
106 foreach($consultant['cv']['certifications'] as $certification) {
107 $consultant_certification[] = [
108 'sertifiserings_navn' => '',
109 'sertifiserings_organiser' => '',
110 'sertifiserings_gjennomforingsar' => rand(0, 2021),
111 'sertifisering_order' => rand(0, 2021),
112 ];
113 }
114 }
115
116 if(!empty($consultant['cv']['courses'])) {
117 // Sorting year descending of consulting certification
118 usort($consultant['cv']['courses'], function ($data1, $data2) {
119 return $data2['year'] <=> $data1['year'];
120 });
121
122 // Getting which course consultant did attend from the description
123 foreach($consultant['cv']['courses'] as $course) {
124 $consultant_course[] = [
125 'kurs_navn' => rand(0, 2021),
126 'kurs_beskrivelse' => rand(0, 2021),
127 'kurs_program' => rand(0, 2021),
128 'kurs_gjennomforingsar' => rand(0, 2021),
129 ];
130 }
131 }
132
133 // Getting consultant project experiences they have attended
134 if($consultant['cv']['project_experiences']) {
135
136 foreach($consultant['cv']['project_experiences'] as $project) {
137
138 if(!array_key_exists('year_to', $project)) {
139 $year_to = rand(0, 2021);
140 } else {
141 $year_to = rand(0, 2021);
142 }
143
144 if(array_key_exists('year_from', $project)) {
145 $year_from = rand(0, 2021);
146 } else {
147 $year_from = rand(0, 2021);
148 }
149
150 if($project['year_from'] === 0) {
151 $year_from = rand(0, 2021);
152 }
153
154 $consultant_project_experience[] = [
155 'prosjekt_kunde' => rand(1, 2021),
156 'prosjekt_sektor' => rand(1, 2021),
157 'prosjekt_beskrivelse' => rand(1, 2021),
158 'prosjekt_oppgaver' => rand(1, 2021),
159 'prosjekt_rolle' => $project['roles']['name']['no'] ?? '',
160 'prosjekt_order' => rand(1, 2021),
161 'prosjekt_periode_fra' => rand(1, 2021),
162 'prosjekt_periode_til' => rand(1, 2021),
163 ];
164
165 if( count( $posts ) ) {
166 // If we found a matching post, loop through the fields to be updated and update them if necessary.
167 $post_id = $posts -> ID;
168
169 foreach( $consultant_project_experience as $meta_key => $meta_value ) {
170 update_field( $meta_key, $meta_value, $post_id );
171 }
172 }
173
174 usort($consultant_project_experience, function ($data1, $data2) {
175 return $data1['prosjekt_order'] <=> $data2['prosjekt_order'];
176 });
177 }
178 }
179
180 // It would probably be better to first check if updating the user is really necessary by comparing the variables
181 // above with those in $user. But this is fine for now.
182 wp_update_user([
183 'ID' => $user_id,
184 'first_name' => $first_name,
185 'last_name' => implode( ' ', [ $middle_name, $last_name ] ),
186 'user_url' => get_site_url() . '/konsulenter/' . $user_slug,
187 'user_email' => $consultant_email,
188 'user_login' => $consultant_email,
189 'description' => "«" . $consultant_bio_description . "»"
190 ]);
191
192 $consultant_file_name = mb_strtolower(str_replace(' ', '-', $consultant['name']), 'UTF-8');
193
194 // Now find/create/update the respective post.
195
196 // Try to look up by API ID post meta
197 $posts = get_posts([
198 'post_type' => 'konsulenter',
199 'posts_per_page' => 1,
200 'meta_key' => 'konsulentens_api_id',
201 'meta_value' => $consultant_id
202 ]);
203
204 // Try to look up by email post meta
205 if( !count( $posts ) ) {
206 $posts = get_posts([
207 'post_type' => 'konsulenter',
208 'posts_per_page' => 1,
209 'meta_key' => 'konsulentens_epost',
210 'meta_value' => $consultant_email
211 ]);
212 }
213
214 if(($consultant['cv']['technologies'])) {
215 foreach ($consultant['cv']['technologies'] as $skills) {
216 if($skills['category']['no'] !== null) {
217 $category = convert_from_norwegian_character(mb_strtolower($skills['category']['no'], 'UTF-8'));
218 $acf_meta = ctype_space($category) ? 'ferdigheter_' . $category : 'ferdigheter_' . mb_strtolower(str_replace(' ', '_', $category), 'UTF-8');
219
220 if(!in_array($acf_meta, $consultant_categories)) {
221 array_push($consultant_categories, $acf_meta);
222 }
223 }
224 }
225 }
226 // Here it doesn't work what I am trying to achieve
227 foreach ($consultant_categories as $category) {
228 $update_fields[] = [
229 'fornavn' => $first_name,
230 'etternavn' => implode( ' ', [ $middle_name, $last_name ] ),
231 'konsulentens_epost' => $consultant_email,
232 'konsulentens_telefonnummer' => $consultant['telephone'],
233 'sammendrag_av_konsulent' => $consultant_bio_description,
234 'fagomrader' => $consultant_subject_area,
235 $category => '',
236 'sertifisering' => $consultant_certification,
237 'kurs' => $consultant_course,
238 'prosjekt_erfaring' => $consultant_project_experience,
239 ];
240 }
241
242 if( count( $posts ) ) {
243 // If we found a matching post, loop through the fields to be updated and update them if necessary.
244 $post_id = $posts[0]->ID;
245
246 // Looping through the repeater and sub-field of the repeater
247 foreach( $update_fields as $field_selector => $field_value ) {
248 if( $field_value !== get_field( $field_selector, $post_id, false ) ) {
249 update_field( $field_selector, $field_value, $post_id );
250 }
251 }
252
253 } else {
254 // If a matching post was not found, create one, and merge the meta fields to be updated with some additional static ones.
255 $post_id = wp_insert_post([
256 'post_type' => 'konsulenter',
257 'post_title' => mb_convert_case($consultant['name'], MB_CASE_TITLE, 'UTF-8'),
258 'post_content' => 'lorem ipsum',
259 'post_status' => 'publish',
260 'meta_input' => array_merge(
261 $update_fields,
262 [
263 'konsulentens_api_id' => $consultant_id,
264 'stilling' => 'Partner / Systemarkitekt',
265 '_consultant_id' => $user_id
266 ]
267 )
268 ]);
269 }
270}