| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class EPL_SEARCH { |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | private $query; |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | private $get_data = array(); |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | private $post_data = array(); |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | private $meta_query = array(); |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | private $tax_query = array(); |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | private $skip_field = array(); |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | private $post_type; |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | private $transaction_type = null; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | public $data; |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public $skip_fields; |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public $form_fields; |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 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: | |
| 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: | |
| 159: | |
| 160: | |
| 161: | |
| 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: | |
| 174: | |
| 175: | |
| 176: | |
| 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: | |
| 188: | |
| 189: | |
| 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: | |
| 208: | |
| 209: | protected function fields_to_skip() { |
| 210: | |
| 211: | $fields = array( 'action' ); |
| 212: | $this->skip_fields = apply_filters( 'epl_skip_search_fields', $fields, $this->get_data ); |
| 213: | } |
| 214: | |
| 215: | |
| 216: | |
| 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: | |
| 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: | |
| 239: | |
| 240: | protected function set_pagination() { |
| 241: | $paged = $this->query->get( 'paged', 1 ); |
| 242: | $this->query->set( 'paged', $paged ); |
| 243: | } |
| 244: | |
| 245: | |
| 246: | |
| 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: | |
| 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: | |
| 269: | |
| 270: | |
| 271: | |
| 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: | |
| 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: | |
| 302: | |
| 303: | protected function modify_fields() { |
| 304: | |
| 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: | |
| 315: | |
| 316: | protected function set_query() { |
| 317: | $this->set_meta_query(); |
| 318: | $this->set_tax_query(); |
| 319: | } |
| 320: | |
| 321: | |
| 322: | |
| 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: | |
| 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: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | protected function get_field_status() { |
| 348: | return epl_search_known_statuses( $this->get_data['property_status'] ); |
| 349: | } |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 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: | |
| 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: | |
| 439: | |
| 440: | |
| 441: | |
| 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: | |
| 457: | |
| 458: | |
| 459: | |
| 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: | |
| 471: | |
| 472: | |
| 473: | |
| 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: | |
| 482: | |
| 483: | |
| 484: | |
| 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: | |
| 518: | |
| 519: | |
| 520: | |
| 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: | |
| 548: | |
| 549: | |
| 550: | |
| 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: | |
| 579: | |
| 580: | |
| 581: | |
| 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: | |
| 600: | |
| 601: | |
| 602: | |
| 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: | |
| 611: | |
| 612: | |
| 613: | |
| 614: | public function get_posts() { |
| 615: | |
| 616: | return $this->query->get_posts(); |
| 617: | } |
| 618: | } |
| 619: | |