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.
This commit is contained in:
cmanon
2012-06-12 17:36:49 -05:00
parent 213f77e65c
commit cba3bfc82f
6 changed files with 202 additions and 145 deletions
Regular → Executable
View File
+2 -1
View File
@@ -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 {
Executable → Regular
View File

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 122 KiB

+39
View File
@@ -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');
});
});
+21 -17
View File
@@ -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) {
+140 -127
View File
@@ -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() {
</div>
<?php } // Finished admin menu options
// Load required javascript libraries
wp_enqueue_script('jquery');
//wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false');
// Register a personalized stylesheet
wp_register_style('wp-strava', plugins_url('css/wp-strava.css', __FILE__));
/**
* WP Strava Latest Rides Widget Class
@@ -250,126 +259,130 @@ function strava_request_handler($strava_search_option, $strava_search_id, $strav
return $response;
} // Function strava_request_handler
// Shortcode handler function
// [ride id=id efforts=false threshold=5 map-width="100%" map-height="400px"] tag
function strava_ride_shortcode_handler($atts) {
$token = get_option('strava_token');
$strava_som = get_option('strava_som');
if($token) {
$pairs = array(
'id' => 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 "
<div id='ride-header-{$id}' class='table'>
<table id='ride-details-table'>
<thead>
<tr>
<th>Elapsed Time</th>
<th>Moving Time</th>
<th>Distance</th>
<th>Average Speed</th>
<th>Max Speed</th>
<th>Elevation Gain</th>
</tr>
</thead>
<tbody>
<tr class='ride-details-table-info'>
<td>{$rideDetails['elapsedTime']}</td>
<td>{$rideDetails['movingTime']}</td>
<td>{$rideDetails['distance']}</td>
<td>{$rideDetails['averageSpeed']}</td>
<td>{$rideDetails['maximumSpeed']}</td>
<td>{$rideDetails['elevationGain']}</td>
</tr>
<tr class='ride-details-table-units'>
<td>{$units['elapsedTime']}</td>
<td>{$units['movingTime']}</td>
<td>{$units['distance']}</td>
<td>{$units['averageSpeed']}</td>
<td>{$units['maximumSpeed']}</td>
<td>{$units['elevationGain']}</td>
</tr>
</tbody>
</table>
</div>
<div id='ride-map-{$id}' class='map' style='width: {$map_width}; height: {$map_height}; border: 1px solid lightgrey;'></div>
<script type='text/javascript' src='http://maps.google.com/maps/api/js?sensor=false'></script>
<script type='text/javascript'>
jQuery(document).ready(function($){
var coordinates = eval({$rideCoordinates});
var rideCoordinates = coordinates.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($('#ride-map-{$id}')[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');
});
</script>
";
}
} 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 "
<div id='ride-header-{$id}' class='table'>
<table id='ride-details-table'>
<thead>
<tr>
<th>Elapsed Time</th>
<th>Moving Time</th>
<th>Distance</th>
<th>Average Speed</th>
<th>Elevation Gain</th>
</tr>
</thead>
<tbody>
<tr class='ride-details-table-info'>
<td>{$rideDetails['elapsedTime']}</td>
<td>{$rideDetails['movingTime']}</td>
<td>{$rideDetails['distance']}</td>
<td>{$rideDetails['averageSpeed']}</td>
<td>{$rideDetails['elevationGain']}</td>
</tr>
<tr class='ride-details-table-units'>
<td>{$units['elapsedTime']}</td>
<td>{$units['movingTime']}</td>
<td>{$units['distance']}</td>
<td>{$units['averageSpeed']}</td>
<td>{$units['elevationGain']}</td>
</tr>
</tbody>
</table>
</div>
<div id='{$id}' class='map' style='width: {$map_width}; height: {$map_height}; border: 1px solid lightgrey;'></div>
<script type='text/javascript'>
if (window.coordinates === undefined) {
window.coordinates = [];
}
window.coordinates[{$id}] = eval({$rideCoordinates});
</script>
";
}
} 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();
?>