2011-08-23 09:34:28 -05:00
|
|
|
<?php
|
|
|
|
|
/*
|
2018-01-26 13:27:57 -06:00
|
|
|
* Activity is a class wrapper for the Strava REST API functions.
|
2011-08-23 09:34:28 -05:00
|
|
|
*/
|
2018-01-26 13:27:57 -06:00
|
|
|
class WPStrava_Activity {
|
2012-06-12 17:36:49 -05:00
|
|
|
|
2020-01-31 11:48:35 -06:00
|
|
|
const ACTIVITIES_URL = 'https://strava.com/activities/';
|
|
|
|
|
const ATHLETES_URL = 'https://strava.com/athletes/';
|
2017-05-26 10:53:24 -05:00
|
|
|
|
2017-08-11 17:28:29 -05:00
|
|
|
/**
|
|
|
|
|
* Get single activity by ID.
|
|
|
|
|
*
|
2019-06-03 16:07:15 -05:00
|
|
|
* @param string $client_id Client ID of athlete to retrieve for
|
2018-01-26 13:27:57 -06:00
|
|
|
* @param int $activity_id ID of activity to retrieve.
|
2019-06-03 16:07:15 -05:00
|
|
|
* @return object stdClass Representing this activity.
|
2019-02-01 14:17:22 -06:00
|
|
|
* @author Justin Foell <justin@foell.org>
|
2017-08-11 17:28:29 -05:00
|
|
|
*/
|
2019-06-03 16:07:15 -05:00
|
|
|
public function get_activity( $client_id, $activity_id ) {
|
|
|
|
|
return WPStrava::get_instance()->get_api( $client_id )->get( "activities/{$activity_id}" );
|
2018-01-26 13:27:57 -06:00
|
|
|
}
|
2017-05-26 10:53:24 -05:00
|
|
|
|
2017-08-11 17:28:29 -05:00
|
|
|
/**
|
|
|
|
|
* Get activity list from Strava API.
|
|
|
|
|
*
|
2019-02-01 14:17:22 -06:00
|
|
|
* @author Justin Foell <justin@foell.org>
|
2020-04-24 16:07:19 -05:00
|
|
|
* @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).
|
|
|
|
|
* }
|
2018-04-27 21:19:27 -05:00
|
|
|
* @return array Array of activities.
|
2017-08-11 17:28:29 -05:00
|
|
|
*/
|
2020-04-24 16:07:19 -05:00
|
|
|
public function get_activities( $args ) {
|
|
|
|
|
$api = WPStrava::get_instance()->get_api( $args['client_id'] );
|
2013-04-07 17:56:07 -05:00
|
|
|
|
2020-04-24 16:07:19 -05:00
|
|
|
$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'] );
|
2017-05-26 10:53:24 -05:00
|
|
|
|
2020-04-24 16:07:19 -05:00
|
|
|
$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;
|
2014-09-11 15:56:35 -05:00
|
|
|
|
2011-08-23 09:34:28 -05:00
|
|
|
//Get the json results using the constructor specified values.
|
2020-04-24 16:07:19 -05:00
|
|
|
if ( ! empty( $args['strava_club_id'] ) && is_numeric( $args['strava_club_id'] ) ) {
|
|
|
|
|
$data = $api->get( "clubs/{$args['strava_club_id']}/activities", $get_args );
|
2011-08-23 09:34:28 -05:00
|
|
|
} else {
|
2020-04-24 16:07:19 -05:00
|
|
|
$data = $api->get( 'athlete/activities', $get_args );
|
2011-08-23 09:34:28 -05:00
|
|
|
}
|
2013-04-07 17:56:07 -05:00
|
|
|
|
2017-08-11 17:28:29 -05:00
|
|
|
if ( is_array( $data ) ) {
|
2014-09-11 10:48:03 -05:00
|
|
|
return $data;
|
2017-08-11 17:28:29 -05:00
|
|
|
}
|
2017-05-26 10:53:24 -05:00
|
|
|
|
|
|
|
|
return array();
|
|
|
|
|
|
2018-01-26 13:27:57 -06:00
|
|
|
}
|
2013-04-07 18:09:50 -05:00
|
|
|
|
2018-01-26 13:27:57 -06:00
|
|
|
/**
|
2019-02-01 14:17:22 -06:00
|
|
|
* Get activities with a distance longer than specified length.
|
2018-01-26 13:27:57 -06:00
|
|
|
*
|
|
|
|
|
* @param array $activities
|
|
|
|
|
* @param float $dist Distance in default system of measure (km/mi).
|
|
|
|
|
* @return void
|
2019-02-01 14:17:22 -06:00
|
|
|
* @author Justin Foell <justin@foell.org>
|
2018-01-26 13:27:57 -06:00
|
|
|
*/
|
|
|
|
|
public function get_activities_longer_than( $activities, $dist ) {
|
2017-12-26 14:18:13 -06:00
|
|
|
$som = WPStrava_SOM::get_som();
|
2013-04-07 18:09:50 -05:00
|
|
|
$meters = $som->distance_inverse( $dist );
|
|
|
|
|
|
2018-01-26 13:27:57 -06:00
|
|
|
$long_activities = array();
|
|
|
|
|
foreach ( $activities as $activity_info ) {
|
|
|
|
|
if ( $activity_info->distance > $meters ) {
|
|
|
|
|
$long_activities[] = $activity_info;
|
2013-04-07 18:09:50 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 10:53:24 -05:00
|
|
|
|
2018-01-26 13:27:57 -06:00
|
|
|
return $long_activities;
|
2013-04-07 18:09:50 -05:00
|
|
|
}
|
2018-01-26 13:27:57 -06:00
|
|
|
}
|