Moved from token being the identifier to Client ID

This commit is contained in:
Justin Foell
2019-04-01 16:21:13 -05:00
parent 2265e73f83
commit 15de60d764
4 changed files with 93 additions and 77 deletions
+4 -3
View File
@@ -35,7 +35,7 @@ abstract class WPStrava_Auth {
$settings = WPStrava::get_instance()->settings;
// User is clearing to start-over, don't oauth, ignore other errors.
if ( isset( $_POST['strava_token'] ) && $settings->tokens_empty( $_POST['strava_token'] ) ) {
if ( isset( $_POST['strava_id'] ) && $settings->ids_empty( $_POST['strava_id'] ) ) {
return array();
}
@@ -60,8 +60,6 @@ abstract class WPStrava_Auth {
if ( isset( $info->access_token ) ) {
// Translators: New strava token
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ), 'updated' );
$settings->add_token( $info->access_token );
$settings->update_token();
} else {
// throw new WPStrava_Exception( '' );
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
@@ -97,6 +95,9 @@ abstract class WPStrava_Auth {
$strava_info = $this->token_request( $data );
if ( isset( $strava_info->access_token ) ) {
$settings->add_id( $client_id );
$settings->save_info( $client_id, $strava_info );
$this->feedback .= __( 'Successfully authenticated.', 'wp-strava' );
return $strava_info;
}
+74 -74
View File
@@ -11,7 +11,7 @@
class WPStrava_Settings {
private $tokens = array();
private $ids = array();
private $page_name = 'wp-strava-options';
private $option_page = 'wp-strava-settings-group';
private $adding_athlete = true;
@@ -37,9 +37,9 @@ 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->tokens = $this->get_tokens();
$this->ids = $this->get_ids();
if ( $this->tokens_empty( $this->tokens ) ) {
if ( $this->ids_empty( $this->ids ) ) {
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' ) );
@@ -48,8 +48,8 @@ class WPStrava_Settings {
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' );
register_setting( $this->option_page, 'strava_id', array( $this, 'sanitize_id' ) );
add_settings_field( 'strava_id', __( 'Saved ID', 'wp-strava' ), array( $this, 'print_id_input' ), 'wp-strava', 'strava_api' );
// Add additional fields
register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) );
@@ -105,8 +105,8 @@ class WPStrava_Settings {
<li>Authorization Callback Domain: <strong>%7\$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>
<li>If you need to re-authorize your API Application, erase your Strava Token here and click 'Save Changes' to start over.</li>
<li>After saving your Client ID and Secret, you'll be redirected to strava to authorize your API Application. If successful, your Strava ID will display in a table, next to your nickname.</li>
<li>If you need to re-authorize your API Application, erase your Strava ID next to your nickname and click 'Save Changes' to start over.</li>
</ol>", 'wp-strava' ),
$settings_url,
$settings_url,
@@ -128,21 +128,7 @@ class WPStrava_Settings {
}
public function print_strava_options() {
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br/></div>
<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 esc_html_e( 'Save Changes', 'wp-strava' ); ?>" />
</p>
</form>
</div>
<?php
include WPSTRAVA_PLUGIN_DIR . 'templates/admin-settings.php';
}
public function print_client_input() {
@@ -158,17 +144,17 @@ class WPStrava_Settings {
}
public function print_nickname_input() {
$nickname = $this->tokens_empty( $this->tokens ) ? __( 'Default', 'wp-strava' ) : '';
$nickname = $this->ids_empty( $this->ids ) ? __( 'Default', 'wp-strava' ) : '';
?>
<input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" />
<?php
}
public function print_token_input() {
foreach ( $this->get_all_tokens() as $token => $nickname ) {
public function print_id_input() {
foreach ( $this->get_all_ids() as $id => $nickname ) {
?>
<input type="text" name="strava_token[]" value="<?php echo $token; ?>" />
<input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" />
<input type="text" name="strava_id[]" value="<?php echo esc_attr( $id ); ?>" />
<input type="text" name="strava_nickname[]" value="<?php echo esc_attr( $nickname ); ?>" />
<br/>
<?php
}
@@ -201,13 +187,13 @@ class WPStrava_Settings {
public function sanitize_nickname( $nicknames ) {
if ( ! $this->adding_athlete ) {
// Chop $nicknames to same size as tokens.
$nicknames = array_slice( $nicknames, 0, count( $_POST['strava_token'] ) );
// Chop $nicknames to same size as ids.
$nicknames = array_slice( $nicknames, 0, count( $_POST['strava_id'] ) );
// Remove indexes from $nicknames that have empty tokens.
foreach ( $_POST['strava_token'] as $index => $token ) {
$token = trim( $token );
if ( empty( $token ) ) {
// Remove indexes from $nicknames that have empty ids.
foreach ( $_POST['strava_id'] as $index => $id ) {
$id = trim( $id );
if ( empty( $id ) ) {
unset( $nicknames[ $index ] );
}
}
@@ -225,8 +211,8 @@ class WPStrava_Settings {
return $nicknames;
}
public function sanitize_token( $token ) {
return $token;
public function sanitize_id( $id ) {
return $id;
}
public function print_gmaps_key_input() {
@@ -292,61 +278,63 @@ class WPStrava_Settings {
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE '_transient_timeout_strava_latest_map_%'" );
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE '_transient_strava_latest_map_%'" );
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE 'strava_latest_map%'" );
delete_option( 'strava_token' );
}
return null;
}
/**
* Gets all saved strava tokens as an array.
* Gets all saved strava ids as an array.
*
* @return array
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @since 2.0.0
*/
public function get_tokens() {
$tokens = get_option( 'strava_token' );
if ( ! is_array( $tokens ) ) {
$tokens = array( $tokens );
public function get_ids() {
$ids = get_option( 'strava_id' );
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}
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
foreach ( $ids as $index => $id ) {
if ( empty( $id ) ) {
unset( $ids[ $index ] );
$ids = array_values( $ids ); // Rebase array keys after unset @see https://stackoverflow.com/a/5943165/2146022
}
}
return $tokens;
return $ids;
}
/**
* Returns first (default) token saved.
* Returns first (default) ID saved.
*
* @return string|null
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*/
public function get_default_token() {
$tokens = $this->get_tokens();
return isset( $tokens[0] ) ? $tokens[0] : null;
public function get_default_id() {
$ids = $this->get_ids();
return isset( $ids[0] ) ? $ids[0] : null;
}
/**
* Get all tokens and their nicknames in one array.
* Get all IDs and their nicknames in one array.
*
* @return void
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @since 2.0.0
*/
public function get_all_tokens() {
$tokens = $this->get_tokens();
public function get_all_ids() {
$ids = $this->get_ids();
$nicknames = $this->nickname;
$all = array();
$number = 1;
foreach ( $tokens as $index => $token ) {
foreach ( $ids as $index => $id ) {
if ( ! empty( $nicknames[ $index ] ) ) {
$all[ $token ] = $nicknames[ $index ];
$all[ $id ] = $nicknames[ $index ];
} else {
$all[ $token ] = $this->get_default_nickname( $number );
$all[ $id ] = $this->get_default_nickname( $number );
}
$number++;
}
@@ -368,22 +356,22 @@ class WPStrava_Settings {
}
/**
* Checks for valid tokens.
* Checks for valid IDs.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*
* @param string|array Single token or array of tokens.
* @param string|array Single ID or array of IDs.
* @return boolean True if empty.
*/
public function tokens_empty( $tokens ) {
if ( empty( $tokens ) ) {
public function ids_empty( $ids ) {
if ( empty( $ids ) ) {
return true;
}
if ( is_array( $tokens ) ) {
foreach ( $tokens as $token ) {
if ( ! empty( $token ) ) {
if ( is_array( $ids ) ) {
foreach ( $ids as $id ) {
if ( ! empty( $id ) ) {
return false;
}
}
@@ -393,28 +381,33 @@ class WPStrava_Settings {
}
/**
* Only add a token if it's not already there.
* Add an ID if it's not already there, and save to the DB.
*
* @param string $token
* @param string $id
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @since 2.0.0
*/
public function add_token( $token ) {
if ( false === array_search( $token, $this->tokens, true ) ) {
$this->tokens[] = $token;
public function add_id( $id ) {
if ( false === array_search( $id, $this->ids, true ) ) {
$this->ids[] = $id;
update_option( 'strava_id', $this->ids );
}
}
/**
* Undocumented function
*
* @param array $token
* @author Justin Foell <justin@foell.org>
* @param [type] $id
* @param [type] $info
* @return void
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function update_token() {
update_option( 'strava_token', $this->tokens );
public function save_info( $id, $info ) {
$infos = get_option( 'strava_info', array() );
$infos[ $id ] = $info;
update_option( 'strava_info', $infos );
}
/**
@@ -463,6 +456,13 @@ class WPStrava_Settings {
return isset( $_GET['page'] ) && $_GET['page'] === $this->page_name;
}
/**
* Undocumented function
*
* @return string
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function get_page_name() {
return $this->page_name;
}
+2
View File
@@ -0,0 +1,2 @@
<?php
+13
View File
@@ -0,0 +1,13 @@
<div class="wrap">
<div id="icon-options-general" class="icon32"><br/></div>
<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 esc_html_e( 'Save Changes', 'wp-strava' ); ?>" />
</p>
</form>
</div>