| 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 |
if(isset( $ready_to_parse_array['data'])){ |
| 181 |
foreach ( $ready_to_parse_array['data'] as $key => $property ) { |
| 182 |
++$counter_prop; |
| 183 |
|
| 184 |
$logs = $this->mlsimport_mem_usage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; |
| 185 |
mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); |
| 186 |
|
| 187 |
|
| 188 |
wp_cache_delete( 'mlsimport_force_stop_' . $item_id_array['item_id'], 'options' ); |
| 189 |
|
| 190 |
$status = get_option( 'mlsimport_force_stop_' . $item_id_array['item_id'] ); |
| 191 |
$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; |
| 192 |
mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); |
| 193 |
|
| 194 |
if ( 'no' === $status ) { |
| 195 |
$logs = 'Will proceed to import - Memory Used ' . $this->mlsimport_mem_usage() . PHP_EOL; |
| 196 |
mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); |
| 197 |
$this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'normal', $mlsimport_item_option_data ); |
| 198 |
} else { |
| 199 |
update_post_meta( $item_id_array['item_id'], 'mlsimport_spawn_status', 'completed' ); |
| 200 |
} |
| 201 |
unset( $logs ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
unset( $ready_to_parse_array ); |
| 206 |
unset( $logs ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* |
| 212 |
* |
| 213 |
* Parse Result Array in CROn |
| 214 |
*/ |
| 215 |
public function mlsimport_saas_cron_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key ) { |
| 216 |
|
| 217 |
$mlsimport_item_option_data = array(); |
| 218 |
$mlsimport_item_option_data['mlsimport_item_standardstatus'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_standardstatus', true ); |
| 219 |
$mlsimport_item_option_data['mlsimport_item_property_user'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_user', true ); |
| 220 |
$mlsimport_item_option_data['mlsimport_item_agent'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_agent', true ); |
| 221 |
$mlsimport_item_option_data['mlsimport_item_property_status'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_status', true ); |
| 222 |
|
| 223 |
foreach ( $ready_to_parse_array['data'] as $key => $property ) { |
| 224 |
$logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; |
| 225 |
mlsimport_saas_single_write_import_custom_logs( $logs, 'cron' ); |
| 226 |
$this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'cron', $mlsimport_item_option_data ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* |
| 238 |
* |
| 239 |
* check if property already imported |
| 240 |
*/ |
| 241 |
public function mlsimport_saas_retrive_property_by_id( $key, $post_type = 'estate_property' ) { |
| 242 |
|
| 243 |
$args = array( |
| 244 |
'post_type' => $post_type, |
| 245 |
'post_status' => 'any', |
| 246 |
|
| 247 |
'meta_query' => array( |
| 248 |
array( |
| 249 |
'key' => 'ListingKey', |
| 250 |
'value' => $key, |
| 251 |
'compare' => '=', |
| 252 |
), |
| 253 |
), |
| 254 |
'fields' => 'ids', |
| 255 |
); |
| 256 |
|
| 257 |
$prop_selection = new WP_Query( $args ); |
| 258 |
if ( $prop_selection->have_posts() ) { |
| 259 |
while ( $prop_selection->have_posts() ) { |
| 260 |
$prop_selection->the_post(); |
| 261 |
$the_id = get_the_ID(); |
| 262 |
wp_reset_postdata(); |
| 263 |
return $the_id; |
| 264 |
} |
| 265 |
} else { |
| 266 |
wp_reset_postdata(); |
| 267 |
return 0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
/** |
| 276 |
* clear taxonomy |
| 277 |
* |
| 278 |
* @since 1.0.0 |
| 279 |
* @access protected |
| 280 |
* @var string $plugin_name |
| 281 |
*/ |
| 282 |
public function mlsimport_saas_clear_property_for_taxonomy( $property_id, $taxonomies ) { |
| 283 |
|
| 284 |
if ( is_array( $taxonomies ) ) : |
| 285 |
foreach ( $taxonomies as $taxonomy => $term ) : |
| 286 |
if (is_wp_error($taxonomy)) { |
| 287 |
error_log('Error with taxonomy: ' . $taxonomy->get_error_message()); |
| 288 |
continue; // Skip this iteration |
| 289 |
} |
| 290 |
|
| 291 |
if ( taxonomy_exists($taxonomy) ) { |
| 292 |
wp_delete_object_term_relationships($property_id, $taxonomy); |
| 293 |
} else { |
| 294 |
error_log("Taxonomy does not exist: {$taxonomy}"); |
| 295 |
} |
| 296 |
endforeach; |
| 297 |
endif; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* return non encoded encoded values |
| 309 |
* |
| 310 |
* @since 1.0.0 |
| 311 |
* @access protected |
| 312 |
* @var string $plugin_name |
| 313 |
*/ |
| 314 |
public function mlsimport_saas_return_non_encoded_value( $item, $encoded_values ) { |
| 315 |
|
| 316 |
if ( is_array( $item ) ) { |
| 317 |
if ( ! empty( $encoded_values ) ) { |
| 318 |
foreach ( $item as $key => $value ) { |
| 319 |
if ( isset( $encoded_values [ $value ] ) ) { |
| 320 |
$item[ $key ] = $encoded_values [ $value ]; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
return $item; |
| 325 |
} elseif ( ! empty( $encoded_values ) && isset( $encoded_values [ $item ] ) ) { |
| 326 |
return $encoded_values [ $item ]; |
| 327 |
} else { |
| 328 |
return $item; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* set taxonomy |
| 334 |
* |
| 335 |
* @since 1.0.0 |
| 336 |
* @access protected |
| 337 |
* @var string $plugin_name |
| 338 |
*/ |
| 339 |
|
| 340 |
function mlsimport_saas_update_taxonomy_for_property_new($taxonomy, $property_id, $field_values) { |
| 341 |
global $wpdb; |
| 342 |
|
| 343 |
// Remove filters temporarily to avoid caching issues |
| 344 |
remove_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts')); |
| 345 |
wp_cache_delete('terms', 'cache'); |
| 346 |
|
| 347 |
if (!is_array($field_values)) { |
| 348 |
if (strpos($field_values, ',') !== false) { |
| 349 |
$field_values = explode(',', $field_values); |
| 350 |
} else { |
| 351 |
$field_values = array($field_values); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
// Use transactions for better performance |
| 356 |
$wpdb->query('START TRANSACTION'); |
| 357 |
|
| 358 |
// Process in smaller chunks |
| 359 |
foreach (array_chunk($field_values, 5) as $chunk) { |
| 360 |
foreach ($chunk as $value) { |
| 361 |
if (!empty($value)) { |
| 362 |
$term = get_term_by('name', $value, $taxonomy); |
| 363 |
|
| 364 |
if (is_wp_error($term) || empty($term)) { |
| 365 |
// Term doesn't exist, insert it |
| 366 |
$term = wp_insert_term($value, $taxonomy); |
| 367 |
|
| 368 |
if (!is_wp_error($term)) { |
| 369 |
$term_id = $term['term_id']; |
| 370 |
$term_taxonomy_id = $term['term_taxonomy_id']; |
| 371 |
} |
| 372 |
} else { |
| 373 |
// Term exists |
| 374 |
$term_id = $term->term_id; |
| 375 |
$term_taxonomy_id = $term->term_taxonomy_id; |
| 376 |
} |
| 377 |
|
| 378 |
if (!empty($term_id) && !empty($term_taxonomy_id)) { |
| 379 |
// Insert term relationship |
| 380 |
$wpdb->insert( |
| 381 |
$wpdb->term_relationships, |
| 382 |
array( |
| 383 |
'object_id' => $property_id, |
| 384 |
'term_taxonomy_id' => $term_taxonomy_id, |
| 385 |
'term_order' => 0 |
| 386 |
), |
| 387 |
array( |
| 388 |
'%d', |
| 389 |
'%d', |
| 390 |
'%d' |
| 391 |
) |
| 392 |
); |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
// Commit transaction |
| 399 |
$wpdb->query('COMMIT'); |
| 400 |
|
| 401 |
// Clear term cache selectively |
| 402 |
wp_cache_delete("{$taxonomy}_terms", 'terms'); |
| 403 |
wp_cache_delete("{$taxonomy}_children", 'terms'); |
| 404 |
|
| 405 |
// Restore the term metadata filter |
| 406 |
add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); |
| 407 |
} |
| 408 |
|
| 409 |
function mlsimport_saas_update_taxonomy_for_property($taxonomy, $property_id, $field_values) { |
| 410 |
global $wpdb; |
| 411 |
|
| 412 |
// Convert comma-separated values to array if necessary |
| 413 |
if (!is_array($field_values)) { |
| 414 |
$field_values = strpos($field_values, ',') !== false ? explode(',', $field_values) : array($field_values); |
| 415 |
} |
| 416 |
|
| 417 |
// Trim values and remove empty ones |
| 418 |
$field_values = array_filter(array_map('trim', $field_values)); |
| 419 |
|
| 420 |
// Start a database transaction |
| 421 |
$wpdb->query('START TRANSACTION'); |
| 422 |
$tax_log = array(); |
| 423 |
|
| 424 |
foreach (array_chunk($field_values, 5) as $chunk) { |
| 425 |
foreach ($chunk as $value) { |
| 426 |
if (!empty($value)) { |
| 427 |
// Check if the term already exists |
| 428 |
|
| 429 |
$term = $wpdb->get_row($wpdb->prepare( |
| 430 |
"SELECT t.*, tt.* FROM $wpdb->terms t |
| 431 |
INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id |
| 432 |
WHERE t.name = %s AND tt.taxonomy = %s", |
| 433 |
$value, $taxonomy |
| 434 |
)); |
| 435 |
|
| 436 |
|
| 437 |
$tax_log[] = json_encode($term); |
| 438 |
if (is_null($term)) { |
| 439 |
// Insert the term if it doesn't exist |
| 440 |
|
| 441 |
$wpdb->insert($wpdb->terms, array( |
| 442 |
'name' => $value, |
| 443 |
'slug' => sanitize_title($value), |
| 444 |
'term_group' => 0 |
| 445 |
)); |
| 446 |
|
| 447 |
$term_id = $wpdb->insert_id; |
| 448 |
|
| 449 |
if ($term_id) { |
| 450 |
// Insert term taxonomy |
| 451 |
$wpdb->insert($wpdb->term_taxonomy, array( |
| 452 |
'term_id' => $term_id, |
| 453 |
'taxonomy' => $taxonomy, |
| 454 |
'description' => '', |
| 455 |
'parent' => 0, |
| 456 |
'count' => 0 |
| 457 |
)); |
| 458 |
|
| 459 |
$term_taxonomy_id = $wpdb->insert_id; |
| 460 |
} else { |
| 461 |
$tax_log[] = 'Error inserting term'; |
| 462 |
continue; |
| 463 |
} |
| 464 |
} else { |
| 465 |
// Term exists, get term_id and term_taxonomy_id |
| 466 |
$term_id = $term->term_id; |
| 467 |
$term_taxonomy_id = $wpdb->get_var($wpdb->prepare( |
| 468 |
"SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s", |
| 469 |
$term_id, |
| 470 |
$taxonomy |
| 471 |
)); |
| 472 |
} |
| 473 |
|
| 474 |
if (!empty($term_taxonomy_id)) { |
| 475 |
// Insert term relationship |
| 476 |
$wpdb->replace($wpdb->term_relationships, array( |
| 477 |
'object_id' => $property_id, |
| 478 |
'term_taxonomy_id' => $term_taxonomy_id |
| 479 |
)); |
| 480 |
// Increment the term count |
| 481 |
$wpdb->query($wpdb->prepare( |
| 482 |
"UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = %d", |
| 483 |
$term_taxonomy_id |
| 484 |
)); |
| 485 |
} else { |
| 486 |
$tax_log[] = 'Error: term_taxonomy_id is null'; |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
// Flush the cache to free up memory |
| 491 |
wp_cache_flush(); |
| 492 |
// Run garbage collection |
| 493 |
gc_collect_cycles(); |
| 494 |
} |
| 495 |
// Commit the transaction |
| 496 |
$wpdb->query('COMMIT'); |
| 497 |
|
| 498 |
// Clear term cache selectively |
| 499 |
wp_cache_delete("{$taxonomy}_terms", 'terms'); |
| 500 |
wp_cache_delete("{$taxonomy}_children", 'terms'); |
| 501 |
|
| 502 |
// Restore the term metadata filter |
| 503 |
add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); |
| 504 |
|
| 505 |
// Log memory usage |
| 506 |
/*$tip_import='normal'; |
| 507 |
if (!empty($tax_log)) { |
| 508 |
$tax_log_str = implode(PHP_EOL, $tax_log); |
| 509 |
mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); |
| 510 |
unset($tax_log_str); |
| 511 |
}*/ |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
public function mlsimport_saas_update_taxonomy_for_property2( $taxonomy, $property_id, $field_values ) { |
| 517 |
|
| 518 |
if ( ! is_array( $field_values ) ) { |
| 519 |
if ( strpos( $field_values, ',' ) !== false ) { |
| 520 |
$field_values = explode( ',', $field_values ); |
| 521 |
} else { |
| 522 |
$field_values = array( $field_values ); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
// Remove empty values and decode values in one pass |
| 527 |
$processed_field_values = array(); |
| 528 |
foreach ( $field_values as $value ) { |
| 529 |
if ( ! empty( $value ) ) { |
| 530 |
$processed_field_values[] = $value; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
// Bulk update terms if array is not empty |
| 535 |
if ( ! empty( $processed_field_values ) ) { |
| 536 |
wp_set_object_terms( $property_id, $processed_field_values, $taxonomy, true ); |
| 537 |
clean_term_cache( $property_id, $taxonomy ); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
/** |
| 545 |
* Set Property Title |
| 546 |
* |
| 547 |
* @since 1.0.0 |
| 548 |
* @access protected |
| 549 |
* @var string $plugin_name |
| 550 |
*/ |
| 551 |
public function mlsimport_saas_update_property_title( $property_id, $mls_import_post_id, $property ) { |
| 552 |
|
| 553 |
global $mlsimport; |
| 554 |
|
| 555 |
$title_format = esc_html( get_post_meta( $mls_import_post_id, 'mlsimport_item_title_format', true ) ); |
| 556 |
|
| 557 |
if ( '' === $title_format ) { |
| 558 |
$options = get_option( 'mlsimport_admin_mls_sync' ); |
| 559 |
$title_format = $options['title_format']; |
| 560 |
} |
| 561 |
|
| 562 |
$start = '{'; |
| 563 |
$end = '}'; |
| 564 |
$title_array = $this->str_between_all( $title_format, $start, $end ); |
| 565 |
|
| 566 |
$property_extra_meta_array_lowecase = array_change_key_case( $property['extra_meta'], CASE_LOWER ); |
| 567 |
|
| 568 |
foreach ( $title_array as $key => $value ) { |
| 569 |
$replace = ''; |
| 570 |
if ( 'Address' === $value ) { |
| 571 |
if ( isset( $property['adr_title'] ) ) { |
| 572 |
$replace = $property['adr_title']; |
| 573 |
} |
| 574 |
$title_format = str_replace( '{Address}', $replace, $title_format ); |
| 575 |
} elseif ( 'City' === $value ) { |
| 576 |
if ( isset( $property['adr_city'] ) ) { |
| 577 |
$replace = $property['adr_city']; |
| 578 |
} |
| 579 |
$title_format = str_replace( '{City}', $replace, $title_format ); |
| 580 |
} elseif ( 'CountyOrParish' === $value ) { |
| 581 |
if ( isset( $property['adr_county'] ) ) { |
| 582 |
$replace = $property['adr_county']; |
| 583 |
} |
| 584 |
$title_format = str_replace( '{CountyOrParish}', $replace, $title_format ); |
| 585 |
} elseif ( 'PropertyType' === $value ) { |
| 586 |
if ( isset( $property['adr_type'] ) ) { |
| 587 |
$replace = $property['adr_type']; |
| 588 |
} |
| 589 |
$title_format = str_replace( '{PropertyType}', $replace, $title_format ); |
| 590 |
} elseif ( 'Bedrooms' === $value ) { |
| 591 |
if ( isset( $property['adr_bedrooms'] ) ) { |
| 592 |
$replace = $property['adr_bedrooms']; |
| 593 |
} |
| 594 |
$title_format = str_replace( '{Bedrooms}', $replace, $title_format ); |
| 595 |
} elseif ( 'Bathrooms' === $value ) { |
| 596 |
if ( isset( $property['adr_bathrooms'] ) ) { |
| 597 |
$replace = $property['adr_bathrooms']; |
| 598 |
} |
| 599 |
$title_format = str_replace( '{Bathrooms}', $replace, $title_format ); |
| 600 |
} elseif ( 'ListingKey' === $value ) { |
| 601 |
$replace = $property['ListingKey']; |
| 602 |
if ( '' !== $replace ) { |
| 603 |
$title_format = str_replace( '{ListingKey}', $replace, $title_format ); |
| 604 |
} |
| 605 |
} elseif ( 'ListingId' === $value ) { |
| 606 |
if ( isset( $property['adr_listingid'] ) ) { |
| 607 |
$replace = $property['adr_listingid']; |
| 608 |
} |
| 609 |
if ( '' !== $replace ) { |
| 610 |
$title_format = str_replace( '{ListingId}', $replace, $title_format ); |
| 611 |
} |
| 612 |
} elseif ( 'StateOrProvince' === $value ) { |
| 613 |
if ( isset( $property['extra_meta']['StateOrProvince'] ) ) { |
| 614 |
$replace = $property['extra_meta']['StateOrProvince']; |
| 615 |
} |
| 616 |
$title_format = str_replace( '{StateOrProvince}', $replace, $title_format ); |
| 617 |
} elseif ( 'PostalCode' === $value ) { |
| 618 |
if ( isset( $property['meta']['property_zip'] ) ) { |
| 619 |
if ( is_array( $property['meta']['property_zip'] ) ) { |
| 620 |
$replace = strval( $property['meta']['property_zip'][0] ); |
| 621 |
} else { |
| 622 |
$replace = strval( $property['meta']['property_zip'] ); |
| 623 |
} |
| 624 |
} elseif ( isset( $property['meta']['fave_property_zip'] ) ) { |
| 625 |
if ( is_array( $property['meta']['fave_property_zip'] ) ) { |
| 626 |
$replace = strval( $property['meta']['fave_property_zip'][0] ); |
| 627 |
} else { |
| 628 |
$replace = strval( $property['meta']['fave_property_zip'] ); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
$title_format = str_replace( '{PostalCode}', $replace, $title_format ); |
| 633 |
} elseif ( 'StreetNumberNumeric' === $value ) { |
| 634 |
if ( isset( $property_extra_meta_array_lowecase['streetnumbernumeric'] ) ) { |
| 635 |
$replace = $property_extra_meta_array_lowecase['streetnumbernumeric']; |
| 636 |
} |
| 637 |
$title_format = str_replace( '{StreetNumberNumeric}', $replace, $title_format ); |
| 638 |
} elseif ( 'StreetName' === $value ) { |
| 639 |
if ( isset( $property_extra_meta_array_lowecase['streetname'] ) ) { |
| 640 |
$replace = $property_extra_meta_array_lowecase['streetname']; |
| 641 |
} |
| 642 |
$title_format = str_replace( '{StreetName}', $replace, $title_format ); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
$post = array( |
| 647 |
'ID' => $property_id, |
| 648 |
'post_title' => $title_format, |
| 649 |
'post_name' => $title_format, |
| 650 |
); |
| 651 |
|
| 652 |
wp_update_post( $post ); |
| 653 |
|
| 654 |
unset( $property_extra_meta_array_lowecase ); |
| 655 |
return $title_format; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
/** |
| 666 |
* |
| 667 |
* |
| 668 |
* |
| 669 |
* |
| 670 |
* import property -prepare |
| 671 |
* |
| 672 |
* @since 1.0.0 |
| 673 |
* @access protected |
| 674 |
* @var string $plugin_name |
| 675 |
*/ |
| 676 |
public function mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, $tip_import, $mlsimport_item_option_data ) { |
| 677 |
// check if MLS id is set |
| 678 |
set_time_limit(0); |
| 679 |
global $mlsimport; |
| 680 |
|
| 681 |
|
| 682 |
$mls_import_item_status = $mlsimport_item_option_data['mlsimport_item_standardstatus']; |
| 683 |
$new_author = $mlsimport_item_option_data['mlsimport_item_property_user']; |
| 684 |
$new_agent = $mlsimport_item_option_data['mlsimport_item_agent']; |
| 685 |
$property_status = $mlsimport_item_option_data['mlsimport_item_property_status']; |
| 686 |
|
| 687 |
if ( is_array( $mls_import_item_status ) ) { |
| 688 |
$mls_import_item_status = array_map( 'strtolower', $mls_import_item_status ); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
if ( ! isset( $property['ListingKey'] ) ) { |
| 693 |
$log = 'ERROR : No Listing Key ' . PHP_EOL; |
| 694 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 695 |
return; |
| 696 |
} |
| 697 |
|
| 698 |
ob_start(); |
| 699 |
|
| 700 |
$ListingKey = $property['ListingKey']; |
| 701 |
$listing_post_type = $mlsimport->admin->env_data->get_property_post_type(); |
| 702 |
$property_id = intval( $this->mlsimport_saas_retrive_property_by_id( $ListingKey, $listing_post_type ) ); |
| 703 |
$status = 'Not Found'; |
| 704 |
$property_history = array(); |
| 705 |
if ( isset( $property['StandardStatus'] ) ) { |
| 706 |
$status = strtolower( $property['StandardStatus'] ); |
| 707 |
} else { |
| 708 |
$status = strtolower( $property['extra_meta']['MlsStatus'] ); |
| 709 |
} |
| 710 |
|
| 711 |
if ( 0 === intval( $property_id) ) { |
| 712 |
$is_insert = 'no'; |
| 713 |
if ( |
| 714 |
'active' === $status || 'active under contract' === $status || |
| 715 |
'active with contract' === $status || 'activewithcontract' === $status || |
| 716 |
'status' === $status || 'activeundercontract' === $status || |
| 717 |
'comingsoon' === $status || 'coming soon' === $status || |
| 718 |
'pending' === $status |
| 719 |
) { |
| 720 |
$is_insert = 'yes'; |
| 721 |
if ( 'cron' === $tip_import ) { |
| 722 |
if ( ! in_array( $status, $mls_import_item_status ) ) { |
| 723 |
// REJECTED because not selected'; |
| 724 |
$is_insert = 'no'; |
| 725 |
} |
| 726 |
} |
| 727 |
} |
| 728 |
} else { |
| 729 |
$is_insert = 'no'; |
| 730 |
} |
| 731 |
|
| 732 |
$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; |
| 733 |
|
| 734 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 735 |
|
| 736 |
// content set and check |
| 737 |
$content = ''; |
| 738 |
$submit_title = $ListingKey; |
| 739 |
if ( isset( $property['content'] ) ) { |
| 740 |
$content = $property['content']; |
| 741 |
} |
| 742 |
|
| 743 |
// $new_author = get_post_meta($item_id_array['item_id'],'mlsimport_item_property_user',true ); |
| 744 |
// $new_agent = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_agent', true)); |
| 745 |
// $property_status = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_property_status', true)); |
| 746 |
|
| 747 |
$mlsimport_item_option_data = null; |
| 748 |
|
| 749 |
if ( 'yes' === $is_insert ) { |
| 750 |
$post = array( |
| 751 |
'post_title' => $submit_title, |
| 752 |
'post_content' => $content, |
| 753 |
'post_status' => $property_status, |
| 754 |
'post_type' => $listing_post_type, |
| 755 |
'post_author' => $new_author, |
| 756 |
); |
| 757 |
|
| 758 |
$property_id = wp_insert_post( $post ); |
| 759 |
|
| 760 |
if ( is_wp_error( $property_id ) ) { |
| 761 |
$log = 'ERROR : on inserting ' . PHP_EOL; |
| 762 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 763 |
} else { |
| 764 |
update_post_meta( $property_id, 'ListingKey', $ListingKey ); |
| 765 |
$property_history[] = date( 'F j, Y, g:i a' ) . ': We Inserted the property with Default title : ' . $submit_title . ' and received id:' . $property_id; |
| 766 |
} |
| 767 |
|
| 768 |
clean_post_cache( $property_id ); |
| 769 |
} elseif ( 0 !== $property_id ) { |
| 770 |
$property_history = array(); |
| 771 |
$property_history[] = get_post_meta( $property_id, 'mlsimport_property_history', true ); |
| 772 |
|
| 773 |
$delete_statuses = array( |
| 774 |
'incomplete' => 'incomplete', |
| 775 |
'hold' => 'hold', |
| 776 |
'canceled' => 'canceled', |
| 777 |
'closed' => 'closed', |
| 778 |
'delete' => 'delete', |
| 779 |
'expired' => 'expired', |
| 780 |
'withdrawn' => 'withdrawn', |
| 781 |
); |
| 782 |
|
| 783 |
if ( ! in_array( 'pending', $mls_import_item_status ) ) { |
| 784 |
$delete_statuses['pending'] = 'pending'; |
| 785 |
} |
| 786 |
|
| 787 |
if ( in_array( $status, $delete_statuses ) ) { |
| 788 |
$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; |
| 789 |
$log = '--1---> ' . $log . wp_json_encode( $mls_import_item_status ) . PHP_EOL; |
| 790 |
if ( in_array( $status, $mls_import_item_status ) ) { |
| 791 |
$log .= '-2--> canceling the delete ' . PHP_EOL; |
| 792 |
} else { |
| 793 |
$log .= '--3--> proceed with the delete ' . PHP_EOL; |
| 794 |
$this->delete_property( $property_id, $ListingKey ); |
| 795 |
} |
| 796 |
|
| 797 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 798 |
unset( $log ); |
| 799 |
|
| 800 |
return; |
| 801 |
} else { |
| 802 |
|
| 803 |
$post = array( |
| 804 |
'ID' => $property_id, |
| 805 |
'post_content' => $content, |
| 806 |
'post_type' => $listing_post_type, |
| 807 |
'post_author' => $new_author, |
| 808 |
); |
| 809 |
|
| 810 |
$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>'; |
| 811 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 812 |
|
| 813 |
$property_id = wp_update_post( $post ); |
| 814 |
|
| 815 |
if ( is_wp_error( $property_id ) ) { |
| 816 |
$log = 'ERROR : on edit ' . PHP_EOL; |
| 817 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 818 |
} else { |
| 819 |
$submit_title = get_the_title( $property_id ); |
| 820 |
$property_history[] = gmdate( 'F j, Y, g:i a' ) . ': Property with title: ' . $submit_title . ', id:' . $property_id . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited'; |
| 821 |
} |
| 822 |
|
| 823 |
clean_post_cache( $property_id ); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
// |
| 828 |
// Insert or edit POST ends her - START ADDING DETAILS |
| 829 |
// |
| 830 |
|
| 831 |
$encoded_values = array();// may be obsolote |
| 832 |
|
| 833 |
if ( intval( $property_id ) === 0 ) { |
| 834 |
mlsimport_saas_single_write_import_custom_logs( 'ERROR property id is 0' . PHP_EOL, $tip_import ); |
| 835 |
return; // no point in going forward if no id |
| 836 |
} |
| 837 |
|
| 838 |
// Start working on Taxonomies |
| 839 |
// |
| 840 |
$log = PHP_EOL.$this->mlsimport_mem_usage() . '====before tax======'. PHP_EOL; |
| 841 |
mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); |
| 842 |
|
| 843 |
$tax_log = array_fill(0, 1, 'Property with ID ' . $property_id . ' NO taxonomies found!'); |
| 844 |
|
| 845 |
if ( isset( $property['taxonomies'] ) && is_array( $property['taxonomies'] ) ) { |
| 846 |
|
| 847 |
remove_filter('get_term_metadata', 'lazyload_term_meta', 10); |
| 848 |
wp_cache_delete('get_ancestors', 'taxonomy'); |
| 849 |
|
| 850 |
$this->mlsimport_saas_clear_property_for_taxonomy( $property_id, $property['taxonomies'] ); |
| 851 |
|
| 852 |
|
| 853 |
foreach ( $property['taxonomies'] as $taxonomy => $term ) : |
| 854 |
wp_cache_delete("{$taxonomy}_term_counts", 'counts'); |
| 855 |
$this->mlsimport_saas_update_taxonomy_for_property( $taxonomy, $property_id, $term ); |
| 856 |
$property_history[] = 'Updatedx Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); |
| 857 |
$tax_log [] = 'Memory:' . $this->mlsimport_mem_usage() . ' Property with ID ' . $property_id . ' Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); |
| 858 |
|
| 859 |
endforeach; |
| 860 |
|
| 861 |
|
| 862 |
unset($property['taxonomies'] ); |
| 863 |
gc_collect_cycles(); |
| 864 |
add_filter('get_term_metadata', 'lazyload_term_meta', 10, 2); |
| 865 |
delete_option('category_children'); |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
// Only log if there are updates |
| 871 |
if (!empty($tax_log)) { |
| 872 |
$tax_log_str = implode(PHP_EOL, $tax_log); |
| 873 |
mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); |
| 874 |
unset($tax_log_str); |
| 875 |
} |
| 876 |
wp_cache_flush(); |
| 877 |
|
| 878 |
// Pre Meta jobs |
| 879 |
// |
| 880 |
$property = $this->mlsimport_saas_prepare_meta_for_property( $property ); |
| 881 |
|
| 882 |
// Start working on Meta |
| 883 |
// |
| 884 |
|
| 885 |
$meta_log = array(); |
| 886 |
$meta_log [] = 'Property with ID ' . $property_id . ' NO meta found ! ' . PHP_EOL; |
| 887 |
|
| 888 |
if ( isset( $property['meta'] ) && is_array( $property['meta'] ) ) { |
| 889 |
$meta_properties = $property['meta']; |
| 890 |
foreach ( $meta_properties as $meta_name => $meta_value ) : |
| 891 |
if ( is_array( $meta_value ) ) { |
| 892 |
$meta_value = implode( ',', $meta_value ); |
| 893 |
} |
| 894 |
update_post_meta( $property_id, $meta_name, $meta_value ); |
| 895 |
|
| 896 |
$property_history[] = 'Updated Meta ' . $meta_name . ' with meta_value ' . $meta_value; |
| 897 |
$meta_log [] = 'Memory:' . $this->mlsimport_mem_usage() . 'Property with ID ' . $property_id . ' Updated Meta ' . $meta_name . ' with value ' . $meta_value; |
| 898 |
endforeach; |
| 899 |
} |
| 900 |
$meta_properties = null; |
| 901 |
$meta_log = implode( PHP_EOL, $meta_log ); |
| 902 |
mlsimport_saas_single_write_import_custom_logs( $meta_log, $tip_import ); |
| 903 |
$meta_log = null; |
| 904 |
|
| 905 |
// Start working on EXTRA Meta |
| 906 |
// |
| 907 |
|
| 908 |
$extra_meta_log = 'Property with ID ' . $property_id . ' Start Extra meta ! ' . PHP_EOL; |
| 909 |
$extra_meta_result = $mlsimport->admin->env_data->mlsimport_saas_set_extra_meta( $property_id, $property ); |
| 910 |
|
| 911 |
if ( isset( $extra_meta_result['property_history'] ) ) { |
| 912 |
$property_history = array_merge( $property_history, (array) $extra_meta_result['property_history'] ); |
| 913 |
} |
| 914 |
if ( isset( $extra_meta_result['extra_meta_log'] ) ) { |
| 915 |
$extra_meta_log .= $extra_meta_result['extra_meta_log']; |
| 916 |
} |
| 917 |
|
| 918 |
mlsimport_saas_single_write_import_custom_logs( $extra_meta_log, $tip_import ); |
| 919 |
|
| 920 |
$extra_meta_log = null; |
| 921 |
$extra_meta_result = null; |
| 922 |
|
| 923 |
// Start working on Property Media |
| 924 |
// |
| 925 |
|
| 926 |
$media = $property['Media']; |
| 927 |
$media_history = $this->mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ); |
| 928 |
$property_history = array_merge( $property_history, (array) $media_history ); |
| 929 |
$media = null; |
| 930 |
$media_history = null; |
| 931 |
|
| 932 |
// Updateing property title and ending |
| 933 |
// |
| 934 |
|
| 935 |
$new_title = $this->mlsimport_saas_update_property_title( $property_id, $item_id_array['item_id'], $property ); |
| 936 |
$property_history[] = 'Updated title to ' . $new_title . '</br>'; |
| 937 |
|
| 938 |
// extra fields to be checked |
| 939 |
$global_extra_fields = array(); |
| 940 |
$mlsimport->admin->env_data->correlation_update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ); |
| 941 |
|
| 942 |
// saving history |
| 943 |
if ( ! empty( $property_history ) ) { |
| 944 |
$disable_history = intval( get_option( 'mlsimport-disable-history', 1 ) ); |
| 945 |
if ( 1 === intval( $disable_history ) ) { |
| 946 |
$property_history[] = '---------------------------------------------------------------</br>'; |
| 947 |
$property_history = implode( '</br>', $property_history ); |
| 948 |
update_post_meta( $property_id, 'mlsimport_property_history', $property_history ); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
$logs = PHP_EOL . 'Ending on Property ' . $property_id . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $is_insert . ' with new title: ' . $new_title . ' ' . PHP_EOL; |
| 953 |
mlsimport_saas_single_write_import_custom_logs( $logs, $tip_import ); |
| 954 |
|
| 955 |
$capture = ob_get_contents(); |
| 956 |
ob_end_clean(); |
| 957 |
mlsimport_saas_single_write_import_custom_logs( $capture, $tip_import ); |
| 958 |
|
| 959 |
$post = null; |
| 960 |
$capture = null; |
| 961 |
$property_status = null; |
| 962 |
$new_agent = null; |
| 963 |
$new_author = null; |
| 964 |
$property_history = null; |
| 965 |
$tax_log = null; |
| 966 |
$meta_log = null; |
| 967 |
$extra_meta_log = null; |
| 968 |
$media_history = null; |
| 969 |
$logs = null; |
| 970 |
$capture = null; |
| 971 |
clean_post_cache( $property_id ); |
| 972 |
wp_cache_flush(); |
| 973 |
gc_collect_cycles(); |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
public function mlsimport_mem_usage() { |
| 983 |
$mem_usage = memory_get_usage( true ); |
| 984 |
$mem_usage_show = round( $mem_usage / 1048576, 2 ); |
| 985 |
return $mem_usage_show . 'mb '; |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
/** |
| 990 |
* prepare meta data |
| 991 |
* |
| 992 |
* @since 1.0.0 |
| 993 |
* @access protected |
| 994 |
* @var string $plugin_name |
| 995 |
*/ |
| 996 |
public function mlsimport_saas_prepare_meta_for_property( $property ) { |
| 997 |
|
| 998 |
if ( isset( $property['extra_meta']['BathroomsTotalDecimal'] ) && floatval( $property['extra_meta']['BathroomsTotalDecimal'] ) > 0 ) { |
| 999 |
$property['meta']['property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); |
| 1000 |
$property['meta']['fave_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); |
| 1001 |
$property['meta']['REAL_HOMES_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); |
| 1002 |
} |
| 1003 |
return $property; |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
/** |
| 1011 |
* attach media to post |
| 1012 |
* |
| 1013 |
* @since 1.0.0 |
| 1014 |
* @access protected |
| 1015 |
* @var string $plugin_name |
| 1016 |
*/ |
| 1017 |
public function mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ) { |
| 1018 |
|
| 1019 |
|
| 1020 |
$media_history = array(); |
| 1021 |
if ( 'no' === $is_insert ) { |
| 1022 |
$media_history[] = ' Media - We have edit - images are not replaced'; |
| 1023 |
return $media_history; |
| 1024 |
} |
| 1025 |
|
| 1026 |
global $mlsimport; |
| 1027 |
include_once ABSPATH . 'wp-admin/includes/image.php'; |
| 1028 |
$has_featured = false; |
| 1029 |
$all_images = array(); |
| 1030 |
|
| 1031 |
delete_post_meta( $property_id, 'fave_property_images' ); |
| 1032 |
delete_post_meta( $property_id, 'REAL_HOMES_property_images' ); |
| 1033 |
|
| 1034 |
add_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); |
| 1035 |
|
| 1036 |
// sorting media |
| 1037 |
if ( isset( $media[0]['Order'] ) ) { |
| 1038 |
$order = array_column( $media, 'Order' ); |
| 1039 |
array_multisort( $order, SORT_ASC, $media ); |
| 1040 |
} |
| 1041 |
|
| 1042 |
if ( is_array( $media ) ) { |
| 1043 |
foreach ( $media as $key => $image ) : |
| 1044 |
if ( isset( $image['MediaCategory'] ) && 'Photo' !== $image['MediaCategory'] ) { |
| 1045 |
continue; |
| 1046 |
} |
| 1047 |
|
| 1048 |
$file = $image['MediaURL']; |
| 1049 |
|
| 1050 |
$media_url = ''; |
| 1051 |
if ( isset( $image['MediaURL'] ) ) { |
| 1052 |
$attachment = array( |
| 1053 |
'guid' => $image['MediaURL'], |
| 1054 |
'post_status' => 'inherit', |
| 1055 |
'post_content' => '', |
| 1056 |
'post_parent' => $property_id, |
| 1057 |
); |
| 1058 |
|
| 1059 |
if ( isset( $image['MimeType'] ) ) { |
| 1060 |
$attachment['post_mime_type'] = $image['MimeType']; |
| 1061 |
} else { |
| 1062 |
$attachment['post_mime_type'] = 'image/jpg'; |
| 1063 |
} |
| 1064 |
|
| 1065 |
if ( isset( $image['MediaKey'] ) ) { |
| 1066 |
$attachment['post_title'] = $image['MediaKey']; |
| 1067 |
} else { |
| 1068 |
$attachment['post_title'] = ''; |
| 1069 |
} |
| 1070 |
|
| 1071 |
$attach_id = wp_insert_attachment( $attachment, $file ); |
| 1072 |
|
| 1073 |
$media_history[] = ' Media - Added ' . $image['MediaURL'] . ' as attachement ' . $attach_id; |
| 1074 |
// wp_generate_attachment_metadata($attach_id,$image['MediaURL']); |
| 1075 |
$mlsimport->admin->env_data->enviroment_image_save( $property_id, $attach_id ); |
| 1076 |
|
| 1077 |
update_post_meta( $attach_id, 'is_mlsimport', 1 ); |
| 1078 |
if ( ! $has_featured ) { |
| 1079 |
set_post_thumbnail( $property_id, $attach_id ); |
| 1080 |
$has_featured = true; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
endforeach; |
| 1084 |
} else { |
| 1085 |
$media_history[] = ' Media data is blank - there are no images'; |
| 1086 |
} |
| 1087 |
remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); |
| 1088 |
|
| 1089 |
$media_history = implode( '</br>', $media_history ); |
| 1090 |
return $media_history; |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
function wpc_unset_imagesizes( $sizes ) { |
| 1096 |
$sizes = array(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
/** |
| 1109 |
* return user option |
| 1110 |
* |
| 1111 |
* @since 1.0.0 |
| 1112 |
* @access protected |
| 1113 |
* @var string $plugin_name |
| 1114 |
*/ |
| 1115 |
public function mlsimport_saas_theme_import_select_user( $selected ) { |
| 1116 |
$blog_list = ''; |
| 1117 |
$blogusers = get_users( 'blog_id=1&orderby=nicename' ); |
| 1118 |
foreach ( $blogusers as $user ) { |
| 1119 |
$the_id = $user->ID; |
| 1120 |
$blog_list .= '<option value="' . $the_id . '" '; |
| 1121 |
if ( $the_id === intval($selected) ) { |
| 1122 |
$blog_list .= ' selected="selected" '; |
| 1123 |
} |
| 1124 |
$blog_list .= '>' . $user->user_login . '</option>'; |
| 1125 |
} |
| 1126 |
return $blog_list; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
/** |
| 1137 |
* return agent option |
| 1138 |
* |
| 1139 |
* @since 1.0.0 |
| 1140 |
* @access protected |
| 1141 |
* @var string $plugin_name |
| 1142 |
*/ |
| 1143 |
public function mlsimport_saas_theme_import_select_agent( $selected ) { |
| 1144 |
global $mlsimport; |
| 1145 |
$args2 = array( |
| 1146 |
'post_type' => $mlsimport->admin->env_data->get_agent_post_type(), |
| 1147 |
'post_status' => 'publish', |
| 1148 |
'posts_per_page' => 150, |
| 1149 |
); |
| 1150 |
|
| 1151 |
if ( method_exists( $mlsimport, 'get_agent_post_type' ) ) { |
| 1152 |
$args2['post_type'] = $mlsimport->admin->env_data->get_agent_post_type(); |
| 1153 |
} |
| 1154 |
|
| 1155 |
$agent_selection2 = new WP_Query( $args2 ); |
| 1156 |
$agent_list_sec = '<option value=""><option>'; |
| 1157 |
|
| 1158 |
while ( $agent_selection2->have_posts() ) { |
| 1159 |
$agent_selection2->the_post(); |
| 1160 |
$the_id = get_the_ID(); |
| 1161 |
|
| 1162 |
$agent_list_sec .= '<option value="' . $the_id . '" '; |
| 1163 |
if ( intval($selected) === $the_id ) { |
| 1164 |
$agent_list_sec .= ' selected="selected" '; |
| 1165 |
} |
| 1166 |
$agent_list_sec .= '>' . get_the_title() . '</option>'; |
| 1167 |
} |
| 1168 |
wp_reset_postdata(); |
| 1169 |
|
| 1170 |
return $agent_list_sec; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
/** |
| 1175 |
* delete property |
| 1176 |
* |
| 1177 |
* @since 1.0.0 |
| 1178 |
* @access protected |
| 1179 |
* @var string $plugin_name |
| 1180 |
*/ |
| 1181 |
public function delete_property( $delete_id, $ListingKey ) { |
| 1182 |
if ( intval( $delete_id ) > 0 ) { |
| 1183 |
$arguments = array( |
| 1184 |
'numberposts' => -1, |
| 1185 |
'post_type' => 'attachment', |
| 1186 |
'post_parent' => $delete_id, |
| 1187 |
'post_status' => null, |
| 1188 |
'orderby' => 'menu_order', |
| 1189 |
'order' => 'ASC', |
| 1190 |
); |
| 1191 |
$post_attachments = get_posts( $arguments ); |
| 1192 |
|
| 1193 |
foreach ( $post_attachments as $attachment ) { |
| 1194 |
wp_delete_post( $attachment->ID ); |
| 1195 |
} |
| 1196 |
|
| 1197 |
wp_delete_post( $delete_id ); |
| 1198 |
$log_entry = ' Property with id ' . $delete_id . ' and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL; |
| 1199 |
mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
/** |
| 1214 |
* return_array with title items |
| 1215 |
* |
| 1216 |
* @since 1.0.0 |
| 1217 |
* @access protected |
| 1218 |
* @var string $plugin_name |
| 1219 |
*/ |
| 1220 |
public function str_between_all( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { |
| 1221 |
$strings = array(); |
| 1222 |
$length = strlen( $string ); |
| 1223 |
|
| 1224 |
while ( $offset < $length ) { |
| 1225 |
$found = $this->str_between( $string, $start, $end, $includeDelimiters, $offset ); |
| 1226 |
if ( null === $found ) { |
| 1227 |
break; |
| 1228 |
} |
| 1229 |
|
| 1230 |
$strings[] = $found; |
| 1231 |
$offset += strlen( $includeDelimiters ? $found : $start . $found . $end ); // move offset to the end of the newfound string |
| 1232 |
} |
| 1233 |
|
| 1234 |
return $strings; |
| 1235 |
} |
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
/** |
| 1246 |
* str_between |
| 1247 |
* |
| 1248 |
* @since 1.0.0 |
| 1249 |
* @access protected |
| 1250 |
* @var string $plugin_name |
| 1251 |
*/ |
| 1252 |
public function str_between( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { |
| 1253 |
if ('' === $string || '' === $start || '' === $end ) { |
| 1254 |
return null; |
| 1255 |
} |
| 1256 |
|
| 1257 |
$startLength = strlen( $start ); |
| 1258 |
$endLength = strlen( $end ); |
| 1259 |
|
| 1260 |
$startPos = strpos( $string, $start, $offset ); |
| 1261 |
if ( false === $startPos ) { |
| 1262 |
return null; |
| 1263 |
} |
| 1264 |
|
| 1265 |
$endPos = strpos( $string, $end, $startPos + $startLength ); |
| 1266 |
if ( false === $endPos) { |
| 1267 |
return null; |
| 1268 |
} |
| 1269 |
|
| 1270 |
$length = $endPos - $startPos + ( $includeDelimiters ? $endLength : -$startLength ); |
| 1271 |
if ( ! $length ) { |
| 1272 |
return ''; |
| 1273 |
} |
| 1274 |
|
| 1275 |
$offset = $startPos + ( $includeDelimiters ? 0 : $startLength ); |
| 1276 |
|
| 1277 |
$result = substr( $string, $offset, $length ); |
| 1278 |
|
| 1279 |
return ( false !== $result ? $result : null ); |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
/** |
| 1290 |
* delete property via sql |
| 1291 |
* |
| 1292 |
* @since 1.0.0 |
| 1293 |
* @access protected |
| 1294 |
* @var string $plugin_name |
| 1295 |
*/ |
| 1296 |
public function mlsimport_saas_delete_property_via_mysql( $delete_id, $ListingKey ) { |
| 1297 |
|
| 1298 |
$post_type = get_post_type( $delete_id ); |
| 1299 |
|
| 1300 |
if ( 'estate_property' === $post_type || 'property' === $post_type ) { |
| 1301 |
$term_obj_list = get_the_terms( $delete_id, 'property_status' ); |
| 1302 |
$delete_id_status = join( ', ', wp_list_pluck( $term_obj_list, 'name' ) ); |
| 1303 |
|
| 1304 |
$ListingKey = get_post_meta( $delete_id, 'ListingKey', true ); |
| 1305 |
if ( '' === $ListingKey ) { // manual added listing |
| 1306 |
$log_entry = 'User added listing with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL; |
| 1307 |
mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); |
| 1308 |
return; |
| 1309 |
} |
| 1310 |
|
| 1311 |
global $wpdb; |
| 1312 |
$wpdb->query( |
| 1313 |
$wpdb->prepare( |
| 1314 |
" |
| 1315 |
DELETE FROM $wpdb->postmeta |
| 1316 |
WHERE `post_id` = %d", |
| 1317 |
$delete_id |
| 1318 |
) |
| 1319 |
); |
| 1320 |
|
| 1321 |
$wpdb->query( |
| 1322 |
$wpdb->prepare( |
| 1323 |
" |
| 1324 |
DELETE FROM $wpdb->posts |
| 1325 |
WHERE `post_parent` = %d", |
| 1326 |
$delete_id |
| 1327 |
) |
| 1328 |
); |
| 1329 |
|
| 1330 |
$wpdb->query( |
| 1331 |
$wpdb->prepare( |
| 1332 |
" |
| 1333 |
DELETE FROM $wpdb->posts |
| 1334 |
WHERE ID = %d", |
| 1335 |
$delete_id |
| 1336 |
) |
| 1337 |
); |
| 1338 |
|
| 1339 |
$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; |
| 1340 |
mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|