Ejemplo n.º 1
0
 /**
  * Outputs the content of the widget
  *
  * @param array $args
  * @param array $instance
  *
  */
 public function widget($args, $instance)
 {
     global $wpdb;
     if (!array_key_exists('price-min', $_GET) || !array_key_exists('price-max', $_GET) || !array_key_exists('beds', $_GET) || !array_key_exists('location', $_GET) || !array_key_exists('distance', $_GET)) {
         return;
     }
     $filters = ['price-min' => intval($_GET['price-min']), 'price-max' => intval($_GET['price-max']), 'beds' => intval($_GET['beds']), 'location' => sanitize_text_field($_GET['location']), 'distance' => intval($_GET['distance'])];
     $sort = array_key_exists('sort', $_GET) ? sanitize_text_field($_GET['sort']) : 'distance';
     $sortDir = array_key_exists('sortdir', $_GET) ? sanitize_text_field($_GET['sortdir']) : 'asc';
     $page = array_key_exists('page', $_GET) ? intval($_GET['page']) : 1;
     require_once get_stylesheet_directory() . '/includes/Geocoder.php';
     $geocoder = new Geocoder();
     $location = $geocoder->getLocation($filters['location']);
     $sql = "\n            SELECT DISTINCT\n              {$wpdb->posts}.ID as post_id,\n              CAST(price.meta_value AS UNSIGNED) AS price,\n              beds.meta_value AS beds,\n              latitude.meta_value AS latitude,\n              longitude.meta_value AS longitude,\n              p.distance_unit * DEGREES(\n                ACOS(\n                  COS(RADIANS(p.latpoint)) * COS(RADIANS(latitude.meta_value)) * COS(\n                    RADIANS(\n                      p.longpoint - longitude.meta_value\n                    )\n                  ) + SIN(RADIANS(p.latpoint)) * SIN(RADIANS(latitude.meta_value))\n                )\n              ) AS distance\n            FROM\n              {$wpdb->posts}\n              INNER JOIN {$wpdb->postmeta} beds\n                ON beds.post_id = {$wpdb->posts}.ID\n                AND beds.meta_key = 'wpcf-bedrooms'\n              INNER JOIN {$wpdb->postmeta} price\n                ON price.post_id = {$wpdb->posts}.ID\n                AND price.meta_key = 'wpcf-price'\n              INNER JOIN {$wpdb->postmeta} latitude\n                ON latitude.post_id = {$wpdb->posts}.ID\n                AND latitude.meta_key = 'wpcf-latitude'\n              INNER JOIN {$wpdb->postmeta} longitude\n                ON longitude.post_id = {$wpdb->posts}.ID\n                AND longitude.meta_key = 'wpcf-longitude'\n              JOIN\n                (SELECT\n                  {$location['lat']} AS latpoint,\n                  {$location['lng']} AS longpoint,\n                  {$filters['distance']} AS radius,\n                  69.0 AS distance_unit) AS p\n                ON 1 = 1\n            WHERE latitude.meta_value BETWEEN p.latpoint - (p.radius / p.distance_unit)\n              AND p.latpoint + (p.radius / p.distance_unit)\n              AND longitude.meta_value BETWEEN p.longpoint - (\n                p.radius / (\n                  p.distance_unit * COS(RADIANS(p.latpoint))\n                )\n              )\n              AND p.longpoint + (\n                p.radius / (\n                  p.distance_unit * COS(RADIANS(p.latpoint))\n                )\n              )\n              AND {$wpdb->posts}.post_type = 'sale-property'\n              AND beds.meta_value >= {$filters['beds']}\n              AND price.meta_value BETWEEN {$filters['price-min']}\n              AND {$filters['price-max']}\n            ORDER BY {$sort} {$sortDir}\n        ";
     $properties = $wpdb->get_results($sql);
     require get_stylesheet_directory() . '/property-results-widget-template.php';
 }