From dfc4ca81c17411475c514acfc81b16187eee7871 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 5 Jul 2013 17:54:12 -0500 Subject: [PATCH 01/15] Reworked error printing for non "200" code responses from Strava --- lib/API.class.php | 1 - lib/LatestMapWidget.class.php | 11 ++++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/API.class.php b/lib/API.class.php index 33631e7..ffea828 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -60,7 +60,6 @@ class WPStrava_API { return $response; if ( $response['response']['code'] != 200 ) { - die($url); //see if there's useful info in the body $body = json_decode( $response['body'] ); $error = ''; diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php index a77ab73..afaa4a9 100644 --- a/lib/LatestMapWidget.class.php +++ b/lib/LatestMapWidget.class.php @@ -64,13 +64,22 @@ class WPStrava_LatestMapWidget extends WP_Widget { if ( $ride_transient ) $ride = $ride_transient; - + if ( ! $ride ) { $strava_rides = WPStrava::get_instance()->rides; $ride_index_params = implode( '&', explode( "\n", $ride_index_params ) ); parse_str( $ride_index_params, $params ); $rides = $strava_rides->getRidesAdvanced( $params ); + if ( is_wp_error( $rides ) ) { + echo $before_widget; + echo '
';
+				print_r($rides);
+				echo '
'; + echo $after_widget; + return; + } + if ( ! empty( $rides ) ) { if ( ! empty( $distance_min ) ) From fde59eab6dcfbf1427399fb8b347d4c5a4b7a4c4 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Wed, 10 Sep 2014 23:35:08 -0500 Subject: [PATCH 02/15] Added new v3 settings --- lib/API.class.php | 41 +++++----- lib/Settings.class.php | 171 ++++++++++++++++++++++++++++++----------- lib/Strava.class.php | 14 ++-- wp-strava.php | 1 + 4 files changed, 159 insertions(+), 68 deletions(-) diff --git a/lib/API.class.php b/lib/API.class.php index ffea828..3506b9c 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -4,29 +4,26 @@ */ 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 + //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/'; - /* - 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 function __construct( $access_token ) { + $this->access_token = $access_token; + } - public function post( $uri, $data = NULL, $version = 2 ) { - $url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API; + public function post( $uri, $data = NULL ) { + $url = self::STRAVA_V3_API; $args = array( 'body' => http_build_query( $data ), + 'sslverify' => false, + 'headers' => array( + 'Authorization' => 'Bearer: ' . $this->access_token, + ) ); - if ( $version == 2 ) - $args['sslverify'] = false; - $response = wp_remote_post( $url . $uri, $args ); if ( $response['response']['code'] != 200 ) { @@ -46,15 +43,21 @@ class WPStrava_API { return json_decode( $response['body'] ); } - public function get( $uri, $args = NULL, $version = 2 ) { - $url = ( $version == 2 ) ? self::STRAVA_V2_API : self::STRAVA_V1_API; + public function get( $uri, $args = NULL ) { + $url = self::STRAVA_V3_API; $url .= $uri; if ( ! empty( $args ) ) $url = add_query_arg( $args, $url ); - $response = wp_remote_get( $url ); + $get_args = array( + 'headers' => array( + 'Authorization' => 'Bearer: ' . $this->access_token, + ) + ); + + $response = wp_remote_get( $url, $get_args ); if ( is_wp_error( $response ) ) return $response; diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 9eee604..09d37fe 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -1,35 +1,90 @@ id = 'settings_page_' . $this->page_name ) { + if ( isset( $_GET['code'] ) ) { + $token = $this->get_token( $_GET['code'] ); + if ( $token ) { + add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' ); + update_option( 'strava_token', $token ); + } else { + add_settings_error( 'strava_token', 'strava_token', $this->feedback ); + } + } + } + } + + /** + * This runs after options are saved + */ + public function option_home() { + if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { + //redirect only if all the right options are in place + $errors = get_settings_errors(); + if ( ! empty( $errors ) ) + return; + + $client_id = get_option( 'strava_client_id' ); + $client_secret = get_option( 'strava_client_secret' ); + + if ( $client_id && $client_secret ) { + $redirect = admin_url( "options-general.php?page={$this->page_name}" ); + $url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force"; + wp_redirect( $url ); + exit(); + } + } } public function add_strava_menu() { add_options_page( __( 'Strava Settings', 'wp-strava' ), __( 'Strava', 'wp-strava' ), 'manage_options', - 'wp-strava-options', + $this->page_name, array( $this, 'print_strava_options' ) ); } public function register_strava_settings() { - register_setting('wp-strava-settings-group','strava_email', array( $this, 'sanitize_email' ) ); - register_setting('wp-strava-settings-group','strava_password', NULL ); - register_setting('wp-strava-settings-group','strava_token', array( $this, 'sanitize_token' ) ); - + $this->token = get_option( 'strava_token' ); + add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed - add_settings_field( 'strava_email', __( 'Strava Email', 'wp-strava' ), array( $this, 'print_email_input' ), 'wp-strava', 'strava_api' ); - add_settings_field( 'strava_password', __( 'Strava Password', 'wp-strava' ), array( $this, 'print_password_input' ), 'wp-strava', 'strava_api' ); - add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' ); + if ( ! $this->token ) { + register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) ); + register_setting( $this->option_page, 'strava_client_secret', array( $this, 'sanitize_client_secret' ) ); - register_setting('wp-strava-settings-group','strava_som', array( $this, 'sanitize_som' ) ); + add_settings_field( 'strava_client_id', __( 'Strava Client ID', 'wp-strava' ), array( $this, 'print_client_input' ), 'wp-strava', 'strava_api' ); + add_settings_field( 'strava_client_secret', __( 'Strava Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' ); + } else { + register_setting( $this->option_page, 'strava_token', array( $this, 'sanitize_token' ) ); + add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' ); + } + + register_setting( $this->option_page, 'strava_som', array( $this, 'sanitize_som' ) ); add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), NULL, 'wp-strava' ); @@ -37,7 +92,23 @@ class WPStrava_Settings { } public function print_api_instructions() { - ?>

Steps:

+
    +
  1. Create your app here: http://www.strava.com/developers
  2. +

    Use the following information:

    +
      +
    • Application Name: [SiteName] Strava
    • +
    • Website: [site_url] +
    • Application Description: WP-Strava for [SiteName] +
    • Authorization Callback Domain: [site_url] + oauth path +
    +
  3. Once you've created your application, enter the Client ID and Client Secret below, which can be found at https://www.strava.com/settings/api
  4. +
  5. You'll be redirected to strava to authorize your app after saving your Client ID and Secret. If successful, your Strava Token will display
  6. +
  7. Erase your Strava Token if you need to re-authorize
  8. +
+ +
- + option_page ); ?>

@@ -57,32 +128,38 @@ class WPStrava_Settings { - -

- get_authentication_token( $this->email, $_POST['strava_password'] ); + /* + if ( isset( $_GET['code'] ) ) { + $token = $this->get_token( $_GET['code'] ); if ( $token ) { add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' ); return $token; @@ -91,28 +168,35 @@ class WPStrava_Settings { return NULL; } } - + */ 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 ); + private function get_token( $code ) { + $client_id = get_option( 'strava_client_id' ); + $client_secret = get_option( 'strava_client_secret' ); - if( $strava_login ) { - if( ! isset( $strava_login->error ) ) { - $this->feedback .= __( 'Successfully authenticated.', 'wp-strava' ); - return $strava_login->token; + if ( $client_id && $client_secret ) { + $data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'code' => $code ); + $strava_info = WPStrava::get_instance()->api->post( 'oauth/token', $data ); + + if( $strava_info ) { + if( isset( $strava_info->access_token ) ) { + $this->feedback .= __( 'Successfully authenticated.', 'wp-strava' ); + return $strava_info->access_token; + } else { + $this->feedback .= __( 'Authentication failed, please check your credentials.', 'wp-strava' ); + return false; + } } else { - $this->feedback .= __( 'Authentication failed, please check your credentials.', 'wp-strava' ); + $this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' ); return false; } } else { - $this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' ); + $this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' ); return false; - } - } // get_authentication_token - + } + } public function print_options_label() { ?>

Options

- + {$name} ) ) - return $this->{$name}; - //on-demand classes if ( $name == 'api' ) return $this->get_api(); @@ -42,13 +41,16 @@ class WPStrava { if ( $name == 'rides' ) return $this->get_rides(); + if ( isset( $this->{$name} ) ) + return $this->{$name}; + return NULL; } public function get_api() { if ( ! $this->api ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php'; - $this->api = new WPStrava_API(); + $this->api = new WPStrava_API( get_option('strava_token') ); } return $this->api; diff --git a/wp-strava.php b/wp-strava.php index 045ab76..fbce144 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -35,6 +35,7 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); +//wp_die( '
' . var_export( $wpstrava->api->get( 'athlete' ), true ) );
 
 //@TODO only load these when needed using is_active_widget()
 function load_styles() {

From 12d529e8acbce9260efb942828aa8adc7436456b Mon Sep 17 00:00:00 2001
From: Justin Foell 
Date: Thu, 11 Sep 2014 09:07:25 -0500
Subject: [PATCH 03/15] Updated instruction wording

---
 lib/Settings.class.php | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/lib/Settings.class.php b/lib/Settings.class.php
index 09d37fe..393a908 100644
--- a/lib/Settings.class.php
+++ b/lib/Settings.class.php
@@ -92,23 +92,26 @@ class WPStrava_Settings {
 	}
 
 	public function print_api_instructions() {
-		?>

Steps:

+ $signup_url = 'http://www.strava.com/developers'; + $settings_url = 'https://www.strava.com/settings/api'; + $blog_name = get_bloginfo( 'name' ); + $app_name = $blog_name . ' Strava'; + $url_parts = parse_url( site_url() ); + $site_url = $url_parts['host']; //strip http/https for copying/pasting into strava + $description = 'WP-Strava for ' . $blog_name; + printf( __( "

Steps:

    -
  1. Create your app here: http://www.strava.com/developers
  2. -

    Use the following information:

    +
  3. Create your API Application here: %s using the following information:
    • -
    • Application Name: [SiteName] Strava
    • -
    • Website: [site_url] -
    • Application Description: WP-Strava for [SiteName] -
    • Authorization Callback Domain: [site_url] + oauth path +
    • Application Name: %s
    • +
    • Website: %s
    • +
    • Application Description: %s
    • +
    • Authorization Callback Domain: %s
    -
  4. Once you've created your application, enter the Client ID and Client Secret below, which can be found at https://www.strava.com/settings/api
  5. -
  6. You'll be redirected to strava to authorize your app after saving your Client ID and Secret. If successful, your Strava Token will display
  7. -
  8. Erase your Strava Token if you need to re-authorize
  9. -
- - Once you've created your API Application at strava.com, enter the Client ID and Client Secret below, which can be found at %s +
  • After saving your Client ID and Secret, you'll be redirected to strava to authorize your API Application. If successful, your Strava Token will display instead of Client ID and Client Secret.
  • +
  • If you need to re-authorize your API Application, erase your Strava Token here and click 'Save Changes' to start over.
  • + ", 'wp-strava' ), $signup_url, $signup_url, $app_name, $site_url, $description, $site_url, $settings_url, $settings_url ); } public function print_strava_options() { From 76cb5f1b22e5784aee66fa44fd1d3adda1f8d167 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Thu, 11 Sep 2014 10:48:03 -0500 Subject: [PATCH 04/15] Fixed latest rides widget and settings workflow --- lib/API.class.php | 4 +- lib/LatestRidesWidget.class.php | 77 +++++++++++---------------------- lib/Rides.class.php | 22 ++++------ lib/Settings.class.php | 37 ++++++++-------- 4 files changed, 55 insertions(+), 85 deletions(-) diff --git a/lib/API.class.php b/lib/API.class.php index 3506b9c..a547a1f 100755 --- a/lib/API.class.php +++ b/lib/API.class.php @@ -20,7 +20,7 @@ class WPStrava_API { 'body' => http_build_query( $data ), 'sslverify' => false, 'headers' => array( - 'Authorization' => 'Bearer: ' . $this->access_token, + 'Authorization' => 'Bearer ' . $this->access_token, ) ); @@ -53,7 +53,7 @@ class WPStrava_API { $get_args = array( 'headers' => array( - 'Authorization' => 'Bearer: ' . $this->access_token, + 'Authorization' => 'Bearer ' . $this->access_token, ) ); diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index e84a4bb..30d4369 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -6,73 +6,58 @@ class WPStrava_LatestRidesWidget extends WP_Widget { public function __construct() { - $widget_ops = array('classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.') ); - parent::__construct('wp-strava', $name = 'Strava Latest Rides', $widget_ops); - wp_enqueue_style('wp-strava'); //TODO only load this when wigit is loaded + $widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.' ) ); + parent::__construct( 'wp-strava', $name = 'Strava Latest Rides', $widget_ops ); + wp_enqueue_style( 'wp-strava' ); //TODO only load this when wigit is loaded } /** @see WP_Widget::widget */ public function widget( $args, $instance ) { - extract($args); + extract( $args ); //$widget_id = $args['widget_id']; - $title = apply_filters('widget_title', empty($instance['title']) ? _e('Rides', 'wp-strava') : $instance['title']); - $strava_search_option = empty($instance['strava_search_option']) ? 'athlete' : $instance['strava_search_option']; - $strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id']; - $quantity = empty($instance['quantity']) ? '5' : $instance['quantity']; + $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Rides', 'wp-strava' ) : $instance['title'] ); + $strava_club_id = empty( $instance['strava_club_id'] ) ? '' : $instance['strava_club_id']; + $quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity']; $this->som = WPStrava_SOM::get_som(); ?> - strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?> + strava_request_handler( $strava_club_id, $strava_som_option, $quantity ); ?>

    - - -

    - -

    - - + +

    - - + +

    @@ -83,41 +68,29 @@ class WPStrava_LatestRidesWidget extends WP_Widget { // 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_search_option, $strava_search_id, $strava_som_option, $quantity ) { + private function strava_request_handler( $strava_club_id, $strava_som_option, $quantity ) { - //Check if the username is empty. - if ( empty( $strava_search_id ) ) - return __("Please configure the Strava search id on the widget options.", "wp-strava"); - //else $strava_rides = WPStrava::get_instance()->rides; - $rides = $strava_rides->getRidesSimple( $strava_search_option, $strava_search_id ); + $rides = $strava_rides->getRides( $quantity, $strava_club_id ); if ( is_wp_error( $rides ) ) return $rides->get_error_message(); - - //adjust quantity - $rides = array_slice( $rides, 0, $quantity ); - $rides_details = $strava_rides->getRidesDetails( $rides ); - if ( is_wp_error( $rides_details ) ) - return $rides_details->get_error_message(); - $response = "

      "; - foreach($rides_details as $ride_obj) { - $ride = $ride_obj->ride; + foreach( $rides as $ride ) { $response .= "
    • "; $response .= "" . $ride->name . ""; $response .= "
      "; $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 ) ); - if ($strava_search_option == "club") { + if ( is_numeric( $strava_club_id ) ) { $response .= " " . $ride->athlete_name . ""; } $response .= sprintf( __(" rode %s %s", "wp-strava"), $this->som->distance( $ride->distance ), $this->som->get_distance_label() ); $response .= sprintf( __( " during %s %s", "wp-strava" ), $this->som->time( $ride->elapsed_time ), $this->som->get_time_label() ); - $response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->elevation_gain ), $this->som->get_elevation_label() ); + $response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->total_elevation_gain ), $this->som->get_elevation_label() ); $response .= "
      "; $response .= "
    • "; } diff --git a/lib/Rides.class.php b/lib/Rides.class.php index d3e145b..174dd44 100755 --- a/lib/Rides.class.php +++ b/lib/Rides.class.php @@ -8,7 +8,7 @@ class WPStrava_Rides { const ATHLETES_URL = "http://app.strava.com/athletes/"; public function getRideDetails( $rideId ) { - return WPStrava::get_instance()->api->get( "rides/{$rideId}" ); + return WPStrava::get_instance()->api->get( "activities/{$rideId}" ); } // getRideDetails public function getRidesDetails( $rides ) { @@ -24,32 +24,26 @@ class WPStrava_Rides { return $rides_details; } // getRidesDetails - public function getRidesSimple( $searchOption, $searchId ) { + public function getRides( $quantity, $club_id = NULL ) { $api = WPStrava::get_instance()->api; $data = NULL; //Get the json results using the constructor specified values. - if ( $searchOption == 'athlete' ) { - if ( is_numeric( $searchId ) ) { - $data = $api->get( 'rides', array( 'athleteId' => $searchId ), 1 ); - } else { - $data = $api->get( 'rides', array( 'athleteName' => $searchId ), 1 ); - } - } elseif ($searchOption == 'club' && is_numeric($searchId)) { - $data = $api->get( 'rides', array( 'clubId' => $searchId ), 1 ); + if ( is_numeric( $club_id ) ) { + $data = $api->get( "clubs/{$club_id}/activities", array( 'per_page' => $quantity ) ); } else { - return new WP_Error( 'wp-strava_options', __("There's an error in your simple options.", 'wp-strava') ); + $data = $api->get( 'athlete/activities', array( 'per_page' => $quantity ) ); } if ( is_wp_error( $data ) ) return $data; - if ( isset( $data->rides ) ) - return $data->rides; + if ( is_array( $data ) ) + return $data; return array(); - } // getRidesSimple + } // getRides public function getRidesAdvanced( $params ) { $data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1 diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 393a908..6cee30b 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -20,24 +20,9 @@ class WPStrava_Settings { public function hook() { add_action( 'admin_init', array( $this, 'register_strava_settings' ) ); add_action( 'admin_menu', array( $this, 'add_strava_menu' ) ); - add_action( 'current_screen', array( $this, 'current_screen' ) ); add_action( 'option_home', array( $this, 'option_home' ) ); } - public function current_screen( $screen ) { - if ( $screen->id = 'settings_page_' . $this->page_name ) { - if ( isset( $_GET['code'] ) ) { - $token = $this->get_token( $_GET['code'] ); - if ( $token ) { - add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' ); - update_option( 'strava_token', $token ); - } else { - add_settings_error( 'strava_token', 'strava_token', $this->feedback ); - } - } - } - } - /** * This runs after options are saved */ @@ -47,9 +32,13 @@ class WPStrava_Settings { $errors = get_settings_errors(); if ( ! empty( $errors ) ) return; + + //clearing to start-over + if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) ) + return; $client_id = get_option( 'strava_client_id' ); - $client_secret = get_option( 'strava_client_secret' ); + $client_secret = get_option( 'strava_client_secret' ); if ( $client_id && $client_secret ) { $redirect = admin_url( "options-general.php?page={$this->page_name}" ); @@ -68,8 +57,22 @@ class WPStrava_Settings { array( $this, 'print_strava_options' ) ); } + public function init() { + if ( isset( $_GET['page'] ) && $_GET['page'] == $this->page_name && isset( $_GET['code'] ) ) { + $token = $this->get_token( $_GET['code'] ); + if ( $token ) { + add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' ); + update_option( 'strava_token', $token ); + } else { + add_settings_error( 'strava_token', 'strava_token', $this->feedback ); + } + } + + $this->token = get_option( 'strava_token' ); + } + public function register_strava_settings() { - $this->token = get_option( 'strava_token' ); + $this->init(); add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed From 1be075356a67c81dfc8cab3fcf98ba78d0f26348 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Thu, 11 Sep 2014 15:56:35 -0500 Subject: [PATCH 05/15] Fixed latest map widget --- lib/LatestMapWidget.class.php | 63 ++++++++++----------------------- lib/LatestRidesWidget.class.php | 2 +- lib/RideShortcode.class.php | 6 ++-- lib/Rides.class.php | 47 +++++------------------- lib/Strava.class.php | 1 + wp-strava.php | 3 +- 6 files changed, 33 insertions(+), 89 deletions(-) diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php index afaa4a9..243acb2 100644 --- a/lib/LatestMapWidget.class.php +++ b/lib/LatestMapWidget.class.php @@ -17,7 +17,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { 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'] ) : ''; + $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'; @@ -27,35 +27,25 @@ class WPStrava_LatestMapWidget extends WP_Widget {

      -

      - - -

      +

      + + +

      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']; + $strava_club_id = empty( $instance['strava_club_id'] ) ? NULL : $instance['strava_club_id']; $build_new = false; //try our transient first @@ -67,9 +57,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { if ( ! $ride ) { $strava_rides = WPStrava::get_instance()->rides; - $ride_index_params = implode( '&', explode( "\n", $ride_index_params ) ); - parse_str( $ride_index_params, $params ); - $rides = $strava_rides->getRidesAdvanced( $params ); + $rides = $strava_rides->getRides( $strava_club_id ); if ( is_wp_error( $rides ) ) { echo $before_widget; @@ -101,47 +89,32 @@ class WPStrava_LatestMapWidget extends WP_Widget { if ( $ride ): echo $before_widget; ?>

      Latest Ride

      - name ?>" href="http://app.strava.com/activities/id ?>">getStaticImage( $ride->id, $build_new ); ?>latlng ); - $full_url = ''; $max_chars = 1865; - - //get the longest usable URL - while ( $url_len + $point_len < $max_chars ) { - $mod = (int) ( $count / $num ); - $points = array(); - for ( $i = 0; $i < $count; $i += $mod ) { - $point = $map_details->latlng[$i]; - $points[] = number_format( $point[0], 4 ) . ',' . number_format( $point[1], 4 ); - } - $url_points = join( '|', $points ); - $point_len = strlen( $url_points ); - if ( $url_len + $point_len < $max_chars ) - $full_url = $url . $url_points; - $num++; - } + if ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) + $url .= $ride->map->polyline; + else + $url .= $ride->map->summary_polyline; - return ""; + return ""; } private function getStaticImage( $ride_id, $build_new ) { $img = get_option( 'strava_latest_map' ); if ( $build_new || ! $img ) { - $map_details = WPStrava::get_instance()->rides->getMapDetails( $ride_id ); - $img = $this->buildImage( $map_details ); + $ride = WPStrava::get_instance()->rides->getRide( $ride_id ); + $img = $this->buildImage( $ride ); update_option( 'strava_latest_map', $img ); } diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index 30d4369..40e8cdd 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -72,7 +72,7 @@ class WPStrava_LatestRidesWidget extends WP_Widget { $strava_rides = WPStrava::get_instance()->rides; - $rides = $strava_rides->getRides( $quantity, $strava_club_id ); + $rides = $strava_rides->getRides( $strava_club_id, $quantity ); if ( is_wp_error( $rides ) ) return $rides->get_error_message(); diff --git a/lib/RideShortcode.class.php b/lib/RideShortcode.class.php index db36f77..7c0eba5 100644 --- a/lib/RideShortcode.class.php +++ b/lib/RideShortcode.class.php @@ -35,8 +35,8 @@ class WPStrava_RideShortcode { $strava_som = get_option('strava_som_option', 'metric'); } - $ride = new Rides(); - $rideDetails = $ride->getRideDetails($id, $strava_som); + $ride = WPStrava::get_instance()->rides; + $rideDetails = $ride->getRide($id); $rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold); if ($strava_som == "metric") { @@ -123,4 +123,4 @@ class WPStrava_RideShortcode { } // Initialize short code -RideShortcode::init(); +WPStrava_RideShortcode::init(); diff --git a/lib/Rides.class.php b/lib/Rides.class.php index 174dd44..c00aef9 100755 --- a/lib/Rides.class.php +++ b/lib/Rides.class.php @@ -7,32 +7,22 @@ class WPStrava_Rides { const RIDES_URL = "http://app.strava.com/rides/"; const ATHLETES_URL = "http://app.strava.com/athletes/"; - public function getRideDetails( $rideId ) { + public function getRide( $rideId ) { return WPStrava::get_instance()->api->get( "activities/{$rideId}" ); } // getRideDetails - - public function getRidesDetails( $rides ) { - $rides_details = array(); - foreach ( $rides as $stravaRide ) { - $detail = $this->getRideDetails( $stravaRide->id ); - - if ( is_wp_error( $detail ) ) - return $detail; - - $rides_details[] = $detail; - } - return $rides_details; - } // getRidesDetails - public function getRides( $quantity, $club_id = NULL ) { + public function getRides( $club_id = NULL, $quantity = NULL ) { $api = WPStrava::get_instance()->api; $data = NULL; + + $args = $quantity ? array( 'per_page' => $quantity ) : NULL; + //Get the json results using the constructor specified values. if ( is_numeric( $club_id ) ) { - $data = $api->get( "clubs/{$club_id}/activities", array( 'per_page' => $quantity ) ); + $data = $api->get( "clubs/{$club_id}/activities", $args ); } else { - $data = $api->get( 'athlete/activities', array( 'per_page' => $quantity ) ); + $data = $api->get( 'athlete/activities', $args ); } if ( is_wp_error( $data ) ) @@ -44,18 +34,6 @@ class WPStrava_Rides { return array(); } // getRides - - public function getRidesAdvanced( $params ) { - $data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1 - - if ( is_wp_error( $data ) ) - return $data; - - if ( isset( $data->rides ) ) - return $data->rides; - - return array(); - } public function getRideMap($rideId, $token, $efforts, $threshold) { if($rideId != 0 AND $token != "") { @@ -81,21 +59,14 @@ class WPStrava_Rides { $meters = $som->distance_inverse( $dist ); $long_rides = array(); - foreach ( $rides as $ride ) { - $ride_info = $this->getRideDetails( $ride->id ); - if ( $ride_info->ride->distance > $meters ) { + foreach ( $rides as $ride_info ) { + if ( $ride_info->distance > $meters ) { $long_rides[] = $ride_info; } } return $long_rides; } - - public function getMapDetails( $ride_id ) { - $token = WPStrava::get_instance()->settings->token; - return WPStrava::get_instance()->api->get( "rides/{$ride_id}/map_details", array( 'token' => $token ) ); - } - } // class Rides ?> diff --git a/lib/Strava.class.php b/lib/Strava.class.php index 16d5b95..ffd1adc 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -4,6 +4,7 @@ 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'; +require_once WPSTRAVA_PLUGIN_DIR . 'lib/RideShortcode.class.php'; class WPStrava { diff --git a/wp-strava.php b/wp-strava.php index fbce144..d8a659d 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ Plugin Name: WP Strava Plugin URI: http://cmanon.com Description: Plugin to show your strava.com information in your wordpress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. -Version: 0.70 +Version: 1.0 Author: Carlos Santa Cruz (cmanon), Justin Foell Author URI: http://cmanon.com License: GPL2 @@ -35,7 +35,6 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); -//wp_die( '
      ' . var_export( $wpstrava->api->get( 'athlete' ), true ) );
       
       //@TODO only load these when needed using is_active_widget()
       function load_styles() {
      
      From 140c04fd24174ae4cd49b10019298af00d82305e Mon Sep 17 00:00:00 2001
      From: Justin Foell 
      Date: Fri, 12 Sep 2014 10:37:34 -0500
      Subject: [PATCH 06/15] Formatting
      
      ---
       lib/RideShortcode.class.php | 2 +-
       lib/Settings.class.php      | 4 ++--
       2 files changed, 3 insertions(+), 3 deletions(-)
      
      diff --git a/lib/RideShortcode.class.php b/lib/RideShortcode.class.php
      index 7c0eba5..e908b9f 100644
      --- a/lib/RideShortcode.class.php
      +++ b/lib/RideShortcode.class.php
      @@ -100,7 +100,7 @@ class WPStrava_RideShortcode {
       				";
       			}
       		} else {
      -			return _e('Please first get your strava token using the settings wp strava page.', 'wp-strava');
      +			return __('Please first get your strava token using the settings wp strava page.', 'wp-strava');
       		}
       	} // handler
       
      diff --git a/lib/Settings.class.php b/lib/Settings.class.php
      index 6cee30b..74f958f 100644
      --- a/lib/Settings.class.php
      +++ b/lib/Settings.class.php
      @@ -29,8 +29,8 @@ class WPStrava_Settings {
       	public function option_home() {
       		if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) {
       			//redirect only if all the right options are in place
      -			$errors = get_settings_errors();
      -			if ( ! empty( $errors ) )
      +			global $wp_settings_errors;
      +			if ( ! empty( $wp_settings_errors ) )
       				return;
       
       			//clearing to start-over
      
      From 5ae8a9bee1a14316692059bf7590c02d297519ae Mon Sep 17 00:00:00 2001
      From: Justin Foell 
      Date: Fri, 12 Sep 2014 10:40:02 -0500
      Subject: [PATCH 07/15] Renamed option_home hook function
      
      ---
       lib/Settings.class.php | 4 ++--
       1 file changed, 2 insertions(+), 2 deletions(-)
      
      diff --git a/lib/Settings.class.php b/lib/Settings.class.php
      index 74f958f..9374e1c 100644
      --- a/lib/Settings.class.php
      +++ b/lib/Settings.class.php
      @@ -20,13 +20,13 @@ class WPStrava_Settings {
       	public function hook() {
       		add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
       		add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
      -		add_action( 'option_home', array( $this, 'option_home' ) );
      +		add_action( 'option_home', array( $this, 'maybe_oauth' ) );
       	}
       
       	/**
       	 * This runs after options are saved
       	 */
      -	public function option_home() {
      +	public function maybe_oauth() {
       		if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) {
       			//redirect only if all the right options are in place
       			global $wp_settings_errors;
      
      From bc36c9c7cc83e95078b064b31bd016b571cf9b64 Mon Sep 17 00:00:00 2001
      From: Justin Foell 
      Date: Fri, 12 Sep 2014 12:28:53 -0500
      Subject: [PATCH 08/15] Updated settings workflow
      
      ---
       lib/Settings.class.php | 80 ++++++++++++++++++++----------------------
       1 file changed, 39 insertions(+), 41 deletions(-)
      
      diff --git a/lib/Settings.class.php b/lib/Settings.class.php
      index 9374e1c..d6e4b07 100644
      --- a/lib/Settings.class.php
      +++ b/lib/Settings.class.php
      @@ -5,7 +5,7 @@
        * 
        * Set up an "API Application" at Strava
        * Save the Client ID and Client Secret in WordPress - redirect to strava oauth/authorize URL for permission
      - * Get redirected back to this settings page with ?code=
      + * Get redirected back to this settings page with ?code= or ?error=
        * Use code to retrieve auth token
        */
       
      @@ -20,33 +20,39 @@ class WPStrava_Settings {
       	public function hook() {
       		add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
       		add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
      -		add_action( 'option_home', array( $this, 'maybe_oauth' ) );
      +		add_filter( 'pre_set_transient_settings_errors', array( $this, 'maybe_oauth' ), 10 );
      +		//for process debugging
      +		//add_action( 'all', array( $this, 'hook_debug' ) );
      +		//add_filter( 'all', array( $this, 'hook_debug' ) );
      +	}
      +
      +	public function hook_debug( $name ) {
      +		echo "\n";
       	}
       
       	/**
       	 * This runs after options are saved
       	 */
      -	public function maybe_oauth() {
      -		if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) {
      -			//redirect only if all the right options are in place
      -			global $wp_settings_errors;
      -			if ( ! empty( $wp_settings_errors ) )
      -				return;
      -
      -			//clearing to start-over
      -			if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
      -				return;
      +	public function maybe_oauth( $value ) {
      +		//redirect only if all the right options are in place
      +		if ( isset( $value[0]['type'] ) && $value[0]['type'] == 'updated' ) { //make sure there were no settings errors
      +			if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { //make sure we're on our settings page
      +				//user is clearing to start-over, don't oauth
      +				if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
      +					return;
       			
      -			$client_id = get_option( 'strava_client_id' );
      -			$client_secret = get_option( 'strava_client_secret' );			
      +				$client_id = get_option( 'strava_client_id' );
      +				$client_secret = get_option( 'strava_client_secret' );
       
      -			if ( $client_id && $client_secret ) {
      -				$redirect = admin_url( "options-general.php?page={$this->page_name}" );
      -				$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
      -				wp_redirect( $url );
      -				exit();
      -			}			
      +				if ( $client_id && $client_secret ) {
      +					$redirect = admin_url( "options-general.php?page={$this->page_name}" );
      +					$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
      +					wp_redirect( $url );
      +					exit();
      +				}
      +			}
       		}
      +		return $value;
       	}
       	
       	public function add_strava_menu() {
      @@ -58,13 +64,18 @@ class WPStrava_Settings {
       	}
       
       	public function init() {
      -		if ( isset( $_GET['page'] ) && $_GET['page'] == $this->page_name && isset( $_GET['code'] ) ) {			
      -			$token = $this->get_token( $_GET['code'] );
      -			if ( $token ) {
      -				add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
      -				update_option( 'strava_token', $token );
      -			} else {
      -				add_settings_error( 'strava_token', 'strava_token', $this->feedback );
      +		//only update when redirected back from strava
      +		if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] == $this->page_name ) {
      +			if ( isset( $_GET['code'] ) ) {
      +				$token = $this->get_token( $_GET['code'] );
      +				if ( $token ) {
      +					add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
      +					update_option( 'strava_token', $token );
      +				} else {
      +					add_settings_error( 'strava_token', 'strava_token', $this->feedback );
      +				}
      +			} else if ( isset( $_GET['error'] ) ) {
      +				add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'Error authenticating at Strava: %s', 'wp-strava' ), str_replace( '_', ' ', $_GET['error'] ) ) );
       			}
       		}
       		
      @@ -158,23 +169,10 @@ class WPStrava_Settings {
       		if ( trim( $client_secret ) == '' ) {
       			add_settings_error( 'strava_client_secret', 'strava_client_secret', __( 'Client Secret is required.', 'wp-strava' ) );
       		}
      -		echo "WHEREAMI";
       		return $client_secret;
       	}
       
       	public function sanitize_token( $token ) {
      -		/*
      -		if ( isset( $_GET['code'] ) ) {
      -			$token = $this->get_token( $_GET['code'] );
      -			if ( $token ) {
      -				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', $this->feedback );
      -				return NULL;
      -			}
      -		}
      -		*/
       		return $token;
       	}
       
      @@ -195,7 +193,7 @@ class WPStrava_Settings {
       					return false;
       				}
       			} else {
      -				$this->feedback .= __( 'There was an error pulling data of strava.com.', 'wp-strava' );
      +				$this->feedback .= __( sprintf( 'There was an error receiving data from Strava: %s', print_r( $strava_info, true ) ), 'wp-strava' );
       				return false;
       			}
       		} else {
      
      From 7992db37f098dac352d0be0fff79a6688154a22f Mon Sep 17 00:00:00 2001
      From: Justin Foell 
      Date: Tue, 23 Sep 2014 13:39:48 -0500
      Subject: [PATCH 09/15] Updated version Addd handling of 503 service unavail
      
      ---
       lib/API.class.php             |  2 ++
       lib/LatestMapWidget.class.php | 10 +++++++---
       lib/Rides.class.php           |  2 +-
       lib/Settings.class.php        |  7 +++++++
       readme.txt                    | 17 +++++++++++------
       wp-strava.php                 |  9 ++++-----
       6 files changed, 32 insertions(+), 15 deletions(-)
      
      diff --git a/lib/API.class.php b/lib/API.class.php
      index a547a1f..44dd657 100755
      --- a/lib/API.class.php
      +++ b/lib/API.class.php
      @@ -68,6 +68,8 @@ class WPStrava_API {
       			$error = '';
       			if ( ! empty( $body->error ) )
       				$error = $body->error;
      +			else if ( $response['response']['code'] == 503 )
      +				$error = __( 'Strava Temporarily Unavailable', 'wp-strava' );
       			else
       				$error = print_r( $response, true );
       
      diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php
      index 243acb2..ce86ff9 100644
      --- a/lib/LatestMapWidget.class.php
      +++ b/lib/LatestMapWidget.class.php
      @@ -61,9 +61,13 @@ class WPStrava_LatestMapWidget extends WP_Widget {
       
       			if ( is_wp_error( $rides ) ) {
       				echo $before_widget;
      -				echo '
      ';
      -				print_r($rides);
      -				echo '
      '; + if ( WPSTRAVA_DEBUG ) { + echo '
      ';
      +					print_r($rides);
      +					echo '
      '; + } else { + echo $rides->get_error_message(); + } echo $after_widget; return; } diff --git a/lib/Rides.class.php b/lib/Rides.class.php index c00aef9..309de3a 100755 --- a/lib/Rides.class.php +++ b/lib/Rides.class.php @@ -45,7 +45,7 @@ class WPStrava_Rides { //return $map_details; return $json; } else { - $this->feedback .= _e("There was an error pulling data of strava.com.", "wp-strava"); + $this->feedback .= _e("There was an error pulling data from strava.com.", "wp-strava"); return false; } } else { diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 74f958f..698116c 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -21,6 +21,7 @@ class WPStrava_Settings { add_action( 'admin_init', array( $this, 'register_strava_settings' ) ); add_action( 'admin_menu', array( $this, 'add_strava_menu' ) ); add_action( 'option_home', array( $this, 'option_home' ) ); + add_filter( 'plugin_action_links_' . WPSTRAVA_PLUGIN_NAME, array( $this, 'settings_link' ) ); } /** @@ -225,5 +226,11 @@ class WPStrava_Settings { public function __get( $name ) { return get_option( "strava_{$name}" ); } + + public function settings_link( $links ) { + $settings_link = 'page_name}" ) . '">' . __( 'Settings' ) . ''; + $links[] = $settings_link; + return $links; + } } \ No newline at end of file diff --git a/readme.txt b/readme.txt index c2c68d8..c4bebca 100755 --- a/readme.txt +++ b/readme.txt @@ -1,20 +1,25 @@ === Plugin Name === Contributors: cmanon, jrfoell Donate link: http://cmanon.com/ -Tags: bicycle, cycling, strava -Requires at least: 2.0 -Tested up to: 3.5.1 -Stable tag: 0.62 +Tags: strava, bicycle, cycling, biking, running, run, swimming, swim, gps, shortcode, widget, plugin +Requires at least: 3.0 +Tested up to: 4.0 +Stable tag: 1.0 License: GPLv2 or later -This plugin is intended to show your strava.com information in your WordPress site. +Show your Strava activity on your WordPress site. == Description == -This plugin uses the REST strava.com API to pull the data out and show the information in your WordPress site. +This plugin uses the Strava V3 API to embed maps and activity for +athletes and clubs on your WordPress site. Included are several +widgets and shortcodes for showing maps and activity summaries. == Changelog == += 1.0 = +Change to Strava API V3 + = 0.70 = Use WordPress HTTP API for all remote calls Use WordPress Settings API for settings page diff --git a/wp-strava.php b/wp-strava.php index d8a659d..60927b0 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -27,6 +27,8 @@ License: GPL2 define( 'WPSTRAVA_PLUGIN_DIR', trailingslashit( dirname( __FILE__) ) ); define( 'WPSTRAVA_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); +define( 'WPSTRAVA_PLUGIN_NAME', plugin_basename(__FILE__) ); +define( 'WPSTRAVA_DEBUG', false ); // Load the multilingual support. if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { @@ -37,16 +39,13 @@ require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); //@TODO only load these when needed using is_active_widget() -function load_styles() { +function wpstrava_load_scripts_and_styles() { // Register a personalized stylesheet wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); wp_enqueue_style('wp-strava'); -} -add_action('wp_enqueue_script', 'load_styles'); -function load_scripts() { // Load required javascript libraries wp_enqueue_script('jquery'); //wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); } -add_action('wp-enqueue_script', 'load_scripts'); +add_action('wp_enqueue_script', 'wpstrava_load_scripts_and_styles'); From 4ac9a8aebfb242f5bdb0402e6ca50cadff3e3d95 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 1 Nov 2014 22:53:32 -0500 Subject: [PATCH 10/15] Updated to the [ride] shortcode --- css/wp-strava.css | 4 +- lib/LatestMapWidget.class.php | 14 +--- lib/RideShortcode.class.php | 147 +++++++++++++--------------------- lib/Rides.class.php | 19 ----- lib/SOM.class.php | 8 +- lib/StaticMap.class.php | 18 +++++ lib/Strava.class.php | 1 + 7 files changed, 83 insertions(+), 128 deletions(-) create mode 100644 lib/StaticMap.class.php diff --git a/css/wp-strava.css b/css/wp-strava.css index 86cae73..fad25b8 100755 --- a/css/wp-strava.css +++ b/css/wp-strava.css @@ -8,7 +8,7 @@ background-position: -224px -160px; margin: 0 6px 2px 0; } -.table { +.wp-strava-ride-container { display: table; margin: 0 auto; } @@ -32,6 +32,6 @@ .ride-details-table-units { font-size: 0.8em; } -.map img { +.wp-strava-ride-container img { max-width: none; } diff --git a/lib/LatestMapWidget.class.php b/lib/LatestMapWidget.class.php index ce86ff9..ec40b16 100644 --- a/lib/LatestMapWidget.class.php +++ b/lib/LatestMapWidget.class.php @@ -100,25 +100,13 @@ class WPStrava_LatestMapWidget extends WP_Widget { endif; } - private function buildImage( $ride ) { - $url = 'http://maps.google.com/maps/api/staticmap?maptype=terrain&size=390x260&sensor=false&path=color:0xFF0000BF|weight:2|enc:'; - $url_len = strlen( $url ); - $max_chars = 1865; - - if ( $url_len + strlen( $ride->map->polyline ) < $max_chars ) - $url .= $ride->map->polyline; - else - $url .= $ride->map->summary_polyline; - - return ""; - } 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 = $this->buildImage( $ride ); + $img = WPStrava_StaticMap::get_image_tag( $ride ); update_option( 'strava_latest_map', $img ); } diff --git a/lib/RideShortcode.class.php b/lib/RideShortcode.class.php index e908b9f..5c755da 100644 --- a/lib/RideShortcode.class.php +++ b/lib/RideShortcode.class.php @@ -15,109 +15,76 @@ class WPStrava_RideShortcode { function handler($atts) { self::$add_script = true; - $token = get_option('strava_token'); + $defaults = array( + 'id' => 0, + 'som' => WPStrava::get_instance()->settings->som, + 'map_width' => '480', + 'map_height' => '320', + ); + + extract(shortcode_atts($defaults, $atts)); - if($token) { - $pairs = array( - 'id' => 0, - 'som' => "metric", - 'efforts' => false, - 'threshold' => 0, - 'map_width' => "100%", - 'map_height' => "400px" - ); - - extract(shortcode_atts($pairs, $atts)); + $strava_som = WPStrava_SOM::get_som( $som ); + $strava_ride = WPStrava::get_instance()->rides; + $rideDetails = $strava_ride->getRide( $id ); - if (isset($som)) { - $strava_som = $som; - } else { - $strava_som = get_option('strava_som_option', 'metric'); - } - - $ride = WPStrava::get_instance()->rides; - $rideDetails = $ride->getRide($id); - $rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold); - - if ($strava_som == "metric") { - $units = array( - 'elapsedTime' => __('hours','wp-strava'), - 'movingTime' => __('hours','wp-strava'), - 'distance' => __('km','wp-strava'), - 'averageSpeed' => __('km/h','wp-strava'), - //'maximumSpeed' => __('km/h','wp-strava'), - 'elevationGain' => __('meters','wp-strava') - ); - } elseif ($strava_som == "english") { - $units = array( - 'elapsedTime' => __('hours','wp-strava'), - 'movingTime' => __('hours','wp-strava'), - 'distance' => __('miles','wp-strava'), - 'averageSpeed' => __('miles/h','wp-strava'), - //'maximumSpeed' => __('miles/h','wp-strava'), - 'elevationGain' => __('feet','wp-strava') - ); - } - - if($rideCoordinates) { - return " -
      - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Elapsed TimeMoving TimeDistanceAverage SpeedElevation Gain
      {$rideDetails['elapsedTime']}{$rideDetails['movingTime']}{$rideDetails['distance']}{$rideDetails['averageSpeed']}{$rideDetails['elevationGain']}
      {$units['elapsedTime']}{$units['movingTime']}{$units['distance']}{$units['averageSpeed']}{$units['elevationGain']}
      -
      -
      - - "; - } - } else { - return __('Please first get your strava token using the settings wp strava page.', 'wp-strava'); + //sanitize width & height + $map_width = str_replace( '%', '', $map_width ); + $map_height = str_replace( '%', '', $map_height ); + $map_width = str_replace( 'px', '', $map_width ); + $map_height = str_replace( 'px', '', $map_height ); + + if( $rideDetails ) { + return ' +
      + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      ' . __( 'Elapsed Time', 'wp-strava' ) . '' . __( 'Moving Time', 'wp-strava' ) . '' . __( 'Distance', 'wp-strava' ) . '' . __( 'Average Speed', 'wp-strava' ) . '' . __( 'Max Speed', 'wp-strava' ) . '' . __( 'Elevation Gain', 'wp-strava' ) . '
      ' . $strava_som->time( $rideDetails->elapsed_time ) . '' . $strava_som->time( $rideDetails->moving_time ) . '' . $strava_som->distance( $rideDetails->distance ) . '' . $strava_som->speed( $rideDetails->average_speed ) . '' . $strava_som->speed( $rideDetails->max_speed ) . '' . $strava_som->elevation( $rideDetails->total_elevation_gain ) . '
      ' . $strava_som->get_time_label() . '' . $strava_som->get_time_label() . '' . $strava_som->get_distance_label() . '' . $strava_som->get_speed_label() . '' . $strava_som->get_speed_label() . '' . $strava_som->get_elevation_label() . '
      ' . + WPStrava_StaticMap::get_image_tag( $rideDetails, $map_height, $map_width ) . + '
      '; } } // handler static function registerScripts() { - wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__)); + wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); - wp_register_script('wp-strava-script', plugins_url('js/wp-strava.js', __FILE__), array('jquery'), '1.0', true); - wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); + //wp_register_script('wp-strava-script', WPSTRAVA_PLUGIN_URL . 'js/wp-strava.js', array( 'jquery' ), '1.0', true); + //wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); } static function printScripts() { if (self::$add_script) { wp_enqueue_style('wp-strava-style'); - wp_enqueue_script('jquery'); - wp_print_scripts('google-maps'); - wp_print_scripts('wp-strava-script'); + //wp_print_scripts('google-maps'); + //wp_print_scripts('wp-strava-script'); } } } diff --git a/lib/Rides.class.php b/lib/Rides.class.php index 309de3a..925f24c 100755 --- a/lib/Rides.class.php +++ b/lib/Rides.class.php @@ -34,25 +34,6 @@ class WPStrava_Rides { return array(); } // getRides - - public function getRideMap($rideId, $token, $efforts, $threshold) { - if($rideId != 0 AND $token != "") { - $url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2); - $json = file_get_contents($url . '?token=' . $token . '&threshold=' . $threshold); - - if($json) { - //$map_details = json_decode($json); - //return $map_details; - return $json; - } else { - $this->feedback .= _e("There was an error pulling data from strava.com.", "wp-strava"); - return false; - } - } else { - $this->feedback .= _e("You need to provide both parameters to complete the call.", "wp-strava"); - return false; - } - } // getRideDetails public function getRidesLongerThan( $rides, $dist ) { $som = WPStrava_SOM::get_som(); diff --git a/lib/SOM.class.php b/lib/SOM.class.php index 7340d76..4bd8ee0 100644 --- a/lib/SOM.class.php +++ b/lib/SOM.class.php @@ -4,12 +4,12 @@ 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 { + if ( $som == 'english' ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMEnglish.class.php'; return new WPStrava_SOMEnglish(); + } else { //default to metric + require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMMetric.class.php'; + return new WPStrava_SOMMetric(); } } diff --git a/lib/StaticMap.class.php b/lib/StaticMap.class.php new file mode 100644 index 0000000..8a3be0b --- /dev/null +++ b/lib/StaticMap.class.php @@ -0,0 +1,18 @@ +map->polyline ) < $max_chars ) + $url .= $ride->map->polyline; + else + $url .= $ride->map->summary_polyline; + + return ""; + } + +} \ No newline at end of file diff --git a/lib/Strava.class.php b/lib/Strava.class.php index ffd1adc..86589be 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -5,6 +5,7 @@ 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'; require_once WPSTRAVA_PLUGIN_DIR . 'lib/RideShortcode.class.php'; +require_once WPSTRAVA_PLUGIN_DIR . 'lib/StaticMap.class.php'; class WPStrava { From 18d8897ee970f75f03071dcb7f8630aab599734f Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sat, 1 Nov 2014 23:07:03 -0500 Subject: [PATCH 11/15] Updated documentation Set version to alpha --- readme.txt | 17 +++++++++++++++++ wp-strava.php | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index c4bebca..d42fe9e 100755 --- a/readme.txt +++ b/readme.txt @@ -15,10 +15,27 @@ This plugin uses the Strava V3 API to embed maps and activity for athletes and clubs on your WordPress site. Included are several widgets and shortcodes for showing maps and activity summaries. += Shortcodes = + +[ride id=NUMBER] - add to any page or post. Also takes the following +optional parameters: + +* som - english/metric (system of measure - override from default setting) +* map_width - width (width of image in pixels) +* map_height - height (height of image in pixels) + += Widgets = + +Strava Latest Rides - shows a list of the last few activities + +Strava Latest Map - shows map of latest activity with option to limit +latest map to activities of a certain minimum distance + == Changelog == = 1.0 = Change to Strava API V3 +Switch ride shortcode to use static map = 0.70 = Use WordPress HTTP API for all remote calls diff --git a/wp-strava.php b/wp-strava.php index 60927b0..954f67a 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ Plugin Name: WP Strava Plugin URI: http://cmanon.com Description: Plugin to show your strava.com information in your wordpress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. -Version: 1.0 +Version: 1.0a Author: Carlos Santa Cruz (cmanon), Justin Foell Author URI: http://cmanon.com License: GPL2 From 353ee5ecb0ea253bf39c467a07f4a9c3e13dcc20 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Tue, 4 Nov 2014 21:09:12 -0600 Subject: [PATCH 12/15] Register styles in main plugin Enqueue styles only if needed --- lib/LatestRidesWidget.class.php | 8 +++++++- lib/RideShortcode.class.php | 11 +---------- lib/Settings.class.php | 4 ---- lib/Strava.class.php | 7 +++++++ wp-strava.php | 12 ------------ 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index 40e8cdd..a4f50b7 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -8,7 +8,13 @@ class WPStrava_LatestRidesWidget extends WP_Widget { public function __construct() { $widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.' ) ); parent::__construct( 'wp-strava', $name = 'Strava Latest Rides', $widget_ops ); - wp_enqueue_style( 'wp-strava' ); //TODO only load this when wigit is loaded + add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) ); + } + + public function maybe_enqueue() { + if ( is_active_widget( false, false, $this->id_base ) ) { + wp_enqueue_style( 'wp-strava-style' ); //only load this when wigit is loaded + } } /** @see WP_Widget::widget */ diff --git a/lib/RideShortcode.class.php b/lib/RideShortcode.class.php index 5c755da..8113310 100644 --- a/lib/RideShortcode.class.php +++ b/lib/RideShortcode.class.php @@ -5,13 +5,11 @@ class WPStrava_RideShortcode { static function init() { add_shortcode('ride', array(__CLASS__, 'handler')); - - add_action('init', array(__CLASS__, 'registerScripts')); add_action('wp_footer', array(__CLASS__, 'printScripts')); } // Shortcode handler function - // [ride id=id som=metric efforts=false threshold=5 map-width="100%" map-height="400px"] tag + // [ride id=id som=metric map_width="100%" map_height="400px"] function handler($atts) { self::$add_script = true; @@ -72,13 +70,6 @@ class WPStrava_RideShortcode { } } // handler - static function registerScripts() { - wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); - - //wp_register_script('wp-strava-script', WPSTRAVA_PLUGIN_URL . 'js/wp-strava.js', array( 'jquery' ), '1.0', true); - //wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); - } - static function printScripts() { if (self::$add_script) { wp_enqueue_style('wp-strava-style'); diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 5ff7f55..f1d7a42 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -203,10 +203,6 @@ class WPStrava_Settings { } } - public function print_options_label() { - ?>

      Options

      diff --git a/lib/Strava.class.php b/lib/Strava.class.php index 86589be..cc807f4 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -19,6 +19,8 @@ class WPStrava { if ( is_admin() ) { $this->settings->hook(); + } else { + add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) ); } // Register StravaLatestRidesWidget widget @@ -66,4 +68,9 @@ class WPStrava { return $this->rides; } + + public function register_scripts() { + // Register a personalized stylesheet + wp_register_style( 'wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); + } } \ No newline at end of file diff --git a/wp-strava.php b/wp-strava.php index 954f67a..f0546d3 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -37,15 +37,3 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; $wpstrava = WPStrava::get_instance(); - -//@TODO only load these when needed using is_active_widget() -function wpstrava_load_scripts_and_styles() { - // Register a personalized stylesheet - wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); - wp_enqueue_style('wp-strava'); - - // Load required javascript libraries - wp_enqueue_script('jquery'); - //wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); -} -add_action('wp_enqueue_script', 'wpstrava_load_scripts_and_styles'); From 3f2b1917c88057cc54035d76bb115c26d91eb1ab Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Tue, 25 Nov 2014 14:58:39 -0600 Subject: [PATCH 13/15] Replaced PHP 5.3 only wigits_init --- lib/Strava.class.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Strava.class.php b/lib/Strava.class.php index cc807f4..0cb1b2d 100644 --- a/lib/Strava.class.php +++ b/lib/Strava.class.php @@ -24,9 +24,8 @@ 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' ); } ); - + add_action( 'widgets_init', create_function('', 'return register_widget( "WPStrava_LatestRidesWidget" );') ); + add_action( 'widgets_init', create_function('', 'return register_widget( "WPStrava_LatestMapWidget" );' ) ); } public static function get_instance() { From b05c64ad954d1e016113c3fdbcc940bd0d29ba7a Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Mon, 1 Dec 2014 15:17:13 -0600 Subject: [PATCH 14/15] Ignore svn dir --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b25c15b..e0c38e3 100755 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +.svn From 6f6ed94329e3b59f2695f2edf617abea949d11cf Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Tue, 9 Dec 2014 21:49:47 -0600 Subject: [PATCH 15/15] Updated version to 1.0 --- wp-strava.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-strava.php b/wp-strava.php index f0546d3..372f8cb 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ Plugin Name: WP Strava Plugin URI: http://cmanon.com Description: Plugin to show your strava.com information in your wordpress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. -Version: 1.0a +Version: 1.0 Author: Carlos Santa Cruz (cmanon), Justin Foell Author URI: http://cmanon.com License: GPL2