From 45058a5e381629ce2129a2ac5814c685600646fd Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 27 Mar 2020 16:39:36 -0500 Subject: [PATCH] Add ActivityRenderer --- src/WPStrava/ActivityRenderer.php | 147 +++++++++++++++++++++++++++++ src/WPStrava/ActivityShortcode.php | 132 +------------------------- 2 files changed, 149 insertions(+), 130 deletions(-) create mode 100644 src/WPStrava/ActivityRenderer.php diff --git a/src/WPStrava/ActivityRenderer.php b/src/WPStrava/ActivityRenderer.php new file mode 100644 index 0000000..8a356b5 --- /dev/null +++ b/src/WPStrava/ActivityRenderer.php @@ -0,0 +1,147 @@ + + * @since 2.2.0 + */ + public function get_html( $atts ) { + $defaults = array( + 'id' => 0, + 'som' => WPStrava::get_instance()->settings->som, + 'map_width' => '480', + 'map_height' => '320', + 'client_id' => WPStrava::get_instance()->settings->get_default_id(), + 'markers' => false, + 'image_only' => false, + ); + + $atts = wp_parse_args( $atts, $defaults ); + + /* Make sure boolean values are actually boolean + * @see https://wordpress.stackexchange.com/a/119299 + */ + $atts['markers'] = filter_var( $atts['markers'], FILTER_VALIDATE_BOOLEAN ); + $atts['image_only'] = filter_var( $atts['image_only'], FILTER_VALIDATE_BOOLEAN ); + + $activity = WPStrava::get_instance()->activity; + $activity_details = null; + + try { + $activity_details = $activity->get_activity( $atts['client_id'], $atts['id'] ); + } catch ( WPStrava_Exception $e ) { + return $e->to_html(); + } + + $activity_output = ''; + if ( $activity_details ) { + $activity_output .= '
'; + if ( ! $atts['image_only'] ) { + $activity_output .= $this->get_table( $activity_details, $atts['som'] ); + } + + // Sanitize width & height. + $map_width = str_replace( '%', '', $atts['map_width'] ); + $map_height = str_replace( '%', '', $atts['map_height'] ); + $map_width = str_replace( 'px', '', $map_width ); + $map_height = str_replace( 'px', '', $map_height ); + + $activity_output .= '' . + WPStrava_StaticMap::get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'] ) . + ' +
'; + } // End if( $activity_details ). + return $activity_output; + } + + /** + * The the activity details in in HTML table. + * + * @param string $activity_details Activity details from the activity class. + * @param string $som System of measure (english/metric). + * @return string HTML Table of activity details. + * @author Justin Foell + * @author Sebastian Erb + * @since 1.7.0 + */ + private function get_table( $activity_details, $som ) { + $strava_som = WPStrava_SOM::get_som( $som ); + $strava_activitytype = WPStrava_ActivityType::get_type_group( $activity_details->type ); + $avg_title = '' . __( 'Average Speed', 'wp-strava' ) . ''; + $max_title = '' . __( 'Max Speed', 'wp-strava' ) . ''; + $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; + $avg_speed = ''; + $max_speed = ''; + $elevation = '' . $strava_som->elevation( $activity_details->total_elevation_gain ) . ''; + $speed_label = ''; + $elevation_label = '' . $strava_som->get_elevation_label() . ''; + + switch ( $strava_activitytype ) { + case WPStrava_ActivityType::TYPE_GROUP_PACE: + $avg_speed = '' . $strava_som->pace( $activity_details->average_speed ) . ''; + $max_speed = '' . $strava_som->pace( $activity_details->max_speed ) . ''; + $speed_label = '' . $strava_som->get_pace_label() . ''; + break; + case WPStrava_ActivityType::TYPE_GROUP_SPEED: + $avg_speed = '' . $strava_som->speed( $activity_details->average_speed ) . ''; + $max_speed = '' . $strava_som->speed( $activity_details->max_speed ) . ''; + $speed_label = '' . $strava_som->get_speed_label() . ''; + break; + case WPStrava_ActivityType::TYPE_GROUP_PACE: + $avg_speed = '' . $strava_som->swimpace( $activity_details->average_speed ) . ''; + $max_speed = '' . $strava_som->swimpace( $activity_details->max_speed ) . ''; + $speed_label = '' . $strava_som->get_swimpace_label() . ''; + break; + default: + $avg_title = ''; + $max_title = ''; + break; + } + + if ( WPStrava::get_instance()->settings->hide_elevation ) { + $elevation = ''; + $elevation_title = ''; + $elevation_label = ''; + } + + return ' + + + + + + + ' . $avg_title . ' + ' . $max_title . ' + ' . $elevation_title . ' + + + + + + + + ' . $avg_speed . ' + ' . $max_speed . ' + ' . $elevation . ' + + + + + + ' . $speed_label . ' + ' . $speed_label . ' + ' . $elevation_label . ' + + +
' . __( 'Elapsed Time', 'wp-strava' ) . '' . __( 'Moving Time', 'wp-strava' ) . '' . __( 'Distance', 'wp-strava' ) . '
' . $strava_som->time( $activity_details->elapsed_time ) . '' . $strava_som->time( $activity_details->moving_time ) . '' . $strava_som->distance( $activity_details->distance ) . '
' . $strava_som->get_time_label() . '' . $strava_som->get_time_label() . '' . $strava_som->get_distance_label() . '
+ '; + } +} diff --git a/src/WPStrava/ActivityShortcode.php b/src/WPStrava/ActivityShortcode.php index eb63ad3..8350294 100644 --- a/src/WPStrava/ActivityShortcode.php +++ b/src/WPStrava/ActivityShortcode.php @@ -51,136 +51,8 @@ class WPStrava_ActivityShortcode { $this->add_script = true; - $defaults = array( - 'id' => 0, - 'som' => WPStrava::get_instance()->settings->som, - 'map_width' => '480', - 'map_height' => '320', - 'client_id' => WPStrava::get_instance()->settings->get_default_id(), - 'markers' => false, - 'image_only' => false, - ); - - $atts = shortcode_atts( $defaults, $atts, 'activity' ); - - /* Make sure boolean values are actually boolean - * @see https://wordpress.stackexchange.com/a/119299 - */ - $atts['markers'] = filter_var( $atts['markers'], FILTER_VALIDATE_BOOLEAN ); - $atts['image_only'] = filter_var( $atts['image_only'], FILTER_VALIDATE_BOOLEAN ); - - $activity = WPStrava::get_instance()->activity; - $activity_details = null; - - try { - $activity_details = $activity->get_activity( $atts['client_id'], $atts['id'] ); - } catch ( WPStrava_Exception $e ) { - return $e->to_html(); - } - - $activity_output = ''; - if ( $activity_details ) { - $activity_output .= '
'; - if ( ! $atts['image_only'] ) { - $activity_output .= $this->get_table( $activity_details, $atts['som'] ); - } - - // Sanitize width & height. - $map_width = str_replace( '%', '', $atts['map_width'] ); - $map_height = str_replace( '%', '', $atts['map_height'] ); - $map_width = str_replace( 'px', '', $map_width ); - $map_height = str_replace( 'px', '', $map_height ); - - $activity_output .= '' . - WPStrava_StaticMap::get_image_tag( $activity_details, $map_height, $map_width, $atts['markers'] ) . - ' -
'; - } // End if( $activity_details ). - return $activity_output; - } - - /** - * The the activity details in in HTML table. - * - * @param string $activity_details Activity details from the activity class. - * @param string $som System of measure (english/metric). - * @return string HTML Table of activity details. - * @author Justin Foell - * @author Sebastian Erb - * @since 1.7.0 - */ - private function get_table( $activity_details, $som ) { - $strava_som = WPStrava_SOM::get_som( $som ); - $strava_activitytype = WPStrava_ActivityType::get_type_group( $activity_details->type ); - $avg_title = '' . __( 'Average Speed', 'wp-strava' ) . ''; - $max_title = '' . __( 'Max Speed', 'wp-strava' ) . ''; - $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; - $avg_speed = ''; - $max_speed = ''; - $elevation = '' . $strava_som->elevation( $activity_details->total_elevation_gain ) . ''; - $speed_label = ''; - $elevation_label = '' . $strava_som->get_elevation_label() . ''; - - switch ( $strava_activitytype ) { - case WPStrava_ActivityType::TYPE_GROUP_PACE: - $avg_speed = '' . $strava_som->pace( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->pace( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_pace_label() . ''; - break; - case WPStrava_ActivityType::TYPE_GROUP_SPEED: - $avg_speed = '' . $strava_som->speed( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->speed( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_speed_label() . ''; - break; - case WPStrava_ActivityType::TYPE_GROUP_PACE: - $avg_speed = '' . $strava_som->swimpace( $activity_details->average_speed ) . ''; - $max_speed = '' . $strava_som->swimpace( $activity_details->max_speed ) . ''; - $speed_label = '' . $strava_som->get_swimpace_label() . ''; - break; - default: - $avg_title = ''; - $max_title = ''; - break; - } - - if ( WPStrava::get_instance()->settings->hide_elevation ) { - $elevation = ''; - $elevation_title = ''; - $elevation_label = ''; - } - - return ' - - - - - - - ' . $avg_title . ' - ' . $max_title . ' - ' . $elevation_title . ' - - - - - - - - ' . $avg_speed . ' - ' . $max_speed . ' - ' . $elevation . ' - - - - - - ' . $speed_label . ' - ' . $speed_label . ' - ' . $elevation_label . ' - - -
' . __( 'Elapsed Time', 'wp-strava' ) . '' . __( 'Moving Time', 'wp-strava' ) . '' . __( 'Distance', 'wp-strava' ) . '
' . $strava_som->time( $activity_details->elapsed_time ) . '' . $strava_som->time( $activity_details->moving_time ) . '' . $strava_som->distance( $activity_details->distance ) . '
' . $strava_som->get_time_label() . '' . $strava_som->get_time_label() . '' . $strava_som->get_distance_label() . '
- '; + $renderer = new WPStrava_ActivityRenderer(); + return $renderer->get_html( $atts ); } /**