\n"; } /** * This runs after options are saved */ public function maybe_oauth( $value ) { //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( $_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; $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(); } } } return $value; } public function add_strava_menu() { add_options_page( __( 'Strava Settings', 'wp-strava' ), __( 'Strava', 'wp-strava' ), 'manage_options', $this->page_name, array( $this, 'print_strava_options' ) ); } public function init() { //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 ); } else { add_settings_error( 'strava_token', 'strava_token', $this->feedback ); } } else if ( isset( $_GET['error'] ) ) { 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() { $this->init(); add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed 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' ) ); 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' ); add_settings_field( 'strava_som', __( 'System of Measurement', 'wp-strava' ), array( $this, 'print_som_input' ), 'wp-strava', 'strava_options' ); } 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 = $blog_name . ' Strava'; $url_parts = parse_url( site_url() ); $site_url = $url_parts['host']; //strip http/https for copying/pasting into strava $description = 'WP-Strava for ' . $blog_name; printf( __( "

Steps:

  1. Create your API Application here: %s using the following information:
  2. Once you've created your API Application at strava.com, enter the Client ID and Client Secret below, which can be found at %s
  3. 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.
  4. If you need to re-authorize your API Application, erase your Strava Token here and click 'Save Changes' to start over.
", 'wp-strava' ), $signup_url, $signup_url, $app_name, $site_url, $description, $site_url, $settings_url, $settings_url ); } public function print_strava_options() { ?>

option_page ); ?>

$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 .= __( sprintf( 'There was an error receiving data from Strava: %s', print_r( $strava_info, true ) ), 'wp-strava' ); return false; } } else { $this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' ); return false; } } public function print_som_input() { $strava_som = get_option( 'strava_som' ); ?> page_name}" ) . '">' . __( 'Settings' ) . ''; $links[] = $settings_link; return $links; } }