From 15de60d76460579b5762dd7e47fa2051ed8136ce Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Mon, 1 Apr 2019 16:21:13 -0500 Subject: [PATCH] Moved from token being the identifier to Client ID --- includes/WPStrava/Auth.php | 7 +- includes/WPStrava/Settings.php | 148 ++++++++++++++++----------------- includes/template-tags.php | 2 + templates/admin-settings.php | 13 +++ 4 files changed, 93 insertions(+), 77 deletions(-) create mode 100644 includes/template-tags.php create mode 100644 templates/admin-settings.php diff --git a/includes/WPStrava/Auth.php b/includes/WPStrava/Auth.php index 208e1e1..0cd3e49 100644 --- a/includes/WPStrava/Auth.php +++ b/includes/WPStrava/Auth.php @@ -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; } diff --git a/includes/WPStrava/Settings.php b/includes/WPStrava/Settings.php index 9ea8605..56a46fa 100644 --- a/includes/WPStrava/Settings.php +++ b/includes/WPStrava/Settings.php @@ -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 {
  • Authorization Callback Domain: %7\$s
  • Once you've created your API Application at strava.com, enter the Client ID and Client Secret below, which can now be found on that same strava API Settings page. -
  • 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.
  • -
  • If you need to re-authorize your API Application, erase your Strava Token here and click 'Save Changes' to start over.
  • +
  • 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.
  • +
  • If you need to re-authorize your API Application, erase your Strava ID next to your nickname and click 'Save Changes' to start over.
  • ", 'wp-strava' ), $settings_url, $settings_url, @@ -128,21 +128,7 @@ class WPStrava_Settings { } public function print_strava_options() { - ?> -
    -

    -

    - -
    - option_page ); ?> - - -

    - -

    -
    -
    - tokens_empty( $this->tokens ) ? __( 'Default', 'wp-strava' ) : ''; + $nickname = $this->ids_empty( $this->ids ) ? __( 'Default', 'wp-strava' ) : ''; ?> get_all_tokens() as $token => $nickname ) { + public function print_id_input() { + foreach ( $this->get_all_ids() as $id => $nickname ) { ?> - - + +
    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 - * @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 * @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 - * @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 * @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 - * @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 + * @param [type] $id + * @param [type] $info + * @return void + * @author Justin Foell * @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 + * @since 2.0.0 + */ public function get_page_name() { return $this->page_name; } diff --git a/includes/template-tags.php b/includes/template-tags.php new file mode 100644 index 0000000..a4abe2d --- /dev/null +++ b/includes/template-tags.php @@ -0,0 +1,2 @@ + +

    +

    + +
    + option_page ); ?> + + +

    + +

    +
    +