| 1 |
<?php |
| 2 |
/** |
| 3 |
* Live mode: explicit-set page blocks address listings by ListingKey. |
| 4 |
* |
| 5 |
* With no local posts there are no post IDs — a block's ids/id field holds |
| 6 |
* ListingKey STRINGS (never cast to int). One key fetch renders the set, |
| 7 |
* order preserved: List-by-ID as the paginated grid, Property List and the |
| 8 |
* Content Slider as cards, Featured Property as the design card, and the |
| 9 |
* Map block as embedded markers. |
| 10 |
* |
| 11 |
* @package Mlsimport |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* A block's ids arg as an ordered ListingKey list (comma string or array; |
| 20 |
* blanks dropped, strings preserved). |
| 21 |
* |
| 22 |
* @param mixed $raw The block's ids arg. |
| 23 |
* @return string[] |
| 24 |
*/ |
| 25 |
function mlsimport_live_selection_keys( $raw ): array { |
| 26 |
$values = is_array( $raw ) ? $raw : explode( ',', (string) $raw ); |
| 27 |
return array_values( array_filter( array_map( 'trim', array_map( 'strval', $values ) ), 'strlen' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Render the List-by-ID block from ListingKeys when live mode is on. |
| 32 |
* |
| 33 |
* @param string|null $pre Prior short-circuit value. |
| 34 |
* @param array $args Block args (ids, count). |
| 35 |
* @return string|null |
| 36 |
*/ |
| 37 |
function mlsimport_live_list_by_id( $pre, $args ) { |
| 38 |
if ( null !== $pre || ! mlsimport_live_mode_active() ) { |
| 39 |
return $pre; |
| 40 |
} |
| 41 |
|
| 42 |
$keys = mlsimport_live_selection_keys( isset( $args['ids'] ) ? $args['ids'] : '' ); |
| 43 |
if ( array() === $keys ) { |
| 44 |
return $pre; |
| 45 |
} |
| 46 |
|
| 47 |
$per_page = isset( $args['count'] ) ? max( 1, (int) $args['count'] ) : 12; |
| 48 |
$total = count( $keys ); |
| 49 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only GET paging of a fixed key set. |
| 50 |
$current = isset( $_GET['page'] ) ? max( 1, (int) $_GET['page'] ) : 1; |
| 51 |
|
| 52 |
$page_keys = array_slice( $keys, ( $current - 1 ) * $per_page, $per_page ); |
| 53 |
$records = mlsimport_live_keys( $page_keys ); |
| 54 |
$records = is_array( $records ) ? $records : array(); |
| 55 |
|
| 56 |
$cards = Mlsimport_Standalone_Render::render_cards( mlsimport_live_payload( $records, $total ) ); |
| 57 |
|
| 58 |
// Same markup as the stored block: the grid plus the one shared pager |
| 59 |
// wrapper every listing surface uses. |
| 60 |
return '<div class="mlsimport-page-block mlsimport-page-block--list-by-id">' |
| 61 |
. '<div class="mlsimport-results__grid">' . $cards . '</div>' |
| 62 |
. '<div class="mlsimport-results__pager">' . Mlsimport_Pagination::render( $total, $per_page, $current ) . '</div>' |
| 63 |
. '</div>'; |
| 64 |
} |
| 65 |
add_filter( 'mlsimport_page_block_list_by_id_pre', 'mlsimport_live_list_by_id', 10, 2 ); |
| 66 |
|
| 67 |
/** |
| 68 |
* Render an explicit-set block's cards (Property List, Content Slider) from |
| 69 |
* ListingKeys when live mode is on. Blocks without an ids selection fall |
| 70 |
* through to the query path, which live mode already serves. |
| 71 |
* |
| 72 |
* @param string|null $pre Prior short-circuit value. |
| 73 |
* @param array $args Block args (ids). |
| 74 |
* @param string $slide_class Extra card wrapper class ('' or splide__slide). |
| 75 |
* @return string|null |
| 76 |
*/ |
| 77 |
function mlsimport_live_ids_cards( $pre, $args, $slide_class ) { |
| 78 |
if ( null !== $pre || ! mlsimport_live_mode_active() ) { |
| 79 |
return $pre; |
| 80 |
} |
| 81 |
|
| 82 |
$keys = mlsimport_live_selection_keys( isset( $args['ids'] ) ? $args['ids'] : '' ); |
| 83 |
if ( array() === $keys ) { |
| 84 |
return $pre; |
| 85 |
} |
| 86 |
|
| 87 |
$records = mlsimport_live_keys( $keys ); |
| 88 |
$records = is_array( $records ) ? $records : array(); |
| 89 |
|
| 90 |
return Mlsimport_Standalone_Render::render_cards( mlsimport_live_payload( $records, count( $records ) ), (string) $slide_class ); |
| 91 |
} |
| 92 |
add_filter( 'mlsimport_page_block_ids_cards_pre', 'mlsimport_live_ids_cards', 10, 3 ); |
| 93 |
|
| 94 |
/** |
| 95 |
* Render the Featured Property card from a ListingKey when live mode is on. |
| 96 |
* The card context mirrors the stored builder's, from the live card view |
| 97 |
* (which fills permalink/photo/address from the raw record); the blurb comes |
| 98 |
* from PublicRemarks, as the importer would have stored it. An unknown key |
| 99 |
* renders nothing, like a missing post. |
| 100 |
* |
| 101 |
* @param string|null $pre Prior short-circuit value. |
| 102 |
* @param array $args Block args (id, design). |
| 103 |
* @return string|null |
| 104 |
*/ |
| 105 |
function mlsimport_live_featured_card( $pre, $args ) { |
| 106 |
if ( null !== $pre || ! mlsimport_live_mode_active() ) { |
| 107 |
return $pre; |
| 108 |
} |
| 109 |
|
| 110 |
$key = trim( (string) ( isset( $args['id'] ) ? $args['id'] : '' ) ); |
| 111 |
if ( '' === $key ) { |
| 112 |
return $pre; |
| 113 |
} |
| 114 |
|
| 115 |
$record = mlsimport_live_get( $key ); |
| 116 |
if ( ! is_array( $record ) ) { |
| 117 |
return ''; |
| 118 |
} |
| 119 |
|
| 120 |
$payload = mlsimport_live_payload( array( $record ), 1 ); |
| 121 |
$post = $payload['posts'][0]; |
| 122 |
$row = $payload['rows'][ $post->ID ]; |
| 123 |
$v = mlsimport_card_view( $post, $row ); |
| 124 |
|
| 125 |
$c = array( |
| 126 |
'id' => $row->listing_key, |
| 127 |
'permalink' => $v['permalink'], |
| 128 |
'address' => $v['address'], |
| 129 |
'price' => $v['price_fmt'], |
| 130 |
'status' => $v['status'], |
| 131 |
'specs' => implode( ' · ', $v['specs'] ), |
| 132 |
'excerpt' => wp_trim_words( wp_strip_all_tags( (string) ( $record['PublicRemarks'] ?? '' ) ), 26, '…' ), |
| 133 |
'img' => '' !== $v['thumb'] ? '<img class="mlsimport-featured__img" src="' . esc_url( $v['thumb'] ) . '" alt="' . esc_attr( $v['address'] ) . '" />' : '', |
| 134 |
'img_url' => $v['thumb'], |
| 135 |
'agent' => null, |
| 136 |
); |
| 137 |
|
| 138 |
return mlsimport_featured_card_render( $c, isset( $args['design'] ) ? (int) $args['design'] : 1 ); |
| 139 |
} |
| 140 |
add_filter( 'mlsimport_page_block_featured_card_pre', 'mlsimport_live_featured_card', 10, 2 ); |
| 141 |
|
| 142 |
/** |
| 143 |
* Build the hand-picked Map block's marker set from ListingKeys when live |
| 144 |
* mode is on — the same markers the viewport map builds, embedded directly |
| 145 |
* (small by definition). Keys the MLS no longer knows just have no pin. |
| 146 |
* |
| 147 |
* @param array|null $pre Prior short-circuit value. |
| 148 |
* @param array $args Block args (ids). |
| 149 |
* @return array|null |
| 150 |
*/ |
| 151 |
function mlsimport_live_ids_markers( $pre, $args ) { |
| 152 |
if ( null !== $pre || ! mlsimport_live_mode_active() ) { |
| 153 |
return $pre; |
| 154 |
} |
| 155 |
|
| 156 |
$keys = mlsimport_live_selection_keys( isset( $args['ids'] ) ? $args['ids'] : '' ); |
| 157 |
if ( array() === $keys ) { |
| 158 |
return $pre; |
| 159 |
} |
| 160 |
|
| 161 |
$markers = array(); |
| 162 |
foreach ( (array) mlsimport_live_keys( $keys ) as $record ) { |
| 163 |
$marker = is_array( $record ) ? mlsimport_live_marker_from_record( $record ) : null; |
| 164 |
if ( null !== $marker ) { |
| 165 |
$markers[] = $marker; |
| 166 |
} |
| 167 |
} |
| 168 |
return $markers; |
| 169 |
} |
| 170 |
add_filter( 'mlsimport_page_block_map_markers_pre', 'mlsimport_live_ids_markers', 10, 2 ); |
| 171 |
|