Skip to content

Commit

Permalink
Removed all instances of 'ride' in favor of 'activity'
Browse files Browse the repository at this point in the history
Removed all extract()
Updated code standards and documentation
Update readme
  • Loading branch information
jrfoell committed Jan 26, 2018
1 parent 49f82ca commit ffe977c
Show file tree
Hide file tree
Showing 18 changed files with 318 additions and 215 deletions.
16 changes: 8 additions & 8 deletions css/wp-strava.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@CHARSET "UTF-8";
.ride-item:before {
.activity-item:before {
content: "";
width: 16px;
height: 16px;
Expand All @@ -8,30 +8,30 @@
background-position: -224px -160px;
margin: 0 6px 2px 0;
}
.wp-strava-ride-container {
.wp-strava-activity-container {
display: table;
margin: 0 auto;
}
#ride-details-table tr td {
#activity-details-table tr td {
padding: 5px;
}
#ride-details-table th {
#activity-details-table th {
padding: 5px;
text-align: center;
font-size: 0.8em;
}
#ride-details-table td {
#activity-details-table td {
text-align: center;
border: 1px solid #e7e7e7;
}
.ride-details-table-info {
.activity-details-table-info {
font-size: 1.2em;
font-weight: bold;
text-shadow: 1px 1px 0px rgba(0, 0, 0, 0.4);
}
.ride-details-table-units {
.activity-details-table-units {
font-size: 0.8em;
}
.wp-strava-ride-container img {
.wp-strava-activity-container img {
max-width: none;
}
39 changes: 0 additions & 39 deletions js/wp-strava.js

This file was deleted.

16 changes: 8 additions & 8 deletions lib/API.class.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

/*
* Util is a class with all the utility methods.
* API class for all remote calls.
*/
class WPStrava_API {

//deactivated
//const STRAVA_V1_API = 'http://www.strava.com/api/v1/'; //rides?athleteId=134698
//const STRAVA_V2_API = 'http://www.strava.com/api/v2/'; //rides/:ride_id/map_details
const STRAVA_V3_API = 'https://www.strava.com/api/v3/';

public function __construct( $access_token = null ) {
Expand All @@ -33,7 +31,8 @@ public function post( $uri, $data = null ) {
}

if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body

// See if there's useful info in the body.
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) ) {
Expand All @@ -45,7 +44,7 @@ public function post( $uri, $data = null ) {
return new WP_Error(
'wp-strava_post',
// Translators: message shown when there's a problem with ab HTTP POST to the Strava API.
sprintf( __( 'ERROR %1$s %2$s - See full error by adding <code>define( \'WP_STRAVA_DEBUG\', true );</code> to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ),
sprintf( __( 'ERROR %1$s %2$s - See full error by adding<br/><code>define( \'WP_STRAVA_DEBUG\', true );</code><br/>to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ),
$error
);
}
Expand Down Expand Up @@ -76,7 +75,8 @@ public function get( $uri, $args = null ) {
}

if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body

// See if there's useful info in the body.
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) ) {
Expand All @@ -90,7 +90,7 @@ public function get( $uri, $args = null ) {
return new WP_Error(
'wp-strava_get',
// Translators: message shown when there's a problem with an HTTP GET to the Strava API.
sprintf( __( 'ERROR %1$s %2$s - See full error by adding <code>define( \'WP_STRAVA_DEBUG\', true );</code> to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ),
sprintf( __( 'ERROR %1$s %2$s - See full error by adding<br/><code>define( \'WP_STRAVA_DEBUG\', true );</code><br/>to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ),
$error
);
}
Expand Down
46 changes: 27 additions & 19 deletions lib/Rides.class.php → lib/Activity.class.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php
/*
* Rides is a class wrapper for the Strava REST API functions.
* Activity is a class wrapper for the Strava REST API functions.
*/
class WPStrava_Rides {
class WPStrava_Activity {

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

/**
* 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.
* @param string $athlete_token Token of athlete to retrieve for
* @param int $activity_id ID of activity to retrieve.
* @return object stdClass representing this activity.
* @author Justin Foell
*/
public function getRide( $athlete_token, $activity_id ) {
public function get_activity( $athlete_token, $activity_id ) {
return WPStrava::get_instance()->get_api( $athlete_token )->get( "activities/{$activity_id}" );
} // getRideDetails
}

/**
* Get activity list from Strava API.
Expand All @@ -27,9 +27,9 @@ public function getRide( $athlete_token, $activity_id ) {
* @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.
* @return array|WP_Error Array of activities or WP_Error.
*/
public function getRides( $athlete_token, $club_id = null, $quantity = null ) {
public function get_activities( $athlete_token, $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->get_api( $athlete_token );

$data = null;
Expand All @@ -53,20 +53,28 @@ public function getRides( $athlete_token, $club_id = null, $quantity = null ) {

return array();

} // getRides
}

public function getRidesLongerThan( $rides, $dist ) {
/**
* Undocumented function
*
* @param array $activities
* @param float $dist Distance in default system of measure (km/mi).
* @return void
* @author Justin Foell
*/
public function get_activities_longer_than( $activities, $dist ) {
$som = WPStrava_SOM::get_som();
$meters = $som->distance_inverse( $dist );

$long_rides = array();
foreach ( $rides as $ride_info ) {
if ( $ride_info->distance > $meters ) {
$long_rides[] = $ride_info;
$long_activities = array();
foreach ( $activities as $activity_info ) {
if ( $activity_info->distance > $meters ) {
$long_activities[] = $activity_info;
}
}

return $long_rides;
return $long_activities;
}

} // class Rides
}
53 changes: 31 additions & 22 deletions lib/ActivityShortcode.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class WPStrava_ActivityShortcode {
private static $add_script;

public static function init() {
add_shortcode( 'ride', array( __CLASS__, 'handler' ) );
add_shortcode( 'ride', array( __CLASS__, 'handler' ) ); // @deprecated 1.1
add_shortcode( 'activity', array( __CLASS__, 'handler' ) );
add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) );
}
Expand All @@ -23,22 +23,30 @@ public static function handler( $atts ) {
'markers' => false,
);

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

$strava_som = WPStrava_SOM::get_som( $som );
$activity = WPStrava::get_instance()->rides;
$ride_details = $activity->getRide( $athlete_token, $id );
$strava_som = WPStrava_SOM::get_som( $atts['som'] );
$activity = WPStrava::get_instance()->activity;
$activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] );

if ( is_wp_error( $activity_details ) ) {
if ( WPSTRAVA_DEBUG ) {
return '<pre>' . print_r( $activity_details, true ) . '</pre>'; // @codingStandardsIgnoreLine
} else {
return $activity_details->get_error_message();
}
}

//sanitize width & height
$map_width = str_replace( '%', '', $map_width );
$map_height = str_replace( '%', '', $map_height );
$map_width = str_replace( '%', '', $atts['map_width'] );
$map_height = str_replace( '%', '', $atts['map_height'] );
$map_width = str_replace( 'px', '', $map_width );
$map_height = str_replace( 'px', '', $map_height );

if ( $ride_details ) {
if ( $activity_details ) {
return '
<div id="ride-header-' . $id . '" class="wp-strava-ride-container">
<table id="ride-details-table">
<div id="activity-header-' . $atts['id'] . '" class="wp-strava-activity-container">
<table id="activity-details-table">
<thead>
<tr>
<th>' . __( 'Elapsed Time', 'wp-strava' ) . '</th>
Expand All @@ -50,15 +58,15 @@ public static function handler( $atts ) {
</tr>
</thead>
<tbody>
<tr class="ride-details-table-info">
<td>' . $strava_som->time( $ride_details->elapsed_time ) . '</td>
<td>' . $strava_som->time( $ride_details->moving_time ) . '</td>
<td>' . $strava_som->distance( $ride_details->distance ) . '</td>
<td>' . $strava_som->speed( $ride_details->average_speed ) . '</td>
<td>' . $strava_som->speed( $ride_details->max_speed ) . '</td>
<td>' . $strava_som->elevation( $ride_details->total_elevation_gain ) . '</td>
<tr class="activity-details-table-info">
<td>' . $strava_som->time( $activity_details->elapsed_time ) . '</td>
<td>' . $strava_som->time( $activity_details->moving_time ) . '</td>
<td>' . $strava_som->distance( $activity_details->distance ) . '</td>
<td>' . $strava_som->speed( $activity_details->average_speed ) . '</td>
<td>' . $strava_som->speed( $activity_details->max_speed ) . '</td>
<td>' . $strava_som->elevation( $activity_details->total_elevation_gain ) . '</td>
</tr>
<tr class="ride-details-table-units">
<tr class="activity-details-table-units">
<td>' . $strava_som->get_time_label() . '</td>
<td>' . $strava_som->get_time_label() . '</td>
<td>' . $strava_som->get_distance_label() . '</td>
Expand All @@ -67,11 +75,12 @@ public static function handler( $atts ) {
<td>' . $strava_som->get_elevation_label() . '</td>
</tr>
</tbody>
</table>' .
WPStrava_StaticMap::get_image_tag( $ride_details, $map_height, $map_width, $markers ) .
</table>
<a title="' . $activity_details->name . '" href="' . WPStrava_Activity::ACTIVITIES_URL . $activity_details->id . '">' .
WPStrava_StaticMap::get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'] ) .
'</div>';
} // End if( $ride_details ).
} // handler
} // End if( $activity_details ).
}

public static function print_scripts() {
if ( self::$add_script ) {
Expand Down
30 changes: 15 additions & 15 deletions lib/LatestActivities.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ public static function get_activities_html( $args ) {

$args = wp_parse_args( $args, $defaults );

$som = WPStrava_SOM::get_som( $args['som'] );
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] );
$som = WPStrava_SOM::get_som( $args['som'] );
$strava_activity = WPStrava::get_instance()->activity;
$activities = $strava_activity->get_activities( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] );

if ( is_wp_error( $rides ) ) {
return $rides->get_error_message();
if ( is_wp_error( $activities ) ) {
return $activities->get_error_message();
}

$response = "<ul id='rides'>";
foreach ( $rides as $ride ) {
$response .= "<li class='ride'>";
$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 = "<ul id='activities'>";
foreach ( $activities as $activity ) {
$response .= "<li class='activity'>";
$response .= "<a href='" . WPStrava_Activity::ACTIVITIES_URL . $activity->id . "'>" . $activity->name . '</a>';
$response .= "<div class='activity-item'>";
$unixtime = strtotime( $activity->start_date_local );
// Translators: Shows something like "On <date> <[went 10 miles] [during 2 hours] [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( $args['strava_club_id'] ) ) {
$response .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . '</a>';
$response .= " <a href='" . WPStrava_Activity::ATHLETES_URL . $activity->athlete_id . "'>" . $activity->athlete_name . '</a>';
}

// Translators: "went 10 miles"
$response .= sprintf( __( ' went %1$s %2$s', 'wp-strava' ), $som->distance( $ride->distance ), $som->get_distance_label() );
$response .= sprintf( __( ' went %1$s %2$s', 'wp-strava' ), $som->distance( $activity->distance ), $som->get_distance_label() );
// Translators: "during 2 hours"
$response .= sprintf( __( ' during %1$s %2$s', 'wp-strava' ), $som->time( $ride->elapsed_time ), $som->get_time_label() );
$response .= sprintf( __( ' during %1$s %2$s', 'wp-strava' ), $som->time( $activity->elapsed_time ), $som->get_time_label() );
// Translators: "climbing 100 ft."
$response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $ride->total_elevation_gain ), $som->get_elevation_label() );
$response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $activity->total_elevation_gain ), $som->get_elevation_label() );
$response .= '</div></li>';
}
$response .= '</ul>';
Expand Down
2 changes: 1 addition & 1 deletion lib/LatestActivitiesWidget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'LatestActivitiesWidget',
'description' => __( 'Will publish your latest activities from strava.com.', 'wp-strava' ),
'description' => __( 'Will show your latest activities from strava.com.', 'wp-strava' ),
);
parent::__construct( 'wp-strava', __( 'Strava Latest Activities List', 'wp-strava' ), $widget_ops );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) );
Expand Down
Loading

0 comments on commit ffe977c

Please sign in to comment.