diff --git a/lib/API.class.php b/lib/API.class.php index ffea828..3506b9c 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -4,29 +4,26 @@ */ class WPStrava_API { - const STRAVA_V1_API = 'http://www.strava.com/api/v1/'; //rides?athleteId=134698 - const STRAVA_V2_API = 'http://www.strava.com/api/v2/'; //rides/:ride_id/map_details + //deactivated + //const STRAVA_V1_API = 'http://www.strava.com/api/v1/'; //rides?athleteId=134698 + //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/'; - /* - private $rideUrl = "http://www.strava.com/api/v1/rides/:id"; - private $rideUrlV2 = "http://www.strava.com/api/v2/rides/:id"; - private $ridesUrl = "http://www.strava.com/api/v1/rides"; - private $authenticationUrl = "https://www.strava.com/api/v1/authentication/login"; - private $authenticationUrlV2 = "https://www.strava.com/api/v2/authentication/login"; - private $rideMapDetailsUrl = "http://www.strava.com/api/v1/rides/:id/map_details"; - private $rideMapDetailsUrlV2 = "http://www.strava.com/api/v2/rides/:id/map_details"; - */ + public function __construct( $access_token ) { + $this->access_token = $access_token; + } - public function post( $uri, $data = NULL, $version = 2 ) { - $url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API; + public function post( $uri, $data = NULL ) { + $url = self::STRAVA_V3_API; $args = array( 'body' => http_build_query( $data ), + 'sslverify' => false, + 'headers' => array( + 'Authorization' => 'Bearer: ' . $this->access_token, + ) ); - if ( $version == 2 ) - $args['sslverify'] = false; - $response = wp_remote_post( $url . $uri, $args ); if ( $response['response']['code'] != 200 ) { @@ -46,15 +43,21 @@ class WPStrava_API { return json_decode( $response['body'] ); } - public function get( $uri, $args = NULL, $version = 2 ) { - $url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API; + public function get( $uri, $args = NULL ) { + $url = self::STRAVA_V3_API; $url .= $uri; if ( ! empty( $args ) ) $url = add_query_arg( $args, $url ); - $response = wp_remote_get( $url ); + $get_args = array( + 'headers' => array( + 'Authorization' => 'Bearer: ' . $this->access_token, + ) + ); + + $response = wp_remote_get( $url, $get_args ); if ( is_wp_error( $response ) ) return $response; diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 9eee604..09d37fe 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -1,35 +1,90 @@ id = 'settings_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 ); + } else { + add_settings_error( 'strava_token', 'strava_token', $this->feedback ); + } + } + } + } + + /** + * This runs after options are saved + */ + public function option_home() { + if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { + //redirect only if all the right options are in place + $errors = get_settings_errors(); + if ( ! empty( $errors ) ) + return; + + $client_id = get_option( 'strava_client_id' ); + $client_secret = get_option( '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(); + } + } } public function add_strava_menu() { add_options_page( __( 'Strava Settings', 'wp-strava' ), __( 'Strava', 'wp-strava' ), 'manage_options', - 'wp-strava-options', + $this->page_name, array( $this, 'print_strava_options' ) ); } public function register_strava_settings() { - register_setting('wp-strava-settings-group','strava_email', array( $this, 'sanitize_email' ) ); - register_setting('wp-strava-settings-group','strava_password', NULL ); - register_setting('wp-strava-settings-group','strava_token', array( $this, 'sanitize_token' ) ); - + $this->token = get_option( 'strava_token' ); + add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed - add_settings_field( 'strava_email', __( 'Strava Email', 'wp-strava' ), array( $this, 'print_email_input' ), 'wp-strava', 'strava_api' ); - add_settings_field( 'strava_password', __( 'Strava Password', 'wp-strava' ), array( $this, 'print_password_input' ), 'wp-strava', 'strava_api' ); - add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' ); + if ( ! $this->token ) { + 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('wp-strava-settings-group','strava_som', array( $this, 'sanitize_som' ) ); + 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' ); + } 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_som', array( $this, 'sanitize_som' ) ); add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), NULL, 'wp-strava' ); @@ -37,7 +92,23 @@ class WPStrava_Settings { } public function print_api_instructions() { - ?>

Steps:

+
    +
  1. Create your app here: http://www.strava.com/developers
  2. +

    Use the following information:

    + +
  3. Once you've created your application, enter the Client ID and Client Secret below, which can be found at https://www.strava.com/settings/api
  4. +
  5. You'll be redirected to strava to authorize your app after saving your Client ID and Secret. If successful, your Strava Token will display
  6. +
  7. Erase your Strava Token if you need to re-authorize
  8. +
+ +
- + option_page ); ?>

@@ -57,32 +128,38 @@ class WPStrava_Settings { - -

- get_authentication_token( $this->email, $_POST['strava_password'] ); + /* + 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' ); return $token; @@ -91,28 +168,35 @@ class WPStrava_Settings { return NULL; } } - + */ return $token; } - private function get_authentication_token( $email, $password ) { - $data = array( 'email' => $email, 'password' => $password ); - $strava_login = WPStrava::get_instance()->api->post( 'authentication/login', $data ); + private function get_token( $code ) { + $client_id = get_option( 'strava_client_id' ); + $client_secret = get_option( 'strava_client_secret' ); - if( $strava_login ) { - if( ! isset( $strava_login->error ) ) { - $this->feedback .= __( 'Successfully authenticated.', 'wp-strava' ); - return $strava_login->token; + if ( $client_id && $client_secret ) { + $data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'code' => $code ); + $strava_info = WPStrava::get_instance()->api->post( 'oauth/token', $data ); + + if( $strava_info ) { + if( isset( $strava_info->access_token ) ) { + $this->feedback .= __( 'Successfully authenticated.', 'wp-strava' ); + return $strava_info->access_token; + } else { + $this->feedback .= __( 'Authentication failed, please check your credentials.', 'wp-strava' ); + return false; + } } else { - $this->feedback .= __( 'Authentication failed, please check your credentials.', 'wp-strava' ); + $this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' ); return false; } } else { - $this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' ); + $this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' ); return false; - } - } // get_authentication_token - + } + } public function print_options_label() { ?>

Options

- + {$name} ) ) - return $this->{$name}; - //on-demand classes if ( $name == 'api' ) return $this->get_api(); @@ -42,13 +41,16 @@ class WPStrava { if ( $name == 'rides' ) return $this->get_rides(); + if ( isset( $this->{$name} ) ) + return $this->{$name}; + return NULL; } public function get_api() { if ( ! $this->api ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php'; - $this->api = new WPStrava_API(); + $this->api = new WPStrava_API( get_option('strava_token') ); } return $this->api; diff --git a/wp-strava.php b/wp-strava.php index 045ab76..fbce144 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -35,6 +35,7 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); +//wp_die( '
' . var_export( $wpstrava->api->get( 'athlete' ), true ) );
 
 //@TODO only load these when needed using is_active_widget()
 function load_styles() {