mirror of
https://github.com/10h30/wp-strava.git
synced 2026-07-12 19:26:50 +09:00
Working multi tokens with nicknames
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
*~
|
||||
.svn
|
||||
.vscode
|
||||
|
||||
+10
-7
@@ -9,7 +9,7 @@ class WPStrava_API {
|
||||
//const STRAVA_V2_API = 'http://www.strava.com/api/v2/'; //rides/:ride_id/map_details
|
||||
const STRAVA_V3_API = 'https://www.strava.com/api/v3/';
|
||||
|
||||
public function __construct( $access_token ) {
|
||||
public function __construct( $access_token = null ) {
|
||||
$this->access_token = $access_token;
|
||||
}
|
||||
|
||||
@@ -19,11 +19,13 @@ class WPStrava_API {
|
||||
$args = array(
|
||||
'body' => http_build_query( $data ),
|
||||
'sslverify' => false,
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
)
|
||||
'headers' => array(),
|
||||
);
|
||||
|
||||
if ( $this->access_token ) {
|
||||
$args['headers']['Authorization'] = 'Bearer ' . $this->access_token;
|
||||
}
|
||||
|
||||
$response = wp_remote_post( $url . $uri, $args );
|
||||
|
||||
if ( is_wp_error( $response ) )
|
||||
@@ -55,10 +57,11 @@ class WPStrava_API {
|
||||
$url = add_query_arg( $args, $url );
|
||||
|
||||
$get_args = array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $this->access_token,
|
||||
)
|
||||
'headers' => array(),
|
||||
);
|
||||
if ( $this->access_token ) {
|
||||
$get_args['headers']['Authorization'] = 'Bearer ' . $this->access_token;
|
||||
}
|
||||
|
||||
$response = wp_remote_get( $url, $get_args );
|
||||
|
||||
|
||||
+156
-44
@@ -12,7 +12,7 @@
|
||||
class WPStrava_Settings {
|
||||
|
||||
private $feedback;
|
||||
private $token;
|
||||
private $tokens = array();
|
||||
private $page_name = 'wp-strava-options';
|
||||
private $option_page = 'wp-strava-settings-group';
|
||||
|
||||
@@ -22,6 +22,7 @@ class WPStrava_Settings {
|
||||
add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
|
||||
add_filter( 'pre_set_transient_settings_errors', array( $this, 'maybe_oauth' ) );
|
||||
add_filter( 'plugin_action_links_' . WPSTRAVA_PLUGIN_NAME, array( $this, 'settings_link' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'settings_scripts' ) );
|
||||
//for process debugging
|
||||
//add_action( 'all', array( $this, 'hook_debug' ) );
|
||||
//add_filter( 'all', array( $this, 'hook_debug' ) );
|
||||
@@ -34,26 +35,24 @@ class WPStrava_Settings {
|
||||
/**
|
||||
* This runs after options are saved
|
||||
*/
|
||||
public function maybe_oauth( $value ) {
|
||||
public function maybe_oauth( $value ) {
|
||||
// User is clearing to start-over, don't oauth, ignore other errors.
|
||||
if ( isset( $_POST['strava_token'] ) && $this->tokens_empty( $_POST['strava_token'] ) )
|
||||
return array();
|
||||
|
||||
// Redirect only if all the right options are in place.
|
||||
if ( isset( $value[0]['type'] ) && $value[0]['type'] == 'updated' ) { // Make sure there were no settings errors.
|
||||
if ( isset( $value[0]['type'] ) && 'updated' == $value[0]['type'] ) { // Make sure there were no settings errors.
|
||||
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { // Make sure we're on our settings page.
|
||||
|
||||
// User is clearing to start-over, don't oauth.
|
||||
if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
|
||||
return;
|
||||
|
||||
// Only re-auth if client ID and secret were shown.
|
||||
// Only re-auth if client ID and secret were saved.
|
||||
if ( ! empty( $_POST['strava_client_id'] ) && ! empty( $_POST['strava_client_secret'] ) ) {
|
||||
$client_id = get_option( 'strava_client_id' );
|
||||
$client_secret = get_option( 'strava_client_secret' );
|
||||
$client_id = $_POST['strava_client_id'];
|
||||
$client_secret = $_POST['strava_client_secret'];
|
||||
|
||||
if ( $client_id && $client_secret ) {
|
||||
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
|
||||
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
|
||||
wp_redirect( $url );
|
||||
exit();
|
||||
}
|
||||
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
|
||||
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
|
||||
wp_redirect( $url );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,22 +68,24 @@ class WPStrava_Settings {
|
||||
}
|
||||
|
||||
public function init() {
|
||||
$this->tokens = $this->get_tokens();
|
||||
|
||||
//only update when redirected back from strava
|
||||
if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] == $this->page_name ) {
|
||||
if ( isset( $_GET['code'] ) ) {
|
||||
$token = $this->get_token( $_GET['code'] );
|
||||
if ( $token ) {
|
||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
|
||||
update_option( 'strava_token', $token );
|
||||
$this->add_token( $token );
|
||||
update_option( 'strava_token', $this->tokens );
|
||||
} else {
|
||||
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
|
||||
}
|
||||
} else if ( isset( $_GET['error'] ) ) {
|
||||
} elseif ( isset( $_GET['error'] ) ) {
|
||||
// Translators: authentication error mess
|
||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'Error authenticating at Strava: %s', 'wp-strava' ), str_replace( '_', ' ', $_GET['error'] ) ) );
|
||||
}
|
||||
}
|
||||
|
||||
$this->token = get_option( 'strava_token' );
|
||||
}
|
||||
|
||||
public function register_strava_settings() {
|
||||
@@ -92,15 +93,26 @@ class WPStrava_Settings {
|
||||
|
||||
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' );
|
||||
|
||||
if ( ! $this->token ) {
|
||||
if ( $this->tokens_empty( $this->tokens ) ) {
|
||||
register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) );
|
||||
register_setting( $this->option_page, 'strava_client_secret', array( $this, 'sanitize_client_secret' ) );
|
||||
register_setting( $this->option_page, 'strava_nickname', array( $this, 'sanitize_nickname' ) );
|
||||
|
||||
add_settings_field( 'strava_client_id', __( 'Strava Client ID', 'wp-strava' ), array( $this, 'print_client_input' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_client_secret', __( 'Strava Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_nickname', __( 'Strava Nickname', 'wp-strava' ), array( $this, 'print_nickname_input' ), 'wp-strava', 'strava_api' );
|
||||
} else {
|
||||
register_setting( $this->option_page, 'strava_token', array( $this, 'sanitize_token' ) );
|
||||
add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' );
|
||||
|
||||
// Add additional fields
|
||||
register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) );
|
||||
register_setting( $this->option_page, 'strava_client_secret', array( $this, 'sanitize_client_secret' ) );
|
||||
register_setting( $this->option_page, 'strava_nickname', array( $this, 'sanitize_nickname' ) );
|
||||
|
||||
add_settings_field( 'strava_client_id', __( 'Additional Athlete Client ID', 'wp-strava' ), array( $this, 'print_client_input' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_client_secret', __( 'Additional Athlete Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_nickname', __( 'Additional Athlete Nickname', 'wp-strava' ), array( $this, 'print_nickname_input' ), 'wp-strava', 'strava_api' );
|
||||
}
|
||||
|
||||
// Google Maps API.
|
||||
@@ -122,18 +134,18 @@ class WPStrava_Settings {
|
||||
public function print_api_instructions() {
|
||||
$signup_url = 'http://www.strava.com/developers';
|
||||
$settings_url = 'https://www.strava.com/settings/api';
|
||||
$blog_name = get_bloginfo( 'name' );
|
||||
$app_name = sprintf( esc_html( '%s Strava', 'wp-strava' ), $blog_name );
|
||||
$blog_name = get_bloginfo( 'name' );
|
||||
$app_name = sprintf( esc_html( '%s Strava', 'wp-strava' ), $blog_name );
|
||||
$site_url = site_url();
|
||||
$description = 'WP-Strava for ' . $blog_name;
|
||||
printf( __( "<p>Steps:</p>
|
||||
<ol>
|
||||
<li>Create your free API Application/Connection here: <a href='%s' target='_blank'>%s</a> using the following information:</li>
|
||||
<li>Create your free API Application/Connection here: <a href='%1\$s' target='_blank'>%2\$s</a> using the following information:</li>
|
||||
<ul>
|
||||
<li>Application Name: <strong>%s</strong></li>
|
||||
<li>Website: <strong>%s</strong></li>
|
||||
<li>Application Description: <strong>%s</strong></li>
|
||||
<li>Authorization Callback Domain: <strong>%s</strong></li>
|
||||
<li>Application Name: <strong>%3\$s</strong></li>
|
||||
<li>Website: <strong>%4\$s</strong></li>
|
||||
<li>Application Description: <strong>%5\$s</strong></li>
|
||||
<li>Authorization Callback Domain: <strong>%6\$s</strong></li>
|
||||
</ul>
|
||||
<li>Once you've created your API Application at strava.com, enter the <strong>Client ID</strong> and <strong>Client Secret</strong> below, which can now be found on that same strava API Settings page.
|
||||
<li>After saving your Client ID and Secret, you'll be redirected to strava to authorize your API Application. If successful, your Strava Token will display instead of Client ID and Client Secret.</li>
|
||||
@@ -145,7 +157,7 @@ class WPStrava_Settings {
|
||||
$maps_url = 'https://developers.google.com/maps/documentation/static-maps/';
|
||||
printf( __( "<p>Steps:</p>
|
||||
<ol>
|
||||
<li>To use Google map images, you must create a Static Maps API Key. Create a free key by going here: <a href='%s' target='_blank'>%s</a> and clicking <strong>Get a Key</strong></li>
|
||||
<li>To use Google map images, you must create a Static Maps API Key. Create a free key by going here: <a href='%1\$s' target='_blank'>%2\$s</a> and clicking <strong>Get a Key</strong></li>
|
||||
<li>Once you've created your Google Static Maps API Key, enter the key below.
|
||||
</ol>", 'wp-strava' ), $maps_url, $maps_url );
|
||||
|
||||
@@ -155,14 +167,14 @@ class WPStrava_Settings {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<div id="icon-options-general" class="icon32"><br/></div>
|
||||
<h2><?php _e( 'Strava Settings', 'wp-strava' ); ?></h2>
|
||||
<h2><?php esc_html_e( 'Strava Settings', 'wp-strava' ); ?></h2>
|
||||
|
||||
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
|
||||
<?php settings_fields( $this->option_page ); ?>
|
||||
<?php do_settings_sections( 'wp-strava' ); ?>
|
||||
|
||||
<p class="submit">
|
||||
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'wp-strava' ); ?>" />
|
||||
<input type="submit" class="button-primary" value="<?php esc_html_e( 'Save Changes', 'wp-strava' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
@@ -170,15 +182,29 @@ class WPStrava_Settings {
|
||||
}
|
||||
|
||||
public function print_client_input() {
|
||||
?><input type="text" id="strava_client_id" name="strava_client_id" value="<?php echo get_option( 'strava_client_id' ); ?>" /><?php
|
||||
?><input type="text" id="strava_client_id" name="strava_client_id" value="" /><?php
|
||||
}
|
||||
|
||||
public function print_secret_input() {
|
||||
?><input type="text" id="strava_client_secret" name="strava_client_secret" value="<?php echo get_option( 'strava_client_secret' ); ?>" /><?php
|
||||
?><input type="text" id="strava_client_secret" name="strava_client_secret" value="" /><?php
|
||||
}
|
||||
|
||||
public function print_nickname_input() {
|
||||
$nickname = $this->tokens_empty( $this->tokens ) ? __( 'default', 'wp-strava' ) : '';
|
||||
?><input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" /><?php
|
||||
}
|
||||
|
||||
public function print_token_input() {
|
||||
?><input type="text" id="strava_token" name="strava_token" value="<?php echo get_option( 'strava_token' ); ?>" /><?php
|
||||
$nicknames = $this->nickname;
|
||||
foreach ( $this->get_tokens() as $index => $token ) {
|
||||
?>
|
||||
<input type="text" name="strava_token[]" value="<?php echo $token; ?>" />
|
||||
<?php if ( ! empty( $nicknames[ $index ] ) ) : ?>
|
||||
<input type="text" name="strava_nickname[]" value="<?php echo $nicknames[ $index ]; ?>" />
|
||||
<?php endif; ?>
|
||||
<br/>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
public function sanitize_client_id( $client_id ) {
|
||||
@@ -189,23 +215,39 @@ class WPStrava_Settings {
|
||||
}
|
||||
|
||||
public function sanitize_client_secret( $client_secret ) {
|
||||
if ( trim( $client_secret ) == '' ) {
|
||||
if ( '' == trim( $client_secret ) ) {
|
||||
add_settings_error( 'strava_client_secret', 'strava_client_secret', __( 'Client Secret is required.', 'wp-strava' ) );
|
||||
}
|
||||
return $client_secret;
|
||||
}
|
||||
|
||||
public function sanitize_nickname( $nicknames ) {
|
||||
foreach ( $nicknames as $nickname ) {
|
||||
if ( '' == trim( $nickname ) ) {
|
||||
add_settings_error( 'strava_nickname', 'strava_nickname', __( 'Nickname is required.', 'wp-strava' ) );
|
||||
return $nicknames;
|
||||
}
|
||||
}
|
||||
return $nicknames;
|
||||
}
|
||||
|
||||
public function sanitize_token( $token ) {
|
||||
return $token;
|
||||
}
|
||||
|
||||
private function get_token( $code ) {
|
||||
$client_id = get_option( 'strava_client_id' );
|
||||
$client_secret = get_option( 'strava_client_secret' );
|
||||
$client_id = $this->client_id;
|
||||
$client_secret = $this->client_secret;
|
||||
|
||||
delete_option( 'strava_client_id' );
|
||||
delete_option( 'strava_client_secret' );
|
||||
|
||||
if ( $client_id && $client_secret ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
|
||||
$api = new WPStrava_API();
|
||||
$data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'code' => $code );
|
||||
$strava_info = WPStrava::get_instance()->api->post( 'oauth/token', $data );
|
||||
|
||||
$strava_info = $api->post( 'oauth/token', $data );
|
||||
|
||||
if ( $strava_info ) {
|
||||
if ( isset( $strava_info->access_token ) ) {
|
||||
@@ -222,11 +264,11 @@ class WPStrava_Settings {
|
||||
} else {
|
||||
$this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' );
|
||||
return false;
|
||||
}
|
||||
} // End if.
|
||||
}
|
||||
|
||||
public function print_gmaps_key_input() {
|
||||
?><input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo get_option( 'strava_gmaps_key' ); ?>" /><?php
|
||||
?><input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo $this->gmaps_key; ?>" /><?php
|
||||
}
|
||||
|
||||
public function sanitize_gmaps_key( $key ) {
|
||||
@@ -234,11 +276,10 @@ class WPStrava_Settings {
|
||||
}
|
||||
|
||||
public function print_som_input() {
|
||||
$strava_som = get_option( 'strava_som' );
|
||||
?>
|
||||
<select id="strava_som" name="strava_som">
|
||||
<option value="metric" <?php selected( $strava_som, 'metric' ); ?>><?php _e( 'Metric', 'wp-strava' )?></option>
|
||||
<option value="english" <?php selected( $strava_som, 'english' ); ?>><?php _e( 'English', 'wp-strava' )?></option>
|
||||
<option value="metric" <?php selected( $this->som, 'metric' ); ?>><?php esc_html_e( 'Metric', 'wp-strava' )?></option>
|
||||
<option value="english" <?php selected( $this->som, 'english' ); ?>><?php esc_html_e( 'English', 'wp-strava' )?></option>
|
||||
</select>
|
||||
<?php
|
||||
}
|
||||
@@ -261,8 +302,73 @@ class WPStrava_Settings {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all saved strava tokens as an array.
|
||||
*
|
||||
* @return array
|
||||
* @author Justin Foell
|
||||
* @since NEXT
|
||||
*/
|
||||
public function get_tokens() {
|
||||
$tokens = get_option( 'strava_token' );
|
||||
if ( ! is_array( $tokens ) ) {
|
||||
$tokens = array( $tokens );
|
||||
}
|
||||
|
||||
foreach ( $tokens as $index => $token ) {
|
||||
if ( empty( $token ) ) {
|
||||
unset( $tokens[ $index ] );
|
||||
$tokens = array_values( $tokens ); // Rebase array keys after unset @see https://stackoverflow.com/a/5943165/2146022
|
||||
}
|
||||
}
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for valid tokens.
|
||||
*
|
||||
* @author Justin Foell
|
||||
* @since NEXT
|
||||
*
|
||||
* @param string|array Single token or array of tokens.
|
||||
* @return boolean True if empty.
|
||||
*/
|
||||
public function tokens_empty( $tokens ) {
|
||||
if ( empty( $tokens ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( is_array( $tokens ) ) {
|
||||
foreach( $tokens as $token ) {
|
||||
if ( ! empty( $token ) ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only add a token if it's not already there.
|
||||
*
|
||||
* @param string $token
|
||||
*
|
||||
* @author Justin Foell
|
||||
* @since NEXT
|
||||
*/
|
||||
public function add_token( $token ) {
|
||||
if ( false === array_search( $token, $this->tokens ) ) {
|
||||
$this->tokens[] = $token;
|
||||
}
|
||||
}
|
||||
|
||||
public function __get( $name ) {
|
||||
return get_option( "strava_{$name}" );
|
||||
if ( ! strpos( 'strava_', $name ) ) {
|
||||
$name = "strava_{$name}";
|
||||
}
|
||||
// Else.
|
||||
return get_option( $name );
|
||||
}
|
||||
|
||||
public function settings_link( $links ) {
|
||||
@@ -271,4 +377,10 @@ class WPStrava_Settings {
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function settings_scripts() {
|
||||
$screen = get_current_screen();
|
||||
if ( "settings_page_{$this->page_name}" === $screen->id ) {
|
||||
wp_enqueue_script( 'wp-strava-settings', WPSTRAVA_PLUGIN_URL . 'js/wp-strava-settings.js', array( 'jquery' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-10
@@ -11,7 +11,7 @@ class WPStrava {
|
||||
|
||||
private static $instance = null;
|
||||
private $settings = null;
|
||||
private $api = null;
|
||||
private $api = array(); // Holds an array of APIs.
|
||||
private $rides = null;
|
||||
|
||||
private function __construct() {
|
||||
@@ -38,10 +38,6 @@ class WPStrava {
|
||||
|
||||
public function __get( $name ) {
|
||||
// On-demand classes.
|
||||
if ( $name == 'api' ) {
|
||||
return $this->get_api();
|
||||
}
|
||||
|
||||
if ( $name == 'rides' ) {
|
||||
return $this->get_rides();
|
||||
}
|
||||
@@ -53,13 +49,13 @@ class WPStrava {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function get_api() {
|
||||
if ( ! $this->api ) {
|
||||
public function get_api( $id = '0' ) {
|
||||
if ( ! $this->api[$id] ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
|
||||
$this->api = new WPStrava_API( get_option( 'strava_token' ) );
|
||||
$this->api[$id] = new WPStrava_API( $this->settings->get_setting( 'strava_token', $id ) );
|
||||
}
|
||||
|
||||
return $this->api;
|
||||
return $this->api[$id];
|
||||
}
|
||||
|
||||
public function get_rides() {
|
||||
@@ -75,4 +71,4 @@ class WPStrava {
|
||||
// Register a personalized stylesheet
|
||||
wp_register_style( 'wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user