diff --git a/readme.txt b/readme.txt index ea2e355..dfbbe6c 100755 --- a/readme.txt +++ b/readme.txt @@ -121,6 +121,9 @@ On the WP-Strava settings page you cannot currently remove and add another athle == Changelog == += Unreleased = +Add Segment Block + = 2.8.0 = Revised `block.json` based on feedback from https://wordpress.org/plugins/developers/block-plugin-validator/ Add PHPCompatibility checks to coding standards (and fixes from recommendations) diff --git a/src/WPStrava.php b/src/WPStrava.php index 2b7c25a..d5d61eb 100644 --- a/src/WPStrava.php +++ b/src/WPStrava.php @@ -165,6 +165,19 @@ class WPStrava { return $this->routes; } + /** + * Get the segments object. + * + * @return WPStrava_Segments + * @since 2.9.0 + */ + public function get_segments() { + if ( ! $this->segments ) { + $this->segments = new WPStrava_Segments(); + } + return $this->segments; + } + /** * Register the wp-strava stylesheet. */ diff --git a/src/WPStrava/Activity.php b/src/WPStrava/Activity.php index 66e8d41..2119ec3 100755 --- a/src/WPStrava/Activity.php +++ b/src/WPStrava/Activity.php @@ -10,7 +10,7 @@ class WPStrava_Activity { /** * Get single activity by ID. * - * @param string $client_id Client ID of athlete to retrieve for + * @param string $client_id Client ID of athlete to retrieve for. * @param int $activity_id ID of activity to retrieve. * @return object stdClass Representing this activity. * @author Justin Foell diff --git a/src/WPStrava/Segments.php b/src/WPStrava/Segments.php new file mode 100644 index 0000000..d0f866c --- /dev/null +++ b/src/WPStrava/Segments.php @@ -0,0 +1,72 @@ + + * @since 2.9.0 + */ + public function get_segment( $client_id, $segment_id ) { + return WPStrava::get_instance()->get_api( $client_id )->get( "segments/{$segment_id}" ); + } + + /** + * Get starred segment list from Strava API. + * + * @author Justin Foell + * @param array $args { + * Array of arguments. + * + * @type string $client_id Client ID of athlete to retrieve for. + * @type int|null $quantity Number of records to retrieve (optional). + * } + * @return array Array of segments. + * @since 2.9.0 + */ + public function get_starred_segments( $args ) { + $api = WPStrava::get_instance()->get_api( $args['client_id'] ); + + $get_args = array(); + + if ( ! empty( $args['quantity'] ) && is_numeric( $args['quantity'] ) ) { + $get_args['per_page'] = $args['quantity']; + } + + $data = $api->get( 'segments/starred', $get_args ); + + if ( is_array( $data ) ) { + return $data; + } + + return array(); + } + + /** + * Conditionally display a link based on settings. + * + * @param int $segments_id Strava Segments ID + * @param string $text Text (or HTML) that is the content of link. + * @param string $title Title attribute (default empty). + * @return string Text with link + * @author Justin Foell + * @since 2.9.0 + */ + public function get_segments_link( $segments_id, $text, $title = '' ) { + if ( WPStrava::is_rest_request() || WPStrava::get_instance()->settings->no_link ) { + return $text; + } + $url = esc_url( self::SEGMENTS_URL . $segments_id ); + $title_attr = $title ? " title='" . esc_attr( $title ) . "'" : ''; + return "{$text}"; + } +} diff --git a/src/WPStrava/SegmentsRenderer.php b/src/WPStrava/SegmentsRenderer.php new file mode 100644 index 0000000..645bf2b --- /dev/null +++ b/src/WPStrava/SegmentsRenderer.php @@ -0,0 +1,125 @@ + + * @since 2.9.0 + */ +class WPStrava_SegmentsRenderer { + + /** + * Get the HTML for a single segment. + * + * @param array $atts + * @return string HTML for an segment. + * @author Justin Foell + * @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 ); + + $segments = WPStrava::get_instance()->segments; + $segment_details = null; + + try { + $segment_details = $segments->get_segments( $atts['client_id'], $atts['id'] ); + } catch ( WPStrava_Exception $e ) { + return $e->to_html(); + } + + $segments_output = ''; + if ( $segment_details ) { + $segments_output .= '
'; + if ( ! $atts['image_only'] ) { + $segments_output .= $this->get_table( $segment_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 ); + + $segments_output .= $segments->get_segments_link( + $segment_details->id, + WPStrava_StaticMap::get_image_tag( $segment_details, $map_height, $map_width, $atts['markers'], $segment_details->name ), + $segment_details->name + ); + + if ( ! empty( $segment_details->description ) ) { + $segments_output .= '
' . esc_html( $segment_details->description ) . '
'; + } + + $segments_output .= '
'; + } // End if( $segment_details ). + return $segments_output; + } + + /** + * The the segment details in in HTML table. + * + * @param stdClass $segment_details Segment details from the segment class. + * @param string $som System of measure (english/metric). + * @return string HTML Table of segment details. + * @author Justin Foell + * @author Sebastian Erb + * @since 2.9.0 + * @TODO FIXME + */ + private function get_table( $segment_details, $som ) { + $strava_som = WPStrava_SOM::get_som( $som ); + $elevation_title = '' . __( 'Elevation Gain', 'wp-strava' ) . ''; + $elevation = ' +
' . $strava_som->elevation( $segment_details->total_elevation_gain ) . '
+
' . $strava_som->get_elevation_label() . '
+ '; + + if ( WPStrava::get_instance()->settings->hide_elevation ) { + $elevation_title = ''; + $elevation = ''; + } + + return ' + + + + + + + ' . $elevation_title . ' + + + + + + ' . $elevation . ' + + +
' . __( 'Elapsed Time', 'wp-strava' ) . '' . __( 'Moving Time', 'wp-strava' ) . '' . __( 'Distance', 'wp-strava' ) . '
+
' . $strava_som->distance( $segment_details->distance ) . '
+
' . $strava_som->get_distance_label() . '
+
+ '; + } +}