Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-link-4/

Lots of formatting (code standards)
Bumped version #
  • Loading branch information
jrfoell committed Dec 26, 2017
1 parent b6abb19 commit bd40fe4
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 165 deletions.
34 changes: 19 additions & 15 deletions lib/API.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function post( $uri, $data = null ) {
$url = self::STRAVA_V3_API;

$args = array(
'body' => http_build_query( $data ),
'body' => http_build_query( $data ),
'sslverify' => false,
'headers' => array(),
'headers' => array(),
);

if ( $this->access_token ) {
Expand All @@ -28,17 +28,19 @@ public function post( $uri, $data = null ) {

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

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

if ( $response['response']['code'] != 200 ) {
if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
if ( ! empty( $body->error ) ) {
$error = $body->error;
else
$error = print_r( $response, true );
} else {
$error = print_r( $response, true ); // @codingStandardsIgnoreLine
}

return new WP_Error(
'wp-strava_post',
Expand Down Expand Up @@ -69,19 +71,21 @@ public function get( $uri, $args = null ) {

$response = wp_remote_get( $url, $get_args );

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

if ( $response['response']['code'] != 200 ) {
if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
if ( ! empty( $body->error ) ) {
$error = $body->error;
else if ( $response['response']['code'] == 503 )
} elseif ( 503 == $response['response']['code'] ) {
$error = __( 'Strava Temporarily Unavailable', 'wp-strava' );
else
$error = print_r( $response, true );
} else {
$error = print_r( $response, true ); // @codingStandardsIgnoreLine
}

return new WP_Error(
'wp-strava_get',
Expand Down
11 changes: 4 additions & 7 deletions lib/ActivityShortcode.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static function init() {
}

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

Expand All @@ -20,13 +20,13 @@ public static function handler( $atts ) {
'map_width' => '480',
'map_height' => '320',
'athlete_token' => WPStrava::get_instance()->settings->get_default_token(),
'markers' => false,
'markers' => false,
);

extract( shortcode_atts( $defaults, $atts ) );

$strava_som = WPStrava_SOM::get_som( $som );
$activity = WPStrava::get_instance()->rides;
$strava_som = WPStrava_SOM::get_som( $som );
$activity = WPStrava::get_instance()->rides;
$ride_details = $activity->getRide( $athlete_token, $id );

//sanitize width & height
Expand Down Expand Up @@ -76,9 +76,6 @@ public static function handler( $atts ) {
public static function print_scripts() {
if ( self::$add_script ) {
wp_enqueue_style( 'wp-strava-style' );

//wp_print_scripts('google-maps');
//wp_print_scripts('wp-strava-script');
}
}
}
Expand Down
60 changes: 32 additions & 28 deletions lib/LatestMapWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ public function __construct() {
$this->som = WPStrava_SOM::get_som();

parent::__construct(
false,
false,
__( 'Strava Latest Map', 'wp-strava' ), // Name
array( 'description' => __( 'Strava latest activity 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 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'] ) : '';
$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'] ) : '';
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';

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

?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></label>
Expand All @@ -33,8 +30,8 @@ public function form( $instance ) {
<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 foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
Expand All @@ -43,47 +40,50 @@ public function form( $instance ) {
<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>
<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( 'strava_club_id' ); ?>"><?php esc_html_e( 'Club ID (leave blank to show 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>
<?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['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
// 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'] );
$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 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'] );
$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'] );
$strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id'];
$build_new = false;
$build_new = false;

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

$ride = $ride_transient ? $ride_transient : null;

if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id );
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id );

if ( is_wp_error( $rides ) ) {
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
if ( $title ) {
echo $before_title . $title . $after_title;
}

if ( WPSTRAVA_DEBUG ) {
echo '<pre>';
print_r($rides);
print_r( $rides ); // @codingStandardsIgnoreLine
echo '</pre>';
} else {
echo $rides->get_error_message();
Expand All @@ -94,8 +94,9 @@ public function widget( $args, $instance ) {

if ( ! empty( $rides ) ) {

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

$ride = current( $rides );

Expand All @@ -115,10 +116,13 @@ public function widget( $args, $instance ) {

if ( $ride ) {
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
if ( $title ) {
echo $before_title . $title . $after_title;
}

echo "<a title='{$ride->name}' target='_blank' href='http://app.strava.com/activities/{$ride->id}'>";
echo $this->getStaticImage( $athlete_token, $ride, $build_new );
?></a><?php
echo '</a>';
echo $after_widget;
}
}
Expand Down
79 changes: 42 additions & 37 deletions lib/LatestRidesWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
class WPStrava_LatestRidesWidget extends WP_Widget {

public function __construct() {
$widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.', 'wp-strava' ) );
parent::__construct( 'wp-strava', $name = __( 'Strava Latest Activity List', 'wp-strava' ), $widget_ops );
$widget_ops = array(
'classname' => 'LatestRidesWidget',
'description' => __( 'Will publish your latest rides activity from strava.com.', 'wp-strava' ),
);
parent::__construct( 'wp-strava', __( 'Strava Latest Activity List', 'wp-strava' ), $widget_ops );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) );
}

Expand All @@ -21,38 +24,37 @@ public function maybe_enqueue() {
public function widget( $args, $instance ) {
extract( $args );

//$widget_id = $args['widget_id'];
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activity', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activity', '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'];
$quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity'];

?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php echo $this->strava_request_handler( $athlete_token, $strava_club_id, $quantity ); ?>
<?php echo $after_widget; ?>
<?php
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
echo $this->strava_request_handler( $athlete_token, $strava_club_id, $quantity );
echo $after_widget;
}

/** @see WP_Widget::update */
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 = $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'];
$instance['quantity'] = $new_instance['quantity'];

return $instance;
}

/** @see WP_Widget::form */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( '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();
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( '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();
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;

?>
<p>
Expand All @@ -62,17 +64,17 @@ public function form( $instance ) {
<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 foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $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; ?>" />
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php esc_html_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>
<label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php _e( 'Quantity:', 'wp-strava' ); ?></label>
<label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php esc_html_e( 'Quantity:', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'quantity' ); ?>" name="<?php echo $this->get_field_name( 'quantity' ); ?>" type="text" value="<?php echo $quantity; ?>" />
</p>
<?php
Expand All @@ -82,34 +84,37 @@ public function form( $instance ) {
// the parsing directly on the jQuery ajax call, the returned text will be enclosed in the $response variable.
private function strava_request_handler( $athlete_token, $strava_club_id, $quantity ) {

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

$rides = $strava_rides->getRides( $athlete_token, $strava_club_id, $quantity );
if ( is_wp_error( $rides ) )
if ( is_wp_error( $rides ) ) {
return $rides->get_error_message();
}

$response = "<ul id='rides'>";
foreach ( $rides as $ride ) {
$response .= "<li class='ride'>";
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' target='_blank'>" . $ride->name . '</a>';
$response .= "<a href='" . WPStrava_Rides::ACTIVITIES_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 ) );
$unixtime = strtotime( $ride->start_date_local );
// translators: Shows something like "On <date> <name [went 10 miles] [during 1 hour] [climbing 100 feet]>."
$response .= sprintf( __( 'On %1$s %2$s', 'wp-strava' ), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );

if ( is_numeric( $strava_club_id ) ) {
$response .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . '</a>';
}

$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>";
// translators: "went 10 miles"
$response .= sprintf( __( ' went %1$s %2$s', 'wp-strava' ), $som->distance( $ride->distance ), $som->get_distance_label() );
// translators: "during 1 hour"
$response .= sprintf( __( ' during %1$s %2$s', 'wp-strava' ), $som->time( $ride->elapsed_time ), $som->get_time_label() );
// translators: "climbing 100 feet"
$response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $ride->total_elevation_gain ), $som->get_elevation_label() );
$response .= '</div></li>';
}
$response .= "</ul>";
$response .= '</ul>';
return $response;
} // Function strava_request_handler

} // class LatestRidesWidget

6 changes: 3 additions & 3 deletions lib/Rides.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/
class WPStrava_Rides {

const RIDES_URL = 'http://app.strava.com/rides/';
const ATHLETES_URL = 'http://app.strava.com/athletes/';
const ACTIVITIES_URL = 'http://app.strava.com/activities/';
const ATHLETES_URL = 'http://app.strava.com/athletes/';

/**
* Get single activity by ID.
Expand Down Expand Up @@ -56,7 +56,7 @@ public function getRides( $athlete_token, $club_id = null, $quantity = null ) {
} // getRides

public function getRidesLongerThan( $rides, $dist ) {
$som = WPStrava_SOM::get_som();
$som = WPStrava_SOM::get_som();
$meters = $som->distance_inverse( $dist );

$long_rides = array();
Expand Down
Loading

0 comments on commit bd40fe4

Please sign in to comment.