From 4668b4fc4e8287520e121916d7ed87dac628195a Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 8 Dec 2017 12:33:29 -0600 Subject: [PATCH] Added check for distance_min and make sure it's a positive integer - fixes Issue #7 Revise name/token update settings Minor formatting --- lib/API.class.php | 21 ++++++++++++------ lib/LatestMapWidget.class.php | 3 ++- lib/Settings.class.php | 41 ++++++++++++++++++++--------------- readme.txt | 6 +++-- 4 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/API.class.php b/lib/API.class.php index 79c9e77..66e8e73 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -40,9 +40,12 @@ class WPStrava_API { else $error = print_r( $response, true ); - return new WP_Error( 'wp-strava_post', - sprintf( __( 'ERROR %s %s - %s', 'wp-strava'), $response['response']['code'], $response['response']['message'], $error ), - $response ); + return new WP_Error( + 'wp-strava_post', + // translators: message shown when there's a problem with ab HTTP POST to the Strava API. + sprintf( __( 'ERROR %1$s %2$s - See full error by adding define( \'WP_STRAVA_DEBUG\' true ); to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), + $error + ); } return json_decode( $response['body'] ); @@ -53,8 +56,9 @@ class WPStrava_API { $url .= $uri; - if ( ! empty( $args ) ) + if ( ! empty( $args ) ) { $url = add_query_arg( $args, $url ); + } $get_args = array( 'headers' => array(), @@ -79,9 +83,12 @@ class WPStrava_API { else $error = print_r( $response, true ); - return new WP_Error( 'wp-strava_get', - sprintf( __( 'ERROR %s %s - %s', 'wp-strava' ), $response['response']['code'], $response['response']['message'], $error ), - $response ); + return new WP_Error( + 'wp-strava_get', + // translators: message shown when there's a problem with an HTTP GET to the Strava API. + sprintf( __( 'ERROR %1$s %2$s - See full error by adding define( \'WP_STRAVA_DEBUG\' true ); to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), + $error + ); } return json_decode( $response['body'] ); diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php index bb4234f..89d925a 100644 --- a/lib/LatestMapWidget.class.php +++ b/lib/LatestMapWidget.class.php @@ -64,7 +64,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] ); $athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token(); - $distance_min = $instance['distance_min']; + $distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] ); $strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id']; $build_new = false; @@ -80,6 +80,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { if ( is_wp_error( $rides ) ) { echo $before_widget; + if ( $title ) echo $before_title . $title . $after_title; if ( WPSTRAVA_DEBUG ) { echo '
';
 					print_r($rides);
diff --git a/lib/Settings.class.php b/lib/Settings.class.php
index 71e9a2e..368f94e 100644
--- a/lib/Settings.class.php
+++ b/lib/Settings.class.php
@@ -1,7 +1,7 @@
 page_name,
-						  array( $this, 'print_strava_options' ) );
+		add_options_page(
+			__( 'Strava Settings', 'wp-strava' ),
+			__( 'Strava', 'wp-strava' ),
+			'manage_options',
+			$this->page_name,
+			array( $this, 'print_strava_options' )
+		);
 	}
 
 	public function init() {
@@ -143,7 +145,7 @@ class WPStrava_Settings {
 		$app_name = sprintf( esc_html( '%s Strava', 'wp-strava' ), $blog_name );
 		$site_url = site_url();
 		$description = 'WP-Strava for ' . $blog_name;
-	   	printf( __( "

Steps:

+ printf( __( "

Steps:

  1. Create your free API Application/Connection here: %2\$s using the following information:
    • @@ -171,7 +173,7 @@ class WPStrava_Settings { public function print_gmaps_instructions() { $maps_url = 'https://developers.google.com/maps/documentation/static-maps/'; - printf( __( "

      Steps:

      + printf( __( "

      Steps:

      1. To use Google map images, you must create a Static Maps API Key. Create a free key by going here: %2\$s and clicking Get a Key
      2. Once you've created your Google Static Maps API Key, enter the key below. @@ -184,7 +186,7 @@ class WPStrava_Settings {

        - +
        option_page ); ?> @@ -245,18 +247,21 @@ class WPStrava_Settings { } public function sanitize_nickname( $nicknames ) { - $second2last = $last = false; if ( ! $this->adding_athlete ) { - $last = end( $nicknames ); - $second2last = prev( $nicknames ); + // Chop $nicknames to same size as tokens. + $nicknames = array_slice( $nicknames, 0, count( $_POST['strava_token'] ) ); - $token_index = count( $nicknames ) - 1; - if ( $last === '' && $second2last !== '' && - empty( $_POST['strava_token'][ $token_index ] ) ) { - // Remove last (blank) entry if not trying to add an additional athlete. - array_pop( $nicknames ); + // Remove indexes from $nicknames that have empty tokens. + foreach( $_POST['strava_token'] as $index => $token ) { + $token = trim( $token ); + if ( empty( $token ) ) { + unset( $nicknames[ $index ] ); + } } + + // Process $nicknames so indexes start with zero. + $nicknames = array_merge( $nicknames, array() ); } foreach ( $nicknames as $index => $nickname ) { @@ -409,7 +414,7 @@ class WPStrava_Settings { * @author Justin Foell * @since 1.2.0 * - * @param integer $number + * @param integer $number Athlete number (default 1). * @return string */ private function get_default_nickname( $number = 1 ) { diff --git a/readme.txt b/readme.txt index 724c062..9b553a5 100755 --- a/readme.txt +++ b/readme.txt @@ -2,8 +2,8 @@ Contributors: cmanon, jrfoell, lancewillett Tags: strava, bicycle, cycling, biking, running, run, swimming, swim, gps, shortcode, widget, plugin Requires at least: 4.6 -Tested up to: 4.7 -Stable tag: 1.1.1 +Tested up to: 4.9 +Stable tag: 1.2.0 License: GPLv2 or later Show your Strava activity on your WordPress site. @@ -37,6 +37,8 @@ Strava Latest Map - shows map of latest activity with option to limit latest map Added multi-athlete configuration. Additional transitions from Ride -> Activity. Updated setup instructions to reflect latest Strava API set up process. +Backwards Compatibility - removed PHP 5.3+ specific operator (should work with PHP 5.2 now - versions 1.1 and 1.1.1 don't). +Reworked error reporting and formatting. = 1.1.1 = Changes to better support translations through https://translate.wordpress.org.