mirror of
https://github.com/10h30/wp-strava.git
synced 2026-07-11 18:56:18 +09:00
Segments WIP
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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 <justin@foell.org>
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/*
|
||||
* Segments is a class wrapper for the Strava REST API functions.
|
||||
*/
|
||||
|
||||
class WPStrava_Segments {
|
||||
|
||||
const SEGMENTS_URL = 'https://strava.com/segments/';
|
||||
|
||||
/**
|
||||
* Get single segment by ID.
|
||||
*
|
||||
* @param string $client_id Client ID of athlete to retrieve for.
|
||||
* @param int $segment_id ID of segment to retrieve.
|
||||
* @return object stdClass Representing this segment.
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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 <justin@foell.org>
|
||||
* @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 <justin@foell.org>
|
||||
* @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 "<a href='{$url}'{$title_attr}>{$text}</a>";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Segments Renderer.
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
/**
|
||||
* Segments class for shortcode and widget.
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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 <justin@foell.org>
|
||||
* @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 .= '<div id="segments-header-' . $atts['id'] . '" class="wp-strava-segments-container">';
|
||||
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 .= '<div class="wp-strava-segments-description">' . esc_html( $segment_details->description ) . '</div>';
|
||||
}
|
||||
|
||||
$segments_output .= '</div>';
|
||||
} // 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 <justin@foell.org>
|
||||
* @author Sebastian Erb <mail@sebastianerb.com>
|
||||
* @since 2.9.0
|
||||
* @TODO FIXME
|
||||
*/
|
||||
private function get_table( $segment_details, $som ) {
|
||||
$strava_som = WPStrava_SOM::get_som( $som );
|
||||
$elevation_title = '<th>' . __( 'Elevation Gain', 'wp-strava' ) . '</th>';
|
||||
$elevation = '<td data-label="' . __( 'Elevation Gain', 'wp-strava' ) . '">
|
||||
<div class="segment-details-table-info">' . $strava_som->elevation( $segment_details->total_elevation_gain ) . '</div>
|
||||
<div class="segment-details-table-units">' . $strava_som->get_elevation_label() . '</div>
|
||||
</td>';
|
||||
|
||||
if ( WPStrava::get_instance()->settings->hide_elevation ) {
|
||||
$elevation_title = '';
|
||||
$elevation = '';
|
||||
}
|
||||
|
||||
return '
|
||||
<table class="segment-details-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>' . __( 'Elapsed Time', 'wp-strava' ) . '</th>
|
||||
<th>' . __( 'Moving Time', 'wp-strava' ) . '</th>
|
||||
<th>' . __( 'Distance', 'wp-strava' ) . '</th>
|
||||
' . $elevation_title . '
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td data-label="' . __( 'Distance', 'wp-strava' ) . '">
|
||||
<div class="segment-details-table-info">' . $strava_som->distance( $segment_details->distance ) . '</div>
|
||||
<div class="segment-details-table-units">' . $strava_som->get_distance_label() . '</div>
|
||||
</td>
|
||||
' . $elevation . '
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user