1: <?php
2: /**
3: * Search Object
4: *
5: * @package EPL
6: * @subpackage Classes/Search
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.1
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_SEARCH Class
19: *
20: * @since 3.1
21: */
22: class EPL_SEARCH {
23:
24: /**
25: * WP_QUERY Object
26: *
27: * @var object
28: */
29: private $query;
30:
31: /**
32: * Search data received from search widget / seach shortcode via GET method
33: *
34: * @var array
35: */
36: private $get_data = array();
37:
38: /**
39: * Search data received from search widget / seach shortcode via POST method
40: *
41: * @var array
42: */
43: private $post_data = array();
44:
45: /**
46: * Meta query array
47: *
48: * @var array
49: */
50: private $meta_query = array();
51:
52: /**
53: * Tax query array
54: *
55: * @var array
56: */
57: private $tax_query = array();
58:
59: /**
60: * Fields to be skipped
61: *
62: * @var array
63: */
64: private $skip_field = array();
65:
66: /**
67: * Post type to search for
68: *
69: * @var string
70: */
71: private $post_type;
72:
73: /**
74: * Transaction type for current post type sale/lease
75: *
76: * @var string
77: */
78: private $transaction_type = null;
79:
80: /**
81: * Holds the submitted form data.
82: *
83: * @var array $data form data.
84: */
85: public $data;
86:
87: /**
88: * Holds skipped fields.
89: *
90: * @var array $skip_fields Skipped fields.
91: */
92: public $skip_fields;
93:
94: /**
95: * Holds Array of form fields.
96: *
97: * @var array $form_fields form fields.
98: */
99: public $form_fields;
100:
101:
102: /**
103: * Get things going
104: *
105: * @since 3.2
106: * @since 3.5.13 Setting page name to null on search pages.
107: *
108: * @param WP_Query $query object of the wp_query object.
109: * @param array $data associative array for fields to search.
110: */
111: public function __construct( $query, $data ) {
112:
113: $this->data = $data;
114:
115: $this->query = $query;
116:
117: $this->sanitize_data();
118:
119: $this->skip_unnecessary_fields();
120:
121: $this->query = apply_filters( 'epl_search_query_before_processing', $this->query, $this->get_data );
122:
123: $this->set_per_page();
124:
125: $this->set_pagination();
126:
127: $this->set_title();
128:
129: $this->set_property_agent();
130:
131: $this->set_post_type();
132:
133: $this->modify_fields();
134:
135: $this->prepare_query();
136:
137: $this->epl_search_query_pre_search();
138:
139: $this->query = apply_filters( 'epl_search_query_pre_search', $this->query, $this->get_data );
140:
141: $this->set_query();
142:
143: $this->query = apply_filters( 'epl_search_query_pre_parse', $this->query, $this->get_data );
144:
145: $this->query->parse_query();
146:
147: // Disable is_* flags which can create conflict with EPL search query flag to avoid redirection to tax archive url.
148: $this->query->is_tax = false;
149: $this->query->is_page = false;
150: $this->query->is_single = false;
151: $this->query->is_singular = false;
152: $this->query->is_epl_search = true;
153: set_query_var( 'page_id', null );
154: set_query_var( 'pagename', null );
155: }
156:
157: /**
158: * These are list of fields which are not directly queried,
159: * but their value is used to query other meta data / post data / tax data
160: *
161: * @return array
162: */
163: protected function altered_fields() {
164: return apply_filters(
165: 'epl_search_altered_fields',
166: array(
167: 'property_unique_id',
168: )
169: );
170: }
171:
172: /**
173: * Sanitize an array of data recursively.
174: *
175: * @param array|string $data The data to be sanitized.
176: * @since 3.5.0
177: */
178: public function sanitize_recursive( $data ) {
179: if ( is_array( $data ) ) {
180: return array_map( array( $this, 'sanitize_recursive' ), $data );
181: } else {
182: return sanitize_text_field( $data );
183: }
184: }
185:
186: /**
187: * Sanitize whole GET & POST array, insures security from XSS
188: *
189: * @since 3.5.0 replaced depricated code for sanitization.
190: */
191: protected function sanitize_data() {
192:
193: $this->get_data = $this->sanitize_recursive( $_GET );
194: if ( ! empty( $this->data ) ) {
195: $this->get_data = $this->data;
196: }
197: $this->get_data = apply_filters( 'epl_search_get_data', $this->get_data );
198: $this->post_data = $this->sanitize_recursive( $_POST );
199: $this->post_data = apply_filters( 'epl_search_post_data', $this->post_data );
200:
201: if ( ! isset( $this->get_data['property_status'] ) ) {
202: $this->get_data['property_status'] = '';
203: }
204: }
205:
206: /**
207: * Set list of fields which are not to be queried
208: */
209: protected function fields_to_skip() {
210: // Keys of fields to be skipped.
211: $fields = array( 'action' );
212: $this->skip_fields = apply_filters( 'epl_skip_search_fields', $fields, $this->get_data );
213: }
214:
215: /**
216: * Skip unnecessay fields
217: */
218: protected function skip_unnecessary_fields() {
219: $this->fields_to_skip();
220: if ( ! empty( $this->skip_fields ) ) {
221: foreach ( $this->skip_fields as $key => &$field ) {
222: if ( isset( $this->get_data[ $key ] ) ) {
223: unset( $this->get_data[ $key ] );
224: }
225: }
226: }
227: }
228:
229: /**
230: * Set per page for query
231: */
232: protected function set_per_page() {
233: $posts_per_page = apply_filters( 'epl_search_results_per_page', get_option( 'posts_per_page' ) );
234: $this->query->set( 'posts_per_page', $posts_per_page );
235: }
236:
237: /**
238: * Set pagination for query
239: */
240: protected function set_pagination() {
241: $paged = $this->query->get( 'paged', 1 );
242: $this->query->set( 'paged', $paged );
243: }
244:
245: /**
246: * Set title for query
247: */
248: protected function set_title() {
249: if ( isset( $this->get_data['property_address'] ) ) {
250: $this->query->set( 'epl_post_title', sanitize_text_field( $this->get_data['property_address'] ) );
251: }
252: }
253:
254: /**
255: * Set property agent for query
256: */
257: protected function set_property_agent() {
258: if ( isset( $this->get_data['property_agent'] ) ) {
259: $property_agent = sanitize_title_with_dashes( $this->get_data['property_agent'] );
260: $property_agent = get_user_by( 'slug', $property_agent );
261: if ( $property_agent ) {
262: $this->query->set( 'author__in', array( $property_agent->ID ) );
263: }
264: }
265: }
266:
267: /**
268: * Set post type for query
269: *
270: * @since 3.4.12 Added support for 'all' post type.
271: * @since 3.6 Fix: only searches post types the site shows to visitors. A listing type that's switched off no longer gives PHP warnings (it finds nothing), and private ones like contacts can't be searched from the URL.
272: */
273: protected function set_post_type() {
274: if ( isset( $this->get_data['post_type'] ) && ! empty( $this->get_data['post_type'] ) && 'all' !== $this->get_data['post_type'] ) {
275: $post_type = $this->get_data['post_type'];
276: $allowed = array_values( array_filter( (array) $post_type, 'epl_search_can_show_post_type' ) );
277:
278: if ( empty( $allowed ) ) {
279: // Nothing the visitor can see: search the active listing types, find nothing.
280: $allowed = array_keys( epl_get_active_post_types() );
281: $this->query->set( 'post__in', array( 0 ) );
282: } elseif ( ! is_array( $post_type ) ) {
283: $allowed = $allowed[0];
284: }
285:
286: $this->query->set( 'post_type', $allowed );
287: $this->post_type = $allowed;
288: $this->get_data['post_type'] = $allowed;
289: } else {
290: $epl_post_types = epl_get_active_post_types();
291: if ( ! empty( $epl_post_types ) ) {
292: $epl_post_types = array_keys( $epl_post_types );
293: $this->query->set( 'post_type', $epl_post_types );
294: $this->post_type = $epl_post_types;
295: $this->get_data['post_type'] = $this->post_type;
296: }
297: }
298: }
299:
300: /**
301: * Modify meta keys according to data received, for special cases
302: */
303: protected function modify_fields() {
304: // if post type is commercial and listing type is set, accordingly change the price meta key.
305: if ( ( 'commercial' === $this->post_type || 'commercial_land' === $this->post_type ) && isset( $this->get_data['property_com_listing_type'] ) ) {
306:
307: $type = $this->get_data['property_com_listing_type'];
308: $this->transaction_type = 'lease' === $type ? 'lease' : 'sale';
309:
310: }
311: }
312:
313: /**
314: * Set queries meta, Tax
315: */
316: protected function set_query() {
317: $this->set_meta_query();
318: $this->set_tax_query();
319: }
320:
321: /**
322: * Set meta query for the search object
323: */
324: protected function set_meta_query() {
325: $this->preprocess_meta_query();
326: if ( ! empty( $this->meta_query ) ) {
327: $this->query->set( 'meta_query', $this->meta_query );
328: }
329: }
330:
331: /**
332: * Set tax query for the search object
333: */
334: protected function set_tax_query() {
335: if ( ! empty( $this->tax_query ) ) {
336: $this->query->set( 'tax_query', $this->tax_query );
337: }
338: }
339:
340: /**
341: * The requested listing statuses, keeping only real ones (Current, Sold, Leased...).
342: *
343: * @since 3.6 New: used for the search form fields, whose queries are cached.
344: *
345: * @return string Comma separated statuses, or ''.
346: */
347: protected function get_field_status() {
348: return epl_search_known_statuses( $this->get_data['property_status'] );
349: }
350:
351: /**
352: * Prepare the query
353: *
354: * @since 3.6 Security: the form fields get only real listing statuses, so ?property_status=<anything> can't fill the listing query cache with junk entries. The search itself still uses the value as before.
355: */
356: protected function prepare_query() {
357:
358: $field_status = $this->get_field_status();
359:
360: if ( 'commercial' === $this->get_data['post_type'] || 'commercial_land' === $this->get_data['post_type'] ) {
361: if ( function_exists( 'epl_listing_search_commercial_widget_fields_frontend' ) ) {
362: $this->form_fields = epl_listing_search_commercial_widget_fields_frontend( $this->get_data['post_type'], $field_status, $this->transaction_type );
363:
364: } else {
365: $this->form_fields = epl_search_widget_fields_frontend( $this->get_data['post_type'], $field_status, $this->transaction_type );
366: }
367: } else {
368: $this->form_fields = epl_search_widget_fields_frontend( $this->get_data['post_type'], $field_status, $this->transaction_type );
369: }
370:
371: foreach ( $this->get_data as $key => $data ) {
372:
373: if ( isset( $this->form_fields[ $key ] ) ) {
374:
375: $query_type = isset( $this->form_fields[ $key ]['query']['query'] ) ?
376: $this->form_fields[ $key ]['query']['query'] : 'special';
377:
378: if ( in_array( $key, $this->altered_fields(), true ) ) {
379: return;
380: }
381:
382: switch ( $query_type ) {
383:
384: case 'meta':
385: $this->prepare_meta_query( $this->form_fields[ $key ], $data );
386:
387: break;
388:
389: case 'tax':
390: $this->prepare_tax_query( $this->form_fields[ $key ], $data );
391:
392: break;
393:
394: case 'special':
395: $this->prepare_special_query( $this->form_fields[ $key ], $data );
396:
397: break;
398: }
399: }
400: }
401: }
402:
403: /**
404: * Pre search query
405: */
406: public function epl_search_query_pre_search() {
407:
408: foreach ( $this->meta_query as $index => &$meta_query ) {
409:
410: if ( ! isset( $meta_query['key'] ) ) {
411: continue;
412: }
413:
414: if ( 'property_com_listing_type' === $meta_query['key'] ) {
415:
416: $meta_query['compare'] = 'IN';
417:
418: switch ( $meta_query['value'] ) {
419:
420: case 'sale':
421: $meta_query['value'] = array( 'sale', 'both' );
422: break;
423:
424: case 'lease':
425: $meta_query['value'] = array( 'lease', 'both' );
426: break;
427:
428: default:
429: $meta_query['value'] = array( 'lease', 'both', 'sale' );
430: break;
431:
432: }
433: }
434: }
435: }
436:
437: /**
438: * Prepare meta query
439: *
440: * @param array $query_field Field to query.
441: * @param string $value Value of field.
442: */
443: protected function prepare_meta_query( $query_field, $value ) {
444:
445: if ( $this->is_query_multiple( $query_field ) ) {
446:
447: $this->multiple_meta_query( $query_field, $value );
448:
449: } else {
450:
451: $this->single_meta_query( $query_field, $value );
452: }
453: }
454:
455: /**
456: * Checks if given meta query is multiple
457: *
458: * @param array $query_field Field to query.
459: * @return boolean
460: */
461: protected function is_query_multiple( $query_field ) {
462:
463: if ( isset( $query_field['query']['multiple'] ) && true === $query_field['query']['multiple'] ) {
464: return true;
465: }
466: return false;
467: }
468:
469: /**
470: * Returns relationship for the multiple query
471: *
472: * @param array $query_field Field to query.
473: * @return string
474: */
475: protected function multiple_relation( $query_field ) {
476:
477: return isset( $query_field['query']['relation'] ) ? $query_field['query']['relation'] : 'OR';
478: }
479:
480: /**
481: * Set multiple meta query
482: *
483: * @param array $query_field Field to query.
484: * @param string $data Data value.
485: */
486: protected function multiple_meta_query( $query_field, $data ) {
487:
488: if ( empty( $data ) ) {
489: return;
490: }
491:
492: $this_meta_query = array();
493:
494: if ( isset( $query_field['meta_key'] ) && ! empty( $query_field['meta_key'] ) ) {
495:
496: $this_meta_query['relation'] = $this->multiple_relation( $query_field );
497:
498: if ( ! empty( $query_field['query']['sub_queries'] ) ) {
499:
500: foreach ( $query_field['query']['sub_queries'] as $sub_query ) {
501:
502: $this_sub_query = array(
503: 'key' => $sub_query['key'],
504: 'value' => $data,
505: 'type' => $sub_query['type'],
506: 'compare' => $sub_query['compare'],
507: );
508: $this_meta_query[] = $this_sub_query;
509: }
510:
511: $this->meta_query[] = $this_meta_query;
512: }
513: }
514: }
515:
516: /**
517: * Set single meta query
518: *
519: * @param array $query_field Field to query.
520: * @param string $data Data value.
521: */
522: protected function single_meta_query( $query_field, $data ) {
523:
524: $query_meta_key = isset( $query_field['query']['key'] ) ? $query_field['query']['key'] : $query_field['meta_key'];
525:
526: if ( isset( $data ) && ! empty( $data ) ) {
527:
528: $this_meta_query = array(
529: 'key' => $query_meta_key,
530: 'value' => $data,
531: );
532:
533: isset( $query_field['query']['compare'] ) ?
534: $this_meta_query['compare'] = $query_field['query']['compare'] : '';
535:
536: isset( $query_field['query']['type'] ) ?
537: $this_meta_query['type'] = $query_field['query']['type'] : '';
538:
539: isset( $query_field['query']['value'] ) ?
540: $this_meta_query['value'] = $query_field['query']['value'] : '';
541:
542: $this->meta_query[] = $this_meta_query;
543: }
544: }
545:
546: /**
547: * Preprocess meta query
548: *
549: * @since 3.1.0
550: * @since 3.4.30 Fix: Only variables can be passed via reference.
551: */
552: protected function preprocess_meta_query() {
553:
554: $range_sep = apply_filters( 'epl_search_field_range_separator', '-' );
555: $option_sep = apply_filters( 'epl_search_field_option_separator', ',' );
556:
557: foreach ( $this->meta_query as $key => &$query ) {
558:
559: if ( isset( $query['compare'] ) && isset( $query['value'] )
560: && in_array( strtoupper( $query['compare'] ), array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true )
561: && ! is_array( $query['value'] ) ) {
562: $query['value'] = array_map( 'trim', explode( $option_sep, $query['value'] ) );
563:
564: if ( isset( $this->form_fields[ $query['key'] ]['option_type'] ) && 'range' === $this->form_fields[ $query['key'] ]['option_type'] ) {
565: $current = explode( $range_sep, current( $query['value'] ) );
566: $next = explode( $range_sep, end( $query['value'] ) );
567: $query['value'] = array(
568: current( $current ),
569: next( $next ),
570: );
571: }
572: }
573: }
574: $this->meta_query = apply_filters( 'epl_preprocess_search_meta_query', $this->meta_query );
575: }
576:
577: /**
578: * Prepare taxonomy query
579: *
580: * @param array $query_field Field to query.
581: * @param string $value Data value.
582: */
583: protected function prepare_tax_query( $query_field, $value ) {
584:
585: $value = (array) $value;
586: $value = array_filter( $value );
587: if ( ! empty( $value ) ) {
588: $this->tax_query[] = array(
589: 'taxonomy' => preg_replace( '/^property_/', '', $query_field['meta_key'] ),
590: 'field' => 'id',
591: 'terms' => $value,
592: );
593: }
594:
595: $this->tax_query = apply_filters( 'epl_preprocess_search_tax_query', $this->tax_query );
596: }
597:
598: /**
599: * Prepare special query
600: *
601: * @param array $query_field Field to query.
602: * @param string $value Data value.
603: */
604: protected function prepare_special_query( $query_field, $value ) {
605:
606: $this->query = apply_filters( 'epl_search_special_query', $this->query, $query_field, $value );
607: }
608:
609: /**
610: * Return array of posts based on search data
611: *
612: * @return array
613: */
614: public function get_posts() {
615:
616: return $this->query->get_posts();
617: }
618: }
619: