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] 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'] ) ) {