From c90d5c6d2896165ee1ac946788faf4ed4fb73cc6 Mon Sep 17 00:00:00 2001
From: Justin Foell <630830+jrfoell@users.noreply.github.com>
Date: Fri, 30 Apr 2021 13:15:16 -0500
Subject: [PATCH] Added calories
---
readme.txt | 2 +-
src/WPStrava/ActivitiesListRenderer.php | 9 ++++++++-
src/WPStrava/ActivityRenderer.php | 12 ++++++++++++
src/WPStrava/AuthRefresh.php | 2 +-
src/WPStrava/SOM.php | 22 ++++++++++++++++++++++
tests/WPStrava/SOMEnglishTest.php | 11 +++++++++++
6 files changed, 55 insertions(+), 3 deletions(-)
diff --git a/readme.txt b/readme.txt
index 6c859de..6829fc0 100755
--- a/readme.txt
+++ b/readme.txt
@@ -122,7 +122,7 @@ On the WP-Strava settings page you cannot currently remove and add another athle
= 2.9.0 =
Switched Activities List to display moving time instead of elapsed time https://wordpress.org/support/topic/moving-time-instead-of-elapsed-time/
-
+Added calories burned (when available) to Activity and Activities List https://wordpress.org/support/topic/calorie/
= 2.8.0 =
diff --git a/src/WPStrava/ActivitiesListRenderer.php b/src/WPStrava/ActivitiesListRenderer.php
index 2416edb..c80d530 100644
--- a/src/WPStrava/ActivitiesListRenderer.php
+++ b/src/WPStrava/ActivitiesListRenderer.php
@@ -48,7 +48,9 @@ class WPStrava_ActivitiesListRenderer {
}
$response = "
";
- foreach ( $activities as $activity ) {
+ foreach ( $activities as $activity_summary ) {
+ // Re-get single activity for greater detail (will be cached).
+ $activity = $strava_activity->get_activity( $atts['client_id'], $activity_summary->id );
$response .= "- ";
$response .= empty( $activity->id ) ?
$activity->name : $strava_activity->get_activity_link( $activity->id, $activity->name );
@@ -81,6 +83,11 @@ class WPStrava_ActivitiesListRenderer {
$response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $activity->total_elevation_gain ), $som->get_elevation_label() );
}
+ if ( $activity->calories ) {
+ // Translators: "burning 200 calories."
+ $response .= sprintf( __( ' burning %1$s calories.', 'wp-strava' ), $som->calories( $activity->calories ) );
+ }
+
$response .= '
';
}
$response .= '
';
diff --git a/src/WPStrava/ActivityRenderer.php b/src/WPStrava/ActivityRenderer.php
index 59e2656..26ecef6 100644
--- a/src/WPStrava/ActivityRenderer.php
+++ b/src/WPStrava/ActivityRenderer.php
@@ -90,6 +90,8 @@ class WPStrava_ActivityRenderer {
';
$avg_speed = '';
$max_speed = '';
+ $calories_title = '';
+ $calories = '';
switch ( $strava_activitytype ) {
case WPStrava_ActivityType::TYPE_GROUP_PACE:
@@ -130,6 +132,14 @@ class WPStrava_ActivityRenderer {
$elevation = '';
}
+ if ( $activity_details->calories ) {
+ $calories_title = '' . __( 'Calories Burned', 'wp-strava' ) . ' | ';
+ $calories = '
+ ' . $strava_som->calories( $activity_details->calories ) . '
+ ' . $strava_som->get_calories_label() . '
+ | ';
+ }
+
return '
@@ -140,6 +150,7 @@ class WPStrava_ActivityRenderer {
' . $avg_title . '
' . $max_title . '
' . $elevation_title . '
+ ' . $calories_title . '
@@ -159,6 +170,7 @@ class WPStrava_ActivityRenderer {
' . $avg_speed . '
' . $max_speed . '
' . $elevation . '
+ ' . $calories . '
diff --git a/src/WPStrava/AuthRefresh.php b/src/WPStrava/AuthRefresh.php
index 1bdd6e3..ba9bbbf 100644
--- a/src/WPStrava/AuthRefresh.php
+++ b/src/WPStrava/AuthRefresh.php
@@ -65,7 +65,7 @@ class WPStrava_AuthRefresh extends WPStrava_Auth {
'client_id' => $client_id,
'redirect_uri' => $this->get_redirect_param(),
'approval_prompt' => 'auto',
- 'scope' => 'activity:read,read',
+ 'scope' => 'read,activity:read',
),
$this->auth_url
);
diff --git a/src/WPStrava/SOM.php b/src/WPStrava/SOM.php
index 0fc6e81..907d27b 100644
--- a/src/WPStrava/SOM.php
+++ b/src/WPStrava/SOM.php
@@ -77,4 +77,26 @@ abstract class WPStrava_SOM {
return "{$min}:{$sec}";
}
+
+ /**
+ * Format calories to add appropriate comma.
+ *
+ * @param string|int $kcal Kilocalories
+ * @author Justin Foell
+ * @since 2.9.0
+ */
+ public function calories( $kcal ) {
+ return number_format_i18n( $kcal, 0 );
+ }
+
+ /**
+ * Abbreviated label for kilocalories.
+ *
+ * @return string
+ * @author Justin Foell
+ * @since 2.9.0
+ */
+ public function get_calories_label() {
+ return __( 'kcal', 'wp-strava' );
+ }
}
diff --git a/tests/WPStrava/SOMEnglishTest.php b/tests/WPStrava/SOMEnglishTest.php
index 9124fb3..26e3d63 100644
--- a/tests/WPStrava/SOMEnglishTest.php
+++ b/tests/WPStrava/SOMEnglishTest.php
@@ -96,4 +96,15 @@ class WPStrava_SOMEnglishTest extends TestCase {
$this->assertEquals( '61:24:31', $this->som->time( 221071 ) );
}
+ /**
+ * Test that 1304 calories is 1,304 using both string and int inputs.
+ *
+ * @author Justin Foell
+ * @since 2.9.0
+ */
+ public function test_calories() {
+ $this->assertEquals( '1,304', $this->som->calories( '1304' ) );
+ $this->assertEquals( '1,304', $this->som->calories( 1304 ) );
+ }
+
}