Last active
August 22, 2018 08:21
-
-
Save shanejones/2163a14f8679507f2757d361498c0e9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Search | |
*/ | |
class Search { | |
public $postcode; | |
public $transient_name; | |
public $location; | |
public $locations_paid = array(); | |
public $locations_free = array(); | |
public $locations_ordered; | |
public $price_low; | |
public $price_high; | |
public $ord; | |
public $min_price; | |
public $max_price; | |
public $has_paid_results = false; | |
public function __construct( $postcode, $pl, $pu, $dl, $du, $ord ) { | |
$this->postcode = $postcode; | |
$this->ord = $ord; | |
$this->check_location(); | |
$posts = $this->get_posts(); | |
foreach ( $posts->posts as $post ) { | |
$latlong = $this->get_lat_longs( $post->ID ); | |
$paid = $this->get_paid( $post->ID ); | |
$rating = $this->get_rating( $post->ID ); | |
$price = $this->get_price( $post->ID ); | |
$distance = number_format( $this->calculate_distance($latlong[0]), 2, '.', '' ); | |
if ( ( $distance >= $dl && $distance <= $du && $price >= $pl && $price <= $pu ) ) { | |
$result = array( | |
'postcode' => $postcode, | |
'post_id' => $post->ID, | |
'post_title' => get_the_title( $post->ID ), | |
'distance' => $distance, | |
'rating' => $rating, | |
'price' => $price, | |
'paid' => $paid | |
); | |
if ($paid) { | |
array_push($this->locations_paid, $result); | |
$this->has_paid_results = true; | |
} else { | |
array_push($this->locations_free, $result); | |
} | |
} | |
} | |
if($this->ord == 'asc'){ | |
$this->array_sort( $this->locations_paid, 'distance' ); | |
$this->array_sort( $this->locations_free, 'distance' ); | |
} else if($this->ord == 'pl'){ | |
$this->array_sort( $this->locations_paid, 'price' ); | |
$this->array_sort( $this->locations_free, 'price' ); | |
} else if($this->ord == 'ph'){ | |
$this->array_sort( $this->locations_paid, 'price' ); | |
$this->array_sort( $this->locations_free, 'price' ); | |
$this->locations_paid = array_reverse($this->locations_paid); | |
$this->locations_free = array_reverse($this->locations_free); | |
} | |
// join arrays | |
$this->locations_ordered = array_merge( $this->locations_paid, $this->locations_free ); | |
// get cheapest | |
$all = $this->locations_ordered; | |
$this->array_sort( $all, 'price' ); | |
reset($all); | |
$this->min_price = $all[key($all)]['price']; | |
$all = array_reverse($all); | |
reset($all); | |
$this->max_price = $all[key($all)]['price']; | |
} | |
/** | |
* Returns the distance in miles between locations | |
* | |
* @param array $latlong | |
* @return int | |
*/ | |
public function calculate_distance($latlong) | |
{ | |
$lat2 = isset($latlong['lat']) ? $latlong['lat'] : null; | |
$lon2 = isset($latlong['lng']) ? $latlong['lng'] : null; | |
$lon1 = $this->location['result']['longitude']; | |
$lat1 = $this->location['result']['latitude']; | |
$theta = $lon1 - $lon2; | |
$dist = sin( deg2rad( $lat1 ) ) * sin( deg2rad( $lat2 ) ) + cos( deg2rad( $lat1 ) ) * cos( deg2rad( $lat2 ) ) * cos( deg2rad( $theta ) ); | |
$dist = acos( $dist ); | |
$dist = rad2deg( $dist ); | |
$miles = $dist * 60 * 1.1515; | |
return $miles; | |
} | |
public function check_location() { | |
$location_details = $this->get_location(); | |
if ( $location_details['status'] == 200 ) { | |
$this->location = $location_details; | |
} else { | |
return 'Postcode not found'; | |
} | |
} | |
public function get_location() { | |
return json_decode( file_get_contents( 'https://api.postcodes.io/postcodes/' . str_replace( ' ', '', $this->postcode ) ), true ); | |
} | |
public function get_posts() { | |
$args = array( | |
'post_type' => 'funeral_directors', | |
'posts_per_page' => -1, | |
'order_by' => 'funeral_price', | |
'order' => $this->ord, | |
'meta_key' => 'direct_cremation', | |
'meta_value' => '0' | |
); | |
$posts = new WP_Query( $args ); | |
return $posts; | |
} | |
public function get_lat_longs( $post_id ) { | |
return get_post_meta( $post_id, 'map_pin_location', false ); | |
} | |
public function get_paid( $post_id ) { | |
return get_post_meta( $post_id, 'paid', true ); | |
} | |
public function get_rating( $post_id ) { | |
return get_post_meta( $post_id, 'rating', true ); | |
} | |
public function get_price( $post_id ) { | |
return get_post_meta( $post_id, 'funeral_price', true ); | |
} | |
public function array_sort( &$array, $key ) { | |
$sorter = array(); | |
$ret = array(); | |
reset( $array ); | |
foreach ( $array as $ii => $va ) { | |
$sorter[ $ii ] = $va[ $key ]; | |
} | |
asort( $sorter ); | |
foreach ( $sorter as $ii => $va ) { | |
$ret[ $ii ] = $array[ $ii ]; | |
} | |
$array = $ret; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment