From c8031dd6782195679139a331511b640403f0b1df Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sun, 3 Feb 2013 20:38:55 -0600 Subject: [PATCH 1/3] Compartmentalized classes Restructured settings to use the WP Settings API --- .gitignore | 1 + lib/LatestRidesWidget.class.php | 90 ++++++ lib/RideShortcode.class.php | 126 +++++++++ rides.class.php => lib/Rides.class.php | 8 +- lib/Settings.class.php | 92 +++++++ lib/Strava.class.php | 82 ++++++ util.class.php => lib/Util.class.php | 5 +- wp-strava.php | 365 +------------------------ 8 files changed, 411 insertions(+), 358 deletions(-) create mode 100644 lib/LatestRidesWidget.class.php create mode 100644 lib/RideShortcode.class.php rename rides.class.php => lib/Rides.class.php (96%) create mode 100644 lib/Settings.class.php create mode 100644 lib/Strava.class.php rename util.class.php => lib/Util.class.php (94%) diff --git a/.gitignore b/.gitignore index e69de29..b25c15b 100755 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php new file mode 100644 index 0000000..a7a2033 --- /dev/null +++ b/lib/LatestRidesWidget.class.php @@ -0,0 +1,90 @@ + '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'); + } + + /** @see WP_Widget::widget */ + function widget($args, $instance) { + 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_som_option = empty($instance['strava_som_option']) ? 'metric' : $instance['strava_som_option']; + $strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id']; + $quantity = empty($instance['quantity']) ? '5' : $instance['quantity']; + + ?> + + + + + +

+ + +

+

+ + +

+

+ + +

+

+ + +

+

+ + +

+ 0, + 'som' => "metric", + 'efforts' => false, + 'threshold' => 0, + 'map_width' => "100%", + 'map_height' => "400px" + ); + + extract(shortcode_atts($pairs, $atts)); + + if (isset($som)) { + $strava_som = $som; + } else { + $strava_som = get_option('strava_som_option', 'metric'); + } + + $ride = new Rides(); + $rideDetails = $ride->getRideDetails($id, $strava_som); + $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 _e('Please first get your strava token using the settings wp strava page.', 'wp-strava'); + } + } // handler + + static function registerScripts() { + wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__)); + + 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'); + } + + 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'); + } + } +} + +// Initialize short code +RideShortcode::init(); diff --git a/rides.class.php b/lib/Rides.class.php similarity index 96% rename from rides.class.php rename to lib/Rides.class.php index 4a1b583..019e3ac 100755 --- a/rides.class.php +++ b/lib/Rides.class.php @@ -1,9 +1,8 @@ $email, 'password' => $password); $json = $util->makePostRequest($this->authenticationUrlV2, $data); diff --git a/lib/Settings.class.php b/lib/Settings.class.php new file mode 100644 index 0000000..8328a7e --- /dev/null +++ b/lib/Settings.class.php @@ -0,0 +1,92 @@ + +
+

+

+

+ +
+ + + +

+ +

+
+
+ + +

+ getAuthenticationToken( $email, $_POST['strava_password'] ); + if ( $token ) { + add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $ride->feedback ) , 'updated' ); + return $token; + } else { + add_settings_error( 'strava_token', 'strava_token', $ride->feedback ); + return NULL; + } + } + + return $token; + } + +} + diff --git a/lib/Strava.class.php b/lib/Strava.class.php new file mode 100644 index 0000000..3b5c2e3 --- /dev/null +++ b/lib/Strava.class.php @@ -0,0 +1,82 @@ +settings = new WPStrava_Settings(); + + if ( is_admin() ) { + $this->settings->hook(); + } + + // Register StravaLatestRidesWidget widget + add_action('widgets_init', function() { return register_widget('WPStrava_LatestRidesWidget'); }); + + } + + public static function getInstance() { + if ( ! self::$instance ) + self::$instance = new WPStrava(); + return self::$instance; + } + + // 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. + function strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity) { + $response = ""; + + //Check if the username is empty. + if (empty($strava_search_id)) { + $response .= __("Please configure the Strava search id on the widget options.", "wp-strava"); + } else { + $strava_rides = new Rides(); + $strava_rides->getLatestRides($strava_search_option, $strava_search_id, $quantity); + $rides_details = $strava_rides->getRidesDetails($strava_som_option); + + if ($strava_som_option == "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_option == "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') + ); + } + + $response .= ""; + } + return $response; + } // Function strava_request_handler + +} \ No newline at end of file diff --git a/util.class.php b/lib/Util.class.php similarity index 94% rename from util.class.php rename to lib/Util.class.php index 9c53526..462ddf9 100755 --- a/util.class.php +++ b/lib/Util.class.php @@ -1,9 +1,8 @@ Author URI: http://cmanon.com License: GPL2 */ -?> - -

