| 1 |
<?php |
| 2 |
/** |
| 3 |
* Standalone (theme_id 990) page-block listing selection resolver. |
| 4 |
* |
| 5 |
* Encodes the one selection model shared by every "set" page block (item list, |
| 6 |
* slider, map, toolbar, list-by-id, agent listings): given the block's args, |
| 7 |
* decide whether to render an explicit, ordered set of property IDs or to run the |
| 8 |
* listings query (taxonomy + sort + count). Pure: no WordPress, no DB — the |
| 9 |
* returned params feed Mlsimport_Standalone_Query unchanged. |
| 10 |
* |
| 11 |
* @package Mlsimport |
| 12 |
*/ |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Resolves a page block's selection args into a query- or ids-mode instruction. |
| 20 |
*/ |
| 21 |
class Mlsimport_Page_Block_Selection { |
| 22 |
|
| 23 |
/** |
| 24 |
* Resolve selection args into a discriminated render instruction. |
| 25 |
* |
| 26 |
* @param array $args Page block selection args. |
| 27 |
* @return array{mode:string,params?:array,ids?:int[]} 'query' with params, or 'ids' with ids. |
| 28 |
*/ |
| 29 |
public static function resolve( array $args ): array { |
| 30 |
$ids = self::parse_ids( isset( $args['ids'] ) ? $args['ids'] : '' ); |
| 31 |
if ( $ids ) { |
| 32 |
return array( |
| 33 |
'mode' => 'ids', |
| 34 |
'ids' => $ids, |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
return array( |
| 39 |
'mode' => 'query', |
| 40 |
'params' => self::query_params( $args ), |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Map selection args to the Mlsimport_Standalone_Query params shape. |
| 46 |
* |
| 47 |
* @param array $args Page block selection args. |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
private static function query_params( array $args ): array { |
| 51 |
$params = array(); |
| 52 |
|
| 53 |
if ( isset( $args['count'] ) && (int) $args['count'] > 0 ) { |
| 54 |
$params['limit'] = (int) $args['count']; |
| 55 |
} |
| 56 |
|
| 57 |
// The sort token is forwarded as-is; Mlsimport_Standalone_Query owns the |
| 58 |
// token => (column, direction) vocabulary and expands it when ordering. An |
| 59 |
// empty/unknown token leaves orderby unset, so the site default order applies. |
| 60 |
if ( isset( $args['sort'] ) && '' !== $args['sort'] ) { |
| 61 |
$params['orderby'] = (string) $args['sort']; |
| 62 |
} |
| 63 |
|
| 64 |
// Only known taxonomy/filter keys forward to the query; builder-only keys |
| 65 |
// (layout, design, …) are ignored so they never reach the SQL. |
| 66 |
foreach ( self::TAXONOMY_KEYS as $key ) { |
| 67 |
if ( isset( $args[ $key ] ) && array() !== $args[ $key ] && '' !== $args[ $key ] ) { |
| 68 |
$params[ $key ] = $args[ $key ]; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $params; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Taxonomy-backed selection keys a page block may forward to the query, in a |
| 77 |
* deterministic order. Each is consumed by Mlsimport_Standalone_Query. |
| 78 |
*/ |
| 79 |
private const TAXONOMY_KEYS = array( |
| 80 |
'city', |
| 81 |
'state', |
| 82 |
'zip', |
| 83 |
'property_type', |
| 84 |
'listing_type', |
| 85 |
'status', |
| 86 |
'features', |
| 87 |
); |
| 88 |
|
| 89 |
/** |
| 90 |
* Parse a comma/newline/space list (or array) of property IDs into an ordered |
| 91 |
* int list, preserving the given order and dropping non-positive/blank entries. |
| 92 |
* |
| 93 |
* @param mixed $raw Delimited string (comma, newline or space) or array of IDs. |
| 94 |
* @return int[] |
| 95 |
*/ |
| 96 |
private static function parse_ids( $raw ): array { |
| 97 |
// Accept either an array or a delimited string. The textarea control invites |
| 98 |
// one ID per line, so split on any run of commas/whitespace, not commas alone. |
| 99 |
$values = is_array( $raw ) ? $raw : preg_split( '/[\s,]+/', (string) $raw, -1, PREG_SPLIT_NO_EMPTY ); |
| 100 |
|
| 101 |
$ids = array(); |
| 102 |
// Cast each entry, keeping only positive IDs and preserving the given order. |
| 103 |
foreach ( $values as $value ) { |
| 104 |
$id = (int) trim( (string) $value ); |
| 105 |
if ( $id > 0 ) { |
| 106 |
$ids[] = $id; |
| 107 |
} |
| 108 |
} |
| 109 |
return $ids; |
| 110 |
} |
| 111 |
} |
| 112 |
|