Fixed latest map widget

This commit is contained in:
Justin Foell
2014-09-11 15:56:35 -05:00
parent 76cb5f1b22
commit 1be075356a
6 changed files with 33 additions and 89 deletions
+18 -45
View File
@@ -17,7 +17,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
public function form( $instance ) { public function form( $instance ) {
// outputs the options form on admin // outputs the options form on admin
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : ''; $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 //provide some defaults
//$ride_index_params = $ride_index_params ? $ride_index_params : 'athleteId=21'; //$ride_index_params = $ride_index_params ? $ride_index_params : 'athleteId=21';
@@ -27,35 +27,25 @@ class WPStrava_LatestMapWidget extends WP_Widget {
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>"><?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?></label> <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; ?>" /> <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>
<p> <p>
<label for="<?php echo $this->get_field_id( 'ride_index_params' ); ?>"><?php _e( 'Ride Search Parameters (one per line): ' ); ?> <label for="<?php echo $this->get_field_id('strava_club_id'); ?>"><?php _e('Club ID (leave blank to show Athlete):'); ?></label>
<a href="https://stravasite-main.pbworks.com/w/page/51754146/Strava%20REST%20API%20Method%3A%20rides%20index" target="_blank"><?php _e( 'help' ); ?></a></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; ?>" />
<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>
<?php <?php
} }
public function update( $new_instance, $old_instance ) { public function update( $new_instance, $old_instance ) {
// processes widget options to be saved from the admin // processes widget options to be saved from the admin
$instance = $old_instance; $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'] ); $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; return $instance;
} }
public function widget( $args, $instance ) { public function widget( $args, $instance ) {
extract( $args ); extract( $args );
$ride_index_params = $instance['ride_index_params'];
$distance_min = $instance['distance_min']; $distance_min = $instance['distance_min'];
$strava_club_id = empty( $instance['strava_club_id'] ) ? NULL : $instance['strava_club_id'];
$build_new = false; $build_new = false;
//try our transient first //try our transient first
@@ -67,9 +57,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
if ( ! $ride ) { if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides; $strava_rides = WPStrava::get_instance()->rides;
$ride_index_params = implode( '&', explode( "\n", $ride_index_params ) ); $rides = $strava_rides->getRides( $strava_club_id );
parse_str( $ride_index_params, $params );
$rides = $strava_rides->getRidesAdvanced( $params );
if ( is_wp_error( $rides ) ) { if ( is_wp_error( $rides ) ) {
echo $before_widget; echo $before_widget;
@@ -101,47 +89,32 @@ class WPStrava_LatestMapWidget extends WP_Widget {
if ( $ride ): if ( $ride ):
echo $before_widget; echo $before_widget;
?><h3 class="widget-title">Latest Ride</h3> ?><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 ); echo $this->getStaticImage( $ride->id, $build_new );
?></a><?php ?></a><?php
echo $after_widget; echo $after_widget;
endif; endif;
} }
private function buildImage( $map_details ) { private function buildImage( $ride ) {
$url = 'http://maps.google.com/maps/api/staticmap?maptype=terrain&size=390x260&sensor=false&path=color:0xFF0000BF|weight:2|'; $url = 'http://maps.google.com/maps/api/staticmap?maptype=terrain&size=390x260&sensor=false&path=color:0xFF0000BF|weight:2|enc:';
$url_len = strlen( $url ); $url_len = strlen( $url );
$point_len = 0;
$num = 50;
$count = count( $map_details->latlng );
$full_url = '';
$max_chars = 1865; $max_chars = 1865;
//get the longest usable URL if ( $url_len + strlen( $ride->map->polyline ) < $max_chars )
while ( $url_len + $point_len < $max_chars ) { $url .= $ride->map->polyline;
$mod = (int) ( $count / $num ); else
$points = array(); $url .= $ride->map->summary_polyline;
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 ); return "<img src='{$url}' />";
$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 ) { private function getStaticImage( $ride_id, $build_new ) {
$img = get_option( 'strava_latest_map' ); $img = get_option( 'strava_latest_map' );
if ( $build_new || ! $img ) { if ( $build_new || ! $img ) {
$map_details = WPStrava::get_instance()->rides->getMapDetails( $ride_id ); $ride = WPStrava::get_instance()->rides->getRide( $ride_id );
$img = $this->buildImage( $map_details ); $img = $this->buildImage( $ride );
update_option( 'strava_latest_map', $img ); update_option( 'strava_latest_map', $img );
} }
+1 -1
View File
@@ -72,7 +72,7 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
$strava_rides = WPStrava::get_instance()->rides; $strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $quantity, $strava_club_id ); $rides = $strava_rides->getRides( $strava_club_id, $quantity );
if ( is_wp_error( $rides ) ) if ( is_wp_error( $rides ) )
return $rides->get_error_message(); return $rides->get_error_message();
+3 -3
View File
@@ -35,8 +35,8 @@ class WPStrava_RideShortcode {
$strava_som = get_option('strava_som_option', 'metric'); $strava_som = get_option('strava_som_option', 'metric');
} }
$ride = new Rides(); $ride = WPStrava::get_instance()->rides;
$rideDetails = $ride->getRideDetails($id, $strava_som); $rideDetails = $ride->getRide($id);
$rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold); $rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold);
if ($strava_som == "metric") { if ($strava_som == "metric") {
@@ -123,4 +123,4 @@ class WPStrava_RideShortcode {
} }
// Initialize short code // Initialize short code
RideShortcode::init(); WPStrava_RideShortcode::init();
+9 -38
View File
@@ -7,32 +7,22 @@ class WPStrava_Rides {
const RIDES_URL = "http://app.strava.com/rides/"; const RIDES_URL = "http://app.strava.com/rides/";
const ATHLETES_URL = "http://app.strava.com/athletes/"; const ATHLETES_URL = "http://app.strava.com/athletes/";
public function getRideDetails( $rideId ) { public function getRide( $rideId ) {
return WPStrava::get_instance()->api->get( "activities/{$rideId}" ); return WPStrava::get_instance()->api->get( "activities/{$rideId}" );
} // getRideDetails } // getRideDetails
public function getRidesDetails( $rides ) { public function getRides( $club_id = NULL, $quantity = NULL ) {
$rides_details = array();
foreach ( $rides as $stravaRide ) {
$detail = $this->getRideDetails( $stravaRide->id );
if ( is_wp_error( $detail ) )
return $detail;
$rides_details[] = $detail;
}
return $rides_details;
} // getRidesDetails
public function getRides( $quantity, $club_id = NULL ) {
$api = WPStrava::get_instance()->api; $api = WPStrava::get_instance()->api;
$data = NULL; $data = NULL;
$args = $quantity ? array( 'per_page' => $quantity ) : NULL;
//Get the json results using the constructor specified values. //Get the json results using the constructor specified values.
if ( is_numeric( $club_id ) ) { if ( is_numeric( $club_id ) ) {
$data = $api->get( "clubs/{$club_id}/activities", array( 'per_page' => $quantity ) ); $data = $api->get( "clubs/{$club_id}/activities", $args );
} else { } else {
$data = $api->get( 'athlete/activities', array( 'per_page' => $quantity ) ); $data = $api->get( 'athlete/activities', $args );
} }
if ( is_wp_error( $data ) ) if ( is_wp_error( $data ) )
@@ -45,18 +35,6 @@ class WPStrava_Rides {
} // getRides } // getRides
public function getRidesAdvanced( $params ) {
$data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1
if ( is_wp_error( $data ) )
return $data;
if ( isset( $data->rides ) )
return $data->rides;
return array();
}
public function getRideMap($rideId, $token, $efforts, $threshold) { public function getRideMap($rideId, $token, $efforts, $threshold) {
if($rideId != 0 AND $token != "") { if($rideId != 0 AND $token != "") {
$url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2); $url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2);
@@ -81,9 +59,8 @@ class WPStrava_Rides {
$meters = $som->distance_inverse( $dist ); $meters = $som->distance_inverse( $dist );
$long_rides = array(); $long_rides = array();
foreach ( $rides as $ride ) { foreach ( $rides as $ride_info ) {
$ride_info = $this->getRideDetails( $ride->id ); if ( $ride_info->distance > $meters ) {
if ( $ride_info->ride->distance > $meters ) {
$long_rides[] = $ride_info; $long_rides[] = $ride_info;
} }
} }
@@ -91,11 +68,5 @@ class WPStrava_Rides {
return $long_rides; return $long_rides;
} }
public function getMapDetails( $ride_id ) {
$token = WPStrava::get_instance()->settings->token;
return WPStrava::get_instance()->api->get( "rides/{$ride_id}/map_details", array( 'token' => $token ) );
}
} // class Rides } // class Rides
?> ?>
+1
View File
@@ -4,6 +4,7 @@ require_once WPSTRAVA_PLUGIN_DIR . 'lib/Settings.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOM.class.php'; require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOM.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php'; require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php'; require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/RideShortcode.class.php';
class WPStrava { class WPStrava {
+1 -2
View File
@@ -3,7 +3,7 @@
Plugin Name: WP Strava Plugin Name: WP Strava
Plugin URI: http://cmanon.com 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. 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.70 Version: 1.0
Author: Carlos Santa Cruz (cmanon), Justin Foell <justin@foell.org> Author: Carlos Santa Cruz (cmanon), Justin Foell <justin@foell.org>
Author URI: http://cmanon.com Author URI: http://cmanon.com
License: GPL2 License: GPL2
@@ -35,7 +35,6 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php';
$wpstrava = WPStrava::get_instance(); $wpstrava = WPStrava::get_instance();
//wp_die( '<pre>' . var_export( $wpstrava->api->get( 'athlete' ), true ) );
//@TODO only load these when needed using is_active_widget() //@TODO only load these when needed using is_active_widget()
function load_styles() { function load_styles() {