+ * @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();