Skip to content

Commit

Permalink
Add get_coordinates method
Browse files Browse the repository at this point in the history
  • Loading branch information
burhandodhy committed Feb 4, 2025
1 parent ef7a316 commit bf466fa
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 8 deletions.
54 changes: 46 additions & 8 deletions includes/classes/Feature/GeoLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public function set_settings_schema() {
$this->settings_schema = [
[
'default' => '',
'help' => __( 'Add your Google Maps API key.', 'elasticpress-labs' ),
'help' => __( 'Providing a Google Maps API key enables an autocomplete address field that automatically fetches the latitude and longitude.', 'elasticpress-labs' ),
'key' => 'google_maps_api_key',
'label' => __( 'Providing a Google Maps API key enables an autocomplete address field that automatically fetches the latitude and longitude.', 'elasticpress-labs' ),
'label' => __( 'Google Maps API Key', 'elasticpress-labs' ),
'type' => 'text',
],
];
Expand Down Expand Up @@ -155,14 +155,14 @@ public function register_meta(): void {
* @return array Mapping.
*/
public function add_mapping( $mapping ): array {
$mapping['mappings']['properties']['geo_point'] = array(
'properties' => array(
'location' => array(
$mapping['mappings']['properties']['geo_point'] = [
'properties' => [
'location' => [
'type' => 'geo_point',
'ignore_malformed' => true,
),
),
);
],
],
];

return $mapping;
}
Expand Down Expand Up @@ -250,4 +250,42 @@ protected function process_geo_distance_sort( $sort, $args ) {

return $sort;
}

/**
* Get coordinates for an address.
*
* @param string $address Address.
* @return array Coordinates.
*/
public function get_coordinates( $address ): array {
$settings = $this->get_settings();
if ( empty( $settings['google_maps_api_key'] ) ) {
return [];
}

$url = add_query_arg(
[
'address' => rawurldecode( $address ),
'key' => $settings['google_maps_api_key'],
],
'https://maps.googleapis.com/maps/api/geocode/json'
);

$response = wp_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return [];
}

$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body );
if ( empty( $data->results ) ) {
return [];
}

$location = $data->results[0]->geometry->location;
return [
'lat' => $location->lat,
'lon' => $location->lng,
];
}
}
127 changes: 127 additions & 0 deletions tests/phpunit/feature/TestGeoLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,133 @@ public function test_search_with_orderby_asc() {
$this->assertEquals( 'Boston', $query->posts[3]->post_title );
}

/**
* Tests get_coordinates method.
*
* @group geo-location
*/
public function test_get_coordinates() {
// Set the Google Maps API key.
add_filter(
'option_ep_feature_settings',
function ( $value ) {
$value['geo_location']['google_maps_api_key'] = 'test_api_key';
return $value;
}
);

// Mock the HTTP request to Google Maps API.
add_filter(
'pre_http_request',
function () {
$response = [
'response' => [
'code' => 200,
],
'body' => json_encode(
[
'results' => [
[
'geometry' => [
'location' => [
'lat' => 40.712776,
'lng' => -74.005974,
],
],
],
],
]
),
];

return $response;
}
);

$coordinates = $this->get_feature()->get_coordinates( 'New York City' );
$this->assertEquals( 40.712776, $coordinates['lat'] );
$this->assertEquals( -74.005974, $coordinates['lon'] );
}

/**
* Tests get_coordinates method with no API key.
*
* @group geo-location
*/
public function test_get_coordinates_with_no_api_key() {
$coordinates = $this->get_feature()->get_coordinates( 'New York City' );
$this->assertEmpty( $coordinates );
}

/**
* Tests get_coordinates method with API error.
*
* @group geo-location
*/
public function test_get_coordinates_with_api_error() {
// Set the Google Maps API key.
add_filter(
'option_ep_feature_settings',
function ( $value ) {
$value['geo_location']['google_maps_api_key'] = 'test_api_key';
return $value;
}
);

// Mock the HTTP request to Google Maps API.
add_filter(
'pre_http_request',
function () {
return new \WP_Error( 'http_request_failed', 'Error' );
}
);

$coordinates = $this->get_feature()->get_coordinates( 'New York City' );
$this->assertEmpty( $coordinates );
}

/**
* Tests `ep_geo_location_geo_points` filter.
*
* @group geo-location
*/
public function test_location_geo_points_filter() {
add_filter(
'ep_geo_location_geo_points',
function ( $geo_points ) {
$geo_points['location'] = [
'lat' => 10000,
'lon' => 20000,
];

return $geo_points;
}
);

$post_id = $this->ep_factory->post->create(
[
'meta_input' => [
'ep_latitude' => 10,
'ep_longitude' => 20,
],
]
);

ElasticPress\Elasticsearch::factory()->refresh_indices();

$post = \ElasticPress\Indexables::factory()->get( 'post' )->get( $post_id );

$expected_result = [
'location' => [
'lat' => 10000,
'lon' => 20000,
],
];

$this->assertArrayHasKey( 'geo_point', $post );
$this->assertSame( $expected_result, $post['geo_point'] );
}

/**
* Creates test posts with geolocation data for various cities.
*
Expand Down

0 comments on commit bf466fa

Please sign in to comment.