PluginProbe
Property Hive / 1.4.6
Property Hive v1.4.6
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / class-ph-query.php

class-ph-query.php in Property Hive 1.4.6, at includes/class-ph-query.php

1,394 lines 40.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Contains the query functions for PropertyHive which alter the front-end post queries and loops.
4 *
5 * @class PH_Query
6 * @version 1.0.0
7 * @package PropertyHive/Classes
8 * @category Class
9 * @author PropertyHive
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
13
14 if ( ! class_exists( 'PH_Query' ) ) :
15
16 /**
17 * PH_Query Class
18 */
19 class PH_Query {
20
21 /** @public array Query vars to add to wp */
22 public $query_vars = array();
23
24 /** @public array Unfiltered property ids (before layered nav etc) */
25 public $unfiltered_property_ids = array();
26
27 /** @public array Filtered property ids (after layered nav) */
28 public $filtered_property_ids = array();
29
30 /** @public array Filtered property ids (after layered nav, per taxonomy) */
31 public $filtered_property_ids_for_taxonomy = array();
32
33 /** @public array property IDs that match the layered nav + price filter */
34 public $post__in = array();
35
36 /** @public array The meta query for the page */
37 public $meta_query = '';
38
39 /** @public array Post IDs matching layered nav only */
40 public $layered_nav_post__in = array();
41
42 /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
43 public $layered_nav_property_ids = array();
44
45 /**
46 * Constructor for the query class. Hooks in methods.
47 *
48 * @access public
49 */
50 public function __construct() {
51
52 //add_action( 'init', array( $this, 'add_endpoints' ) );
53 add_action( 'init', array( $this, 'layered_nav_init' ) );
54 add_action( 'init', array( $this, 'price_filter_init' ) );
55
56 if ( ! is_admin() ) {
57 add_action( 'init', array( $this, 'get_errors' ) );
58 add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
59 add_action( 'parse_request', array( $this, 'parse_request'), 0 );
60 add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
61 add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
62 add_action( 'wp', array( $this, 'remove_property_query' ) );
63 add_action( 'wp', array( $this, 'remove_ordering_args' ) );
64 }
65
66 $this->init_query_vars();
67 }
68
69 /**
70 * Init query vars by loading options.
71 */
72 public function init_query_vars() {
73 // Query vars to add to WP
74 $this->query_vars = array(
75
76 );
77 }
78
79 /**
80 * Get any errors from querystring
81 */
82 public function get_errors() {
83 if ( ! empty( $_GET['ph_error'] ) && ( $error = sanitize_text_field( $_GET['ph_error'] ) ) && ! ph_has_notice( $error, 'error' ) )
84 ph_add_notice( $error, 'error' );
85 }
86
87 /**
88 * Add endpoints for query vars
89 */
90 public function add_endpoints() {
91 foreach ( $this->query_vars as $key => $var )
92 add_rewrite_endpoint( $var, EP_PAGES );
93 }
94
95 /**
96 * add_query_vars function.
97 *
98 * @access public
99 * @param array $vars
100 * @return array
101 */
102 public function add_query_vars( $vars ) {
103 foreach ( $this->query_vars as $key => $var )
104 $vars[] = $key;
105
106 return $vars;
107 }
108
109 /**
110 * Get query vars
111 * @return array()
112 */
113 public function get_query_vars() {
114 return $this->query_vars;
115 }
116
117 /**
118 * Parse the request and look for query vars - endpoints may not be supported
119 */
120 public function parse_request() {
121 global $wp;
122
123 // Map query vars to their keys, or get them if endpoints are not supported
124 foreach ( $this->query_vars as $key => $var ) {
125 if ( isset( $_GET[ $var ] ) ) {
126 $wp->query_vars[ $key ] = $_GET[ $var ];
127 }
128
129 elseif ( isset( $wp->query_vars[ $var ] ) ) {
130 $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
131 }
132 }
133 }
134
135 /**
136 * Hook into pre_get_posts to do the main property query
137 *
138 * @access public
139 * @param mixed $q query object
140 * @return void
141 */
142 public function pre_get_posts( $q ) {
143 // We only want to affect the main query
144 if ( ! $q->is_main_query() )
145 return;
146
147 // Fix for verbose page rules
148 if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules && isset( $q->queried_object->ID ) && $q->queried_object->ID == ph_get_page_id( 'search_results' ) ) {
149 $q->set( 'post_type', 'property' );
150 $q->set( 'page', '' );
151 $q->set( 'pagename', '' );
152
153 // Fix conditional Functions
154 $q->is_archive = true;
155 $q->is_post_type_archive = true;
156 $q->is_singular = false;
157 $q->is_page = false;
158 }
159
160 // When orderby is set, WordPress shows posts. Get around that here.
161 /*if ( $q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == ph_get_page_id('search_results') ) {
162 $_query = wp_parse_args( $q->query );
163 if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
164 $q->is_page = true;
165 $q->is_home = false;
166 $q->set( 'page_id', get_option('page_on_front') );
167 $q->set( 'post_type', 'property' );
168 }
169 }*/
170
171 // Special check for sites with the property search results on front page
172 /*if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == ph_get_page_id('search_results') ) {
173
174 // This is a front-page property listings
175 $q->set( 'post_type', 'property' );
176 $q->set( 'page_id', '' );
177 if ( isset( $q->query['paged'] ) )
178 $q->set( 'paged', $q->query['paged'] );
179
180 // Define a variable so we know this is the front page shop later on
181 define( 'SEARCH_RESULTS_IS_ON_FRONT', true );
182
183 // Get the actual WP page to avoid errors and let us use is_front_page()
184 // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
185 global $wp_post_types;
186
187 $search_results_page = get_post( ph_get_page_id('search_results') );
188 $q->is_page = true;
189
190 $wp_post_types['property']->ID = $search_results_page->ID;
191 $wp_post_types['property']->post_title = $search_results_page->post_title;
192 $wp_post_types['property']->post_name = $search_results_page->post_name;
193 $wp_post_types['property']->post_type = $search_results_page->post_type;
194 $wp_post_types['property']->ancestors = get_ancestors( $search_results_page->ID, $search_results_page->post_type );
195
196 // Fix conditional Functions like is_front_page
197 $q->is_singular = false;
198 $q->is_post_type_archive = true;
199 $q->is_archive = true;
200
201 // Fix WP SEO
202 if ( class_exists( 'WPSEO_Meta' ) ) {
203 add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
204 add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
205 }
206
207 } else {*/
208
209 // Only apply to property categories, the property post archive, the search results page, and property attribute taxonomies
210 if ( ! $q->is_post_type_archive( 'property' ) && ! $q->is_tax( get_object_taxonomies( 'property' ) ) )
211 return;
212
213 //}
214
215 $this->property_query( $q );
216
217 if ( is_search() )
218 {
219 add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
220 add_filter( 'wp', array( $this, 'remove_posts_where' ) );
221 }
222
223 add_filter( 'posts_where', array( $this, 'exclude_protected_properties' ) );
224
225 // We're on a property search page so queue the propertyhive_get_properties_in_view function
226 add_action( 'wp', array( $this, 'get_properties_in_view' ), 2);
227
228 // And remove the pre_get_posts hook
229 $this->remove_property_query();
230 }
231
232 /**
233 * search_post_excerpt function.
234 *
235 * @access public
236 * @param string $where (default: '')
237 * @return string (modified where clause)
238 */
239 public function search_post_excerpt( $where = '' ) {
240 /*global $wp_the_query;
241
242 // If this is not a PH Query, do not modify the query
243 if ( empty( $wp_the_query->query_vars['ph_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
244 return $where;
245
246 $where = preg_replace(
247 "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
248 "post_title LIKE $1) OR (post_excerpt LIKE $1", $where );*/
249
250 return $where;
251 }
252
253 /**
254 * Prevent password protected properties appearing in the loops
255 *
256 * @param string $where
257 * @return string
258 */
259 public function exclude_protected_properties( $where ) {
260 global $wpdb;
261 $where .= " AND {$wpdb->posts}.post_password = ''";
262 return $where;
263 }
264
265 /**
266 * wpseo_metadesc function.
267 * Hooked into wpseo_ hook already, so no need for function_exist
268 *
269 * @access public
270 * @return string
271 */
272 public function wpseo_metadesc() {
273 return WPSEO_Meta::get_value( 'metadesc', ph_get_page_id('search_results') );
274 }
275
276
277 /**
278 * wpseo_metakey function.
279 * Hooked into wpseo_ hook already, so no need for function_exist
280 *
281 * @access public
282 * @return string
283 */
284 public function wpseo_metakey() {
285 return WPSEO_Meta::get_value( 'metakey', ph_get_page_id('search_results') );
286 }
287
288
289 /**
290 * Hook into the_posts to do the main property query if needed - relevanssi compatibility
291 *
292 * @access public
293 * @param array $posts
294 * @param WP_Query|bool $query (default: false)
295 * @return array
296 */
297 public function the_posts( $posts, $query = false ) {
298
299 // Abort if there's no query
300 if ( ! $query )
301 return $posts;
302
303 // Abort if we're not filtering posts
304 if ( empty( $this->post__in ) )
305 return $posts;
306
307 // Abort if this query has already been done
308 if ( ! empty( $query->ph_query ) )
309 return $posts;
310
311 // Abort if this isn't a search query
312 if ( empty( $query->query_vars["s"] ) )
313 return $posts;
314
315 // Abort if we're not on a post type archive/property taxonomy
316 if ( ! $query->is_post_type_archive( 'property' ) && ! $query->is_tax( get_object_taxonomies( 'property' ) ) )
317 return $posts;
318
319 $filtered_posts = array();
320 $queried_post_ids = array();
321
322 foreach ( $posts as $post ) {
323 if ( in_array( $post->ID, $this->post__in ) ) {
324 $filtered_posts[] = $post;
325 $queried_post_ids[] = $post->ID;
326 }
327 }
328
329 $query->posts = $filtered_posts;
330 $query->post_count = count( $filtered_posts );
331
332 // Ensure filters are set
333 $this->unfiltered_property_ids = $queried_post_ids;
334 $this->filtered_property_ids = $queried_post_ids;
335
336 if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
337 $this->layered_nav_property_ids = array_intersect( $this->unfiltered_property_ids, $this->layered_nav_post__in );
338 } else {
339 $this->layered_nav_property_ids = $this->unfiltered_property_ids;
340 }
341
342 return $filtered_posts;
343 }
344
345
346 /**
347 * Query the properties, applying sorting/ordering etc. This applies to the main wordpress loop
348 *
349 * @access public
350 * @param mixed $q
351 * @return void
352 */
353 public function property_query( $q ) {
354
355 // Meta query
356 $meta_query = $this->get_meta_query( $q );
357
358 // Tax query
359 $tax_query = $this->get_tax_query( $q->get( 'tax_query' ) );
360
361 // Date query
362 $date_query = $this->get_date_query();
363
364 // Ordering
365 $ordering = $this->get_search_results_ordering_args();
366
367 // Get a list of post id's which match the current filters set (in the layered nav and price filter)
368 $post__in = array();
369 //$post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
370
371 // Ordering query vars
372 $q->set( 'orderby', $ordering['orderby'] );
373 $q->set( 'order', $ordering['order'] );
374 if ( isset( $ordering['meta_key'] ) )
375 $q->set( 'meta_key', $ordering['meta_key'] );
376
377 // Query vars that affect posts shown
378 $q->set( 'meta_query', $meta_query );
379 $q->set( 'tax_query', $tax_query );
380 $q->set( 'date_query', $date_query );
381 $q->set( 'post__in', $post__in );
382 $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_search_results_per_page', get_option( 'posts_per_page' ) ) );
383
384 // Set a special variable
385 $q->set( 'ph_query', true );
386
387 // Store variables
388 $this->post__in = $post__in;
389 $this->meta_query = $meta_query;
390 $this->tax_query = $tax_query;
391
392 do_action( 'propertyhive_property_query', $q, $this );
393 }
394
395
396 /**
397 * Remove the query
398 *
399 * @access public
400 * @return void
401 */
402 public function remove_property_query() {
403 remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
404 }
405
406 /**
407 * Remove ordering queries
408 */
409 public function remove_ordering_args() {
410
411 }
412
413 /**
414 * Remove the posts_where filter
415 *
416 * @access public
417 * @return void
418 */
419 public function remove_posts_where() {
420 remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
421 }
422
423
424 /**
425 * Get an unpaginated list all property ID's (both filtered and unfiltered). Makes use of transients.
426 *
427 * @access public
428 * @return void
429 */
430 public function get_properties_in_view() {
431 global $wp_the_query;
432
433 $unfiltered_property_ids = array();
434
435 // Get main query
436 $current_wp_query = $wp_the_query->query;
437
438 // Get WP Query for current page (without 'paged')
439 unset( $current_wp_query['paged'] );
440
441 // Generate a transient name based on current query
442 $transient_name = 'ph_uf_pid_' . md5( http_build_query( $current_wp_query ) );
443 $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
444
445 if ( false === ( $unfiltered_property_ids = get_transient( $transient_name ) ) ) {
446
447 // Get all visible posts, regardless of filters
448 $unfiltered_property_ids = get_posts(
449 array_merge(
450 $current_wp_query,
451 array(
452 'post_type' => 'property',
453 'numberposts' => -1,
454 'post_status' => 'publish',
455 'meta_query' => $this->meta_query,
456 'fields' => 'ids',
457 'no_found_rows' => true,
458 'update_post_meta_cache' => false,
459 'update_post_term_cache' => false
460 )
461 )
462 );
463
464 set_transient( $transient_name, $unfiltered_property_ids, YEAR_IN_SECONDS );
465 }
466
467 // Store the variable
468 $this->unfiltered_property_ids = $unfiltered_property_ids;
469
470 // Also store filtered posts ids...
471 if ( sizeof( $this->post__in ) > 0 )
472 $this->filtered_property_ids = array_intersect( $this->unfiltered_property_ids, $this->post__in );
473 else
474 $this->filtered_property_ids = $this->unfiltered_property_ids;
475
476 // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
477 if ( sizeof( $this->layered_nav_post__in ) > 0 )
478 $this->layered_nav_property_ids = array_intersect( $this->unfiltered_property_ids, $this->layered_nav_post__in );
479 else
480 $this->layered_nav_property_ids = $this->unfiltered_property_ids;
481 }
482
483
484 /**
485 * Returns an array of arguments for ordering properties based on the selected values
486 *
487 * @access public
488 * @return array
489 */
490 public function get_search_results_ordering_args( $orderby = '', $order = '' ) {
491 // Get ordering from query string unless defined
492 if ( ! $orderby ) {
493 $orderby_value = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : apply_filters( 'propertyhive_default_search_results_orderby', get_option( 'propertyhive_default_search_results_orderby' ) );
494
495 // Get order + orderby args from string
496 $orderby_value = explode( '-', $orderby_value );
497 $orderby = esc_attr( $orderby_value[0] );
498 $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
499 }
500
501 $orderby = strtolower( $orderby );
502 $order = strtoupper( $order );
503
504 $args = array();
505
506 // default - menu_order
507 if (
508 ( isset($_REQUEST['department']) && $_REQUEST['department'] != 'commercial' ) ||
509 ( !isset($_REQUEST['department']) && get_option( 'propertyhive_primary_department' ) != 'commercial' )
510 )
511 {
512 $args['orderby'] = 'meta_value_num';
513 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
514 $args['meta_key'] = '_price_actual';
515 }
516 elseif (
517 ( isset($_REQUEST['department']) && $_REQUEST['department'] == 'commercial' ) ||
518 ( !isset($_REQUEST['department']) && get_option( 'propertyhive_primary_department' ) == 'commercial' )
519 )
520 {
521 $args['orderby'] = 'meta_value_num';
522 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
523 $args['meta_key'] = '_floor_area_from_sqft';
524 }
525
526 switch ( $orderby ) {
527 case 'rand' :
528 $args['orderby'] = 'rand';
529 break;
530 case 'date' :
531 $args['orderby'] = 'date';
532 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
533 break;
534 case 'price' :
535 $args['orderby'] = 'meta_value_num';
536 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
537 $args['meta_key'] = '_price_actual';
538 break;
539 case 'floor_area' :
540 $args['orderby'] = 'meta_value_num';
541 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
542 $args['meta_key'] = '_floor_area_from_sqft';
543 break;
544 }
545
546 return apply_filters( 'propertyhive_get_search_results_ordering_args', $args );
547 }
548
549 /**
550 * Appends date queries to an array.
551 * @access public
552 * @param array $meta_query
553 * @return array
554 */
555 public function get_date_query() {
556
557 $date_query = array();
558
559 if ( isset( $_REQUEST['added_from_hours'] ) && $_REQUEST['added_from_hours'] != '' )
560 {
561 $date_query = array(
562 'column' => 'post_date_gmt',
563 'after' => sanitize_text_field( $_REQUEST['added_from_hours'] . ' hours ago' )
564 );
565 }
566
567 return array_filter( $date_query );
568 }
569
570 /**
571 * Appends meta queries to an array.
572 * @access public
573 * @param array $meta_query
574 * @return array
575 */
576 public function get_meta_query( $q = array() ) {
577
578 $meta_query = array();
579 if ( !empty($q) )
580 {
581 $meta_query = $q->get( 'meta_query' );
582 }
583
584 if ( ! is_array( $meta_query ) )
585 $meta_query = array();
586
587 $meta_query[] = $this->on_market_meta_query();
588 $meta_query[] = $this->department_meta_query($q);
589 $meta_query[] = $this->address_keyword_meta_query();
590 $meta_query[] = $this->country_meta_query();
591 $meta_query[] = $this->minimum_price_meta_query();
592 $meta_query[] = $this->maximum_price_meta_query();
593 $meta_query[] = $this->price_range_meta_query();
594 $meta_query[] = $this->minimum_rent_meta_query();
595 $meta_query[] = $this->maximum_rent_meta_query();
596 $meta_query[] = $this->rent_range_meta_query();
597 $meta_query[] = $this->bedrooms_meta_query();
598 $meta_query[] = $this->minimum_bedrooms_meta_query();
599 $meta_query[] = $this->maximum_bedrooms_meta_query();
600 $meta_query[] = $this->minimum_bathrooms_meta_query();
601 $meta_query[] = $this->maximum_bathrooms_meta_query();
602 $meta_query[] = $this->minimum_reception_rooms_meta_query();
603 $meta_query[] = $this->maximum_reception_rooms_meta_query();
604 $meta_query[] = $this->available_date_from_meta_query();
605 $meta_query[] = $this->minimum_floor_area_meta_query();
606 $meta_query[] = $this->maximum_floor_area_meta_query();
607 $meta_query[] = $this->floor_area_range_meta_query();
608 $meta_query[] = $this->office_meta_query();
609
610 return array_filter( apply_filters( 'propertyhive_property_query_meta_query', $meta_query, $this ) );
611 }
612
613 /**
614 * Returns a meta query to handle property on market status
615 *
616 * @access public
617 * @param string $compare (default: 'IN')
618 * @return array
619 */
620 public function on_market_meta_query( ) {
621
622 if ( !is_admin() )
623 {
624 $meta_query = array(
625 'key' => '_on_market',
626 'value' => 'yes',
627 'compare' => '='
628 );
629 }
630
631 return $meta_query;
632 }
633
634 /**
635 * Returns a meta query to handle property department
636 *
637 * @access public
638 * @return array
639 */
640 public function department_meta_query( $q ) {
641
642 $meta_query = array();
643
644 if ( isset( $_REQUEST['department'] ) && $_REQUEST['department'] != '' )
645 {
646 $meta_query = array(
647 'key' => '_department',
648 'value' => sanitize_text_field( $_REQUEST['department'] ),
649 'compare' => '='
650 );
651 }
652 else
653 {
654 // Need a department set if on search results page
655 if (!empty($q) && $q->is_post_type_archive( 'property' ))
656 {
657 if (get_option( 'propertyhive_primary_department' ) != '')
658 {
659 $department = get_option( 'propertyhive_primary_department' );
660 }
661 else
662 {
663 $departments = array();
664 if ( get_option( 'propertyhive_active_departments_sales' ) == 'yes' )
665 {
666 $departments[] = 'residential-sales';
667 }
668 if ( get_option( 'propertyhive_active_departments_lettings' ) == 'yes' )
669 {
670 $departments[] = 'residential-lettings';
671 }
672 if ( get_option( 'propertyhive_active_departments_commercial' ) == 'yes' )
673 {
674 $departments[] = 'commercial';
675 }
676
677 $department = $departments[0];
678 }
679 $meta_query = array(
680 'key' => '_department',
681 'value' => $department,
682 'compare' => '='
683 );
684 }
685 }
686
687 return $meta_query;
688 }
689
690 /**
691 * Returns a meta query to handle searching for a keyword in the address
692 *
693 * @access public
694 * @param string $compare (default: 'IN')
695 * @return array
696 */
697 public function address_keyword_meta_query( ) {
698
699 $meta_query = array();
700
701 if ( isset( $_REQUEST['address_keyword'] ) && $_REQUEST['address_keyword'] != '' )
702 {
703 $_REQUEST['address_keyword'] = sanitize_text_field( trim( $_REQUEST['address_keyword'] ) );
704
705 $address_keywords = array( $_REQUEST['address_keyword'] );
706
707 if ( strpos( $_REQUEST['address_keyword'], ' ' ) !== FALSE )
708 {
709 $address_keywords[] = str_replace(" ", "-", $_REQUEST['address_keyword']);
710 }
711 if ( strpos( $_REQUEST['address_keyword'], '-' ) !== FALSE )
712 {
713 $address_keywords[] = str_replace("-", " ", $_REQUEST['address_keyword']);
714 }
715
716 $meta_query = array('relation' => 'OR');
717
718 foreach ( $address_keywords as $address_keyword )
719 {
720 $meta_query[] = array(
721 'key' => '_reference_number',
722 'value' => $address_keyword,
723 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
724 );
725 $meta_query[] = array(
726 'key' => '_address_street',
727 'value' => $address_keyword,
728 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
729 );
730 $meta_query[] = array(
731 'key' => '_address_two',
732 'value' => $address_keyword,
733 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
734 );
735 $meta_query[] = array(
736 'key' => '_address_three',
737 'value' => $address_keyword,
738 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
739 );
740 $meta_query[] = array(
741 'key' => '_address_four',
742 'value' => $address_keyword,
743 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
744 );
745 }
746 if ( strlen($_REQUEST['address_keyword']) <= 4 )
747 {
748 $meta_query[] = array(
749 'key' => '_address_postcode',
750 'value' => sanitize_text_field( $_REQUEST['address_keyword'] ),
751 'compare' => '='
752 );
753 $meta_query[] = array(
754 'key' => '_address_postcode',
755 'value' => sanitize_text_field( $_REQUEST['address_keyword'] ) . '[ ]',
756 'compare' => 'RLIKE'
757 );
758 }
759 else
760 {
761 $meta_query[] = array(
762 'key' => '_address_postcode',
763 'value' => sanitize_text_field( $_REQUEST['address_keyword'] ),
764 'compare' => 'LIKE'
765 );
766 }
767 }
768
769 return $meta_query;
770 }
771
772 /**
773 * Returns a meta query to handle country
774 *
775 * @access public
776 * @return array
777 */
778 public function country_meta_query( ) {
779
780 $meta_query = array();
781
782 if ( isset( $_REQUEST['country'] ) && $_REQUEST['country'] != '' )
783 {
784 $meta_query = array(
785 'key' => '_address_country',
786 'value' => sanitize_text_field( $_REQUEST['country'] )
787 );
788 }
789
790 return $meta_query;
791 }
792
793 /**
794 * Returns a meta query to handle minimum price
795 *
796 * @access public
797 * @return array
798 */
799 public function minimum_price_meta_query( ) {
800
801 $meta_query = array();
802
803 if (
804 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
805 isset( $_REQUEST['minimum_price'] ) && $_REQUEST['minimum_price'] != ''
806 )
807 {
808 $meta_query = array(
809 'key' => '_price_actual',
810 'value' => sanitize_text_field( $_REQUEST['minimum_price'] ),
811 'compare' => '>=',
812 'type' => 'NUMERIC'
813 );
814 }
815
816 return $meta_query;
817 }
818
819 /**
820 * Returns a meta query to handle maximum price
821 *
822 * @access public
823 * @return array
824 */
825 public function maximum_price_meta_query( ) {
826
827 $meta_query = array();
828
829 if (
830 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
831 isset( $_REQUEST['maximum_price'] ) && $_REQUEST['maximum_price'] != ''
832 )
833 {
834 $meta_query = array(
835 'key' => '_price_actual',
836 'value' => sanitize_text_field( $_REQUEST['maximum_price'] ),
837 'compare' => '<=',
838 'type' => 'NUMERIC'
839 );
840 }
841
842 return $meta_query;
843 }
844
845 /**
846 * Returns a meta query to handle price range
847 *
848 * @access public
849 * @return array
850 */
851 public function price_range_meta_query( ) {
852
853 $meta_query = array();
854
855 if (
856 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
857 isset( $_REQUEST['price_range'] ) && $_REQUEST['price_range'] != ''
858 )
859 {
860 $explode_price_range = explode("-", $_REQUEST['price_range']);
861
862 if ( isset($explode_price_range[0]) && $explode_price_range[0] != '' )
863 {
864 $meta_query = array(
865 'key' => '_price_actual',
866 'value' => sanitize_text_field( $explode_price_range[0] ),
867 'compare' => '>=',
868 'type' => 'NUMERIC'
869 );
870 }
871 if ( isset($explode_price_range[1]) && $explode_price_range[1] != '' )
872 {
873 $meta_query = array(
874 'key' => '_price_actual',
875 'value' => sanitize_text_field( $explode_price_range[1] ),
876 'compare' => '<=',
877 'type' => 'NUMERIC'
878 );
879 }
880 }
881
882 return $meta_query;
883 }
884
885 /**
886 * Returns a meta query to handle minimum rent
887 *
888 * @access public
889 * @return array
890 */
891 public function minimum_rent_meta_query( ) {
892
893 $meta_query = array();
894
895 if (
896 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
897 isset( $_REQUEST['minimum_rent'] ) && $_REQUEST['minimum_rent'] != ''
898 )
899 {
900 $meta_query = array(
901 'key' => '_price_actual',
902 'value' => sanitize_text_field( $_REQUEST['minimum_rent'] ),
903 'compare' => '>=',
904 'type' => 'NUMERIC'
905 );
906 }
907
908 return $meta_query;
909 }
910
911 /**
912 * Returns a meta query to handle maximum rent
913 *
914 * @access public
915 * @return array
916 */
917 public function maximum_rent_meta_query( ) {
918
919 $meta_query = array();
920
921 if (
922 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
923 isset( $_REQUEST['maximum_rent'] ) && $_REQUEST['maximum_rent'] != ''
924 )
925 {
926 $meta_query = array(
927 'key' => '_price_actual',
928 'value' => sanitize_text_field( $_REQUEST['maximum_rent'] ),
929 'compare' => '<=',
930 'type' => 'NUMERIC'
931 );
932 }
933
934 return $meta_query;
935 }
936
937 /**
938 * Returns a meta query to handle rent range
939 *
940 * @access public
941 * @return array
942 */
943 public function rent_range_meta_query( ) {
944
945 $meta_query = array();
946
947 if (
948 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
949 isset( $_REQUEST['rent_range'] ) && $_REQUEST['rent_range'] != ''
950 )
951 {
952 $explode_rent_range = explode("-", $_REQUEST['rent_range']);
953
954 if ( isset($explode_rent_range[0]) && $explode_rent_range[0] != '' )
955 {
956 $meta_query = array(
957 'key' => '_price_actual',
958 'value' => sanitize_text_field( $explode_rent_range[0] ),
959 'compare' => '>=',
960 'type' => 'NUMERIC'
961 );
962 }
963 if ( isset($explode_rent_range[1]) && $explode_rent_range[1] != '' )
964 {
965 $meta_query = array(
966 'key' => '_price_actual',
967 'value' => sanitize_text_field( $explode_rent_range[1] ),
968 'compare' => '<=',
969 'type' => 'NUMERIC'
970 );
971 }
972 }
973
974 return $meta_query;
975 }
976
977 /**
978 * Returns a meta query to handle exact bedrooms
979 *
980 * @access public
981 * @return array
982 */
983 public function bedrooms_meta_query( ) {
984
985 $meta_query = array();
986
987 if (
988 (
989 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
990 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
991 ) &&
992 isset( $_REQUEST['bedrooms'] ) && $_REQUEST['bedrooms'] != ''
993 )
994 {
995 $meta_query = array(
996 'key' => '_bedrooms',
997 'value' => sanitize_text_field( $_REQUEST['bedrooms'] ),
998 'compare' => '=',
999 'type' => 'NUMERIC'
1000 );
1001 }
1002
1003 return $meta_query;
1004 }
1005
1006 /**
1007 * Returns a meta query to handle minimum bedrooms
1008 *
1009 * @access public
1010 * @return array
1011 */
1012 public function minimum_bedrooms_meta_query( ) {
1013
1014 $meta_query = array();
1015
1016 if (
1017 (
1018 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1019 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1020 ) &&
1021 isset( $_REQUEST['minimum_bedrooms'] ) && $_REQUEST['minimum_bedrooms'] != ''
1022 )
1023 {
1024 $meta_query = array(
1025 'key' => '_bedrooms',
1026 'value' => sanitize_text_field( $_REQUEST['minimum_bedrooms'] ),
1027 'compare' => '>=',
1028 'type' => 'NUMERIC'
1029 );
1030 }
1031
1032 return $meta_query;
1033 }
1034
1035 /**
1036 * Returns a meta query to handle maximum bedrooms
1037 *
1038 * @access public
1039 * @return array
1040 */
1041 public function maximum_bedrooms_meta_query( ) {
1042
1043 $meta_query = array();
1044
1045 if (
1046 (
1047 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1048 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1049 ) &&
1050 isset( $_REQUEST['maximum_bedrooms'] ) && $_REQUEST['maximum_bedrooms'] != ''
1051 )
1052 {
1053 $meta_query = array(
1054 'key' => '_bedrooms',
1055 'value' => sanitize_text_field( $_REQUEST['maximum_bedrooms'] ),
1056 'compare' => '<=',
1057 'type' => 'NUMERIC'
1058 );
1059 }
1060
1061 return $meta_query;
1062 }
1063
1064 /**
1065 * Returns a meta query to handle minimum bathrooms
1066 *
1067 * @access public
1068 * @return array
1069 */
1070 public function minimum_bathrooms_meta_query( ) {
1071
1072 $meta_query = array();
1073
1074 if (
1075 (
1076 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1077 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1078 ) &&
1079 isset( $_REQUEST['minimum_bathrooms'] ) && $_REQUEST['minimum_bathrooms'] != ''
1080 )
1081 {
1082 $meta_query = array(
1083 'key' => '_bathrooms',
1084 'value' => sanitize_text_field( $_REQUEST['minimum_bathrooms'] ),
1085 'compare' => '>=',
1086 'type' => 'NUMERIC'
1087 );
1088 }
1089
1090 return $meta_query;
1091 }
1092
1093 /**
1094 * Returns a meta query to handle maximum bathrooms
1095 *
1096 * @access public
1097 * @return array
1098 */
1099 public function maximum_bathrooms_meta_query( ) {
1100
1101 $meta_query = array();
1102
1103 if (
1104 (
1105 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1106 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1107 ) &&
1108 isset( $_REQUEST['maximum_bathrooms'] ) && $_REQUEST['maximum_bathrooms'] != ''
1109 )
1110 {
1111 $meta_query = array(
1112 'key' => '_bathrooms',
1113 'value' => sanitize_text_field( $_REQUEST['maximum_bathrooms'] ),
1114 'compare' => '<=',
1115 'type' => 'NUMERIC'
1116 );
1117 }
1118
1119 return $meta_query;
1120 }
1121
1122 /**
1123 * Returns a meta query to handle minimum reception rooms
1124 *
1125 * @access public
1126 * @return array
1127 */
1128 public function minimum_reception_rooms_meta_query( ) {
1129
1130 $meta_query = array();
1131
1132 if (
1133 (
1134 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1135 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1136 ) &&
1137 isset( $_REQUEST['minimum_reception_rooms'] ) && $_REQUEST['minimum_reception_rooms'] != ''
1138 )
1139 {
1140 $meta_query = array(
1141 'key' => '_reception_rooms',
1142 'value' => sanitize_text_field( $_REQUEST['minimum_reception_rooms'] ),
1143 'compare' => '>=',
1144 'type' => 'NUMERIC'
1145 );
1146 }
1147
1148 return $meta_query;
1149 }
1150
1151 /**
1152 * Returns a meta query to handle maximum reception rooms
1153 *
1154 * @access public
1155 * @return array
1156 */
1157 public function maximum_reception_rooms_meta_query( ) {
1158
1159 $meta_query = array();
1160
1161 if (
1162 (
1163 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1164 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1165 ) &&
1166 isset( $_REQUEST['maximum_reception_rooms'] ) && $_REQUEST['maximum_reception_rooms'] != ''
1167 )
1168 {
1169 $meta_query = array(
1170 'key' => '_reception_rooms',
1171 'value' => sanitize_text_field( $_REQUEST['maximum_reception_rooms'] ),
1172 'compare' => '<=',
1173 'type' => 'NUMERIC'
1174 );
1175 }
1176
1177 return $meta_query;
1178 }
1179
1180 /**
1181 * Returns a meta query to handle available date from
1182 *
1183 * @access public
1184 * @return array
1185 */
1186 public function available_date_from_meta_query( ) {
1187
1188 $meta_query = array();
1189
1190 if (
1191 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
1192 isset( $_REQUEST['available_date_from'] ) && $_REQUEST['available_date_from'] != ''
1193 )
1194 {
1195 $available_date = $_REQUEST['available_date_from'];
1196 if ( strpos($available_date, '/') !== FALSE )
1197 {
1198 // it's been provided in the format dd/mm/yyyy
1199 $explode_available_date = explode("/", $available_date);
1200 if ( count($explode_available_date) == 3 )
1201 {
1202 $available_date = $explode_available_date[0] . '-' . $explode_available_date[1] . '-' . $explode_available_date[2];
1203 }
1204 }
1205 $meta_query = array(
1206 'key' => '_available_date',
1207 'value' => sanitize_text_field( $available_date ),
1208 'compare' => '<=',
1209 );
1210 }
1211
1212 return $meta_query;
1213 }
1214
1215 /**
1216 * Returns a meta query to handle minimum floor area
1217 *
1218 * @access public
1219 * @return array
1220 */
1221 public function minimum_floor_area_meta_query( ) {
1222
1223 $meta_query = array();
1224
1225 if (
1226 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1227 isset( $_REQUEST['minimum_floor_area'] ) && $_REQUEST['minimum_floor_area'] != ''
1228 )
1229 {
1230 $meta_query = array(
1231 'key' => '_floor_area_from_sqft',
1232 'value' => sanitize_text_field( $_REQUEST['minimum_floor_area'] ),
1233 'compare' => '>=',
1234 'type' => 'NUMERIC'
1235 );
1236 }
1237
1238 return $meta_query;
1239 }
1240
1241 /**
1242 * Returns a meta query to handle maximum floor area
1243 *
1244 * @access public
1245 * @return array
1246 */
1247 public function maximum_floor_area_meta_query( ) {
1248
1249 $meta_query = array();
1250
1251 if (
1252 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1253 isset( $_REQUEST['maximum_floor_area'] ) && $_REQUEST['maximum_floor_area'] != ''
1254 )
1255 {
1256 $meta_query = array(
1257 'key' => '_floor_area_to_sqft',
1258 'value' => sanitize_text_field( $_REQUEST['maximum_floor_area'] ),
1259 'compare' => '<=',
1260 'type' => 'NUMERIC'
1261 );
1262 }
1263
1264 return $meta_query;
1265 }
1266
1267 /**
1268 * Returns a meta query to handle floor area range
1269 *
1270 * @access public
1271 * @return array
1272 */
1273 public function floor_area_range_meta_query( ) {
1274
1275 $meta_query = array();
1276
1277 if (
1278 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1279 isset( $_REQUEST['floor_area_range'] ) && $_REQUEST['floor_area_range'] != ''
1280 )
1281 {
1282 $explode_floor_area_range = explode("-", $_REQUEST['floor_area_range']);
1283
1284 if ( isset($explode_floor_area_range[0]) && $explode_floor_area_range[0] != '' )
1285 {
1286 $meta_query = array(
1287 'key' => '_floor_area_from_sqft',
1288 'value' => sanitize_text_field( $explode_floor_area_range[0] ),
1289 'compare' => '>=',
1290 'type' => 'NUMERIC'
1291 );
1292 }
1293 if ( isset($explode_floor_area_range[1]) && $explode_floor_area_range[1] != '' )
1294 {
1295 $meta_query = array(
1296 'key' => '_floor_area_to_sqft',
1297 'value' => sanitize_text_field( $explode_floor_area_range[1] ),
1298 'compare' => '<=',
1299 'type' => 'NUMERIC'
1300 );
1301 }
1302 }
1303
1304 return $meta_query;
1305 }
1306
1307 /**
1308 * Returns a meta query to handle property office
1309 *
1310 * @access public
1311 * @param string $compare (default: 'IN')
1312 * @return array
1313 */
1314 public function office_meta_query( ) {
1315
1316 $meta_query = array();
1317
1318 if ( isset( $_REQUEST['officeID'] ) && $_REQUEST['officeID'] != '' )
1319 {
1320 $meta_query = array(
1321 'key' => '_office_id',
1322 'value' => $_REQUEST['officeID'],
1323 'compare' => '='
1324 );
1325 }
1326
1327 return $meta_query;
1328 }
1329
1330 /**
1331 * Appends taxonomy queries to an array.
1332 * @access public
1333 * @param array $tax_query
1334 * @return array
1335 */
1336 public function get_tax_query( $tax_query = array() ) {
1337 if ( ! is_array( $tax_query ) )
1338 $tax_query = array();
1339
1340 if ( isset($_REQUEST) && !empty($_REQUEST) )
1341 {
1342 foreach ( $_REQUEST as $key => $value )
1343 {
1344 if ( taxonomy_exists($key) && isset( $_REQUEST[$key] ) && !empty($_REQUEST[$key]) )
1345 {
1346 $tax_query[] = array(
1347 'taxonomy' => $key,
1348 'terms' => ( (is_array($value)) ? $value : array( $value ) )
1349 );
1350 }
1351 }
1352 }
1353
1354 return array_filter( $tax_query );
1355 }
1356
1357 /**
1358 * Layered Nav Init
1359 */
1360 public function layered_nav_init( ) {
1361
1362 }
1363
1364 /**
1365 * Layered Nav post filter
1366 *
1367 * @param array $filtered_posts
1368 * @return array
1369 */
1370 public function layered_nav_query( $filtered_posts ) {
1371
1372 }
1373
1374 /**
1375 * Price filter Init
1376 */
1377 public function price_filter_init() {
1378
1379 }
1380
1381 /**
1382 * Price Filter post filter
1383 *
1384 * @param array $filtered_posts
1385 * @return array
1386 */
1387 public function price_filter( $filtered_posts ) {
1388
1389 }
1390
1391 }
1392
1393 endif;
1394