| 1 |
<?php |
| 2 |
/** |
| 3 |
* Live mode: rebuild the card view model from the live row. |
| 4 |
* |
| 5 |
* The mlsimport_card_view() helper derives address/permalink/thumbnail from post meta and |
| 6 |
* attachments — all empty on a synthetic negative-ID post. The card-view filter |
| 7 |
* fires last with the row in hand, so this one callback re-derives those pieces |
| 8 |
* from the raw record in $row->_live. Scalars (price, specs, status) already |
| 9 |
* come from the row and need no help. |
| 10 |
* |
| 11 |
* @package Mlsimport |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Fill the card view from the raw RESO record carried by a live row. |
| 20 |
* |
| 21 |
* Keys on the row carrying _live rather than the gate: a payload built while |
| 22 |
* live mode was on should render correctly even if rendering happens later. |
| 23 |
* |
| 24 |
* @param array $view The view model mlsimport_card_view() built. |
| 25 |
* @param WP_Post $post The (synthetic) listing post. |
| 26 |
* @param object|null $row The live row. |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
function mlsimport_live_card_view( array $view, $post, $row ): array { |
| 30 |
// Not a live row (no raw record attached): leave the stored-mode view as-is. |
| 31 |
if ( ! is_object( $row ) || empty( $row->_live ) || ! is_array( $row->_live ) ) { |
| 32 |
return $view; |
| 33 |
} |
| 34 |
$record = $row->_live; |
| 35 |
|
| 36 |
// Address: the raw record's UnparsedAddress, if present and non-blank. |
| 37 |
$address = isset( $record['UnparsedAddress'] ) ? trim( (string) $record['UnparsedAddress'] ) : ''; |
| 38 |
if ( '' !== $address ) { |
| 39 |
$view['address'] = $address; |
| 40 |
} |
| 41 |
|
| 42 |
// Permalink points at the virtual single-listing URL keyed by ListingKey; |
| 43 |
// office name comes straight off the record. |
| 44 |
$view['permalink'] = mlsimport_live_url( isset( $row->listing_key ) ? (string) $row->listing_key : '' ); |
| 45 |
$view['office'] = isset( $record['ListOfficeName'] ) ? trim( (string) $record['ListOfficeName'] ) : ''; |
| 46 |
|
| 47 |
// Thumbnail: the first media URL the row resolved; has_thumb flags whether |
| 48 |
// the card should render an image at all. |
| 49 |
$thumb = isset( $row->thumb ) ? (string) $row->thumb : ''; |
| 50 |
$view['thumb'] = $thumb; |
| 51 |
$view['has_thumb'] = '' !== $thumb; |
| 52 |
|
| 53 |
return $view; |
| 54 |
} |
| 55 |
add_filter( 'mlsimport_card_view', 'mlsimport_live_card_view', 10, 3 ); |
| 56 |
|