diff --git a/wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php b/wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php index 48b6d3f..e1c85c8 100644 --- a/wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php +++ b/wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php @@ -22,7 +22,7 @@ public static function getAccessToken($key, $secret) ); $body = json_decode(wp_remote_retrieve_body($response), true); - return $body['access_token']; + return isset($body['access_token']) ? $body['access_token'] : ''; } public static function getGeolocationNames($geolocationName, $accessToken) { diff --git a/wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php b/wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php index b39009e..58ef003 100644 --- a/wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php +++ b/wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php @@ -7,7 +7,7 @@ class SrfWeatherWidgetSettings { const DEFAULT_LOCATION = 'srf-weather-api-geolocation'; const DEFAULT_LOCATION_NAME = 'srf-weather-api-geolocation-name'; - + static $credentials = []; public function __construct() { @@ -17,7 +17,7 @@ public function __construct() public static function validateGeolocation($value) { if (!$value) { - return $value; + return ''; } if (!strpos($value, ',')) { @@ -30,11 +30,16 @@ public static function validateGeolocation($value) { return $value; } - // Use the given keys to fetch the nearest point. Use the submittet options, not the - // stored one for that. + // Use the given keys to fetch the nearest point. Use the submitted options, not the + // stored one for that (because a user can change them and it should work initially). $key = $_POST[SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY]; $secret = $_POST[SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET]; + $token = SrfWeatherWidgetApiClient::getAccessToken($key, $secret); + if (!$token) { + + return $value; + } $parts = array_filter(explode(',', $value), 'trim'); $nearest = SrfWeatherWidgetApiClient::getNearestGeolocations($parts[0], $parts[1], $token); if (!count($nearest)) { @@ -46,12 +51,13 @@ public static function validateGeolocation($value) { return $value; } else { - + // update the geolocation name as well + $_POST[SrfWeatherWidgetSettings::DEFAULT_LOCATION_NAME] = $nearest[0]['default_name']; // keep it simple return $nearest[0]['id']; } } - public static function validateNotEmptyApiKey($value) { + public static function validateNonEmptyKey($value) { if (!$value) { add_settings_error( SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY, @@ -60,23 +66,44 @@ public static function validateNotEmptyApiKey($value) { ); return get_option(SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY); +; } + self::$credentials['key'] = $value; return $value; } - public static function validateNotEmptyApiSecret($value) { + public static function validateConnection($value) { + // not blank if (!$value) { add_settings_error( SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET, 'not empty', 'App Secret must not be empty.' ); + return get_option(SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET); +; + } + // no api key stored for some reason + if (!isset(self::$credentials['key'])) { + + return get_option(SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET); +; // dont validate further + } + // validate connection (by retrieving an access token + $accessToken = SrfWeatherWidgetApiClient::getAccessToken(self::$credentials['key'], $value); + if (!$accessToken) { + add_settings_error( + SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET, + 'no connection', + 'Either API Key or API Secret is wrong.' + ); return get_option(SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET); + ; } - return $value; + return self::$credentials['secret'] = $value; } @@ -96,8 +123,8 @@ public static function displayWeatherWidgetSettings() { } public static function initSettings() { - register_setting(self::SRF_WEATHER_OPTION_GROUP, self::SRF_WEATHER_API_KEY, [SrfWeatherWidgetSettings::class]); - register_setting(self::SRF_WEATHER_OPTION_GROUP, self::SRF_WEATHER_API_SECRET, [SrfWeatherWidgetSettings::class]); + register_setting(self::SRF_WEATHER_OPTION_GROUP, self::SRF_WEATHER_API_KEY, [SrfWeatherWidgetSettings::class, 'validateNonEmptyKey']); + register_setting(self::SRF_WEATHER_OPTION_GROUP, self::SRF_WEATHER_API_SECRET, [SrfWeatherWidgetSettings::class, 'validateConnection']); register_setting(self::SRF_WEATHER_OPTION_GROUP, self::DEFAULT_LOCATION, [SrfWeatherWidgetSettings::class, 'validateGeolocation']); register_setting(self::SRF_WEATHER_OPTION_GROUP, self::DEFAULT_LOCATION_NAME); } diff --git a/wp-plugin/srf-weather-widget/tpl/admin_settings.tpl.php b/wp-plugin/srf-weather-widget/tpl/admin_settings.tpl.php index 5e6bc6a..e03a01c 100644 --- a/wp-plugin/srf-weather-widget/tpl/admin_settings.tpl.php +++ b/wp-plugin/srf-weather-widget/tpl/admin_settings.tpl.php @@ -32,21 +32,21 @@
To get an SRF Meteo API Key register here and add the Meteo App here. Use the "SRF-MeteoProductFreemium" Product to get a free account.
Note: The Meteo API only returns data for places in Switzerland.
- - + + = ($errorFields[SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY] ?? ''); ?> - - + + = ($errorFields[SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET] ?? ''); ?>Set either a default location by its location-id (most accurate results, but the api must be used to find out a correct location). Or just use the name of the city.
- + = ($errorFields[SrfWeatherWidgetSettings::DEFAULT_LOCATION] ?? ''); ?> - +