From 50865bbbd21633e69d4f535947a825ebcba3c8b3 Mon Sep 17 00:00:00 2001 From: Justin Foell Date: Fri, 27 Apr 2018 21:19:27 -0500 Subject: [PATCH] Added WPStrava Exception Consolidated error handling --- lib/WPStrava.php | 8 +-- lib/WPStrava/API.php | 24 +++---- lib/WPStrava/Activity.php | 6 +- lib/WPStrava/ActivityShortcode.php | 73 ++++++++++++++----- lib/WPStrava/Exception.php | 81 ++++++++++++++++++++++ lib/WPStrava/LatestActivities.php | 8 ++- lib/WPStrava/LatestActivitiesShortcode.php | 62 +++++++++++++---- lib/WPStrava/LatestMapWidget.php | 34 ++++----- lib/WPStrava/RouteShortcode.php | 71 +++++++++++++------ 9 files changed, 268 insertions(+), 99 deletions(-) create mode 100644 lib/WPStrava/Exception.php diff --git a/lib/WPStrava.php b/lib/WPStrava.php index 91ef906..c68a999 100644 --- a/lib/WPStrava.php +++ b/lib/WPStrava.php @@ -152,9 +152,9 @@ class WPStrava { * Register the shortcodes. */ public function register_shortcodes() { - add_shortcode( 'ride', array( 'WPStrava_ActivityShortcode', 'handler' ) ); // @deprecated 1.1 - add_shortcode( 'activity', array( 'WPStrava_ActivityShortcode', 'handler' ) ); - add_shortcode( 'activities', array( 'WPStrava_LatestActivitiesShortcode', 'handler' ) ); - add_shortcode( 'route', array( 'WPStrava_RouteShortcode', 'handler' ) ); + // Initialize short code classes. + new WPStrava_ActivityShortcode(); + new WPStrava_LatestActivitiesShortcode(); + new WPStrava_RouteShortcode(); } } diff --git a/lib/WPStrava/API.php b/lib/WPStrava/API.php index 793204f..c4fd011 100755 --- a/lib/WPStrava/API.php +++ b/lib/WPStrava/API.php @@ -28,7 +28,7 @@ class WPStrava_API { $response = wp_remote_post( $url . $uri, $args ); if ( is_wp_error( $response ) ) { - return $response; + throw WPStrava_Exception::from_wp_error( $response ); } if ( 200 != $response['response']['code'] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison @@ -42,11 +42,11 @@ class WPStrava_API { $error = print_r( $response, true ); // phpcs:ignore -- Debug output. } - return new WP_Error( - 'wp-strava_post', - // Translators: message shown when there's a problem with an HTTP POST to the Strava API. - sprintf( __( 'ERROR %1$s %2$s - See full error by adding
define( \'WPSTRAVA_DEBUG\', true );
to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), - $error + // Throw an informational exception with a detailed debug exception. + throw new WPStrava_Exception( + $response['response']['message'], + $response['response']['code'], + new WPStrava_Exception( $error ) ); } @@ -75,7 +75,7 @@ class WPStrava_API { $response = wp_remote_get( $url, $get_args ); if ( is_wp_error( $response ) ) { - return $response; + throw WPStrava_Exception::from_wp_error( $response ); } if ( 200 != $response['response']['code'] ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison @@ -91,11 +91,11 @@ class WPStrava_API { $error = print_r( $response, true ); // phpcs:ignore -- Debug output. } - return new WP_Error( - 'wp-strava_get', - // Translators: message shown when there's a problem with an HTTP GET to the Strava API. - sprintf( __( 'ERROR %1$s %2$s - See full error by adding
define( \'WPSTRAVA_DEBUG\', true );
to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), - $error + // Throw an informational exception with a detailed debug exception. + throw new WPStrava_Exception( + $response['response']['message'], + $response['response']['code'], + new WPStrava_Exception( $error ) ); } diff --git a/lib/WPStrava/Activity.php b/lib/WPStrava/Activity.php index 4377836..68769e0 100755 --- a/lib/WPStrava/Activity.php +++ b/lib/WPStrava/Activity.php @@ -27,7 +27,7 @@ class WPStrava_Activity { * @param string $athlete_token Token of athlete to retrieve for * @param int $club_id Club ID of all club riders (optional). * @param int|null $quantity Number of records to retrieve (optional). - * @return array|WP_Error Array of activities or WP_Error. + * @return array Array of activities. */ public function get_activities( $athlete_token, $club_id = null, $quantity = null ) { $api = WPStrava::get_instance()->get_api( $athlete_token ); @@ -43,10 +43,6 @@ class WPStrava_Activity { $data = $api->get( 'athlete/activities', $args ); } - if ( is_wp_error( $data ) ) { - return $data; - } - if ( is_array( $data ) ) { return $data; } diff --git a/lib/WPStrava/ActivityShortcode.php b/lib/WPStrava/ActivityShortcode.php index e2b261b..7044b15 100644 --- a/lib/WPStrava/ActivityShortcode.php +++ b/lib/WPStrava/ActivityShortcode.php @@ -1,16 +1,50 @@ + * @since 1.0 + */ class WPStrava_ActivityShortcode { - private static $add_script; - public static function init() { - add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) ); + /** + * Whether or not to enqueue styles (if shortcode is present). + * + * @var boolean + * @author Justin Foell + * @since 1.0 + */ + private $add_script = false; + + /** + * Constructor (converted from static init()). + * + * @author Justin Foell + * @since 1.0 + */ + public function __construct() { + add_shortcode( 'ride', array( $this, 'handler' ) ); // @deprecated 1.1 + add_shortcode( 'activity', array( $this, 'handler' ) ); + add_action( 'wp_footer', array( $this, 'print_scripts' ) ); } - // Shortcode handler function - // [activity id=id som=metric map_width="100%" map_height="400px" markers=false] - public static function handler( $atts ) { - self::$add_script = true; + /** + * Shortcode handler for [activity]. + * + * [activity id=id som=metric map_width="100%" map_height="400px" markers=false] + * + * @param array $atts Array of attributes (id, map_width, etc.). + * @return string Shortcode output + * @author Justin Foell + * @since 1.0 + */ + public function handler( $atts ) { + $this->add_script = true; $defaults = array( 'id' => 0, @@ -25,14 +59,12 @@ class WPStrava_ActivityShortcode { $strava_som = WPStrava_SOM::get_som( $atts['som'] ); $activity = WPStrava::get_instance()->activity; - $activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] ); + $activity_details = null; - if ( is_wp_error( $activity_details ) ) { - if ( WPSTRAVA_DEBUG ) { - return '
' . print_r( $activity_details, true ) . '
'; // phpcs:ignore -- Debug output. - } else { - return $activity_details->get_error_message(); - } + try { + $activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] ); + } catch( WPStrava_Exception $e ) { + return $e->to_html(); } //sanitize width & height @@ -81,12 +113,15 @@ class WPStrava_ActivityShortcode { } // End if( $activity_details ). } - public static function print_scripts() { - if ( self::$add_script ) { + /** + * Enqueue style if shortcode is being used. + * + * @author Justin Foell + * @since 1.0 + */ + public function print_scripts() { + if ( $this->add_script ) { wp_enqueue_style( 'wp-strava-style' ); } } } - -// Initialize short code -WPStrava_ActivityShortcode::init(); diff --git a/lib/WPStrava/Exception.php b/lib/WPStrava/Exception.php new file mode 100644 index 0000000..b7e9afa --- /dev/null +++ b/lib/WPStrava/Exception.php @@ -0,0 +1,81 @@ +=' ) ) { + abstract class WPStrava_Abstract_Exception extends Exception {} +} else { + abstract class WPStrava_52_Exception extends Exception { + protected $previous; + + public function __construct( $message, $code = 0, Exception $previous = null ) { + $this->previous = $previous; + parent::__construct( $message, $code ); + } + + public function getPrevious() { + return $this->previous; + } + } + + abstract class WPStrava_Abstract_Exception extends WPStrava_52_Exception {} +} + +/* + * Exception class for error handling/display. + */ +class WPStrava_Exception extends WPStrava_Abstract_Exception { + + /** + * Create a WPStrava_Exception from a WP_Error. + * + * @param WP_Error $error + * @return WPStrava_Exception + * @author Justin Foell + * @since + */ + public static function from_wp_error( WP_Error $error ) { + $class = __CLASS__; + return new $class( $error->get_error_message( $error->get_error_code() ) ); + } + + public function to_html() { + return '
' . $this . '
'; + } + + public function __toString() { + if ( WPSTRAVA_DEBUG && $this->getPrevious() ) { + return $this->get_formatted_message( $this->getPrevious() ); + } + + return $this->get_formatted_message( $this ); + } + + public function get_formatted_message( $exception ) { + $code = $exception->getCode(); + + if ( $exception->getPrevious() ) { + if ( $code ) { + // Translators: Message shown when there's an exception thrown with a code and there's more details available. + return sprintf( __( 'WP Strava ERROR %1$s %2$s - See full error by adding
define( \'WPSTRAVA_DEBUG\', true );
to wp-config.php', 'wp-strava' ), $code, $this->getMessage() ); + } + // Translators: Message shown when there's an exception thrown (no code) and there's more details available. + return sprintf( __( 'WP Strava ERROR %1$s - See full error by adding
define( \'WPSTRAVA_DEBUG\', true );
to wp-config.php', 'wp-strava' ), $this->getMessage() ); + } + + if ( $code ) { + // Translators: Message shown when there's an exception thrown with a code. + return sprintf( __( 'WP Strava ERROR %1$s %2$s', 'wp-strava' ), $code, $exception->getMessage() ); + } + // Translators: Message shown when there's an exception thrown without a code. + return sprintf( __( 'WP Strava ERROR %1$s', 'wp-strava' ), $exception->getMessage() ); + } +} +// phpcs:enable diff --git a/lib/WPStrava/LatestActivities.php b/lib/WPStrava/LatestActivities.php index 2d42ee3..ba88f9f 100644 --- a/lib/WPStrava/LatestActivities.php +++ b/lib/WPStrava/LatestActivities.php @@ -14,10 +14,12 @@ class WPStrava_LatestActivities { $som = WPStrava_SOM::get_som( $args['som'] ); $strava_activity = WPStrava::get_instance()->activity; - $activities = $strava_activity->get_activities( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] ); + $activities = array(); - if ( is_wp_error( $activities ) ) { - return $activities->get_error_message(); + try { + $activities = $strava_activity->get_activities( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] ); + } catch ( WPStrava_Exception $e ) { + return $e->to_html(); } $response = "
    "; diff --git a/lib/WPStrava/LatestActivitiesShortcode.php b/lib/WPStrava/LatestActivitiesShortcode.php index 7edbf38..05fec95 100644 --- a/lib/WPStrava/LatestActivitiesShortcode.php +++ b/lib/WPStrava/LatestActivitiesShortcode.php @@ -1,25 +1,61 @@ + * @since 1.3.0 + */ class WPStrava_LatestActivitiesShortcode { - private static $add_script; - public static function init() { - add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) ); + /** + * Whether or not to enqueue styles (if shortcode is present). + * + * @var boolean + * @author Justin Foell + * @since 1.3.0 + */ + private $add_script = false; + + /** + * Constructor (converted from static init()). + * + * @author Justin Foell + * @since 1.3.0 + */ + public function __construct() { + add_shortcode( 'activities', array( $this, 'handler' ) ); + add_action( 'wp_footer', array( $this, 'print_scripts' ) ); } - // Shortcode handler function - // [activities som=metric quantity=5 athlete_token=xxx|strava_club_id=yyy] - public static function handler( $atts ) { - self::$add_script = true; + /** + * Shortcode handler for [activities]. + * + * [activities som=metric quantity=5 athlete_token=xxx|strava_club_id=yyy] + * + * @param array $atts Array of attributes (id, som, etc.). + * @return string Shortcode output + * @author Justin Foell + * @since 1.3.0 + */ + public function handler( $atts ) { + $this->add_script = true; return WPStrava_LatestActivities::get_activities_html( $atts ); - } // handler + } - public static function print_scripts() { - if ( self::$add_script ) { + /** + * Enqueue style if shortcode is being used. + * + * @author Justin Foell + * @since 1.3.0 + */ + public function print_scripts() { + if ( $this->add_script ) { wp_enqueue_style( 'wp-strava-style' ); } } } - -// Initialize short code -WPStrava_LatestActivitiesShortcode::init(); diff --git a/lib/WPStrava/LatestMapWidget.php b/lib/WPStrava/LatestMapWidget.php index 5299a51..0ca26f7 100644 --- a/lib/WPStrava/LatestMapWidget.php +++ b/lib/WPStrava/LatestMapWidget.php @@ -84,25 +84,20 @@ class WPStrava_LatestMapWidget extends WP_Widget { $activity = $activity_transient ? $activity_transient : null; + echo $args['before_widget']; + if ( $title ) { + echo $args['before_title'] . $title . $args['after_title']; + } + if ( ! $activity || empty( $activity->map ) ) { $strava_activity = WPStrava::get_instance()->activity; - $activities = $strava_activity->get_activities( $athlete_token, $strava_club_id ); - if ( is_wp_error( $activities ) ) { - echo $args['before_widget']; - if ( $title ) { - echo $args['before_title'] . $title . $args['after_title']; - } + $activities = array(); - if ( WPSTRAVA_DEBUG ) { - echo '
    ';
    -					print_r( $activities ); // phpcs:ignore -- Debug output.
    -					echo '
    '; - } else { - echo $activities->get_error_message(); - } - echo $args['after_widget']; - return; + try { + $activities = $strava_activity->get_activities( $athlete_token, $strava_club_id ); + } catch( WPStrava_Exception $e ) { + echo $e->to_html(); } if ( ! empty( $activities ) ) { @@ -113,7 +108,7 @@ class WPStrava_LatestMapWidget extends WP_Widget { $activity = current( $activities ); - // Compare transient (temporary storage) to option (more permenant). + // Compare transient (temporary storage) to option (more permanent). // If the option isn't set or the transient is different, update the option. if ( empty( $activity_option->id ) || $activity->id != $activity_option->id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison $build_new = true; @@ -128,19 +123,14 @@ class WPStrava_LatestMapWidget extends WP_Widget { } if ( $activity ) { - echo $args['before_widget']; - if ( $title ) { - echo $args['before_title'] . $title . $args['after_title']; - } - echo empty( $activity->map ) ? // Translators: Text with activity name shown in place of image if not available. sprintf( __( 'Map not available for activity "%s"', 'wp-strava' ), $activity->name ) : "" . $this->get_static_image( $id, $activity, $build_new ) . ''; - echo $args['after_widget']; } + echo $args['after_widget']; } /** diff --git a/lib/WPStrava/RouteShortcode.php b/lib/WPStrava/RouteShortcode.php index 371f12d..d30be37 100644 --- a/lib/WPStrava/RouteShortcode.php +++ b/lib/WPStrava/RouteShortcode.php @@ -1,22 +1,50 @@ + * @since 1.6.0 + */ + public function __construct() { + add_shortcode( 'route', array( $this, 'handler' ) ); + add_action( 'wp_footer', array( $this, 'print_scripts' ) ); } - // Shortcode handler function - // [route id=id som=metric map_width="100%" map_height="400px" markers=false] - public static function handler( $atts ) { - self::$add_script = true; + /** + * Shortcode handler for [route]. + * + * [route id=id som=metric map_width="100%" map_height="400px" markers=false] + * + * @param array $atts Array of attributes (id, map_width, etc.). + * @return string Shortcode output + * @author Daniel Lintott + * @since 1.3.0 + */ + public function handler( $atts ) { + $this->add_script = true; $defaults = array( 'id' => 0, @@ -31,17 +59,15 @@ class WPStrava_RouteShortcode { $strava_som = WPStrava_SOM::get_som( $atts['som'] ); $route = WPStrava::get_instance()->routes; - $route_details = $route->get_route( $atts['id'] ); + $route_details = null; - if ( is_wp_error( $route_details ) ) { - if ( WPSTRAVA_DEBUG ) { - return '
    ' . print_r( $route_details, true ) . '
    '; // phpcs:ignore -- Debug output. - } else { - return $route_details->get_error_message(); - } + try { + $route_details = $route->get_route( $atts['id'] ); + } catch( WPStrava_Exception $e ) { + return $e->to_html(); } - //sanitize width & height + // Sanitize width & height. $map_width = str_replace( '%', '', $atts['map_width'] ); $map_height = str_replace( '%', '', $atts['map_height'] ); $map_width = str_replace( 'px', '', $map_width ); @@ -78,12 +104,15 @@ class WPStrava_RouteShortcode { } // End if( $route_details ). } - public static function print_scripts() { - if ( self::$add_script ) { + /** + * Enqueue style if shortcode is being used. + * + * @author Daniel Lintott + * @since 1.3.0 + */ + public function print_scripts() { + if ( $this->add_script ) { wp_enqueue_style( 'wp-strava-style' ); } } } - -// Initialize short code -WPStrava_RouteShortcode::init();