Skip to content

Commit

Permalink
Merge pull request #5 from jrfoell/master
Browse files Browse the repository at this point in the history
V3 API
  • Loading branch information
cmanon committed Dec 10, 2014
2 parents d88de7c + 6f6ed94 commit 09a2679
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 382 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*~
.svn
4 changes: 2 additions & 2 deletions css/wp-strava.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
background-position: -224px -160px;
margin: 0 6px 2px 0;
}
.table {
.wp-strava-ride-container {
display: table;
margin: 0 auto;
}
Expand All @@ -32,6 +32,6 @@
.ride-details-table-units {
font-size: 0.8em;
}
.map img {
.wp-strava-ride-container img {
max-width: none;
}
44 changes: 24 additions & 20 deletions lib/API.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -46,26 +43,33 @@ public function post( $uri, $data = NULL, $version = 2 ) {
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;

if ( $response['response']['code'] != 200 ) {
die($url);
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
$error = $body->error;
else if ( $response['response']['code'] == 503 )
$error = __( 'Strava Temporarily Unavailable', 'wp-strava' );
else
$error = print_r( $response, true );

Expand Down
76 changes: 25 additions & 51 deletions lib/LatestMapWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct() {
public function form( $instance ) {
// outputs the options form on admin
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
$ride_index_params = isset( $instance['ride_index_params'] ) ? esc_attr( $instance['ride_index_params'] ) : '';
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';

//provide some defaults
//$ride_index_params = $ride_index_params ? $ride_index_params : 'athleteId=21';
Expand All @@ -27,35 +27,25 @@ public function form( $instance ) {
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>"><?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'distance_min' ); ?>" name="<?php echo $this->get_field_name( 'distance_min' ); ?>" type="text" value="<?php echo $distance_min; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'ride_index_params' ); ?>"><?php _e( 'Ride Search Parameters (one per line): ' ); ?>
<a href="https://stravasite-main.pbworks.com/w/page/51754146/Strava%20REST%20API%20Method%3A%20rides%20index" target="_blank"><?php _e( 'help' ); ?></a></label>
<textarea name="<?php echo $this->get_field_name( 'ride_index_params' ); ?>" id="<?php echo $this->get_field_id( 'ride_index_params' ); ?>" cols="10" rows="5" class="widefat"><?php echo $ride_index_params; ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('strava_club_id'); ?>"><?php _e('Club ID (leave blank to show Athlete):'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('strava_club_id'); ?>" name="<?php echo $this->get_field_name('strava_club_id'); ?>" type="text" value="<?php echo $strava_club_id; ?>" />
</p>
<?php
}

public function update( $new_instance, $old_instance ) {
// processes widget options to be saved from the admin
$instance = $old_instance;
$instance['ride_index_params'] = strip_tags( $new_instance['ride_index_params'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );

/*
if ( empty( $instance['ride_index_params'] ) ) {
$instance['ride_index_params'] = "athleteId={$auth->athlete->id}";
}
*/

//$instance['athlete_hash'] = strip_tags( $new_instance['athlete_hash'] );

return $instance;
}

public function widget( $args, $instance ) {
extract( $args );
$ride_index_params = $instance['ride_index_params'];
$distance_min = $instance['distance_min'];
$strava_club_id = empty( $instance['strava_club_id'] ) ? NULL : $instance['strava_club_id'];
$build_new = false;

//try our transient first
Expand All @@ -64,12 +54,23 @@ public function widget( $args, $instance ) {

if ( $ride_transient )
$ride = $ride_transient;

if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides;
$ride_index_params = implode( '&', explode( "\n", $ride_index_params ) );
parse_str( $ride_index_params, $params );
$rides = $strava_rides->getRidesAdvanced( $params );
$rides = $strava_rides->getRides( $strava_club_id );

if ( is_wp_error( $rides ) ) {
echo $before_widget;
if ( WPSTRAVA_DEBUG ) {
echo '<pre>';
print_r($rides);
echo '</pre>';
} else {
echo $rides->get_error_message();
}
echo $after_widget;
return;
}

if ( ! empty( $rides ) ) {

Expand All @@ -92,47 +93,20 @@ public function widget( $args, $instance ) {
if ( $ride ):
echo $before_widget;
?><h3 class="widget-title">Latest Ride</h3>
<a title="<?php echo $ride->ride->name ?>" href="http://app.strava.com/activities/<?php echo $ride->id ?>"><?php
<a title="<?php echo $ride->name ?>" href="http://app.strava.com/activities/<?php echo $ride->id ?>"><?php
echo $this->getStaticImage( $ride->id, $build_new );
?></a><?php
echo $after_widget;
endif;
}

private function buildImage( $map_details ) {
$url = 'http://maps.google.com/maps/api/staticmap?maptype=terrain&size=390x260&sensor=false&path=color:0xFF0000BF|weight:2|';
$url_len = strlen( $url );
$point_len = 0;
$num = 50;
$count = count( $map_details->latlng );
$full_url = '';
$max_chars = 1865;

//get the longest usable URL
while ( $url_len + $point_len < $max_chars ) {
$mod = (int) ( $count / $num );
$points = array();
for ( $i = 0; $i < $count; $i += $mod ) {
$point = $map_details->latlng[$i];
$points[] = number_format( $point[0], 4 ) . ',' . number_format( $point[1], 4 );
}

$url_points = join( '|', $points );
$point_len = strlen( $url_points );
if ( $url_len + $point_len < $max_chars )
$full_url = $url . $url_points;
$num++;
}

return "<img src='{$full_url}' />";
}

private function getStaticImage( $ride_id, $build_new ) {
$img = get_option( 'strava_latest_map' );

if ( $build_new || ! $img ) {
$map_details = WPStrava::get_instance()->rides->getMapDetails( $ride_id );
$img = $this->buildImage( $map_details );
$ride = WPStrava::get_instance()->rides->getRide( $ride_id );
$img = WPStrava_StaticMap::get_image_tag( $ride );
update_option( 'strava_latest_map', $img );
}

Expand Down
83 changes: 31 additions & 52 deletions lib/LatestRidesWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,64 @@
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 );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) );
}

public function maybe_enqueue() {
if ( is_active_widget( false, false, $this->id_base ) ) {
wp_enqueue_style( 'wp-strava-style' ); //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();
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php echo $this->strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?>
<?php echo $this->strava_request_handler( $strava_club_id, $strava_som_option, $quantity ); ?>
<?php echo $after_widget; ?>
<?php
}

/** @see WP_Widget::update */
public function update($new_instance, $old_instance) {
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if(in_array($new_instance['strava_search_option'], array('athlete', 'club'))) {
$instance['strava_search_option'] = $new_instance['strava_search_option'];
} else {
$instance['strava_search_option'] = 'athlete';
}
if(in_array($new_instance['strava_som_option'], array('metric', 'english'))) {
$instance['title'] = strip_tags( $new_instance['title'] );
if( in_array( $new_instance['strava_som_option'], array( 'metric', 'english' ) ) ) {
$instance['strava_som_option'] = $new_instance['strava_som_option'];
} else {
$instance['strava_som_option'] = 'metric';
}
$instance['strava_search_id'] = strip_tags($new_instance['strava_search_id']);
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['quantity'] = $new_instance['quantity'];

return $instance;
}

/** @see WP_Widget::form */
public function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : _e('Rides', 'wp-strava');
$strava_search_option = isset($instance['strava_search_option']) ? esc_attr($instance['strava_search_option']) : "athlete";
$strava_search_id = isset($instance['strava_search_id']) ? esc_attr($instance['strava_search_id']) : "";
$quantity = isset($instance['quantity']) ? absint($instance['quantity']) : 5;
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Rides', 'wp-strava' );
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;

?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<!-- TODO: make an 'advanced' option -->
<p>
<label for="<?php echo $this->get_field_id('strava_search_option'); ?>"><?php _e('Search Option:'); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('strava_search_option'); ?>" name="<?php echo $this->get_field_name('strava_search_option'); ?>">
<option value="athlete" <?php selected($strava_search_option, 'athlete'); ?>><?php _e("Athlete", "wp-strava")?></option>
<option value="club" <?php selected($strava_search_option, 'club'); ?>><?php _e("Club", "wp-strava")?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('strava_search_id'); ?>"><?php _e('Search Id:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('strava_search_id'); ?>" name="<?php echo $this->get_field_name('strava_search_id'); ?>" type="text" value="<?php echo $strava_search_id; ?>" />
<label for="<?php echo $this->get_field_id('strava_club_id'); ?>"><?php _e('Club ID (leave blank to show Athlete):'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('strava_club_id'); ?>" name="<?php echo $this->get_field_name('strava_club_id'); ?>" type="text" value="<?php echo $strava_club_id; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('quantity'); ?>"><?php _e('Quantity:'); ?></label>
Expand All @@ -83,41 +74,29 @@ public function form($instance) {

// 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( $strava_club_id, $quantity );
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 = "<ul id='rides'>";
foreach($rides_details as $ride_obj) {
$ride = $ride_obj->ride;
foreach( $rides as $ride ) {
$response .= "<li class='ride'>";
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' >" . $ride->name . "</a>";
$response .= "<div class='ride-item'>";
$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 .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . "</a>";
}

$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 .= "</div>";
$response .= "</li>";
}
Expand Down
Loading

0 comments on commit 09a2679

Please sign in to comment.