From 414e2648bb96d4b8f04153e784ed15a5b0088d0b Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sun, 31 Mar 2013 21:28:49 -0500 Subject: [PATCH] Renamed Util to API Added SOM classes for English & Metric Use WP HTTP API for requests --- lib/API.class.php | 57 ++++++++++++++++ lib/LatestMapWidget.class.php | 122 ++++++++++++++++++++++++++++++++++ lib/Rides.class.php | 39 +---------- lib/SOM.class.php | 25 +++++++ lib/SOMEnglish.class.php | 48 +++++++++++++ lib/SOMMetric.class.php | 46 +++++++++++++ lib/Settings.class.php | 30 +++++++-- lib/Strava.class.php | 16 +++++ lib/Util.class.php | 57 ---------------- readme.txt | 2 +- 10 files changed, 341 insertions(+), 101 deletions(-) create mode 100755 lib/API.class.php create mode 100644 lib/LatestMapWidget.class.php create mode 100644 lib/SOM.class.php create mode 100644 lib/SOMEnglish.class.php create mode 100644 lib/SOMMetric.class.php delete mode 100755 lib/Util.class.php diff --git a/lib/API.class.php b/lib/API.class.php new file mode 100755 index 0000000..d35f6c9 --- /dev/null +++ b/lib/API.class.php @@ -0,0 +1,57 @@ + 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 \ No newline at end of file diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php new file mode 100644 index 0000000..d54aa64 --- /dev/null +++ b/lib/LatestMapWidget.class.php @@ -0,0 +1,122 @@ +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'; + + ?> +

+ + +

+

+ + +

+ 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 ""; + /* + echo '
';
+			print_r($map_deets);
+			echo '
'; + */ + 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}" ); + } +} \ No newline at end of file diff --git a/lib/Rides.class.php b/lib/Rides.class.php index 019e3ac..a31aea0 100755 --- a/lib/Rides.class.php +++ b/lib/Rides.class.php @@ -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); diff --git a/lib/SOM.class.php b/lib/SOM.class.php new file mode 100644 index 0000000..e114a33 --- /dev/null +++ b/lib/SOM.class.php @@ -0,0 +1,25 @@ +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(); + +} \ No newline at end of file diff --git a/lib/SOMEnglish.class.php b/lib/SOMEnglish.class.php new file mode 100644 index 0000000..ee9ee8f --- /dev/null +++ b/lib/SOMEnglish.class.php @@ -0,0 +1,48 @@ +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() { ?>

Options

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; + } } \ No newline at end of file diff --git a/lib/Util.class.php b/lib/Util.class.php deleted file mode 100755 index 462ddf9..0000000 --- a/lib/Util.class.php +++ /dev/null @@ -1,57 +0,0 @@ -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 \ No newline at end of file diff --git a/readme.txt b/readme.txt index 75afa85..f9381f4 100755 --- a/readme.txt +++ b/readme.txt @@ -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