| 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 |
* Deal with extra meta |
| 76 |
*/ |
| 77 |
public function mlsimportSaasSetExtraMeta2( $property_id, $property ) { |
| 78 |
$property_history = array(); |
| 79 |
$extra_meta_log = array(); |
| 80 |
$answer = array(); |
| 81 |
$options = get_option( 'mlsimport_admin_fields_select' ); |
| 82 |
$permited_meta = $options['mls-fields']; |
| 83 |
|
| 84 |
|
| 85 |
if ( isset( $property['extra_meta'] ) && is_array( $property['extra_meta'] ) ) { |
| 86 |
$meta_properties = $property['extra_meta']; |
| 87 |
|
| 88 |
foreach ( $meta_properties as $meta_name => $meta_value ) : |
| 89 |
// check if extra meta is set to import |
| 90 |
if ( ! isset( $permited_meta[ $meta_name ] ) ) { |
| 91 |
// we do not have the extra meta |
| 92 |
|
| 93 |
continue; |
| 94 |
} elseif ( isset( $permited_meta[ $meta_name ] ) && intval( $permited_meta[ $meta_name ] ) === 0 ) { |
| 95 |
// meta exists but is set to no |
| 96 |
|
| 97 |
continue; |
| 98 |
} |
| 99 |
$orignal_meta_name = $meta_name; |
| 100 |
$meta_name = strtolower( $meta_name ); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if( isset( $options['mls-fields-map-postmeta'][ $orignal_meta_name ]) && $options['mls-fields-map-postmeta'][ $orignal_meta_name ]!=='' ){ |
| 105 |
$new_post_meta_key=$options['mls-fields-map-postmeta'][ $orignal_meta_name ]; |
| 106 |
|
| 107 |
if ( is_array( $meta_value ) ) { |
| 108 |
$meta_value = implode( ',', $meta_value ); |
| 109 |
} |
| 110 |
|
| 111 |
update_post_meta( $property_id, $new_post_meta_key, $meta_value ); |
| 112 |
$property_history[] = 'Updated CUSTOM post meta ' . $new_post_meta_key . ' original ' . $meta_name . ' new meta '.$new_post_meta_key.' and value ' . $meta_value; |
| 113 |
} |
| 114 |
else if( isset( $options['mls-fields-map-taxonomy'][ $orignal_meta_name ]) && $options['mls-fields-map-taxonomy'][ $orignal_meta_name ]!=='' ){ |
| 115 |
$new_taxonomy=$options['mls-fields-map-taxonomy'][ $orignal_meta_name ]; |
| 116 |
|
| 117 |
$custom_label=$options['mls-fields-label'][ $orignal_meta_name ]; |
| 118 |
if ($custom_label=='none'){ |
| 119 |
$custom_label=''; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
if(!is_array($meta_value)){ |
| 124 |
$meta_value_with_label = array( trim($custom_label.' '.$meta_value) ); |
| 125 |
}else{ |
| 126 |
$meta_value_with_label=array( trim( $custom_label.' '.implode(', ',$meta_value)) ); |
| 127 |
} |
| 128 |
|
| 129 |
wp_set_object_terms( $property_id, $meta_value_with_label, $new_taxonomy, true ); |
| 130 |
clean_term_cache( $property_id, $new_taxonomy ); |
| 131 |
|
| 132 |
|
| 133 |
$property_history[] = 'Updated CUSTOM TAX: ' . $new_taxonomy . '<-- original '.$orignal_meta_name.'/' . $meta_name .'/'.$custom_label. ' and value ' . json_encode($meta_value_with_label); |
| 134 |
}else{ |
| 135 |
|
| 136 |
if ( is_array( $meta_value ) ) { |
| 137 |
$meta_value = implode( ',', $meta_value ); |
| 138 |
} |
| 139 |
|
| 140 |
update_post_meta( $property_id, $meta_name, $meta_value ); |
| 141 |
$property_history[] = 'Updated EXTRA Meta ' . $meta_name . ' with meta_value ' . $meta_value; |
| 142 |
$mem_usage = memory_get_usage( true ); |
| 143 |
$mem_usage_show = round( $mem_usage / 1048576, 2 ); |
| 144 |
$extra_meta_log[] = 'Memory:' . $mem_usage_show . ' Property with ID ' . $property_id . ' Updated EXTRA Meta ' . $meta_name . ' with value ' . $meta_value; |
| 145 |
|
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
// Remove empty values and decode values in one pass |
| 153 |
$processed_field_values = array(); |
| 154 |
if(isset($field_values) && is_array($field_values)){ |
| 155 |
foreach ( $field_values as $value ) { |
| 156 |
if ( ! empty( $value ) ) { |
| 157 |
$processed_field_values[] = $value; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Bulk update terms if array is not empty |
| 163 |
if ( ! empty( $processed_field_values ) ) { |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
$meta_value = null; |
| 173 |
|
| 174 |
endforeach; |
| 175 |
|
| 176 |
$answer['property_history'] = implode( '</br>', $property_history ); |
| 177 |
$answer['extra_meta_log'] = implode( PHP_EOL, $extra_meta_log ); |
| 178 |
} |
| 179 |
|
| 180 |
$property_history = null; |
| 181 |
$extra_meta_log = null; |
| 182 |
$options = null; |
| 183 |
$permited_meta = null; |
| 184 |
return $answer; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Deal with extra meta |
| 190 |
*/ |
| 191 |
public function mlsimportSaasSetExtraMeta($property_id, $property) { |
| 192 |
// Memory tracking |
| 193 |
$startMemory = memory_get_usage(true); |
| 194 |
$startMemoryFormatted = round($startMemory / 1048576, 2); |
| 195 |
//error_log("[Memory-ExtraMeta] Starting extra meta processing with memory: {$startMemoryFormatted} MB"); |
| 196 |
|
| 197 |
$property_history = array(); |
| 198 |
$answer = array(); |
| 199 |
|
| 200 |
if (!isset($property['extra_meta']) || !is_array($property['extra_meta'])) { |
| 201 |
$answer['property_history'] = ''; |
| 202 |
|
| 203 |
$endMemory = memory_get_usage(true); |
| 204 |
$endMemoryFormatted = round($endMemory / 1048576, 2); |
| 205 |
$memoryDiff = $endMemoryFormatted - $startMemoryFormatted; |
| 206 |
//error_log("[Memory-ExtraMeta] No extra meta, finished with memory: {$endMemoryFormatted} MB (change: +{$memoryDiff} MB)"); |
| 207 |
|
| 208 |
return $answer; |
| 209 |
} |
| 210 |
|
| 211 |
// Get options once and extract only what we need |
| 212 |
$options = get_option('mlsimport_admin_fields_select'); |
| 213 |
$permited_meta = isset($options['mls-fields']) ? $options['mls-fields'] : array(); |
| 214 |
|
| 215 |
// Process meta in smaller batches |
| 216 |
$meta_batches = array_chunk(array_keys($property['extra_meta']), 10, true); |
| 217 |
|
| 218 |
$batch_count = 0; |
| 219 |
foreach ($meta_batches as $meta_batch_keys) { |
| 220 |
$batch_count++; |
| 221 |
|
| 222 |
$batchMemory = memory_get_usage(true); |
| 223 |
$batchMemoryFormatted = round($batchMemory / 1048576, 2); |
| 224 |
//error_log("[Memory-ExtraMeta] Processing batch {$batch_count} with memory: {$batchMemoryFormatted} MB"); |
| 225 |
|
| 226 |
foreach ($meta_batch_keys as $meta_name) { |
| 227 |
// Skip if meta doesn't exist in property extra_meta |
| 228 |
if (!isset($property['extra_meta'][$meta_name])) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
$meta_value = $property['extra_meta'][$meta_name]; |
| 233 |
|
| 234 |
// Check if extra meta is set to import |
| 235 |
if (!isset($permited_meta[$meta_name])) { |
| 236 |
// We do not have the extra meta |
| 237 |
continue; |
| 238 |
} elseif (isset($permited_meta[$meta_name]) && intval($permited_meta[$meta_name]) === 0) { |
| 239 |
// Meta exists but is set to no |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
$original_meta_name = $meta_name; |
| 244 |
$meta_name_lower = strtolower($meta_name); |
| 245 |
|
| 246 |
// Process custom postmeta mapping |
| 247 |
if (isset($options['mls-fields-map-postmeta'][$original_meta_name]) && $options['mls-fields-map-postmeta'][$original_meta_name] !== '') { |
| 248 |
$new_post_meta_key = $options['mls-fields-map-postmeta'][$original_meta_name]; |
| 249 |
|
| 250 |
if (is_array($meta_value)) { |
| 251 |
$meta_value = implode(',', $meta_value); |
| 252 |
} |
| 253 |
|
| 254 |
update_post_meta($property_id, $new_post_meta_key, $meta_value); |
| 255 |
$property_history[] = 'Updated CUSTOM post meta ' . $new_post_meta_key . ' original ' . $meta_name; |
| 256 |
} |
| 257 |
// Process custom taxonomy mapping |
| 258 |
else if (isset($options['mls-fields-map-taxonomy'][$original_meta_name]) && $options['mls-fields-map-taxonomy'][$original_meta_name] !== '') { |
| 259 |
$new_taxonomy = $options['mls-fields-map-taxonomy'][$original_meta_name]; |
| 260 |
$custom_label = isset($options['mls-fields-label'][$original_meta_name]) ? $options['mls-fields-label'][$original_meta_name] : ''; |
| 261 |
|
| 262 |
if ($custom_label == 'none') { |
| 263 |
$custom_label = ''; |
| 264 |
} |
| 265 |
|
| 266 |
if (!is_array($meta_value)) { |
| 267 |
$meta_value_with_label = array(trim($custom_label . ' ' . $meta_value)); |
| 268 |
} else { |
| 269 |
$meta_value_with_label = array(trim($custom_label . ' ' . implode(', ', $meta_value))); |
| 270 |
} |
| 271 |
|
| 272 |
wp_set_object_terms($property_id, $meta_value_with_label, $new_taxonomy, true); |
| 273 |
|
| 274 |
// Fix the clean_term_cache call to avoid SQL errors |
| 275 |
if (!empty($meta_value_with_label)) { |
| 276 |
$term_ids = array(); |
| 277 |
foreach ($meta_value_with_label as $term_name) { |
| 278 |
$term = get_term_by('name', $term_name, $new_taxonomy); |
| 279 |
if ($term && !is_wp_error($term)) { |
| 280 |
$term_ids[] = $term->term_id; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if (!empty($term_ids)) { |
| 285 |
clean_term_cache($term_ids, $new_taxonomy); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
$property_history[] = 'Updated CUSTOM TAX: ' . $new_taxonomy . ' original ' . $original_meta_name; |
| 290 |
} |
| 291 |
// Standard meta update |
| 292 |
else { |
| 293 |
if (is_array($meta_value)) { |
| 294 |
$meta_value = implode(',', $meta_value); |
| 295 |
} |
| 296 |
|
| 297 |
update_post_meta($property_id, $meta_name_lower, $meta_value); |
| 298 |
$property_history[] = 'Updated EXTRA Meta ' . $meta_name_lower; |
| 299 |
} |
| 300 |
|
| 301 |
// Clear each variable after use |
| 302 |
$meta_value = null; |
| 303 |
} |
| 304 |
|
| 305 |
// Clean up after each batch |
| 306 |
wp_cache_flush(); |
| 307 |
gc_collect_cycles(); |
| 308 |
|
| 309 |
$afterBatchMemory = memory_get_usage(true); |
| 310 |
$afterBatchMemoryFormatted = round($afterBatchMemory / 1048576, 2); |
| 311 |
$batchMemoryDiff = $afterBatchMemoryFormatted - $batchMemoryFormatted; |
| 312 |
//error_log("[Memory-ExtraMeta] Finished batch {$batch_count} with memory: {$afterBatchMemoryFormatted} MB (change: +{$batchMemoryDiff} MB)"); |
| 313 |
} |
| 314 |
|
| 315 |
// Remove unused code that was referencing undefined variables |
| 316 |
// (The processed_field_values code wasn't being used) |
| 317 |
|
| 318 |
// Prepare the answer with limited history to conserve memory |
| 319 |
if (count($property_history) > 20) { |
| 320 |
$property_history = array_slice($property_history, -20); |
| 321 |
$property_history[] = '... [truncated history to save memory] ...'; |
| 322 |
} |
| 323 |
|
| 324 |
$answer['property_history'] = implode('</br>', $property_history); |
| 325 |
|
| 326 |
// Clean up variables |
| 327 |
$property_history = null; |
| 328 |
$options = null; |
| 329 |
$permited_meta = null; |
| 330 |
|
| 331 |
// Final memory tracking |
| 332 |
$endMemory = memory_get_usage(true); |
| 333 |
$endMemoryFormatted = round($endMemory / 1048576, 2); |
| 334 |
$memoryDiff = $endMemoryFormatted - $startMemoryFormatted; |
| 335 |
//error_log("[Memory-ExtraMeta] Finished extra meta processing with memory: {$endMemoryFormatted} MB (change: +{$memoryDiff} MB)"); |
| 336 |
|
| 337 |
return $answer; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* set hardcode fields after updated |
| 346 |
* |
| 347 |
* @since 1.0.0 |
| 348 |
* @access protected |
| 349 |
* @var string $plugin_name |
| 350 |
*/ |
| 351 |
public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { |
| 352 |
if ( 'yes' === $is_insert ) { |
| 353 |
update_post_meta( $property_id, 'prop_featured', 0 ); |
| 354 |
update_post_meta( $property_id, 'page_custom_zoom', 16 ); |
| 355 |
update_post_meta( $property_id, 'property_country', 'United States' ); |
| 356 |
|
| 357 |
update_post_meta( $property_id, 'property_agent', $new_agent ); |
| 358 |
update_post_meta( $property_id, 'property_page_desing_local', '' ); |
| 359 |
update_post_meta( $property_id, 'header_transparent', 'global' ); |
| 360 |
update_post_meta( $property_id, 'page_show_adv_search', 'global' ); |
| 361 |
update_post_meta( $property_id, 'page_show_adv_search', 'global' ); |
| 362 |
update_post_meta( $property_id, 'header_type', 0 ); |
| 363 |
update_post_meta( $property_id, 'sidebar_agent_option', 'global' ); |
| 364 |
update_post_meta( $property_id, 'local_pgpr_slider_type', 'global' ); |
| 365 |
update_post_meta( $property_id, 'local_pgpr_content_type', 'global' ); |
| 366 |
update_post_meta( $property_id, 'sidebar_select', 'global' ); |
| 367 |
update_post_meta( $property_id, 'sidebar_option', 'global' ); |
| 368 |
|
| 369 |
if ( function_exists( 'wpestate_update_hiddent_address_single' ) ) { |
| 370 |
wpestate_update_hiddent_address_single( $property_id ); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
/** |
| 379 |
* save custom fields per environment |
| 380 |
* |
| 381 |
* @since 1.0.0 |
| 382 |
* @access protected |
| 383 |
* @var string $plugin_name |
| 384 |
*/ |
| 385 |
public function enviroment_custom_fields( $option_name ) { |
| 386 |
$theme_options = get_option( 'wpresidence_admin' ); |
| 387 |
$custom_fields = array(); |
| 388 |
if ( isset( $theme_options['wpestate_custom_fields_list'] ) ) { |
| 389 |
$custom_fields = $theme_options['wpestate_custom_fields_list']; |
| 390 |
} |
| 391 |
|
| 392 |
if ( ! is_array( $custom_fields ) ) { |
| 393 |
$custom_fields = array(); |
| 394 |
} |
| 395 |
|
| 396 |
foreach ( array( 'add_field_name', 'add_field_label', 'add_field_type', 'add_field_order', 'add_dropdown_order' ) as $field_key ) { |
| 397 |
if ( ! isset( $custom_fields[ $field_key ] ) || ! is_array( $custom_fields[ $field_key ] ) ) { |
| 398 |
$custom_fields[ $field_key ] = array(); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
$options = get_option( $option_name . '_admin_fields_select' ); |
| 403 |
|
| 404 |
foreach ( $options['mls-fields'] as $key => $value ) { |
| 405 |
$import = intval( $value ); |
| 406 |
$admin = isset( $options['mls-fields-admin'][ $key ] ) ? intval( $options['mls-fields-admin'][ $key ] ) : 0; |
| 407 |
$taxonomy = isset( $options['mls-fields-map-taxonomy'][ $key ] ) ? $options['mls-fields-map-taxonomy'][ $key ] : ''; |
| 408 |
$order_value = isset( $options['field_order'][ $key ] ) ? intval( $options['field_order'][ $key ] ) + 100 : 100; |
| 409 |
|
| 410 |
if ( 1 === $import && 0 === $admin && '' === $taxonomy ) { |
| 411 |
$existing_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 412 |
|
| 413 |
if ( false === $existing_index ) { |
| 414 |
$custom_fields['add_field_name'][] = $key; |
| 415 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 416 |
$custom_fields['add_field_label'][] = $label; |
| 417 |
$custom_fields['add_field_type'][] = 'short text'; |
| 418 |
$custom_fields['add_field_order'][] = $order_value; |
| 419 |
$custom_fields['add_dropdown_order'][] = ''; |
| 420 |
} else { |
| 421 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 422 |
$custom_fields['add_field_label'][ $existing_index ] = $label; |
| 423 |
$custom_fields['add_field_order'][ $existing_index ] = $order_value; |
| 424 |
} |
| 425 |
} else { |
| 426 |
$remove_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 427 |
if ( false !== $remove_index ) { |
| 428 |
unset( $custom_fields['add_field_name'][ $remove_index ] ); |
| 429 |
unset( $custom_fields['add_field_label'][ $remove_index ] ); |
| 430 |
unset( $custom_fields['add_field_type'][ $remove_index ] ); |
| 431 |
unset( $custom_fields['add_field_order'][ $remove_index ] ); |
| 432 |
unset( $custom_fields['add_dropdown_order'][ $remove_index ] ); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
if ( ! empty( $custom_fields['add_field_order'] ) ) { |
| 438 |
asort( $custom_fields['add_field_order'] ); |
| 439 |
$ordered = array( |
| 440 |
'add_field_name' => array(), |
| 441 |
'add_field_label' => array(), |
| 442 |
'add_field_type' => array(), |
| 443 |
'add_field_order' => array(), |
| 444 |
'add_dropdown_order' => array(), |
| 445 |
); |
| 446 |
foreach ( array_keys( $custom_fields['add_field_order'] ) as $idx ) { |
| 447 |
$ordered['add_field_name'][] = $custom_fields['add_field_name'][ $idx ]; |
| 448 |
$ordered['add_field_label'][] = $custom_fields['add_field_label'][ $idx ]; |
| 449 |
$ordered['add_field_type'][] = $custom_fields['add_field_type'][ $idx ]; |
| 450 |
$ordered['add_field_order'][] = $custom_fields['add_field_order'][ $idx ]; |
| 451 |
$ordered['add_dropdown_order'][] = $custom_fields['add_dropdown_order'][ $idx ]; |
| 452 |
} |
| 453 |
$custom_fields = $ordered; |
| 454 |
} |
| 455 |
|
| 456 |
$theme_options['wpestate_custom_fields_list'] = $custom_fields; |
| 457 |
update_option( 'wpresidence_admin', $theme_options ); |
| 458 |
|
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
/** |
| 467 |
* return theme schema |
| 468 |
* |
| 469 |
* @since 1.0.0 |
| 470 |
* @access protected |
| 471 |
* @var string $plugin_name |
| 472 |
*/ |
| 473 |
public function return_theme_schema() { |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
} |
| 480 |
|