Skip to content

Instantly share code, notes, and snippets.

@Junex123
Created September 23, 2025 16:04
Show Gist options
  • Select an option

  • Save Junex123/fc77bd2ea50b37cad0f8397a6e9edd0f to your computer and use it in GitHub Desktop.

Select an option

Save Junex123/fc77bd2ea50b37cad0f8397a6e9edd0f to your computer and use it in GitHub Desktop.
<?php
/**
* Property Filter
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_RealEstate_Property_Filter extends WP_RealEstate_Abstract_Filter {
public static function init() {
add_action( 'pre_get_posts', array( __CLASS__, 'archive' ) );
add_action( 'pre_get_posts', array( __CLASS__, 'taxonomy' ) );
add_filter( 'wp-realestate-property-filter-query', array( __CLASS__, 'filter_query_property' ), 10, 2 );
add_filter( 'wp-realestate-property-query-args', array( __CLASS__, 'filter_query_args_property' ), 10, 2 );
}
public static function get_fields() {
return apply_filters( 'wp-realestate-default-property-filter-fields', array(
'center-location' => array(
'name' => __( 'Location', 'wp-realestate' ),
'field_call_back' => array( 'WP_RealEstate_Abstract_Filter', 'filter_field_input_location'),
'placeholder' => __( 'All Location', 'wp-realestate' ),
'show_distance' => true,
'toggle' => true,
'for_post_type' => 'property',
),
));
}
public static function archive($query) {
$suppress_filters = ! empty( $query->query_vars['suppress_filters'] ) ? $query->query_vars['suppress_filters'] : '';
if ( ! is_post_type_archive( 'property' ) || ! $query->is_main_query() || is_admin() || $query->query_vars['post_type'] != 'property' || $suppress_filters ) {
return;
}
$limit = wp_realestate_get_option('number_properties_per_page', 10);
$query_vars = &$query->query_vars;
$query_vars['posts_per_page'] = $limit;
$query->query_vars = $query_vars;
return self::filter_query( $query );
}
public static function taxonomy($query) {
$is_correct_taxonomy = false;
if ( is_tax( 'property_type' ) || is_tax( 'property_amenity' ) || is_tax( 'property_location' ) || is_tax( 'property_status' ) || is_tax( 'property_label' ) || is_tax( 'property_material' ) || apply_filters( 'wp-realestate-property-query-taxonomy', false ) ) {
$is_correct_taxonomy = true;
}
if ( ! $is_correct_taxonomy || ! $query->is_main_query() || is_admin() ) {
return;
}
$limit = wp_realestate_get_option('number_properties_per_page', 10);
$query_vars = $query->query_vars;
$query_vars['posts_per_page'] = $limit;
$query->query_vars = $query_vars;
return self::filter_query( $query );
}
public static function filter_query( $query = null, $params = array() ) {
global $wpdb, $wp_query;
if ( empty( $query ) ) {
$query = $wp_query;
}
if ( empty( $params ) ) {
$params = $_GET;
}
// Filter params
$params = apply_filters( 'wp_realestate_property_filter_params', $params );
// Initialize variables
$query_vars = $query->query_vars;
$query_vars = self::get_query_var_filter($query_vars, $params);
$query->query_vars = $query_vars;
// Meta query
$meta_query = self::get_meta_filter($params);
if ( $meta_query ) {
$query->set( 'meta_query', $meta_query );
}
// Tax query
$tax_query = self::get_tax_filter($params);
if ( $tax_query ) {
$query->set( 'tax_query', $tax_query );
}
return apply_filters('wp-realestate-property-filter-query', $query, $params);
}
public static function get_query_var_filter($query_vars, $params) {
$ids = null;
$query_vars = self::orderby($query_vars, $params);
// Property title
if ( ! empty( $params['filter-title'] ) ) {
global $wp_realestate_property_keyword;
$wp_realestate_property_keyword = sanitize_text_field( wp_unslash($params['filter-title']) );
$query_vars['s'] = sanitize_text_field( wp_unslash($params['filter-title']) );
add_filter( 'posts_search', array( __CLASS__, 'get_properties_keyword_search' ) );
}
$distance_ids = self::filter_by_distance($params, 'property');
if ( !empty($distance_ids) ) {
$ids = self::build_post_ids( $ids, $distance_ids );
}
if ( ! empty( $params['filter-author'] ) ) {
$query_vars['author'] = sanitize_text_field( wp_unslash($params['filter-author']) );
}
// Post IDs
if ( is_array( $ids ) && count( $ids ) > 0 ) {
$query_vars['post__in'] = $ids;
}
return $query_vars;
}
public static function get_meta_filter($params) {
$meta_query = array();
// price
if ( isset($params['filter-price-from']) && intval($params['filter-price-from']) >= 0 && isset($params['filter-price-to']) && intval($params['filter-price-to']) > 0) {
$price_from = WP_RealEstate_Price::convert_current_currency_to_default($params['filter-price-from']);
$price_to = WP_RealEstate_Price::convert_current_currency_to_default($params['filter-price-to']);
if ( $price_from == 0 ) {
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'price',
'value' => array( intval($price_from), intval($price_to) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'price',
'value' => '',
'compare' => '==',
),
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'price',
'value' => array( intval($price_from), intval($price_to) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
);
}
}
if ( ! empty( $params['filter-featured'] ) ) {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'featured',
'value' => 'on',
'compare' => '==',
);
}
// Rooms
if ( ! empty( $params['filter-rooms'] ) ) {
$value = sanitize_text_field( wp_unslash($params['filter-rooms']) );
if (strpos($value, '+') !== false) {
$value = rtrim($value, '+');
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'rooms',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC',
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'rooms',
'value' => $value,
'compare' => '=',
'type' => 'NUMERIC',
);
}
}
// Beds
if ( ! empty( $params['filter-beds'] ) ) {
$value = sanitize_text_field( wp_unslash($params['filter-beds']) );
if (strpos($value, '+') !== false) {
$value = rtrim($value, '+');
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'beds',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC',
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'beds',
'value' => $value,
'compare' => '=',
'type' => 'NUMERIC',
);
}
}
// Baths
if ( ! empty( $params['filter-baths'] ) ) {
$value = sanitize_text_field( wp_unslash($params['filter-baths']) );
if (strpos($value, '+') !== false) {
$value = rtrim($value, '+');
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'baths',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC',
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'baths',
'value' => $value,
'compare' => '=',
'type' => 'NUMERIC',
);
}
}
// Garages
if ( ! empty( $params['filter-garages'] ) ) {
$value = sanitize_text_field( wp_unslash($params['filter-garages']) );
if (strpos($value, '+') !== false) {
$value = rtrim($value, '+');
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'garages',
'value' => $value,
'compare' => '>=',
'type' => 'NUMERIC',
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'garages',
'value' => $value,
'compare' => '=',
'type' => 'NUMERIC',
);
}
}
if ( isset($params['filter-home_area-from']) && intval($params['filter-home_area-from']) >= 0 && isset($params['filter-home_area-to']) && intval($params['filter-home_area-to']) > 0) {
if ( $params['filter-home_area-from'] == 0 ) {
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'home_area',
'value' => array( intval($params['filter-home_area-from']), intval($params['filter-home_area-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'home_area',
'value' => '',
'compare' => '==',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'home_area',
'compare' => 'NOT EXISTS',
),
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'home_area',
'value' => array( intval($params['filter-home_area-from']), intval($params['filter-home_area-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
);
}
}
if ( isset($params['filter-lot_area-from']) && intval($params['filter-lot_area-from']) >= 0 && isset($params['filter-lot_area-to']) && intval($params['filter-lot_area-to']) > 0) {
if ( $params['filter-lot_area-from'] == 0 ) {
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'lot_area',
'value' => array( intval($params['filter-lot_area-from']), intval($params['filter-lot_area-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'lot_area',
'value' => '',
'compare' => '==',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'lot_area',
'compare' => 'NOT EXISTS',
),
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'lot_area',
'value' => array( intval($params['filter-lot_area-from']), intval($params['filter-lot_area-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
);
}
}
// Year built
if ( isset($params['filter-year_built-from']) && intval($params['filter-year_built-from']) >= 0 && isset($params['filter-year_built-to']) && intval($params['filter-year_built-to']) > 0) {
if ( $params['filter-year_built-from'] == 0 ) {
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'year_built',
'value' => array( intval($params['filter-year_built-from']), intval($params['filter-year_built-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'year_built',
'value' => '',
'compare' => '==',
),
array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'year_built',
'compare' => 'NOT EXISTS',
),
);
} else {
$meta_query[] = array(
'key' => WP_REALESTATE_PROPERTY_PREFIX . 'year_built',
'value' => array( intval($params['filter-year_built-from']), intval($params['filter-year_built-to']) ),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
);
}
}
return $meta_query;
}
public static function get_tax_filter($params) {
$tax_query = array();
if ( ! empty( $params['filter-status'] ) ) {
if ( is_array($params['filter-status']) ) {
$field = is_numeric( $params['filter-status'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-status'] ) ) );
if ( !empty($values) ) {
$tax_query[] = array(
'taxonomy' => 'property_status',
'field' => $field,
'terms' => array_values($values),
'compare' => 'IN',
);
}
} else {
$field = is_numeric( $params['filter-status'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_status',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-status']) ),
'compare' => '==',
);
}
}
if ( ! empty( $params['filter-type'] ) ) {
if ( is_array($params['filter-type']) ) {
$field = is_numeric( $params['filter-type'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-type'] ) ) );
if ( !empty($values) ) {
$tax_query[] = array(
'taxonomy' => 'property_type',
'field' => $field,
'terms' => array_values($values),
'compare' => 'IN',
);
}
} else {
$field = is_numeric( $params['filter-type'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_type',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-type']) ),
'compare' => '==',
);
}
}
if ( ! empty( $params['filter-location'] ) ) {
if ( is_array($params['filter-location']) ) {
$field = is_numeric( $params['filter-location'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-location'] ) ) );
// if ( !empty($values) ) {
// $tax_query[] = array(
// 'taxonomy' => 'property_location',
// 'field' => $field,
// 'terms' => array_values($values),
// 'compare' => 'IN',
// );
// }
if ( !empty($values) ) {
$location_tax_query = array('relation' => 'AND');
foreach ($values as $key => $value) {
$location_tax_query[] = array(
'taxonomy' => 'property_location',
'field' => $field,
'terms' => $value,
'compare' => '==',
);
}
$tax_query[] = $location_tax_query;
}
} else {
$field = is_numeric( $params['filter-location'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_location',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-location']) ),
'compare' => '==',
);
}
}
if ( ! empty( $params['filter-amenity'] ) ) {
if ( is_array($params['filter-amenity']) ) {
$field = is_numeric( $params['filter-amenity'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-amenity'] ) ) );
if ( !empty($values) ) {
$tax_query[] = array(
'taxonomy' => 'property_amenity',
'field' => $field,
'terms' => array_values($values),
'compare' => 'IN',
);
}
} else {
$field = is_numeric( $params['filter-amenity'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_amenity',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-amenity']) ),
'compare' => '==',
);
}
}
if ( ! empty( $params['filter-material'] ) ) {
if ( is_array($params['filter-material']) ) {
$field = is_numeric( $params['filter-material'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-material'] ) ) );
if ( !empty($values) ) {
$tax_query[] = array(
'taxonomy' => 'property_material',
'field' => $field,
'terms' => array_values($values),
'compare' => 'IN',
);
}
} else {
$field = is_numeric( $params['filter-material'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_material',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-material']) ),
'compare' => '==',
);
}
}
if ( ! empty( $params['filter-label'] ) ) {
if ( is_array($params['filter-label']) ) {
$field = is_numeric( $params['filter-label'][0] ) ? 'term_id' : 'slug';
$values = array_filter( array_map( 'sanitize_title', wp_unslash( $params['filter-label'] ) ) );
if ( !empty($values) ) {
$tax_query[] = array(
'taxonomy' => 'property_label',
'field' => $field,
'terms' => array_values($values),
'compare' => 'IN',
);
}
} else {
$field = is_numeric( $params['filter-label'] ) ? 'term_id' : 'slug';
$tax_query[] = array(
'taxonomy' => 'property_label',
'field' => $field,
'terms' => sanitize_text_field( wp_unslash($params['filter-label']) ),
'compare' => '==',
);
}
}
return $tax_query;
}
public static function get_properties_keyword_search( $search ) {
global $wpdb, $wp_realestate_property_keyword;
// Searchable Meta Keys: set to empty to search all meta keys.
$searchable_meta_keys = array(
WP_REALESTATE_PROPERTY_PREFIX.'address',
WP_REALESTATE_PROPERTY_PREFIX.'property_id',
);
$searchable_meta_keys = apply_filters( 'wp_realestate_searchable_meta_keys', $searchable_meta_keys );
// Set Search DB Conditions.
$conditions = array();
// Search Post Meta.
if ( apply_filters( 'wp_realestate_search_post_meta', true ) ) {
// Only selected meta keys.
if ( $searchable_meta_keys ) {
$conditions[] = "{$wpdb->posts}.ID IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ( '" . implode( "','", array_map( 'esc_sql', $searchable_meta_keys ) ) . "' ) AND meta_value LIKE '%" . esc_sql( $wp_realestate_property_keyword ) . "%' )";
} else {
// No meta keys defined, search all post meta value.
$conditions[] = "{$wpdb->posts}.ID IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%" . esc_sql( $wp_realestate_property_keyword ) . "%' )";
}
}
// Search taxonomy.
$conditions[] = "{$wpdb->posts}.ID IN ( SELECT object_id FROM {$wpdb->term_relationships} AS tr LEFT JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id LEFT JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE t.name LIKE '%" . esc_sql( $wp_realestate_property_keyword ) . "%' )";
$conditions = apply_filters( 'wp_realestate_search_conditions', $conditions, $wp_realestate_property_keyword );
if ( empty( $conditions ) ) {
return $search;
}
$conditions_str = implode( ' OR ', $conditions );
if ( ! empty( $search ) ) {
$search = preg_replace( '/^ AND /', '', $search );
$search = " AND ( {$search} OR ( {$conditions_str} ) )";
} else {
$search = " AND ( {$conditions_str} )";
}
remove_filter( 'posts_search', array( __CLASS__, 'get_properties_keyword_search' ) );
return $search;
}
public static function filter_query_property($query, $params) {
$query_vars = $query->query_vars;
$meta_query = self::filter_meta($query_vars, $params);
$query->set('meta_query', $meta_query);
return $query;
}
public static function filter_query_args_property($query_vars, $params) {
$meta_query = self::filter_meta($query_vars, $params);
$query_vars['meta_query'] = $meta_query;
return $query_vars;
}
public static function filter_meta($query_args, $params) {
if ( isset($query_args['meta_query']) ) {
$meta_query = $query_args['meta_query'];
} else {
$meta_query = array();
}
if ( empty($params) || !is_array($params) ) {
return $meta_query;
}
$filter_fields = WP_RealEstate_Custom_Fields::filter_custom_fields(array());
$cfielddate = [];
foreach ( $params as $key => $value ) {
if ( !empty($value) && strrpos( $key, 'filter-cfielddate-', -strlen( $key ) ) !== false ) {
$cfielddate[$key] = $value;
}
if ( !empty($value) && strrpos( $key, 'filter-cfield-', -strlen( $key ) ) !== false ) {
$custom_key = str_replace( 'filter-cfield-', '', $key );
if ( !empty($filter_fields[$custom_key]) ) {
$fielddata = $filter_fields[$custom_key];
$field_type = $fielddata['type'];
$meta_key = $custom_key;
switch ($field_type) {
case 'text':
case 'textarea':
case 'wysiwyg':
case 'number':
case 'url':
case 'email':
$meta_query[] = array(
'key' => $meta_key,
'value' => $value,
'compare' => 'LIKE',
);
break;
case 'radio':
case 'select':
case 'pw_select':
$meta_query[] = array(
'key' => $meta_key,
'value' => $value,
'compare' => '=',
);
break;
case 'checkbox':
$meta_query[] = array(
'key' => $meta_key,
'value' => 'on',
'compare' => '=',
);
break;
case 'pw_multiselect':
case 'multiselect':
case 'multicheck':
if ( is_array($value) ) {
$multi_meta = array( 'relation' => 'OR' );
foreach ($value as $val) {
$multi_meta[] = array(
'key' => $meta_key,
'value' => '"'.$val.'"',
'compare' => 'LIKE',
);
}
$meta_query[] = $multi_meta;
} else {
$meta_query[] = array(
'key' => $meta_key,
'value' => '"'.$value.'"',
'compare' => 'LIKE',
);
}
break;
}
}
}
}
if ( !empty($cfielddate) ) {
foreach ( $cfielddate as $key => $values ) {
if ( !empty($values) && is_array($values) && count($values) == 2 ) {
$custom_key = str_replace( 'filter-cfielddate-', '', $key );
if ( !empty($filter_fields[$custom_key]) ) {
$fielddata = $filter_fields[$custom_key];
$field_type = $fielddata['type'];
$meta_key = $custom_key;
if ( !empty($values['from']) && !empty($values['to']) ) {
$meta_query[] = array(
'key' => $meta_key,
'value' => array($values['from'], $values['to']),
'compare' => 'BETWEEN',
'type' => 'DATE',
);
} elseif ( !empty($values['from']) && empty($values['to']) ) {
$meta_query[] = array(
'key' => $meta_key,
'value' => $values['from'],
'compare' => '>',
'type' => 'DATE',
);
} elseif (empty($values['from']) && !empty($values['to']) ) {
$meta_query[] = array(
'key' => $meta_key,
'value' => $values['to'],
'compare' => '<',
'type' => 'DATE',
);
}
}
}
}
}
if ( !empty($params['filter-counter']) ) {
foreach ( $params['filter-counter'] as $key => $value ) {
if ( !empty($value) && strrpos( $key, 'filter-cfield-', -strlen( $key ) ) !== false ) {
$custom_key = str_replace( 'filter-cfield-', '', $key );
if ( !empty($filter_fields[$custom_key]) ) {
$fielddata = $filter_fields[$custom_key];
$field_type = $fielddata['type'];
$meta_key = $custom_key;
switch ($field_type) {
case 'text':
case 'textarea':
case 'wysiwyg':
case 'number':
case 'url':
case 'email':
$meta_query[] = array(
'key' => $meta_key,
'value' => $value,
'compare' => 'LIKE',
);
break;
case 'radio':
case 'select':
case 'pw_select':
$meta_query[] = array(
'key' => $meta_key,
'value' => $value,
'compare' => '=',
);
break;
case 'checkbox':
$meta_query[] = array(
'key' => $meta_key,
'value' => 'on',
'compare' => '=',
);
break;
case 'pw_multiselect':
case 'multiselect':
case 'multicheck':
if ( is_array($value) ) {
$multi_meta = array( 'relation' => 'OR' );
foreach ($value as $val) {
$multi_meta[] = array(
'key' => $meta_key,
'value' => '"'.$val.'"',
'compare' => 'LIKE',
);
}
$meta_query[] = $multi_meta;
} else {
$meta_query[] = array(
'key' => $meta_key,
'value' => '"'.$value.'"',
'compare' => 'LIKE',
);
}
break;
}
}
}
}
}
return $meta_query;
}
public static function display_filter_value($key, $value, $filters) {
$url = urldecode(WP_RealEstate_Mixes::get_full_current_url());
if ( is_array($value) ) {
$value = array_filter( array_map( 'sanitize_title', wp_unslash( $value ) ) );
} else {
$value = sanitize_text_field( wp_unslash($value) );
}
switch ($key) {
case 'filter-status':
self::render_filter_tax($key, $value, 'property_status', $url);
break;
case 'filter-location':
self::render_filter_tax($key, $value, 'property_location', $url);
break;
case 'filter-type':
self::render_filter_tax($key, $value, 'property_type', $url);
break;
case 'filter-amenity':
self::render_filter_tax($key, $value, 'property_amenity', $url);
break;
case 'filter-price':
if ( isset($value[0]) && isset($value[1]) ) {
$from = WP_RealEstate_Price::format_price($value[0], true);
$to = WP_RealEstate_Price::format_price($value[1], true);
$rm_url = self::remove_url_var($key . '-from=' . $value[0], $url);
$rm_url = self::remove_url_var($key . '-to=' . $value[1], $rm_url);
self::render_filter_result_item( $from.' - '.$to, $rm_url );
}
break;
case 'filter-distance':
if ( !empty($filters['filter-center-location']) ) {
$distance_type = apply_filters( 'wp_realestate_filter_distance_type', 'miles' );
$title = $value.' '.$distance_type;
$rm_url = self::remove_url_var( $key . '=' . $value, $url);
self::render_filter_result_item( $title, $rm_url );
}
break;
case 'filter-featured':
$title = esc_html__('Featured', 'wp-realestate');
$rm_url = self::remove_url_var($key . $key . '=' . $value, $url);
self::render_filter_result_item( $title, $rm_url );
break;
case 'filter-author':
$user_info = get_userdata($value);
if ( is_object($user_info) ) {
$title = $user_info->display_name;
} else {
$title = $value;
}
$rm_url = self::remove_url_var( $key . '=' . $value, $url);
self::render_filter_result_item( $title, $rm_url );
break;
case 'filter-orderby':
$orderby_options = apply_filters( 'wp-realestate-properties-orderby', array(
'menu_order' => esc_html__('Default', 'wp-realestate'),
'newest' => esc_html__('Newest', 'wp-realestate'),
'oldest' => esc_html__('Oldest', 'wp-realestate'),
'random' => esc_html__('Random', 'wp-realestate'),
));
$title = $value;
if ( !empty($orderby_options[$value]) ) {
$title = $orderby_options[$value];
}
$rm_url = self::remove_url_var( $key . '=' . $value, $url);
self::render_filter_result_item( $title, $rm_url );
break;
default:
if ( is_array($value) ) {
foreach ($value as $val) {
$rm_url = self::remove_url_var( $key . '[]=' . $val, $url);
self::render_filter_result_item( $val, $rm_url);
}
} else {
$rm_url = self::remove_url_var( $key . '=' . $value, $url);
self::render_filter_result_item( $value, $rm_url);
}
break;
}
}
public static function display_filter_value_simple($key, $value, $filters) {
if ( is_array($value) ) {
$value = array_filter( array_map( 'sanitize_title', wp_unslash( $value ) ) );
} else {
$value = sanitize_text_field( wp_unslash($value) );
}
switch ($key) {
case 'filter-status':
self::render_filter_tax_simple($key, $value, 'property_status', esc_html__('Status', 'wp-realestate'));
break;
case 'filter-location':
self::render_filter_tax_simple($key, $value, 'property_location', esc_html__('Location', 'wp-realestate'));
break;
case 'filter-type':
self::render_filter_tax_simple($key, $value, 'property_type', esc_html__('Type', 'wp-realestate'));
break;
case 'filter-amenity':
self::render_filter_tax_simple($key, $value, 'property_amenity', esc_html__('Tag', 'wp-realestate'));
break;
case 'filter-price':
if ( isset($value[0]) && isset($value[1]) ) {
$from = WP_RealEstate_Price::format_price($value[0]);
$to = WP_RealEstate_Price::format_price($value[1]);
self::render_filter_result_item_simple( $from.' - '.$to, esc_html__('Price', 'wp-realestate') );
}
break;
case 'filter-distance':
if ( !empty($filters['filter-center-location']) ) {
$distance_type = apply_filters( 'wp_realestate_filter_distance_type', 'miles' );
$title = $value.' '.$distance_type;
self::render_filter_result_item_simple( $title, esc_html__('Distance', 'wp-realestate') );
}
break;
case 'filter-featured':
$title = esc_html__('Yes', 'wp-realestate');
self::render_filter_result_item_simple( $title, esc_html__('Featured', 'wp-realestate') );
break;
case 'filter-author':
$user_info = get_userdata($value);
if ( is_object($user_info) ) {
$title = $user_info->display_name;
} else {
$title = $value;
}
self::render_filter_result_item_simple( $title, esc_html__('Author', 'wp-realestate') );
break;
case 'filter-orderby':
$orderby_options = apply_filters( 'wp-realestate-properties-orderby', array(
'menu_order' => esc_html__('Default', 'wp-realestate'),
'newest' => esc_html__('Newest', 'wp-realestate'),
'oldest' => esc_html__('Oldest', 'wp-realestate'),
'random' => esc_html__('Random', 'wp-realestate'),
));
$title = $value;
if ( !empty($orderby_options[$value]) ) {
$title = $orderby_options[$value];
}
self::render_filter_result_item_simple( $title, esc_html__('Orderby', 'wp-realestate') );
break;
default:
$meta_obj = WP_RealEstate_Property_Meta::get_instance(0);
$label_key = str_replace('filter-', '', $key);
$label_key = str_replace('filter-', '', $label_key);
$prefix = '';
if (preg_match("/-to/i", $key)) {
$prefix = esc_html__('to', 'wp-realestate');
$label_key = str_replace('-to', '', $label_key);
} elseif (preg_match("/-from/i", $key)) {
$prefix = esc_html__('from', 'wp-realestate');
$label_key = str_replace('-from', '', $label_key);
}
$label = $meta_obj->get_post_meta_title($label_key);
if ( empty($label) ) {
$label = $label_key;
}
if ( $prefix ) {
$label .= ' '.$prefix;
}
if ( is_array($value) ) {
foreach ($value as $val) {
self::render_filter_result_item_simple( $val, $label);
}
} else {
self::render_filter_result_item_simple( $value, $label);
}
break;
}
}
}
WP_RealEstate_Property_Filter::init();<?php
/**
* Price
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_RealEstate_Email {
public static $emails_vars;
public static function init() {
// Ajax endpoints.
add_action( 'wre_ajax_wp_realestate_ajax_contact_form', array(__CLASS__,'process_send_contact') );
// compatible handlers.
add_action( 'wp_ajax_wp_realestate_ajax_contact_form', array(__CLASS__,'process_send_contact') );
add_action( 'wp_ajax_nopriv_wp_realestate_ajax_contact_form', array(__CLASS__,'process_send_contact') );
}
public static function wp_mail( $author_email, $subject, $content, $headers, $attachments = null) {
if ( !preg_match( '%<html[>\s].*</html>%is', $content ) ) {
$header = apply_filters( 'wp-realestate-mail-html-header',
'<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset='.get_bloginfo( 'charset' ).'" />
<title>' . esc_html( $subject ) . '</title>
</head>
<body>
', $subject );
$footer = apply_filters( 'wp-realestate-mail-html-footer',
'</body>
</html>' );
$content = $header . wpautop( $content ) . $footer;
}
return wp_mail( $author_email, $subject, $content, $headers, $attachments );
}
public static function process_send_contact() {
$is_form_filled = ! empty( $_POST['email'] ) && ! empty( $_POST['name'] ) && ! empty( $_POST['message'] ) && ! empty( $_POST['post_id'] );
if ( WP_RealEstate_Recaptcha::is_recaptcha_enabled() ) {
$is_recaptcha_valid = array_key_exists( 'g-recaptcha-response', $_POST ) ? WP_RealEstate_Recaptcha::is_recaptcha_valid( sanitize_text_field( $_POST['g-recaptcha-response'] ) ) : false;
if ( !$is_recaptcha_valid ) {
$is_form_filled = false;
}
}
$post_type = get_post_type( $_POST['post_id'] );
$author_email = get_option('admin_email');
$subject = $content = '';
if ( $post_type == 'agent' ) {
$author_email = get_post_meta( $_POST['post_id'], WP_REALESTATE_AGENT_PREFIX.'email', true );
$subject = wp_realestate_get_option('contact_form_notice_subject');
$content = wp_realestate_get_option('contact_form_notice_content');
if ( empty($content) ) {
$content = self::get_email_default_content('contact_form_notice_content');
}
} elseif ( $post_type == 'agency' ) {
$author_email = get_post_meta( $_POST['post_id'], WP_REALESTATE_AGENCY_PREFIX.'email', true );
$subject = wp_realestate_get_option('contact_form_notice_subject');
$content = wp_realestate_get_option('contact_form_notice_content');
if ( empty($content) ) {
$content = self::get_email_default_content('contact_form_notice_content');
}
} elseif ( $post_type == 'property' ) {
$author_id = get_post_field ('post_author', $_POST['post_id'] );
if ( WP_RealEstate_User::is_agent($author_id) ) {
$agent_id = WP_RealEstate_User::get_agent_by_user_id($author_id);
$author_email = get_post_meta( $agent_id, WP_REALESTATE_AGENT_PREFIX.'email', true );
} elseif ( WP_RealEstate_User::is_agency($author_id) ) {
$agency_id = WP_RealEstate_User::get_agency_by_user_id($author_id);
$author_email = get_post_meta( $agency_id, WP_REALESTATE_AGENCY_PREFIX.'email', true );
} else {
$author_email = get_the_author_meta( 'user_email' , $author_id );
}
$property_title = get_the_title($_POST['post_id']);
$property_url = get_permalink($_POST['post_id']);
$subject = wp_realestate_get_option('property_contact_form_notice_subject');
$content = wp_realestate_get_option('property_contact_form_notice_content');
if ( empty($content) ) {
$content = self::get_email_default_content('property_contact_form_notice_content');
}
$subject = str_replace('{{property_title}}', $property_title, $subject);
$content = str_replace('{{property_title}}', $property_title, $content);
$content = str_replace('{{property_url}}', $property_url, $content);
}
if ( $is_form_filled && $author_email ) {
$email = sanitize_text_field( $_POST['email'] );
$phone = sanitize_text_field( $_POST['phone'] );
$user_name = sanitize_text_field( $_POST['name'] );
$message = sanitize_text_field( $_POST['message'] );
$subject = str_replace('{{user_name}}', $user_name, $subject);
$content = str_replace('{{user_name}}', $user_name, $content);
$content = str_replace('{{website_url}}', home_url(), $content);
$content = str_replace('{{website_name}}', get_bloginfo( 'name' ), $content);
$content = str_replace('{{email}}', $email, $content);
$content = str_replace('{{phone}}', $phone, $content);
$content = str_replace('{{message}}', $message, $content);
$headers = sprintf( "From: %s <%s>\r\n Content-type: text/html", $email, $email );
$result = false;
$result = WP_RealEstate_Email::wp_mail( $author_email, $subject, $content, $headers );
do_action('wp-realestate-after-process-send-contact', $author_email, $subject, $content, $headers);
if ( $result ) {
$return = array( 'status' => true, 'msg' => esc_html__('Your message has been successfully sent.', 'wp-realestate') );
} else {
$return = array( 'status' => false, 'msg' => esc_html__('An error occurred when sending an email.', 'wp-realestate') );
}
} else {
$return = array( 'status' => false, 'msg' => esc_html__('Form has been not filled correctly.', 'wp-realestate') );
}
echo wp_json_encode($return);
exit;
}
public static function emails_vars() {
self::$emails_vars = apply_filters( 'wp-realestate-emails-vars', array(
'admin_notice_add_new_listing' => array(
'subject' => array( 'property_title' ),
'content' => array( 'property_title', 'property_type', 'property_publish_date', 'property_expiry_date', 'property_featured', 'property_status', 'status', 'property_url', 'author', 'website_url', 'website_name' )
),
'admin_notice_updated_listing' => array(
'subject' => array( 'property_title' ),
'content' => array( 'property_title', 'property_type', 'property_publish_date', 'property_expiry_date', 'property_featured', 'property_status', 'status', 'property_url', 'author', 'website_url', 'website_name' )
),
'admin_notice_expiring_listing' => array(
'subject' => array( 'property_title' ),
'content' => array( 'property_title', 'property_type', 'property_publish_date', 'property_expiry_date', 'property_featured', 'property_status', 'status', 'property_url', 'website_url', 'website_name', 'property_admin_edit_url' )
),
'user_notice_expiring_listing' => array(
'subject' => array( 'property_title' ),
'content' => array( 'property_title', 'property_type', 'property_publish_date', 'property_expiry_date', 'property_featured', 'property_status', 'status', 'property_url', 'website_url', 'website_name', 'dashboard_url', 'my_properties' )
),
'saved_search_notice' => array(
'subject' => array( 'saved_search_title' ),
'content' => array( 'saved_search_title', 'properties_found', 'website_url', 'website_name', 'email_frequency_type', 'properties_saved_search_url' )
),
'contact_form_notice' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'message', 'email', 'phone', 'website_url', 'website_name' )
),
'property_contact_form_notice' => array(
'subject' => array( 'user_name', 'property_url' ),
'content' => array( 'user_name', 'property_title', 'property_url', 'message', 'email', 'phone', 'website_url', 'website_name' )
),
'user_register_auto_approve' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'user_email', 'login_url', 'website_url', 'website_name' )
),
'user_register_need_approve' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'user_email', 'approve_url', 'website_url', 'website_name' )
),
'user_register_approved' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'user_email', 'login_url', 'dashboard_url', 'website_url', 'website_name' )
),
'user_register_denied' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'user_email', 'website_url', 'website_name' )
),
'user_reset_password' => array(
'subject' => array( 'user_name' ),
'content' => array( 'user_name', 'user_email', 'new_password', 'website_url', 'website_name' )
),
));
return self::$emails_vars;
}
public static function display_email_vars($key, $type = 'subject') {
self::emails_vars();
$output = '';
if ( !empty(self::$emails_vars[$key][$type]) ) {
$i = 1;
foreach (self::$emails_vars[$key][$type] as $value) {
$output .= '{{'.$value.'}}'.($i < count(self::$emails_vars[$key][$type]) ? ', ' : '');
$i++;
}
}
return $output;
}
public static function render_email_vars($args, $key, $type = 'subject') {
self::emails_vars();
$output = wp_realestate_get_option($key.'_'.$type);
if ( empty($output) && $type == 'content' ) {
$output = self::get_email_default_content($key);
}
if ( !empty(self::$emails_vars[$key][$type]) ) {
$vars = self::$emails_vars[$key][$type];
foreach ($vars as $var) {
if ( strpos($output, '{{'.$var.'}}') !== false ) {
if ( isset($args[$var]) ) {
$value = $args[$var];
} elseif ( is_callable( array('WP_RealEstate_Email', $var) ) ) {
$value = call_user_func( array('WP_RealEstate_Email', $var), $args );
} else {
$value = apply_filters('wp-realestate-render-email-var-'.$var, '', $args);
}
$output = str_replace('{{'.$var.'}}', $value, $output);
}
}
}
return apply_filters( 'wp-realestate-render-emails-vars', $output, $args, $key, $type );
}
public static function get_email_default_content($key) {
$return = '';
if ( !empty($key) ) {
$name = 'html-'.str_replace('_', '-', $key);
if ( file_exists( WP_REALESTATE_PLUGIN_DIR . "includes/email-templates-default/{$name}.php" ) ) {
ob_start();
load_template(WP_REALESTATE_PLUGIN_DIR . "includes/email-templates-default/{$name}.php", false);
$return = ob_get_clean();
}
}
return trim($return);
}
public static function property_title($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->post_title) ) {
$output = $args['property']->post_title;
}
return $output;
}
public static function property_status($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$terms = get_the_terms( $args['property']->ID, 'property_status' );
if ( $terms ) {
$k = count( $terms );
foreach ($terms as $term) {
$k -= 1;
if ( $k == 0 ) {
$output .= '<a class="status-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>';
} else {
$output .= '<a class="status-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>, ';
}
}
}
}
return $output;
}
public static function property_type($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$terms = get_the_terms( $args['property']->ID, 'property_type' );
if ( $terms ) {
$k = count( $terms );
foreach ($terms as $term) {
$k -= 1;
if ( $k == 0 ) {
$output .= '<a class="type-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>';
} else {
$output .= '<a class="type-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>, ';
}
}
}
}
return $output;
}
public static function property_location($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$terms = get_the_terms( $args['property']->ID, 'property_location' );
if ( $terms ) {
$k = count( $terms );
foreach ($terms as $term) {
$k -= 1;
if ( $k == 0 ) {
$output .= '<a class="location-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>';
} else {
$output .= '<a class="location-property" href="'.get_term_link($term).'" >'.esc_html($term->name).'</a>, ';
}
}
}
}
return $output;
}
public static function property_publish_date($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$output = get_the_date(get_option('date_format'), $args['property']->ID);
}
return $output;
}
public static function property_expiry_date($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$meta_obj = WP_RealEstate_Property_Meta::get_instance($args['property']->ID);
$expiry_date = $meta_obj->get_post_meta( 'expiry_date' );
if ( $expiry_date ) {
$expiry_date = strtotime($expiry_date);
$output = date_i18n(get_option('date_format'), $expiry_date);
}
}
return $output;
}
public static function property_featured($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->ID) ) {
$meta_obj = WP_RealEstate_Property_Meta::get_instance($args['property']->ID);
$featured = $meta_obj->get_post_meta( 'featured' );
if ( $featured ) {
$output = esc_html__('Yes', 'wp-realestate');
} else {
$output = esc_html__('No', 'wp-realestate');
}
}
return $output;
}
public static function status($args) {
$output = '';
if ( isset($args['property']) && !empty($args['property']->post_status) ) {
$post_status = get_post_status_object( $args['property']->post_status );
if ( !empty($post_status->label) ) {
$output = $post_status->label;
} else {
$output = $post_status->post_status;
}
}
return $output;
}
public static function property_url($args) {
$output = '';
if ( !empty($args['property']) ) {
$output = get_permalink($args['property']);
}
return $output;
}
public static function website_url($args) {
$output = home_url();
return $output;
}
public static function website_name($args) {
$output = get_bloginfo( 'name' );
return $output;
}
public static function dashboard_url($args) {
$output = '';
$page_id = wp_realestate_get_option('user_dashboard_page_id');
if ( !empty($page_id) ) {
$output = get_permalink($page_id);
} else {
$output = home_url();
}
return $output;
}
public static function login_url($args) {
$page_id = wp_realestate_get_option('login_register_page_id');
if ( !empty($page_id) ) {
$output = get_permalink($page_id);
} else {
$output = home_url();
}
return $output;
}
public static function my_properties($args) {
$output = '';
$my_properties_page_id = wp_realestate_get_option('my_properties_page_id');
$output = get_permalink($my_properties_page_id);
return $output;
}
public static function property_admin_edit_url($args) {
$output = '';
if ( !empty($args['property']) ) {
$output = admin_url( sprintf( 'post.php?post=%d&amp;action=edit', $args['property']->ID ) );
}
return $output;
}
public static function author($args) {
$output = '';
if ( !empty($args['property']) && !empty($args['property']->post_author) ) {
$output = get_the_author_meta( 'display_name', $args['property']->post_author );
}
return $output;
}
public static function agent_name($args) {
$output = '';
if ( isset($args['agent']) && !empty($args['agent']->post_title) ) {
$output = $args['agent']->post_title;
}
return $output;
}
public static function approve_url($args) {
$output = '';
if ( isset($args['user_obj']) && !empty($args['user_obj']->ID) ) {
$approve_user_page_id = wp_realestate_get_option('approve_user_page_id');
$admin_url = get_permalink($approve_user_page_id);
$user_id = $args['user_obj']->ID;
$code = get_user_meta($user_id, 'account_approve_key', true);
$output = add_query_arg(array('action' => 'wp_realestate_approve_user', 'user_id' => $user_id, 'approve-key' => $code), $admin_url);
}
return $output;
}
public static function user_name($args) {
$output = '';
if ( isset($args['user_obj']) && !empty($args['user_obj']->display_name) ) {
$output = $args['user_obj']->display_name;
}
return $output;
}
public static function user_email($args) {
$output = '';
if ( isset($args['user_obj']) && !empty($args['user_obj']->user_email) ) {
$output = $args['user_obj']->user_email;
}
return $output;
}
}
WP_RealEstate_Email::init();<?php
/**
* Property Filter
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_RealEstate_Abstract_Filter {
public static function has_filter($params = null) {
if ( empty($params) ) {
$params = $_GET;
}
if ( ! empty( $params ) && is_array( $params ) ) {
foreach ( $params as $key => $value ) {
if ( strrpos( $key, 'filter-', -strlen( $key ) ) !== false ) {
return true;
}
}
}
return false;
}
public static function get_filters($params = null) {
$filters = array();
if ( empty($params) ) {
if ( ! empty( $_GET ) && is_array( $_GET ) ) {
$params = $_GET;
}
}
if ( ! empty( $params ) && is_array( $params ) ) {
foreach ( $params as $key => $value ) {
if ( strrpos( $key, 'filter-', -strlen( $key ) ) !== false && !empty($value) || $key == 'filter-price-from' ) {
$filters[$key] = $value;
}
}
if ( isset($filters['filter-price-from']) && isset($filters['filter-price-to']) ) {
$filters['filter-price'] = array($filters['filter-price-from'], $filters['filter-price-to'] );
unset($filters['filter-price-from']);
unset($filters['filter-price-to']);
}
if ( isset($filters['filter-center-latitude']) ) {
unset($filters['filter-center-latitude']);
}
if ( isset($filters['filter-center-longitude']) ) {
unset($filters['filter-center-longitude']);
}
if ( !empty($filters['filter-distance']) && !isset($filters['filter-center-location']) ) {
unset($filters['filter-distance']);
}
}
return $filters;
}
public static function orderby($query_vars, $params) {
// Order
if ( ! empty( $params['filter-orderby'] ) ) {
switch ( $params['filter-orderby'] ) {
case 'newest':
$query_vars['orderby'] = 'date';
$query_vars['order'] = 'DESC';
break;
case 'oldest':
$query_vars['orderby'] = 'date';
$query_vars['order'] = 'ASC';
break;
case 'random':
$query_vars['orderby'] = 'rand';
break;
case 'title':
$query_vars['orderby'] = 'title';
$query_vars['order'] = 'ASC';
case 'name':
$query_vars['orderby'] = 'name';
$query_vars['order'] = 'ASC';
break;
case 'published':
$query_vars['orderby'] = 'date';
break;
case 'price-lowest':
$query_vars['meta_key'] = WP_REALESTATE_PROPERTY_PREFIX . 'price';
$query_vars['orderby'] = 'meta_value_num';
$query_vars['order'] = 'ASC';
break;
case 'price-highest':
$query_vars['meta_key'] = WP_REALESTATE_PROPERTY_PREFIX . 'price';
$query_vars['orderby'] = 'meta_value_num';
$query_vars['order'] = 'DESC';
break;
}
} else {
$query_vars['order'] = 'DESC';
$query_vars['orderby'] = array(
'menu_order' => 'ASC',
'date' => 'DESC',
'ID' => 'DESC',
);
}
return $query_vars;
}
public static function build_post_ids( $haystack, array $ids ) {
if ( ! is_array( $haystack ) ) {
$haystack = array();
}
if ( is_array( $haystack ) && count( $haystack ) > 0 ) {
return array_intersect( $haystack, $ids );
} else {
$haystack = $ids;
}
return $haystack;
}
public static function filter_by_distance($params, $post_type = 'property') {
$distance_ids = array();
if ( ! empty( $params['filter-center-location'] ) && ! empty( $params['filter-center-latitude'] ) && ! empty( $params['filter-center-longitude'] ) ) {
$filter_distance = 50;
if ( ! empty( $params['filter-distance'] ) ) {
$filter_distance = $params['filter-distance'];
}
$post_ids = self::get_posts_by_distance( $params['filter-center-latitude'], $params['filter-center-longitude'], $filter_distance, $post_type );
if ( $post_ids ) {
foreach ( $post_ids as $post ) {
$distance_ids[] = $post->ID;
}
}
if ( empty( $distance_ids ) || ! $distance_ids ) {
$distance_ids = array(0);
}
}
return $distance_ids;
}
public static function get_posts_by_distance($latitude, $longitude, $distance, $post_type = 'property') {
global $wpdb;
$distance_type = apply_filters( 'wp_realestate_filter_distance_type', 'miles' );
$earth_distance = $distance_type == 'miles' ? 3959 : 6371;
$prefix = WP_REALESTATE_PROPERTY_PREFIX;
switch ($post_type) {
case 'agent':
$prefix = WP_REALESTATE_AGENT_PREFIX;
break;
case 'agency':
$prefix = WP_REALESTATE_AGENCY_PREFIX;
break;
case 'property':
default:
$prefix = WP_REALESTATE_PROPERTY_PREFIX;
break;
}
$post_ids = false;
$sql = $wpdb->prepare( "
SELECT $wpdb->posts.ID,
( %s * acos( cos( radians(%s) ) * cos( radians( latmeta.meta_value ) ) * cos( radians( longmeta.meta_value ) - radians(%s) ) + sin( radians(%s) ) * sin( radians( latmeta.meta_value ) ) ) ) AS distance, latmeta.meta_value AS latitude, longmeta.meta_value AS longitude
FROM $wpdb->posts
INNER JOIN $wpdb->postmeta AS latmeta ON $wpdb->posts.ID = latmeta.post_id
INNER JOIN $wpdb->postmeta AS longmeta ON $wpdb->posts.ID = longmeta.post_id
WHERE $wpdb->posts.post_type = %s AND $wpdb->posts.post_status = 'publish' AND latmeta.meta_key=%s AND longmeta.meta_key=%s
HAVING distance < %s
ORDER BY $wpdb->posts.menu_order ASC, distance ASC",
$earth_distance,
$latitude,
$longitude,
$latitude,
$post_type,
$prefix.'map_location_latitude',
$prefix.'map_location_longitude',
$distance
);
if ( apply_filters( 'wp_realestate_get_propertys_cache_results', false ) ) {
$to_hash = json_encode( array($earth_distance, $latitude, $longitude, $latitude, $distance, $post_type) );
$query_args_hash = 'wp_realestate_' . md5( $to_hash . WP_REALESTATE_PLUGIN_VERSION );
$post_ids = get_transient( $query_args_hash );
}
if ( ! $post_ids ) {
$post_ids = $wpdb->get_results( $sql, OBJECT_K );
if ( !empty($query_args_hash) ) {
set_transient( $query_args_hash, $post_ids, DAY_IN_SECONDS );
}
}
return $post_ids;
}
public static function filter_count($name, $term_id, $field) {
$args = array(
'post_type' => !empty($field['for_post_type']) ? $field['for_post_type'] : 'property',
'post_per_page' => 1,
'fields' => 'ids'
);
$params = array();
if ( WP_RealEstate_Abstract_Filter::has_filter() ) {
$params = $_GET;
}
if ( !empty($params[$name]) ) {
$values = $params[$name];
if ( is_array($values) ) {
$values[] = $term_id;
} else {
$values = $term_id;
}
$params[$name] = $values;
} else {
$params[$name] = $term_id;
}
$query_hash = md5( json_encode($args) ) .'-'. md5( json_encode($params) );
$cached_counts = (array) get_transient( 'wp_realestate_filter_counts' );
if ( ! isset( $cached_counts[ $query_hash ] ) ) {
$loop = WP_RealEstate_Query::get_posts($args, $params);
$cached_counts[ $query_hash ] = $loop->found_posts;
set_transient( 'wp_realestate_filter_counts', $cached_counts, DAY_IN_SECONDS );
}
return $cached_counts[ $query_hash ];
}
public static function get_term_name($term_id, $tax) {
$term = get_term($term_id, $tax);
if ( $term ) {
return $term->name;
}
return '';
}
public static function render_filter_tax($key, $value, $tax, $url) {
if ( is_array($value) ) {
foreach ($value as $val) {
$name = self::get_term_name($val, $tax);
$rm_url = self::remove_url_var($key . '[]=' . $val, $url);
self::render_filter_result_item($name, $rm_url);
}
} else {
$name = self::get_term_name($value, $tax);
$rm_url = self::remove_url_var($key . '=' . $value, $url);
self::render_filter_result_item($name, $rm_url);
}
}
public static function remove_url_var($url_var, $url) {
$str = "?" . $url_var;
if ( strpos($url, $str) !== false ) {
$rm_url = str_replace($url_var, "", $url);
$rm_url = str_replace('?&', "?", $rm_url);
} else {
$rm_url = str_replace("&" . $url_var, "", $url);
}
return $rm_url;
}
public static function render_filter_result_item($value, $rm_url) {
if ( $value ) {
?>
<li><a href="<?php echo esc_url($rm_url); ?>" ><span class="close-value">x</span><?php echo trim($value); ?></a></li>
<?php
}
}
public static function render_filter_tax_simple($key, $value, $tax, $label) {
if ( is_array($value) ) {
foreach ($value as $val) {
$name = self::get_term_name($val, $tax);
self::render_filter_result_item_simple($name, $label);
}
} else {
$name = self::get_term_name($value, $tax);
self::render_filter_result_item_simple($name, $label);
}
}
public static function render_filter_result_item_simple($value, $label) {
if ( $value ) {
?>
<li><strong class="text"><?php echo trim($label); ?>:</strong> <span class="value"><?php echo trim($value); ?></span></li>
<?php
}
}
// filter function
public static function filter_get_name($key, $field) {
$prefix = 'filter';
if ( !empty($field['filter-name-prefix']) ) {
$prefix = $field['filter-name-prefix'];
}
$name = $prefix.'-'.$key;
return apply_filters('wp-realestate-filter-get-name', $name, $key, $field);
}
public static function filter_field_input($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/text' );
}
public static function filter_date_field_input($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/text_date' );
}
public static function filter_field_input_location($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/text_location' );
}
public static function filter_field_input_distance($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : apply_filters( 'wp_realestate_filter_distance_default', 50 );
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/distance' );
}
public static function filter_field_year_built_range_slider($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
$min_max = WP_RealEstate_Query::get_min_max_meta_value(WP_REALESTATE_PROPERTY_PREFIX.'year_built', 'property');
if ( empty($min_max) ) {
return;
}
$min = floor( $min_max->min );
// $max = ceil( $min_max->max );
$max = date("Y");
if ( $min == $max ) {
return;
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/range_slider' );
}
public static function filter_field_property_price($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
$price_min = WP_RealEstate_Query::get_min_max_meta_value(WP_REALESTATE_PROPERTY_PREFIX.'price', 'property');
$price_max = WP_RealEstate_Query::get_min_max_meta_value(WP_REALESTATE_PROPERTY_PREFIX.'max_price', 'property');
if ( empty($price_min) && empty($price_max) ) {
return;
}
$min = $max = 0;
//$min = $price_min->min < $price_max->min ? $price_min->min : $price_max->min;
$max = $price_min->max > $price_max->max ? $price_min->max : $price_max->max;
if ( $min >= $max ) {
return;
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/price_range_slider' );
}
public static function filter_field_checkbox($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/checkbox' );
}
public static function filter_field_select($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
$options = array();
if ( !empty($field['options']) ) {
foreach ($field['options'] as $key => $value) {
$options[] = array('value' => $key, 'text' => $value);
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/select' );
}
public static function filter_field_taxonomy_radio_list($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
$options = array();
$query_args = array( 'hierarchical' => 1, 'hide_empty' => false );
$terms = get_terms($field['taxonomy'], $query_args);
if ( ! is_wp_error( $terms ) && ! empty ( $terms ) ) {
foreach ($terms as $term) {
$options[] = array(
'value' => $term->term_id,
'text' => $term->name
);
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/radios' );
}
public static function filter_field_taxonomy_check_list($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
$options = array();
$query_args = array( 'hierarchical' => 1, 'hide_empty' => false );
$terms = get_terms($field['taxonomy'], $query_args);
if ( ! is_wp_error( $terms ) && ! empty ( $terms ) ) {
foreach ($terms as $term) {
$options[] = array(
'value' => $term->term_id,
'text' => $term->name
);
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/check_list' );
}
public static function filter_field_taxonomy_select($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
$options = array();
$query_args = array( 'hierarchical' => 1, 'hide_empty' => false );
$terms = get_terms($field['taxonomy'], $query_args);
if ( ! is_wp_error( $terms ) && ! empty ( $terms ) ) {
foreach ($terms as $term) {
$options[] = array(
'value' => $term->term_id,
'text' => $term->name
);
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/select' );
}
public static function filter_field_taxonomy_hierarchical_radio_list($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/tax_radios' );
}
public static function filter_field_taxonomy_hierarchical_check_list($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/tax_check_list' );
}
public static function filter_field_taxonomy_hierarchical_select($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/tax_select' );
}
public static function filter_field_location_select($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
// $selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
$selected = '';
if ( isset( $_GET[$name] ) ) {
$selected = $_GET[$name];
} elseif ( !empty($field['taxonomy']) && is_tax($field['taxonomy']) ) {
global $wp_query;
$term = $wp_query->queried_object;
if ( isset( $term->term_id) ) {
$selected = $term->term_id;
}
}
$location_type = wp_realestate_get_option('location_multiple_fields', 'yes');
// echo $location_type; die;
if ( $location_type === 'no' ) {
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/tax_select' );
} else {
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/regions_select' );
}
}
public static function get_the_level($id, $type = 'property_location') {
return count( get_ancestors($id, $type) );
}
public static function hierarchical_tax_tree($catId, $depth, $input_name, $key, $field, $selected, $input_type = 'checkbox') {
$output = $return = '';
$next_depth = $depth + 1;
if ( empty($field['taxonomy']) ) {
return;
}
$terms = get_terms($field['taxonomy'], array( 'hierarchical' => 1, 'hide_empty' => false, 'parent' => $catId ));
if ( ! is_wp_error( $terms ) && ! empty ( $terms ) ) {
$_id = WP_RealEstate_Mixes::random_key();
foreach ($terms as $term) {
$checked = '';
if ( !empty($selected) ) {
if ( is_array($selected) ) {
if ( in_array($term->slug, $selected) || in_array($term->term_id, $selected) ) {
$checked = ' checked="checked"';
}
} elseif ( $term->slug == $selected || $term->term_id == $selected ) {
$checked = ' checked="checked"';
}
}
$output .= '<li class="list-item">';
$output .= '<div class="list-item-inner">';
if ( $input_type == 'checkbox' ) {
$output .= '<input id="'.esc_attr($term->slug.'-'.$_id).'" type="checkbox" name="'.esc_attr($input_name).'[]" value="'.esc_attr($term->term_id).'" '.$checked.'>';
} else {
$output .= '<input id="'.esc_attr($term->slug.'-'.$_id).'" type="radio" name="'.esc_attr($input_name).'" value="'.esc_attr($term->term_id).'" '.$checked.'>';
}
$output .= '<label for="'.esc_attr($term->slug.'-'.$_id).'">'.wp_kses_post($term->name).'</label>';
$child_output = self::hierarchical_tax_tree($term->term_id, $next_depth, $input_name, $key, $field, $selected, $input_type);
if ( $child_output ) {
$output .= '<span class="caret"></span>';
}
$output .= '</div>';
$output .= $child_output;
$output .= '</li>';
}
if ( $output ) {
$return = '<ul class="terms-list circle-check level-'.$depth.'">'.$output.'</ul>';
}
}
return $return;
}
public static function hierarchical_tax_option_tree($catId, $depth, $input_name, $key, $field, $selected ){
$output = $show_depth = '';
$next_depth = $depth + 1;
for ($i = 1; $i <= $depth; $i++) {
$show_depth .= '-';
}
if ( empty($field['taxonomy']) ) {
return;
}
$terms = get_terms($field['taxonomy'], array( 'hierarchical' => 1, 'hide_empty' => false, 'parent' => $catId ));
if ( ! is_wp_error( $terms ) && ! empty ( $terms ) ) {
foreach ($terms as $term) {
$selected_html = '';
if ( !empty($selected) ) {
if ( is_array($selected) ) {
if ( in_array($term->slug, $selected) || in_array($term->term_id, $selected) ) {
$selected_html = ' selected="selected"';
}
} elseif ( $term->slug == $selected || $term->term_id == $selected ) {
$selected_html = ' selected="selected"';
}
}
$output .= '<option value="'.esc_attr($term->term_id).'" '.$selected_html.'>';
$output .= $show_depth.' '.wp_kses_post($term->name);
$output .= '</option>';
$output .= self::hierarchical_tax_option_tree($term->term_id, $next_depth, $input_name, $key, $field, $selected);
}
}
return $output;
}
public static function filter_field_min_max_input($instance, $args, $key, $field) {
$min_name = 'filter-'.$key.'-from';
$max_name = 'filter-'.$key.'-to';
$min_selected = !empty( $_GET[$min_name] ) ? $_GET[$min_name] : '';
$max_selected = !empty( $_GET[$max_name] ) ? $_GET[$max_name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/min_max_text' );
}
public static function filter_field_range_slider($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
$min_max = WP_RealEstate_Query::get_min_max_meta_value($field['id'], 'property');
if ( empty($min_max) ) {
return;
}
//$min = floor( $min_max->min );
$min = 0;
$max = ceil( $min_max->max );
if ( $min == $max ) {
return;
}
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/range_slider' );
}
public static function filter_field_number_select($instance, $args, $key, $field) {
$name = self::filter_get_name($key, $field);
$selected = !empty( $_GET[$name] ) ? $_GET[$name] : '';
include WP_RealEstate_Template_Loader::locate( 'widgets/filter-fields/number_select' );
}
}
<?php
/**
* Price
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_RealEstate_Query {
public static function get_posts( $params = array(), $filter_params = null ) {
$params = wp_parse_args( $params, array(
'post_type' => 'property',
'post_per_page' => -1,
'paged' => 1,
'post_status' => 'publish',
'post__in' => array(),
'post__not_in' => array(),
'fields' => null, // ids
'author' => null,
'author__in' => null,
'meta_query' => null,
'tax_query' => null,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
'ID' => 'DESC',
),
'order' => 'DESC',
's' => ''
));
extract($params);
$query_args = array(
'post_type' => $post_type,
'paged' => $paged,
'posts_per_page' => $post_per_page,
'post_status' => $post_status,
'orderby' => $orderby,
'order' => $order,
);
if ( !empty($post__in) ) {
$query_args['post__in'] = $post__in;
}
if ( !empty($post__not_in) ) {
$query_args['post__not_in'] = $post__not_in;
}
if ( !empty($s) ) {
$query_args['s'] = $s;
}
if ( !empty($fields) ) {
$query_args['fields'] = $fields;
}
if ( !empty($author) ) {
$query_args['author'] = $author;
}
if ( !empty($author__in) ) {
$query_args['author__in'] = $author__in;
}
if ( !empty($meta_query) ) {
$query_args['meta_query'] = $meta_query;
}
if ( !empty($tax_query) ) {
$query_args['tax_query'] = $tax_query;
}
if ( $filter_params != null ) {
// TODO: apply filter params
switch ($post_type) {
case 'property':
$query_args = WP_RealEstate_Property_Filter::get_query_var_filter($query_args, $filter_params);
// Meta query
$meta_query = WP_RealEstate_Property_Filter::get_meta_filter($filter_params);
if ( $meta_query ) {
$query_args['meta_query'] = $meta_query;
}
// Tax query
$tax_query = WP_RealEstate_Property_Filter::get_tax_filter($filter_params);
if ( $tax_query ) {
$query_args['tax_query'] = $tax_query;
}
break;
case 'agent':
$query_args = WP_RealEstate_Agent_Filter::get_query_var_filter($query_args, $filter_params);
// Meta query
$meta_query = WP_RealEstate_Agent_Filter::get_meta_filter($filter_params);
if ( $meta_query ) {
$query_args['meta_query'] = $meta_query;
}
// Tax query
$tax_query = WP_RealEstate_Agent_Filter::get_tax_filter($filter_params);
if ( $tax_query ) {
$query_args['tax_query'] = $tax_query;
}
break;
case 'agency':
$query_args = WP_RealEstate_Agency_Filter::get_query_var_filter($query_args, $filter_params);
// Meta query
$meta_query = WP_RealEstate_Agency_Filter::get_meta_filter($filter_params);
if ( $meta_query ) {
$query_args['meta_query'] = $meta_query;
}
// Tax query
$tax_query = WP_RealEstate_Agency_Filter::get_tax_filter($filter_params);
if ( $tax_query ) {
$query_args['tax_query'] = $tax_query;
}
break;
}
$query_args = apply_filters('wp-realestate-'.$post_type.'-query-args', $query_args, $filter_params);
}
$query = new WP_Query( $query_args );
return $query;
}
public static function get_agents( $params = array() ) {
$params = wp_parse_args( $params, array(
'post_per_page' => -1,
'post_status' => 'publish',
'ids' => array()
));
extract($params);
$query_args = array(
'post_type' => 'agent',
'posts_per_page' => $post_per_page,
'post_status' => $post_status,
);
if ( !empty($ids) ) {
$query_args['post__in'] = $ids;
}
return new WP_Query( $query_args );
}
public static function get_property_location_name( $post_id = null, $separator = ',' ) {
static $property_locations;
if ( null == $post_id ) {
$post_id = get_the_ID();
}
if ( ! empty( $property_locations[ $post_id ] ) ) {
return $property_locations[ $post_id ];
}
$locations = wp_get_post_terms( $post_id, 'property_location', array(
'orderby' => 'parent',
'order' => 'DESC',
) );
if ( is_array( $locations ) && count( $locations ) > 0 ) {
$output = '';
$locations = array_reverse( $locations );
foreach ( $locations as $key => $location ) {
$output .= '<a href="' . get_term_link( $location, 'property_location' ). '">' . $location->name . '</a>';
if ( array_key_exists( $key + 1, $locations ) ) {
$output .= ' <span class="separator">' . $separator . '</span> ';
}
}
$property_locations[ $post_id ] = $output;
return $output;
}
return false;
}
public static function get_min_max_meta_value( $key, $post_type = 'property', $meta_condition = array() ){
global $wpdb;
$cash_key = md5($key.'_'.$post_type.json_encode($meta_condition));
$results = wp_cache_get($cash_key);
if ($results === false) {
$sql = "SELECT min( CAST( postmeta.meta_value AS UNSIGNED ) ) as min, max( CAST( postmeta.meta_value AS UNSIGNED ) ) as max FROM {$wpdb->posts} ";
$sql .= " LEFT JOIN {$wpdb->postmeta} as postmeta ON {$wpdb->posts}.ID = postmeta.post_id ";
$sql .= " WHERE {$wpdb->posts}.post_type = %s
AND {$wpdb->posts}.post_status = 'publish'
AND postmeta.meta_key='%s' ";
if ( !empty($meta_condition) ) {
$sql .= " AND {$wpdb->posts}.ID IN (
SELECT {$wpdb->posts}.ID
FROM {$wpdb->posts}
LEFT JOIN {$wpdb->postmeta} as pmeta ON {$wpdb->posts}.ID = pmeta.post_id
WHERE {$wpdb->posts}.post_type = '%s'
AND {$wpdb->posts}.post_status = 'publish'
AND pmeta.meta_key='%s' AND pmeta.meta_value='%s'
) ";
$query = $wpdb->prepare( $sql, $post_type, $key, $post_type, $meta_condition['key'], $meta_condition['value'] );
} else {
$query = $wpdb->prepare( $sql, $post_type, $key);
}
$results = $wpdb->get_row( $query );
wp_cache_set( $cash_key, $results, '', DAY_IN_SECONDS );
}
return $results;
}
public static function get_agents_properties( $args = array() ) {
$args = wp_parse_args( $args, array(
'agent_ids' => array(),
'post_per_page' => -1,
'paged' => 1,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
'ID' => 'DESC',
),
'order' => 'DESC',
'fields' => ''
));
extract($args);
if ( empty($agent_ids) ) {
return false;
}
$user_ids = array(0);
foreach ($agent_ids as $agent_id) {
$user_id = WP_RealEstate_User::get_user_by_agent_id($agent_id);
if ( $user_id ) {
$user_ids[] = $user_id;
}
}
$query_args = array(
'post_type' => 'property',
'posts_per_page' => $post_per_page,
'paged' => $paged,
'orderby' => $orderby,
'order' => $order,
'fields' => $fields,
'author__in' => $user_ids
);
return new WP_Query( $query_args );
}
public static function get_agencies( $count = -1 ) {
$args = array(
'post_type' => 'agency',
'posts_per_page' => $count,
);
return new WP_Query( $args );
}
public static function get_agency_agents( $post_id = null, $args = array() ) {
if ( null == $post_id ) {
$post_id = get_the_ID();
}
$args = wp_parse_args( $args, array(
'post_per_page' => -1,
'paged' => 1,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
'ID' => 'DESC',
),
'order' => 'DESC',
'fields' => ''
));
extract($args);
$agents = WP_RealEstate_Agency::get_post_meta($post_id, 'agents', false);
if ( $agents ) {
$args = array(
'post_type' => 'agent',
'posts_per_page' => $post_per_page,
'paged' => $paged,
'orderby' => $orderby,
'order' => $order,
'fields' => $fields,
'post__in' => $agents
);
return new WP_Query( $args );
}
return false;
}
}
<?php
/**
* Template Loader
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WP_RealEstate_Template_Loader {
/**
* Initialize template loader
*
* @access public
* @return void
*/
public static function init() {
add_filter( 'template_include', array( __CLASS__, 'templates' ) );
}
/**
* Default templates
*
* @access public
* @param $template
* @return string
* @throws Exception
*/
public static function templates( $template ) {
global $wp_query;
$post_type = get_post_type();
if ( is_tax('property_status') || is_tax('property_amenity') || is_tax('property_label') || is_tax('property_location') || is_tax('property_material') || is_tax('property_type') ) {
return self::locate( 'archive-property' );
} elseif ( is_tax('agency_location') || is_tax('agency_category') ) {
return self::locate( 'archive-agency' );
} elseif ( is_tax('agent_location') || is_tax('agent_category') ) {
return self::locate( 'archive-agent' );
} elseif ( !empty($wp_query->query_vars['post_type']) || $post_type ) {
$custom_post_types = array( 'property', 'agent', 'agency' );
if ( in_array( $post_type, $custom_post_types ) ) {
if ( is_archive() ) {
return self::locate( 'archive-' . $post_type );
}
if ( is_single() ) {
return self::locate( 'single-' . $post_type );
}
} elseif ( in_array( $wp_query->query_vars['post_type'], $custom_post_types ) ) {
$post_type = $wp_query->query_vars['post_type'];
if ( is_archive() ) {
return self::locate( 'archive-' . $post_type );
}
if ( is_single() ) {
return self::locate( 'single-' . $post_type );
}
}
}
return $template;
}
/**
* Gets template path
*
* @access public
* @param $name
* @param $plugin_dir
* @return string
* @throws Exception
*/
public static function locate( $name, $plugin_dir = WP_REALESTATE_PLUGIN_DIR ) {
$template = '';
$theme_folder_name = apply_filters( 'wp-realestate-theme-folder-name', 'wp-realestate' );
// Current theme base dir
if ( ! empty( $name ) ) {
$template = locate_template( array("{$name}.php") );
}
// Child theme
if ( ! $template && ! empty( $name ) && file_exists( get_stylesheet_directory() . "/".$theme_folder_name."/{$name}.php" ) ) {
$template = get_stylesheet_directory() . "/".$theme_folder_name."/{$name}.php";
}
// Original theme
if ( ! $template && ! empty( $name ) && file_exists( get_template_directory() . "/".$theme_folder_name."/{$name}.php" ) ) {
$template = get_template_directory() . "/".$theme_folder_name."/{$name}.php";
}
// Plugin
if ( ! $template && ! empty( $name ) && file_exists( $plugin_dir . "templates/{$name}.php" ) ) {
$template = $plugin_dir . "/templates/{$name}.php";
}
// Nothing found
if ( empty( $template ) ) {
throw new Exception( "Template /templates/{$name}.php in plugin dir {$plugin_dir} not found." );
}
return $template;
}
/**
* Loads template content
*
* @param string $name
* @param array $args
* @param string $plugin_dir
* @return string
* @throws Exception
*/
public static function get_template_part( $name, $args = array(), $plugin_dir = WP_REALESTATE_PLUGIN_DIR ) {
if ( is_array( $args ) && count( $args ) > 0 ) {
extract( $args, EXTR_SKIP );
}
$path = self::locate( $name, $plugin_dir );
ob_start();
if ( $path ) {
include $path;
}
$result = ob_get_contents();
ob_end_clean();
return $result;
}
}
WP_RealEstate_Template_Loader::init();<?php
/**
* Permalink Settings
*
* @package wp-realestate
* @author Habq
* @license GNU General Public License, version 3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WP_RealEstate_Permalink_Settings {
public static function init() {
add_action('admin_init', array( __CLASS__, 'setup_fields') );
add_action('admin_init', array( __CLASS__, 'settings_save') );
}
public static function setup_fields() {
add_settings_field(
'wp_realestate_property_base_slug',
__( 'Property base', 'wp-realestate' ),
array( __CLASS__, 'property_base_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_type_slug',
__( 'Property type base', 'wp-realestate' ),
array( __CLASS__, 'property_type_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_location_slug',
__( 'Property location base', 'wp-realestate' ),
array( __CLASS__, 'property_location_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_amenity_slug',
__( 'Property amenity base', 'wp-realestate' ),
array( __CLASS__, 'property_amenity_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_status_slug',
__( 'Property status base', 'wp-realestate' ),
array( __CLASS__, 'property_status_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_label_slug',
__( 'Property label base', 'wp-realestate' ),
array( __CLASS__, 'property_label_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_property_material_slug',
__( 'Property material base', 'wp-realestate' ),
array( __CLASS__, 'property_material_slug_input' ),
'permalink',
'optional'
);
//
add_settings_field(
'wp_realestate_property_archive_slug',
__( 'Property archive page', 'wp-realestate' ),
array( __CLASS__, 'property_archive_slug_input' ),
'permalink',
'optional'
);
// agent
add_settings_field(
'wp_realestate_agent_base_slug',
__( 'Agent base', 'wp-realestate' ),
array( __CLASS__, 'agent_base_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agent_category_slug',
__( 'Agent category base', 'wp-realestate' ),
array( __CLASS__, 'agent_category_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agent_location_slug',
__( 'Agent location base', 'wp-realestate' ),
array( __CLASS__, 'agent_location_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agent_archive_slug',
__( 'Agent archive page', 'wp-realestate' ),
array( __CLASS__, 'agent_archive_slug_input' ),
'permalink',
'optional'
);
// agency
add_settings_field(
'wp_realestate_agency_base_slug',
__( 'Agency base', 'wp-realestate' ),
array( __CLASS__, 'agency_base_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agency_category_slug',
__( 'Agency category base', 'wp-realestate' ),
array( __CLASS__, 'agency_category_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agency_location_slug',
__( 'Agency location base', 'wp-realestate' ),
array( __CLASS__, 'agency_location_slug_input' ),
'permalink',
'optional'
);
add_settings_field(
'wp_realestate_agency_archive_slug',
__( 'Agency archive page', 'wp-realestate' ),
array( __CLASS__, 'agency_archive_slug_input' ),
'permalink',
'optional'
);
}
public static function property_base_slug_input() {
$value = get_option('wp_realestate_property_base_slug');
?>
<input name="wp_realestate_property_base_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property', 'wp-realestate' ); ?>" />
<?php
}
public static function property_amenity_slug_input() {
$value = get_option('wp_realestate_property_amenity_slug');
?>
<input name="wp_realestate_property_amenity_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-amenity', 'wp-realestate' ); ?>" />
<?php
}
public static function property_status_slug_input() {
$value = get_option('wp_realestate_property_status_slug');
?>
<input name="wp_realestate_property_status_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-status', 'wp-realestate' ); ?>" />
<?php
}
public static function property_label_slug_input() {
$value = get_option('wp_realestate_property_label_slug');
?>
<input name="wp_realestate_property_label_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-label', 'wp-realestate' ); ?>" />
<?php
}
public static function property_material_slug_input() {
$value = get_option('wp_realestate_property_material_slug');
?>
<input name="wp_realestate_property_material_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-material', 'wp-realestate' ); ?>" />
<?php
}
public static function property_type_slug_input() {
$value = get_option('wp_realestate_property_type_slug');
?>
<input name="wp_realestate_property_type_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-type', 'wp-realestate' ); ?>" />
<?php
}
public static function property_location_slug_input() {
$value = get_option('wp_realestate_property_location_slug');
?>
<input name="wp_realestate_property_location_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'property-location', 'wp-realestate' ); ?>" />
<?php
}
public static function property_archive_slug_input() {
$value = get_option('wp_realestate_property_archive_slug');
?>
<input name="wp_realestate_property_archive_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'properties', 'wp-realestate' ); ?>" />
<?php
}
// agent
public static function agent_base_slug_input() {
$value = get_option('wp_realestate_agent_base_slug');
?>
<input name="wp_realestate_agent_base_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agent', 'wp-realestate' ); ?>" />
<?php
}
public static function agent_category_slug_input() {
$value = get_option('wp_realestate_agent_category_slug');
?>
<input name="wp_realestate_agent_category_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agent-category', 'wp-realestate' ); ?>" />
<?php
}
public static function agent_location_slug_input() {
$value = get_option('wp_realestate_agent_location_slug');
?>
<input name="wp_realestate_agent_location_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agent-location', 'wp-realestate' ); ?>" />
<?php
}
public static function agent_archive_slug_input() {
$value = get_option('wp_realestate_agent_archive_slug');
?>
<input name="wp_realestate_agent_archive_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agents', 'wp-realestate' ); ?>" />
<?php
}
// agency
public static function agency_base_slug_input() {
$value = get_option('wp_realestate_agency_base_slug');
?>
<input name="wp_realestate_agency_base_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agency', 'wp-realestate' ); ?>" />
<?php
}
public static function agency_category_slug_input() {
$value = get_option('wp_realestate_agency_category_slug');
?>
<input name="wp_realestate_agency_category_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agency-category', 'wp-realestate' ); ?>" />
<?php
}
public static function agency_location_slug_input() {
$value = get_option('wp_realestate_agency_location_slug');
?>
<input name="wp_realestate_agency_location_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agency-location', 'wp-realestate' ); ?>" />
<?php
}
public static function agency_archive_slug_input() {
$value = get_option('wp_realestate_agency_archive_slug');
?>
<input name="wp_realestate_agency_archive_slug" type="text" class="regular-text code" value="<?php echo esc_attr( $value ); ?>" placeholder="<?php esc_attr_e( 'agencies', 'wp-realestate' ); ?>" />
<?php
}
public static function settings_save() {
if ( ! is_admin() ) {
return;
}
if ( isset( $_POST['permalink_structure'] ) ) {
if ( function_exists( 'switch_to_locale' ) ) {
switch_to_locale( get_locale() );
}
if ( isset($_POST['wp_realestate_property_base_slug']) ) {
update_option( 'wp_realestate_property_base_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_base_slug']) );
}
if ( isset($_POST['wp_realestate_property_amenity_slug']) ) {
update_option( 'wp_realestate_property_amenity_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_amenity_slug']) );
}
if ( isset($_POST['wp_realestate_property_status_slug']) ) {
update_option( 'wp_realestate_property_status_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_status_slug']) );
}
if ( isset($_POST['wp_realestate_property_label_slug']) ) {
update_option( 'wp_realestate_property_label_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_label_slug']) );
}
if ( isset($_POST['wp_realestate_property_material_slug']) ) {
update_option( 'wp_realestate_property_material_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_material_slug']) );
}
if ( isset($_POST['wp_realestate_property_type_slug']) ) {
update_option( 'wp_realestate_property_type_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_type_slug']) );
}
if ( isset($_POST['wp_realestate_property_location_slug']) ) {
update_option( 'wp_realestate_property_location_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_location_slug']) );
}
if ( isset($_POST['wp_realestate_property_archive_slug']) ) {
update_option( 'wp_realestate_property_archive_slug', sanitize_title_with_dashes($_POST['wp_realestate_property_archive_slug']) );
}
// agent
if ( isset($_POST['wp_realestate_agent_base_slug']) ) {
update_option( 'wp_realestate_agent_base_slug', sanitize_title_with_dashes($_POST['wp_realestate_agent_base_slug']) );
}
if ( isset($_POST['wp_realestate_agent_category_slug']) ) {
update_option( 'wp_realestate_agent_category_slug', sanitize_title_with_dashes($_POST['wp_realestate_agent_category_slug']) );
}
if ( isset($_POST['wp_realestate_agent_location_slug']) ) {
update_option( 'wp_realestate_agent_location_slug', sanitize_title_with_dashes($_POST['wp_realestate_agent_location_slug']) );
}
if ( isset($_POST['wp_realestate_agent_archive_slug']) ) {
update_option( 'wp_realestate_agent_archive_slug', sanitize_title_with_dashes($_POST['wp_realestate_agent_archive_slug']) );
}
// agency
if ( isset($_POST['wp_realestate_agency_base_slug']) ) {
update_option( 'wp_realestate_agency_base_slug', sanitize_title_with_dashes($_POST['wp_realestate_agency_base_slug']) );
}
if ( isset($_POST['wp_realestate_agency_category_slug']) ) {
update_option( 'wp_realestate_agency_category_slug', sanitize_title_with_dashes($_POST['wp_realestate_agency_category_slug']) );
}
if ( isset($_POST['wp_realestate_agency_location_slug']) ) {
update_option( 'wp_realestate_agency_location_slug', sanitize_title_with_dashes($_POST['wp_realestate_agency_location_slug']) );
}
if ( isset($_POST['wp_realestate_agency_archive_slug']) ) {
update_option( 'wp_realestate_agency_archive_slug', sanitize_title_with_dashes($_POST['wp_realestate_agency_archive_slug']) );
}
}
}
}
WP_RealEstate_Permalink_Settings::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment