Merge pull request #33 from cmanon/feature/19-latest-map-shortcode

Feature/19 latest map shortcode
This commit is contained in:
Justin Foell
2019-11-01 13:37:02 -05:00
committed by GitHub
6 changed files with 195 additions and 126 deletions
+3
View File
@@ -1,6 +1,9 @@
{
"name": "wpackagist-plugin/wp-strava",
"description": "Show your strava.com activity on your WordPress site.",
"homepage": "https://wordpress.org/plugins/wp-strava/",
"license": "GPL-2.0+",
"type": "wordpress-plugin",
"require": {
"php": ">=5.2.4"
},
+1
View File
@@ -173,5 +173,6 @@ class WPStrava {
new WPStrava_ActivityShortcode();
new WPStrava_LatestActivitiesShortcode();
new WPStrava_RouteShortcode();
new WPStrava_LatestMapShortcode();
}
}
+129
View File
@@ -0,0 +1,129 @@
<?php
class WPStrava_LatestMap {
public static function get_map_html( $args ) {
$build_new = false;
$defaults = array(
'client_id' => WPStrava::get_instance()->settings->get_default_id(),
'strava_club_id' => null,
'distance_min' => 0,
);
$args = wp_parse_args( $args, $defaults );
$id = empty( $args['strava_club_id'] ) ? $args['client_id'] : $args['strava_club_id'];
// Try our transient first.
$activity_transient = get_transient( 'strava_latest_map_activity_' . $id );
$activity_option = get_option( 'strava_latest_map_activity_' . $id );
$activity = $activity_transient ? $activity_transient : null;
if ( ! $activity || empty( $activity->map ) ) {
$strava_activity = WPStrava::get_instance()->activity;
$activities = array();
try {
$activities = $strava_activity->get_activities( $args['client_id'], $args['strava_club_id'] );
} catch ( WPStrava_Exception $e ) {
// If athlete_token is still set, warn about that first and foremost.
if ( isset( $args['athlete_token'] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
echo wp_kses_post( __( 'The <code>athlete_token</code> parameter is deprecated as of WP-Strava version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' ) );
} else {
echo $e->to_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Debug only.
}
}
if ( ! empty( $activities ) ) {
if ( ! empty( $args['distance_min'] ) ) {
$activities = $strava_activity->get_activities_longer_than( $activities, $args['distance_min'] );
}
$activity = current( $activities );
// 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;
self::update_activity( $id, $activity );
}
// Update the transient if it needs updating.
if ( empty( $activity_transient->id ) || $activity->id != $activity_transient->id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
self::update_activity_transient( $id, $activity );
}
}
}
if ( $activity ) {
echo empty( $activity->map ) ?
// Translators: Text with activity name shown in place of image if not available.
esc_html( sprintf( __( 'Map not available for activity "%s"', 'wp-strava' ), $activity->name ) ) :
"<a title='" . esc_attr( $activity->name ) . "' href='" . esc_attr( WPStrava_Activity::ACTIVITIES_URL . $activity->id ) . "'>" .
self::get_static_image( $id, $activity, $build_new ) . // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Image OK.
'</a>';
}
}
/**
* Get image for specific activity using Static Maps class.
*
* @author Justin Foell <justin@foell.org>
* @param string $id Client ID or Club ID.
* @param object $activity Activity to get image for.
* @param boolean $build_new Whether to refresh the image from cache.
* @return string Image tag.
*/
private static function get_static_image( $id, $activity, $build_new ) {
$img = get_option( 'strava_latest_map_' . $id );
if ( $build_new || ! $img ) {
$img = WPStrava_StaticMap::get_image_tag( $activity );
self::update_map( $id, $img );
}
return $img;
}
/**
* Update map in option to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id Client ID or Club ID.
* @param string $img Image tag.
*/
private static function update_map( $id, $img ) {
update_option( 'strava_latest_map_' . $id, $img );
}
/**
* Update activity in option to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id Client ID or Club ID.
* @param object $activity stdClass Strava activity object.
*/
private static function update_activity( $id, $activity ) {
update_option( 'strava_latest_map_activity_' . $id, $activity );
}
/**
* Update activity in transient to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id CLient ID or Club ID.
* @param object $activity stdClass Strava activity object.
*/
private static function update_activity_transient( $id, $activity ) {
set_transient( 'strava_latest_map_activity_' . $id, $activity, HOUR_IN_SECONDS );
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
/**
* Latest Map Shortcode [latest_map].
* @package WPStrava
*/
/**
* Latest Map Shortcode class.
*
* @author Justin Foell <justin@foell.org>
* @since 2.0.1
*/
class WPStrava_LatestMapShortcode {
/**
* Constructor.
*
* @author Justin Foell <justin@foell.org>
* @since 2.0.1
*/
public function __construct() {
add_shortcode( 'latest_map', array( $this, 'handler' ) );
}
/**
* Shortcode handler for [latest_map].
*
* [latest_map som=metric distance_min=10 client_id=xxx|strava_club_id=yyy]
*
* @param array $atts Array of attributes (client_id, som, etc.).
* @return string Shortcode output
* @author Justin Foell <justin@foell.org>
* @since 2.0.1
*/
public function handler( $atts ) {
return WPStrava_LatestMap::get_map_html( $atts );
}
}
+19 -126
View File
@@ -24,30 +24,30 @@ class WPStrava_LatestMapWidget extends WP_Widget {
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
<?php // Translator: Widget Title. ?>
<?php esc_html_e( 'Title:', 'wp-strava' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'client_id' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'client_id' ); ?>">
<label for="<?php echo esc_attr( $this->get_field_id( 'client_id' ) ); ?>"><?php esc_html_e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo esc_attr( $this->get_field_name( 'client_id' ) ); ?>">
<?php foreach ( $all_ids as $id => $nickname ) : ?>
<option value="<?php echo $id; ?>"<?php selected( $id, $client_id ); ?>><?php echo $nickname; ?></option>
<option value="<?php echo esc_attr( $id ); ?>"<?php selected( $id, $client_id ); ?>><?php echo esc_html( $nickname ); ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>">
<label for="<?php echo esc_attr( $this->get_field_id( 'distance_min' ) ); ?>">
<?php // Translators: Label for minimum distance input. ?>
<?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?>
<?php echo esc_html( sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ) ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'distance_min' ); ?>" name="<?php echo $this->get_field_name( 'distance_min' ); ?>" type="text" value="<?php echo $distance_min; ?>" />
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'distance_min' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'distance_min' ) ); ?>" type="text" value="<?php echo esc_attr( $distance_min ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php esc_html_e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'strava_club_id' ); ?>" name="<?php echo $this->get_field_name( 'strava_club_id' ); ?>" type="text" value="<?php echo $strava_club_id; ?>" />
<label for="<?php echo esc_attr( $this->get_field_id( 'strava_club_id' ) ); ?>"><?php esc_html_e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'strava_club_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'strava_club_id' ) ); ?>" type="text" value="<?php echo esc_attr( $strava_club_id ); ?>" />
</p>
<?php
}
@@ -70,128 +70,21 @@ class WPStrava_LatestMapWidget extends WP_Widget {
*/
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] );
$client_id = isset( $instance['client_id'] ) ? $instance['client_id'] : WPStrava::get_instance()->settings->get_default_id();
$distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] );
$strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id'];
$build_new = false;
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] );
$id = empty( $strava_club_id ) ? $client_id : $strava_club_id;
// Try our transient first.
$activity_transient = get_transient( 'strava_latest_map_activity_' . $id );
$activity_option = get_option( 'strava_latest_map_activity_' . $id );
$activity = $activity_transient ? $activity_transient : null;
$activities_args = array(
'client_id' => isset( $instance['client_id'] ) ? $instance['client_id'] : null,
'strava_club_id' => isset( $instance['strava_club_id'] ) ? $instance['strava_club_id'] : null,
'distance_min' => isset( $instance['distance_min'] ) ? absint( $instance['distance_min'] ) : 0,
);
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget OK.
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 = array();
try {
$activities = $strava_activity->get_activities( $client_id, $strava_club_id );
} catch ( WPStrava_Exception $e ) {
// If athlete_token is still set, warn about that first and foremost.
if ( isset( $instance['athlete_token'] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
echo wp_kses_post( __( 'The <code>athlete_token</code> parameter is deprecated as of WP-Strava version 2 and should be replaced with <code>client_id</code>.', 'wp-strava' ) );
} else {
echo $e->to_html();
}
}
if ( ! empty( $activities ) ) {
if ( ! empty( $distance_min ) ) {
$activities = $strava_activity->get_activities_longer_than( $activities, $distance_min );
}
$activity = current( $activities );
// 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;
$this->update_activity( $id, $activity );
}
// Update the transient if it needs updating.
if ( empty( $activity_transient->id ) || $activity->id != $activity_transient->id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
$this->update_activity_transient( $id, $activity );
}
}
}
if ( $activity ) {
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 WPStrava_LatestMap::get_map_html( $activities_args );
echo $args['after_widget'];
}
/**
* Get image for specific activity using Static Maps class.
*
* @author Justin Foell <justin@foell.org>
* @param string $id Client ID or Club ID.
* @param object $activity Activity to get image for.
* @param boolean $build_new Whether to refresh the image from cache.
* @return string Image tag.
*/
private function get_static_image( $id, $activity, $build_new ) {
$img = get_option( 'strava_latest_map_' . $id );
if ( $build_new || ! $img ) {
$img = WPStrava_StaticMap::get_image_tag( $activity );
$this->update_map( $id, $img );
}
return $img;
}
/**
* Update map in option to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id Client ID or Club ID.
* @param string $img Image tag.
*/
private function update_map( $id, $img ) {
update_option( 'strava_latest_map_' . $id, $img );
}
/**
* Update activity in option to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id Client ID or Club ID.
* @param object $activity stdClass Strava activity object.
*/
private function update_activity( $id, $activity ) {
update_option( 'strava_latest_map_activity_' . $id, $activity );
}
/**
* Update activity in transient to cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
* @param string $id CLient ID or Club ID.
* @param object $activity stdClass Strava activity object.
*/
private function update_activity_transient( $id, $activity ) {
set_transient( 'strava_latest_map_activity_' . $id, $activity, HOUR_IN_SECONDS );
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
+5
View File
@@ -92,6 +92,11 @@ WP-Strava caches activity for one hour so your site doesn't hit the Strava API o
== Changelog ==
= 2.0.1 =
Added latest_map shortcode https://wordpress.org/support/topic/show-latest-map-not-in-widget/
= 2.0.0 =
Added new Strava "refresh tokens" ala https://developers.strava.com/docs/oauth-updates/#migration-instructions