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

RealHomesClass.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 6.3.3, at enviroment/RealHomesClass.php

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