From 9bd53800430fb641fdc3f754a173de2d72035355 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 11:54:40 -0500 Subject: [PATCH 1/6] Updated settings method documentation. --- lib/WPStrava/Settings.php | 173 +++++++++++++++++++++++++++++++++++++- 1 file changed, 170 insertions(+), 3 deletions(-) diff --git a/lib/WPStrava/Settings.php b/lib/WPStrava/Settings.php index 28e0326..c922665 100644 --- a/lib/WPStrava/Settings.php +++ b/lib/WPStrava/Settings.php @@ -17,7 +17,12 @@ class WPStrava_Settings { private $option_page = 'wp-strava-settings-group'; private $adding_athlete = true; - //register admin menus + /** + * Register actions & filters for menus and authentication. + * + * @author Justin Foell + * @since 0.62 + */ public function hook() { add_action( 'admin_init', array( $this, 'register_strava_settings' ) ); add_action( 'admin_menu', array( $this, 'add_strava_menu' ) ); @@ -26,7 +31,10 @@ class WPStrava_Settings { } /** - * This runs after options are saved + * Run the OAuth process after options are saved. + * + * @author Justin Foell + * @since 1.0 */ public function maybe_oauth( $value ) { // User is clearing to start-over, don't oauth, ignore other errors. @@ -53,6 +61,12 @@ class WPStrava_Settings { return $value; } + /** + * Add the strava settings menu. + * + * @author Justin Foell + * @since 0.62 + */ public function add_strava_menu() { add_options_page( __( 'Strava Settings', 'wp-strava' ), @@ -63,6 +77,12 @@ class WPStrava_Settings { ); } + /** + * Initialize the settings by getting tokens. + * + * @author Justin Foell + * @since 1.0 + */ public function init() { $this->tokens = $this->get_tokens(); @@ -88,6 +108,12 @@ class WPStrava_Settings { } } + /** + * Register settings using the WP Settings API. + * + * @author Justin Foell + * @since 0.62 + */ public function register_strava_settings() { $this->init(); @@ -125,9 +151,11 @@ class WPStrava_Settings { add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), null, 'wp-strava' ); add_settings_field( 'strava_som', __( 'System of Measurement', 'wp-strava' ), array( $this, 'print_som_input' ), 'wp-strava', 'strava_options' ); - // Hide Time Option. + // Hide Options. register_setting( $this->option_page, 'strava_hide_time', array( $this, 'sanitize_hide_time' ) ); add_settings_field( 'strava_hide_time', __( 'Hide Activity Time', 'wp-strava' ), array( $this, 'print_hide_time_input' ), 'wp-strava', 'strava_options' ); + register_setting( $this->option_page, 'strava_hide_elevation', array( $this, 'sanitize_hide_elevation' ) ); + add_settings_field( 'strava_hide_elevation', __( 'Hide Activity Elevation', 'wp-strava' ), array( $this, 'print_hide_elevation_input' ), 'wp-strava', 'strava_options' ); // Clear cache. register_setting( $this->option_page, 'strava_cache_clear', array( $this, 'sanitize_cache_clear' ) ); @@ -135,6 +163,12 @@ class WPStrava_Settings { add_settings_field( 'strava_cache_clear', __( 'Clear cache (images & transient data)', 'wp-strava' ), array( $this, 'print_clear_input' ), 'wp-strava', 'strava_cache' ); } + /** + * Print the Strava setup instructions. + * + * @author Justin Foell + * @since 0.62 + */ public function print_api_instructions() { $signup_url = 'http://www.strava.com/developers'; $settings_url = 'https://www.strava.com/settings/api'; @@ -173,6 +207,12 @@ class WPStrava_Settings { ); } + /** + * Print the google maps instructions. + * + * @author Justin Foell + * @since 1.1 + */ public function print_gmaps_instructions() { $maps_url = 'https://developers.google.com/maps/documentation/static-maps/'; printf( __( "

Steps:

@@ -183,6 +223,12 @@ class WPStrava_Settings { } + /** + * Print the settings page container. + * + * @author Justin Foell + * @since 0.62 + */ public function print_strava_options() { ?>
@@ -201,18 +247,36 @@ class WPStrava_Settings { + * @since 1.2.0 + */ public function print_client_input() { ?> + * @since 1.2.0 + */ public function print_secret_input() { ?> + * @since 1.2.0 + */ public function print_nickname_input() { $nickname = $this->tokens_empty( $this->tokens ) ? __( 'Default', 'wp-strava' ) : ''; ?> @@ -220,6 +284,12 @@ class WPStrava_Settings { + * @since 0.62 + */ public function print_token_input() { foreach ( $this->get_all_tokens() as $token => $nickname ) { ?> @@ -230,6 +300,14 @@ class WPStrava_Settings { } } + /** + * Sanitize the client ID. + * + * @param string $client_id + * @return string + * @author Justin Foell + * @since 1.2.0 + */ public function sanitize_client_id( $client_id ) { // Return early if not trying to add an additional athlete. if ( ! $this->adding_athlete ) { @@ -242,6 +320,14 @@ class WPStrava_Settings { return $client_id; } + /** + * Sanitize the client secret. + * + * @param string $client_secret + * @return string + * @author Justin Foell + * @since 1.2.0 + */ public function sanitize_client_secret( $client_secret ) { // Return early if not trying to add an additional athlete. if ( ! $this->adding_athlete ) { @@ -254,6 +340,14 @@ class WPStrava_Settings { return $client_secret; } + /** + * Sanitize the nicknames - make sure we've got the same number of nicknames sa tokens. + * + * @param array $nicknames Nicknames for the athletes saved. + * @return array + * @author Justin Foell + * @since 1.2.0 + */ public function sanitize_nickname( $nicknames ) { if ( ! $this->adding_athlete ) { @@ -281,10 +375,26 @@ class WPStrava_Settings { return $nicknames; } + /** + * Sanitize the token. + * + * @param string $token + * @return string + * @author Justin Foell + * @since 0.62 + */ public function sanitize_token( $token ) { return $token; } + /** + * Fetch the access token from Strava - previously get_token() + * + * @param string $code + * @return mixed Boolean false if there was a problem, token string otherwise. + * @author Justin Foell + * @since 1.0 + */ private function fetch_token( $code ) { $client_id = $this->client_id; $client_secret = $this->client_secret; @@ -317,16 +427,36 @@ class WPStrava_Settings { return false; } + /** + * Print the GMaps key input. + * + * @author Justin Foell + * @since 1.1 + */ public function print_gmaps_key_input() { ?> + * @since 1.1 + */ public function sanitize_gmaps_key( $key ) { return $key; } + /** + * Print System of Measure option. + * + * @author Justin Foell + * @since 0.62 + */ public function print_som_input() { ?> + * @since 1.1 + */ public function sanitize_cache_clear( $checked ) { if ( 'on' === $checked ) { global $wpdb; @@ -494,6 +646,13 @@ class WPStrava_Settings { } } + /** + * Getter for Strava settings in wp_options. + * + * @param string $name Option name without the 'strava_' prefix. + * @return mixed + * @since 0.62 + */ public function __get( $name ) { if ( ! strpos( 'strava_', $name ) ) { $name = "strava_{$name}"; @@ -502,6 +661,14 @@ class WPStrava_Settings { return get_option( $name ); } + /** + * Link to the settings on the plugin list page. + * + * @param array $links Array of plugin links. + * @return array Links with settings added. + * @author Justin Foell + * @since 1.0 + */ public function settings_link( $links ) { $settings_link = 'page_name}" ) . '">' . __( 'Settings', 'wp-strava' ) . ''; $links[] = $settings_link; From 8444866558ee920338993c46895bee4bee1378eb Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 14:23:46 -0500 Subject: [PATCH 2/6] Added setting to hide elevation --- lib/WPStrava/ActivityShortcode.php | 21 +++++++++++++++------ lib/WPStrava/LatestActivities.php | 8 ++++++-- lib/WPStrava/RouteShortcode.php | 18 +++++++++++++++--- lib/WPStrava/Settings.php | 27 +++++++++++++++++++++++++++ readme.txt | 5 +++++ 5 files changed, 68 insertions(+), 11 deletions(-) diff --git a/lib/WPStrava/ActivityShortcode.php b/lib/WPStrava/ActivityShortcode.php index 8b5a459..d1d5fa4 100644 --- a/lib/WPStrava/ActivityShortcode.php +++ b/lib/WPStrava/ActivityShortcode.php @@ -107,11 +107,14 @@ class WPStrava_ActivityShortcode { private function get_table( $activity_details, $som ) { $strava_som = WPStrava_SOM::get_som( $som ); $strava_activitytype = WPStrava_ActivityType::get_type_group( $activity_details->type ); - $avg_speed = ''; - $max_speed = ''; - $speed_label = ''; $avg_title = '' . __( 'Average Speed', 'wp-strava' ) . ''; $max_title = '' . __( 'Max Speed', 'wp-strava' ) . ''; + $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; + $avg_speed = ''; + $max_speed = ''; + $elevation = '' . $strava_som->elevation( $activity_details->total_elevation_gain ) . ''; + $speed_label = ''; + $elevation_label = '' . $strava_som->get_elevation_label() . ''; switch ( $strava_activitytype ) { case WPStrava_ActivityType::TYPE_GROUP_PACE: @@ -135,6 +138,12 @@ class WPStrava_ActivityShortcode { break; } + if ( WPStrava::get_instance()->settings->hide_elevation ) { + $elevation = ''; + $elevation_title = ''; + $elevation_label = ''; + } + return ' @@ -144,7 +153,7 @@ class WPStrava_ActivityShortcode { ' . $avg_title . ' ' . $max_title . ' - + ' . $elevation_title . ' @@ -154,7 +163,7 @@ class WPStrava_ActivityShortcode { ' . $avg_speed . ' ' . $max_speed . ' - + ' . $elevation . ' @@ -162,7 +171,7 @@ class WPStrava_ActivityShortcode { ' . $speed_label . ' ' . $speed_label . ' - + ' . $elevation_label . '
' . __( 'Distance', 'wp-strava' ) . '' . __( 'Elevation Gain', 'wp-strava' ) . '
' . $strava_som->distance( $activity_details->distance ) . '' . $strava_som->elevation( $activity_details->total_elevation_gain ) . '
' . $strava_som->get_time_label() . '' . $strava_som->get_distance_label() . '' . $strava_som->get_elevation_label() . '
diff --git a/lib/WPStrava/LatestActivities.php b/lib/WPStrava/LatestActivities.php index 0117532..3ae8597 100644 --- a/lib/WPStrava/LatestActivities.php +++ b/lib/WPStrava/LatestActivities.php @@ -50,8 +50,12 @@ class WPStrava_LatestActivities { $response .= sprintf( __( ' went %1$s %2$s', 'wp-strava' ), $som->distance( $activity->distance ), $som->get_distance_label() ); // Translators: "during 2 hours" $response .= sprintf( __( ' during %1$s %2$s', 'wp-strava' ), $som->time( $activity->elapsed_time ), $som->get_time_label() ); - // Translators: "climbing 100 ft." - $response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $activity->total_elevation_gain ), $som->get_elevation_label() ); + + if ( ! WPStrava::get_instance()->settings->hide_elevation ) { + // Translators: "climbing 100 ft." + $response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $activity->total_elevation_gain ), $som->get_elevation_label() ); + } + $response .= '
'; } $response .= ''; diff --git a/lib/WPStrava/RouteShortcode.php b/lib/WPStrava/RouteShortcode.php index a0a9a51..323a524 100644 --- a/lib/WPStrava/RouteShortcode.php +++ b/lib/WPStrava/RouteShortcode.php @@ -105,25 +105,37 @@ class WPStrava_RouteShortcode { */ private function get_table( $route_details, $som ) { $strava_som = WPStrava_SOM::get_som( $som ); + + + $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; + $elevation = '' . $strava_som->elevation( $route_details->elevation_gain ) . ''; + $elevation_label = '' . $strava_som->get_elevation_label() . ''; + + if ( WPStrava::get_instance()->settings->hide_elevation ) { + $elevation = ''; + $elevation_title = ''; + $elevation_label = ''; + } + return ' - + ' . $elevation_title . ' - + ' . $elevation . ' - + ' . $elevation_label . '
' . __( 'Est. Moving Time', 'wp-strava' ) . ' ' . __( 'Distance', 'wp-strava' ) . '' . __( 'Elevation Gain', 'wp-strava' ) . '
' . $strava_som->time( $route_details->estimated_moving_time ) . ' ' . $strava_som->distance( $route_details->distance ) . '' . $strava_som->elevation( $route_details->elevation_gain ) . '
' . $strava_som->get_time_label() . ' ' . $strava_som->get_distance_label() . '' . $strava_som->get_elevation_label() . '
diff --git a/lib/WPStrava/Settings.php b/lib/WPStrava/Settings.php index c922665..b68d34f 100644 --- a/lib/WPStrava/Settings.php +++ b/lib/WPStrava/Settings.php @@ -505,6 +505,33 @@ class WPStrava_Settings { return null; } + /** + * Display the Hide Elevation Checkbox. + * + * @author Justin Foell + * @since 1.7.2 + */ + public function print_hide_elevation_input() { + ?> + hide_elevation, 'on' ); ?>/> + + * @since 1.7.2 + */ + public function sanitize_hide_elevation( $checked ) { + if ( 'on' === $checked ) { + return $checked; + } + return null; + } + /** * Print checkbox option to clear cache. * diff --git a/readme.txt b/readme.txt index 64a51ea..3921aeb 100644 --- a/readme.txt +++ b/readme.txt @@ -83,6 +83,11 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o == Changelog == += 1.7.2 = + +Added setting to hide elevation. + + = 1.7.1 = Added PHPUnit tests for all System of Measure calculations. From 3e14f06a48120666a009adb97f244475c34604b3 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 14:56:49 -0500 Subject: [PATCH 3/6] Fix issue with activities longer than 24h --- lib/WPStrava/SOM.php | 6 +++++- readme.txt | 1 + tests/WPStrava/SOMEnglishTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/WPStrava/SOM.php b/lib/WPStrava/SOM.php index 485bfb8..11c4edb 100644 --- a/lib/WPStrava/SOM.php +++ b/lib/WPStrava/SOM.php @@ -33,9 +33,13 @@ abstract class WPStrava_SOM { * Create a time string of hours:minutes:seconds from just seconds. * * @return string Time formatted as 'H:i:s'. + * @see https://stackoverflow.com/a/20870843/2146022 */ public function time( $seconds ) { - return date( 'H:i:s', mktime( 0, 0, $seconds ) ); + $zero = new DateTime( '@0' ); + $offset = new DateTime( "@{$seconds}" ); + $diff = $zero->diff( $offset ); + return sprintf( '%02d:%02d:%02d', $diff->days * 24 + $diff->h, $diff->i, $diff->s ); } /** diff --git a/readme.txt b/readme.txt index 3921aeb..497e39b 100644 --- a/readme.txt +++ b/readme.txt @@ -86,6 +86,7 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o = 1.7.2 = Added setting to hide elevation. +Fixed hours for activities greater than 24 hours. = 1.7.1 = diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php index f72d9cd..65cc3a5 100644 --- a/tests/WPStrava/SOMEnglishTest.php +++ b/tests/WPStrava/SOMEnglishTest.php @@ -80,4 +80,16 @@ class WPStrava_SOMEnglishTest extends TestCase { $this->assertEquals( '01:20:05', $this->som->time( 4805 ) ); } + + /** + * Test that 221071 seconds is 61:24:31 time (H:i:s) using both string and float inputs. + * + * @author Justin Foell + * @since 1.7.2 + */ + public function test_time_greater24h() { + $this->assertEquals( '61:24:31', $this->som->time( '221071' ) ); + $this->assertEquals( '61:24:31', $this->som->time( 221071 ) ); + } + } From adf1612a399b4f51583102f43c8c35922e27713d Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 14:57:22 -0500 Subject: [PATCH 4/6] Cleaned up @author --- lib/WPStrava/Settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WPStrava/Settings.php b/lib/WPStrava/Settings.php index b68d34f..1db745f 100644 --- a/lib/WPStrava/Settings.php +++ b/lib/WPStrava/Settings.php @@ -305,7 +305,7 @@ class WPStrava_Settings { * * @param string $client_id * @return string - * @author Justin Foell + * @author Justin Foell * @since 1.2.0 */ public function sanitize_client_id( $client_id ) { From 4a9b531fa61989062987b1eefc54adf878f2232b Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 15:57:14 -0500 Subject: [PATCH 5/6] Added scale=2 to maps image --- lib/WPStrava/StaticMap.php | 2 +- readme.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/WPStrava/StaticMap.php b/lib/WPStrava/StaticMap.php index 14b4894..d84e5b8 100644 --- a/lib/WPStrava/StaticMap.php +++ b/lib/WPStrava/StaticMap.php @@ -23,7 +23,7 @@ class WPStrava_StaticMap { return ''; } - $url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:"; + $url = "https://maps.googleapis.com/maps/api/staticmap?maptype=terrain&size={$width}x{$height}&scale=2&sensor=false&key={$key}&path=color:0xFF0000BF|weight:2|enc:"; $url_len = strlen( $url ); $max_chars = 1865; diff --git a/readme.txt b/readme.txt index 497e39b..2d3f0e2 100644 --- a/readme.txt +++ b/readme.txt @@ -87,6 +87,7 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o Added setting to hide elevation. Fixed hours for activities greater than 24 hours. +Added scale=2 to static map to which allows for greater pixel resolution (up to 1024x1024 at 2x) for Google Maps API Premium Plan subscribers https://developers.google.com/maps/documentation/maps-static/dev-guide#Imagesizes = 1.7.1 = From 3985244ffad0eab74c6e0728fad2d07b1ce9025d Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 2 Aug 2019 15:58:28 -0500 Subject: [PATCH 6/6] Updated to 1.7.2 --- readme.txt | 2 +- wp-strava.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.txt b/readme.txt index 2d3f0e2..290d264 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Contributors: cmanon, jrfoell, lancewillett, dlintott, sebastianerb Tags: strava, activity, bicycle, cycling, biking, running, run, swimming, swim, gps, shortcode, widget, plugin Requires at least: 4.6 Tested up to: 5.1 -Stable tag: 1.7.1 +Stable tag: 1.7.2 Requires PHP: 5.2 License: GPLv2 or later diff --git a/wp-strava.php b/wp-strava.php index 1c08fa9..3aad5c5 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ * Plugin Name: WP Strava * Plugin URI: https://wordpress.org/plugins/wp-strava/ * Description: Show your strava.com activity on your WordPress site. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. - * Version: 1.7.1 + * Version: 1.7.2 * Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott, Sebastian Erb * License: GPL2 * Text Domain: wp-strava