Skip to content

Commit

Permalink
Merge pull request #6 from jrfoell/master
Browse files Browse the repository at this point in the history
This is currently what's v1.1 on wp.org :)
  • Loading branch information
cmanon authored May 10, 2017
2 parents 09a2679 + 3bb9faa commit 2e99255
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 113 deletions.
3 changes: 3 additions & 0 deletions lib/API.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function post( $uri, $data = NULL ) {

$response = wp_remote_post( $url . $uri, $args );

if ( is_wp_error( $response ) )
return $response;

if ( $response['response']['code'] != 200 ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
Expand Down
47 changes: 27 additions & 20 deletions lib/LatestMapWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@
class WPStrava_LatestMapWidget extends WP_Widget {

private $som;

public function __construct() {
$this->som = WPStrava_SOM::get_som();

parent::__construct(
false,
'Strava Latest Map', // Name
array( 'description' => __( 'Strava latest ride using static google map image', 'wp-strava' ), ) // Args
);
}

public function form( $instance ) {
// outputs the options form on admin
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity', 'wp-strava' );
$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'] ) : '';

//provide some defaults
//$ride_index_params = $ride_index_params ? $ride_index_params : 'athleteId=21';
//$ride_index_params = $ride_index_params ?: 'athleteId=21';

?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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( '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; ?>" />
Expand All @@ -31,29 +36,31 @@ public function form( $instance ) {
<label for="<?php echo $this->get_field_id('strava_club_id'); ?>"><?php _e('Club ID (leave blank to show Athlete):'); ?></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>
<?php
<?php
}

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['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
return $instance;
}

public function widget( $args, $instance ) {
extract( $args );

$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity', 'wp-strava' ) : $instance['title'] );
$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

// Try our transient first.
$ride_transient = get_transient( 'strava_latest_map_ride' );
$ride_option = get_option( 'strava_latest_map_ride' );

if ( $ride_transient )
$ride = $ride_transient;
$ride = $ride_transient ?: null;

if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides;
Expand All @@ -73,37 +80,38 @@ public function widget( $args, $instance ) {
}

if ( ! empty( $rides ) ) {

if ( ! empty( $distance_min ) )
$rides = $strava_rides->getRidesLongerThan( $rides, $distance_min );

$ride = current( $rides );

//update transients & options
if ( $ride->id != $ride_option->id ) {
if ( empty( $ride_option->id ) || $ride->id != $ride_option->id ) {
$build_new = true;
update_option( 'strava_latest_map_ride', $ride );
}

if ( $ride->id != $ride_transient->id )
set_transient( 'strava_latest_map_ride', $ride, 60 * 60 ); //one hour
if ( empty( $ride_transient->id ) || $ride->id != $ride_transient->id ) {
set_transient( 'strava_latest_map_ride', $ride, HOUR_IN_SECONDS );
}
}
}

if ( $ride ):
if ( $ride ) {
echo $before_widget;
?><h3 class="widget-title">Latest Ride</h3>
<a title="<?php echo $ride->name ?>" href="http://app.strava.com/activities/<?php echo $ride->id ?>"><?php
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 );
?></a><?php
echo $after_widget;
endif;
}
}


private function getStaticImage( $ride_id, $build_new ) {
$img = get_option( 'strava_latest_map' );

if ( $build_new || ! $img ) {
$ride = WPStrava::get_instance()->rides->getRide( $ride_id );
$img = WPStrava_StaticMap::get_image_tag( $ride );
Expand All @@ -112,5 +120,4 @@ private function getStaticImage( $ride_id, $build_new ) {

return $img;
}

}
11 changes: 3 additions & 8 deletions lib/LatestRidesWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function widget( $args, $instance ) {
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php echo $this->strava_request_handler( $strava_club_id, $strava_som_option, $quantity ); ?>
<?php echo $this->strava_request_handler( $strava_club_id, $quantity ); ?>
<?php echo $after_widget; ?>
<?php
}
Expand All @@ -39,11 +39,6 @@ public function widget( $args, $instance ) {
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
if( in_array( $new_instance['strava_som_option'], array( 'metric', 'english' ) ) ) {
$instance['strava_som_option'] = $new_instance['strava_som_option'];
} else {
$instance['strava_som_option'] = 'metric';
}
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['quantity'] = $new_instance['quantity'];

Expand Down Expand Up @@ -74,7 +69,7 @@ public function form( $instance ) {

// 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, $strava_som_option, $quantity ) {
private function strava_request_handler( $strava_club_id, $quantity ) {

$strava_rides = WPStrava::get_instance()->rides;

Expand All @@ -85,7 +80,7 @@ private function strava_request_handler( $strava_club_id, $strava_som_option, $q
$response = "<ul id='rides'>";
foreach( $rides as $ride ) {
$response .= "<li class='ride'>";
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' >" . $ride->name . "</a>";
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' target='_blank'>" . $ride->name . "</a>";
$response .= "<div class='ride-item'>";
$unixtime = strtotime( $ride->start_date_local );
$response .= sprintf( __("On %s %s", "wp-strava"), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );
Expand Down
15 changes: 8 additions & 7 deletions lib/RideShortcode.class.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php

class WPStrava_RideShortcode {
static $add_script;
private static $add_script;

static function init() {
add_shortcode('ride', array(__CLASS__, 'handler'));
add_action('wp_footer', array(__CLASS__, 'printScripts'));
public static function init() {
add_shortcode( 'ride', array( __CLASS__, 'handler' ) );
add_shortcode( 'activity', array( __CLASS__, 'handler' ) );
add_action( 'wp_footer', array( __CLASS__, 'printScripts' ) );
}

// Shortcode handler function
// [ride id=id som=metric map_width="100%" map_height="400px"]
function handler($atts) {
public static function handler($atts) {
self::$add_script = true;

$defaults = array(
Expand All @@ -31,7 +32,7 @@ function handler($atts) {
$map_height = str_replace( '%', '', $map_height );
$map_width = str_replace( 'px', '', $map_width );
$map_height = str_replace( 'px', '', $map_height );

if( $rideDetails ) {
return '
<div id="ride-header-' . $id . '" class="wp-strava-ride-container">
Expand Down Expand Up @@ -70,7 +71,7 @@ function handler($atts) {
}
} // handler

static function printScripts() {
public static function printScripts() {
if (self::$add_script) {
wp_enqueue_style('wp-strava-style');

Expand Down
6 changes: 3 additions & 3 deletions lib/SOM.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public static function get_som( $som = NULL ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMMetric.class.php';
return new WPStrava_SOMMetric();
}

}

abstract public function distance( $m );
abstract public function distance_inverse( $dist );
abstract public function get_distance_label();
Expand All @@ -29,4 +29,4 @@ public function time( $seconds ) {
public function get_time_label() {
return __( 'hours', 'wp-strava' );
}
}
}
Loading

0 comments on commit 2e99255

Please sign in to comment.