diff --git a/.gitignore b/.gitignore index b25c15b..e0c38e3 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +.svn diff --git a/css/wp-strava.css b/css/wp-strava.css index 86cae73..fad25b8 100755 --- a/css/wp-strava.css +++ b/css/wp-strava.css @@ -8,7 +8,7 @@ background-position: -224px -160px; margin: 0 6px 2px 0; } -.table { +.wp-strava-ride-container { display: table; margin: 0 auto; } @@ -32,6 +32,6 @@ .ride-details-table-units { font-size: 0.8em; } -.map img { +.wp-strava-ride-container img { max-width: none; } diff --git a/lib/API.class.php b/lib/API.class.php index 33631e7..44dd657 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -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 ) { @@ -46,26 +43,33 @@ class WPStrava_API { 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 ); diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php index a77ab73..ec40b16 100644 --- a/lib/LatestMapWidget.class.php +++ b/lib/LatestMapWidget.class.php @@ -17,7 +17,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { 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'; @@ -27,35 +27,25 @@ class WPStrava_LatestMapWidget extends WP_Widget {

-

- - -

+

+ + +

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 @@ -64,12 +54,23 @@ class WPStrava_LatestMapWidget extends WP_Widget { 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 '
';
+					print_r($rides);
+					echo '
'; + } else { + echo $rides->get_error_message(); + } + echo $after_widget; + return; + } if ( ! empty( $rides ) ) { @@ -92,47 +93,20 @@ class WPStrava_LatestMapWidget extends WP_Widget { if ( $ride ): echo $before_widget; ?>

Latest Ride

- name ?>" href="http://app.strava.com/activities/id ?>">getStaticImage( $ride->id, $build_new ); ?>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 ""; - } 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 ); } diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index e84a4bb..a4f50b7 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -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(); ?> - 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 +74,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( $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 = "