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:
Steps: