From 335ea74d07e5bdde74a77088304705e64245d77f Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:15:27 -0500 Subject: [PATCH 1/6] Renamed LatestActivities classes to ActivitiesList --- readme.txt | 3 +++ src/WPStrava.php | 4 ++-- ...atestActivities.php => ActivitiesList.php} | 12 +++++++++- ...rtcode.php => ActivitiesListShortcode.php} | 6 ++--- ...iesWidget.php => ActivitiesListWidget.php} | 23 ++++++++++++++----- 5 files changed, 36 insertions(+), 12 deletions(-) rename src/WPStrava/{LatestActivities.php => ActivitiesList.php} (93%) rename src/WPStrava/{LatestActivitiesShortcode.php => ActivitiesListShortcode.php} (90%) rename src/WPStrava/{LatestActivitiesWidget.php => ActivitiesListWidget.php} (88%) diff --git a/readme.txt b/readme.txt index 17b5585..2d290da 100755 --- a/readme.txt +++ b/readme.txt @@ -103,6 +103,9 @@ On the WP-Strava settings page you cannot currently remove and add another athle == Changelog == += 2.3.0 = +Renamed LatestActivities classes to ActivitiesList. + = 2.2.0 = Added rudimentary gutenberg block for single Activity. Changed all Strava links to HTTPS. diff --git a/src/WPStrava.php b/src/WPStrava.php index 66e7fe6..5b8c18b 100644 --- a/src/WPStrava.php +++ b/src/WPStrava.php @@ -166,7 +166,7 @@ class WPStrava { * Register the widgets. */ public function register_widgets() { - register_widget( 'WPStrava_LatestActivitiesWidget' ); + register_widget( 'WPStrava_ActivitiesListWidget' ); register_widget( 'WPStrava_LatestMapWidget' ); } @@ -176,7 +176,7 @@ class WPStrava { public function register_shortcodes() { // Initialize short code classes. new WPStrava_ActivityShortcode(); - new WPStrava_LatestActivitiesShortcode(); + new WPStrava_ActivitiesListShortcode(); new WPStrava_RouteShortcode(); new WPStrava_LatestMapShortcode(); } diff --git a/src/WPStrava/LatestActivities.php b/src/WPStrava/ActivitiesList.php similarity index 93% rename from src/WPStrava/LatestActivities.php rename to src/WPStrava/ActivitiesList.php index 3a96618..807f922 100644 --- a/src/WPStrava/LatestActivities.php +++ b/src/WPStrava/ActivitiesList.php @@ -1,6 +1,16 @@ + * @since 2.3.0 + */ +class WPStrava_ActivitiesList { public static function get_activities_html( $args ) { if ( isset( $args['athlete_token'] ) ) { // Translators: Message shown when using deprecated athlete_token parameter. diff --git a/src/WPStrava/LatestActivitiesShortcode.php b/src/WPStrava/ActivitiesListShortcode.php similarity index 90% rename from src/WPStrava/LatestActivitiesShortcode.php rename to src/WPStrava/ActivitiesListShortcode.php index db24387..2fa13d4 100644 --- a/src/WPStrava/LatestActivitiesShortcode.php +++ b/src/WPStrava/ActivitiesListShortcode.php @@ -8,9 +8,9 @@ * Latest Activities Shortcode class (converted from LatestRides). * * @author Justin Foell - * @since 1.3.0 + * @since 2.3.0 */ -class WPStrava_LatestActivitiesShortcode { +class WPStrava_ActivitiesListShortcode { /** * Whether or not to enqueue styles (if shortcode is present). @@ -44,7 +44,7 @@ class WPStrava_LatestActivitiesShortcode { */ public function handler( $atts ) { $this->add_script = true; - return WPStrava_LatestActivities::get_activities_html( $atts ); + return WPStrava_ActivitiesList::get_activities_html( $atts ); } /** diff --git a/src/WPStrava/LatestActivitiesWidget.php b/src/WPStrava/ActivitiesListWidget.php similarity index 88% rename from src/WPStrava/LatestActivitiesWidget.php rename to src/WPStrava/ActivitiesListWidget.php index 06b489a..e6087d0 100644 --- a/src/WPStrava/LatestActivitiesWidget.php +++ b/src/WPStrava/ActivitiesListWidget.php @@ -1,16 +1,27 @@ + * @since 2.3.0 + */ +class WPStrava_ActivitiesListWidget extends WP_Widget { public function __construct() { $widget_ops = array( - 'classname' => 'LatestActivitiesWidget', - 'description' => __( 'Will show your latest activities from strava.com.', 'wp-strava' ), + 'classname' => 'wp-strava-activities-list-widget', + 'description' => __( 'Show a list of activities from strava.com.', 'wp-strava' ), ); - parent::__construct( 'wp-strava', __( 'Strava Latest Activities List', 'wp-strava' ), $widget_ops ); + parent::__construct( 'wp-strava', __( 'Strava Activities List', 'wp-strava' ), $widget_ops ); add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) ); } @@ -35,7 +46,7 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget { if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } - echo WPStrava_LatestActivities::get_activities_html( $activities_args ); + echo WPStrava_ActivitiesList::get_activities_html( $activities_args ); echo $args['after_widget']; // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped } @@ -83,4 +94,4 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget { Date: Fri, 24 Apr 2020 10:25:11 -0500 Subject: [PATCH 2/6] Added exception handling to authorization process --- readme.txt | 2 ++ src/WPStrava/Auth.php | 6 +++++- src/WPStrava/Exception.php | 10 +++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/readme.txt b/readme.txt index 2d290da..ee931fb 100755 --- a/readme.txt +++ b/readme.txt @@ -105,6 +105,8 @@ On the WP-Strava settings page you cannot currently remove and add another athle = 2.3.0 = Renamed LatestActivities classes to ActivitiesList. +Added exception handling to authorization process. + = 2.2.0 = Added rudimentary gutenberg block for single Activity. diff --git a/src/WPStrava/Auth.php b/src/WPStrava/Auth.php index d286040..f2d0d76 100644 --- a/src/WPStrava/Auth.php +++ b/src/WPStrava/Auth.php @@ -118,7 +118,11 @@ abstract class WPStrava_Auth { $data = $this->add_initial_params( $data ); - $strava_info = $this->token_request( $data ); + try { + $strava_info = $this->token_request( $data ); + } catch ( WPStrava_Exception $e ) { + wp_die( $e->to_html() ); // phpcs:ignore -- Debug only. + } if ( isset( $strava_info->access_token ) ) { $settings->add_id( $client_id ); diff --git a/src/WPStrava/Exception.php b/src/WPStrava/Exception.php index 325c4e4..def4c4d 100644 --- a/src/WPStrava/Exception.php +++ b/src/WPStrava/Exception.php @@ -1,10 +1,8 @@ -=' ) ) { /* * Exception class for error handling/display. + * + * For creation use `new WPStrava_Exception()` or `WPStrava_Exception::from_wp_error( $error )`. + * For display use $e->to_html(), echo/(s)printf $e, or strval( $e ). */ class WPStrava_Exception extends WPStrava_Abstract_Exception { @@ -80,7 +81,7 @@ class WPStrava_Exception extends WPStrava_Abstract_Exception { * @author Justin Foell * @since 1.6.0 */ - public function get_formatted_message( $exception ) { + private function get_formatted_message( $exception ) { $code = $exception->getCode(); if ( $exception->getPrevious() ) { @@ -100,4 +101,3 @@ class WPStrava_Exception extends WPStrava_Abstract_Exception { return sprintf( __( 'WP Strava ERROR %1$s', 'wp-strava' ), $exception->getMessage() ); } } -// phpcs:enable From 0eec8b25c629b6190aa548dac2caec845853fbcd Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:33:56 -0500 Subject: [PATCH 3/6] Fixed strings and documentation Latest -> List --- src/WPStrava/ActivitiesListShortcode.php | 4 ++-- src/WPStrava/ActivitiesListWidget.php | 14 +++++--------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/WPStrava/ActivitiesListShortcode.php b/src/WPStrava/ActivitiesListShortcode.php index 2fa13d4..8749366 100644 --- a/src/WPStrava/ActivitiesListShortcode.php +++ b/src/WPStrava/ActivitiesListShortcode.php @@ -1,11 +1,11 @@ * @since 2.3.0 diff --git a/src/WPStrava/ActivitiesListWidget.php b/src/WPStrava/ActivitiesListWidget.php index e6087d0..fea2fa9 100644 --- a/src/WPStrava/ActivitiesListWidget.php +++ b/src/WPStrava/ActivitiesListWidget.php @@ -1,15 +1,11 @@ * @since 2.3.0 @@ -33,7 +29,7 @@ class WPStrava_ActivitiesListWidget extends WP_Widget { /** @see WP_Widget::widget */ public function widget( $args, $instance ) { - $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activities', 'wp-strava' ) : $instance['title'] ); + $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activities List', 'wp-strava' ) : $instance['title'] ); $activities_args = array( 'client_id' => isset( $instance['client_id'] ) ? $instance['client_id'] : null, @@ -64,7 +60,7 @@ class WPStrava_ActivitiesListWidget extends WP_Widget { /** @see WP_Widget::form */ public function form( $instance ) { - $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activities', 'wp-strava' ); + $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Activities List', 'wp-strava' ); $all_ids = WPStrava::get_instance()->settings->get_all_ids(); $client_id = isset( $instance['client_id'] ) ? esc_attr( $instance['client_id'] ) : WPStrava::get_instance()->settings->get_default_id(); $strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : ''; From b75f9809d4f0ebffebf963a9d43962a2ea308f9d Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:53:08 -0500 Subject: [PATCH 4/6] Bumped version to 2.3.0 --- readme.txt | 2 +- wp-strava.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.txt b/readme.txt index ee931fb..dbf14f8 100755 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: cmanon, jrfoell, lancewillett, dlintott, sebastianerb Tags: strava, activity, bicycle, cycling, biking, running, run, swimming, swim, paddle, kayak, gps, shortcode, widget, plugin Requires at least: 4.6 -Tested up to: 5.3 +Tested up to: 5.4 Stable tag: 2.2.0 Requires PHP: 5.3 License: GPLv2 or later diff --git a/wp-strava.php b/wp-strava.php index 5b1810a..e4b06b7 100755 --- a/wp-strava.php +++ b/wp-strava.php @@ -3,7 +3,7 @@ * Plugin Name: WP Strava * Plugin URI: https://wordpress.org/plugins/wp-strava/ * Description: Show your strava.com activity on your WordPress site. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license. - * Version: 2.2.0 + * Version: 2.3.0 * Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott, Sebastian Erb * License: GPL2 * Text Domain: wp-strava @@ -27,7 +27,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -define( 'WPSTRAVA_PLUGIN_VERSION', '2.2.0' ); +define( 'WPSTRAVA_PLUGIN_VERSION', '2.3.0' ); define( 'WPSTRAVA_PLUGIN_FILE', __FILE__ ); define( 'WPSTRAVA_PLUGIN_DIR', trailingslashit( dirname( __FILE__ ) ) ); define( 'WPSTRAVA_PLUGIN_URL', plugins_url( '/', __FILE__ ) ); From 411f762865a8c381e4b3beb610cc807f41c65945 Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 10:53:32 -0500 Subject: [PATCH 5/6] Fixed Polyline include path --- src/WPStrava/StaticMap.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WPStrava/StaticMap.php b/src/WPStrava/StaticMap.php index 762e9f3..bd3b441 100644 --- a/src/WPStrava/StaticMap.php +++ b/src/WPStrava/StaticMap.php @@ -66,7 +66,7 @@ class WPStrava_StaticMap { * } */ private static function decode_start_finish( $enc ) { - require_once WPSTRAVA_PLUGIN_DIR . 'includes/Polyline.php'; + require_once WPSTRAVA_PLUGIN_DIR . 'src/Polyline.php'; $points = Polyline::decode( $enc ); $points = Polyline::pair( $points ); $start = $points[0]; From b67ac210babc141e4f1bf7ebce6eb7fdcc34fd7f Mon Sep 17 00:00:00 2001 From: Justin Foell <630830+jrfoell@users.noreply.github.com> Date: Fri, 24 Apr 2020 16:07:19 -0500 Subject: [PATCH 6/6] Added date_start & date_end params to Activities shortcode Switched get_activities to single args array param --- readme.md | 1 + readme.txt | 4 ++- src/WPStrava/API.php | 2 +- src/WPStrava/ActivitiesList.php | 4 ++- src/WPStrava/Activity.php | 47 +++++++++++++++++++++++++-------- src/WPStrava/LatestMap.php | 2 +- 6 files changed, 45 insertions(+), 15 deletions(-) create mode 120000 readme.md diff --git a/readme.md b/readme.md new file mode 120000 index 0000000..0d79d56 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +readme.txt \ No newline at end of file diff --git a/readme.txt b/readme.txt index dbf14f8..9d2c105 100755 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Contributors: cmanon, jrfoell, lancewillett, dlintott, sebastianerb Tags: strava, activity, bicycle, cycling, biking, running, run, swimming, swim, paddle, kayak, gps, shortcode, widget, plugin Requires at least: 4.6 Tested up to: 5.4 -Stable tag: 2.2.0 +Stable tag: 2.3.0 Requires PHP: 5.3 License: GPLv2 or later @@ -49,6 +49,8 @@ This also takes the same optional parameters as the [activity] shortcode above. * quantity - number of activities to show. * client_id - specify a different athlete (you can copy this value from https://www.strava.com/settings/api or the wp-strava settings page at /wp-admin/options-general.php?page=wp-strava-options). * strava_club_id - Will display activity from the specified Strava club ID instead of an athlete. +* date_start - Will display activities after specified date - must be [PHP DateTime strtotime compatible](https://www.php.net/manual/en/datetime.formats.php). +* date_end - Will display activities before the specified date - must be [PHP DateTime compatible](https://www.php.net/manual/en/datetime.formats.php). [latest_map] - shows a map of your latest activity. Takes the following optional parameters: diff --git a/src/WPStrava/API.php b/src/WPStrava/API.php index c67c9cd..cee2daa 100755 --- a/src/WPStrava/API.php +++ b/src/WPStrava/API.php @@ -83,7 +83,7 @@ class WPStrava_API { public function get( $uri, $args = null ) { // @see https://stackoverflow.com/a/3764390/2146022 - $arg_suffix = is_array( $args ) ? '_' . substr( md5( wp_json_encode( $args ) ), 0, 12 ) : ''; + $arg_suffix = is_array( $args ) && ! empty( $args ) ? '_' . substr( md5( wp_json_encode( $args ) ), 0, 12 ) : ''; $transient_key = 'strava_api_data_' . $this->client_id . '_' . $uri . $arg_suffix; diff --git a/src/WPStrava/ActivitiesList.php b/src/WPStrava/ActivitiesList.php index 807f922..6193ab1 100644 --- a/src/WPStrava/ActivitiesList.php +++ b/src/WPStrava/ActivitiesList.php @@ -22,6 +22,8 @@ class WPStrava_ActivitiesList { 'strava_club_id' => null, 'quantity' => 5, 'som' => WPStrava::get_instance()->settings->som, + 'date_start' => '', + 'date_end' => '', ); $args = wp_parse_args( $args, $defaults ); @@ -31,7 +33,7 @@ class WPStrava_ActivitiesList { $activities = array(); try { - $activities = $strava_activity->get_activities( $args['client_id'], $args['strava_club_id'], $args['quantity'] ); + $activities = $strava_activity->get_activities( $args ); } catch ( WPStrava_Exception $e ) { return $e->to_html(); } diff --git a/src/WPStrava/Activity.php b/src/WPStrava/Activity.php index 32f7d1b..e936779 100755 --- a/src/WPStrava/Activity.php +++ b/src/WPStrava/Activity.php @@ -23,23 +23,49 @@ class WPStrava_Activity { * Get activity list from Strava API. * * @author Justin Foell - * @param string $client_id Client ID of athlete to retrieve for - * @param int $club_id Club ID of all club riders (optional). - * @param int|null $quantity Number of records to retrieve (optional). + * @param array $args { + * Array of arguments. + * + * @type string $client_id Client ID of athlete to retrieve for + * @type int $strava_club_id Club ID of all club riders (optional). + * @type int|null $quantity Number of records to retrieve (optional). + * @type int|null $date_start Timestamp for filtering activities after a certain time (optional, negates $quantity). + * @type int|null $date_end Timestamp for filtering activities before a certain time (optional, negates $quantity). + * } * @return array Array of activities. */ - public function get_activities( $client_id, $club_id = null, $quantity = null ) { - $api = WPStrava::get_instance()->get_api( $client_id ); + public function get_activities( $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']; + } + + // Add start/end date (not supported for clubs). + if ( empty( $args['strava_club_id'] ) && ! empty( $args['date_start'] ) && ! empty( $args['date_end'] ) ) { + + // Check for valid dates. + if ( strtotime( $args['date_start'] ) && strtotime( $args['date_end'] ) ) { + unset( $get_args['per_page'] ); + + $localtime = new DateTimeZone( get_option( 'timezone_string' ) ); + $before_dt = new DateTime( $args['date_end'], $localtime ); + $after_dt = new DateTime( $args['date_start'], $localtime ); + + $get_args['before'] = $before_dt->format( 'U' ); + $get_args['after'] = $after_dt->format( 'U' ); + } + } $data = null; - $args = $quantity ? array( 'per_page' => $quantity ) : null; - //Get the json results using the constructor specified values. - if ( is_numeric( $club_id ) ) { - $data = $api->get( "clubs/{$club_id}/activities", $args ); + if ( ! empty( $args['strava_club_id'] ) && is_numeric( $args['strava_club_id'] ) ) { + $data = $api->get( "clubs/{$args['strava_club_id']}/activities", $get_args ); } else { - $data = $api->get( 'athlete/activities', $args ); + $data = $api->get( 'athlete/activities', $get_args ); } if ( is_array( $data ) ) { @@ -71,5 +97,4 @@ class WPStrava_Activity { return $long_activities; } - } diff --git a/src/WPStrava/LatestMap.php b/src/WPStrava/LatestMap.php index 9b1cb75..29866cd 100644 --- a/src/WPStrava/LatestMap.php +++ b/src/WPStrava/LatestMap.php @@ -16,7 +16,7 @@ class WPStrava_LatestMap { $activities = array(); try { - $activities = $strava_activity->get_activities( $args['client_id'], $args['strava_club_id'] ); + $activities = $strava_activity->get_activities( $args ); } catch ( WPStrava_Exception $e ) { // If athlete_token is still set, warn about that first and foremost. if ( isset( $args['athlete_token'] ) ) {