1: <?php
2: /**
3: * Search Object
4: *
5: * @package EPL
6: * @subpackage Classes/Search
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_Search_Fields Class
19: *
20: * @since 3.0
21: * @author Taher Atashbar <taher.atashbar@gmail.com>
22: */
23: class EPL_Search_Fields {
24:
25: /**
26: * Initialize hooks.
27: *
28: * @since 3.0
29: *
30: * @return void
31: */
32: public function init() {
33: // Initialize hooks for displaying search frontend fields.
34: add_action( 'epl_frontend_search_field_text', array( $this, 'render_text' ), 10, 5 );
35: add_action( 'epl_frontend_search_field_checkbox', array( $this, 'render_checkbox' ), 10, 5 );
36: add_action( 'epl_frontend_search_field_select', array( $this, 'render_select' ), 10, 5 );
37: add_action( 'epl_frontend_search_field_multiple_select', array( $this, 'render_multiple_select' ), 10, 5 );
38: add_action( 'epl_frontend_search_field_number', array( $this, 'render_number' ), 10, 5 );
39: add_action( 'epl_frontend_search_field_hidden', array( $this, 'render_hidden' ), 10, 5 );
40: add_action( 'epl_frontend_search_field_radio', array( $this, 'render_radio' ), 10, 5 );
41: add_action( 'epl_frontend_search_field_checkbox_multiple', array( $this, 'render_checkbox_multiple' ), 10, 5 );
42: }
43:
44: /**
45: * Renders search frontend Text field.
46: *
47: * @param array $field The field.
48: * @param string $config Configuration.
49: * @param string $value Value.
50: * @param string $post_type Post type name.
51: * @param string $property_status Listing status.
52: *
53: * @return void
54: *
55: * @since 3.0
56: * @since 3.5.10 Icon support added.
57: */
58: public function render_text( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
59: if ( isset( $field['wrap_start'] ) ) {
60: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
61: }
62: $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
63: ?>
64: <div class="epl-search-row epl-search-row-text epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
65: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
66: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
67: </label>
68: <div class="field">
69: <?php if ( ! empty( $field['icon'] ) ) : ?>
70: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
71: <?php endif; ?>
72: <input
73: placeholder="<?php echo esc_attr( $placeholder ); ?>"
74: type="text"
75: class="in-field field-width <?php echo esc_attr( $field['meta_key'] ); ?>"
76: name="<?php echo esc_attr( $field['meta_key'] ); ?>"
77: id="<?php echo esc_attr( $field['meta_key'] ); ?>"
78: value="<?php echo esc_attr( $value ); ?>"
79: />
80: </div>
81: </div>
82: <?php
83: if ( isset( $field['wrap_end'] ) ) {
84: echo '</div>';
85: }
86: }
87:
88: /**
89: * Renders search frontend Checkbox field.
90: *
91: * @param array $field The field.
92: * @param string $config Configuration.
93: * @param string $value Value.
94: * @param string $post_type Post type name.
95: * @param string $property_status Listing status.
96: *
97: * @return void
98: *
99: * @since 3.0
100: * @since 3.5.10 Icon support added.
101: */
102: public function render_checkbox( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
103: if ( isset( $field['wrap_start'] ) ) {
104: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
105: }
106: ?>
107: <?php if ( ! empty( $field['icon'] ) ) : ?>
108: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
109: <?php endif; ?>
110: <span class="epl-search-row epl-search-row-checkbox <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
111: <input type="checkbox" name="<?php echo esc_attr( $field['meta_key'] ); ?>" id="<?php echo esc_attr( $field['meta_key'] ); ?>" class="in-field <?php echo esc_attr( $field['meta_key'] ); ?>"
112: <?php
113: if ( isset( $value ) && ! empty( $value ) ) {
114: echo 'checked="checked"'; }
115: ?>
116: />
117: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="check-label">
118: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
119: </label>
120: </span>
121: <?php
122: if ( isset( $field['wrap_end'] ) ) {
123: echo '</div>';
124: }
125: }
126:
127: /**
128: * Renders search frontend Select field.
129: *
130: * @param array $field The field.
131: * @param string $config Configuration.
132: * @param string $value Value.
133: * @param string $post_type Post type name.
134: * @param string $property_status Listing status.
135: *
136: * @return void
137: *
138: * @since 3.0
139: * @since 3.5.10 Icon support added.
140: * @since 3.5.26 Escape data-min and data-max attributes at output.
141: */
142: public function render_select( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
143:
144: if ( isset( $field['wrap_start'] ) ) {
145: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
146: }
147:
148: $has_min_max = false;
149:
150: if ( isset( $field['query']['type'] ) && 'numeric' === $field['query']['type'] && ! empty( $field['options'] ) && is_array( $field['options'] ) ) {
151: reset( $field['options'] );
152: $opt_min = key( $field['options'] );
153: end( $field['options'] );
154: $opt_max = key( $field['options'] );
155: $has_min_max = true;
156: }
157:
158: ?>
159: <div class="epl-search-row epl-search-row-select epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
160: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
161: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
162: </label>
163: <div class="field">
164: <?php if ( ! empty( $field['icon'] ) ) : ?>
165: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
166: <?php endif; ?>
167: <select
168: name="<?php echo esc_attr( $field['meta_key'] ); ?>"
169: id="<?php echo esc_attr( $field['meta_key'] ); ?>"
170: <?php if ( $has_min_max ) : ?>
171: data-min="<?php echo esc_attr( $opt_min ); ?>"
172: data-max="<?php echo esc_attr( $opt_max ); ?>"
173: <?php endif; ?>
174: class="in-field field-width <?php echo esc_attr( $field['meta_key'] ); ?>">
175: <option value="">
176: <?php echo esc_attr( apply_filters( 'epl_search_widget_option_label_' . $field['option_filter'], __( 'Any', 'easy-property-listings' ) ) ); ?>
177: </option>
178: <?php
179: if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) {
180: foreach ( $field['options'] as $k => $v ) {
181: echo '<option value="' . esc_attr( $k ) . '"' . selected( $k, $value, false ) . '>' . esc_attr( $v ) . '</option>';
182: }
183: }
184: ?>
185: </select>
186: </div>
187: </div>
188: <?php
189: if ( isset( $field['wrap_end'] ) ) {
190: echo '</div>';
191: }
192: }
193:
194: /**
195: * Renders search frontend Multiple Select field.
196: *
197: * @param array $field The field.
198: * @param string $config Configuration.
199: * @param string $value Value.
200: * @param string $post_type Post type name.
201: * @param string $property_status Listing status.
202: *
203: * @return void
204: *
205: * @since 3.0
206: * @since 3.4.36 Fix: Corrected issue with multi select where values are numbers.
207: * @since 3.4.48 Tweak: Accessibility label added for select field.
208: * @since 3.5.10 Icon support added.
209: */
210: public function render_multiple_select( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
211: if ( isset( $field['wrap_start'] ) ) {
212: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
213: }
214: $field_label = apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] );
215: ?>
216: <div class="epl-search-row epl-search-row-select epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
217: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
218: <?php echo esc_html( $field_label ); ?>
219: </label>
220: <div class="field">
221: <?php if ( ! empty( $field['icon'] ) ) : ?>
222: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
223: <?php endif; ?>
224: <select aria-label="<?php echo esc_attr( $field_label ); ?>" name="<?php echo esc_attr( $field['meta_key'] ); ?>[]"
225: id="<?php echo esc_attr( $field['meta_key'] ); ?>"
226: class="in-field field-width field-multiple <?php echo esc_attr( $field['meta_key'] ); ?>" multiple>
227: <option value="">
228: <?php echo esc_attr( apply_filters( 'epl_search_widget_option_label_' . $field['option_filter'], __( 'Any', 'easy-property-listings' ) ) ); ?>
229: </option>
230: <?php
231: if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) {
232: foreach ( $field['options'] as $k => $v ) {
233: $selected = in_array( $k, (array) $value, true ) ? true : false;
234: echo '<option value="' . esc_attr( $k ) . '"' . selected( $selected, true, false ) . '>' . esc_attr( $v ) . '</option>';
235: }
236: }
237: ?>
238: </select>
239: </div>
240: </div>
241: <?php
242: if ( isset( $field['wrap_end'] ) ) {
243: echo '</div>';
244: }
245: }
246:
247: /**
248: * Renders search frontend Number field.
249: *
250: * @param array $field The field.
251: * @param string $config Configuration.
252: * @param string $value Value.
253: * @param string $post_type Post type name.
254: * @param string $property_status Listing status.
255: *
256: * @return void
257: *
258: * @since 3.0
259: * @since 3.5.10 Icon support added.
260: */
261: public function render_number( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
262: if ( isset( $field['wrap_start'] ) ) {
263: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
264: }
265: $placeholder = isset( $field['placeholder'] ) ? $field['placeholder'] : '';
266: ?>
267: <div class="epl-search-row epl-search-row-number epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
268: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
269: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
270: </label>
271: <div class="field">
272: <?php if ( ! empty( $field['icon'] ) ) : ?>
273: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
274: <?php endif; ?>
275: <input type="number" class="in-field field-width <?php echo esc_attr( $field['meta_key'] ); ?>"
276: placeholder="<?php echo esc_attr( $placeholder ); ?>"
277: name="<?php echo esc_attr( $field['meta_key'] ); ?>"
278: id="<?php echo esc_attr( $field['meta_key'] ); ?>"
279: value="<?php echo esc_attr( $value ); ?>"
280: />
281: </div>
282: </div>
283: <?php
284: if ( isset( $field['wrap_end'] ) ) {
285: echo '</div>';
286: }
287: }
288:
289: /**
290: * Renders search frontend Hidden field.
291: *
292: * @param array $field The field.
293: * @param string $config Configuration.
294: * @param string $value Value.
295: * @param string $post_type Post type name.
296: * @param string $property_status Listing status.
297: *
298: * @return void
299: *
300: * @since 3.0
301: */
302: public function render_hidden( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
303: if ( isset( $field['wrap_start'] ) ) {
304: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
305: }
306: ?>
307: <input type="hidden" class="in-field field-width <?php echo esc_attr( $field['meta_key'] ); ?>"
308: name="<?php echo esc_attr( $field['meta_key'] ); ?>"
309: id="<?php echo esc_attr( $field['meta_key'] ); ?>"
310: value="<?php echo esc_attr( $value ); ?>"
311: />
312: <?php
313: if ( isset( $field['wrap_end'] ) ) {
314: echo '</div>';
315: }
316: }
317:
318: /**
319: * Renders search frontend Radio field.
320: *
321: * @param array $field The field.
322: * @param string $config Configuration.
323: * @param string $value Value.
324: * @param string $post_type Post type name.
325: * @param string $property_status Listing status.
326: *
327: * @return void
328: *
329: * @since 3.0
330: * @since 3.5.10 Icon support added.
331: */
332: public function render_radio( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
333: if ( isset( $field['wrap_start'] ) ) {
334: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
335: }
336: ?>
337: <div class="epl-search-row epl-search-row-radio epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
338: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
339: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
340: </label>
341: <div class="field">
342: <?php if ( ! empty( $field['icon'] ) ) : ?>
343: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
344: <?php endif; ?>
345: <?php
346: if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) {
347: foreach ( $field['options'] as $k => $v ) {
348: ?>
349: <input
350: type="radio"
351: <?php checked( $k, $value, true ); ?>
352: name="<?php echo esc_attr( $field['meta_key'] ); ?>"
353: id="<?php echo esc_attr( $field['meta_key'] ) . '_' . esc_attr( $k ); ?>"
354: value="<?php echo esc_attr( $k ); ?>"
355: class="in-field field-width <?php echo esc_attr( $field['meta_key'] ) . '_' . esc_attr( $k ); ?>" />
356: <label class="epl-search-radio-label"><?php echo esc_attr( $v ); ?></label>
357: <?php
358: }
359: }
360: ?>
361:
362: </div>
363: </div>
364: <?php
365: if ( isset( $field['wrap_end'] ) ) {
366: echo '</div>';
367: }
368: }
369:
370: /**
371: * Renders search frontend Radio field.
372: *
373: * @param array $field The field.
374: * @param string $config Configuration.
375: * @param string $value Value.
376: * @param string $post_type Post type name.
377: * @param string $property_status Listing status.
378: *
379: * @return void
380: *
381: * @since 3.0.0
382: * @since 3.4.22 For attribute added for checkbox labels.
383: * @since 3.5.10 Icon support added.
384: */
385: public function render_checkbox_multiple( array $field, $config = '', $value = '', $post_type = '', $property_status = '' ) {
386: if ( isset( $field['wrap_start'] ) ) {
387: echo '<div class="' . esc_attr( $field['wrap_start'] ) . '">';
388: }
389: ?>
390: <div class="epl-search-row epl-search-row-checkbox-multiple epl-<?php echo esc_attr( $field['meta_key'] ); ?> fm-block <?php echo isset( $field['class'] ) ? esc_attr( $field['class'] ) : ''; ?>">
391: <label for="<?php echo esc_attr( $field['meta_key'] ); ?>" class="epl-search-label fm-label">
392: <?php echo esc_attr( apply_filters( 'epl_search_widget_label_' . $field['meta_key'], $field['label'] ) ); ?>
393: </label>
394: <div class="field">
395: <?php if ( ! empty( $field['icon'] ) ) : ?>
396: <i class="epl-icon <?php echo esc_attr( $field['icon'] ); ?>"></i>
397: <?php endif; ?>
398: <?php
399: if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) {
400: foreach ( $field['options'] as $k => $v ) {
401: $checked = in_array( $k, (array) $value, true ) ? 'checked' : '';
402: ?>
403: <input
404: type="checkbox"
405: <?php echo ' ' . esc_attr( $checked ) . ' '; ?>
406: name="<?php echo esc_attr( $field['meta_key'] ); ?>[]"
407: id="<?php echo esc_attr( $field['meta_key'] ) . '_' . esc_attr( $k ); ?>"
408: value="<?php echo esc_attr( $k ); ?>"
409: class="in-field field-width <?php echo esc_attr( $field['meta_key'] ) . '_' . esc_attr( $k ); ?>" />
410: <label for="<?php echo esc_attr( $field['meta_key'] ) . '_' . esc_attr( $k ); ?>" class="epl-search-checkbox-label"><?php echo esc_attr( $v ); ?></label>
411: <?php
412: }
413: }
414: ?>
415: </div>
416: </div>
417: <?php
418: if ( isset( $field['wrap_end'] ) ) {
419: echo '</div>';
420: }
421: }
422: }
423: