mirror of
https://github.com/10h30/wp-strava.git
synced 2026-06-05 15:10:01 +09:00
Added WPStrava Exception
Consolidated error handling
This commit is contained in:
+4
-4
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
+12
-12
@@ -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<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>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<br/><code>define( \'WPSTRAVA_DEBUG\', true );</code><br/>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 )
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* Activity Shortcode [activity].
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
/**
|
||||
* Activity Shortcode class (converted from Ride).
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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 <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]
|
||||
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 <justin@foell.org>
|
||||
* @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 '<pre>' . print_r( $activity_details, true ) . '</pre>'; // 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 <justin@foell.org>
|
||||
* @since 1.0
|
||||
*/
|
||||
public function print_scripts() {
|
||||
if ( $this->add_script ) {
|
||||
wp_enqueue_style( 'wp-strava-style' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize short code
|
||||
WPStrava_ActivityShortcode::init();
|
||||
|
||||
@@ -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
|
||||
@@ -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 = "<ul id='activities'>";
|
||||
|
||||
@@ -1,25 +1,61 @@
|
||||
<?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 {
|
||||
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]
|
||||
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 <justin@foell.org>
|
||||
* @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 <justin@foell.org>
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public function print_scripts() {
|
||||
if ( $this->add_script ) {
|
||||
wp_enqueue_style( 'wp-strava-style' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize short code
|
||||
WPStrava_LatestActivitiesShortcode::init();
|
||||
|
||||
@@ -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 '<pre>';
|
||||
print_r( $activities ); // phpcs:ignore -- Debug output.
|
||||
echo '</pre>';
|
||||
} 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 ) :
|
||||
"<a title='{$activity->name}' href='" . WPStrava_Activity::ACTIVITIES_URL . "{$activity->id}'>" .
|
||||
$this->get_static_image( $id, $activity, $build_new ) .
|
||||
'</a>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,22 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* Route Shortcode [route].
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
/**
|
||||
* Route Shortcode.
|
||||
* Route Shortcode class.
|
||||
*
|
||||
* @author Daniel Lintott
|
||||
* @since 1.3.0
|
||||
*/
|
||||
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]
|
||||
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 '<pre>' . print_r( $route_details, true ) . '</pre>'; // 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();
|
||||
|
||||
Reference in New Issue
Block a user