diff --git a/wp-content/themes/homeproz/inc/ajax-handlers.php b/wp-content/themes/homeproz/inc/ajax-handlers.php index d6804f5a..2c637d7b 100755 --- a/wp-content/themes/homeproz/inc/ajax-handlers.php +++ b/wp-content/themes/homeproz/inc/ajax-handlers.php @@ -27,7 +27,7 @@ function homeproz_ajax_filter_properties() { // Get filter values $property_type = isset($_POST['property_type']) ? sanitize_text_field($_POST['property_type']) : ''; $property_status = isset($_POST['property_status']) ? sanitize_text_field($_POST['property_status']) : ''; - $property_location = isset($_POST['property_location']) ? sanitize_text_field($_POST['property_location']) : ''; + $property_location = isset($_POST['city']) ? sanitize_text_field($_POST['city']) : ''; $zip = isset($_POST['zip']) ? sanitize_text_field($_POST['zip']) : ''; $min_price = isset($_POST['min_price']) ? intval($_POST['min_price']) : ''; $max_price = isset($_POST['max_price']) ? intval($_POST['max_price']) : ''; @@ -64,7 +64,12 @@ function homeproz_ajax_filter_properties() { $filter_args['property_type'] = $property_type; } if ($property_location) { - $filter_args['city'] = $property_location; + // Parse "City, SS" format - extract just the city name + $city_name = $property_location; + if (preg_match('/^(.+),\s*([A-Z]{2})$/', $property_location, $matches)) { + $city_name = $matches[1]; + } + $filter_args['city'] = $city_name; } if ($zip) { $filter_args['postal_code'] = $zip; @@ -106,6 +111,14 @@ function homeproz_ajax_filter_properties() { $mls_args['order'] = 'DESC'; } + // Get featured property IDs to prioritize after HomeProz listings + if (function_exists('homeproz_get_featured_mls_ids')) { + $featured_ids = homeproz_get_featured_mls_ids(); + if (!empty($featured_ids)) { + $mls_args['featured_ids'] = $featured_ids; + } + } + // Fetch only the properties we need for this page $paged_properties = mls_get_properties($mls_args); @@ -165,7 +178,7 @@ function homeproz_ajax_filter_properties() { // Build URL with current filters in query string $filter_args = array(); if ($property_type) $filter_args['property_type'] = $property_type; - if ($property_location) $filter_args['property_location'] = $property_location; + if ($property_location) $filter_args['city'] = $property_location; if ($zip) $filter_args['zip'] = $zip; if ($min_price) $filter_args['min_price'] = $min_price; if ($max_price) $filter_args['max_price'] = $max_price; @@ -231,6 +244,7 @@ function homeproz_ajax_get_filter_bounds() { // Get filter values $property_type = isset($_GET['property_type']) ? sanitize_text_field($_GET['property_type']) : ''; $property_location = isset($_GET['city']) ? sanitize_text_field($_GET['city']) : ''; + $zip = isset($_GET['zip']) ? sanitize_text_field($_GET['zip']) : ''; $min_price = isset($_GET['min_price']) ? intval($_GET['min_price']) : ''; $max_price = isset($_GET['max_price']) ? intval($_GET['max_price']) : ''; $min_beds = isset($_GET['min_beds']) ? intval($_GET['min_beds']) : ''; @@ -244,7 +258,15 @@ function homeproz_ajax_get_filter_bounds() { $filter_args['property_type'] = $property_type; } if ($property_location) { - $filter_args['city'] = $property_location; + // Parse "City, SS" format - extract just the city name + $city_name = $property_location; + if (preg_match('/^(.+),\s*([A-Z]{2})$/', $property_location, $matches)) { + $city_name = $matches[1]; + } + $filter_args['city'] = $city_name; + } elseif ($zip) { + // Zip filter (only if city not set) + $filter_args['postal_code'] = $zip; } if ($min_price) { $filter_args['min_price'] = $min_price; @@ -331,15 +353,96 @@ function homeproz_ajax_get_locations() { add_action('wp_ajax_homeproz_get_locations', 'homeproz_ajax_get_locations'); add_action('wp_ajax_nopriv_homeproz_get_locations', 'homeproz_ajax_get_locations'); +/** + * Calculate optimal zoom level for "near me" feature + * + * Starts at zoom 12 and zooms out until finding >= 150 properties + * or reaching minimum zoom level 8. + */ +function homeproz_ajax_calculate_near_me_zoom() { + // Get coordinates + $lat = isset($_POST['lat']) ? floatval($_POST['lat']) : 0; + $lng = isset($_POST['lng']) ? floatval($_POST['lng']) : 0; + + if (!$lat || !$lng) { + wp_send_json_error('Missing coordinates'); + } + + // Check if MLS plugin is available + if (!function_exists('mls_get_property_count')) { + wp_send_json_error('MLS plugin not available'); + } + + // Configuration + $min_properties = 150; + $max_zoom = 12; // Most zoomed in + $min_zoom = 8; // Most zoomed out + + // Approximate viewport dimensions in pixels (reasonable default for map container) + $viewport_width = 800; + $viewport_height = 500; + $tile_size = 256; + + // Find optimal zoom level + $optimal_zoom = $min_zoom; + $property_count = 0; + + for ($zoom = $max_zoom; $zoom >= $min_zoom; $zoom--) { + // Calculate degrees per pixel at this zoom level + // Formula: 360 / (tileSize * 2^zoom) + $degrees_per_pixel = 360 / ($tile_size * pow(2, $zoom)); + + // Calculate viewport span in degrees + $lng_span = $viewport_width * $degrees_per_pixel; + // Latitude adjustment for Mercator (approximate) + $lat_radians = $lat * M_PI / 180; + $lat_span = $viewport_height * $degrees_per_pixel / cos($lat_radians); + + // Calculate bounds + $sw_lat = $lat - ($lat_span / 2); + $ne_lat = $lat + ($lat_span / 2); + $sw_lng = $lng - ($lng_span / 2); + $ne_lng = $lng + ($lng_span / 2); + + // Count properties in this viewport + $count = mls_get_property_count(array( + 'status' => 'Active', + 'bounds' => array($sw_lat, $sw_lng, $ne_lat, $ne_lng), + )); + + if ($count >= $min_properties) { + $optimal_zoom = $zoom; + $property_count = $count; + break; + } + + // Update for potential fallback + $property_count = $count; + $optimal_zoom = $zoom; + } + + wp_send_json_success(array( + 'zoom' => $optimal_zoom, + 'count' => $property_count, + 'lat' => $lat, + 'lng' => $lng, + )); +} +add_action('wp_ajax_homeproz_calculate_near_me_zoom', 'homeproz_ajax_calculate_near_me_zoom'); +add_action('wp_ajax_nopriv_homeproz_calculate_near_me_zoom', 'homeproz_ajax_calculate_near_me_zoom'); + /** * Localize script data for AJAX */ function homeproz_localize_ajax_data() { - if (is_post_type_archive('property') || is_tax('property_type') || is_tax('property_status') || is_tax('property_location')) { + // Check if on properties page (page template or slug) + $is_properties_page = is_page_template('page-properties.php') || is_page('properties'); + + if ($is_properties_page) { wp_localize_script('homeproz-script', 'homeprozAjax', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('homeproz_filter_nonce'), - 'archiveUrl' => get_post_type_archive_link('property'), + 'archiveUrl' => home_url('/properties/'), )); }