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