diff --git a/lib/API.class.php b/lib/API.class.php
index 3506b9c..a547a1f 100755
--- a/lib/API.class.php
+++ b/lib/API.class.php
@@ -20,7 +20,7 @@ class WPStrava_API {
'body' => http_build_query( $data ),
'sslverify' => false,
'headers' => array(
- 'Authorization' => 'Bearer: ' . $this->access_token,
+ 'Authorization' => 'Bearer ' . $this->access_token,
)
);
@@ -53,7 +53,7 @@ class WPStrava_API {
$get_args = array(
'headers' => array(
- 'Authorization' => 'Bearer: ' . $this->access_token,
+ 'Authorization' => 'Bearer ' . $this->access_token,
)
);
diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php
index e84a4bb..30d4369 100644
--- a/lib/LatestRidesWidget.class.php
+++ b/lib/LatestRidesWidget.class.php
@@ -6,73 +6,58 @@
class WPStrava_LatestRidesWidget extends WP_Widget {
public function __construct() {
- $widget_ops = array('classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.') );
- parent::__construct('wp-strava', $name = 'Strava Latest Rides', $widget_ops);
- wp_enqueue_style('wp-strava'); //TODO only load this when wigit is loaded
+ $widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.' ) );
+ parent::__construct( 'wp-strava', $name = 'Strava Latest Rides', $widget_ops );
+ wp_enqueue_style( 'wp-strava' ); //TODO only load this when wigit is loaded
}
/** @see WP_Widget::widget */
public function widget( $args, $instance ) {
- extract($args);
+ extract( $args );
//$widget_id = $args['widget_id'];
- $title = apply_filters('widget_title', empty($instance['title']) ? _e('Rides', 'wp-strava') : $instance['title']);
- $strava_search_option = empty($instance['strava_search_option']) ? 'athlete' : $instance['strava_search_option'];
- $strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id'];
- $quantity = empty($instance['quantity']) ? '5' : $instance['quantity'];
+ $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Rides', 'wp-strava' ) : $instance['title'] );
+ $strava_club_id = empty( $instance['strava_club_id'] ) ? '' : $instance['strava_club_id'];
+ $quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity'];
$this->som = WPStrava_SOM::get_som();
?>
- strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?>
+ strava_request_handler( $strava_club_id, $strava_som_option, $quantity ); ?>
-
-
-
-
-
-
-
- >
- >
-
+
+
-
-
+
+
@@ -83,41 +68,29 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
// The handler to the ajax call, we will avoid this if Strava support jsonp request and we can do it
// the parsing directly on the jQuery ajax call, the returned text will be enclosed in the $response variable.
- private function strava_request_handler( $strava_search_option, $strava_search_id, $strava_som_option, $quantity ) {
+ private function strava_request_handler( $strava_club_id, $strava_som_option, $quantity ) {
- //Check if the username is empty.
- if ( empty( $strava_search_id ) )
- return __("Please configure the Strava search id on the widget options.", "wp-strava");
- //else
$strava_rides = WPStrava::get_instance()->rides;
- $rides = $strava_rides->getRidesSimple( $strava_search_option, $strava_search_id );
+ $rides = $strava_rides->getRides( $quantity, $strava_club_id );
if ( is_wp_error( $rides ) )
return $rides->get_error_message();
-
- //adjust quantity
- $rides = array_slice( $rides, 0, $quantity );
- $rides_details = $strava_rides->getRidesDetails( $rides );
- if ( is_wp_error( $rides_details ) )
- return $rides_details->get_error_message();
-
$response = "
";
- foreach($rides_details as $ride_obj) {
- $ride = $ride_obj->ride;
+ foreach( $rides as $ride ) {
$response .= "";
$response .= "" . $ride->name . " ";
$response .= "";
$unixtime = strtotime( $ride->start_date_local );
$response .= sprintf( __("On %s %s", "wp-strava"), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );
- if ($strava_search_option == "club") {
+ if ( is_numeric( $strava_club_id ) ) {
$response .= "
" . $ride->athlete_name . " ";
}
$response .= sprintf( __(" rode %s %s", "wp-strava"), $this->som->distance( $ride->distance ), $this->som->get_distance_label() );
$response .= sprintf( __( " during %s %s", "wp-strava" ), $this->som->time( $ride->elapsed_time ), $this->som->get_time_label() );
- $response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->elevation_gain ), $this->som->get_elevation_label() );
+ $response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->total_elevation_gain ), $this->som->get_elevation_label() );
$response .= "
";
$response .= " ";
}
diff --git a/lib/Rides.class.php b/lib/Rides.class.php
index d3e145b..174dd44 100755
--- a/lib/Rides.class.php
+++ b/lib/Rides.class.php
@@ -8,7 +8,7 @@ class WPStrava_Rides {
const ATHLETES_URL = "http://app.strava.com/athletes/";
public function getRideDetails( $rideId ) {
- return WPStrava::get_instance()->api->get( "rides/{$rideId}" );
+ return WPStrava::get_instance()->api->get( "activities/{$rideId}" );
} // getRideDetails
public function getRidesDetails( $rides ) {
@@ -24,32 +24,26 @@ class WPStrava_Rides {
return $rides_details;
} // getRidesDetails
- public function getRidesSimple( $searchOption, $searchId ) {
+ public function getRides( $quantity, $club_id = NULL ) {
$api = WPStrava::get_instance()->api;
$data = NULL;
//Get the json results using the constructor specified values.
- if ( $searchOption == 'athlete' ) {
- if ( is_numeric( $searchId ) ) {
- $data = $api->get( 'rides', array( 'athleteId' => $searchId ), 1 );
- } else {
- $data = $api->get( 'rides', array( 'athleteName' => $searchId ), 1 );
- }
- } elseif ($searchOption == 'club' && is_numeric($searchId)) {
- $data = $api->get( 'rides', array( 'clubId' => $searchId ), 1 );
+ if ( is_numeric( $club_id ) ) {
+ $data = $api->get( "clubs/{$club_id}/activities", array( 'per_page' => $quantity ) );
} else {
- return new WP_Error( 'wp-strava_options', __("There's an error in your simple options.", 'wp-strava') );
+ $data = $api->get( 'athlete/activities', array( 'per_page' => $quantity ) );
}
if ( is_wp_error( $data ) )
return $data;
- if ( isset( $data->rides ) )
- return $data->rides;
+ if ( is_array( $data ) )
+ return $data;
return array();
- } // getRidesSimple
+ } // getRides
public function getRidesAdvanced( $params ) {
$data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1
diff --git a/lib/Settings.class.php b/lib/Settings.class.php
index 393a908..6cee30b 100644
--- a/lib/Settings.class.php
+++ b/lib/Settings.class.php
@@ -20,24 +20,9 @@ class WPStrava_Settings {
public function hook() {
add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
- 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
*/
@@ -47,9 +32,13 @@ class WPStrava_Settings {
$errors = get_settings_errors();
if ( ! empty( $errors ) )
return;
+
+ //clearing to start-over
+ if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
+ return;
$client_id = get_option( 'strava_client_id' );
- $client_secret = get_option( 'strava_client_secret' );
+ $client_secret = get_option( 'strava_client_secret' );
if ( $client_id && $client_secret ) {
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
@@ -68,8 +57,22 @@ class WPStrava_Settings {
array( $this, 'print_strava_options' ) );
}
+ public function init() {
+ if ( isset( $_GET['page'] ) && $_GET['page'] == $this->page_name && 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->token = get_option( 'strava_token' );
+ }
+
public function register_strava_settings() {
- $this->token = get_option( 'strava_token' );
+ $this->init();
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed