· 6 years ago · Jan 25, 2019, 12:06 PM
1<?php
2 ob_start();
3 global $plugin_dir_path;
4 $plugin_dir_path = plugin_dir_path( __FILE__ ); // plugin main directory
5 include($plugin_dir_path . 'const.php');
6 function hasProperty($property)
7 {
8 return property_exists($this, $property) && isset($this->$property);
9 }
10 function wp_exist_car_by_id( $id ) {
11 global $wpdb;
12 $return = $wpdb->get_row( "SELECT ID FROM wplexussq23_posts WHERE ID = '" . $id . "' && post_status = 'publish' && post_type = 'car' ", 'ARRAY_N' );
13 if( empty( $return ) ) {
14 return false;
15 } else {
16 return true;
17 }
18 }
19 $user_lexus = array(
20 'user' => 'maciej.piechowiak@lexus-poznan.pl',
21 'password' => '9N3HJRDW'
22 );
23
24
25 $user_toyota = array(
26 'user' => 'pawel.hoffmann@toyota-ukleja.pl',
27 'password' => 'toyotaukleja'
28 );
29
30
31 $api_url = 'https://www.otomoto.pl/api/open/';
32 $token_lexus = getAccessToken($api_url . 'oauth/token', $user_lexus['user'], $user_lexus['password']);
33 $token_toyota = getAccessToken($api_url . 'oauth/token', $user_toyota['user'], $user_toyota['password']);
34
35 $adverts_lexus = getData($token_lexus, $api_url . 'account/adverts');
36 $adverts_toyota = getData($token_toyota, $api_url . 'account/adverts');
37 $all = array_merge($adverts_lexus->results, $adverts_toyota->results);
38 $categories = getData($token_lexus, $api_url. 'categories/29/makes');
39 create_new_cars_posts($all, $api_url, $token_lexus, $categories, $CAR_CARTYPE, $CAR_COLORS, $COMFORT_TAGS, $CAR_FUEL_TYPE, $CAR_GEARBOX, $CAR_OTHER_TAGS, $CAR_PRICE_OPTIONS,$CAR_SAFETY_TAGS, $CAR_TRANSMISSION, $COUNTRIES);
40
41 function getAccessToken($url, $user, $userpassword)
42 {
43 $curl = curl_init();
44
45 curl_setopt($curl, CURLOPT_POST, 1);
46
47 $fields = array(
48 'grant_type' => 'password',
49 'client_id' => '993',
50 'client_secret' => '3ed1c7d964b8a207da87f43b77bbbd1b',
51 'username' => $user,
52 'password' => $userpassword,
53 );
54
55 $fieldsString = '';
56 foreach ($fields as $key => $value) {
57 $fieldsString .= $key.'='.$value.'&';
58 }
59 rtrim($fieldsString, '&');
60
61 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
62 curl_setopt($curl, CURLOPT_HEADER, "Content-Type: application/json");
63 curl_setopt($curl, CURLOPT_POSTFIELDS, $fieldsString);
64 curl_setopt($curl, CURLOPT_URL, $url);
65 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
66 curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
67
68 $result = curl_exec($curl);
69
70 curl_close($curl);
71
72 return json_decode($result)->access_token;
73 }
74
75 function getData($accessToken, $url)
76 {
77 $curl = curl_init();
78
79 curl_setopt(
80 $curl,
81 CURLOPT_HTTPHEADER,
82 [
83 'Content-Type: application/json',
84 'Authorization: Bearer '. $accessToken
85 ]
86 );
87
88 curl_setopt($curl, CURLOPT_URL, $url);
89 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
90 curl_setopt($curl, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
91 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
92 $result = curl_exec($curl);
93
94 curl_close($curl);
95
96 return json_decode($result);
97 }
98 function create_new_cars_posts(
99 $adverts, $api_url, $token_lexus, $categories, $car_type, $colors, $comfort, $fuel_type, $gearbox, $other, $price_options, $safety, $transmission_type, $countries)
100 {
101 global $post;
102 $cars = $adverts;
103
104
105 if($cars) {
106
107 foreach($cars as $car) {
108 $car_id = $car->id;
109 $ID = $car_id;
110 // if(!get_page($ID, OBJECT, 'car'))
111 // {
112 if($car->status == 'active')
113 {
114
115 $new_post = array(
116 // 'ID' => $ID,
117 'post_title' => $car->title . $car->id,
118 'post_type' => 'car',
119 'post_status' => 'publish',
120 'taxonomy' => 'models',
121 'import_id' => $ID,
122 );
123
124
125 if(wp_exist_car_by_id($new_post['import_id']) == false)
126 {
127 $new_post_id = wp_insert_post($new_post);
128 $count_photos = ($car->photos != '') ? count(get_object_vars($car->photos)) : '';
129 $make = ($car->params->make !== '') ? $car->params->make : '';
130 $model = ($car->params->model != '') ? $car->params->model : '';
131 $unique_id = ($car->id != '') ? $car->id : '';
132 $price = ($car->params->price->{1} != '') ? $car->params->price->{1} : '';
133 $features = ($car->params->features != '') ? $car->params->features : '';
134 $made = ($car->params->year != '') ? $car->params->year : '';
135 $fuel = ($car->params->fuel_type != '') ? $car->params->fuel_type : '';
136 $color = ($car->params->color != '') ? $car->params->color : '';
137 $description = ($car->description != '') ? $car->description : '';
138 $mileage = ($car->params->mileage != '') ? $car->params->mileage : '';
139 $horse_power = ($car->params->engine_power != '') ? $car->params->engine_power : '';
140 $body_type = ($car->params->body_type != '') ? $car->params->body_type : '';
141 $last_update = $car->last_update_date;
142
143 update_post_meta($new_post_id, 'ostatni_update', $last_update, true);
144 if(array_key_exists("country_origin", $car->params))
145 {
146 $country = ($car->params->country_origin != '') ? $car->params->country_origin: '';
147 }
148
149 $capacity = ($car->params->engine_capacity != '') ? $car->params->engine_capacity : '';
150 // $otomoto_thumbnail =
151 // ($car->photos->$count_photos->{"1080x720"} != NULL) ?
152 // $car->photos->{$count_photos - 1} ->{"1080x720"}
153 // '' :
154 // '';
155 $gallery = ($car->photos != NULL) ? $car->photos : '';
156 if (array_key_exists("transmission", $car->params))
157 {
158 $transmission = ($car->params->transmission != '') ? $car->params->transmission : '';
159 $transmissionValue = ($transmission != '') ? $transmission_type[$transmission] : '';
160 }
161
162 $which_gear = ($car->params->gearbox != '') ? $car->params->gearbox : '';
163 $colorValue = ($color != '') ? $colors[$color] : '';
164 $bodyTypeValue = ($body_type != '') ? $car_type[$body_type] : '';
165 $fuelTypeValue = ($fuel != '') ? $fuel_type[$fuel] : '';
166 $whichGearValue = ($which_gear != '') ? $gearbox[$which_gear] : '';
167 // $countryValue = ($country != '') ? $countries[$country] : '';
168
169 $first_registration = $car->params;
170 if (array_key_exists("date_registration", $first_registration))
171 {
172 $dataValue = ($first_registration->{"date_registration"} != '') ? $first_registration->{"date_registration"} : '';
173 }
174
175 $car_make_name = ($categories != NULL) ? $categories : '';
176 $car_make_name_obj = ($car_make_name != NULL)? $car_make_name->options->$make : NULL;
177 $term_parent = term_exists($car_make_name_obj->pl, 'models');
178 if($term_parent == 0 && $term_parent == null) {
179 $parent_term = wp_insert_term( $car_make_name_obj->pl, 'models');
180 wp_set_object_terms( $new_post_id, $parent_term->term_id, 'models', true);
181 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
182 wp_update_term_count_now($terms, 'models');
183 // wp_update_term_count_now($parent_term->term_id, 'models');
184 } else {
185 $parent_term = get_term_by('slug', $make, 'models');
186 wp_set_object_terms( $new_post_id, $parent_term->term_id, 'models', true);
187 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
188 wp_update_term_count_now($terms, 'models');
189 // wp_update_term_count_now($parent_term->term_id, 'models');
190 }
191
192 $car_makes_name_obj = getData($token_lexus, $api_url . 'categories/29/models/' . $make );
193 $car_model_name = $car_makes_name_obj->options->$model->pl;
194
195 $term_child = term_exists($car_model_name, 'models');
196 if($term_child == 0 && $term_child == null) {
197 $parent_term = get_term_by('slug', $make, 'models');
198 $child_term = wp_insert_term($car_model_name, 'models', $args = array(
199 'parent' => $parent_term->term_id,
200 'slug' => $model,
201 ));
202 wp_set_object_terms( $new_post_id, $child_term->term_id, 'models', true);
203 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
204 wp_update_term_count_now($terms, 'models');
205 // wp_update_term_count_now($child_term->term_id, 'models');
206 } else {
207 $child_term = get_term_by('slug', $model, 'models');
208 wp_set_object_terms( $new_post_id, $child_term->term_id, 'models', true);
209 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
210 wp_update_term_count_now($terms, 'models');
211 // wp_update_term_count_now($child_term->term_id, 'models');
212 }
213 if($price != '')
214 update_post_meta($new_post_id, 'price', $price, true);
215 if($fuelTypeValue != '')
216 update_post_meta($new_post_id, 'fuel', $fuelTypeValue, true);
217 if($made != '')
218 update_post_meta($new_post_id, 'made', $made, true);
219 if($mileage != '')
220 update_post_meta($new_post_id, 'milage', $mileage, true);
221 if($capacity != '')
222 update_post_meta($new_post_id, 'capacity', $capacity, true);
223 if($colorValue != '')
224 update_post_meta($new_post_id, 'color', $colorValue, true);
225 if($horse_power != '')
226 update_post_meta($new_post_id, 'horse_power', $horse_power, true);
227 if($transmissionValue != '')
228 update_post_meta($new_post_id, 'driving_gear', $transmissionValue, true);
229 if($whichGearValue != '')
230 update_post_meta($new_post_id, 'which_gear', $whichGearValue, true);
231 if($bodyTypeValue != '')
232 update_post_meta($new_post_id, 'top_body', $bodyTypeValue, true);
233 if($dataValue != '')
234 update_post_meta($new_post_id, 'first_registration', $dataValue, true);
235 // if($countryValue != '')
236 // update_post_meta($new_post_id, 'country', $countryValue, true);
237 // if($otomoto_thumbnail != '')
238 // update_post_meta($new_post_id, 'otomoto_thumbnail', $otomoto_thumbnail, true);
239 if($parent_term->name && $child_term->name)
240 update_post_meta($new_post_id, 'visible_title', $parent_term->name . ' ' .$child_term->name, true);
241 if($car->title != '')
242 update_post_meta($new_post_id, 'front_title', $car->title, true);
243
244 $row = 0;
245 if($gallery) {
246 foreach($gallery as $key=>$value) {
247 $row = array(
248 'field_5bc6f98079f40' => $value->{"732x488"},
249 );
250 $row++;
251 add_row('field_5bc6f95779f3f', $row, $new_post_id);
252 }
253 }
254
255 // tags
256 $feature_tags = array();
257 foreach( $features as $feature ) {
258 if(array_key_exists($feature, $comfort)) {
259 $existscomfortTerm = term_exists($comfort[$feature], 'comfort' );
260 if($existscomfortTerm == 0 && $existscomfortTerm == NULL) {
261 $comfort_term = wp_insert_term($comfort[$feature], 'comfort', $args = array(
262 'slug' => $comfort[$feature],
263 ));
264 wp_set_object_terms( $new_post_id, $comfort_term->term_id, 'comfort', true);
265 } else {
266 $comfort_term = get_term_by('name', $comfort[$feature], 'comfort');
267 wp_set_object_terms( $new_post_id, $comfort_term->term_id, 'comfort', true);
268 }
269 }
270 if(array_key_exists($feature, $other)) {
271 $existsotherTerm = term_exists($other[$feature], 'other' );
272 if($existsotherTerm == 0 && $existsotherTerm == NULL) {
273 $other_term = wp_insert_term($other[$feature], 'other', $args = array(
274 'slug' => $other[$feature],
275 ));
276 wp_set_object_terms( $new_post_id, $other_term->term_id, 'other', true);
277 } else {
278 $other_term = get_term_by('name', $other[$feature], 'other');
279 wp_set_object_terms( $new_post_id, $other_term->term_id, 'other', true);
280 }
281 }
282 if(array_key_exists($feature, $safety)) {
283 $existssafetyTerm = term_exists($safety[$feature], 'safety' );
284 if($existssafetyTerm == 0 && $existssafetyTerm == NULL) {
285 $safety_term = wp_insert_term($safety[$feature], 'safety', $args = array(
286 'slug' => $safety[$feature],
287 ));
288 wp_set_object_terms( $new_post_id, $safety_term->term_id, 'safety', true);
289 } else {
290 $safety_term = get_term_by('name', $safety[$feature], 'safety');
291 wp_set_object_terms( $new_post_id, $safety_term->term_id, 'safety', true);
292 }
293 }
294 }
295 $my_post = array(
296 'ID' => $new_post_id,
297 'post_content' => $description,
298 'import_id' => $ID,
299 );
300 wp_update_post($my_post);
301
302 // }
303
304 }
305 else if(wp_exist_car_by_id($car->id) == true) {
306 $existingPostId = $car->id;
307 if($car->last_update_date != get_post_meta($existingPostId, 'ostatni_update'))
308 {
309 $last_update = $car->last_update_date;
310 $count_photos = ($car->photos != '') ? count(get_object_vars($car->photos)) : '';
311 $make = ($car->params->make !== '') ? $car->params->make : '';
312 $model = ($car->params->model != '') ? $car->params->model : '';
313 $unique_id = ($car->id != '') ? $car->id : '';
314 $price = ($car->params->price->{1} != '') ? $car->params->price->{1} : '';
315 $features = ($car->params->features != '') ? $car->params->features : '';
316 $made = ($car->params->year != '') ? $car->params->year : '';
317 $fuel = ($car->params->fuel_type != '') ? $car->params->fuel_type : '';
318 $color = ($car->params->color != '') ? $car->params->color : '';
319 $description = ($car->description != '') ? $car->description : '';
320 $mileage = ($car->params->mileage != '') ? $car->params->mileage : '';
321 $horse_power = ($car->params->engine_power != '') ? $car->params->engine_power : '';
322 $body_type = ($car->params->body_type != '') ? $car->params->body_type : '';
323
324 if(array_key_exists("country_origin", $car->params))
325 {
326 $country = ($car->params->country_origin != '') ? $car->params->country_origin: '';
327 }
328
329 $capacity = ($car->params->engine_capacity != '') ? $car->params->engine_capacity : '';
330 // $otomoto_thumbnail =
331 // ($car->photos->$count_photos->{"1080x720"} != NULL) ?
332 // $car->photos->{$count_photos - 1} ->{"1080x720"}
333 // '' :
334 // '';
335 $gallery = ($car->photos != NULL) ? $car->photos : '';
336 if (array_key_exists("transmission", $car->params))
337 {
338 $transmission = ($car->params->transmission != '') ? $car->params->transmission : '';
339 $transmissionValue = ($transmission != '') ? $transmission_type[$transmission] : '';
340 }
341
342 $which_gear = ($car->params->gearbox != '') ? $car->params->gearbox : '';
343 $colorValue = ($color != '') ? $colors[$color] : '';
344 $bodyTypeValue = ($body_type != '') ? $car_type[$body_type] : '';
345 $fuelTypeValue = ($fuel != '') ? $fuel_type[$fuel] : '';
346 $whichGearValue = ($which_gear != '') ? $gearbox[$which_gear] : '';
347 // $countryValue = ($country != '') ? $countries[$country] : '';
348
349 $first_registration = $car->params;
350 if (array_key_exists("date_registration", $first_registration))
351 {
352 $dataValue = ($first_registration->{"date_registration"} != '') ? $first_registration->{"date_registration"} : '';
353 }
354
355 $car_make_name = ($categories != NULL) ? $categories : '';
356 $car_make_name_obj = ($car_make_name != NULL)? $car_make_name->options->$make : NULL;
357 $term_parent = term_exists($car_make_name_obj->pl, 'models');
358 if($term_parent == 0 && $term_parent == null) {
359 $parent_term = wp_insert_term( $car_make_name_obj->pl, 'models');
360 wp_set_object_terms( $existingPostId, $parent_term->term_id, 'models', true);
361 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
362 wp_update_term_count_now($terms, 'models');
363 // wp_update_term_count_now($parent_term->term_id, 'models');
364 } else {
365 $parent_term = get_term_by('slug', $make, 'models');
366 wp_set_object_terms($existingPostId, $parent_term->term_id, 'models', true);
367 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
368 wp_update_term_count_now($terms, 'models');
369 // wp_update_term_count_now($parent_term->term_id, 'models');
370 }
371
372 $car_makes_name_obj = getData($token_lexus, $api_url . 'categories/29/models/' . $make );
373 $car_model_name = $car_makes_name_obj->options->$model->pl;
374
375 $term_child = term_exists($car_model_name, 'models');
376 if($term_child == 0 && $term_child == null) {
377 $parent_term = get_term_by('slug', $make, 'models');
378 $child_term = wp_insert_term($car_model_name, 'models', $args = array(
379 'parent' => $parent_term->term_id,
380 'slug' => $model,
381 ));
382 wp_set_object_terms( $existingPostId, $child_term->term_id, 'models', true);
383 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
384 wp_update_term_count_now($terms, 'models');
385 } else {
386 $child_term = get_term_by('slug', $model, 'models');
387 wp_set_object_terms( $existingPostId, $child_term->term_id, 'models', true);
388 $terms = get_terms('models', array('hide_empty' => 0, 'fields' => 'ids'));
389 wp_update_term_count_now($terms, 'models');
390 }
391 if($price != get_post_meta($existingPostId,'price'))
392 update_post_meta($existingPostId, 'price', $price, true);
393 if($fuelTypeValue != get_post_meta($existingPostId,'fuel'))
394 update_post_meta($existingPostId, 'fuel', $fuelTypeValue, true);
395 if($made != get_post_meta($existingPostId,'made'))
396 update_post_meta($existingPostId, 'made', $made, true);
397 if($mileage != get_post_meta($existingPostId,'milage'))
398 update_post_meta($existingPostId, 'milage', $mileage, true);
399 if($capacity != get_post_meta($existingPostId,'capacity'))
400 update_post_meta($existingPostId, 'capacity', $capacity, true);
401 if($colorValue != get_post_meta($existingPostId, 'color'))
402 update_post_meta($existingPostId, 'color', $colorValue, true);
403 if($horse_power != get_post_meta($existingPostId,'horse_power'))
404 update_post_meta($existingPostId, 'horse_power', $horse_power, true);
405 if($transmissionValue != get_post_meta($existingPostId,'driving_gear'))
406 update_post_meta($existingPostId, 'driving_gear', $transmissionValue, true);
407 if($whichGearValue != get_post_meta($existingPostId,'which_gear'))
408 update_post_meta($existingPostId, 'which_gear', $whichGearValue, true);
409 if($bodyTypeValue != get_post_meta($existingPostId,'top_body'))
410 update_post_meta($existingPostId, 'top_body', $bodyTypeValue, true);
411 if($dataValue != get_post_meta($existingPostId,'first_registration'))
412 update_post_meta($existingPostId, 'first_registration', $dataValue, true);
413 // if($countryValue != '')
414 // update_post_meta($new_post_id, 'country', $countryValue, true);
415 // if($otomoto_thumbnail != '')
416 // update_post_meta($new_post_id, 'otomoto_thumbnail', $otomoto_thumbnail, true);
417 if($parent_term->name && $child_term->name)
418 update_post_meta($existingPostId, 'visible_title', $parent_term->name . ' ' .$child_term->name, true);
419 if($car->title != get_post_meta($existingPostId,'front_title'))
420 update_post_meta($existingPostId, 'front_title', $car->title, true);
421
422 $row = 0;
423 if($gallery) {
424 foreach($gallery as $key=>$value) {
425 $row = array(
426 'field_5bc6f98079f40' => $value->{"732x488"},
427 );
428 $row++;
429 add_row('field_5bc6f95779f3f', $row, $existingPostId);
430 }
431 }
432
433 // tags
434 $feature_tags = array();
435 foreach( $features as $feature ) {
436 if(array_key_exists($feature, $comfort)) {
437 $existscomfortTerm = term_exists($comfort[$feature], 'comfort' );
438 if($existscomfortTerm == 0 && $existscomfortTerm == NULL) {
439 $comfort_term = wp_insert_term($comfort[$feature], 'comfort', $args = array(
440 'slug' => $comfort[$feature],
441 ));
442 wp_set_object_terms( $existingPostId, $comfort_term->term_id, 'comfort', true);
443 } else {
444 $comfort_term = get_term_by('name', $comfort[$feature], 'comfort');
445 wp_set_object_terms( $existingPostId, $comfort_term->term_id, 'comfort', true);
446 }
447 }
448 if(array_key_exists($feature, $other)) {
449 $existsotherTerm = term_exists($other[$feature], 'other' );
450 if($existsotherTerm == 0 && $existsotherTerm == NULL) {
451 $other_term = wp_insert_term($other[$feature], 'other', $args = array(
452 'slug' => $other[$feature],
453 ));
454 wp_set_object_terms( $existingPostId, $other_term->term_id, 'other', true);
455 } else {
456 $other_term = get_term_by('name', $other[$feature], 'other');
457 wp_set_object_terms( $existingPostId, $other_term->term_id, 'other', true);
458 }
459 }
460 if(array_key_exists($feature, $safety)) {
461 $existssafetyTerm = term_exists($safety[$feature], 'safety' );
462 if($existssafetyTerm == 0 && $existssafetyTerm == NULL) {
463 $safety_term = wp_insert_term($safety[$feature], 'safety', $args = array(
464 'slug' => $safety[$feature],
465 ));
466 wp_set_object_terms( $existingPostId, $safety_term->term_id, 'safety', true);
467 } else {
468 $safety_term = get_term_by('name', $safety[$feature], 'safety');
469 wp_set_object_terms( $existingPostId, $safety_term->term_id, 'safety', true);
470 }
471 }
472 }
473 update_post_meta($existingPostId, 'ostatni_update', $last_update, true);
474 $my_existing_post = array(
475 'ID' => $existingPostId,
476 'post_title' => $car->title . $car->id,
477 'post_content' => $description,
478 'import_id' => $ID,
479 );
480 wp_update_post($my_existing_post);
481 }
482
483 }
484 }
485 }
486 }
487 }
488 ?>