Added WPStrava Exception

Consolidated error handling
This commit is contained in:
Justin Foell
2018-04-27 21:19:27 -05:00
parent 7ad681cd1b
commit 50865bbbd2
9 changed files with 268 additions and 99 deletions
+4 -4
View File
@@ -152,9 +152,9 @@ class WPStrava {
* Register the shortcodes. * Register the shortcodes.
*/ */
public function register_shortcodes() { public function register_shortcodes() {
add_shortcode( 'ride', array( 'WPStrava_ActivityShortcode', 'handler' ) ); // @deprecated 1.1 // Initialize short code classes.
add_shortcode( 'activity', array( 'WPStrava_ActivityShortcode', 'handler' ) ); new WPStrava_ActivityShortcode();
add_shortcode( 'activities', array( 'WPStrava_LatestActivitiesShortcode', 'handler' ) ); new WPStrava_LatestActivitiesShortcode();
add_shortcode( 'route', array( 'WPStrava_RouteShortcode', 'handler' ) ); new WPStrava_RouteShortcode();
} }
} }
+12 -12
View File
@@ -28,7 +28,7 @@ class WPStrava_API {
$response = wp_remote_post( $url . $uri, $args ); $response = wp_remote_post( $url . $uri, $args );
if ( is_wp_error( $response ) ) { 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 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. $error = print_r( $response, true ); // phpcs:ignore -- Debug output.
} }
return new WP_Error( // Throw an informational exception with a detailed debug exception.
'wp-strava_post', throw new WPStrava_Exception(
// Translators: message shown when there's a problem with an HTTP POST to the Strava API. $response['response']['message'],
sprintf( __( 'ERROR %1$s %2$s - See full error by adding<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), $response['response']['code'],
$error new WPStrava_Exception( $error )
); );
} }
@@ -75,7 +75,7 @@ class WPStrava_API {
$response = wp_remote_get( $url, $get_args ); $response = wp_remote_get( $url, $get_args );
if ( is_wp_error( $response ) ) { 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 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. $error = print_r( $response, true ); // phpcs:ignore -- Debug output.
} }
return new WP_Error( // Throw an informational exception with a detailed debug exception.
'wp-strava_get', throw new WPStrava_Exception(
// Translators: message shown when there's a problem with an HTTP GET to the Strava API. $response['response']['message'],
sprintf( __( 'ERROR %1$s %2$s - See full error by adding<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>to wp-config.php', 'wp-strava' ), $response['response']['code'], $response['response']['message'] ), $response['response']['code'],
$error new WPStrava_Exception( $error )
); );
} }
+1 -5
View File
@@ -27,7 +27,7 @@ class WPStrava_Activity {
* @param string $athlete_token Token of athlete to retrieve for * @param string $athlete_token Token of athlete to retrieve for
* @param int $club_id Club ID of all club riders (optional). * @param int $club_id Club ID of all club riders (optional).
* @param int|null $quantity Number of records to retrieve (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 ) { public function get_activities( $athlete_token, $club_id = null, $quantity = null ) {
$api = WPStrava::get_instance()->get_api( $athlete_token ); $api = WPStrava::get_instance()->get_api( $athlete_token );
@@ -43,10 +43,6 @@ class WPStrava_Activity {
$data = $api->get( 'athlete/activities', $args ); $data = $api->get( 'athlete/activities', $args );
} }
if ( is_wp_error( $data ) ) {
return $data;
}
if ( is_array( $data ) ) { if ( is_array( $data ) ) {
return $data; return $data;
} }
+54 -19
View File
@@ -1,16 +1,50 @@
<?php <?php
/**
* Activity Shortcode [activity].
* @package WPStrava
*/
/**
* Activity Shortcode class (converted from Ride).
*
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
class WPStrava_ActivityShortcode { 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 <justin@foell.org>
* @since 1.0
*/
private $add_script = false;
/**
* Constructor (converted from static init()).
*
* @author Justin Foell <justin@foell.org>
* @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] * Shortcode handler for [activity].
public static function handler( $atts ) { *
self::$add_script = true; * [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 <justin@foell.org>
* @since 1.0
*/
public function handler( $atts ) {
$this->add_script = true;
$defaults = array( $defaults = array(
'id' => 0, 'id' => 0,
@@ -25,14 +59,12 @@ class WPStrava_ActivityShortcode {
$strava_som = WPStrava_SOM::get_som( $atts['som'] ); $strava_som = WPStrava_SOM::get_som( $atts['som'] );
$activity = WPStrava::get_instance()->activity; $activity = WPStrava::get_instance()->activity;
$activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] ); $activity_details = null;
if ( is_wp_error( $activity_details ) ) { try {
if ( WPSTRAVA_DEBUG ) { $activity_details = $activity->get_activity( $atts['athlete_token'], $atts['id'] );
return '<pre>' . print_r( $activity_details, true ) . '</pre>'; // phpcs:ignore -- Debug output. } catch( WPStrava_Exception $e ) {
} else { return $e->to_html();
return $activity_details->get_error_message();
}
} }
//sanitize width & height //sanitize width & height
@@ -81,12 +113,15 @@ class WPStrava_ActivityShortcode {
} // End if( $activity_details ). } // End if( $activity_details ).
} }
public static function print_scripts() { /**
if ( self::$add_script ) { * Enqueue style if shortcode is being used.
*
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
public function print_scripts() {
if ( $this->add_script ) {
wp_enqueue_style( 'wp-strava-style' ); wp_enqueue_style( 'wp-strava-style' );
} }
} }
} }
// Initialize short code
WPStrava_ActivityShortcode::init();
+81
View File
@@ -0,0 +1,81 @@
<?php
/**
* WPStrava Exception(s).
*/
// phpcs:disable Generic.Files.OneClassPerFile.MultipleFound, Generic.Classes.DuplicateClassName.Found
/*
* PHP 5.2 Nonsense
* @see http://php.net/manual/en/exception.getprevious.php#106020
*/
if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
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 <justin.foell@webdevstudios.com>
* @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 '<pre>' . $this . '</pre>';
}
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<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>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<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>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
+5 -3
View File
@@ -14,10 +14,12 @@ class WPStrava_LatestActivities {
$som = WPStrava_SOM::get_som( $args['som'] ); $som = WPStrava_SOM::get_som( $args['som'] );
$strava_activity = WPStrava::get_instance()->activity; $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 ) ) { try {
return $activities->get_error_message(); $activities = $strava_activity->get_activities( $args['athlete_token'], $args['strava_club_id'], $args['quantity'] );
} catch ( WPStrava_Exception $e ) {
return $e->to_html();
} }
$response = "<ul id='activities'>"; $response = "<ul id='activities'>";
+49 -13
View File
@@ -1,25 +1,61 @@
<?php <?php
/**
* Latest Activities Shortcode [activities].
* @package WPStrava
*/
/**
* Latest Activities Shortcode class (converted from LatestRides).
*
* @author Justin Foell <justin@foell.org>
* @since 1.3.0
*/
class WPStrava_LatestActivitiesShortcode { 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 <justin@foell.org>
* @since 1.3.0
*/
private $add_script = false;
/**
* Constructor (converted from static init()).
*
* @author Justin Foell <justin@foell.org>
* @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] * Shortcode handler for [activities].
public static function handler( $atts ) { *
self::$add_script = true; * [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 <justin@foell.org>
* @since 1.3.0
*/
public function handler( $atts ) {
$this->add_script = true;
return WPStrava_LatestActivities::get_activities_html( $atts ); 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 <justin@foell.org>
* @since 1.3.0
*/
public function print_scripts() {
if ( $this->add_script ) {
wp_enqueue_style( 'wp-strava-style' ); wp_enqueue_style( 'wp-strava-style' );
} }
} }
} }
// Initialize short code
WPStrava_LatestActivitiesShortcode::init();
+12 -22
View File
@@ -84,25 +84,20 @@ class WPStrava_LatestMapWidget extends WP_Widget {
$activity = $activity_transient ? $activity_transient : null; $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 ) ) { if ( ! $activity || empty( $activity->map ) ) {
$strava_activity = WPStrava::get_instance()->activity; $strava_activity = WPStrava::get_instance()->activity;
$activities = $strava_activity->get_activities( $athlete_token, $strava_club_id );
if ( is_wp_error( $activities ) ) { $activities = array();
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if ( WPSTRAVA_DEBUG ) { try {
echo '<pre>'; $activities = $strava_activity->get_activities( $athlete_token, $strava_club_id );
print_r( $activities ); // phpcs:ignore -- Debug output. } catch( WPStrava_Exception $e ) {
echo '</pre>'; echo $e->to_html();
} else {
echo $activities->get_error_message();
}
echo $args['after_widget'];
return;
} }
if ( ! empty( $activities ) ) { if ( ! empty( $activities ) ) {
@@ -113,7 +108,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
$activity = current( $activities ); $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 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 if ( empty( $activity_option->id ) || $activity->id != $activity_option->id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
$build_new = true; $build_new = true;
@@ -128,19 +123,14 @@ class WPStrava_LatestMapWidget extends WP_Widget {
} }
if ( $activity ) { if ( $activity ) {
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo empty( $activity->map ) ? echo empty( $activity->map ) ?
// Translators: Text with activity name shown in place of image if not available. // Translators: Text with activity name shown in place of image if not available.
sprintf( __( 'Map not available for activity "%s"', 'wp-strava' ), $activity->name ) : sprintf( __( 'Map not available for activity "%s"', 'wp-strava' ), $activity->name ) :
"<a title='{$activity->name}' href='" . WPStrava_Activity::ACTIVITIES_URL . "{$activity->id}'>" . "<a title='{$activity->name}' href='" . WPStrava_Activity::ACTIVITIES_URL . "{$activity->id}'>" .
$this->get_static_image( $id, $activity, $build_new ) . $this->get_static_image( $id, $activity, $build_new ) .
'</a>'; '</a>';
echo $args['after_widget'];
} }
echo $args['after_widget'];
} }
/** /**
+50 -21
View File
@@ -1,22 +1,50 @@
<?php <?php
/**
* Route Shortcode [route].
* @package WPStrava
*/
/** /**
* Route Shortcode. * Route Shortcode class.
* *
* @author Daniel Lintott * @author Daniel Lintott
* @since 1.3.0 * @since 1.3.0
*/ */
class WPStrava_RouteShortcode { class WPStrava_RouteShortcode {
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 Daniel Lintott
* @since 1.3.0
*/
private $add_script = false;
/**
* Constructor (converted from static init()).
*
* @author Daniel Lintott
* @author Justin Foell <justin@foell.org>
* @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] * Shortcode handler for [route].
public static function handler( $atts ) { *
self::$add_script = true; * [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( $defaults = array(
'id' => 0, 'id' => 0,
@@ -31,17 +59,15 @@ class WPStrava_RouteShortcode {
$strava_som = WPStrava_SOM::get_som( $atts['som'] ); $strava_som = WPStrava_SOM::get_som( $atts['som'] );
$route = WPStrava::get_instance()->routes; $route = WPStrava::get_instance()->routes;
$route_details = $route->get_route( $atts['id'] ); $route_details = null;
if ( is_wp_error( $route_details ) ) { try {
if ( WPSTRAVA_DEBUG ) { $route_details = $route->get_route( $atts['id'] );
return '<pre>' . print_r( $route_details, true ) . '</pre>'; // phpcs:ignore -- Debug output. } catch( WPStrava_Exception $e ) {
} else { return $e->to_html();
return $route_details->get_error_message();
}
} }
//sanitize width & height // Sanitize width & height.
$map_width = str_replace( '%', '', $atts['map_width'] ); $map_width = str_replace( '%', '', $atts['map_width'] );
$map_height = str_replace( '%', '', $atts['map_height'] ); $map_height = str_replace( '%', '', $atts['map_height'] );
$map_width = str_replace( 'px', '', $map_width ); $map_width = str_replace( 'px', '', $map_width );
@@ -78,12 +104,15 @@ class WPStrava_RouteShortcode {
} // End if( $route_details ). } // 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' ); wp_enqueue_style( 'wp-strava-style' );
} }
} }
} }
// Initialize short code
WPStrava_RouteShortcode::init();