File manager - Edit - /home/colomboelectrici/public_html/wp-content/plugins/page-generator-pro/includes/admin/geo.php
Back
<?php /** * Cities class * * Performs remote queries to build lists of nearby cities. * * @package Page_Generator_Pro * @author Tim Carr * @version 1.1.7 */ class Page_Generator_Pro_Geo { /** * Holds the class object. * * @since 1.1.7 * * @var object */ public static $instance; /** * Holds the API Key * * @since 1.2.1 * * @var string */ public $api_key = 'AIzaSyDQJjWCtWqtYGE7cVu9zHrNjX_kweNSJm8'; /** * Returns an array comprising of the latitude and longitude for a given city and country * using the Google Geocoding API * * @since 1.1.7 * * @param string $city City * @param string $country Country * @return mixed Array | WP_Error */ public function google_get_lat_lng( $city, $country = '' ) { // Format address if ( ! empty( $country ) ) { $address = $city . ',' . $country; } else { $address = $city; } // Run query $response = wp_remote_get( 'https://maps.google.com/maps/api/geocode/json?address=' . urlencode( $address ) . '&sensor=false&key=' . $this->api_key ); // Bail if an error occured if ( is_wp_error( $response ) ) { // Try the OpenStreetMap API return $this->openstreetmap_get_lat_lng( $city, $country ); } // Get body $data = wp_remote_retrieve_body( $response ); $json = json_decode( $data ); // Check status if ( isset( $json->status ) && $json->status != 'OK' ) { // Try the OpenStreetMap API return $this->openstreetmap_get_lat_lng( $city, $country ); } // Get lat/lng $lat = (float) $json->results[0]->geometry->location->lat; $lng = (float) $json->results[0]->geometry->location->lng; // Return return array( 'lat' => $lat, 'lng' => $lng, ); } /** * Returns an array comprising of the latitude and longitude for a given city and country * using the OpenStreetMap API * * @since 1.1.7 * * @param string $city City * @param string $country Country * @return mixed Array | WP_Error */ public function openstreetmap_get_lat_lng( $city, $country = '' ) { // Format address if ( ! empty( $country ) ) { $address = $city . ',' . $country; } else { $address = $city; } // Run query $response = wp_remote_get( 'http://nominatim.openstreetmap.org/search?q=' . urlencode( $address ) . '&format=json' ); // Bail if an error occured if ( is_wp_error( $response ) ) { return $response; } // Get body $data = wp_remote_retrieve_body( $response ); $json = json_decode( $data ); // Check some results were found if ( ! is_array( $json ) || count( $json ) == 0 ) { return new WP_Error( 'page_generator_pro_geo_openstreetmap_get_lat_lng_error', sprintf( __( 'Could not get latitude and longitude for %s', 'page-generator-pro' ), $address ) ); } // Get lat/lng from the first result $lat = (float) $json[0]->lat; $lng = (float) $json[0]->lon; // Return return array( 'lat' => $lat, 'lng' => $lng, ); } /** * Returns an array of nearby cities and towns for the given latitude, longitude and radius * * @since 1.1.7 * * @param float $lat Latitude * @param float $lng Longitude * @param int $radius Radius (in miles) * @return mixed Array | WP_Error */ public function get_nearby_cities( $lat, $lng, $radius ) { // Limit the radius to the permitted maximum of 100 // If it's greater, we will get fewer results from the API, for some reason if ( $radius > 100 ) { $radius = 100; } // Use cURL to fetch data. wp_remote_get returns a 403, most likely because the UA isn't set correctly. $ch = curl_init(); curl_setopt( $ch, CURLOPT_URL, 'http://www.geoplugin.net/extras/nearby.gp?lat=' . $lat . '&long=' . $lng . '&limit=100&radius=' . absint( $radius ) . '&format=json' ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0' ); $response = curl_exec( $ch ); $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); // If the HTTP response code isn't 200, something went wrong if ( $http_code != 200 ) { return new WP_Error( 'page_generator_pro_geo_get_nearby_cities_no_results', sprintf( __( 'GeoPlugin returned HTTP Status Code %s. Please try again.', 'page-generator-pro' ), $http_code ) ); } // Decode JSON $json = json_decode( $response ); // Check cities were found if ( ! is_array( $json ) || count( $json ) == 0 ) { return new WP_Error( 'page_generator_pro_geo_get_nearby_cities_no_results', __( 'No nearby cities found. Try increasing the radius.', 'page-generator-pro' ) ); } // Build array $cities = array(); foreach ( $json as $city ) { $cities[] = $city->geoplugin_place; } // Return cities return $cities; } /** * Returns the singleton instance of the class. * * @since 1.1.7 * * @return object Class. */ public static function get_instance() { if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { self::$instance = new self; } return self::$instance; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.06 |
proxy
|
phpinfo
|
Settings