PluginProbe
Property Hive / 1.4.62
Property Hive v1.4.62
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.62, at includes/class-ph-query.php

1,776 lines 52.4 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 add_filter( 'posts_where', array( $this, 'commercial_display_where' ), 10, 2 );
65 }
66
67 $this->init_query_vars();
68 }
69
70 public function commercial_display_where( $where, $query )
71 {
72 if ( $query->get('post_type') == 'property' )
73 {
74 global $wpdb;
75
76 $commercial_display = get_option( 'propertyhive_commercial_display', '' );
77
78 switch ( $commercial_display )
79 {
80 case "top_level_only":
81 {
82 $where .= " AND $wpdb->posts.post_parent=0 ";
83 break;
84 }
85 case "top_level_only_but_units_when_filtered":
86 {
87 $unit_filter_parameters = apply_filters( 'propertyhive_unit_filter_parameters', array( 'minimum_floor_area', 'maximum_floor_area' ) );
88 $unit_filter_parameter_found = false;
89 foreach ( $unit_filter_parameters as $parameter )
90 {
91 if ( isset($_REQUEST[$parameter]) && ph_clean($_REQUEST[$parameter]) != '' )
92 {
93 $unit_filter_parameter_found = true;
94 }
95 }
96 if ( !$unit_filter_parameter_found )
97 {
98 $where .= " AND $wpdb->posts.post_parent=0 ";
99 }
100 break;
101 }
102 default:
103 {
104 // do nothing
105 }
106 }
107 }
108
109 return $where;
110 }
111
112 /**
113 * Init query vars by loading options.
114 */
115 public function init_query_vars() {
116 // Query vars to add to WP
117 $this->query_vars = array(
118
119 );
120 }
121
122 /**
123 * Get any errors from querystring
124 */
125 public function get_errors() {
126 if ( ! empty( $_GET['ph_error'] ) && ( $error = sanitize_text_field( $_GET['ph_error'] ) ) && ! ph_has_notice( $error, 'error' ) )
127 ph_add_notice( $error, 'error' );
128 }
129
130 /**
131 * Add endpoints for query vars
132 */
133 public function add_endpoints() {
134 foreach ( $this->query_vars as $key => $var )
135 add_rewrite_endpoint( $var, EP_PAGES );
136 }
137
138 /**
139 * add_query_vars function.
140 *
141 * @access public
142 * @param array $vars
143 * @return array
144 */
145 public function add_query_vars( $vars ) {
146 foreach ( $this->query_vars as $key => $var )
147 $vars[] = $key;
148
149 return $vars;
150 }
151
152 /**
153 * Get query vars
154 * @return array()
155 */
156 public function get_query_vars() {
157 return $this->query_vars;
158 }
159
160 /**
161 * Parse the request and look for query vars - endpoints may not be supported
162 */
163 public function parse_request() {
164 global $wp;
165
166 // Map query vars to their keys, or get them if endpoints are not supported
167 foreach ( $this->query_vars as $key => $var ) {
168 if ( isset( $_GET[ $var ] ) ) {
169 $wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
170 }
171
172 elseif ( isset( $wp->query_vars[ $var ] ) ) {
173 $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
174 }
175 }
176 }
177
178 /**
179 * Hook into pre_get_posts to do the main property query
180 *
181 * @access public
182 * @param mixed $q query object
183 * @return void
184 */
185 public function pre_get_posts( $q ) {
186 // We only want to affect the main query
187 if ( ! $q->is_main_query() )
188 return;
189
190 // Fix for verbose page rules
191 if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules && isset( $q->queried_object->ID ) && $q->queried_object->ID == ph_get_page_id( 'search_results' ) ) {
192 $q->set( 'post_type', 'property' );
193 $q->set( 'page', '' );
194 $q->set( 'pagename', '' );
195
196 // Fix conditional Functions
197 $q->is_archive = true;
198 $q->is_post_type_archive = true;
199 $q->is_singular = false;
200 $q->is_page = false;
201 }
202
203 // When orderby is set, WordPress shows posts. Get around that here.
204 if ( ($q->is_home() || $q->get( 'page_id' ) == get_option('page_on_front')) && 'page' == get_option('show_on_front') && get_option('page_on_front') == ph_get_page_id('search_results') )
205 {
206 $_query = wp_parse_args( $q->query );
207 if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) )
208 {
209
210 $q->is_page = true;
211 $q->is_home = false;
212 $q->set( 'page_id', (int) get_option('page_on_front') );
213 $q->set( 'post_type', 'property' );
214 }
215 }
216
217 // Special check for sites with the property search results on front page
218 if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == ph_get_page_id('search_results') ) {
219
220 // This is a front-page property listings
221 $q->set( 'post_type', 'property' );
222 $q->set( 'page_id', '' );
223 if ( isset( $q->query['paged'] ) )
224 $q->set( 'paged', $q->query['paged'] );
225
226 // Define a variable so we know this is the front page search results later on
227 define( 'SEARCH_RESULTS_IS_ON_FRONT', true );
228
229 // Get the actual WP page to avoid errors and let us use is_front_page()
230 // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
231 global $wp_post_types;
232
233 $search_results_page = get_post( ph_get_page_id('search_results') );
234 $q->is_page = true;
235
236 $wp_post_types['property']->ID = $search_results_page->ID;
237 $wp_post_types['property']->post_title = $search_results_page->post_title;
238 $wp_post_types['property']->post_name = $search_results_page->post_name;
239 $wp_post_types['property']->post_type = $search_results_page->post_type;
240 $wp_post_types['property']->ancestors = get_ancestors( $search_results_page->ID, $search_results_page->post_type );
241
242 // Fix conditional Functions like is_front_page
243 $q->is_singular = false;
244 $q->is_post_type_archive = true;
245 $q->is_archive = true;
246
247 // Fix WP SEO
248 if ( class_exists( 'WPSEO_Meta' ) ) {
249 add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
250 add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
251 }
252
253 } else {
254
255 // Only apply to property categories, the property post archive, the search results page, and property attribute taxonomies
256 if ( ! $q->is_post_type_archive( 'property' ) && ! $q->is_tax( get_object_taxonomies( 'property' ) ) )
257 return;
258
259 }
260
261 $this->property_query( $q );
262
263 if ( is_search() )
264 {
265 add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
266 add_filter( 'wp', array( $this, 'remove_posts_where' ) );
267 }
268
269 add_filter( 'posts_where', array( $this, 'exclude_protected_properties' ) );
270
271 // We're on a property search page so queue the propertyhive_get_properties_in_view function
272 //add_action( 'wp', array( $this, 'get_properties_in_view' ), 2);
273
274 // And remove the pre_get_posts hook
275 $this->remove_property_query();
276 }
277
278 /**
279 * search_post_excerpt function.
280 *
281 * @access public
282 * @param string $where (default: '')
283 * @return string (modified where clause)
284 */
285 public function search_post_excerpt( $where = '' ) {
286 /*global $wp_the_query;
287
288 // If this is not a PH Query, do not modify the query
289 if ( empty( $wp_the_query->query_vars['ph_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
290 return $where;
291
292 $where = preg_replace(
293 "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
294 "post_title LIKE $1) OR (post_excerpt LIKE $1", $where );*/
295
296 return $where;
297 }
298
299 /**
300 * Prevent password protected properties appearing in the loops
301 *
302 * @param string $where
303 * @return string
304 */
305 public function exclude_protected_properties( $where ) {
306 global $wpdb;
307 $where .= " AND {$wpdb->posts}.post_password = ''";
308 return $where;
309 }
310
311 /**
312 * wpseo_metadesc function.
313 * Hooked into wpseo_ hook already, so no need for function_exist
314 *
315 * @access public
316 * @return string
317 */
318 public function wpseo_metadesc() {
319 return WPSEO_Meta::get_value( 'metadesc', ph_get_page_id('search_results') );
320 }
321
322
323 /**
324 * wpseo_metakey function.
325 * Hooked into wpseo_ hook already, so no need for function_exist
326 *
327 * @access public
328 * @return string
329 */
330 public function wpseo_metakey() {
331 return WPSEO_Meta::get_value( 'metakey', ph_get_page_id('search_results') );
332 }
333
334
335 /**
336 * Hook into the_posts to do the main property query if needed - relevanssi compatibility
337 *
338 * @access public
339 * @param array $posts
340 * @param WP_Query|bool $query (default: false)
341 * @return array
342 */
343 public function the_posts( $posts, $query = false ) {
344
345 // Abort if there's no query
346 if ( ! $query )
347 return $posts;
348
349 // Abort if we're not filtering posts
350 if ( empty( $this->post__in ) )
351 return $posts;
352
353 // Abort if this query has already been done
354 if ( ! empty( $query->ph_query ) )
355 return $posts;
356
357 // Abort if this isn't a search query
358 if ( empty( $query->query_vars["s"] ) )
359 return $posts;
360
361 // Abort if we're not on a post type archive/property taxonomy
362 if ( ! $query->is_post_type_archive( 'property' ) && ! $query->is_tax( get_object_taxonomies( 'property' ) ) )
363 return $posts;
364
365 $filtered_posts = array();
366 $queried_post_ids = array();
367
368 foreach ( $posts as $post ) {
369 if ( in_array( $post->ID, $this->post__in ) ) {
370 $filtered_posts[] = $post;
371 $queried_post_ids[] = $post->ID;
372 }
373 }
374
375 $query->posts = $filtered_posts;
376 $query->post_count = count( $filtered_posts );
377
378 // Ensure filters are set
379 $this->unfiltered_property_ids = $queried_post_ids;
380 $this->filtered_property_ids = $queried_post_ids;
381
382 if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
383 $this->layered_nav_property_ids = array_intersect( $this->unfiltered_property_ids, $this->layered_nav_post__in );
384 } else {
385 $this->layered_nav_property_ids = $this->unfiltered_property_ids;
386 }
387
388 return $filtered_posts;
389 }
390
391
392 /**
393 * Query the properties, applying sorting/ordering etc. This applies to the main wordpress loop
394 *
395 * @access public
396 * @param mixed $q
397 * @return void
398 */
399 public function property_query( $q ) {
400
401 // Meta query
402 $meta_query = $this->get_meta_query( $q );
403
404 // Tax query
405 $tax_query = $this->get_tax_query( $q->get( 'tax_query' ) );
406
407 // Date query
408 $date_query = $this->get_date_query();
409
410 // Ordering
411 $ordering = $this->get_search_results_ordering_args();
412
413 // Get a list of post id's which match the current filters set (in the layered nav and price filter)
414 $post__in = array();
415 //$post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
416
417 // Ordering query vars
418 $q->set( 'orderby', $ordering['orderby'] );
419 $q->set( 'order', $ordering['order'] );
420 if ( isset( $ordering['meta_key'] ) )
421 $q->set( 'meta_key', $ordering['meta_key'] );
422
423 // Query vars that affect posts shown
424 $q->set( 'meta_query', $meta_query );
425 $q->set( 'tax_query', $tax_query );
426 $q->set( 'date_query', $date_query );
427 $q->set( 'post__in', $post__in );
428 $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' ) ) );
429
430 // Set a special variable
431 $q->set( 'ph_query', true );
432
433 // Store variables
434 $this->post__in = $post__in;
435 $this->meta_query = $meta_query;
436 $this->tax_query = $tax_query;
437
438 do_action( 'propertyhive_property_query', $q, $this );
439 }
440
441
442 /**
443 * Remove the query
444 *
445 * @access public
446 * @return void
447 */
448 public function remove_property_query() {
449 remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
450 }
451
452 /**
453 * Remove ordering queries
454 */
455 public function remove_ordering_args() {
456
457 }
458
459 /**
460 * Remove the posts_where filter
461 *
462 * @access public
463 * @return void
464 */
465 public function remove_posts_where() {
466 remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
467 }
468
469
470 /**
471 * Get an unpaginated list all property ID's (both filtered and unfiltered). Makes use of transients.
472 *
473 * @access public
474 * @return void
475 */
476 public function get_properties_in_view() {
477 global $wp_the_query;
478
479 $unfiltered_property_ids = array();
480
481 // Get main query
482 $current_wp_query = $wp_the_query->query;
483
484 // Get WP Query for current page (without 'paged')
485 unset( $current_wp_query['paged'] );
486
487 // Generate a transient name based on current query
488 $transient_name = 'ph_uf_pid_' . md5( http_build_query( $current_wp_query ) );
489 $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
490
491 if ( false === ( $unfiltered_property_ids = get_transient( $transient_name ) ) ) {
492
493 // Get all visible posts, regardless of filters
494 $unfiltered_property_ids = get_posts(
495 array_merge(
496 $current_wp_query,
497 array(
498 'post_type' => 'property',
499 'numberposts' => -1,
500 'post_status' => 'publish',
501 'meta_query' => $this->meta_query,
502 'fields' => 'ids',
503 'no_found_rows' => true,
504 'update_post_meta_cache' => false,
505 'update_post_term_cache' => false
506 )
507 )
508 );
509
510 set_transient( $transient_name, $unfiltered_property_ids, YEAR_IN_SECONDS );
511 }
512
513 // Store the variable
514 $this->unfiltered_property_ids = $unfiltered_property_ids;
515
516 // Also store filtered posts ids...
517 if ( sizeof( $this->post__in ) > 0 )
518 $this->filtered_property_ids = array_intersect( $this->unfiltered_property_ids, $this->post__in );
519 else
520 $this->filtered_property_ids = $this->unfiltered_property_ids;
521
522 // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
523 if ( sizeof( $this->layered_nav_post__in ) > 0 )
524 $this->layered_nav_property_ids = array_intersect( $this->unfiltered_property_ids, $this->layered_nav_post__in );
525 else
526 $this->layered_nav_property_ids = $this->unfiltered_property_ids;
527 }
528
529
530 /**
531 * Returns an array of arguments for ordering properties based on the selected values
532 *
533 * @access public
534 * @return array
535 */
536 public function get_search_results_ordering_args( $orderby = '', $order = '' ) {
537 // Get ordering from query string unless defined
538 if ( ! $orderby ) {
539 $orderby_value = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : apply_filters( 'propertyhive_default_search_results_orderby', get_option( 'propertyhive_default_search_results_orderby' ) );
540
541 // Get order + orderby args from string
542 $orderby_value = explode( '-', $orderby_value );
543 $orderby = esc_attr( $orderby_value[0] );
544 $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
545 }
546
547 $orderby = strtolower( $orderby );
548 $order = strtoupper( $order );
549
550 $args = array();
551
552 // default - menu_order
553 if (
554 ( isset($_REQUEST['department']) && $_REQUEST['department'] != 'commercial' ) ||
555 ( !isset($_REQUEST['department']) && get_option( 'propertyhive_primary_department' ) != 'commercial' )
556 )
557 {
558 $args['orderby'] = 'meta_value_num';
559 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
560 $args['meta_key'] = '_price_actual';
561 }
562 elseif (
563 ( isset($_REQUEST['department']) && $_REQUEST['department'] == 'commercial' ) ||
564 ( !isset($_REQUEST['department']) && get_option( 'propertyhive_primary_department' ) == 'commercial' )
565 )
566 {
567 $args['orderby'] = 'meta_value_num';
568 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
569 $args['meta_key'] = '_floor_area_from_sqft';
570 }
571
572 switch ( $orderby ) {
573 case 'price' :
574 $args['orderby'] = 'meta_value_num';
575 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
576 if (
577 ( isset($_REQUEST['department']) && $_REQUEST['department'] == 'commercial' ) ||
578 ( !isset($_REQUEST['department']) && get_option( 'propertyhive_primary_department' ) == 'commercial' )
579 )
580 {
581 $args['meta_key'] = '_price_from_actual';
582 }
583 else
584 {
585 $args['meta_key'] = '_price_actual';
586 }
587 break;
588 case 'floor_area' :
589 $args['orderby'] = 'meta_value_num';
590 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
591 $args['meta_key'] = '_floor_area_from_sqft';
592 break;
593 default :
594 {
595 if ( $orderby != '' )
596 {
597 $args['orderby'] = $orderby;
598 $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
599 }
600 }
601 }
602
603 return apply_filters( 'propertyhive_get_search_results_ordering_args', $args );
604 }
605
606 /**
607 * Appends date queries to an array.
608 * @access public
609 * @param array $meta_query
610 * @return array
611 */
612 public function get_date_query() {
613
614 $date_query = array();
615
616 if ( isset( $_REQUEST['added_from'] ) && $_REQUEST['added_from'] != '' )
617 {
618 $date_query = array(
619 'column' => 'post_date_gmt',
620 'after' => sanitize_text_field( $_REQUEST['added_from'] )
621 );
622 }
623
624 if ( isset( $_REQUEST['added_from_hours'] ) && $_REQUEST['added_from_hours'] != '' )
625 {
626 $date_query = array(
627 'column' => 'post_date_gmt',
628 'after' => sanitize_text_field( $_REQUEST['added_from_hours'] ) . ' hours ago'
629 );
630 }
631
632 return array_filter( $date_query );
633 }
634
635 /**
636 * Appends meta queries to an array.
637 * @access public
638 * @param array $meta_query
639 * @return array
640 */
641 public function get_meta_query( $q = array() ) {
642
643 $meta_query = array();
644 if ( !empty($q) )
645 {
646 $meta_query = $q->get( 'meta_query' );
647 }
648
649 if ( ! is_array( $meta_query ) )
650 $meta_query = array();
651
652 $meta_query[] = $this->on_market_meta_query();
653 $meta_query[] = $this->department_meta_query($q);
654 $meta_query[] = $this->address_keyword_meta_query();
655 $meta_query[] = $this->country_meta_query();
656 $meta_query[] = $this->minimum_price_meta_query();
657 $meta_query[] = $this->maximum_price_meta_query();
658 $meta_query[] = $this->price_range_meta_query();
659 $meta_query[] = $this->minimum_rent_meta_query();
660 $meta_query[] = $this->maximum_rent_meta_query();
661 $meta_query[] = $this->rent_range_meta_query();
662 $meta_query[] = $this->bedrooms_meta_query();
663 $meta_query[] = $this->minimum_bedrooms_meta_query();
664 $meta_query[] = $this->maximum_bedrooms_meta_query();
665 $meta_query[] = $this->minimum_bathrooms_meta_query();
666 $meta_query[] = $this->maximum_bathrooms_meta_query();
667 $meta_query[] = $this->minimum_reception_rooms_meta_query();
668 $meta_query[] = $this->maximum_reception_rooms_meta_query();
669 $meta_query[] = $this->available_date_from_meta_query();
670 $meta_query[] = $this->minimum_floor_area_meta_query();
671 $meta_query[] = $this->maximum_floor_area_meta_query();
672 $meta_query[] = $this->minimum_maximum_floor_area_meta_query();
673 $meta_query[] = $this->floor_area_range_meta_query();
674 $meta_query[] = $this->commercial_for_sale_to_rent_meta_query();
675 $meta_query[] = $this->commercial_for_sale_meta_query();
676 $meta_query[] = $this->commercial_to_rent_meta_query();
677 $meta_query[] = $this->negotiator_meta_query();
678 $meta_query[] = $this->office_meta_query();
679
680 return array_filter( apply_filters( 'propertyhive_property_query_meta_query', $meta_query, $this ) );
681 }
682
683 /**
684 * Returns a meta query to handle property on market status
685 *
686 * @access public
687 * @param string $compare (default: 'IN')
688 * @return array
689 */
690 public function on_market_meta_query( ) {
691
692 $meta_query = array();
693
694 if ( !is_admin() )
695 {
696 $meta_query = array(
697 'key' => '_on_market',
698 'value' => 'yes',
699 'compare' => '='
700 );
701 }
702
703 return $meta_query;
704 }
705
706 /**
707 * Returns a meta query to handle property department
708 *
709 * @access public
710 * @return array
711 */
712 public function department_meta_query( $q ) {
713
714 $meta_query = array();
715
716 if ( isset( $_REQUEST['department'] ) && $_REQUEST['department'] != '' )
717 {
718 $meta_query = array(
719 'key' => '_department',
720 'value' => sanitize_text_field( $_REQUEST['department'] ),
721 'compare' => '='
722 );
723 }
724 else
725 {
726 // Need a department set if on search results page
727 if (!empty($q) && $q->is_post_type_archive( 'property' ))
728 {
729 if (get_option( 'propertyhive_primary_department' ) != '')
730 {
731 $department = get_option( 'propertyhive_primary_department' );
732 }
733 else
734 {
735 // No primary department set. Use first active one. Should never get to this scenario
736 $department = '';
737
738 $departments = ph_get_departments();
739
740 foreach ( $departments as $key => $value )
741 {
742 if ( get_option( 'propertyhive_active_departments_' . str_replace("residential-", "", $key) ) == 'yes' )
743 {
744 if ( $department == '' )
745 {
746 $department = $key;
747 }
748 }
749 }
750 }
751 $meta_query = array(
752 'key' => '_department',
753 'value' => $department,
754 'compare' => '='
755 );
756 }
757 }
758
759 return $meta_query;
760 }
761
762 /**
763 * Returns a meta query to handle searching for a keyword in the address
764 *
765 * @access public
766 * @param string $compare (default: 'IN')
767 * @return array
768 */
769 public function address_keyword_meta_query( ) {
770
771 $meta_query = array();
772
773 if ( isset( $_REQUEST['address_keyword'] ) && $_REQUEST['address_keyword'] != '' )
774 {
775 $_REQUEST['address_keyword'] = ph_clean( wp_unslash( $_REQUEST['address_keyword'] ) );
776
777 // Remove country code from end (i.e. ', UK')
778 $_REQUEST['address_keyword'] = preg_replace('/\,\s?[A-Z][A-Z]$/', '', $_REQUEST['address_keyword']);
779
780 $address_keywords = array( $_REQUEST['address_keyword'] );
781
782 if ( strpos( $_REQUEST['address_keyword'], ' ' ) !== FALSE )
783 {
784 $address_keywords[] = str_replace(" ", "-", ph_clean($_REQUEST['address_keyword']));
785 }
786 if ( strpos( $_REQUEST['address_keyword'], '-' ) !== FALSE )
787 {
788 $address_keywords[] = str_replace("-", " ", ph_clean($_REQUEST['address_keyword']));
789 }
790
791 $meta_query = array('relation' => 'OR');
792
793 $address_fields_to_query = array(
794 '_reference_number',
795 '_address_street',
796 '_address_two',
797 '_address_three',
798 '_address_four',
799 '_address_postcode'
800 );
801
802 $address_fields_to_query = apply_filters( 'propertyhive_address_fields_to_query', $address_fields_to_query );
803
804 foreach ( $address_keywords as $address_keyword )
805 {
806 foreach ( $address_fields_to_query as $address_field )
807 {
808 if ( $address_field == '_address_postcode' ) { continue; } // ignore postcode as that is handled differently afterwards
809
810 $meta_query[] = array(
811 'key' => $address_field,
812 'value' => $address_keyword,
813 'compare' => get_option( 'propertyhive_address_keyword_compare', '=' )
814 );
815 }
816 }
817 if ( in_array('_address_postcode', $address_fields_to_query) )
818 {
819 if ( strlen($_REQUEST['address_keyword']) <= 4 )
820 {
821 $meta_query[] = array(
822 'key' => '_address_postcode',
823 'value' => ph_clean( $_REQUEST['address_keyword'] ),
824 'compare' => '='
825 );
826 $meta_query[] = array(
827 'key' => '_address_postcode',
828 'value' => '^' . ph_clean( $_REQUEST['address_keyword'] ) . '[ ]',
829 'compare' => 'RLIKE'
830 );
831 }
832 else
833 {
834 $postcode = ph_clean( $_REQUEST['address_keyword'] );
835
836 if ( preg_match('#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW])[0-9][ABD-HJLNP-UW-Z]{2})$#i', $postcode) )
837 {
838 // UK postcode found with no space
839
840 if ( strlen($postcode) == 5 )
841 {
842 $first_part = substr($postcode, 0, 2);
843 $last_part = substr($postcode, 2, 3);
844
845 $postcode = $first_part . ' ' . $last_part;
846 }
847 elseif ( strlen($postcode) == 6 )
848 {
849 $first_part = substr($postcode, 0, 3);
850 $last_part = substr($postcode, 3, 3);
851
852 $postcode = $first_part . ' ' . $last_part;
853 }
854 elseif ( strlen($postcode) == 7 )
855 {
856 $first_part = substr($postcode, 0, 4);
857 $last_part = substr($postcode, 4, 3);
858
859 $postcode = $first_part . ' ' . $last_part;
860 }
861 }
862
863 $meta_query[] = array(
864 'key' => '_address_postcode',
865 'value' => ph_clean( $postcode ),
866 'compare' => 'LIKE'
867 );
868 }
869 }
870 }
871
872 return $meta_query;
873 }
874
875 /**
876 * Returns a meta query to handle country
877 *
878 * @access public
879 * @return array
880 */
881 public function country_meta_query( ) {
882
883 $meta_query = array();
884
885 if ( isset( $_REQUEST['country'] ) && $_REQUEST['country'] != '' )
886 {
887 $meta_query = array(
888 'key' => '_address_country',
889 'value' => ph_clean( $_REQUEST['country'] )
890 );
891 }
892
893 return $meta_query;
894 }
895
896 /**
897 * Returns a meta query to handle minimum price
898 *
899 * @access public
900 * @return array
901 */
902 public function minimum_price_meta_query( ) {
903
904 $meta_query = array();
905
906 if (
907 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
908 isset( $_REQUEST['minimum_price'] ) && $_REQUEST['minimum_price'] != ''
909 )
910 {
911 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
912
913 $minimum_price = $_REQUEST['minimum_price'];
914 if ( $search_form_currency != 'GBP' )
915 {
916 // Convert $_REQUEST['minimum_price'] to GBP
917 $ph_countries = new PH_Countries();
918
919 $minimum_price = $ph_countries->convert_price_to_gbp( $minimum_price, $search_form_currency );
920 }
921
922 $meta_query = array(
923 'key' => '_price_actual',
924 'value' => ph_clean( floor( $minimum_price ) ),
925 'compare' => '>=',
926 'type' => 'NUMERIC'
927 );
928 }
929
930 return $meta_query;
931 }
932
933 /**
934 * Returns a meta query to handle maximum price
935 *
936 * @access public
937 * @return array
938 */
939 public function maximum_price_meta_query( ) {
940
941 $meta_query = array();
942
943 if (
944 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
945 isset( $_REQUEST['maximum_price'] ) && $_REQUEST['maximum_price'] != ''
946 )
947 {
948 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
949
950 $maximum_price = $_REQUEST['maximum_price'];
951 if ( $search_form_currency != 'GBP' )
952 {
953 // Convert $_REQUEST['maximum_price'] to GBP
954 $ph_countries = new PH_Countries();
955
956 $maximum_price = $ph_countries->convert_price_to_gbp( $maximum_price, $search_form_currency );
957 }
958
959 $meta_query = array(
960 'key' => '_price_actual',
961 'value' => ph_clean( ceil( $maximum_price ) ),
962 'compare' => '<=',
963 'type' => 'NUMERIC'
964 );
965 }
966
967 return $meta_query;
968 }
969
970 /**
971 * Returns a meta query to handle price range
972 *
973 * @access public
974 * @return array
975 */
976 public function price_range_meta_query( ) {
977
978 $meta_query = array();
979
980 if (
981 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales' &&
982 isset( $_REQUEST['price_range'] ) && $_REQUEST['price_range'] != ''
983 )
984 {
985 $explode_price_range = explode("-", ph_clean($_REQUEST['price_range']));
986
987 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
988
989 if ( isset($explode_price_range[0]) && $explode_price_range[0] != '' )
990 {
991 $minimum_price = $explode_price_range[0];
992
993 if ( $search_form_currency != 'GBP' )
994 {
995 // Convert $explode_price_range[0] to GBP
996 $ph_countries = new PH_Countries();
997
998 $minimum_price = $ph_countries->convert_price_to_gbp( $minimum_price, $search_form_currency );
999 }
1000
1001 $meta_query[] = array(
1002 'key' => '_price_actual',
1003 'value' => sanitize_text_field( floor( $minimum_price ) ),
1004 'compare' => '>=',
1005 'type' => 'NUMERIC'
1006 );
1007 }
1008 if ( isset($explode_price_range[1]) && $explode_price_range[1] != '' )
1009 {
1010 $maximum_price = $explode_price_range[1];
1011 if ( $search_form_currency != 'GBP' )
1012 {
1013 // Convert $explode_price_range[1] to GBP
1014 $ph_countries = new PH_Countries();
1015
1016 $maximum_price = $ph_countries->convert_price_to_gbp( $maximum_price, $search_form_currency );
1017 }
1018
1019 $meta_query[] = array(
1020 'key' => '_price_actual',
1021 'value' => sanitize_text_field( ceil( $maximum_price ) ),
1022 'compare' => '<=',
1023 'type' => 'NUMERIC'
1024 );
1025 }
1026 }
1027
1028 return $meta_query;
1029 }
1030
1031 /**
1032 * Returns a meta query to handle minimum rent
1033 *
1034 * @access public
1035 * @return array
1036 */
1037 public function minimum_rent_meta_query( ) {
1038
1039 $meta_query = array();
1040
1041 if (
1042 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
1043 isset( $_REQUEST['minimum_rent'] ) && $_REQUEST['minimum_rent'] != ''
1044 )
1045 {
1046 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
1047
1048 $minimum_rent = $_REQUEST['minimum_rent'];
1049 if ( $search_form_currency != 'GBP' )
1050 {
1051 // Convert $_REQUEST['minimum_rent'] to GBP
1052 $ph_countries = new PH_Countries();
1053
1054 $minimum_rent = $ph_countries->convert_price_to_gbp( $minimum_rent, $search_form_currency );
1055 }
1056
1057 $meta_query = array(
1058 'key' => '_price_actual',
1059 'value' => ph_clean( floor( $minimum_rent ) ),
1060 'compare' => '>=',
1061 'type' => 'NUMERIC'
1062 );
1063 }
1064
1065 return $meta_query;
1066 }
1067
1068 /**
1069 * Returns a meta query to handle maximum rent
1070 *
1071 * @access public
1072 * @return array
1073 */
1074 public function maximum_rent_meta_query( ) {
1075
1076 $meta_query = array();
1077
1078 if (
1079 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
1080 isset( $_REQUEST['maximum_rent'] ) && $_REQUEST['maximum_rent'] != ''
1081 )
1082 {
1083 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
1084
1085 $maximum_rent = $_REQUEST['maximum_rent'];
1086 if ( $search_form_currency != 'GBP' )
1087 {
1088 // Convert $_REQUEST['maximum_rent'] to GBP
1089 $ph_countries = new PH_Countries();
1090
1091 $maximum_rent = $ph_countries->convert_price_to_gbp( $maximum_rent, $search_form_currency );
1092 }
1093
1094 $meta_query = array(
1095 'key' => '_price_actual',
1096 'value' => ph_clean( ceil( $maximum_rent ) ),
1097 'compare' => '<=',
1098 'type' => 'NUMERIC'
1099 );
1100 }
1101
1102 return $meta_query;
1103 }
1104
1105 /**
1106 * Returns a meta query to handle rent range
1107 *
1108 * @access public
1109 * @return array
1110 */
1111 public function rent_range_meta_query( ) {
1112
1113 $meta_query = array();
1114
1115 if (
1116 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
1117 isset( $_REQUEST['rent_range'] ) && $_REQUEST['rent_range'] != ''
1118 )
1119 {
1120 $explode_rent_range = explode("-", ph_clean($_REQUEST['rent_range']));
1121
1122 $search_form_currency = get_option( 'propertyhive_search_form_currency', 'GBP' );
1123
1124 if ( isset($explode_rent_range[0]) && $explode_rent_range[0] != '' )
1125 {
1126 $minimum_rent = $explode_rent_range[0];
1127 if ( $search_form_currency != 'GBP' )
1128 {
1129 // Convert $explode_rent_range[0] to GBP
1130 $ph_countries = new PH_Countries();
1131
1132 $minimum_rent = $ph_countries->convert_price_to_gbp( $minimum_rent, $search_form_currency );
1133 }
1134
1135 $meta_query[] = array(
1136 'key' => '_price_actual',
1137 'value' => sanitize_text_field( floor( $minimum_rent ) ),
1138 'compare' => '>=',
1139 'type' => 'NUMERIC'
1140 );
1141 }
1142 if ( isset($explode_rent_range[1]) && $explode_rent_range[1] != '' )
1143 {
1144 $maximum_rent = $explode_rent_range[1];
1145 if ( $search_form_currency != 'GBP' )
1146 {
1147 // Convert $explode_rent_range[1] to GBP
1148 $ph_countries = new PH_Countries();
1149
1150 $maximum_rent = $ph_countries->convert_price_to_gbp( $maximum_rent, $search_form_currency );
1151 }
1152
1153 $meta_query[] = array(
1154 'key' => '_price_actual',
1155 'value' => sanitize_text_field( ceil( $maximum_rent ) ),
1156 'compare' => '<=',
1157 'type' => 'NUMERIC'
1158 );
1159 }
1160 }
1161
1162 return $meta_query;
1163 }
1164
1165 /**
1166 * Returns a meta query to handle exact bedrooms
1167 *
1168 * @access public
1169 * @return array
1170 */
1171 public function bedrooms_meta_query( ) {
1172
1173 $meta_query = array();
1174
1175 if (
1176 (
1177 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1178 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1179 ) &&
1180 isset( $_REQUEST['bedrooms'] ) && $_REQUEST['bedrooms'] != ''
1181 )
1182 {
1183 $meta_query = array(
1184 'key' => '_bedrooms',
1185 'value' => ph_clean( $_REQUEST['bedrooms'] ),
1186 'compare' => '=',
1187 'type' => 'NUMERIC'
1188 );
1189 }
1190
1191 return $meta_query;
1192 }
1193
1194 /**
1195 * Returns a meta query to handle minimum bedrooms
1196 *
1197 * @access public
1198 * @return array
1199 */
1200 public function minimum_bedrooms_meta_query( ) {
1201
1202 $meta_query = array();
1203
1204 if (
1205 (
1206 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1207 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1208 ) &&
1209 isset( $_REQUEST['minimum_bedrooms'] ) && $_REQUEST['minimum_bedrooms'] != ''
1210 )
1211 {
1212 $meta_query = array(
1213 'key' => '_bedrooms',
1214 'value' => ph_clean( $_REQUEST['minimum_bedrooms'] ),
1215 'compare' => '>=',
1216 'type' => 'NUMERIC'
1217 );
1218 }
1219
1220 return $meta_query;
1221 }
1222
1223 /**
1224 * Returns a meta query to handle maximum bedrooms
1225 *
1226 * @access public
1227 * @return array
1228 */
1229 public function maximum_bedrooms_meta_query( ) {
1230
1231 $meta_query = array();
1232
1233 if (
1234 (
1235 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1236 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1237 ) &&
1238 isset( $_REQUEST['maximum_bedrooms'] ) && $_REQUEST['maximum_bedrooms'] != ''
1239 )
1240 {
1241 $meta_query = array(
1242 'key' => '_bedrooms',
1243 'value' => ph_clean( $_REQUEST['maximum_bedrooms'] ),
1244 'compare' => '<=',
1245 'type' => 'NUMERIC'
1246 );
1247 }
1248
1249 return $meta_query;
1250 }
1251
1252 /**
1253 * Returns a meta query to handle minimum bathrooms
1254 *
1255 * @access public
1256 * @return array
1257 */
1258 public function minimum_bathrooms_meta_query( ) {
1259
1260 $meta_query = array();
1261
1262 if (
1263 (
1264 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1265 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1266 ) &&
1267 isset( $_REQUEST['minimum_bathrooms'] ) && $_REQUEST['minimum_bathrooms'] != ''
1268 )
1269 {
1270 $meta_query = array(
1271 'key' => '_bathrooms',
1272 'value' => ph_clean( $_REQUEST['minimum_bathrooms'] ),
1273 'compare' => '>=',
1274 'type' => 'NUMERIC'
1275 );
1276 }
1277
1278 return $meta_query;
1279 }
1280
1281 /**
1282 * Returns a meta query to handle maximum bathrooms
1283 *
1284 * @access public
1285 * @return array
1286 */
1287 public function maximum_bathrooms_meta_query( ) {
1288
1289 $meta_query = array();
1290
1291 if (
1292 (
1293 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1294 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1295 ) &&
1296 isset( $_REQUEST['maximum_bathrooms'] ) && $_REQUEST['maximum_bathrooms'] != ''
1297 )
1298 {
1299 $meta_query = array(
1300 'key' => '_bathrooms',
1301 'value' => ph_clean( $_REQUEST['maximum_bathrooms'] ),
1302 'compare' => '<=',
1303 'type' => 'NUMERIC'
1304 );
1305 }
1306
1307 return $meta_query;
1308 }
1309
1310 /**
1311 * Returns a meta query to handle minimum reception rooms
1312 *
1313 * @access public
1314 * @return array
1315 */
1316 public function minimum_reception_rooms_meta_query( ) {
1317
1318 $meta_query = array();
1319
1320 if (
1321 (
1322 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1323 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1324 ) &&
1325 isset( $_REQUEST['minimum_reception_rooms'] ) && $_REQUEST['minimum_reception_rooms'] != ''
1326 )
1327 {
1328 $meta_query = array(
1329 'key' => '_reception_rooms',
1330 'value' => ph_clean( $_REQUEST['minimum_reception_rooms'] ),
1331 'compare' => '>=',
1332 'type' => 'NUMERIC'
1333 );
1334 }
1335
1336 return $meta_query;
1337 }
1338
1339 /**
1340 * Returns a meta query to handle maximum reception rooms
1341 *
1342 * @access public
1343 * @return array
1344 */
1345 public function maximum_reception_rooms_meta_query( ) {
1346
1347 $meta_query = array();
1348
1349 if (
1350 (
1351 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-sales') ||
1352 (isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings')
1353 ) &&
1354 isset( $_REQUEST['maximum_reception_rooms'] ) && $_REQUEST['maximum_reception_rooms'] != ''
1355 )
1356 {
1357 $meta_query = array(
1358 'key' => '_reception_rooms',
1359 'value' => ph_clean( $_REQUEST['maximum_reception_rooms'] ),
1360 'compare' => '<=',
1361 'type' => 'NUMERIC'
1362 );
1363 }
1364
1365 return $meta_query;
1366 }
1367
1368 /**
1369 * Returns a meta query to handle available date from
1370 *
1371 * @access public
1372 * @return array
1373 */
1374 public function available_date_from_meta_query( ) {
1375
1376 $meta_query = array();
1377
1378 if (
1379 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'residential-lettings' &&
1380 isset( $_REQUEST['available_date_from'] ) && $_REQUEST['available_date_from'] != ''
1381 )
1382 {
1383 $available_date = ph_clean($_REQUEST['available_date_from']);
1384 if ( strpos($available_date, '/') !== FALSE )
1385 {
1386 // it's been provided in the format dd/mm/yyyy
1387 $explode_available_date = explode("/", $available_date);
1388 if ( count($explode_available_date) == 3 )
1389 {
1390 $available_date = $explode_available_date[0] . '-' . $explode_available_date[1] . '-' . $explode_available_date[2];
1391 }
1392 }
1393 $meta_query = array(
1394 'key' => '_available_date',
1395 'value' => ph_clean( $available_date ),
1396 'compare' => '<=',
1397 );
1398 }
1399
1400 return $meta_query;
1401 }
1402
1403 /**
1404 * Returns a meta query to handle minimum floor area
1405 *
1406 * @access public
1407 * @return array
1408 */
1409 public function minimum_floor_area_meta_query( ) {
1410
1411 $meta_query = array();
1412
1413 if (
1414 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1415 isset( $_REQUEST['minimum_floor_area'] ) && $_REQUEST['minimum_floor_area'] != '' &&
1416 (
1417 !isset( $_REQUEST['maximum_floor_area'] ) ||
1418 ( isset( $_REQUEST['maximum_floor_area'] ) && $_REQUEST['maximum_floor_area'] == '' )
1419 )
1420 )
1421 {
1422 $meta_query = array(
1423 'key' => '_floor_area_from_sqft',
1424 'value' => ph_clean( $_REQUEST['minimum_floor_area'] ),
1425 'compare' => '>=',
1426 'type' => 'NUMERIC'
1427 );
1428 }
1429
1430 return $meta_query;
1431 }
1432
1433 /**
1434 * Returns a meta query to handle maximum floor area
1435 *
1436 * @access public
1437 * @return array
1438 */
1439 public function maximum_floor_area_meta_query( ) {
1440
1441 $meta_query = array();
1442
1443 if (
1444 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1445 isset( $_REQUEST['maximum_floor_area'] ) && $_REQUEST['maximum_floor_area'] != '' &&
1446 (
1447 !isset( $_REQUEST['minimum_floor_area'] ) ||
1448 ( isset( $_REQUEST['minimum_floor_area'] ) && $_REQUEST['minimum_floor_area'] == '' )
1449 )
1450 )
1451 {
1452 $meta_query = array(
1453 'key' => '_floor_area_to_sqft',
1454 'value' => ph_clean( $_REQUEST['maximum_floor_area'] ),
1455 'compare' => '<=',
1456 'type' => 'NUMERIC'
1457 );
1458 }
1459
1460 return $meta_query;
1461 }
1462
1463 /**
1464 * Returns a meta query to handle minimum AND maximum floor area
1465 *
1466 * @access public
1467 * @return array
1468 */
1469 public function minimum_maximum_floor_area_meta_query( ) {
1470
1471 $meta_query = array();
1472
1473 if (
1474 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1475 isset( $_REQUEST['minimum_floor_area'] ) && $_REQUEST['minimum_floor_area'] != '' &&
1476 isset( $_REQUEST['maximum_floor_area'] ) && $_REQUEST['maximum_floor_area'] != ''
1477 )
1478 {
1479 $meta_query[] = array(
1480 'key' => '_floor_area_from_sqft',
1481 'value' => ph_clean( $_REQUEST['maximum_floor_area'] ),
1482 'compare' => '<=',
1483 'type' => 'NUMERIC'
1484 );
1485 $meta_query[] = array(
1486 'key' => '_floor_area_to_sqft',
1487 'value' => ph_clean( $_REQUEST['minimum_floor_area'] ),
1488 'compare' => '>=',
1489 'type' => 'NUMERIC'
1490 );
1491 }
1492
1493 return $meta_query;
1494 }
1495
1496
1497 /**
1498 * Returns a meta query to handle floor area range
1499 *
1500 * @access public
1501 * @return array
1502 */
1503 public function floor_area_range_meta_query( ) {
1504
1505 $meta_query = array();
1506
1507 if (
1508 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1509 isset( $_REQUEST['floor_area_range'] ) && $_REQUEST['floor_area_range'] != ''
1510 )
1511 {
1512 $explode_floor_area_range = explode("-", ph_clean($_REQUEST['floor_area_range']));
1513
1514 if ( isset($explode_floor_area_range[0]) && $explode_floor_area_range[0] != '' )
1515 {
1516 $meta_query = array(
1517 'key' => '_floor_area_from_sqft',
1518 'value' => ph_clean( $explode_floor_area_range[0] ),
1519 'compare' => '>=',
1520 'type' => 'NUMERIC'
1521 );
1522 }
1523 if ( isset($explode_floor_area_range[1]) && $explode_floor_area_range[1] != '' )
1524 {
1525 $meta_query = array(
1526 'key' => '_floor_area_to_sqft',
1527 'value' => ph_clean( $explode_floor_area_range[1] ),
1528 'compare' => '<=',
1529 'type' => 'NUMERIC'
1530 );
1531 }
1532 }
1533
1534 return $meta_query;
1535 }
1536
1537 /**
1538 * Returns a meta query to handle commercial for sale or to rent
1539 *
1540 * @access public
1541 * @return array
1542 */
1543 public function commercial_for_sale_to_rent_meta_query( ) {
1544
1545 $meta_query = array();
1546
1547 if (
1548 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1549 isset( $_REQUEST['commercial_for_sale_to_rent'] ) && $_REQUEST['commercial_for_sale_to_rent'] == 'for_sale'
1550 )
1551 {
1552 $meta_query = array(
1553 'key' => '_for_sale',
1554 'value' => 'yes',
1555 'compare' => '=',
1556 );
1557 }
1558
1559 if (
1560 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1561 isset( $_REQUEST['commercial_for_sale_to_rent'] ) && $_REQUEST['commercial_for_sale_to_rent'] == 'to_rent'
1562 )
1563 {
1564 $meta_query = array(
1565 'key' => '_to_rent',
1566 'value' => 'yes',
1567 'compare' => '=',
1568 );
1569 }
1570
1571 return $meta_query;
1572 }
1573
1574 /**
1575 * Returns a meta query to handle commercial for sale
1576 *
1577 * @access public
1578 * @return array
1579 */
1580 public function commercial_for_sale_meta_query( ) {
1581
1582 $meta_query = array();
1583
1584 if (
1585 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1586 isset( $_REQUEST['commercial_for_sale'] ) && $_REQUEST['commercial_for_sale'] == '1'
1587 )
1588 {
1589 $meta_query = array(
1590 'key' => '_for_sale',
1591 'value' => 'yes',
1592 'compare' => '=',
1593 );
1594 }
1595
1596 return $meta_query;
1597 }
1598
1599 /**
1600 * Returns a meta query to handle commercial to rent
1601 *
1602 * @access public
1603 * @return array
1604 */
1605 public function commercial_to_rent_meta_query( ) {
1606
1607 $meta_query = array();
1608
1609 if (
1610 isset( $_REQUEST['department'] ) && $_REQUEST['department'] == 'commercial' &&
1611 isset( $_REQUEST['commercial_to_rent'] ) && $_REQUEST['commercial_to_rent'] == '1'
1612 )
1613 {
1614 $meta_query = array(
1615 'key' => '_to_rent',
1616 'value' => 'yes',
1617 'compare' => '=',
1618 );
1619 }
1620
1621 return $meta_query;
1622 }
1623
1624 /**
1625 * Returns a meta query to handle property negotiator
1626 *
1627 * @access public
1628 * @return array
1629 */
1630 public function negotiator_meta_query( ) {
1631
1632 $meta_query = array();
1633
1634 if ( isset( $_REQUEST['negotiator_id'] ) && $_REQUEST['negotiator_id'] != '' )
1635 {
1636 $meta_query = array(
1637 'key' => '_negotiator_id',
1638 'value' => (int)$_REQUEST['negotiator_id'],
1639 'compare' => '='
1640 );
1641 }
1642
1643 return $meta_query;
1644 }
1645
1646 /**
1647 * Returns a meta query to handle property office
1648 *
1649 * @access public
1650 * @param string $compare (default: 'IN')
1651 * @return array
1652 */
1653 public function office_meta_query( ) {
1654
1655 $meta_query = array();
1656
1657 if ( isset( $_REQUEST['officeID'] ) && $_REQUEST['officeID'] != '' )
1658 {
1659 $meta_query = array(
1660 'key' => '_office_id',
1661 'value' => ph_clean( (is_array($_REQUEST['officeID'])) ? $_REQUEST['officeID'] : array( $_REQUEST['officeID'] ) ),
1662 'compare' => 'IN'
1663 );
1664 }
1665
1666 return $meta_query;
1667 }
1668
1669 /**
1670 * Appends taxonomy queries to an array.
1671 * @access public
1672 * @param array $tax_query
1673 * @return array
1674 */
1675 public function get_tax_query( $tax_query = array() ) {
1676 if ( ! is_array( $tax_query ) )
1677 $tax_query = array();
1678
1679 if ( isset($_REQUEST) && !empty($_REQUEST) )
1680 {
1681 foreach ( $_REQUEST as $key => $value )
1682 {
1683 if ( taxonomy_exists($key) && isset( $_REQUEST[$key] ) && !empty($_REQUEST[$key]) && $this->taxonomy_allowed_for_department( $key ) )
1684 {
1685 $tax_query[] = array(
1686 'taxonomy' => $key,
1687 'terms' => ph_clean( (is_array($value)) ? $value : array( $value ) )
1688 );
1689 }
1690 }
1691 }
1692
1693 return array_filter( apply_filters( 'propertyhive_property_query_tax_query', $tax_query, $this ) );
1694 }
1695
1696 private function taxonomy_allowed_for_department( $taxonomy )
1697 {
1698 if ( isset( $_REQUEST['department'] ) && $_REQUEST['department'] != '' )
1699 {
1700 $department = ph_clean($_REQUEST['department']);
1701 }
1702 else
1703 {
1704 $department = get_option( 'propertyhive_primary_department', 'residential-sales' );
1705 }
1706
1707 switch ( $department )
1708 {
1709 case 'residential-sales':
1710 {
1711 if ( in_array( $taxonomy, array('commercial_property_type', 'commercial_tenure', 'furnished') ) )
1712 {
1713 return false;
1714 }
1715 break;
1716 }
1717 case 'residential-lettings':
1718 {
1719 if ( in_array( $taxonomy, array('commercial_property_type', 'commercial_tenure', 'tenure', 'sale_by', 'price_qualifier') ) )
1720 {
1721 return false;
1722 }
1723 break;
1724 }
1725 case 'commercial':
1726 {
1727 if ( in_array( $taxonomy, array('property_type', 'tenure', 'parking', 'outside_space', 'furnished') ) )
1728 {
1729 return false;
1730 }
1731 break;
1732 }
1733 }
1734
1735
1736 return true;
1737 }
1738
1739 /**
1740 * Layered Nav Init
1741 */
1742 public function layered_nav_init( ) {
1743
1744 }
1745
1746 /**
1747 * Layered Nav post filter
1748 *
1749 * @param array $filtered_posts
1750 * @return array
1751 */
1752 public function layered_nav_query( $filtered_posts ) {
1753
1754 }
1755
1756 /**
1757 * Price filter Init
1758 */
1759 public function price_filter_init() {
1760
1761 }
1762
1763 /**
1764 * Price Filter post filter
1765 *
1766 * @param array $filtered_posts
1767 * @return array
1768 */
1769 public function price_filter( $filtered_posts ) {
1770
1771 }
1772
1773 }
1774
1775 endif;
1776