mirror of
https://github.com/10h30/wp-strava.git
synced 2026-06-05 15:10:01 +09:00
Load API IDs in Settings constructor
Make sure auth token is correct for Client ID Add a one-time 404 auth_refresh and retry
This commit is contained in:
+19
-11
@@ -81,8 +81,9 @@ class WPStrava_API {
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
*/
|
||||
public function get( $uri, $args = null ) {
|
||||
$url = self::STRAVA_V3_API;
|
||||
static $retry = true;
|
||||
|
||||
$url = self::STRAVA_V3_API;
|
||||
$url .= $uri;
|
||||
|
||||
if ( ! empty( $args ) ) {
|
||||
@@ -101,11 +102,22 @@ class WPStrava_API {
|
||||
}
|
||||
|
||||
$response = wp_remote_get( $url, $get_args );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
throw WPStrava_Exception::from_wp_error( $response );
|
||||
}
|
||||
|
||||
// Try *one* real-time token refresh if 404.
|
||||
if ( $retry && 404 == $response['response']['code'] ) {
|
||||
$retry = false;
|
||||
$auth = WPStrava::get_instance()->auth;
|
||||
if ( $auth instanceof WPStrava_AuthRefresh ) {
|
||||
$auth->auth_refresh();
|
||||
}
|
||||
return $this->get( $uri, $args );
|
||||
} else {
|
||||
$retry = true;
|
||||
}
|
||||
|
||||
if ( 200 != $response['response']['code'] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
|
||||
|
||||
// See if there's useful info in the body.
|
||||
@@ -138,21 +150,17 @@ class WPStrava_API {
|
||||
* @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;
|
||||
$settings = WPStrava::get_instance()->settings;
|
||||
$info = $settings->info;
|
||||
|
||||
if ( ! empty( $info[ $this->client_id ]->access_token ) ) {
|
||||
$access_token = $info[ $this->client_id ]->access_token;
|
||||
}
|
||||
if ( ! empty( $info[ $this->client_id ]->access_token ) ) {
|
||||
return $info[ $this->client_id ]->access_token;
|
||||
}
|
||||
return $access_token;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ class WPStrava_AuthRefresh extends WPStrava_Auth {
|
||||
'client_id' => $client_id,
|
||||
'redirect_uri' => $this->get_redirect_param(),
|
||||
'approval_prompt' => 'auto',
|
||||
'scope' => 'read,activity:read',
|
||||
'scope' => 'activity:read,read',
|
||||
),
|
||||
$this->auth_url
|
||||
);
|
||||
|
||||
@@ -16,6 +16,17 @@ class WPStrava_Settings {
|
||||
private $option_page = 'wp-strava-settings-group';
|
||||
private $adding_athlete = true;
|
||||
|
||||
|
||||
/**
|
||||
* Settings constructor.
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @since 2.0
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->ids = $this->get_ids();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register actions & filters for menus and authentication.
|
||||
*
|
||||
@@ -54,7 +65,6 @@ class WPStrava_Settings {
|
||||
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' );
|
||||
|
||||
$this->adding_athlete = $this->is_adding_athlete();
|
||||
$this->ids = $this->get_ids();
|
||||
|
||||
if ( $this->ids_empty( $this->ids ) ) {
|
||||
register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) );
|
||||
|
||||
+10
-1
@@ -10,12 +10,16 @@ License: GPLv2 or later
|
||||
|
||||
Show your Strava activity on your WordPress site.
|
||||
|
||||
|
||||
== Description ==
|
||||
|
||||
This plugin uses the Strava API to embed maps and activity for athletes and clubs on your WordPress site. Included are several widgets and shortcodes for showing maps and activity summaries.
|
||||
|
||||
|
||||
= Cron =
|
||||
|
||||
Using WP-Strava after September 2019 requires a working WordPress cron configuration. By default, WordPress has a built-in cron system to run scheduled events, but it relies on your website getting frequent visitors. The Strava authentication token system expires after 6 hours if not refreshed. If you think your site will not get any visitors over the span on 6 hours, you might want to set up a _real_ cron: https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/. Setting up this sort of cron is beyond the scope of support for this free plugin, so you should seek assistance through your host.
|
||||
|
||||
|
||||
= Shortcodes =
|
||||
|
||||
[activity id=NUMBER] - add to any page or post. Shows a summary of the activity plus a map if a google maps key has been added.
|
||||
@@ -53,20 +57,24 @@ Strava Latest Activity List - shows a list of the last few activities.
|
||||
|
||||
Strava Latest Map - shows map of latest activity with option to limit latest map to activities of a certain minimum distance.
|
||||
|
||||
|
||||
== Frequently Asked Questions ==
|
||||
|
||||
= 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 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? =
|
||||
|
||||
If your API key works with other Google Maps plugins but not WP Strava, you may need to enable the "Static Maps" functionality on your google account. This is especially true for people using G Suite accounts (not just a @gmail.com address). While logged into your G Suite email, visit https://console.developers.google.com/apis/library/static-maps-backend.googleapis.com/?q=static and make sure the "Static Maps API" is enabled. For more details see https://wordpress.org/support/topic/no-data-errors/
|
||||
|
||||
|
||||
= I recently uploaded an activity, why is it not showing on my site? =
|
||||
|
||||
WP-Strava caches activity for one hour so your site doesn't hit the Strava API on every page load. If you recently uploaded activity and want to see it right away, go to the Settings -> Strava in the wp-admin dashboard, check the checkbox labeled "Clear cache (images & transient data)" and then click Save Changes.
|
||||
|
||||
|
||||
== Screenshots ==
|
||||
|
||||
1. WP Strava settings - this walks you through connecting the WP Strava plugin to your strava account. You can connect multiple accounts by authenticating each one here. Add your Google Maps key for map display here. You can also set the system of measurement (miles/kilometers) and clear any saved data.
|
||||
@@ -81,6 +89,7 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o
|
||||
10. Activities Shortcode - Shows latest athlete activity in a page or post.
|
||||
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 ==
|
||||
|
||||
= 2.0.0 =
|
||||
|
||||
Reference in New Issue
Block a user