Switch calls to use Client ID instead of Access Token

This commit is contained in:
Justin Foell
2019-06-03 16:07:15 -05:00
parent 33c0deb870
commit 520bd1b649
10 changed files with 125 additions and 57 deletions
+6 -6
View File
@@ -104,16 +104,16 @@ class WPStrava {
* @param string $token Athlete token.
* @return WPStrava_API
*/
public function get_api( $token = null ) {
if ( ! $token ) {
$token = $this->settings->get_default_token();
public function get_api( $id = null ) {
if ( ! $id ) {
$id = $this->settings->get_default_id();
}
if ( empty( $this->api[ $token ] ) ) {
$this->api[ $token ] = new WPStrava_API( $token );
if ( empty( $this->api[ $id ] ) ) {
$this->api[ $id ] = new WPStrava_API( $id );
}
return $this->api[ $token ];
return $this->api[ $id ];
}
/**
+60 -7
View File
@@ -7,10 +7,27 @@ class WPStrava_API {
const STRAVA_V3_API = 'https://www.strava.com/api/v3/';
public function __construct( $access_token = null ) {
$this->access_token = $access_token;
private $client_id = null;
/**
* Constructor.
*
* @param int $client_id Strava API Client ID representing an athlete.
* @author Justin Foell <justin@foell.org>
*/
public function __construct( $client_id = null ) {
$this->client_id = $client_id;
}
/**
* POST something to the Strava API.
*
* @param string $uri Path within the Strava API.
* @param array $data Data to POST.
* @return stdClass Strava API response.
* @throws WPStrava_Exception
* @author Justin Foell <justin@foell.org>
*/
public function post( $uri, $data = null ) {
$url = self::STRAVA_V3_API;
@@ -21,8 +38,9 @@ class WPStrava_API {
'timeout' => 30,
);
if ( $this->access_token ) {
$args['headers']['Authorization'] = 'Bearer ' . $this->access_token;
$access_token = $this->get_access_token();
if ( $access_token ) {
$args['headers']['Authorization'] = 'Bearer ' . $access_token;
}
$response = wp_remote_post( $url . $uri, $args );
@@ -53,6 +71,15 @@ class WPStrava_API {
return json_decode( $response['body'] );
}
/**
* GET something from the Strava API.
*
* @param string $uri Path within the Strava API.
* @param array $args Request arguments.
* @return stdClass Strava API response.
* @throws WPStrava_Exception
* @author Justin Foell <justin@foell.org>
*/
public function get( $uri, $args = null ) {
$url = self::STRAVA_V3_API;
@@ -68,8 +95,9 @@ class WPStrava_API {
'timeout' => 30,
);
if ( $this->access_token ) {
$get_args['headers']['Authorization'] = 'Bearer ' . $this->access_token;
$access_token = $this->get_access_token();
if ( $access_token ) {
$get_args['headers']['Authorization'] = 'Bearer ' . $access_token;
}
$response = wp_remote_get( $url, $get_args );
@@ -102,4 +130,29 @@ class WPStrava_API {
return json_decode( $response['body'] );
}
} // Class API.
/**
* Get the (ever changing) access token based on current Client ID.
*
* @return string|null String for access token, null if not found.
* @author Justin Foell <justin@foell.org>
* @since 2.0.0
*/
private function get_access_token() {
static $access_token = null;
// If client_id not set (OAuth set-up), don't even look.
if ( ! $this->client_id ) {
return null;
}
if ( ! $access_token ) {
$settings = WPStrava::get_instance()->settings;
$info = $settings->info;
if ( ! empty( $info[ $this->client_id ]->access_token ) ) {
$access_token = $info[ $this->client_id ]->access_token;
}
}
return $access_token;
}
}
+9 -9
View File
@@ -10,26 +10,26 @@ class WPStrava_Activity {
/**
* Get single activity by ID.
*
* @param string $athlete_token Token of athlete to retrieve for
* @param string $client_id Client ID of athlete to retrieve for
* @param int $activity_id ID of activity to retrieve.
* @return object stdClass representing this activity.
* @return object stdClass Representing this activity.
* @author Justin Foell <justin@foell.org>
*/
public function get_activity( $athlete_token, $activity_id ) {
return WPStrava::get_instance()->get_api( $athlete_token )->get( "activities/{$activity_id}" );
public function get_activity( $client_id, $activity_id ) {
return WPStrava::get_instance()->get_api( $client_id )->get( "activities/{$activity_id}" );
}
/**
* Get activity list from Strava API.
*
* @author Justin Foell <justin@foell.org>
* @param string $athlete_token Token of athlete to retrieve for
* @param int $club_id Club ID of all club riders (optional).
* @param int|null $quantity Number of records to retrieve (optional).
* @param string $client_id Client ID of athlete to retrieve for
* @param int $club_id Club ID of all club riders (optional).
* @param int|null $quantity Number of records to retrieve (optional).
* @return array Array of activities.
*/
public function get_activities( $athlete_token, $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->get_api( $athlete_token );
public function get_activities( $client_id, $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->get_api( $client_id );
$data = null;
+7 -2
View File
@@ -51,13 +51,18 @@ class WPStrava_ActivityShortcode {
'som' => WPStrava::get_instance()->settings->som,
'map_width' => '480',
'map_height' => '320',
'athlete_token' => WPStrava::get_instance()->settings->get_default_token(),
'client_id' => WPStrava::get_instance()->settings->get_default_id(),
'markers' => false,
'image_only' => false,
);
$atts = shortcode_atts( $defaults, $atts, 'activity' );
if ( isset( $atts['athlete_token'] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
return __( 'The <code>athlete_token</code> parameter is deprecated as of version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' );
}
/* Make sure boolean values are actually boolean
* @see https://wordpress.stackexchange.com/a/119299
*/
@@ -68,7 +73,7 @@ class WPStrava_ActivityShortcode {
$activity_details = null;
try {
$activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] );
$activity_details = $activity->get_activity( $atts['client_id'], $atts['id'] );
} catch ( WPStrava_Exception $e ) {
return $e->to_html();
}
+7 -2
View File
@@ -4,7 +4,7 @@ class WPStrava_LatestActivities {
public static function get_activities_html( $args ) {
$defaults = array(
'athlete_token' => WPStrava::get_instance()->settings->get_default_token(),
'client_id' => WPStrava::get_instance()->settings->get_default_id(),
'strava_club_id' => null,
'quantity' => 5,
'som' => WPStrava::get_instance()->settings->som,
@@ -12,12 +12,17 @@ class WPStrava_LatestActivities {
$args = wp_parse_args( $args, $defaults );
if ( isset( $args['athlete_token'] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
return __( 'The <code>athlete_token</code> parameter is deprecated as of version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' );
}
$som = WPStrava_SOM::get_som( $args['som'] );
$strava_activity = WPStrava::get_instance()->activity;
$activities = array();
try {
$activities = $strava_activity->get_activities( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] );
$activities = $strava_activity->get_activities( $args['client_id'], $args['strava_club_id'], $args['quantity'] );
} catch ( WPStrava_Exception $e ) {
return $e->to_html();
}
@@ -35,7 +35,7 @@ class WPStrava_LatestActivitiesShortcode {
/**
* Shortcode handler for [activities].
*
* [activities som=metric quantity=5 athlete_token=xxx|strava_club_id=yyy]
* [activities som=metric quantity=5 client_id=xxx|strava_club_id=yyy]
*
* @param array $atts Array of attributes (id, som, etc.).
* @return string Shortcode output
+8 -8
View File
@@ -25,7 +25,7 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activities', 'wp-strava' ) : $instance['title'] );
$activities_args = array(
'athlete_token' => isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : null,
'client_id' => isset( $instance['client_id'] ) ? $instance['client_id'] : null,
'strava_club_id' => isset( $instance['strava_club_id'] ) ? $instance['strava_club_id'] : null,
'quantity' => isset( $instance['quantity'] ) ? $instance['quantity'] : null,
);
@@ -42,7 +42,7 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['client_id'] = strip_tags( $new_instance['client_id'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['quantity'] = $new_instance['quantity'];
@@ -52,8 +52,8 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
/** @see WP_Widget::form */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activities', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$all_ids = WPStrava::get_instance()->settings->get_all_ids();
$client_id = isset( $instance['client_id'] ) ? esc_attr( $instance['client_id'] ) : WPStrava::get_instance()->settings->get_default_token();
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
@@ -63,10 +63,10 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $nickname; ?></option>
<label for="<?php echo $this->get_field_id( 'client_id' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'client_id' ); ?>">
<?php foreach ( $all_ids as $id => $nickname ) : ?>
<option value="<?php echo $id; ?>"<?php selected( $id, $client_id ); ?>><?php echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
+10 -10
View File
@@ -17,8 +17,8 @@ class WPStrava_LatestMapWidget extends WP_Widget {
public function form( $instance ) {
// outputs the options form on admin
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity Map', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$all_ids = WPStrava::get_instance()->settings->get_all_ids();
$client_id = isset( $instance['client_id'] ) ? esc_attr( $instance['client_id'] ) : WPStrava::get_instance()->settings->get_default_token();
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
@@ -31,10 +31,10 @@ class WPStrava_LatestMapWidget extends WP_Widget {
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $nickname; ?></option>
<label for="<?php echo $this->get_field_id( 'client_id' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'client_id' ); ?>">
<?php foreach ( $all_ids as $id => $nickname ) : ?>
<option value="<?php echo $id; ?>"<?php selected( $id, $client_id ); ?>><?php echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
@@ -56,7 +56,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
// Processes widget options to be saved from the admin.
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['client_id'] = strip_tags( $new_instance['client_id'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
return $instance;
@@ -71,12 +71,12 @@ class WPStrava_LatestMapWidget extends WP_Widget {
public function widget( $args, $instance ) {
$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();
$client_id = isset( $instance['client_id'] ) ? $instance['client_id'] : WPStrava::get_instance()->settings->get_default_id();
$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;
$id = empty( $strava_club_id ) ? $athlete_token : $strava_club_id;
$id = empty( $strava_club_id ) ? $client_id : $strava_club_id;
// Try our transient first.
$activity_transient = get_transient( 'strava_latest_map_activity_' . $id );
@@ -95,7 +95,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
$activities = array();
try {
$activities = $strava_activity->get_activities( $athlete_token, $strava_club_id );
$activities = $strava_activity->get_activities( $client_id, $strava_club_id );
} catch ( WPStrava_Exception $e ) {
echo $e->to_html();
}
+12 -7
View File
@@ -47,17 +47,22 @@ class WPStrava_RouteShortcode {
$this->add_script = true;
$defaults = array(
'id' => 0,
'som' => WPStrava::get_instance()->settings->som,
'map_width' => '480',
'map_height' => '320',
'athlete_token' => WPStrava::get_instance()->settings->get_default_token(),
'markers' => false,
'image_only' => false,
'id' => 0,
'som' => WPStrava::get_instance()->settings->som,
'map_width' => '480',
'map_height' => '320',
'client_id' => WPStrava::get_instance()->settings->get_default_id(),
'markers' => false,
'image_only' => false,
);
$atts = shortcode_atts( $defaults, $atts, 'route' );
if ( isset( $atts['athlete_token'] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
return __( 'The <code>athlete_token</code> parameter is deprecated as of version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' );
}
/* Make sure boolean values are actually boolean
* @see https://wordpress.stackexchange.com/a/119299
*/
+5 -5
View File
@@ -27,7 +27,7 @@ Also takes the following optional parameters:
* som - english/metric (system of measure - override from default setting).
* map_width - width (width of image in pixels). Note both width and height parameters are limited to 640px except on premium API plans: https://developers.google.com/maps/documentation/maps-static/dev-guide#Imagesizes
* map_height - height (height of image in pixels). See note above on max height.
* athlete_token - specify a different athlete (you can copy this value from https://www.strava.com/settings/api or the wp-strava settings page at /wp-admin/options-general.php?page=wp-strava-options).
* client_id - specify a different athlete (you can copy this value from https://www.strava.com/settings/api or the wp-strava settings page at /wp-admin/options-general.php?page=wp-strava-options).
* markers - Display markers at the start/finish point (true/false, defaults to false).
* image_only - Display only the map image and not the table (true/false, defaults to false).
@@ -43,7 +43,7 @@ This also takes the same optional parameters as the [activity] shortcode above.
* som - english/metric (system of measure - override from default setting).
* quantity - number of activities to show.
* athlete_token - specify a different athlete (you can copy this value from https://www.strava.com/settings/api or the wp-strava settings page at /wp-admin/options-general.php?page=wp-strava-options).
* client_id - specify a different athlete (you can copy this value from https://www.strava.com/settings/api or the wp-strava settings page at /wp-admin/options-general.php?page=wp-strava-options).
* strava_club_id - Will display activity from the specified Strava club ID instead of an athlete.
@@ -57,7 +57,7 @@ Strava Latest Map - shows map of latest activity with option to limit latest map
= Why am I getting "ERROR 401 Unauthorized"? =
When you have multiple athletes saved, the first is considered to be the default athlete. If you use a shortcode to display activity from anyone other than the default athlete, you must add the athlete token (found on the wp-strava settings page) to the shortcode, such as athlete_token=c764a2b199cff281e39f24671760c1b9c9fe005e. If you've recently had to re-authorize with Strava, your athlete token may have changed and will need to be updated in your shortcodes and widgets. For widgets, re-select the athlete you'd like to display and click Save.
When you have multiple athletes saved, the first is considered to be the default athlete. If you use a shortcode to display activity from anyone other than the default athlete, you must add the athlete token (found on the wp-strava settings page) to the shortcode, such as client_id=17791. If you've recently had to re-authorize with Strava, your athlete token may have changed and will need to be updated in your shortcodes and widgets. For widgets, re-select the athlete you'd like to display and click Save.
= Why is my Google Map not showing up? =
@@ -75,11 +75,11 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o
4. Latest Map Widget - shows a map of your most recent activity.
5. Latest Map Widget Settings - settings for the Latest Map Widget. You can limit your activity by minimum distance to show only longer efforts.
6. Activity Shortcode - Shows a map of activity with some statistics.
7. Activity Shortcode Settings - An example activity shortcode. The athlete_token parameter is only needed if your site is connected to multiple athlete accounts.
7. Activity Shortcode Settings - An example activity shortcode. The client_id parameter is only needed if your site is connected to multiple athlete accounts.
8. Route Shortcode - Shows a map of a route.
9. Route Shortcode Settings - An example route shortcode. Add markers=true to show green/red start/stop points.
10. Activities Shortcode - Shows latest athlete activity in a page or post.
11. Activities Shortcode Settings - An example activities shortcode. The athlete_token parameter is only needed if your site is connected to multiple athlete accounts.
11. Activities Shortcode Settings - An example activities shortcode. The client_id parameter is only needed if your site is connected to multiple athlete accounts.
== Changelog ==