PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 7.1.1
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v7.1.1
7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 6.0.5 All 36 releases
mlsimport / includes / standalone / class-mlsimport-page-block-selection.php

class-mlsimport-page-block-selection.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 7.1.1, at includes/standalone/class-mlsimport-page-block-selection.php

112 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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