| 1 |
<?php |
| 2 |
/** |
| 3 |
* Live mode: raw RESO record -> the row/view-model shapes the render stack |
| 4 |
* already consumes. |
| 5 |
* |
| 6 |
* The row mirrors a wp_mlsimport_listings flat row (same columns, built by the |
| 7 |
* same reso-map via Mlsimport_Standalone_Row::build_columns()) so the card and |
| 8 |
* grid code read live rows unchanged. _live carries the raw record so the card |
| 9 |
* view and single-page VM can rebuild richer data without another fetch. |
| 10 |
* |
| 11 |
* @package Mlsimport |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
require_once dirname( __DIR__ ) . '/standalone/class-mlsimport-standalone-row.php'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Build a flat-table-shaped row object from a raw RESO record. |
| 22 |
* |
| 23 |
* @param array $record Raw RESO property (PascalCase fields, Media[] optional). |
| 24 |
* @return object Row with the flat-table columns + listing_key, thumb, _live. |
| 25 |
*/ |
| 26 |
function mlsimport_live_row_from_reso( array $record ): object { |
| 27 |
// Same reso-map the flat-table build uses, so live rows match stored rows. |
| 28 |
$row = Mlsimport_Standalone_Row::build_columns( $record ); |
| 29 |
|
| 30 |
// Carry the ListingKey for permalinks/media lookups. |
| 31 |
$row['listing_key'] = isset( $record['ListingKey'] ) ? (string) $record['ListingKey'] : ''; |
| 32 |
|
| 33 |
// First photo URL as the thumbnail (empty when the record has no media). |
| 34 |
$urls = mlsimport_live_media_urls( $record ); |
| 35 |
$row['thumb'] = array() === $urls ? '' : $urls[0]; |
| 36 |
// Stash the raw record for the card/single rebuilds that need richer data. |
| 37 |
$row['_live'] = $record; |
| 38 |
|
| 39 |
// Hand back an object (the render stack reads rows as objects). |
| 40 |
return (object) $row; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* The record's public property-photo URLs, in delivered order — the same |
| 45 |
* filter the import applies (MediaCategory Property Photo/Photo, non-empty |
| 46 |
* MediaURL) but returning CDN URLs instead of sideloading. |
| 47 |
* |
| 48 |
* @param array $record Raw RESO property. |
| 49 |
* @return string[] Photo URLs. |
| 50 |
*/ |
| 51 |
function mlsimport_live_media_urls( array $record ): array { |
| 52 |
// No media collection at all: no URLs. |
| 53 |
if ( empty( $record['Media'] ) || ! is_array( $record['Media'] ) ) { |
| 54 |
return array(); |
| 55 |
} |
| 56 |
|
| 57 |
$urls = array(); |
| 58 |
// Keep delivered order; filter to property photos with a real URL. |
| 59 |
foreach ( $record['Media'] as $item ) { |
| 60 |
// Skip malformed items and ones with no MediaURL. |
| 61 |
if ( ! is_array( $item ) || empty( $item['MediaURL'] ) ) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
// When a category is present, keep only Property Photo / Photo. |
| 65 |
if ( isset( $item['MediaCategory'] ) && ! in_array( $item['MediaCategory'], array( 'Property Photo', 'Photo' ), true ) ) { |
| 66 |
continue; |
| 67 |
} |
| 68 |
$urls[] = (string) $item['MediaURL']; |
| 69 |
} |
| 70 |
|
| 71 |
return $urls; |
| 72 |
} |
| 73 |
|