get_results( $wpdb->prepare( " SELECT polygon_coordinates FROM {$wpdb->prefix}ph_address_keyword_polygon WHERE address_keyword = %s ", $address_keyword ) ); foreach ( $results as $result ) { return @unserialize($result->polygon_coordinates, ['allowed_classes' => false]); } // nothing found in the table. Let's go get it $url = 'https://nominatim.openstreetmap.org/search.php?q=' . urlencode($address_keyword) . '&polygon_geojson=1&format=json'; $response = wp_remote_get( $url, array( 'headers' => array( 'Referer' => home_url(), 'User-Agent' => 'Property-Hive/' . PH_VERSION . ' (+https://wp-property-hive.com)', ), ) ); if ( is_array( $response ) && ! is_wp_error( $response ) ) { $body = $response['body']; // use the content $json = json_decode($body, true); if ( $json !== FALSE && !empty($json) ) { foreach ( $json as $json_result ) { if ( isset($json_result['class']) && in_array($json_result['class'], array( 'boundary' )) && isset($json_result['geojson']['type']) && $json_result['geojson']['type'] == 'Polygon' ) { $polygon_coordinates = array(); foreach ( $json_result['geojson']['coordinates'][0] as $coordinate ) { $polygon_coordinates[] = $coordinate[1] . ' ' . $coordinate[0]; } if ( !empty($polygon_coordinates) ) { // Insert into email log // 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. $insert = $wpdb->insert( $wpdb->prefix . 'ph_address_keyword_polygon', array( 'address_keyword' => $address_keyword, 'polygon_coordinates' => serialize($polygon_coordinates) ), array( '%s', '%s' ) ); return $polygon_coordinates; } } } } } return false; } }