| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Description of HouzezClass |
| 8 |
* |
| 9 |
* @author mlsimport |
| 10 |
*/ |
| 11 |
class HouzezClass { |
| 12 |
|
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
// Enable support for remote MLS images in media JS and thumbnails |
| 16 |
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'inject_remote_image_data' ], 20 ); |
| 17 |
add_filter( 'image_downsize', [ $this, 'override_image_downsize' ], 10, 3 ); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* return custom post field |
| 23 |
* |
| 24 |
* @since 1.0.0 |
| 25 |
* @access protected |
| 26 |
* @var string $plugin_name |
| 27 |
*/ |
| 28 |
public function get_property_post_type() { |
| 29 |
return 'property'; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* return custom post field |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @access protected |
| 38 |
* @var string $plugin_name |
| 39 |
*/ |
| 40 |
public function get_agent_post_type() { |
| 41 |
return array('houzez_agency','houzez_agent'); |
| 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 |
add_post_meta( $property_id, 'fave_property_images', intval( $attach_id ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* gallery save |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* @access protected |
| 60 |
* @var string $plugin_name |
| 61 |
*/ |
| 62 |
public function enviroment_image_save_gallery( $property_id, $post_attachments ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Format extra meta values into a safe string representation. |
| 69 |
*/ |
| 70 |
private function normalizeExtraMetaValue($meta_value) { |
| 71 |
if (is_array($meta_value)) { |
| 72 |
$normalized = array(); |
| 73 |
|
| 74 |
foreach ($meta_value as $value) { |
| 75 |
if (is_array($value)) { |
| 76 |
$encoded = function_exists('wp_json_encode') ? wp_json_encode($value) : json_encode($value); |
| 77 |
|
| 78 |
if (false !== $encoded && null !== $encoded) { |
| 79 |
$normalized[] = $encoded; |
| 80 |
} |
| 81 |
} else { |
| 82 |
$value = trim((string) $value); |
| 83 |
if ('' !== $value) { |
| 84 |
$normalized[] = $value; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (empty($normalized)) { |
| 90 |
return ''; |
| 91 |
} |
| 92 |
|
| 93 |
return implode(', ', $normalized); |
| 94 |
} |
| 95 |
|
| 96 |
return preg_replace('/\s*,\s*/', ', ', trim((string) $meta_value)); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Format Rooms extra meta entries into a single string. |
| 101 |
*/ |
| 102 |
private function formatRoomsExtraMeta($rooms) { |
| 103 |
if (!is_array($rooms) || empty($rooms)) { |
| 104 |
return ''; |
| 105 |
} |
| 106 |
|
| 107 |
$formatted_rooms = array(); |
| 108 |
|
| 109 |
foreach ($rooms as $room_details) { |
| 110 |
if (!is_array($room_details)) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$room_type = isset($room_details['RoomType']) ? trim((string) $room_details['RoomType']) : ''; |
| 115 |
$room_level = isset($room_details['RoomLevel']) ? trim((string) $room_details['RoomLevel']) : ''; |
| 116 |
$room_length = isset($room_details['RoomLength']) ? trim((string) $room_details['RoomLength']) : ''; |
| 117 |
$room_width = isset($room_details['RoomWidth']) ? trim((string) $room_details['RoomWidth']) : ''; |
| 118 |
$room_units = isset($room_details['RoomLengthWidthUnits']) ? trim((string) $room_details['RoomLengthWidthUnits']) : ''; |
| 119 |
|
| 120 |
if ($room_type === '') { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
$details = array(); |
| 125 |
if ($room_level !== '') { |
| 126 |
$details[] = $room_level; |
| 127 |
} |
| 128 |
|
| 129 |
$dimension = ''; |
| 130 |
if ($room_length !== '' && $room_width !== '') { |
| 131 |
$dimension = $room_length . ' x ' . $room_width; |
| 132 |
} elseif ($room_length !== '') { |
| 133 |
$dimension = $room_length; |
| 134 |
} elseif ($room_width !== '') { |
| 135 |
$dimension = $room_width; |
| 136 |
} |
| 137 |
|
| 138 |
if ($dimension !== '') { |
| 139 |
if ($room_units !== '') { |
| 140 |
$dimension .= ' ' . $room_units; |
| 141 |
} |
| 142 |
$details[] = $dimension; |
| 143 |
} elseif ($room_units !== '') { |
| 144 |
$details[] = $room_units; |
| 145 |
} |
| 146 |
|
| 147 |
$formatted_value = $room_type; |
| 148 |
if (!empty($details)) { |
| 149 |
$formatted_value .= ': ' . implode(', ', $details); |
| 150 |
} |
| 151 |
|
| 152 |
$formatted_rooms[] = $formatted_value; |
| 153 |
} |
| 154 |
|
| 155 |
return implode(' | ', $formatted_rooms); |
| 156 |
} |
| 157 |
|
| 158 |
public function return_extra_fields |
| 159 |
( $property_id ) { |
| 160 |
return get_post_meta( $property_id, 'additional_features', true ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* Deal with extra meta |
| 166 |
*/ |
| 167 |
public function mlsimportSaasSetExtraMeta( $property_id, $property ) { |
| 168 |
$property_history = ''; |
| 169 |
$extra_meta_log = ''; |
| 170 |
$answer = array(); |
| 171 |
$extra_fields = array(); |
| 172 |
$options = get_option('mlsimport_admin_fields_select'); |
| 173 |
$permited_meta = isset($options['mls-fields']) ? $options['mls-fields'] : array(); |
| 174 |
|
| 175 |
if ( isset( $property['meta']['houzez_geolocation_long'] ) && isset( $property['meta']['houzez_geolocation_lat'] ) ) { |
| 176 |
$savingx = $property['meta']['houzez_geolocation_lat'] . ',' . $property['meta']['houzez_geolocation_long']; |
| 177 |
update_post_meta( $property_id, 'fave_property_location', $savingx ); |
| 178 |
update_post_meta( $property_id, 'property_location', $savingx ); |
| 179 |
$property_history .= 'Update Coordinates Meta (having houzez_geolocation_long ) with ' . $savingx . '</br>'; |
| 180 |
$extra_meta_log .= 'Property with ID ' . $property_id . ' Update Coordinates Meta with ' . $savingx . PHP_EOL; |
| 181 |
} elseif ( isset( $property['meta']['property_longitude'] ) && isset( $property['meta']['property_latitude'] ) ) { |
| 182 |
$savingx = $property['meta']['property_latitude'] . ',' . $property['meta']['property_longitude']; |
| 183 |
update_post_meta( $property_id, 'fave_property_location', $savingx ); |
| 184 |
update_post_meta( $property_id, 'property_location', $savingx ); |
| 185 |
$property_history .= 'Update Coordinates Meta (having property_longitude) with ' . $savingx . '</br>'; |
| 186 |
$extra_meta_log .= 'Property with ID ' . $property_id . ' Update Coordinates Meta with ' . $savingx . PHP_EOL; |
| 187 |
} |
| 188 |
|
| 189 |
if ( isset( $property['extra_meta'] ) && is_array( $property['extra_meta'] ) ) { |
| 190 |
$meta_properties = $property['extra_meta']; |
| 191 |
|
| 192 |
foreach ( $meta_properties as $meta_name => $meta_value ) { |
| 193 |
if ( ! isset( $permited_meta[ $meta_name ] ) || intval( $permited_meta[ $meta_name ] ) === 0 ) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
if ( 'Rooms' === $meta_name && is_array( $meta_value ) ) { |
| 198 |
$formatted_rooms = $this->formatRoomsExtraMeta( $meta_value ); |
| 199 |
if ( '' === $formatted_rooms ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
// Save the formatted value on the dedicated meta key and |
| 204 |
// reuse the string for the rest of the processing pipeline |
| 205 |
// (custom mappings, additional features, etc.). |
| 206 |
update_post_meta( $property_id, 'rooms', $formatted_rooms ); |
| 207 |
$property_history .= 'Updated EXTRA Meta rooms</br>'; |
| 208 |
$extra_meta_log .= 'Property with ID ' . $property_id . ' Updated EXTRA Meta rooms with value ' . $formatted_rooms . PHP_EOL; |
| 209 |
|
| 210 |
$meta_value = $formatted_rooms; |
| 211 |
} |
| 212 |
|
| 213 |
$meta_value = $this->normalizeExtraMetaValue( $meta_value ); |
| 214 |
$orignal_meta_name = $meta_name; |
| 215 |
$feature_name = isset( $options['mls-fields-label'][ $meta_name ] ) && '' !== $options['mls-fields-label'][ $meta_name ] ? $options['mls-fields-label'][ $meta_name ] : $meta_name; |
| 216 |
|
| 217 |
if ( isset( $options['mls-fields-map-postmeta'][ $orignal_meta_name ] ) && $options['mls-fields-map-postmeta'][ $orignal_meta_name ] !== '' ) { |
| 218 |
$new_post_meta_key = $options['mls-fields-map-postmeta'][ $orignal_meta_name ]; |
| 219 |
update_post_meta( $property_id, $new_post_meta_key, $meta_value ); |
| 220 |
$log_msg = 'Property with ID ' . $property_id . ' Updated CUSTOM post meta ' . $new_post_meta_key . ' original ' . $meta_name . ' with value ' . $meta_value; |
| 221 |
$property_history .= $log_msg . '</br>'; |
| 222 |
$extra_meta_log .= $log_msg . PHP_EOL; |
| 223 |
continue; |
| 224 |
} |
| 225 |
|
| 226 |
if ( isset( $options['mls-fields-map-taxonomy'][ $orignal_meta_name ] ) && $options['mls-fields-map-taxonomy'][ $orignal_meta_name ] !== '' ) { |
| 227 |
$new_taxonomy = $options['mls-fields-map-taxonomy'][ $orignal_meta_name ]; |
| 228 |
$custom_label = isset( $options['mls-fields-label'][ $orignal_meta_name ] ) ? $options['mls-fields-label'][ $orignal_meta_name ] : ''; |
| 229 |
if ( 'none' === $custom_label ) { |
| 230 |
$custom_label = ''; |
| 231 |
} |
| 232 |
$term_value = trim( $custom_label . ' ' . $meta_value ); |
| 233 |
$meta_value_with_label = array(); |
| 234 |
if ( '' !== $term_value ) { |
| 235 |
$meta_value_with_label[] = $term_value; |
| 236 |
wp_set_object_terms( $property_id, $meta_value_with_label, $new_taxonomy, true ); |
| 237 |
clean_term_cache( $property_id, $new_taxonomy ); |
| 238 |
} |
| 239 |
$property_history .= 'Updated CUSTOM TAX: ' . $new_taxonomy . '<-- original ' . $orignal_meta_name . '/' . $meta_name . '/' . $custom_label . ' value ' . json_encode( $meta_value_with_label ); |
| 240 |
continue; |
| 241 |
} |
| 242 |
|
| 243 |
if ( |
| 244 |
'' !== $meta_value && |
| 245 |
isset( $options['mls-fields'][ $meta_name ] ) && |
| 246 |
1 === intval( $options['mls-fields'][ $meta_name ] ) |
| 247 |
) { |
| 248 |
if ( |
| 249 |
isset( $options['mls-fields-admin'][ $meta_name ] ) && |
| 250 |
0 === intval( $options['mls-fields-admin'][ $meta_name ] ) |
| 251 |
) { |
| 252 |
if ( isset( $options['field_order'][ $orignal_meta_name ] ) ) { |
| 253 |
$order = intval( $options['field_order'][ $orignal_meta_name ] ); |
| 254 |
} else { |
| 255 |
$index = array_search( $orignal_meta_name, $options['field_order'], true ); |
| 256 |
$order = ( false !== $index ) ? intval( $index ) : 9999; |
| 257 |
} |
| 258 |
$extra_fields[] = array( |
| 259 |
'meta_name' => $orignal_meta_name, |
| 260 |
'fave_additional_feature_title' => $feature_name, |
| 261 |
'fave_additional_feature_value' => $meta_value, |
| 262 |
'order' => $order, |
| 263 |
); |
| 264 |
} elseif ( |
| 265 |
isset( $options['mls-fields-admin'][ $meta_name ] ) && |
| 266 |
1 === intval( $options['mls-fields-admin'][ $meta_name ] ) |
| 267 |
) { |
| 268 |
update_post_meta( $property_id, strtolower( $feature_name ), $meta_value ); |
| 269 |
$log_msg = 'Property with ID ' . $property_id . ' Updated EXTRA Meta ' . $meta_name . ' label ' . $feature_name . ' with value ' . $meta_value; |
| 270 |
$property_history .= $log_msg . '</br>'; |
| 271 |
$extra_meta_log .= $log_msg . PHP_EOL; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
usort( |
| 277 |
$extra_fields, |
| 278 |
function ( $a, $b ) { |
| 279 |
$orderA = isset( $a['order'] ) ? intval( $a['order'] ) : 9999; |
| 280 |
$orderB = isset( $b['order'] ) ? intval( $b['order'] ) : 9999; |
| 281 |
return $orderA <=> $orderB; |
| 282 |
} |
| 283 |
); |
| 284 |
|
| 285 |
$ordered_extra_fields = array(); |
| 286 |
foreach ( $extra_fields as $field ) { |
| 287 |
$ordered_extra_fields[] = array( |
| 288 |
'fave_additional_feature_title' => $field['fave_additional_feature_title'], |
| 289 |
'fave_additional_feature_value' => $field['fave_additional_feature_value'], |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
update_post_meta( $property_id, 'additional_features', $ordered_extra_fields ); |
| 294 |
} |
| 295 |
|
| 296 |
$answer['property_history'] = $property_history; |
| 297 |
$answer['extra_meta_log'] = $extra_meta_log; |
| 298 |
|
| 299 |
return $answer; |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* set hardcode fields after updated |
| 304 |
* |
| 305 |
* @since 1.0.0 |
| 306 |
* @access protected |
| 307 |
* @var string $plugin_name |
| 308 |
*/ |
| 309 |
public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { |
| 310 |
if ( 'yes' === $is_insert ) { |
| 311 |
$options_mls = get_option( 'mlsimport_admin_mls_sync' ); |
| 312 |
if ( is_array( $options_mls ) && isset( $options_mls['property_agent'] ) ) { |
| 313 |
update_post_meta( $property_id, 'fave_agents', $options_mls['property_agent'] ); |
| 314 |
} |
| 315 |
update_post_meta( $property_id, 'fave_agents', $new_agent ); |
| 316 |
update_post_meta( $property_id, 'fave_agent_display_option', 'agent_info' ); |
| 317 |
update_post_meta( $property_id, 'fave_featured', 0 ); |
| 318 |
// fave_agents |
| 319 |
|
| 320 |
update_post_meta( $property_id, 'fave_property_map', '1' ); |
| 321 |
update_post_meta( $property_id, 'fave_property_map_street_view', 'show' ); |
| 322 |
update_post_meta( $property_id, 'fave_single_top_area', 'global' ); |
| 323 |
update_post_meta( $property_id, 'fave_single_content_area', 'global' ); |
| 324 |
update_post_meta( $property_id, 'fave_additional_features_enable', 'enable' ); |
| 325 |
|
| 326 |
$fave_property_size_prefix = get_post_meta( $property_id, 'fave_property_size_prefix', true ); |
| 327 |
if ( '' === $fave_property_size_prefix ) { |
| 328 |
update_post_meta( $property_id, 'fave_property_size_prefix', 'Sq Ft' ); |
| 329 |
} |
| 330 |
|
| 331 |
$fave_property_land_postfix = get_post_meta( $property_id, 'fave_property_land_postfix', true ); |
| 332 |
if ( '' === $fave_property_land_postfix ) { |
| 333 |
update_post_meta( $property_id, 'fave_property_land_postfix', 'Sq Ft' ); |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/** |
| 340 |
* save custom fields per environment |
| 341 |
*/ |
| 342 |
public function enviroment_custom_fields( $option_name ) { |
| 343 |
return; |
| 344 |
$theme_options = get_option( 'wpresidence_admin' ); |
| 345 |
$custom_fields = $theme_options['wpestate_custom_fields_list']; |
| 346 |
$custom_field_no = 100; |
| 347 |
|
| 348 |
$options = get_option( $option_name . '_admin_fields_select' ); |
| 349 |
|
| 350 |
$custom_fields_admin = array(); |
| 351 |
|
| 352 |
$test = 0; |
| 353 |
foreach ( $options['mls-fields'] as $key => $value ) { |
| 354 |
++$test; |
| 355 |
|
| 356 |
if ( 1 === $value && 0 === intval( $options['mls-fields-admin'][$key]) ) { |
| 357 |
if ( !in_array( $key, $custom_fields['add_field_name'] ) && '' !== $key ) { |
| 358 |
++$custom_field_no; |
| 359 |
$custom_fields['add_field_name'][] = $key; |
| 360 |
$custom_fields['add_field_label'][] = $key; |
| 361 |
$custom_fields['add_field_type'][] = 'short text'; |
| 362 |
$custom_fields['add_field_order'][] = $custom_field_no; |
| 363 |
$custom_fields['add_dropdown_order'][] = ''; |
| 364 |
} |
| 365 |
} else { |
| 366 |
// remove item from custom fields |
| 367 |
$key_remove = array_search( $key, $custom_fields['add_field_name'] ); |
| 368 |
|
| 369 |
unset( $custom_fields['add_field_name'][ $key_remove ] ); |
| 370 |
unset( $custom_fields['add_field_label'][ $key_remove ] ); |
| 371 |
unset( $custom_fields['add_field_type'][ $key_remove ] ); |
| 372 |
unset( $custom_fields['add_field_order'][ $key_remove ] ); |
| 373 |
unset( $custom_fields['add_dropdown_order'][ $key_remove ] ); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
$theme_options['wpestate_custom_fields_list'] = $custom_fields; |
| 378 |
update_option( 'wpresidence_admin', $theme_options ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* return theme schema |
| 383 |
* |
| 384 |
* @since 1.0.0 |
| 385 |
* @access protected |
| 386 |
* @var string $plugin_name |
| 387 |
* property_type - is type, property_status is action,property_feature is features,property_label is status, |
| 388 |
* property_city is city, property_area is area,property_state is county state |
| 389 |
*/ |
| 390 |
public function return_theme_schema() { |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
/** |
| 397 |
* Filter wp_prepare_attachment_for_js to inject remote image URL and fake sizes. |
| 398 |
* |
| 399 |
* This ensures Meta Box and the WordPress media library display thumbnails |
| 400 |
* for images that are stored remotely (i.e., imported via MLS). |
| 401 |
* |
| 402 |
* @param array $response Attachment response data prepared for JS. |
| 403 |
* @return array Modified response with remote image URL and sizes if applicable. |
| 404 |
*/ |
| 405 |
public function inject_remote_image_data( $response ) { |
| 406 |
if ( empty( $response['id'] ) ) { |
| 407 |
return $response; |
| 408 |
} |
| 409 |
|
| 410 |
$attachment_id = $response['id']; |
| 411 |
|
| 412 |
// Only modify attachments imported by MLS |
| 413 |
if ( intval( get_post_meta( $attachment_id, 'is_mlsimport', true ) ) === 1 ) { |
| 414 |
$attachment = get_post( $attachment_id ); |
| 415 |
if ( ! $attachment ) { |
| 416 |
return $response; |
| 417 |
} |
| 418 |
|
| 419 |
// Determine the correct remote URL |
| 420 |
$url = filter_var( $attachment->guid, FILTER_VALIDATE_URL ) |
| 421 |
? $attachment->guid |
| 422 |
: get_post_meta( $attachment_id, 'houzez_external_url', true ); |
| 423 |
|
| 424 |
// Inject the URL and fake thumbnail size into the response |
| 425 |
if ( $url ) { |
| 426 |
$response['url'] = $url; |
| 427 |
$response['sizes'] = [ |
| 428 |
'thumbnail' => [ |
| 429 |
'url' => $url, |
| 430 |
'width' => 150, |
| 431 |
'height' => 150, |
| 432 |
], |
| 433 |
]; |
| 434 |
|
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
return $response; |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Filter image_downsize to return remote image data instead of local file paths. |
| 443 |
* |
| 444 |
* This enables WordPress functions like wp_get_attachment_image() to work |
| 445 |
* with remote images by returning a URL and fake dimensions. |
| 446 |
* |
| 447 |
* @param bool|array $out Whether to short-circuit the image downsize. Default false. |
| 448 |
* @param int $id Attachment ID. |
| 449 |
* @param string|array $size Requested image size. |
| 450 |
* @return array|false Array of image data (URL, width, height, crop) or false to fall back. |
| 451 |
*/ |
| 452 |
public function override_image_downsize( $out, $id, $size ) { |
| 453 |
// Only override for MLS-imported attachments |
| 454 |
if ( intval( get_post_meta( $id, 'is_mlsimport', true ) ) !== 1 ) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
$attachment = get_post( $id ); |
| 459 |
if ( ! $attachment ) { |
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
// Determine the remote image URL |
| 464 |
$url = filter_var( $attachment->guid, FILTER_VALIDATE_URL ) |
| 465 |
? $attachment->guid |
| 466 |
: get_post_meta( $id, 'houzez_external_url', true ); |
| 467 |
|
| 468 |
if ( ! $url ) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
// Return URL with fake dimensions (used for thumbnail display) |
| 475 |
return [ $url, 150, 150, false ]; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
} |
| 480 |
|