Updated Latest Map and Latest Activity widgets to work with new multiple athlete settings.

This commit is contained in:
Justin Foell
2017-08-11 17:28:29 -05:00
parent 07b8b51762
commit 4f696b8c19
8 changed files with 215 additions and 50 deletions
+85 -12
View File
@@ -17,6 +17,8 @@ class WPStrava_LatestMapWidget extends WP_Widget {
public function form( $instance ) {
// outputs the options form on admin
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
@@ -28,6 +30,14 @@ class WPStrava_LatestMapWidget extends WP_Widget {
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></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>
<p>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ): ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php esc_attr_e( $nickname ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<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; ?>" />
@@ -43,6 +53,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
// processes widget options to be saved from the admin
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
return $instance;
@@ -52,19 +63,20 @@ class WPStrava_LatestMapWidget extends WP_Widget {
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$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.
$ride_transient = get_transient( 'strava_latest_map_ride' );
$ride_option = get_option( 'strava_latest_map_ride' );
$ride_transient = get_transient( 'strava_latest_map_activity_' . $athlete_token );
$ride_option = get_option( 'strava_latest_map_activity_' . $athlete_token );
$ride = $ride_transient ?: null;
if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $strava_club_id );
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id );
if ( is_wp_error( $rides ) ) {
echo $before_widget;
@@ -86,14 +98,16 @@ class WPStrava_LatestMapWidget extends WP_Widget {
$ride = current( $rides );
//update transients & options
// Compare transient (temporary storage) to option (more permenant).
// If the option isn't set or the transient is different, update the option.
if ( empty( $ride_option->id ) || $ride->id != $ride_option->id ) {
$build_new = true;
update_option( 'strava_latest_map_ride', $ride );
$this->update_activity( $athlete_token, $ride );
}
// Update the transient if it needs updating.
if ( empty( $ride_transient->id ) || $ride->id != $ride_transient->id ) {
set_transient( 'strava_latest_map_ride', $ride, HOUR_IN_SECONDS );
$this->update_activity_transient( $athlete_token, $ride );
}
}
}
@@ -102,22 +116,81 @@ class WPStrava_LatestMapWidget extends WP_Widget {
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
?><a title="<?php echo $ride->name ?>" target="_blank" href="http://app.strava.com/activities/<?php echo $ride->id ?>"><?php
echo $this->getStaticImage( $ride->id, $build_new );
echo $this->getStaticImage( $athlete_token, $ride, $build_new );
?></a><?php
echo $after_widget;
}
}
private function getStaticImage( $ride_id, $build_new ) {
$img = get_option( 'strava_latest_map' );
/**
* Get image for specific ride using Static Maps class.
*
* @author Justin Foell
*
* @param string $athlete_token Token for athelete.
* @param int $ride_id Club ID (optional).
* @param boolean $build_new Whether to refresh the image from cache.
* @return string Image tag.
*/
private function getStaticImage( $athlete_token, $ride, $build_new ) {
$img = get_option( 'strava_latest_map_' . $athlete_token );
if ( $build_new || ! $img ) {
$ride = WPStrava::get_instance()->rides->getRide( $ride_id );
$img = WPStrava_StaticMap::get_image_tag( $ride );
update_option( 'strava_latest_map', $img );
$this->update_map( $athlete_token, $img );
}
return $img;
}
/**
* Update map in option to cache.
*
* @author Justin Foell
* @since 1.2.0
*
* @param string $athlete_token Token for athelete.
* @param string $img Image tag.
*/
private function update_map( $athlete_token, $img ) {
// Remove old (pre 1.2.0) cached maps.
if ( get_option( 'strava_latest_map' ) ) {
delete_option( 'strava_latest_map' );
}
update_option( 'strava_latest_map_' . $athlete_token, $img );
}
/**
* Update activity in option to cache.
*
* @author Justin Foell
* @since 1.2.0
*
* @param string $athlete_token Token for athelete.
* @param object $activity stdClass Strava activity object.
*/
private function update_activity( $athlete_token, $activity ) {
// Remove old (pre 1.2.0) option.
if ( get_option( 'strava_latest_map_ride' ) ) {
delete_option( 'strava_latest_map_ride' );
}
update_option( 'strava_latest_map_activity_' . $athlete_token, $activity );
}
/**
* Update activity in transient to cache.
*
* @author Justin Foell
* @since 1.2.0
*
* @param string $athlete_token Token for athelete.
* @param object $activity stdClass Strava activity object.
*/
private function update_activity_transient( $athlete_token, $activity ) {
// Remove old (pre 1.2.0) transient.
if ( get_transient( 'strava_latest_map_ride' ) ) {
delete_transient( 'strava_latest_map_ride' );
}
set_transient( 'strava_latest_map_activity_' . $athlete_token, $activity, HOUR_IN_SECONDS );
}
}
+20 -8
View File
@@ -23,14 +23,14 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
//$widget_id = $args['widget_id'];
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Rides', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$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_club_id, $quantity ); ?>
<?php echo $this->strava_request_handler( $athlete_token, $strava_club_id, $quantity ); ?>
<?php echo $after_widget; ?>
<?php
}
@@ -39,6 +39,7 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['quantity'] = $new_instance['quantity'];
@@ -48,6 +49,8 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
/** @see WP_Widget::form */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Rides', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
@@ -57,7 +60,15 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
<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>
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php _e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ): ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php esc_attr_e( $nickname ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php _e( 'Club ID (leave blank to show single Athlete):', 'wp-strava' ); ?></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>
@@ -69,11 +80,12 @@ 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_club_id, $quantity ) {
private function strava_request_handler( $athlete_token, $strava_club_id, $quantity ) {
$som = WPStrava_SOM::get_som();
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $strava_club_id, $quantity );
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id, $quantity );
if ( is_wp_error( $rides ) )
return $rides->get_error_message();
@@ -89,9 +101,9 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
$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->total_elevation_gain ), $this->som->get_elevation_label() );
$response .= sprintf( __( ' rode %s %s', 'wp-strava' ), $som->distance( $ride->distance ), $som->get_distance_label() );
$response .= sprintf( __( ' during %s %s', 'wp-strava' ), $som->time( $ride->elapsed_time ), $som->get_time_label() );
$response .= sprintf( __( ' climbing %s %s', 'wp-strava' ), $som->elevation( $ride->total_elevation_gain ), $som->get_elevation_label() );
$response .= "</div>";
$response .= "</li>";
}
+26 -6
View File
@@ -7,12 +7,30 @@ class WPStrava_Rides {
const RIDES_URL = 'http://app.strava.com/rides/';
const ATHLETES_URL = 'http://app.strava.com/athletes/';
public function getRide( $rideId ) {
return WPStrava::get_instance()->api->get( "activities/{$rideId}" );
/**
* Get single activity by ID.
*
* @param string $athlete_token Token of athlete to retrieve for
* @param int $activity_id ID of activity to retrieve.
* @return object stdClass representing this activty.
* @author Justin Foell
*/
public function getRide( $athlete_token, $activity_id ) {
return WPStrava::get_instance()->get_api( $athlete_token )->get( "activities/{$activity_id}" );
} // getRideDetails
public function getRides( $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->api;
/**
* Get activity list from Strava API.
*
* @author Justin Foell
*
* @param string $athlete_token Token of athlete to retrieve for
* @param int $club_id Club ID of all club riders (optional).
* @param int|null $quantity Number of records to retrieve (optional).
* @return array|WP_Error Array of rides or WP_Error.
*/
public function getRides( $athlete_token, $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->get_api( $athlete_token );
$data = null;
@@ -25,11 +43,13 @@ class WPStrava_Rides {
$data = $api->get( 'athlete/activities', $args );
}
if ( is_wp_error( $data ) )
if ( is_wp_error( $data ) ) {
return $data;
}
if ( is_array( $data ) )
if ( is_array( $data ) ) {
return $data;
}
return array();
+70 -17
View File
@@ -77,7 +77,7 @@ class WPStrava_Settings {
//only update when redirected back from strava
if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] == $this->page_name ) {
if ( isset( $_GET['code'] ) ) {
$token = $this->get_token( $_GET['code'] );
$token = $this->fetch_token( $_GET['code'] );
if ( $token ) {
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
$this->add_token( $token );
@@ -194,18 +194,15 @@ class WPStrava_Settings {
}
public function print_nickname_input() {
$nickname = $this->tokens_empty( $this->tokens ) ? __( 'default', 'wp-strava' ) : '';
$nickname = $this->tokens_empty( $this->tokens ) ? __( 'Default', 'wp-strava' ) : '';
?><input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" /><?php
}
public function print_token_input() {
$nicknames = $this->nickname;
foreach ( $this->get_tokens() as $index => $token ) {
foreach ( $this->get_all_tokens() as $token => $nickname ) {
?>
<input type="text" name="strava_token[]" value="<?php echo $token; ?>" />
<?php if ( isset( $nicknames[ $index ] ) ) : ?>
<input type="text" name="strava_nickname[]" value="<?php echo $nicknames[ $index ]; ?>" />
<?php endif; ?>
<input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" />
<br/>
<?php
}
@@ -238,14 +235,14 @@ class WPStrava_Settings {
public function sanitize_nickname( $nicknames ) {
$second2last = $last = false;
if ( ! $this->adding_athlete ) {
// Reemove last (blank) entry if not trying to add an additional athlete.
$last = array_pop( $nicknames );
$second2last = end( $nicknames );
$last = end( $nicknames );
$second2last = prev( $nicknames );
$token_index = count( $nicknames ) - 1;
if ( $last === '' && $second2last === '' &&
if ( $last === '' && $second2last !== '' &&
empty( $_POST['strava_token'][ $token_index ] ) ) {
// Remove one more
// Remove last (blank) entry if not trying to add an additional athlete.
array_pop( $nicknames );
}
}
@@ -263,7 +260,7 @@ class WPStrava_Settings {
return $token;
}
private function get_token( $code ) {
private function fetch_token( $code ) {
$client_id = $this->client_id;
$client_secret = $this->client_secret;
@@ -322,10 +319,17 @@ class WPStrava_Settings {
public function sanitize_cache_clear( $checked ) {
if ( 'on' === $checked ) {
// Clear these values:
// Clear these (pre 1.2.0) values:
delete_transient( 'strava_latest_map_ride' );
delete_option( 'strava_latest_map_ride' );
delete_option( 'strava_latest_map' );
// Remove activity transients and options.
foreach ( $this->get_tokens() as $token ) {
delete_transient( 'strava_latest_map_activity_' . $token );
delete_option( 'strava_latest_map_activity_' . $token );
delete_option( 'strava_latest_map_' . $token );
}
}
return null;
}
@@ -335,7 +339,7 @@ class WPStrava_Settings {
*
* @return array
* @author Justin Foell
* @since NEXT
* @since 1.2.0
*/
public function get_tokens() {
$tokens = get_option( 'strava_token' );
@@ -352,11 +356,60 @@ class WPStrava_Settings {
return $tokens;
}
/**
* Returns first (default) token saved.
*
* @return string|null
* @author Justin Foell
* @since 1.2.0
*/
public function get_default_token() {
$tokens = $this->get_tokens();
return isset( $tokens[0] ) ? $tokens[0] : null;
}
/**
* Get all tokens and their nicknames in one array.
*
* @return void
* @author Justin Foell
* @since 1.2.0
*/
public function get_all_tokens() {
$tokens = $this->get_tokens();
$nicknames = $this->nickname;
$all = array();
$number = 1;
foreach ( $tokens as $index => $token ) {
if ( ! empty( $nicknames[ $index ] ) ) {
$all[ $token ] = $nicknames[ $index ];
} else {
$all[ $token ] = $this->get_default_nickname( $number );
}
$number++;
}
return $all;
}
/**
* Returns default nickname 'Default' / 'Athlete n'.
*
* @author Justin Foell
* @since 1.2.0
*
* @param integer $number
* @return string
*/
private function get_default_nickname( $number = 1 ) {
// Translators: Athelete number if no nickname present.
return ( 1 == $number ) ? __( 'Default', 'wp-strava' ) : sprintf( __( 'Athlete %s', 'wp-strava' ), $number );
}
/**
* Checks for valid tokens.
*
* @author Justin Foell
* @since NEXT
* @since 1.2.0
*
* @param string|array Single token or array of tokens.
* @return boolean True if empty.
@@ -383,7 +436,7 @@ class WPStrava_Settings {
* @param string $token
*
* @author Justin Foell
* @since NEXT
* @since 1.2.0
*/
public function add_token( $token ) {
if ( false === array_search( $token, $this->tokens ) ) {
+1 -1
View File
@@ -27,7 +27,7 @@ class WPStrava_StaticMap {
if ( ! empty( $ride->map->polyline ) && ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) ) {
$url .= $ride->map->polyline;
} else {
} elseif ( ! empty( $ride->map->summary_polyline ) ) {
$url .= $ride->map->summary_polyline;
}
+9 -5
View File
@@ -49,13 +49,17 @@ class WPStrava {
return null;
}
public function get_api( $id = '0' ) {
if ( ! $this->api[$id] ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
$this->api[$id] = new WPStrava_API( $this->settings->get_setting( 'strava_token', $id ) );
public function get_api( $token = null ) {
if ( ! $token ) {
$token = $this->settings->get_default_token();
}
return $this->api[$id];
if ( empty( $this->api[ $token ] ) ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
$this->api[$token] = new WPStrava_API( $token );
}
return $this->api[$token];
}
public function get_rides() {
+3
View File
@@ -32,6 +32,9 @@ latest map to activities of a certain minimum distance
== Changelog ==
= 1.2.0 =
Added multi-athlete configuration
= 1.1.1 =
Changes to better support translations through https://translate.wordpress.org
Cleaned up formatting
+1 -1
View File
@@ -3,7 +3,7 @@
* Plugin Name: WP Strava
* Plugin URI: https://wordpress.org/plugins/wp-strava/
* 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: 1.1.1
* Version: 1.2.0-beta1
* Author: Carlos Santa Cruz, Justin Foell, Lance Willet
* License: GPL2
* Text Domain: wp-strava