PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 5.7.3
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v5.7.3
7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 6.0.5 All 36 releases
mlsimport / includes / ThemeImport.php

ThemeImport.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 5.7.3, at includes/ThemeImport.php

1,147 lines 32.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4 }
5
6 /**
7 * Description of ThemeImport
8 *
9 * @author cretu
10 */
11 class ThemeImport {
12
13 // put your code here
14
15 public $theme;
16 public $plugin_name;
17 public $enviroment;
18 public $encoded_values;
19 /**
20 * class construct
21 *
22 * @since 1.0.0
23 * @access protected
24 * @var string $plugin_name
25 */
26
27
28 /*
29 *
30 *
31 * Api Request to MLSimport APi
32 *
33 *
34 *
35 * */
36
37 public function global_api_request_CURL_saas( $method, $values_array, $type = 'GET' ) {
38 global $mlsimport;
39 $url = MLSIMPORT_API_URL . $method;
40 $headers = array( 'Content-Type' => 'text/plain' );
41
42 if ( 'token' !== $method ) {
43 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();
44 $headers = array();
45 $headers['Content-Type'] = 'application/json';
46 $headers['authorizationToken'] = $token;
47
48 }
49
50
51 $args = array(
52 'method' => 'GET',
53 'headers' => $headers,
54 'body' => wp_json_encode($values_array),
55 'timeout' => 120,
56 'redirection' => 10,
57 'httpversion' => '1.1',
58 'blocking' => true,
59 'user-agent' => $_SERVER['HTTP_USER_AGENT']
60 );
61
62
63 if(empty($values_array)){
64 unset($args['body']);
65 }
66
67
68
69 // Choose the appropriate WordPress HTTP API function based on the request type
70 if ( 'GET' === $type ) {
71 $response = wp_remote_get( $url, $args );
72 } else {
73 $args['method'] = $type;
74 $response = wp_remote_post( $url, $args );
75 }
76
77
78
79 // Handle response
80 if ( is_wp_error( $response ) ) {
81 return $response->get_error_message();
82 } else {
83 $body = wp_remote_retrieve_body( $response );
84 $to_return = json_decode( $body, true );
85
86 return $to_return;
87 }
88 }
89
90
91
92
93
94 /*
95 *
96 *
97 * Api Request to MLSimport APi
98 *
99 *
100 *
101 * */
102
103
104 public static function global_api_request_saas( $method, $values_array, $type = 'GET' ) {
105 global $mlsimport;
106 $url = MLSIMPORT_API_URL . $method;
107
108 $headers = array();
109 if ( 'token' !== $method && 'mls' !== $method ) {
110 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();
111 $headers = array(
112 'authorizationToken' => $token,
113 'Content-Type' => 'application/json',
114 );
115 }
116
117
118 $arguments = array(
119 'method' => $type,
120 'timeout' => 45,
121 'redirection' => 5,
122 'httpversion' => '1.0',
123 'blocking' => true,
124 'headers' => $headers,
125 'cookies' => array(),
126 );
127
128 if ( is_array( $values_array ) && ! empty( $values_array ) ) {
129 $arguments['body'] = wp_json_encode( $values_array );
130 }
131
132 $response = wp_remote_post( $url, $arguments );
133
134
135 if ( is_wp_error( $response ) ) {
136 // It is a WordPress error.
137 $error_code = $response->get_error_code();
138 $error_message = esc_html( $response->get_error_message( $error_code ) );
139 $warning_message = sprintf(
140 esc_html__( 'You have a WordPress Error. Error code: %s. Error Description: %s', 'mlsimport' ),
141 esc_html( $error_code ),
142 $error_message
143 );
144 print esc_html( '<div class="mlsimport_warning">' . $warning_message . '</div>' );
145
146 $received_data['succes'] = false;
147 return $received_data;
148 }
149
150 if ( isset( $response['response']['code'] ) && 200 === $response['response']['code'] ) {
151 $received_data = json_decode( wp_remote_retrieve_body( $response ), true );
152
153 return $received_data;
154 } else {
155 $received_data['succes'] = false;
156 return $received_data;
157 }
158 exit();
159 }
160
161
162
163
164
165
166
167
168 /**
169 *
170 *
171 *
172 * Parse Result Array
173 */
174 public function mlsimport_saas_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key, $mlsimport_item_option_data ) {
175 $logs = '';
176
177 wp_cache_flush();
178 gc_collect_cycles();
179 $counter_prop = 0;
180 foreach ( $ready_to_parse_array['data'] as $key => $property ) {
181 ++$counter_prop;
182
183 $logs = $this->mlsimport_mem_usage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL;
184 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' );
185
186
187 wp_cache_delete( 'mlsimport_force_stop_' . $item_id_array['item_id'], 'options' );
188
189 $status = get_option( 'mlsimport_force_stop_' . $item_id_array['item_id'] );
190 $logs = $this->mlsimport_mem_usage() . ' / on Batch ' . $item_id_array['batch_counter'] . ', Item ID: ' . $item_id_array['item_id'] . '/' . $counter_prop . ' check ListingKey ' . $property['ListingKey'] . ' - stop command issued ? ' . $status . PHP_EOL;
191 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' );
192
193 if ( 'no' === $status ) {
194 $logs = 'Will proceed to import - Memory Used ' . $this->mlsimport_mem_usage() . PHP_EOL;
195 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' );
196 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'normal', $mlsimport_item_option_data );
197 } else {
198 update_post_meta( $item_id_array['item_id'], 'mlsimport_spawn_status', 'completed' );
199 }
200 unset( $logs );
201 }
202
203 unset( $ready_to_parse_array );
204 unset( $logs );
205 }
206
207
208 /**
209 *
210 *
211 * Parse Result Array in CROn
212 */
213 public function mlsimport_saas_cron_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key ) {
214
215 $mlsimport_item_option_data = array();
216 $mlsimport_item_option_data['mlsimport_item_standardstatus'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_standardstatus', true );
217 $mlsimport_item_option_data['mlsimport_item_property_user'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_user', true );
218 $mlsimport_item_option_data['mlsimport_item_agent'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_agent', true );
219 $mlsimport_item_option_data['mlsimport_item_property_status'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_status', true );
220
221 foreach ( $ready_to_parse_array['data'] as $key => $property ) {
222 $logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL;
223 mlsimport_saas_single_write_import_custom_logs( $logs, 'cron' );
224 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'cron', $mlsimport_item_option_data );
225 }
226 }
227
228
229
230
231
232
233
234 /**
235 *
236 *
237 * check if property already imported
238 */
239 public function mlsimport_saas_retrive_property_by_id( $key, $post_type = 'estate_property' ) {
240
241 $args = array(
242 'post_type' => $post_type,
243 'post_status' => 'any',
244
245 'meta_query' => array(
246 array(
247 'key' => 'ListingKey',
248 'value' => $key,
249 'compare' => '=',
250 ),
251 ),
252 'fields' => 'ids',
253 );
254
255 $prop_selection = new WP_Query( $args );
256 if ( $prop_selection->have_posts() ) {
257 while ( $prop_selection->have_posts() ) {
258 $prop_selection->the_post();
259 $the_id = get_the_ID();
260 wp_reset_postdata();
261 return $the_id;
262 }
263 } else {
264 wp_reset_postdata();
265 return 0;
266 }
267 }
268
269
270
271
272
273 /**
274 * clear taxonomy
275 *
276 * @since 1.0.0
277 * @access protected
278 * @var string $plugin_name
279 */
280 public function mlsimport_saas_clear_property_for_taxonomy( $property_id, $taxonomies ) {
281
282 if ( is_array( $taxonomies ) ) :
283 foreach ( $taxonomies as $taxonomy => $term ) :
284 if (is_wp_error($taxonomy)) {
285 error_log('Error with taxonomy: ' . $taxonomy->get_error_message());
286 continue; // Skip this iteration
287 }
288
289 if ( taxonomy_exists($taxonomy) ) {
290 wp_delete_object_term_relationships($property_id, $taxonomy);
291 } else {
292 error_log("Taxonomy does not exist: {$taxonomy}");
293 }
294 endforeach;
295 endif;
296 }
297
298
299
300
301
302
303
304
305 /**
306 * return non encoded encoded values
307 *
308 * @since 1.0.0
309 * @access protected
310 * @var string $plugin_name
311 */
312 public function mlsimport_saas_return_non_encoded_value( $item, $encoded_values ) {
313
314 if ( is_array( $item ) ) {
315 if ( ! empty( $encoded_values ) ) {
316 foreach ( $item as $key => $value ) {
317 if ( isset( $encoded_values [ $value ] ) ) {
318 $item[ $key ] = $encoded_values [ $value ];
319 }
320 }
321 }
322 return $item;
323 } elseif ( ! empty( $encoded_values ) && isset( $encoded_values [ $item ] ) ) {
324 return $encoded_values [ $item ];
325 } else {
326 return $item;
327 }
328 }
329
330 /**
331 * set taxonomy
332 *
333 * @since 1.0.0
334 * @access protected
335 * @var string $plugin_name
336 */
337 public function mlsimport_saas_update_taxonomy_for_property( $taxonomy, $property_id, $field_values ) {
338
339 if ( ! is_array( $field_values ) ) {
340 if ( strpos( $field_values, ',' ) !== false ) {
341 $field_values = explode( ',', $field_values );
342 } else {
343 $field_values = array( $field_values );
344 }
345 }
346
347 // Remove empty values and decode values in one pass
348 $processed_field_values = array();
349 foreach ( $field_values as $value ) {
350 if ( ! empty( $value ) ) {
351 $processed_field_values[] = $value;
352 }
353 }
354
355 // Bulk update terms if array is not empty
356 if ( ! empty( $processed_field_values ) ) {
357 wp_set_object_terms( $property_id, $processed_field_values, $taxonomy, true );
358 clean_term_cache( $property_id, $taxonomy );
359 }
360 }
361
362
363
364
365 /**
366 * Set Property Title
367 *
368 * @since 1.0.0
369 * @access protected
370 * @var string $plugin_name
371 */
372 public function mlsimport_saas_update_property_title( $property_id, $mls_import_post_id, $property ) {
373
374 global $mlsimport;
375
376 $title_format = esc_html( get_post_meta( $mls_import_post_id, 'mlsimport_item_title_format', true ) );
377
378 if ( '' === $title_format ) {
379 $options = get_option( 'mlsimport_admin_mls_sync' );
380 $title_format = $options['title_format'];
381 }
382
383 $start = '{';
384 $end = '}';
385 $title_array = $this->str_between_all( $title_format, $start, $end );
386
387 $property_extra_meta_array_lowecase = array_change_key_case( $property['extra_meta'], CASE_LOWER );
388
389 foreach ( $title_array as $key => $value ) {
390 $replace = '';
391 if ( 'Address' === $value ) {
392 if ( isset( $property['adr_title'] ) ) {
393 $replace = $property['adr_title'];
394 }
395 $title_format = str_replace( '{Address}', $replace, $title_format );
396 } elseif ( 'City' === $value ) {
397 if ( isset( $property['adr_city'] ) ) {
398 $replace = $property['adr_city'];
399 }
400 $title_format = str_replace( '{City}', $replace, $title_format );
401 } elseif ( 'CountyOrParish' === $value ) {
402 if ( isset( $property['adr_county'] ) ) {
403 $replace = $property['adr_county'];
404 }
405 $title_format = str_replace( '{CountyOrParish}', $replace, $title_format );
406 } elseif ( 'PropertyType' === $value ) {
407 if ( isset( $property['adr_type'] ) ) {
408 $replace = $property['adr_type'];
409 }
410 $title_format = str_replace( '{PropertyType}', $replace, $title_format );
411 } elseif ( 'Bedrooms' === $value ) {
412 if ( isset( $property['adr_bedrooms'] ) ) {
413 $replace = $property['adr_bedrooms'];
414 }
415 $title_format = str_replace( '{Bedrooms}', $replace, $title_format );
416 } elseif ( 'Bathrooms' === $value ) {
417 if ( isset( $property['adr_bathrooms'] ) ) {
418 $replace = $property['adr_bathrooms'];
419 }
420 $title_format = str_replace( '{Bathrooms}', $replace, $title_format );
421 } elseif ( 'ListingKey' === $value ) {
422 $replace = $property['ListingKey'];
423 if ( '' !== $replace ) {
424 $title_format = str_replace( '{ListingKey}', $replace, $title_format );
425 }
426 } elseif ( 'ListingId' === $value ) {
427 if ( isset( $property['adr_listingid'] ) ) {
428 $replace = $property['adr_listingid'];
429 }
430 if ( '' !== $replace ) {
431 $title_format = str_replace( '{ListingId}', $replace, $title_format );
432 }
433 } elseif ( 'StateOrProvince' === $value ) {
434 if ( isset( $property['extra_meta']['StateOrProvince'] ) ) {
435 $replace = $property['extra_meta']['StateOrProvince'];
436 }
437 $title_format = str_replace( '{StateOrProvince}', $replace, $title_format );
438 } elseif ( 'PostalCode' === $value ) {
439 if ( isset( $property['meta']['property_zip'] ) ) {
440 if ( is_array( $property['meta']['property_zip'] ) ) {
441 $replace = strval( $property['meta']['property_zip'][0] );
442 } else {
443 $replace = strval( $property['meta']['property_zip'] );
444 }
445 } elseif ( isset( $property['meta']['fave_property_zip'] ) ) {
446 if ( is_array( $property['meta']['fave_property_zip'] ) ) {
447 $replace = strval( $property['meta']['fave_property_zip'][0] );
448 } else {
449 $replace = strval( $property['meta']['fave_property_zip'] );
450 }
451 }
452
453 $title_format = str_replace( '{PostalCode}', $replace, $title_format );
454 } elseif ( 'StreetNumberNumeric' === $value ) {
455 if ( isset( $property_extra_meta_array_lowecase['streetnumbernumeric'] ) ) {
456 $replace = $property_extra_meta_array_lowecase['streetnumbernumeric'];
457 }
458 $title_format = str_replace( '{StreetNumberNumeric}', $replace, $title_format );
459 } elseif ( 'StreetName' === $value ) {
460 if ( isset( $property_extra_meta_array_lowecase['streetname'] ) ) {
461 $replace = $property_extra_meta_array_lowecase['streetname'];
462 }
463 $title_format = str_replace( '{StreetName}', $replace, $title_format );
464 }
465 }
466
467 $post = array(
468 'ID' => $property_id,
469 'post_title' => $title_format,
470 'post_name' => $title_format,
471 );
472
473 wp_update_post( $post );
474
475 unset( $property_extra_meta_array_lowecase );
476 return $title_format;
477 }
478
479
480
481
482
483
484
485
486 /**
487 *
488 *
489 *
490 *
491 * import property -prepare
492 *
493 * @since 1.0.0
494 * @access protected
495 * @var string $plugin_name
496 */
497 public function mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, $tip_import, $mlsimport_item_option_data ) {
498 // check if MLS id is set
499 global $mlsimport;
500
501
502 $mls_import_item_status = $mlsimport_item_option_data['mlsimport_item_standardstatus'];
503 $new_author = $mlsimport_item_option_data['mlsimport_item_property_user'];
504 $new_agent = $mlsimport_item_option_data['mlsimport_item_agent'];
505 $property_status = $mlsimport_item_option_data['mlsimport_item_property_status'];
506
507 if ( is_array( $mls_import_item_status ) ) {
508 $mls_import_item_status = array_map( 'strtolower', $mls_import_item_status );
509 }
510
511
512 if ( ! isset( $property['ListingKey'] ) ) {
513 $log = 'ERROR : No Listing Key ' . PHP_EOL;
514 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
515 return;
516 }
517
518 ob_start();
519
520 $ListingKey = $property['ListingKey'];
521 $listing_post_type = $mlsimport->admin->env_data->get_property_post_type();
522 $property_id = intval( $this->mlsimport_saas_retrive_property_by_id( $ListingKey, $listing_post_type ) );
523 $status = 'Not Found';
524 $property_history = array();
525 if ( isset( $property['StandardStatus'] ) ) {
526 $status = strtolower( $property['StandardStatus'] );
527 } else {
528 $status = strtolower( $property['extra_meta']['MlsStatus'] );
529 }
530
531 if ( 0 === intval( $property_id) ) {
532 $is_insert = 'no';
533 if (
534 'active' === $status || 'active under contract' === $status ||
535 'active with contract' === $status || 'activewithcontract' === $status ||
536 'status' === $status || 'activeundercontract' === $status ||
537 'comingsoon' === $status || 'coming soon' === $status ||
538 'pending' === $status
539 ) {
540 $is_insert = 'yes';
541 if ( 'cron' === $tip_import ) {
542 if ( ! in_array( $status, $mls_import_item_status ) ) {
543 // REJECTED because not selected';
544 $is_insert = 'no';
545 }
546 }
547 }
548 } else {
549 $is_insert = 'no';
550 }
551
552 $log = $this->mlsimport_mem_usage() . '==========' . wp_json_encode( $mls_import_item_status ) . '/' . $new_author . '/' . $new_agent . '/' . $property_status . '/ We have property with $ListingKey=' . $ListingKey . ' id=' . $property_id . ' with status ' . $status . ' is insert? ' . $is_insert . PHP_EOL;
553
554 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
555
556 // content set and check
557 $content = '';
558 $submit_title = $ListingKey;
559 if ( isset( $property['content'] ) ) {
560 $content = $property['content'];
561 }
562
563 // $new_author = get_post_meta($item_id_array['item_id'],'mlsimport_item_property_user',true );
564 // $new_agent = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_agent', true));
565 // $property_status = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_property_status', true));
566
567 $mlsimport_item_option_data = null;
568
569 if ( 'yes' === $is_insert ) {
570 $post = array(
571 'post_title' => $submit_title,
572 'post_content' => $content,
573 'post_status' => $property_status,
574 'post_type' => $listing_post_type,
575 'post_author' => $new_author,
576 );
577
578 $property_id = wp_insert_post( $post );
579
580 if ( is_wp_error( $property_id ) ) {
581 $log = 'ERROR : on inserting ' . PHP_EOL;
582 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
583 } else {
584 update_post_meta( $property_id, 'ListingKey', $ListingKey );
585 $property_history[] = date( 'F j, Y, g:i a' ) . ': We Inserted the property with Default title : ' . $submit_title . ' and received id:' . $property_id;
586 }
587
588 clean_post_cache( $property_id );
589 } elseif ( 0 !== $property_id ) {
590 $property_history = array();
591 $property_history[] = get_post_meta( $property_id, 'mlsimport_property_history', true );
592
593 $delete_statuses = array(
594 'incomplete' => 'incomplete',
595 'hold' => 'hold',
596 'canceled' => 'canceled',
597 'closed' => 'closed',
598 'delete' => 'delete',
599 'expired' => 'expired',
600 'withdrawn' => 'withdrawn',
601 );
602
603 if ( ! in_array( 'pending', $mls_import_item_status ) ) {
604 $delete_statuses['pending'] = 'pending';
605 }
606
607 if ( in_array( $status, $delete_statuses ) ) {
608 $log = 'Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be deleted' . PHP_EOL;
609 $log = '--1---> ' . $log . wp_json_encode( $mls_import_item_status ) . PHP_EOL;
610 if ( in_array( $status, $mls_import_item_status ) ) {
611 $log .= '-2--> canceling the delete ' . PHP_EOL;
612 } else {
613 $log .= '--3--> proceed with the delete ' . PHP_EOL;
614 $this->delete_property( $property_id, $ListingKey );
615 }
616
617 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
618 unset( $log );
619
620 return;
621 } else {
622
623 $post = array(
624 'ID' => $property_id,
625 'post_content' => $content,
626 'post_type' => $listing_post_type,
627 'post_author' => $new_author,
628 );
629
630 $log = ' Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be Edited</br>';
631 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
632
633 $property_id = wp_update_post( $post );
634
635 if ( is_wp_error( $property_id ) ) {
636 $log = 'ERROR : on edit ' . PHP_EOL;
637 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import );
638 } else {
639 $submit_title = get_the_title( $property_id );
640 $property_history[] = gmdate( 'F j, Y, g:i a' ) . ': Property with title: ' . $submit_title . ', id:' . $property_id . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited';
641 }
642
643 clean_post_cache( $property_id );
644 }
645 }
646
647 //
648 // Insert or edit POST ends her - START ADDING DETAILS
649 //
650
651 $encoded_values = array();// may be obsolote
652
653 if ( intval( $property_id ) === 0 ) {
654 mlsimport_saas_single_write_import_custom_logs( 'ERROR property id is 0' . PHP_EOL, $tip_import );
655 return; // no point in going forward if no id
656 }
657
658 // Start working on Taxonomies
659 //
660 $tax_log = array();
661 $tax_log[] = 'Property with ID ' . $property_id . ' NO taxonomies found ! ';
662
663 if ( isset( $property['taxonomies'] ) && is_array( $property['taxonomies'] ) ) {
664 $taxonomies = $property['taxonomies'];
665 $tax_log = array();
666
667 $this->mlsimport_saas_clear_property_for_taxonomy( $property_id, $property['taxonomies'] );
668
669 foreach ( $taxonomies as $taxonomy => $term ) :
670 $this->mlsimport_saas_update_taxonomy_for_property( $taxonomy, $property_id, $term );
671 $property_history[] = 'Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term );
672 $tax_log [] = 'Memory:' . $this->mlsimport_mem_usage() . ' Property with ID ' . $property_id . ' Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term );
673 endforeach;
674 $taxonomies = null;
675 }
676
677 $tax_log = implode( PHP_EOL, $tax_log );
678 mlsimport_saas_single_write_import_custom_logs( $tax_log, $tip_import );
679 $tax_log = null;
680
681 // Pre Meta jobs
682 //
683 $property = $this->mlsimport_saas_prepare_meta_for_property( $property );
684
685 // Start working on Meta
686 //
687
688 $meta_log = array();
689 $meta_log [] = 'Property with ID ' . $property_id . ' NO meta found ! ' . PHP_EOL;
690
691 if ( isset( $property['meta'] ) && is_array( $property['meta'] ) ) {
692 $meta_properties = $property['meta'];
693 foreach ( $meta_properties as $meta_name => $meta_value ) :
694 if ( is_array( $meta_value ) ) {
695 $meta_value = implode( ',', $meta_value );
696 }
697 update_post_meta( $property_id, $meta_name, $meta_value );
698
699 $property_history[] = 'Updated Meta ' . $meta_name . ' with meta_value ' . $meta_value;
700 $meta_log [] = 'Memory:' . $this->mlsimport_mem_usage() . 'Property with ID ' . $property_id . ' Updated Meta ' . $meta_name . ' with value ' . $meta_value;
701 endforeach;
702 }
703 $meta_properties = null;
704 $meta_log = implode( PHP_EOL, $meta_log );
705 mlsimport_saas_single_write_import_custom_logs( $meta_log, $tip_import );
706 $meta_log = null;
707
708 // Start working on EXTRA Meta
709 //
710
711 $extra_meta_log = 'Property with ID ' . $property_id . ' Start Extra meta ! ' . PHP_EOL;
712 $extra_meta_result = $mlsimport->admin->env_data->mlsimport_saas_set_extra_meta( $property_id, $property );
713
714 if ( isset( $extra_meta_result['property_history'] ) ) {
715 $property_history = array_merge( $property_history, (array) $extra_meta_result['property_history'] );
716 }
717 if ( isset( $extra_meta_result['extra_meta_log'] ) ) {
718 $extra_meta_log .= $extra_meta_result['extra_meta_log'];
719 }
720
721 mlsimport_saas_single_write_import_custom_logs( $extra_meta_log, $tip_import );
722
723 $extra_meta_log = null;
724 $extra_meta_result = null;
725
726 // Start working on Property Media
727 //
728
729 $media = $property['Media'];
730 $media_history = $this->mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert );
731 $property_history = array_merge( $property_history, (array) $media_history );
732 $media = null;
733 $media_history = null;
734
735 // Updateing property title and ending
736 //
737
738 $new_title = $this->mlsimport_saas_update_property_title( $property_id, $item_id_array['item_id'], $property );
739 $property_history[] = 'Updated title to ' . $new_title . '</br>';
740
741 // extra fields to be checked
742 $global_extra_fields = array();
743 $mlsimport->admin->env_data->correlation_update_after( $is_insert, $property_id, $global_extra_fields, $new_agent );
744
745 // saving history
746 if ( ! empty( $property_history ) ) {
747 $disable_history = intval( get_option( 'mlsimport-disable-history', 1 ) );
748 if ( 1 === intval( $disable_history ) ) {
749 $property_history[] = '---------------------------------------------------------------</br>';
750 $property_history = implode( '</br>', $property_history );
751 update_post_meta( $property_id, 'mlsimport_property_history', $property_history );
752 }
753 }
754
755 $logs = PHP_EOL . 'Ending on Property ' . $property_id . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $is_insert . ' with new title: ' . $new_title . ' ' . PHP_EOL;
756 mlsimport_saas_single_write_import_custom_logs( $logs, $tip_import );
757
758 $capture = ob_get_contents();
759 ob_end_clean();
760 mlsimport_saas_single_write_import_custom_logs( $capture, $tip_import );
761
762 $post = null;
763 $capture = null;
764 $property_status = null;
765 $new_agent = null;
766 $new_author = null;
767 $property_history = null;
768 $tax_log = null;
769 $meta_log = null;
770 $extra_meta_log = null;
771 $media_history = null;
772 $logs = null;
773 $capture = null;
774 clean_post_cache( $property_id );
775 wp_cache_flush();
776 gc_collect_cycles();
777 }
778
779
780
781
782
783
784
785 public function mlsimport_mem_usage() {
786 $mem_usage = memory_get_usage( true );
787 $mem_usage_show = round( $mem_usage / 1048576, 2 );
788 return $mem_usage_show . 'mb ';
789 }
790
791
792 /**
793 * prepare meta data
794 *
795 * @since 1.0.0
796 * @access protected
797 * @var string $plugin_name
798 */
799 public function mlsimport_saas_prepare_meta_for_property( $property ) {
800
801 if ( isset( $property['extra_meta']['BathroomsTotalDecimal'] ) && floatval( $property['extra_meta']['BathroomsTotalDecimal'] ) > 0 ) {
802 $property['meta']['property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] );
803 $property['meta']['fave_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] );
804 $property['meta']['REAL_HOMES_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] );
805 }
806 return $property;
807 }
808
809
810
811
812
813 /**
814 * attach media to post
815 *
816 * @since 1.0.0
817 * @access protected
818 * @var string $plugin_name
819 */
820 public function mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ) {
821
822
823 $media_history = array();
824 if ( 'no' === $is_insert ) {
825 $media_history[] = ' Media - We have edit - images are not replaced';
826 return $media_history;
827 }
828
829 global $mlsimport;
830 include_once ABSPATH . 'wp-admin/includes/image.php';
831 $has_featured = false;
832 $all_images = array();
833
834 delete_post_meta( $property_id, 'fave_property_images' );
835 delete_post_meta( $property_id, 'REAL_HOMES_property_images' );
836
837 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) );
838
839 // sorting media
840 if ( isset( $media[0]['Order'] ) ) {
841 $order = array_column( $media, 'Order' );
842 array_multisort( $order, SORT_ASC, $media );
843 }
844
845 if ( is_array( $media ) ) {
846 foreach ( $media as $key => $image ) :
847 if ( isset( $image['MediaCategory'] ) && 'Photo' !== $image['MediaCategory'] ) {
848 continue;
849 }
850
851 $file = $image['MediaURL'];
852
853 $media_url = '';
854 if ( isset( $image['MediaURL'] ) ) {
855 $attachment = array(
856 'guid' => $image['MediaURL'],
857 'post_status' => 'inherit',
858 'post_content' => '',
859 'post_parent' => $property_id,
860 );
861
862 if ( isset( $image['MimeType'] ) ) {
863 $attachment['post_mime_type'] = $image['MimeType'];
864 } else {
865 $attachment['post_mime_type'] = 'image/jpg';
866 }
867
868 if ( isset( $image['MediaKey'] ) ) {
869 $attachment['post_title'] = $image['MediaKey'];
870 } else {
871 $attachment['post_title'] = '';
872 }
873
874 $attach_id = wp_insert_attachment( $attachment, $file );
875
876 $media_history[] = ' Media - Added ' . $image['MediaURL'] . ' as attachement ' . $attach_id;
877 // wp_generate_attachment_metadata($attach_id,$image['MediaURL']);
878 $mlsimport->admin->env_data->enviroment_image_save( $property_id, $attach_id );
879
880 update_post_meta( $attach_id, 'is_mlsimport', 1 );
881 if ( ! $has_featured ) {
882 set_post_thumbnail( $property_id, $attach_id );
883 $has_featured = true;
884 }
885 }
886 endforeach;
887 } else {
888 $media_history[] = ' Media data is blank - there are no images';
889 }
890 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) );
891
892 $media_history = implode( '</br>', $media_history );
893 return $media_history;
894 }
895
896
897
898 function wpc_unset_imagesizes( $sizes ) {
899 $sizes = array();
900 }
901
902
903
904
905
906
907
908
909
910
911 /**
912 * return user option
913 *
914 * @since 1.0.0
915 * @access protected
916 * @var string $plugin_name
917 */
918 public function mlsimport_saas_theme_import_select_user( $selected ) {
919 $blog_list = '';
920 $blogusers = get_users( 'blog_id=1&orderby=nicename' );
921 foreach ( $blogusers as $user ) {
922 $the_id = $user->ID;
923 $blog_list .= '<option value="' . $the_id . '" ';
924 if ( $the_id === intval($selected) ) {
925 $blog_list .= ' selected="selected" ';
926 }
927 $blog_list .= '>' . $user->user_login . '</option>';
928 }
929 return $blog_list;
930 }
931
932
933
934
935
936
937
938
939 /**
940 * return agent option
941 *
942 * @since 1.0.0
943 * @access protected
944 * @var string $plugin_name
945 */
946 public function mlsimport_saas_theme_import_select_agent( $selected ) {
947 global $mlsimport;
948 $args2 = array(
949 'post_type' => $mlsimport->admin->env_data->get_agent_post_type(),
950 'post_status' => 'publish',
951 'posts_per_page' => 150,
952 );
953
954 if ( method_exists( $mlsimport, 'get_agent_post_type' ) ) {
955 $args2['post_type'] = $mlsimport->admin->env_data->get_agent_post_type();
956 }
957
958 $agent_selection2 = new WP_Query( $args2 );
959 $agent_list_sec = '<option value=""><option>';
960
961 while ( $agent_selection2->have_posts() ) {
962 $agent_selection2->the_post();
963 $the_id = get_the_ID();
964
965 $agent_list_sec .= '<option value="' . $the_id . '" ';
966 if ( intval($selected) === $the_id ) {
967 $agent_list_sec .= ' selected="selected" ';
968 }
969 $agent_list_sec .= '>' . get_the_title() . '</option>';
970 }
971 wp_reset_postdata();
972
973 return $agent_list_sec;
974 }
975
976
977 /**
978 * delete property
979 *
980 * @since 1.0.0
981 * @access protected
982 * @var string $plugin_name
983 */
984 public function delete_property( $delete_id, $ListingKey ) {
985 if ( intval( $delete_id ) > 0 ) {
986 $arguments = array(
987 'numberposts' => -1,
988 'post_type' => 'attachment',
989 'post_parent' => $delete_id,
990 'post_status' => null,
991 'orderby' => 'menu_order',
992 'order' => 'ASC',
993 );
994 $post_attachments = get_posts( $arguments );
995
996 foreach ( $post_attachments as $attachment ) {
997 wp_delete_post( $attachment->ID );
998 }
999
1000 wp_delete_post( $delete_id );
1001 $log_entry = ' Property with id ' . $delete_id . ' and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL;
1002 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' );
1003 }
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 /**
1017 * return_array with title items
1018 *
1019 * @since 1.0.0
1020 * @access protected
1021 * @var string $plugin_name
1022 */
1023 public function str_between_all( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) {
1024 $strings = array();
1025 $length = strlen( $string );
1026
1027 while ( $offset < $length ) {
1028 $found = $this->str_between( $string, $start, $end, $includeDelimiters, $offset );
1029 if ( null === $found ) {
1030 break;
1031 }
1032
1033 $strings[] = $found;
1034 $offset += strlen( $includeDelimiters ? $found : $start . $found . $end ); // move offset to the end of the newfound string
1035 }
1036
1037 return $strings;
1038 }
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 /**
1049 * str_between
1050 *
1051 * @since 1.0.0
1052 * @access protected
1053 * @var string $plugin_name
1054 */
1055 public function str_between( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) {
1056 if ('' === $string || '' === $start || '' === $end ) {
1057 return null;
1058 }
1059
1060 $startLength = strlen( $start );
1061 $endLength = strlen( $end );
1062
1063 $startPos = strpos( $string, $start, $offset );
1064 if ( false === $startPos ) {
1065 return null;
1066 }
1067
1068 $endPos = strpos( $string, $end, $startPos + $startLength );
1069 if ( false === $endPos) {
1070 return null;
1071 }
1072
1073 $length = $endPos - $startPos + ( $includeDelimiters ? $endLength : -$startLength );
1074 if ( ! $length ) {
1075 return '';
1076 }
1077
1078 $offset = $startPos + ( $includeDelimiters ? 0 : $startLength );
1079
1080 $result = substr( $string, $offset, $length );
1081
1082 return ( false !== $result ? $result : null );
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092 /**
1093 * delete property via sql
1094 *
1095 * @since 1.0.0
1096 * @access protected
1097 * @var string $plugin_name
1098 */
1099 public function mlsimport_saas_delete_property_via_mysql( $delete_id, $ListingKey ) {
1100
1101 $post_type = get_post_type( $delete_id );
1102
1103 if ( 'estate_property' === $post_type || 'property' === $post_type ) {
1104 $term_obj_list = get_the_terms( $delete_id, 'property_status' );
1105 $delete_id_status = join( ', ', wp_list_pluck( $term_obj_list, 'name' ) );
1106
1107 $ListingKey = get_post_meta( $delete_id, 'ListingKey', true );
1108 if ( '' === $ListingKey ) { // manual added listing
1109 $log_entry = 'User added listing with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL;
1110 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' );
1111 return;
1112 }
1113
1114 global $wpdb;
1115 $wpdb->query(
1116 $wpdb->prepare(
1117 "
1118 DELETE FROM $wpdb->postmeta
1119 WHERE `post_id` = %d",
1120 $delete_id
1121 )
1122 );
1123
1124 $wpdb->query(
1125 $wpdb->prepare(
1126 "
1127 DELETE FROM $wpdb->posts
1128 WHERE `post_parent` = %d",
1129 $delete_id
1130 )
1131 );
1132
1133 $wpdb->query(
1134 $wpdb->prepare(
1135 "
1136 DELETE FROM $wpdb->posts
1137 WHERE ID = %d",
1138 $delete_id
1139 )
1140 );
1141
1142 $log_entry = 'MYSQL DELETE -> Property with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL;
1143 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' );
1144 }
1145 }
1146 }
1147