From cba3bfc82f6387ef6bb336962a623cc2e293a098 Mon Sep 17 00:00:00 2001 From: cmanon Date: Tue, 12 Jun 2012 17:36:49 -0500 Subject: [PATCH] Version 0.62 release: - Tested with wordpress 3.3.2 - Changed default icon for side widget. - Fixed issue with club id search. - Fixed and enabled shortcode [ride]. - Changed login and ride_details calls to use Strava V2 API. --- .gitignore | 0 css/wp-strava.css | 3 +- images/fugue-sprite.png | Bin js/wp-strava.js | 39 ++++++ rides.class.php | 38 +++--- wp-strava.php | 267 +++++++++++++++++++++------------------- 6 files changed, 202 insertions(+), 145 deletions(-) mode change 100644 => 100755 .gitignore mode change 100755 => 100644 images/fugue-sprite.png create mode 100644 js/wp-strava.js diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/css/wp-strava.css b/css/wp-strava.css index 05e10ae..86cae73 100755 --- a/css/wp-strava.css +++ b/css/wp-strava.css @@ -4,7 +4,8 @@ width: 16px; height: 16px; float: left; - background: url('../images/fugue-sprite.png') -48px -96px no-repeat; + background: url('../images/fugue-sprite.png') no-repeat; + background-position: -224px -160px; margin: 0 6px 2px 0; } .table { diff --git a/images/fugue-sprite.png b/images/fugue-sprite.png old mode 100755 new mode 100644 diff --git a/js/wp-strava.js b/js/wp-strava.js new file mode 100644 index 0000000..5b05e32 --- /dev/null +++ b/js/wp-strava.js @@ -0,0 +1,39 @@ + +jQuery(document).ready(function($){ + $('.map').each(function(){ + var mapId = $(this).attr('id'); + + var rideCoordinates = window.coordinates[mapId].latlng; + var mapCenter = new google.maps.LatLng(23.091860, -102.839356); + var mapOptions = { + zoom: 5, + center: mapCenter, + //mapTypeControl: true, + //mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, + zoomControl: true, + zoomControlOptions: {style: google.maps.MapTypeControlStyle.SMALL}, + mapTypeId: google.maps.MapTypeId.TERRAIN + }; + var mapObject = new google.maps.Map($('#' + mapId)[0], mapOptions); + var mapBounds = new google.maps.LatLngBounds(); + var mapCoordinates = new google.maps.MVCArray(); + var size = rideCoordinates.length; + for(i = 0; i < size; i++) { + point = new google.maps.LatLng(parseFloat(rideCoordinates[i][0]), parseFloat(rideCoordinates[i][1])); + mapBounds.extend(point); + mapCoordinates.push(point); + } + + var polylineOptions = { + path: mapCoordinates, + strokeColor: '#e0642e', + strokeOpacity: 0.8, + strokeWeight: 3 + }; + + var polyline = new google.maps.Polyline(polylineOptions); + polyline.setMap(mapObject); + mapObject.fitBounds(mapBounds); + //google.maps.event.trigger(mapObject, 'resize'); + }); +}); diff --git a/rides.class.php b/rides.class.php index f9695fe..39d165b 100755 --- a/rides.class.php +++ b/rides.class.php @@ -5,10 +5,12 @@ namespace WP\Strava; */ class Rides { 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"; + private $rideMapDetailsUrlV2 = "http://www.strava.com/api/v2/rides/:id/map_details"; public $ridesLinkUrl = "http://app.strava.com/rides/"; public $stravaRides; @@ -19,35 +21,37 @@ class Rides { } // __construct public function getRideDetails($rideId, $systemOfMeasurement) { - $url = preg_replace('/:id/', $rideId, $this->rideUrl); - //$url = $this->ridesUrl . $rideId; + $url = preg_replace('/:id/', $rideId, $this->rideUrlV2); $json = file_get_contents($url); + if($json) { $strava_ride = json_decode($json); //Transform data to a ready to be displayed format - $startDate = date("F j, Y - H:i a", strtotime($strava_ride->ride->startDateLocal)); - $elapsedTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->elapsedTime)); - $movingTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->movingTime)); + $startDate = date("F j, Y - H:i a", strtotime($strava_ride->ride->start_date_local)); + $elapsedTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->elapsed_time)); + $movingTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->moving_time)); if ($systemOfMeasurement == "metric") { //To km $distance = number_format($strava_ride->ride->distance/1000, 2); //To km/h - $averageSpeed = number_format($strava_ride->ride->averageSpeed * 3.6, 2); + $averageSpeed = number_format($strava_ride->ride->average_speed * 3.6, 2); //To km/h - $maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1000, 2); + // Removed on version 2 of the Strava API + //$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1000, 2); //It is already in meters - $elevationGain = number_format($strava_ride->ride->elevationGain, 2); + $elevationGain = number_format($strava_ride->ride->elevation_gain, 2); } elseif ($systemOfMeasurement == "english") { //To miles $distance = number_format($strava_ride->ride->distance/1609.34, 2); //To miles/h - $averageSpeed = number_format($strava_ride->ride->averageSpeed * 2.2369, 2); + $averageSpeed = number_format($strava_ride->ride->average_speed * 2.2369, 2); //To miles/h - $maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1609.34, 2); + // Removed on version 2 of the Strava API + //$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1609.34, 2); //To foot - $elevationGain = number_format($strava_ride->ride->elevationGain/0.3048, 2); + $elevationGain = number_format($strava_ride->ride->elevation_gain/0.3048, 2); } $ride_details = array( @@ -59,7 +63,7 @@ class Rides { 'movingTime' => $movingTime, 'distance' => $distance, 'averageSpeed' => $averageSpeed, - 'maximumSpeed' => $maximumSpeed, + //'maximumSpeed' => $maximumSpeed, 'elevationGain' => $elevationGain ); return $ride_details; @@ -91,7 +95,7 @@ class Rides { } else { $json = file_get_contents($url . '?athleteName=' . urlencode($searchId)); } - } elseif ($this->searchOption == "club" AND is_numeric($searchId)) { + } elseif ($searchOption == "club" AND is_numeric($searchId)) { $json = file_get_contents($url . '?clubId=' . urlencode($searchId)); } else { $this->feedback .= _e("There's an error on the widget options combination.", "wp-strava"); @@ -108,11 +112,11 @@ class Rides { public function getAuthenticationToken($email, $password) { $util = new Util; $data = array('email' => $email, 'password' => $password); - $json = $util->makePostRequest($this->authenticationUrl, $data); + $json = $util->makePostRequest($this->authenticationUrlV2, $data); if($json) { $strava_login = json_decode($json); - if($strava_login->success == "true") { + if(!isset($strava_login->error)) { $this->feedback .= __('Successfully authenticated.', 'wp-strava'); return $strava_login->token; } else { @@ -127,7 +131,7 @@ class Rides { public function getRideMap($rideId, $token, $efforts, $threshold) { if($rideId != 0 AND $token != "") { - $url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlv2); + $url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2); $json = file_get_contents($url . '?token=' . $token . '&threshold=' . $threshold); if($json) { diff --git a/wp-strava.php b/wp-strava.php index eecb9a8..13efab4 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -6,7 +6,7 @@ require_once 'util.class.php'; Plugin Name: WP Strava Plugin URI: http://cmanon.com Description: Plugin to show your strava.com information in your wordpress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. -Version: 0.61 +Version: 0.62 Author: Carlos Santa Cruz (cmanon) Author URI: http://cmanon.com License: GPL2 @@ -49,6 +49,21 @@ function register_strava_settings() { //register_setting('wp-strava-settings-group','strava_som'); } +function load_styles() { + // Register a personalized stylesheet + wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__)); + wp_enqueue_style('wp-strava'); +} +add_action('wp_enqueue_script', 'load_styles'); + +function load_scripts() { + // Load required javascript libraries + wp_enqueue_script('jquery'); + //wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); +} +add_action('wp-enqueue_script', 'load_scripts'); + + function wp_strava_plugin_options() { if (!current_user_can('administrator')) { wp_die(__('You do not have sufficient permissions to access this page.')); @@ -102,12 +117,6 @@ function wp_strava_plugin_options() { 0, - 'efforts' => false, - 'threshold' => 0, - 'map_width' => "100%", - 'map_height' => "400px" - ); - - extract(shortcode_atts($pairs, $atts)); - - $ride = new Rides(); - $rideDetails = $ride->getRideDetails($id, $strava_som_option); - $rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold); - - if ($strava_som == "metric") { - $units = array( - 'elapsedTime' => __('hours','wp-strava'), - 'movingTime' => __('hours','wp-strava'), - 'distance' => __('km','wp-strava'), - 'averageSpeed' => __('km/h','wp-strava'), - 'maximumSpeed' => __('km/h','wp-strava'), - 'elevationGain' => __('meters','wp-strava') - ); - } elseif ($strava_som_option == "english") { - $units = array( - 'elapsedTime' => __('hours','wp-strava'), - 'movingTime' => __('hours','wp-strava'), - 'distance' => __('miles','wp-strava'), - 'averageSpeed' => __('miles/h','wp-strava'), - 'maximumSpeed' => __('miles/h','wp-strava'), - 'elevationGain' => __('feet','wp-strava') - ); - } - - if($rideCoordinates) { - return " -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Elapsed TimeMoving TimeDistanceAverage SpeedMax SpeedElevation Gain
{$rideDetails['elapsedTime']}{$rideDetails['movingTime']}{$rideDetails['distance']}{$rideDetails['averageSpeed']}{$rideDetails['maximumSpeed']}{$rideDetails['elevationGain']}
{$units['elapsedTime']}{$units['movingTime']}{$units['distance']}{$units['averageSpeed']}{$units['maximumSpeed']}{$units['elevationGain']}
-
-
- - - "; - } - } else { - return _e('Please first get your strava token using the settings wp strava page.', 'wp-strava'); + +class RideShortcode { + static $add_script; + + static function init() { + add_shortcode('ride', array(__CLASS__, 'handler')); + + add_action('init', array(__CLASS__, 'registerScripts')); + add_action('wp_footer', array(__CLASS__, 'printScripts')); } -} // strava_ride_shortcode_handler -//add_shortcode('ride', 'WP\Strava\strava_ride_shortcode_handler'); + + // Shortcode handler function + // [ride id=id som=metric efforts=false threshold=5 map-width="100%" map-height="400px"] tag + function handler($atts) { + self::$add_script = true; + + $token = get_option('strava_token'); + + if($token) { + $pairs = array( + 'id' => 0, + 'som' => "metric", + 'efforts' => false, + 'threshold' => 0, + 'map_width' => "100%", + 'map_height' => "400px" + ); + + extract(shortcode_atts($pairs, $atts)); + + if (isset($som)) { + $strava_som = $som; + } else { + $strava_som = get_option('strava_som_option', 'metric'); + } + + $ride = new Rides(); + $rideDetails = $ride->getRideDetails($id, $strava_som); + $rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold); + + if ($strava_som == "metric") { + $units = array( + 'elapsedTime' => __('hours','wp-strava'), + 'movingTime' => __('hours','wp-strava'), + 'distance' => __('km','wp-strava'), + 'averageSpeed' => __('km/h','wp-strava'), + //'maximumSpeed' => __('km/h','wp-strava'), + 'elevationGain' => __('meters','wp-strava') + ); + } elseif ($strava_som == "english") { + $units = array( + 'elapsedTime' => __('hours','wp-strava'), + 'movingTime' => __('hours','wp-strava'), + 'distance' => __('miles','wp-strava'), + 'averageSpeed' => __('miles/h','wp-strava'), + //'maximumSpeed' => __('miles/h','wp-strava'), + 'elevationGain' => __('feet','wp-strava') + ); + } + + if($rideCoordinates) { + return " +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Elapsed TimeMoving TimeDistanceAverage SpeedElevation Gain
{$rideDetails['elapsedTime']}{$rideDetails['movingTime']}{$rideDetails['distance']}{$rideDetails['averageSpeed']}{$rideDetails['elevationGain']}
{$units['elapsedTime']}{$units['movingTime']}{$units['distance']}{$units['averageSpeed']}{$units['elevationGain']}
+
+
+ + "; + } + } else { + return _e('Please first get your strava token using the settings wp strava page.', 'wp-strava'); + } + } // handler + + static function registerScripts() { + wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__)); + + wp_register_script('wp-strava-script', plugins_url('js/wp-strava.js', __FILE__), array('jquery'), '1.0', true); + wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); + } + + static function printScripts() { + if (self::$add_script) { + wp_enqueue_style('wp-strava-style'); + wp_enqueue_script('jquery'); + + wp_print_scripts('google-maps'); + wp_print_scripts('wp-strava-script'); + } + } +} + +// Initialize short code +RideShortcode::init(); ?>