mirror of
https://github.com/10h30/wp-strava.git
synced 2026-07-13 03:36:43 +09:00
Renamed Util to API
Added SOM classes for English & Metric Use WP HTTP API for requests
This commit is contained in:
Executable
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* Util is a class with all the utility methods.
|
||||
*/
|
||||
class WPStrava_API {
|
||||
|
||||
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
|
||||
|
||||
/*
|
||||
private $rideUrl = "http://www.strava.com/api/v1/rides/:id";
|
||||
private $rideUrlV2 = "http://www.strava.com/api/v2/rides/:id";
|
||||
private $ridesUrl = "http://www.strava.com/api/v1/rides";
|
||||
private $authenticationUrl = "https://www.strava.com/api/v1/authentication/login";
|
||||
private $authenticationUrlV2 = "https://www.strava.com/api/v2/authentication/login";
|
||||
private $rideMapDetailsUrl = "http://www.strava.com/api/v1/rides/:id/map_details";
|
||||
private $rideMapDetailsUrlV2 = "http://www.strava.com/api/v2/rides/:id/map_details";
|
||||
*/
|
||||
|
||||
public $ridesLinkUrl = "http://app.strava.com/rides/";
|
||||
public $athletesLinkUrl = "http://app.strava.com/athletes/";
|
||||
public $feedback = '';
|
||||
|
||||
public function post( $uri, $data = NULL, $version = 2 ) {
|
||||
$url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API;
|
||||
|
||||
$args = array(
|
||||
'body' => http_build_query( $data ),
|
||||
);
|
||||
|
||||
if ( $version == 2 )
|
||||
$args['sslverify'] = false;
|
||||
|
||||
$response = wp_remote_post( $url . $uri, $args );
|
||||
|
||||
if ( empty( $response['response']['code'] ) || $response['response']['code'] != 200 ) {
|
||||
$this->feedback .= sprintf( __( 'ERROR - %s', 'wp-strava'), print_r( $response, true ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
return json_decode( $response['body'] );
|
||||
}
|
||||
|
||||
public function get( $uri, $version = 2 ) {
|
||||
$url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API;
|
||||
|
||||
$response = wp_remote_get( $url . $uri );
|
||||
|
||||
if ( empty( $response['response']['code'] ) || $response['response']['code'] != 200 ) {
|
||||
$this->feedback .= sprintf( __( 'ERROR - %s', 'wp-strava'), print_r( $response, true ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
return json_decode( $response['body'] );
|
||||
}
|
||||
|
||||
} // class API
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
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
|
||||
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
|
||||
$ride_index_params = isset( $instance['ride_index_params'] ) ? esc_attr( $instance['ride_index_params'] ) : '';
|
||||
|
||||
//provide some defaults
|
||||
//$ride_index_params = $ride_index_params ? $ride_index_params : 'athleteId=21';
|
||||
|
||||
?>
|
||||
<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; ?>" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id( 'ride_index_params' ); ?>"><?php _e( 'Ride Search Parameters (one per line): ' ); ?>
|
||||
<a href="https://stravasite-main.pbworks.com/w/page/51754146/Strava%20REST%20API%20Method%3A%20rides%20index" target="_blank"><?php _e( 'help' ); ?></a></label>
|
||||
<textarea name="<?php echo $this->get_field_name( 'ride_index_params' ); ?>" id="<?php echo $this->get_field_id( 'ride_index_params' ); ?>" cols="10" rows="5" class="widefat"><?php echo $ride_index_params; ?></textarea>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
// processes widget options to be saved from the admin
|
||||
$instance = $old_instance;
|
||||
$instance['ride_index_params'] = strip_tags( $new_instance['ride_index_params'] );
|
||||
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
|
||||
|
||||
/*
|
||||
if ( empty( $instance['ride_index_params'] ) ) {
|
||||
$instance['ride_index_params'] = "athleteId={$auth->athlete->id}";
|
||||
}
|
||||
*/
|
||||
|
||||
//$instance['athlete_hash'] = strip_tags( $new_instance['athlete_hash'] );
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public function widget( $args, $instance ) {
|
||||
extract( $args );
|
||||
$ride_index_params = $instance['ride_index_params'];
|
||||
$distance_min = $instance['distance_min'];
|
||||
|
||||
$rides = $this->getRides( $ride_index_params );
|
||||
|
||||
if ( ! empty( $rides ) ):
|
||||
|
||||
if ( ! empty( $distance_min ) )
|
||||
$rides = $this->getRidesLongerThan( $rides, $distance_min );
|
||||
|
||||
$ride = current( $rides );
|
||||
|
||||
$map_deets = $this->getMapDetails( $ride->id );
|
||||
|
||||
echo $before_widget;
|
||||
|
||||
$max = 50;
|
||||
$count = count( $map_deets->latlng );
|
||||
$mod = (int) ( $count / $max );
|
||||
$points = array();
|
||||
for ( $i = 0; $i < $count; $i += $mod ) {
|
||||
$point = $map_deets->latlng[$i];
|
||||
$points[] = number_format( $point[0],4 ) . ',' . number_format( $point[1],4 );
|
||||
}
|
||||
|
||||
$url_points = join( '|', $points );
|
||||
echo "<img src='http://maps.google.com/maps/api/staticmap?maptype=terrain&size=390x260&sensor=false&path=color:0xFF0000BF|weight:2|{$url_points}' />";
|
||||
/*
|
||||
echo '<pre>';
|
||||
print_r($map_deets);
|
||||
echo '</pre>';
|
||||
*/
|
||||
echo $after_widget;
|
||||
endif;
|
||||
}
|
||||
|
||||
private function getRides( $params ) {
|
||||
$data = WPStrava::get_instance()->api->get( 'rides?' . implode( '&', explode( "\n", $params ) ), 1 ); //version 1
|
||||
|
||||
if ( isset( $data->rides ) )
|
||||
return $data->rides;
|
||||
return array();
|
||||
}
|
||||
|
||||
private function getRideInfo( $ride_id ) {
|
||||
return WPStrava::get_instance()->api->get( "rides/{$ride_id}" );
|
||||
}
|
||||
|
||||
private function getRidesLongerThan( $rides, $dist ) {
|
||||
$meters = $this->som->distance_inverse( $dist );
|
||||
|
||||
$long_rides = array();
|
||||
foreach ( $rides as $ride ) {
|
||||
$ride_info = $this->getRideInfo( $ride->id );
|
||||
if ( $ride_info->ride->distance > $meters ) {
|
||||
$long_rides[] = $ride_info;
|
||||
}
|
||||
}
|
||||
return $long_rides;
|
||||
}
|
||||
|
||||
private function getMapDetails( $ride_id ) {
|
||||
$token = WPStrava::get_instance()->settings->token;
|
||||
return WPStrava::get_instance()->api->get( "rides/{$ride_id}/map_details?token={$token}" );
|
||||
}
|
||||
}
|
||||
+2
-37
@@ -3,23 +3,9 @@
|
||||
* Rides is a class wrapper for the Strava REST API functions.
|
||||
*/
|
||||
class WPStrava_Rides {
|
||||
private $rideUrl = "http://www.strava.com/api/v1/rides/:id";
|
||||
private $rideUrlV2 = "http://www.strava.com/api/v2/rides/:id";
|
||||
private $ridesUrl = "http://www.strava.com/api/v1/rides";
|
||||
private $authenticationUrl = "https://www.strava.com/api/v1/authentication/login";
|
||||
private $authenticationUrlV2 = "https://www.strava.com/api/v2/authentication/login";
|
||||
private $rideMapDetailsUrl = "http://www.strava.com/api/v1/rides/:id/map_details";
|
||||
private $rideMapDetailsUrlV2 = "http://www.strava.com/api/v2/rides/:id/map_details";
|
||||
|
||||
public $ridesLinkUrl = "http://app.strava.com/rides/";
|
||||
public $athletesLinkUrl = "http://app.strava.com/athletes/";
|
||||
public $stravaRides;
|
||||
public $feedback;
|
||||
|
||||
public function __construct() {
|
||||
// Empty constructor
|
||||
} // __construct
|
||||
|
||||
|
||||
public function getRideDetails($rideId, $systemOfMeasurement) {
|
||||
$url = preg_replace('/:id/', $rideId, $this->rideUrl);
|
||||
$json = file_get_contents($url);
|
||||
@@ -110,28 +96,7 @@ class WPStrava_Rides {
|
||||
return false;
|
||||
}
|
||||
} // getLatestRides
|
||||
|
||||
public function getAuthenticationToken($email, $password) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Util.class.php';
|
||||
$util = new WPStrava_Util();
|
||||
$data = array('email' => $email, 'password' => $password);
|
||||
$json = $util->makePostRequest($this->authenticationUrlV2, $data);
|
||||
|
||||
if($json) {
|
||||
$strava_login = json_decode($json);
|
||||
if(!isset($strava_login->error)) {
|
||||
$this->feedback .= __('Successfully authenticated.', 'wp-strava');
|
||||
return $strava_login->token;
|
||||
} else {
|
||||
$this->feedback .= __('Authentication failed, please check your credentials.', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= __('There was an error pulling data of strava.com.', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
} // getAuthenticationToken
|
||||
|
||||
|
||||
public function getRideMap($rideId, $token, $efforts, $threshold) {
|
||||
if($rideId != 0 AND $token != "") {
|
||||
$url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
abstract class WPStrava_SOM {
|
||||
|
||||
public static function get_som( $som = NULL ) {
|
||||
$som = $som ? $som : WPStrava::get_instance()->settings->som;
|
||||
if ( $som == 'metric' ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMMetric.class.php';
|
||||
return new WPStrava_SOMMetric();
|
||||
} else {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMEnglish.class.php';
|
||||
return new WPStrava_SOMEnglish();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract public function distance( $m );
|
||||
abstract public function distance_inverse( $dist );
|
||||
abstract public function get_distance_label();
|
||||
abstract public function speed( $mps );
|
||||
abstract public function get_speed_label();
|
||||
abstract public function elevation( $m );
|
||||
abstract public function get_elevation_label();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
class WPStrava_SOMEnglish extends WPStrava_SOM {
|
||||
|
||||
/**
|
||||
* @param string $m meters
|
||||
* @return string mi
|
||||
*/
|
||||
public function distance( $m ) {
|
||||
return number_format( $m / 1609.344, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dist miles
|
||||
* @return string meters
|
||||
*/
|
||||
public function distance_inverse( $dist ) {
|
||||
return number_format( $dist * 1609.344, 2 );
|
||||
}
|
||||
|
||||
public function get_distance_label() {
|
||||
return __( 'mi.', 'wp-strava' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mps
|
||||
* @return string mph
|
||||
*/
|
||||
public function speed( $mps ) {
|
||||
return number_format( $mps * 2.2369, 2 );
|
||||
}
|
||||
|
||||
public function get_speed_label() {
|
||||
return __( 'mph', 'wp-strava' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $m meters
|
||||
* @return string feet
|
||||
*/
|
||||
public function elevation( $m ) {
|
||||
return number_format( $m / 0.3048, 2 );
|
||||
}
|
||||
|
||||
public function get_elevation_label() {
|
||||
return __( 'ft.', 'wp-strava' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
class WPStrava_SOMMetric extends WPStrava_SOM
|
||||
/**
|
||||
* @param $m meters
|
||||
* @return string km
|
||||
*/
|
||||
public function distance( $m ) {
|
||||
return number_format( $m / 1000, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dist km
|
||||
* @return string meters
|
||||
*/
|
||||
public function distance_inverse( $dist ) {
|
||||
return number_format( $dist * 1000, 2 );
|
||||
}
|
||||
|
||||
public function get_distance_label() {
|
||||
return __( 'km', 'wp-strava' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $mps
|
||||
* @return string km/h
|
||||
*/
|
||||
public function speed( $mps ) {
|
||||
return number_format( $mps * 3.6, 2 );
|
||||
}
|
||||
|
||||
public function get_speed_label() {
|
||||
return __( 'km/h', 'wp-strava' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $m meters
|
||||
* @return string meters
|
||||
*/
|
||||
public function elevation( $m ) {
|
||||
return number_format( $m, 2 );
|
||||
}
|
||||
|
||||
public function get_elevation_label() {
|
||||
return __( 'meters', 'wp-strava' );
|
||||
}
|
||||
+24
-6
@@ -2,6 +2,8 @@
|
||||
|
||||
class WPStrava_Settings {
|
||||
|
||||
private $feedback;
|
||||
|
||||
//register admin menus
|
||||
public function hook() {
|
||||
add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
|
||||
@@ -80,15 +82,12 @@ class WPStrava_Settings {
|
||||
|
||||
public function sanitize_token( $token ) {
|
||||
if ( ! empty( $_POST['strava_password'] ) ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Rides.class.php';
|
||||
$ride = new WPStrava_Rides();
|
||||
$email = get_option( 'strava_email' );
|
||||
$token = $ride->getAuthenticationToken( $email, $_POST['strava_password'] );
|
||||
$token = $this->get_authentication_token( $this->email, $_POST['strava_password'] );
|
||||
if ( $token ) {
|
||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $ride->feedback ) , 'updated' );
|
||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' );
|
||||
return $token;
|
||||
} else {
|
||||
add_settings_error( 'strava_token', 'strava_token', $ride->feedback );
|
||||
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -96,6 +95,25 @@ class WPStrava_Settings {
|
||||
return $token;
|
||||
}
|
||||
|
||||
private function get_authentication_token( $email, $password ) {
|
||||
$data = array( 'email' => $email, 'password' => $password );
|
||||
$strava_login = WPStrava::get_instance()->api->post( 'authentication/login', $data );
|
||||
|
||||
if( $strava_login ) {
|
||||
if( ! isset( $strava_login->error ) ) {
|
||||
$this->feedback .= __( 'Successfully authenticated.', 'wp-strava' );
|
||||
return $strava_login->token;
|
||||
} else {
|
||||
$this->feedback .= __( 'Authentication failed, please check your credentials.', 'wp-strava' );
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' );
|
||||
return false;
|
||||
}
|
||||
} // get_authentication_token
|
||||
|
||||
|
||||
public function print_options_label() {
|
||||
?><p>Options</p><?php
|
||||
}
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
<?php
|
||||
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Settings.class.php';
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOM.class.php';
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php';
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php';
|
||||
|
||||
class WPStrava {
|
||||
|
||||
private static $instance = NULL;
|
||||
private $settings = NULL;
|
||||
private $api = NULL;
|
||||
|
||||
private function __construct() {
|
||||
$this->settings = new WPStrava_Settings();
|
||||
@@ -17,6 +20,7 @@ class WPStrava {
|
||||
|
||||
// Register StravaLatestRidesWidget widget
|
||||
add_action( 'widgets_init', function() { return register_widget( 'WPStrava_LatestRidesWidget' ); } );
|
||||
add_action( 'widgets_init', function() { return register_widget( 'WPStrava_LatestMapWidget' ); } );
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +34,18 @@ class WPStrava {
|
||||
if ( isset( $this->{$name} ) )
|
||||
return $this->{$name};
|
||||
|
||||
if ( $name == 'api' )
|
||||
return $this->get_api();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
public function get_api() {
|
||||
if ( ! $this->api ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
|
||||
$this->api = new WPStrava_API();
|
||||
}
|
||||
|
||||
return $this->api;
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* Util is a class with all the utility methods.
|
||||
*/
|
||||
class WPStrava_Util {
|
||||
public function makePostRequest ($url, $data) {
|
||||
$data = http_build_query($data);
|
||||
$url = parse_url($url);
|
||||
|
||||
if ($url['scheme'] == "http") {
|
||||
$port = 80;
|
||||
$domain = "tcp://";
|
||||
} elseif ($url['scheme'] == "https") {
|
||||
$port = 443;
|
||||
$domain = "ssl://";
|
||||
} else {
|
||||
$this->feedback .= __('This function only support http and https', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = $url['host'];
|
||||
$path = $url['path'];
|
||||
$protocol = $url['scheme'] . "://";
|
||||
|
||||
// Open a socket connection to the specified port - timeout 30 seconds
|
||||
$fp = fsockopen($domain . $host, $port, $error_number, $error_string, 30);
|
||||
|
||||
if ($fp) {
|
||||
// Build the request headers and data
|
||||
$request = "POST " . $protocol . $host . $path . " HTTP/1.0\r\n";
|
||||
$request .= "Host: " . $host . "\r\n";
|
||||
$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
$request .= "Content-Length: " . strlen($data) . "\r\n";
|
||||
$request .= "Connection: close\r\n\r\n";
|
||||
$request .= $data;
|
||||
|
||||
fputs($fp, $request);
|
||||
|
||||
$result = "";
|
||||
while (!feof($fp)) {
|
||||
$result .= fgets($fp, 128);
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= __('ERROR - ' . $error_string . '-' . $error_number, 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
|
||||
// Split the result header from the content
|
||||
$result = explode("\r\n\r\n", $result, 2);
|
||||
$header = isset($result[0]) ? $result[0] : '';
|
||||
$content = isset($result[1]) ? $result[1] : '';
|
||||
|
||||
return $content;
|
||||
} // makePostRequest
|
||||
} // class Util
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
=== Plugin Name ===
|
||||
Contributors: cmanon (@cmanon), jrfoell
|
||||
Contributors: cmanon, jrfoell
|
||||
Donate link: http://cmanon.com/
|
||||
Tags: bicycle, cycling, strava
|
||||
Requires at least: 2.0
|
||||
|
||||
Reference in New Issue
Block a user