2013-02-03 20:38:55 -06:00
|
|
|
<?php
|
|
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
/**
|
|
|
|
|
* v3 - http://strava.github.io/api/v3/oauth/
|
|
|
|
|
*
|
|
|
|
|
* Set up an "API Application" at Strava
|
|
|
|
|
* Save the Client ID and Client Secret in WordPress - redirect to strava oauth/authorize URL for permission
|
|
|
|
|
* Get redirected back to this settings page with ?code=
|
|
|
|
|
* Use code to retrieve auth token
|
|
|
|
|
*/
|
|
|
|
|
|
2013-02-03 20:38:55 -06:00
|
|
|
class WPStrava_Settings {
|
|
|
|
|
|
2013-03-31 21:28:49 -05:00
|
|
|
private $feedback;
|
2014-09-10 23:35:08 -05:00
|
|
|
private $token;
|
|
|
|
|
private $page_name = 'wp-strava-options';
|
|
|
|
|
private $option_page = 'wp-strava-settings-group';
|
2013-03-31 21:28:49 -05:00
|
|
|
|
2013-02-03 20:38:55 -06:00
|
|
|
//register admin menus
|
|
|
|
|
public function hook() {
|
2013-02-17 21:19:08 -06:00
|
|
|
add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
|
|
|
|
|
add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
|
2014-09-10 23:35:08 -05:00
|
|
|
add_action( 'current_screen', array( $this, 'current_screen' ) );
|
|
|
|
|
add_action( 'option_home', array( $this, 'option_home' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function current_screen( $screen ) {
|
|
|
|
|
if ( $screen->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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 21:19:08 -06:00
|
|
|
public function add_strava_menu() {
|
2013-02-03 20:38:55 -06:00
|
|
|
add_options_page( __( 'Strava Settings', 'wp-strava' ),
|
|
|
|
|
__( 'Strava', 'wp-strava' ),
|
|
|
|
|
'manage_options',
|
2014-09-10 23:35:08 -05:00
|
|
|
$this->page_name,
|
2013-02-17 21:19:08 -06:00
|
|
|
array( $this, 'print_strava_options' ) );
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 21:19:08 -06:00
|
|
|
public function register_strava_settings() {
|
2014-09-10 23:35:08 -05:00
|
|
|
$this->token = get_option( 'strava_token' );
|
|
|
|
|
|
2013-02-17 22:14:10 -06:00
|
|
|
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed
|
2013-02-17 21:19:08 -06:00
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
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' ) );
|
2013-02-17 22:14:10 -06:00
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
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' ) );
|
2013-02-17 22:14:10 -06:00
|
|
|
|
|
|
|
|
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' );
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 22:14:10 -06:00
|
|
|
public function print_api_instructions() {
|
2014-09-10 23:35:08 -05:00
|
|
|
?><p>Steps:</p>
|
|
|
|
|
<ol>
|
|
|
|
|
<li>Create your app here: http://www.strava.com/developers</li>
|
|
|
|
|
<p>Use the following information:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>Application Name: [SiteName] Strava</li>
|
|
|
|
|
<li>Website: [site_url]
|
|
|
|
|
<li>Application Description: WP-Strava for [SiteName]
|
|
|
|
|
<li>Authorization Callback Domain: [site_url] + oauth path
|
|
|
|
|
</ul>
|
|
|
|
|
<li>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</li>
|
|
|
|
|
<li>You'll be redirected to strava to authorize your app after saving your Client ID and Secret. If successful, your Strava Token will display</li>
|
|
|
|
|
<li>Erase your Strava Token if you need to re-authorize</li>
|
|
|
|
|
</ol>
|
|
|
|
|
|
|
|
|
|
<?php
|
|
|
|
|
//'
|
2013-02-17 22:14:10 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 21:19:08 -06:00
|
|
|
public function print_strava_options() {
|
2013-02-03 20:38:55 -06:00
|
|
|
?>
|
|
|
|
|
<div class="wrap">
|
|
|
|
|
<div id="icon-options-general" class="icon32"><br/></div>
|
2013-02-17 22:14:10 -06:00
|
|
|
<h2><?php _e( 'Strava Settings', 'wp-strava' ); ?></h2>
|
|
|
|
|
|
2013-02-03 20:38:55 -06:00
|
|
|
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
|
2014-09-10 23:35:08 -05:00
|
|
|
<?php settings_fields( $this->option_page ); ?>
|
2013-02-03 20:38:55 -06:00
|
|
|
<?php do_settings_sections( 'wp-strava' ); ?>
|
|
|
|
|
|
|
|
|
|
<p class="submit">
|
|
|
|
|
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
|
|
|
|
|
</p>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
<?php
|
|
|
|
|
}
|
2014-09-10 23:35:08 -05:00
|
|
|
|
|
|
|
|
public function print_client_input() {
|
|
|
|
|
?><input type="text" id="strava_client_id" name="strava_client_id" value="<?php echo get_option('strava_client_id'); ?>" /><?php
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
public function print_secret_input() {
|
|
|
|
|
?><input type="text" id="strava_client_secret" name="strava_client_secret" value="<?php echo get_option('strava_client_secret'); ?>" /><?php
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 21:19:08 -06:00
|
|
|
public function print_token_input() {
|
2013-02-03 20:38:55 -06:00
|
|
|
?><input type="text" id="strava_token" name="strava_token" value="<?php echo get_option('strava_token'); ?>" /><?php
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
public function sanitize_client_id( $client_id ) {
|
|
|
|
|
if ( ! is_numeric( $client_id ) ) {
|
|
|
|
|
add_settings_error( 'strava_client_id', 'strava_client_id', __( 'Client ID must be a number.', 'wp-strava' ) );
|
|
|
|
|
}
|
|
|
|
|
return $client_id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sanitize_client_secret( $client_secret ) {
|
|
|
|
|
if ( trim( $client_secret ) == '' ) {
|
|
|
|
|
add_settings_error( 'strava_client_secret', 'strava_client_secret', __( 'Client Secret is required.', 'wp-strava' ) );
|
|
|
|
|
}
|
|
|
|
|
echo "WHEREAMI";
|
|
|
|
|
return $client_secret;
|
2013-02-03 20:38:55 -06:00
|
|
|
}
|
|
|
|
|
|
2013-02-17 21:19:08 -06:00
|
|
|
public function sanitize_token( $token ) {
|
2014-09-10 23:35:08 -05:00
|
|
|
/*
|
|
|
|
|
if ( isset( $_GET['code'] ) ) {
|
|
|
|
|
$token = $this->get_token( $_GET['code'] );
|
2013-02-03 20:38:55 -06:00
|
|
|
if ( $token ) {
|
2013-03-31 21:28:49 -05:00
|
|
|
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' );
|
2013-02-03 20:38:55 -06:00
|
|
|
return $token;
|
|
|
|
|
} else {
|
2013-03-31 21:28:49 -05:00
|
|
|
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
|
2013-02-03 20:38:55 -06:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-09-10 23:35:08 -05:00
|
|
|
*/
|
2013-02-03 20:38:55 -06:00
|
|
|
return $token;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-10 23:35:08 -05:00
|
|
|
private function get_token( $code ) {
|
|
|
|
|
$client_id = get_option( 'strava_client_id' );
|
|
|
|
|
$client_secret = get_option( 'strava_client_secret' );
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2013-03-31 21:28:49 -05:00
|
|
|
} else {
|
2014-09-10 23:35:08 -05:00
|
|
|
$this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' );
|
2013-03-31 21:28:49 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-09-10 23:35:08 -05:00
|
|
|
$this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' );
|
2013-03-31 21:28:49 -05:00
|
|
|
return false;
|
2014-09-10 23:35:08 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-31 21:28:49 -05:00
|
|
|
|
2013-02-17 22:14:10 -06:00
|
|
|
public function print_options_label() {
|
|
|
|
|
?><p>Options</p><?php
|
|
|
|
|
}
|
2013-02-03 20:38:55 -06:00
|
|
|
|
2013-02-17 22:14:10 -06:00
|
|
|
public function print_som_input() {
|
|
|
|
|
$strava_som = get_option( 'strava_som' );
|
|
|
|
|
?>
|
2014-09-10 23:35:08 -05:00
|
|
|
<select id="strava_som" name="strava_som">
|
|
|
|
|
<option value="metric" <?php selected( $strava_som, 'metric' ); ?>><?php _e( 'Metric', 'wp-strava' )?></option>
|
|
|
|
|
<option value="english" <?php selected( $strava_som, 'english' ); ?>><?php _e( 'English', 'wp-strava' )?></option>
|
|
|
|
|
</select>
|
2013-02-17 22:14:10 -06:00
|
|
|
<?php
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sanitize_som( $som ) {
|
|
|
|
|
return $som;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __get( $name ) {
|
|
|
|
|
return get_option( "strava_{$name}" );
|
|
|
|
|
}
|
2014-09-10 23:35:08 -05:00
|
|
|
|
2013-02-17 22:14:10 -06:00
|
|
|
}
|