Files
wp-strava/lib/LatestMapWidget.class.php
T

197 lines
7.2 KiB
PHP
Raw Normal View History

2013-03-31 21:28:49 -05:00
<?php
class WPStrava_LatestMapWidget extends WP_Widget {
private $som;
2013-03-31 21:28:49 -05:00
public function __construct() {
$this->som = WPStrava_SOM::get_som();
2013-03-31 21:28:49 -05:00
parent::__construct(
false,
2017-05-26 10:53:24 -05:00
__( 'Strava Latest Map', 'wp-strava' ), // Name
2017-09-29 11:25:22 -05:00
array( 'description' => __( 'Strava latest activity using static google map image', 'wp-strava' ) ) // Args.
2013-03-31 21:28:49 -05:00
);
}
2013-03-31 21:28:49 -05:00
public function form( $instance ) {
// outputs the options form on admin
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity Map', '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'] ) : '';
2014-09-11 15:56:35 -05:00
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
2013-03-31 21:28:49 -05:00
?>
2015-05-11 22:28:33 -05:00
<p>
2017-05-26 10:53:24 -05:00
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></label>
2015-05-11 22:28:33 -05:00
<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 echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
2017-05-26 10:53:24 -05:00
<p>
2013-03-31 21:28:49 -05:00
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>"><?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?></label>
2017-05-26 10:53:24 -05:00
<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>
2014-09-11 15:56:35 -05:00
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php esc_html_e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
2017-05-26 10:53:24 -05:00
<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; ?>" />
2014-09-11 15:56:35 -05:00
</p>
2017-05-26 10:53:24 -05:00
<?php
2013-03-31 21:28:49 -05:00
}
2013-03-31 21:28:49 -05:00
public function update( $new_instance, $old_instance ) {
// 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'] );
2014-09-11 15:56:35 -05:00
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
2017-05-26 10:53:24 -05:00
return $instance;
2013-03-31 21:28:49 -05:00
}
/**
* Method to render the widget on the front end.
*
* @param array $args Arguments from the widget settings.
* @param array $instance Settings for this particular widget.
*/
2013-03-31 21:28:49 -05:00
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] );
2017-05-26 10:53:24 -05:00
$strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id'];
$build_new = false;
2018-01-26 16:11:34 -06:00
$id = empty( $strava_club_id ) ? $athlete_token : $strava_club_id;
2017-05-10 14:23:11 -05:00
// Try our transient first.
2018-01-26 16:11:34 -06:00
$activity_transient = get_transient( 'strava_latest_map_activity_' . $id );
$activity_option = get_option( 'strava_latest_map_activity_' . $id );
2013-03-31 21:28:49 -05:00
$activity = $activity_transient ? $activity_transient : null;
if ( ! $activity ) {
$strava_activity = WPStrava::get_instance()->activity;
$activities = $strava_activity->get_activities( $athlete_token, $strava_club_id );
if ( is_wp_error( $activities ) ) {
echo $args['before_widget'];
if ( $title ) {
echo $args['$before_title'] . $title . $args['$after_title'];
}
2014-09-23 13:39:48 -05:00
if ( WPSTRAVA_DEBUG ) {
echo '<pre>';
print_r( $activities ); // @codingStandardsIgnoreLine
2014-09-23 13:39:48 -05:00
echo '</pre>';
} else {
echo $activities->get_error_message();
2014-09-23 13:39:48 -05:00
}
echo $args['$after_widget'];
return;
}
if ( ! empty( $activities ) ) {
if ( ! empty( $distance_min ) ) {
$activities = $strava_activity->get_activities_longer_than( $activities, $distance_min );
}
2013-03-31 22:44:35 -05:00
$activity = current( $activities );
2013-03-31 22:44:35 -05:00
// Compare transient (temporary storage) to option (more permenant).
// If the option isn't set or the transient is different, update the option.
if ( empty( $activity_option->id ) || $activity->id != $activity_option->id ) {
2013-03-31 22:44:35 -05:00
$build_new = true;
2018-01-26 16:11:34 -06:00
$this->update_activity( $id, $activity );
2013-03-31 22:44:35 -05:00
}
// Update the transient if it needs updating.
if ( empty( $activity_transient->id ) || $activity->id != $activity_transient->id ) {
2018-01-26 16:11:34 -06:00
$this->update_activity_transient( $id, $activity );
}
2013-03-31 22:44:35 -05:00
}
}
if ( $activity ) {
2018-01-26 14:43:34 -06:00
echo $args['before_widget'];
if ( $title ) {
2018-01-26 14:43:34 -06:00
echo $args['before_title'] . $title . $args['after_title'];
}
echo "<a title='{$activity->name}' href='http://app.strava.com/activities/{$activity->id}'>";
2018-01-26 16:11:34 -06:00
echo $this->get_static_image( $id, $activity, $build_new );
echo '</a>';
2018-01-26 14:43:34 -06:00
echo $args['after_widget'];
}
2013-03-31 22:44:35 -05:00
}
2013-03-31 21:28:49 -05:00
/**
* Get image for specific activity using Static Maps class.
*
* @author Justin Foell
*
2018-01-26 16:11:34 -06:00
* @param string $id Athlete Token or Club ID.
* @param object $activity Activity to get image for.
* @param boolean $build_new Whether to refresh the image from cache.
* @return string Image tag.
*/
2018-01-26 16:11:34 -06:00
private function get_static_image( $id, $activity, $build_new ) {
$img = get_option( 'strava_latest_map_' . $id );
2013-03-31 22:44:35 -05:00
if ( $build_new || ! $img ) {
$img = WPStrava_StaticMap::get_image_tag( $activity );
2018-01-26 16:11:34 -06:00
$this->update_map( $id, $img );
2013-03-31 22:44:35 -05:00
}
return $img;
2013-03-31 21:28:49 -05:00
}
/**
* Update map in option to cache.
*
* @author Justin Foell
* @since 1.2.0
*
2018-01-26 16:11:34 -06:00
* @param string $id Athlete Token or Club ID.
* @param string $img Image tag.
*/
2018-01-26 16:11:34 -06:00
private function update_map( $id, $img ) {
update_option( 'strava_latest_map_' . $id, $img );
}
/**
* Update activity in option to cache.
*
* @author Justin Foell
* @since 1.2.0
*
2018-01-26 16:11:34 -06:00
* @param string $id Athlete Token or Club ID.
* @param object $activity stdClass Strava activity object.
*/
2018-01-26 16:11:34 -06:00
private function update_activity( $id, $activity ) {
update_option( 'strava_latest_map_activity_' . $id, $activity );
}
/**
* Update activity in transient to cache.
*
* @author Justin Foell
* @since 1.2.0
*
2018-01-26 16:11:34 -06:00
* @param string $id Athlete Token or Club ID.
* @param object $activity stdClass Strava activity object.
*/
2018-01-26 16:11:34 -06:00
private function update_activity_transient( $id, $activity ) {
set_transient( 'strava_latest_map_activity_' . $id, $activity, HOUR_IN_SECONDS );
}
2017-05-26 10:53:24 -05:00
}