Merge pull request #84 from cmanon/feature/83-api-transient-time-option

Add cache lifetime setting
This commit is contained in:
Justin Foell
2021-02-26 12:05:13 -06:00
committed by GitHub
3 changed files with 47 additions and 3 deletions
+7
View File
@@ -118,10 +118,15 @@ On the WP-Strava settings page you cannot currently remove and add another athle
== Changelog ==
= 2.7.0 =
Add setting to choose API cache interval (1 hour / 15 minutes) https://wordpress.org/support/topic/decrease-update-time-for-group-recent-activities/
= 2.6.0 =
Make reusable components for embed screens and SOMOverride
Add Route Block
= 2.5.1 =
Fix latest_map class replacing echo with return https://wordpress.org/support/topic/latest-activity-map-shortcode-wrong-placed-on-page/
@@ -130,6 +135,7 @@ Fix broken water pace on activity.
Fix pace types and remove 'other' - making 'speed' default.
Fix to remove map link in editor so you don't leave the editor.
= 2.5.0 =
Fix missing translation domain on "Save Changes" in settings. https://wordpress.org/support/topic/small-fix-in-settings-php-function-print_clear_input
@@ -340,6 +346,7 @@ Initial version.
Adds API caching - speed up your page loads 8^)
= 2.0.0 =
Version 2.0 is mandatory after October 15th, 2019. 2.0 settings upgrade instructions: <a href="https://github.com/cmanon/wp-strava/wiki/2.0-Upgrade">https://github.com/cmanon/wp-strava/wiki/2.0-Upgrade</a>.
+5 -1
View File
@@ -95,7 +95,11 @@ class WPStrava_API {
$data = $this->remote_get( $uri, $args );
set_transient( $transient_key, $data, HOUR_IN_SECONDS );
// Default to 1 hour of transient cache unless set otherwise.
$settings = WPStrava::get_instance()->settings;
$cache_time = $settings->cache_time ? absint( $settings->cache_time ) : HOUR_IN_SECONDS;
set_transient( $transient_key, $data, $cache_time );
return $data;
}
+35 -2
View File
@@ -99,9 +99,13 @@ class WPStrava_Settings {
register_setting( $this->option_page, 'strava_no_link', array( $this, 'sanitize_no_link' ) );
add_settings_field( 'strava_no_link', __( 'Links', 'wp-strava' ), array( $this, 'print_no_link_input' ), 'wp-strava', 'strava_options' );
// Cache lifetime.
register_setting( $this->option_page, 'strava_cache_time', array( $this, 'sanitize_cache_time' ) );
add_settings_section( 'strava_cache', __( 'Cache', 'wp-strava' ), null, 'wp-strava' );
add_settings_field( 'strava_cache_time', __( 'Cache time', 'wp-strava' ), array( $this, 'print_cache_input' ), 'wp-strava', 'strava_cache' );
// Clear cache.
register_setting( $this->option_page, 'strava_cache_clear', array( $this, 'sanitize_cache_clear' ) );
add_settings_section( 'strava_cache', __( 'Cache', 'wp-strava' ), null, 'wp-strava' );
add_settings_field( 'strava_cache_clear', __( 'Clear cache', 'wp-strava' ), array( $this, 'print_clear_input' ), 'wp-strava', 'strava_cache' );
}
@@ -530,6 +534,35 @@ class WPStrava_Settings {
return null;
}
/**
* Print dropdown to select cache lifetime.
*
* @author Justin Foell <justin@foell.org>
* @since 2.7.0
*/
public function print_cache_input() {
$fifteen_min = 15 * MINUTE_IN_SECONDS;
?>
<select id="strava_cache_time" name="strava_cache_time">
<option value="<?php echo esc_attr( HOUR_IN_SECONDS )?>" <?php selected( $this->cache_time, HOUR_IN_SECONDS ); ?>><?php esc_html_e( '1 Hour', 'wp-strava' ); ?></option>
<option value="<?php echo esc_attr( $fifteen_min )?>" <?php selected( $this->cache_time, $fifteen_min ); ?>><?php esc_html_e( '15 Minutes', 'wp-strava' ); ?></option>
</select>
<p class="description"><?php esc_html_e( 'How often to refresh data from the Strava API', 'wp-strava' ); ?></p>
<?php
}
/**
* Sanitize cache time input.
*
* @param int $cache_time Selected cache time from dropdown.
* @return int Cache lifetime in seconds.
* @author Justin Foell <justin@foell.org>
* @since 2.7.0
*/
public function sanitize_cache_time( $cache_time ) {
return absint( $cache_time );
}
/**
* Gets all saved strava ids as an array.
*
@@ -598,7 +631,7 @@ class WPStrava_Settings {
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*
* @param integer $number Athlete number (default 1).
* @param int $number Athlete number (default 1).
* @return string
*/
private function get_default_nickname( $number = 1 ) {