From c8031dd6782195679139a331511b640403f0b1df Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Sun, 3 Feb 2013 20:38:55 -0600 Subject: [PATCH] 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