' . __('Please include your strava email and password the password will not be stored.', 'wp-strava' ) . '

'; - } else { - $ride = new Rides(); - $token = $ride->getAuthenticationToken($email, $_POST['strava_password']); - if($token) { - update_option('strava_token', $token); - echo '

' . $ride->feedback . __('Token saved.', 'wp-strava' ) . '

'; - } else { - echo '

' . $ride->feedback . '

'; - } - } - } -?> -
-

-

-

- -
- - - - - - - - - - - - - - - - -
Email
Password
Token
- -

- -

-
-
- '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'); - } - - /** @see WP_Widget::widget */ - function widget($args, $instance) { - 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_som_option = empty($instance['strava_som_option']) ? 'metric' : $instance['strava_som_option']; - $strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id']; - $quantity = empty($instance['quantity']) ? '5' : $instance['quantity']; - - ?> - - - - - -

- - -

-

- - -

-

- - -

-

- - -

-

- - -

- getLatestRides($strava_search_option, $strava_search_id, $quantity); - $rides_details = $strava_rides->getRidesDetails($strava_som_option); - - if ($strava_som_option == "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_option == "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') - ); - } - - $response .= ""; - } - return $response; -} // Function strava_request_handler - - -class RideShortcode { - static $add_script; - - 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 - function handler($atts) { - self::$add_script = true; - - $token = get_option('strava_token'); - - if($token) { - $pairs = array( - 'id' => 0, - 'som' => "metric", - 'efforts' => false, - 'threshold' => 0, - 'map_width' => "100%", - 'map_height' => "400px" - ); - - extract(shortcode_atts($pairs, $atts)); - - if (isset($som)) { - $strava_som = $som; - } else { - $strava_som = get_option('strava_som_option', 'metric'); - } - - $ride = new Rides(); - $rideDetails = $ride->getRideDetails($id, $strava_som); - $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 _e('Please first get your strava token using the settings wp strava page.', 'wp-strava'); - } - } // handler - - static function registerScripts() { - wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__)); - - 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'); - } - - 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'); - } - } -} - -// Initialize short code -RideShortcode::init(); - -?> +*/ \ No newline at end of file From cfa7b083f946f0db8d278b0d7f2ef887e7a619e3 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sun, 17 Feb 2013 21:19:08 -0600 Subject: [PATCH 2/3] Fixed broken references after moving things around a bit --- lib/LatestRidesWidget.class.php | 67 ++++++++++++++++++++++++++++++--- lib/Settings.class.php | 34 ++++++++--------- lib/Strava.class.php | 56 +-------------------------- readme.txt | 15 +++++--- wp-strava.php | 6 +-- 5 files changed, 92 insertions(+), 86 deletions(-) diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index a7a2033..e9eb695 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -4,14 +4,15 @@ * WP Strava Latest Rides Widget Class */ class WPStrava_LatestRidesWidget extends WP_Widget { - function __construct() { + + 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'); } /** @see WP_Widget::widget */ - function widget($args, $instance) { + public function widget($args, $instance) { extract($args); //$widget_id = $args['widget_id']; @@ -24,13 +25,13 @@ class WPStrava_LatestRidesWidget extends WP_Widget { ?> - + strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?> get_field_id('title'); ?>">

+

getLatestRides($strava_search_option, $strava_search_id, $quantity); - $rides_details = $strava_rides->getRidesDetails($strava_som_option); - - if ($strava_som_option == "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_option == "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') - ); - } - - $response .= ""; - } - return $response; - } // Function strava_request_handler - } \ No newline at end of file diff --git a/readme.txt b/readme.txt index de84fa1..75afa85 100755 --- a/readme.txt +++ b/readme.txt @@ -1,18 +1,23 @@ === Plugin Name === -Contributors: cmanon (@cmanon) +Contributors: cmanon (@cmanon), jrfoell Donate link: http://cmanon.com/ -Tags: bycicle, strava +Tags: bicycle, cycling, strava Requires at least: 2.0 -Tested up to: 3.3.2 +Tested up to: 3.5.1 Stable tag: 0.62 +License: GPLv2 or later -This plugin is inteneded to show your strava.com information in your wordpress blog. +This plugin is intended to show your strava.com information in your WordPress site. == Description == -This plugin uses the REST strava.com API to pull the data out and show the information in your wordpress blog. +This plugin uses the REST strava.com API to pull the data out and show the information in your WordPress site. == Changelog == + += 0.70 = +Use WordPress Settings API for settings page + = 0.62 = Refactor some code. Fixed several bugs. diff --git a/wp-strava.php b/wp-strava.php index 45ca155..a0bf823 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -34,9 +34,9 @@ if( file_exists( WPSTRAVA_PLUGIN_DIR . 'lang/' . get_locale() . '.mo' ) ) { } require_once WPSTRAVA_PLUGIN_DIR . 'lib/Strava.class.php'; -$wpstrava = WPStrava::getInstance(); +$wpstrava = WPStrava::get_instance(); -/* +//@TODO only load these when needed function load_styles() { // Register a personalized stylesheet wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' ); @@ -50,5 +50,3 @@ function load_scripts() { //wp_enqueue_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false'); } add_action('wp-enqueue_script', 'load_scripts'); - -*/ \ No newline at end of file From 8d511f72ec9417c5bb23aa4f999ce75423cca7e0 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sun, 17 Feb 2013 22:14:10 -0600 Subject: [PATCH 3/3] Added system of measurement to settings page --- lib/LatestRidesWidget.class.php | 18 +++++--------- lib/Settings.class.php | 42 +++++++++++++++++++++++++++------ lib/Strava.class.php | 11 +++++++-- wp-strava.php | 2 +- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/lib/LatestRidesWidget.class.php b/lib/LatestRidesWidget.class.php index e9eb695..6af3bad 100644 --- a/lib/LatestRidesWidget.class.php +++ b/lib/LatestRidesWidget.class.php @@ -18,10 +18,12 @@ class WPStrava_LatestRidesWidget extends WP_Widget { //$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_som_option = empty($instance['strava_som_option']) ? 'metric' : $instance['strava_som_option']; $strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id']; $quantity = empty($instance['quantity']) ? '5' : $instance['quantity']; - + + $wpstrava = WPStrava::get_instance(); + $strava_som_option = $wpstrava->settings->som; + ?> @@ -54,7 +56,6 @@ class WPStrava_LatestRidesWidget extends WP_Widget { public function form($instance) { $title = isset($instance['title']) ? esc_attr($instance['title']) : _e('Rides', 'wp-strava'); $strava_search_option = isset($instance['strava_search_option']) ? esc_attr($instance['strava_search_option']) : "athlete"; - $strava_som_option = isset($instance['strava_som_option']) ? esc_attr($instance['strava_som_option']) : "metric"; $strava_search_id = isset($instance['strava_search_id']) ? esc_attr($instance['strava_search_id']) : ""; $quantity = isset($instance['quantity']) ? absint($instance['quantity']) : 5; @@ -71,13 +72,6 @@ class WPStrava_LatestRidesWidget extends WP_Widget {

-

- - -

@@ -117,8 +111,8 @@ class WPStrava_LatestRidesWidget extends WP_Widget { 'elapsedTime' => __('hours','wp-strava'), 'movingTime' => __('hours','wp-strava'), 'distance' => __('miles','wp-strava'), - 'averageSpeed' => __('miles/h','wp-strava'), - 'maximumSpeed' => __('miles/h','wp-strava'), + 'averageSpeed' => __('mph','wp-strava'), + 'maximumSpeed' => __('mph','wp-strava'), 'elevationGain' => __('feet','wp-strava') ); } diff --git a/lib/Settings.class.php b/lib/Settings.class.php index 5c957b1..5f6d368 100644 --- a/lib/Settings.class.php +++ b/lib/Settings.class.php @@ -18,24 +18,32 @@ class WPStrava_Settings { 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', '__return_null' ); + register_setting('wp-strava-settings-group','strava_password', NULL ); register_setting('wp-strava-settings-group','strava_token', array( $this, 'sanitize_token' ) ); - add_settings_section( 'strava_api', NULL, '__return_null', 'wp-strava' ); //NULL / __return_null no section label needed + 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' ); - + + register_setting('wp-strava-settings-group','strava_som', array( $this, 'sanitize_som' ) ); + + add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), NULL, 'wp-strava' ); + + add_settings_field( 'strava_som', __( 'System of Measurement', 'wp-strava' ), array( $this, 'print_som_input' ), 'wp-strava', 'strava_options' ); } + public function print_api_instructions() { + ?>


-

-

- +

+
@@ -88,5 +96,25 @@ class WPStrava_Settings { return $token; } -} + public function print_options_label() { + ?>

Options

+ + settings = new WPStrava_Settings(); @@ -16,7 +16,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_LatestRidesWidget' ); } ); } @@ -25,4 +25,11 @@ class WPStrava { self::$instance = new WPStrava(); return self::$instance; } + + public function __get( $name ) { + if ( isset( $this->{$name} ) ) + return $this->{$name}; + + return NULL; + } } \ No newline at end of file diff --git a/wp-strava.php b/wp-strava.php index a0bf823..e6e3eb4 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -36,7 +36,7 @@ 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 +//@TODO only load these when needed using is_active_widget() function load_styles() { // Register a personalized stylesheet wp_register_style('wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' );