Skip to content

Commit

Permalink
Fix some edge cases (required settings are left out by using console …
Browse files Browse the repository at this point in the history
…asf), update settings page documentation, verify access token
  • Loading branch information
lhausammann committed Oct 3, 2023
1 parent 274e38a commit 297fa85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion wp-plugin/srf-weather-widget/SrfWeatherWidgetApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
47 changes: 37 additions & 10 deletions wp-plugin/srf-weather-widget/SrfWeatherWidgetSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -17,7 +17,7 @@ public function __construct()

public static function validateGeolocation($value) {
if (!$value) {
return $value;
return '';
}

if (!strpos($value, ',')) {
Expand All @@ -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)) {
Expand All @@ -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,
Expand All @@ -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;
}


Expand All @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions wp-plugin/srf-weather-widget/tpl/admin_settings.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
<p>To get an SRF Meteo API Key <a href="https://developer.srgssr.ch/user/register">register here</a> and add the Meteo App <a href="https://developer.srgssr.ch/user/4205/create-app">here</a>. Use the "SRF-MeteoProductFreemium" Product to get a free account.
</p>
<p class="alert-info">Note: The Meteo API only returns data for places in Switzerland.</p>
<label for="srf-weather-widget-auth-key">App ID:</label>
<input type="text" id="srf-weather-widget-auth-key" name="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY; ?>" value="<?php echo esc_attr($apiKey); ?>" />
<label for="srf-weather-widget-auth-key">App ID*</label>
<input type="text" required="true" id="srf-weather-widget-auth-key" name="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY; ?>" value="<?php echo esc_attr($apiKey); ?>" />
<span class="error-field"><?= ($errorFields[SrfWeatherWidgetSettings::SRF_WEATHER_API_KEY] ?? ''); ?></span>

<label for="srf-weather-widget-auth-secret">App Secret:</label>
<input type="text" id="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET; ?>" name="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET; ?>" value="<?php echo esc_attr($apiSecret); ?>" />
<label for="srf-weather-widget-auth-secret">App Secret*</label>
<input type="text" required="true" id="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET; ?>" name="<?=SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET; ?>" value="<?php echo esc_attr($apiSecret); ?>" />
<span class="error-field"><?= ($errorFields[SrfWeatherWidgetSettings::SRF_WEATHER_API_SECRET] ?? ''); ?></span>


<h3>Default location</h3>
<p>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.</p>
<label for="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION; ?>">Default location-id (lat,lon):</label>
<label for="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION; ?>">Default location-id (lat,lon). Will be rounded to the nearest point with forecasts available.</label>
<input type="text" id="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION; ?>" name="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION; ?>" value="<?php echo esc_attr($location); ?>" />
<span class="error-field"><?= ($errorFields[SrfWeatherWidgetSettings::DEFAULT_LOCATION] ?? ''); ?></span>
<label for=<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION_NAME; ?>>Default location name (if no location is given, search by name)</label>
<label for=<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION_NAME; ?>>Default location name. Only set if you don't know the geolocation and have a unique name (like Zurich or Bern)</label>
<input type="text" id="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION_NAME; ?>" name="<?=SrfWeatherWidgetSettings::DEFAULT_LOCATION_NAME;?>" value="<?php echo esc_attr($locationName); ?>" />
<?php submit_button(); ?>
</form>
Expand Down

0 comments on commit 297fa85

Please sign in to comment.