PluginProbe
Property Hive / 2.3.1
Property Hive v2.3.1
2.3.1 2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 All 261 releases
propertyhive / includes / class-ph-address-keyword-polygon.php

class-ph-address-keyword-polygon.php in Property Hive 2.3.1, at includes/class-ph-address-keyword-polygon.php

151 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Address_Keyword_Polygon; preserving the existing PH_* class name is required for plugin and extension compatibility.
17 class PH_Address_Keyword_Polygon {
18
19 /** @var PH_Emails The single instance of the class */
20 protected static $_instance = null;
21
22 /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
23 private $address_keyword_polygon_points = array();
24
25 /**
26 * Main PH_Emails Instance.
27 *
28 * Ensures only one instance of PH_Emails is loaded or can be loaded.
29 *
30 * @since 1.0.0
31 * @static
32 * @return PH_Emails Main instance
33 */
34 public static function instance() {
35 if ( is_null( self::$_instance ) ) {
36 self::$_instance = new self();
37 }
38 return self::$_instance;
39 }
40
41 /**
42 * Cloning is forbidden.
43 *
44 * @since 1.0.0
45 */
46 public function __clone() {
47 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'propertyhive' ), '1.0.0' );
48 }
49
50 /**
51 * Unserializing instances of this class is forbidden.
52 *
53 * @since 1.0.0
54 */
55 public function __wakeup() {
56 _doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin&#8217; huh?', 'propertyhive' ), '1.0.0' );
57 }
58
59 /**
60 * Constructor for the email class hooks in all emails that can be sent.
61 *
62 */
63 public function __construct() {
64
65 }
66
67 public function get_address_keyword_polygon_coordinates( $address_keyword )
68 {
69 global $wpdb;
70
71 $address_keyword = trim($address_keyword);
72
73 // see if it exists in the table
74 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Prepared read from the plugin's persistent geocoding cache; cached polygons avoid external HTTP and this custom table has no core data API.
75 $results = $wpdb->get_results(
76 $wpdb->prepare(
77 "
78 SELECT polygon_coordinates
79 FROM {$wpdb->prefix}ph_address_keyword_polygon
80 WHERE address_keyword = %s
81 ",
82 $address_keyword
83 )
84 );
85
86 foreach ( $results as $result )
87 {
88 return @unserialize($result->polygon_coordinates, ['allowed_classes' => false]);
89 }
90
91 // nothing found in the table. Let's go get it
92
93 $url = 'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($address_keyword) . '&polygon_geojson=1&format=json';
94
95 $response = wp_remote_get(
96 $url,
97 array(
98 'headers' => array(
99 'Referer' => home_url(),
100 'User-Agent' => 'Property-Hive/' . PH_VERSION . ' (+https://wp-property-hive.com)',
101 ),
102 )
103 );
104
105 if ( is_array( $response ) && ! is_wp_error( $response ) )
106 {
107 $body = $response['body']; // use the content
108
109 $json = json_decode($body, true);
110
111 if ( $json !== FALSE && !empty($json) )
112 {
113 foreach ( $json as $json_result )
114 {
115 if (
116 isset($json_result['class']) && in_array($json_result['class'], array( 'boundary' )) &&
117 isset($json_result['geojson']['type']) && $json_result['geojson']['type'] == 'Polygon'
118 )
119 {
120 $polygon_coordinates = array();
121 foreach ( $json_result['geojson']['coordinates'][0] as $coordinate )
122 {
123 $polygon_coordinates[] = $coordinate[1] . ' ' . $coordinate[0];
124 }
125
126 if ( !empty($polygon_coordinates) )
127 {
128 // Insert into email log
129 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Populate the plugin's persistent polygon cache using explicit string formats; no equivalent core data API exists for this table.
130 $insert = $wpdb->insert(
131 $wpdb->prefix . 'ph_address_keyword_polygon',
132 array(
133 'address_keyword' => $address_keyword,
134 'polygon_coordinates' => serialize($polygon_coordinates)
135 ),
136 array(
137 '%s',
138 '%s'
139 )
140 );
141
142 return $polygon_coordinates;
143 }
144 }
145 }
146 }
147 }
148
149 return false;
150 }
151 }