Skip to content

Commit

Permalink
Add nearest location search
Browse files Browse the repository at this point in the history
  • Loading branch information
lhausammann committed Oct 3, 2023
1 parent 818fdab commit a0e8cf2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 6 additions & 6 deletions wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ public static function getForecastData($geolocationId, $accessToken)
return wp_remote_retrieve_body($response);
}

public static function nearestGeolocation($longitude, $latitude, $accessToken)
public static function getNearestGeolocations($lat, $lon, $accessToken)
{
$cacheKey = hash('sha256', 'near_'.$latitude.$longitude);
$response = get_transient('srf_weather_nearestloc' . $cacheKey);
if (false === $response) {
$cacheKey = hash('sha256', 'nearest_loc_'.$lat.$lon);
$response = get_transient('srf_weather_nearestlocations' . $cacheKey);
if (false === $response) {
$response = wp_remote_get(
self::API_BASE_URL . sprintf('/geolocations/?longitude=%s&latitude=%s', $longitude, $latitude),
$url = self::API_BASE_URL . sprintf('/geolocations?latitude=%s&longitude=%s&ch=1', $lat, $lon),
[
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
'timeout' => self::REQUEST_TIMEOUT,
]
);
set_transient('srf_weather_nearestloc' . $cacheKey, $response, self::CACHE_TTL);
set_transient('srf_weather_nearestlocations' . $cacheKey, $response, self::CACHE_TTL);
}

return json_decode(wp_remote_retrieve_body($response), true);
Expand Down
25 changes: 23 additions & 2 deletions wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class SrfWeatherWidgetSettings {
const DEFAULT_LOCATION = 'srf-weather-api-geolocation';
const DEFAULT_LOCATION_NAME = 'srf-weather-api-geolocation-name';



public function __construct()
{
add_action('admin_menu', array(__CLASS__, 'adminMenu'));
Expand All @@ -25,9 +27,28 @@ public static function validateGeolocation($value) {
'swiss geolocation identifier must be in the format: xx.xxxx,x.xxxx (e.g. 47.5239,8.5363)'
);

return get_option(SrfWeatherWidgetSettings::DEFAULT_LOCATION);
return $value;
}

// Use the given keys to fetch the nearest point. Use the submittet options, not the
// stored one for that.
$key = $_POST[SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY];
$secret = $_POST[SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET];
$token = SrfWeatherWidgetApiClient::getAccessToken($key, $secret);
$parts = array_filter(explode(',', $value), 'trim');
$nearest = SrfWeatherWidgetApiClient::getNearestGeolocations($parts[0], $parts[1], $token);
if (!count($nearest)) {
add_settings_error(
SrfWeatherWidgetSettings::DEFAULT_LOCATION,
'not ch',
'geolocation must be in switzerland.'
);

return $value;
} else {

return $nearest[0]['id'];
}
return $value;
}

public static function validateNotEmptyApiKey($value) {
Expand Down
1 change: 0 additions & 1 deletion wp-plugin/srf-weather-widget/meteo.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ function meteo_widget($atts = [], $content = null, $tag = '') {
$forecastpoint = SrfWeatherWidgetApiClient::getForecastData($geolocationId, $accessToken);
}


$widget = '<div class="srf-weather-widget" data-size="' . esc_html($widgetAtts['size']) . '" data-mode="' . esc_html($widgetAtts['mode']) . '"';

if (!empty($widgetAtts['geolocation'])) {
Expand Down

0 comments on commit a0e8cf2

Please sign in to comment.