From 8fc5ea34077cc1edf27b6d7a6441fa5f0539abf3 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 21 Jul 2017 16:12:15 -0500 Subject: [PATCH] Working multi tokens with nicknames --- .gitignore | 1 + lib/API.class.php | 17 ++-- lib/Settings.class.php | 200 ++++++++++++++++++++++++++++++++--------- lib/Strava.class.php | 16 ++-- 4 files changed, 173 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index e0c38e3..2e8a44a 100755 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *~ .svn +.vscode diff --git a/lib/API.class.php b/lib/API.class.php index 7df3aa0..79c9e77 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -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 ); diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 4161fa8..4db719c 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -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( __( "

Steps:

    -
  1. Create your free API Application/Connection here: %s using the following information:
  2. +
  3. Create your free API Application/Connection here: %2\$s using the following information:
  4. 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.
  5. 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.
  6. @@ -145,7 +157,7 @@ class WPStrava_Settings { $maps_url = 'https://developers.google.com/maps/documentation/static-maps/'; printf( __( "

    Steps:

      -
    1. To use Google map images, you must create a Static Maps API Key. Create a free key by going here: %s and clicking Get a Key
    2. +
    3. To use Google map images, you must create a Static Maps API Key. Create a free key by going here: %2\$s and clicking Get a Key
    4. Once you've created your Google Static Maps API Key, enter the key below.
    ", 'wp-strava' ), $maps_url, $maps_url ); @@ -155,14 +167,14 @@ class WPStrava_Settings { ?>

    -

    +

    option_page ); ?>

    - +

    @@ -170,15 +182,29 @@ class WPStrava_Settings { } public function print_client_input() { - ?>tokens_empty( $this->tokens ) ? __( 'default', 'wp-strava' ) : ''; + ?>nickname; + foreach ( $this->get_tokens() as $index => $token ) { + ?> + + + + +
    + 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() { - ?> $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' ) ); + } + } } diff --git a/lib/Strava.class.php b/lib/Strava.class.php index cb8f9f1..bf1a3e6 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -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' ); } -} \ No newline at end of file +}