| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
|
| 7 |
/** |
| 8 |
* Description of ResidenceClass |
| 9 |
* |
| 10 |
* @author mlsimport |
| 11 |
*/ |
| 12 |
class ResidenceClass { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* return custom post field |
| 21 |
* |
| 22 |
* @since 1.0.0 |
| 23 |
* @access protected |
| 24 |
* @var string $plugin_name |
| 25 |
*/ |
| 26 |
public function get_property_post_type() { |
| 27 |
return 'estate_property'; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* return custom post field |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @access protected |
| 37 |
* @var string $plugin_name |
| 38 |
*/ |
| 39 |
public function get_agent_post_type() { |
| 40 |
return array('estate_agent','estate_agency','estate_developer'); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* image save |
| 46 |
* |
| 47 |
* @since 1.0.0 |
| 48 |
* @access protected |
| 49 |
* @var string $plugin_name |
| 50 |
*/ |
| 51 |
public function enviroment_image_save( $property_id, $attach_id ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* image save |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* @access protected |
| 62 |
* @var string $plugin_name |
| 63 |
*/ |
| 64 |
public function enviroment_image_save_gallery( $property_id, $post_attachments ) { |
| 65 |
$gallery_meta = implode(',', $post_attachments); |
| 66 |
update_post_meta($property_id, 'wpestate_property_gallery', $post_attachments); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* Format extra meta values into a safe string representation. |
| 76 |
*/ |
| 77 |
private function normalizeExtraMetaValue($meta_value) { |
| 78 |
if (is_array($meta_value)) { |
| 79 |
$normalized = array(); |
| 80 |
|
| 81 |
foreach ($meta_value as $value) { |
| 82 |
if (is_array($value)) { |
| 83 |
if (function_exists('wp_json_encode')) { |
| 84 |
$encoded = wp_json_encode($value); |
| 85 |
} else { |
| 86 |
$encoded = json_encode($value); |
| 87 |
} |
| 88 |
|
| 89 |
if (false !== $encoded && null !== $encoded) { |
| 90 |
$normalized[] = $encoded; |
| 91 |
} |
| 92 |
} else { |
| 93 |
$value = trim((string) $value); |
| 94 |
if ('' !== $value) { |
| 95 |
$normalized[] = $value; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (empty($normalized)) { |
| 101 |
return ''; |
| 102 |
} |
| 103 |
|
| 104 |
return implode(', ', $normalized); |
| 105 |
} |
| 106 |
|
| 107 |
return preg_replace('/\s*,\s*/', ', ', trim((string) $meta_value)); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Format Rooms extra meta entries into a single string. |
| 112 |
*/ |
| 113 |
private function formatRoomsExtraMeta($rooms, $property_id) { |
| 114 |
if (!is_array($rooms) || empty($rooms)) { |
| 115 |
return ''; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
$formatted_rooms = array(); |
| 120 |
|
| 121 |
foreach ($rooms as $index => $room_details) { |
| 122 |
if (!is_array($room_details)) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
$room_type = isset($room_details['RoomType']) ? trim((string) $room_details['RoomType']) : ''; |
| 127 |
$room_level = isset($room_details['RoomLevel']) ? trim((string) $room_details['RoomLevel']) : ''; |
| 128 |
$room_length = isset($room_details['RoomLength']) ? trim((string) $room_details['RoomLength']) : ''; |
| 129 |
$room_width = isset($room_details['RoomWidth']) ? trim((string) $room_details['RoomWidth']) : ''; |
| 130 |
$room_units = isset($room_details['RoomLengthWidthUnits']) ? trim((string) $room_details['RoomLengthWidthUnits']) : ''; |
| 131 |
|
| 132 |
if ($room_type === '') { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
$details = array(); |
| 137 |
if ($room_level !== '') { |
| 138 |
$details[] = $room_level; |
| 139 |
} |
| 140 |
|
| 141 |
$dimension = ''; |
| 142 |
if ($room_length !== '' && $room_width !== '') { |
| 143 |
$dimension = $room_length . ' x ' . $room_width; |
| 144 |
} elseif ($room_length !== '') { |
| 145 |
$dimension = $room_length; |
| 146 |
} elseif ($room_width !== '') { |
| 147 |
$dimension = $room_width; |
| 148 |
} |
| 149 |
|
| 150 |
if ($dimension !== '') { |
| 151 |
if ($room_units !== '') { |
| 152 |
$dimension .= ' ' . $room_units; |
| 153 |
} |
| 154 |
$details[] = $dimension; |
| 155 |
} elseif ($room_units !== '') { |
| 156 |
$details[] = $room_units; |
| 157 |
} |
| 158 |
|
| 159 |
$formatted_value = $room_type; |
| 160 |
if (!empty($details)) { |
| 161 |
$formatted_value .= ': ' . implode(', ', $details); |
| 162 |
} |
| 163 |
|
| 164 |
$formatted_rooms[] = $formatted_value; |
| 165 |
} |
| 166 |
|
| 167 |
$result = implode(' | ', $formatted_rooms); |
| 168 |
|
| 169 |
return $result; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/** |
| 174 |
* Deal with extra meta |
| 175 |
*/ |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Deal with extra meta |
| 180 |
*/ |
| 181 |
public function mlsimportSaasSetExtraMeta($property_id, $property) { |
| 182 |
// Memory tracking |
| 183 |
$startMemory = memory_get_usage(true); |
| 184 |
$startMemoryFormatted = round($startMemory / 1048576, 2); |
| 185 |
|
| 186 |
$property_history = array(); |
| 187 |
$answer = array(); |
| 188 |
|
| 189 |
if (!isset($property['extra_meta']) || !is_array($property['extra_meta'])) { |
| 190 |
$answer['property_history'] = ''; |
| 191 |
|
| 192 |
$endMemory = memory_get_usage(true); |
| 193 |
$endMemoryFormatted = round($endMemory / 1048576, 2); |
| 194 |
$memoryDiff = $endMemoryFormatted - $startMemoryFormatted; |
| 195 |
|
| 196 |
return $answer; |
| 197 |
} |
| 198 |
|
| 199 |
// Get options once and extract only what we need |
| 200 |
$options = get_option('mlsimport_admin_fields_select'); |
| 201 |
$permited_meta = isset($options['mls-fields']) ? $options['mls-fields'] : array(); |
| 202 |
|
| 203 |
// Process meta in smaller batches |
| 204 |
$meta_batches = array_chunk(array_keys($property['extra_meta']), 10, true); |
| 205 |
|
| 206 |
$batch_count = 0; |
| 207 |
foreach ($meta_batches as $meta_batch_keys) { |
| 208 |
$batch_count++; |
| 209 |
|
| 210 |
$batchMemory = memory_get_usage(true); |
| 211 |
$batchMemoryFormatted = round($batchMemory / 1048576, 2); |
| 212 |
|
| 213 |
foreach ($meta_batch_keys as $meta_name) { |
| 214 |
// Skip if meta doesn't exist in property extra_meta |
| 215 |
if (!isset($property['extra_meta'][$meta_name])) { |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
$meta_value = $property['extra_meta'][$meta_name]; |
| 220 |
|
| 221 |
// Check if extra meta is set to import |
| 222 |
if (!isset($permited_meta[$meta_name])) { |
| 223 |
// We do not have the extra meta |
| 224 |
continue; |
| 225 |
} elseif (isset($permited_meta[$meta_name]) && intval($permited_meta[$meta_name]) === 0) { |
| 226 |
// Meta exists but is set to no |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
if ('Rooms' === $meta_name && is_array($meta_value)) { |
| 231 |
$formatted_rooms = $this->formatRoomsExtraMeta($meta_value, $property_id); |
| 232 |
if ($formatted_rooms !== '') { |
| 233 |
update_post_meta($property_id, 'rooms', $formatted_rooms); |
| 234 |
$property_history[] = 'Updated EXTRA Meta rooms'; |
| 235 |
} |
| 236 |
continue; |
| 237 |
} |
| 238 |
|
| 239 |
$normalized_meta_value = $this->normalizeExtraMetaValue($meta_value); |
| 240 |
|
| 241 |
$original_meta_name = $meta_name; |
| 242 |
$meta_name_lower = strtolower($meta_name); |
| 243 |
|
| 244 |
// Process custom postmeta mapping |
| 245 |
if (isset($options['mls-fields-map-postmeta'][$original_meta_name]) && $options['mls-fields-map-postmeta'][$original_meta_name] !== '') { |
| 246 |
$new_post_meta_key = $options['mls-fields-map-postmeta'][$original_meta_name]; |
| 247 |
|
| 248 |
update_post_meta($property_id, $new_post_meta_key, $normalized_meta_value); |
| 249 |
$property_history[] = 'Updated CUSTOM post meta ' . $new_post_meta_key . ' original ' . $meta_name; |
| 250 |
} |
| 251 |
// Process custom taxonomy mapping |
| 252 |
else if (isset($options['mls-fields-map-taxonomy'][$original_meta_name]) && $options['mls-fields-map-taxonomy'][$original_meta_name] !== '') { |
| 253 |
$new_taxonomy = $options['mls-fields-map-taxonomy'][$original_meta_name]; |
| 254 |
$custom_label = isset($options['mls-fields-label'][$original_meta_name]) ? $options['mls-fields-label'][$original_meta_name] : ''; |
| 255 |
|
| 256 |
if ($custom_label == 'none') { |
| 257 |
$custom_label = ''; |
| 258 |
} |
| 259 |
|
| 260 |
$meta_value_with_label = array(); |
| 261 |
$term_value = trim($custom_label . ' ' . $normalized_meta_value); |
| 262 |
if ($term_value !== '') { |
| 263 |
$meta_value_with_label[] = $term_value; |
| 264 |
} |
| 265 |
|
| 266 |
if (!empty($meta_value_with_label)) { |
| 267 |
wp_set_object_terms($property_id, $meta_value_with_label, $new_taxonomy, true); |
| 268 |
|
| 269 |
// Fix the clean_term_cache call to avoid SQL errors |
| 270 |
$term_ids = array(); |
| 271 |
foreach ($meta_value_with_label as $term_name) { |
| 272 |
$term = get_term_by('name', $term_name, $new_taxonomy); |
| 273 |
if ($term && !is_wp_error($term)) { |
| 274 |
$term_ids[] = $term->term_id; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
if (!empty($term_ids)) { |
| 279 |
clean_term_cache($term_ids, $new_taxonomy); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
$property_history[] = 'Updated CUSTOM TAX: ' . $new_taxonomy . ' original ' . $original_meta_name; |
| 284 |
} |
| 285 |
// Standard meta update |
| 286 |
else { |
| 287 |
update_post_meta($property_id, $meta_name_lower, $normalized_meta_value); |
| 288 |
$property_history[] = 'Updated EXTRA Meta ' . $meta_name_lower; |
| 289 |
} |
| 290 |
|
| 291 |
// Clear each variable after use |
| 292 |
$meta_value = null; |
| 293 |
$normalized_meta_value = null; |
| 294 |
} |
| 295 |
|
| 296 |
// Clean up after each batch |
| 297 |
wp_cache_flush(); |
| 298 |
gc_collect_cycles(); |
| 299 |
|
| 300 |
$afterBatchMemory = memory_get_usage(true); |
| 301 |
$afterBatchMemoryFormatted = round($afterBatchMemory / 1048576, 2); |
| 302 |
$batchMemoryDiff = $afterBatchMemoryFormatted - $batchMemoryFormatted; |
| 303 |
} |
| 304 |
|
| 305 |
// Remove unused code that was referencing undefined variables |
| 306 |
// (The processed_field_values code wasn't being used) |
| 307 |
|
| 308 |
// Prepare the answer with limited history to conserve memory |
| 309 |
if (count($property_history) > 20) { |
| 310 |
$property_history = array_slice($property_history, -20); |
| 311 |
$property_history[] = '... [truncated history to save memory] ...'; |
| 312 |
} |
| 313 |
|
| 314 |
$answer['property_history'] = implode('</br>', $property_history); |
| 315 |
|
| 316 |
// Clean up variables |
| 317 |
$property_history = null; |
| 318 |
$options = null; |
| 319 |
$permited_meta = null; |
| 320 |
|
| 321 |
// Final memory tracking |
| 322 |
$endMemory = memory_get_usage(true); |
| 323 |
$endMemoryFormatted = round($endMemory / 1048576, 2); |
| 324 |
$memoryDiff = $endMemoryFormatted - $startMemoryFormatted; |
| 325 |
|
| 326 |
return $answer; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
/** |
| 334 |
* set hardcode fields after updated |
| 335 |
* |
| 336 |
* @since 1.0.0 |
| 337 |
* @access protected |
| 338 |
* @var string $plugin_name |
| 339 |
*/ |
| 340 |
public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { |
| 341 |
if ( 'yes' === $is_insert ) { |
| 342 |
update_post_meta( $property_id, 'prop_featured', 0 ); |
| 343 |
update_post_meta( $property_id, 'page_custom_zoom', 16 ); |
| 344 |
update_post_meta( $property_id, 'property_country', 'United States' ); |
| 345 |
|
| 346 |
update_post_meta( $property_id, 'property_agent', $new_agent ); |
| 347 |
update_post_meta( $property_id, 'property_page_desing_local', '' ); |
| 348 |
update_post_meta( $property_id, 'header_transparent', 'global' ); |
| 349 |
update_post_meta( $property_id, 'page_show_adv_search', 'global' ); |
| 350 |
update_post_meta( $property_id, 'page_show_adv_search', 'global' ); |
| 351 |
update_post_meta( $property_id, 'header_type', 0 ); |
| 352 |
update_post_meta( $property_id, 'sidebar_agent_option', 'global' ); |
| 353 |
update_post_meta( $property_id, 'local_pgpr_slider_type', 'global' ); |
| 354 |
update_post_meta( $property_id, 'local_pgpr_content_type', 'global' ); |
| 355 |
update_post_meta( $property_id, 'sidebar_select', 'global' ); |
| 356 |
update_post_meta( $property_id, 'sidebar_option', 'global' ); |
| 357 |
|
| 358 |
if ( function_exists( 'wpestate_update_hiddent_address_single' ) ) { |
| 359 |
wpestate_update_hiddent_address_single( $property_id ); |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* save custom fields per environment |
| 369 |
* |
| 370 |
* @since 1.0.0 |
| 371 |
* @access protected |
| 372 |
* @var string $plugin_name |
| 373 |
*/ |
| 374 |
public function enviroment_custom_fields( $option_name ) { |
| 375 |
$theme_options = get_option( 'wpresidence_admin' ); |
| 376 |
$custom_fields = array(); |
| 377 |
if ( isset( $theme_options['wpestate_custom_fields_list'] ) ) { |
| 378 |
$custom_fields = $theme_options['wpestate_custom_fields_list']; |
| 379 |
} |
| 380 |
|
| 381 |
if ( ! is_array( $custom_fields ) ) { |
| 382 |
$custom_fields = array(); |
| 383 |
} |
| 384 |
|
| 385 |
foreach ( array( 'add_field_name', 'add_field_label', 'add_field_type', 'add_field_order', 'add_dropdown_order' ) as $field_key ) { |
| 386 |
if ( ! isset( $custom_fields[ $field_key ] ) || ! is_array( $custom_fields[ $field_key ] ) ) { |
| 387 |
$custom_fields[ $field_key ] = array(); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
$options = get_option( $option_name . '_admin_fields_select' ); |
| 392 |
|
| 393 |
foreach ( $options['mls-fields'] as $key => $value ) { |
| 394 |
$import = intval( $value ); |
| 395 |
$admin = isset( $options['mls-fields-admin'][ $key ] ) ? intval( $options['mls-fields-admin'][ $key ] ) : 0; |
| 396 |
$taxonomy = isset( $options['mls-fields-map-taxonomy'][ $key ] ) ? $options['mls-fields-map-taxonomy'][ $key ] : ''; |
| 397 |
$order_value = isset( $options['field_order'][ $key ] ) ? intval( $options['field_order'][ $key ] ) + 100 : 100; |
| 398 |
|
| 399 |
if ( 1 === $import && 0 === $admin && '' === $taxonomy ) { |
| 400 |
$existing_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 401 |
|
| 402 |
if ( false === $existing_index ) { |
| 403 |
$custom_fields['add_field_name'][] = $key; |
| 404 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 405 |
$custom_fields['add_field_label'][] = $label; |
| 406 |
$custom_fields['add_field_type'][] = 'short text'; |
| 407 |
$custom_fields['add_field_order'][] = $order_value; |
| 408 |
$custom_fields['add_dropdown_order'][] = ''; |
| 409 |
} else { |
| 410 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 411 |
$custom_fields['add_field_label'][ $existing_index ] = $label; |
| 412 |
$custom_fields['add_field_order'][ $existing_index ] = $order_value; |
| 413 |
} |
| 414 |
} else { |
| 415 |
$remove_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 416 |
if ( false !== $remove_index ) { |
| 417 |
unset( $custom_fields['add_field_name'][ $remove_index ] ); |
| 418 |
unset( $custom_fields['add_field_label'][ $remove_index ] ); |
| 419 |
unset( $custom_fields['add_field_type'][ $remove_index ] ); |
| 420 |
unset( $custom_fields['add_field_order'][ $remove_index ] ); |
| 421 |
unset( $custom_fields['add_dropdown_order'][ $remove_index ] ); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
if ( ! empty( $custom_fields['add_field_order'] ) ) { |
| 427 |
asort( $custom_fields['add_field_order'] ); |
| 428 |
$ordered = array( |
| 429 |
'add_field_name' => array(), |
| 430 |
'add_field_label' => array(), |
| 431 |
'add_field_type' => array(), |
| 432 |
'add_field_order' => array(), |
| 433 |
'add_dropdown_order' => array(), |
| 434 |
); |
| 435 |
foreach ( array_keys( $custom_fields['add_field_order'] ) as $idx ) { |
| 436 |
$ordered['add_field_name'][] = $custom_fields['add_field_name'][ $idx ]; |
| 437 |
$ordered['add_field_label'][] = $custom_fields['add_field_label'][ $idx ]; |
| 438 |
$ordered['add_field_type'][] = $custom_fields['add_field_type'][ $idx ]; |
| 439 |
$ordered['add_field_order'][] = $custom_fields['add_field_order'][ $idx ]; |
| 440 |
$ordered['add_dropdown_order'][] = $custom_fields['add_dropdown_order'][ $idx ]; |
| 441 |
} |
| 442 |
$custom_fields = $ordered; |
| 443 |
} |
| 444 |
|
| 445 |
$theme_options['wpestate_custom_fields_list'] = $custom_fields; |
| 446 |
update_option( 'wpresidence_admin', $theme_options ); |
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
/** |
| 456 |
* return theme schema |
| 457 |
* |
| 458 |
* @since 1.0.0 |
| 459 |
* @access protected |
| 460 |
* @var string $plugin_name |
| 461 |
*/ |
| 462 |
public function return_theme_schema() { |
| 463 |
return; |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
} |
| 469 |
|