| 1 |
<?php |
| 2 |
/** |
| 3 |
* Standalone (theme_id 990) shortcodes (M7). |
| 4 |
* |
| 5 |
* Thin wrappers over the one base render fn so [mlsimport_listings], the Gutenberg |
| 6 |
* block and the Elementor widget all emit identical output from a single source. |
| 7 |
* |
| 8 |
* @package Mlsimport |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
require_once __DIR__ . '/class-mlsimport-standalone-render.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Registers and handles the standalone front-end shortcodes. |
| 19 |
*/ |
| 20 |
class Mlsimport_Standalone_Shortcodes { |
| 21 |
|
| 22 |
/** |
| 23 |
* Filter attributes a listings shortcode/block accepts, mapped 1:1 to the |
| 24 |
* render args. Scalar only; defaults are empty so unset atts are dropped. |
| 25 |
*/ |
| 26 |
private const FILTER_ATTS = array( |
| 27 |
'price_min', |
| 28 |
'price_max', |
| 29 |
'beds', |
| 30 |
'baths', |
| 31 |
'sqft_min', |
| 32 |
'sqft_max', |
| 33 |
'lot_min', |
| 34 |
'lot_max', |
| 35 |
'year_min', |
| 36 |
'year_max', |
| 37 |
'hoa_max', |
| 38 |
'dom_max', |
| 39 |
'garage_min', |
| 40 |
'stories', |
| 41 |
'subdivision', |
| 42 |
'list_date_min', |
| 43 |
'location', |
| 44 |
'city', |
| 45 |
'state', |
| 46 |
'zip', |
| 47 |
'area', |
| 48 |
'county', |
| 49 |
'high_school_district', |
| 50 |
'label', |
| 51 |
'status', |
| 52 |
'property_type', |
| 53 |
'listing_type', |
| 54 |
'keywords', |
| 55 |
'features', |
| 56 |
'agent', |
| 57 |
'orderby', |
| 58 |
'order', |
| 59 |
'limit', |
| 60 |
'page', |
| 61 |
'lat_min', |
| 62 |
'lat_max', |
| 63 |
'lng_min', |
| 64 |
'lng_max', |
| 65 |
'polygon', |
| 66 |
); |
| 67 |
|
| 68 |
/** |
| 69 |
* Register the shortcodes. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public static function register(): void { |
| 74 |
add_shortcode( 'mlsimport_listings', array( __CLASS__, 'listings' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* The filter keys the listings surfaces accept (shared with the block). |
| 79 |
* |
| 80 |
* @return string[] |
| 81 |
*/ |
| 82 |
public static function filter_keys(): array { |
| 83 |
return self::FILTER_ATTS; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* [mlsimport_listings] handler — map attributes to filter args and render. |
| 88 |
* |
| 89 |
* @param array|string $atts Shortcode attributes. |
| 90 |
* @return string |
| 91 |
*/ |
| 92 |
public static function listings( $atts ): string { |
| 93 |
$atts = (array) $atts; |
| 94 |
$args = self::atts_to_args( $atts ); |
| 95 |
// search_fields is display config (which search fields to show), not a filter |
| 96 |
// key, so it lives outside the FILTER_ATTS whitelist atts_to_args applies. |
| 97 |
if ( isset( $atts['search_fields'] ) && '' !== trim( (string) $atts['search_fields'] ) ) { |
| 98 |
$args['search_fields'] = (string) $atts['search_fields']; |
| 99 |
} |
| 100 |
// fields_per_row (how many search fields per row) is display config, not a |
| 101 |
// filter key, so it also lives outside the FILTER_ATTS whitelist. |
| 102 |
if ( isset( $atts['fields_per_row'] ) && '' !== trim( (string) $atts['fields_per_row'] ) ) { |
| 103 |
$args['fields_per_row'] = (string) $atts['fields_per_row']; |
| 104 |
} |
| 105 |
return Mlsimport_Standalone_Render::render_grid( $args ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Normalize raw shortcode/block attributes into render args. Comma lists |
| 110 |
* (status, property_type, features) become arrays. |
| 111 |
* |
| 112 |
* @param array|string $atts Raw attributes. |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
public static function atts_to_args( $atts ): array { |
| 116 |
// Whitelist to the known filter keys (unknown atts are dropped, defaults ''). |
| 117 |
$atts = shortcode_atts( array_fill_keys( self::FILTER_ATTS, '' ), (array) $atts, 'mlsimport_listings' ); |
| 118 |
|
| 119 |
// Keys whose value is a set of choices rather than a single scalar. |
| 120 |
$multi = array( 'status', 'property_type', 'listing_type', 'features', 'area', 'county', 'high_school_district', 'label', 'city', 'state', 'zip' ); |
| 121 |
|
| 122 |
$args = array(); |
| 123 |
foreach ( $atts as $key => $value ) { |
| 124 |
// Skip unset filters so they don't narrow the query. |
| 125 |
if ( '' === $value || array() === $value ) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
if ( in_array( $key, $multi, true ) ) { |
| 129 |
// A multi-select submits an array (name="key[]"); a shortcode/legacy |
| 130 |
// attribute submits a comma list. Both normalize to a value array. |
| 131 |
$values = is_array( $value ) ? $value : explode( ',', (string) $value ); |
| 132 |
// Trim, stringify and drop empties so stray commas don't add blank terms. |
| 133 |
$values = array_values( array_filter( array_map( 'trim', array_map( 'strval', $values ) ), 'strlen' ) ); |
| 134 |
$args[ $key ] = $values; |
| 135 |
} elseif ( ! is_array( $value ) ) { |
| 136 |
// Single-value filter: pass the scalar straight through. |
| 137 |
$args[ $key ] = $value; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $args; |
| 142 |
} |
| 143 |
} |
| 144 |
|