| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
/** |
| 6 |
* PropertyHive PH_Address_Keyword_Polygon |
| 7 |
* |
| 8 |
* Polygon Handler |
| 9 |
* |
| 10 |
* @class PH_Address_Keyword_Polygon |
| 11 |
* @version 1.0.0 |
| 12 |
* @package PropertyHive/Classes |
| 13 |
* @category Class |
| 14 |
* @author PropertyHive |
| 15 |
*/ |
| 16 |
class PH_Address_Keyword_Polygon { |
| 17 |
|
| 18 |
/** @var PH_Emails The single instance of the class */ |
| 19 |
protected static $_instance = null; |
| 20 |
|
| 21 |
/** @public array Stores post IDs matching layered nav, so price filter can find max price in view */ |
| 22 |
private $address_keyword_polygon_points = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Main PH_Emails Instance. |
| 26 |
* |
| 27 |
* Ensures only one instance of PH_Emails is loaded or can be loaded. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* @static |
| 31 |
* @return PH_Emails Main instance |
| 32 |
*/ |
| 33 |
public static function instance() { |
| 34 |
if ( is_null( self::$_instance ) ) { |
| 35 |
self::$_instance = new self(); |
| 36 |
} |
| 37 |
return self::$_instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Cloning is forbidden. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public function __clone() { |
| 46 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Unserializing instances of this class is forbidden. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
*/ |
| 54 |
public function __wakeup() { |
| 55 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'propertyhive' ), '1.0.0' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Constructor for the email class hooks in all emails that can be sent. |
| 60 |
* |
| 61 |
*/ |
| 62 |
public function __construct() { |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public function get_address_keyword_polygon_coordinates( $address_keyword ) |
| 67 |
{ |
| 68 |
global $wpdb; |
| 69 |
|
| 70 |
$address_keyword = trim($address_keyword); |
| 71 |
|
| 72 |
// see if it exists in the table |
| 73 |
$results = $wpdb->get_results( |
| 74 |
$wpdb->prepare( |
| 75 |
" |
| 76 |
SELECT polygon_coordinates |
| 77 |
FROM {$wpdb->prefix}ph_address_keyword_polygon |
| 78 |
WHERE address_keyword = %s |
| 79 |
", |
| 80 |
$address_keyword |
| 81 |
) |
| 82 |
); |
| 83 |
|
| 84 |
foreach ( $results as $result ) |
| 85 |
{ |
| 86 |
return @unserialize($result->polygon_coordinates, ['allowed_classes' => false]); |
| 87 |
} |
| 88 |
|
| 89 |
// nothing found in the table. Let's go get it |
| 90 |
|
| 91 |
$url = 'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($address_keyword) . '&polygon_geojson=1&format=json'; |
| 92 |
|
| 93 |
$response = wp_remote_get( |
| 94 |
$url, |
| 95 |
array( |
| 96 |
'headers' => array( |
| 97 |
'Referer' => home_url(), |
| 98 |
'User-Agent' => 'Property-Hive/' . PH_VERSION . ' (+https://wp-property-hive.com)', |
| 99 |
), |
| 100 |
) |
| 101 |
); |
| 102 |
|
| 103 |
if ( is_array( $response ) && ! is_wp_error( $response ) ) |
| 104 |
{ |
| 105 |
$body = $response['body']; // use the content |
| 106 |
|
| 107 |
$json = json_decode($body, true); |
| 108 |
|
| 109 |
if ( $json !== FALSE && !empty($json) ) |
| 110 |
{ |
| 111 |
foreach ( $json as $json_result ) |
| 112 |
{ |
| 113 |
if ( |
| 114 |
isset($json_result['class']) && in_array($json_result['class'], array( 'boundary' )) && |
| 115 |
isset($json_result['geojson']['type']) && $json_result['geojson']['type'] == 'Polygon' |
| 116 |
) |
| 117 |
{ |
| 118 |
$polygon_coordinates = array(); |
| 119 |
foreach ( $json_result['geojson']['coordinates'][0] as $coordinate ) |
| 120 |
{ |
| 121 |
$polygon_coordinates[] = $coordinate[1] . ' ' . $coordinate[0]; |
| 122 |
} |
| 123 |
|
| 124 |
if ( !empty($polygon_coordinates) ) |
| 125 |
{ |
| 126 |
// Insert into email log |
| 127 |
$insert = $wpdb->insert( |
| 128 |
$wpdb->prefix . 'ph_address_keyword_polygon', |
| 129 |
array( |
| 130 |
'address_keyword' => $address_keyword, |
| 131 |
'polygon_coordinates' => serialize($polygon_coordinates) |
| 132 |
), |
| 133 |
array( |
| 134 |
'%s', |
| 135 |
'%s' |
| 136 |
) |
| 137 |
); |
| 138 |
|
| 139 |
return $polygon_coordinates; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return false; |
| 147 |
} |
| 148 |
} |