mirror of
https://github.com/10h30/wp-strava.git
synced 2026-07-11 18:56:18 +09:00
Merge pull request #45 from cmanon/feature/activities-list-by-date-range
Feature/activities list by date range
This commit is contained in:
+9
-2
@@ -3,8 +3,8 @@
|
||||
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
|
||||
Stable tag: 2.2.0
|
||||
Tested up to: 5.4
|
||||
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:
|
||||
|
||||
@@ -103,6 +105,11 @@ On the WP-Strava settings page you cannot currently remove and add another athle
|
||||
|
||||
== Changelog ==
|
||||
|
||||
= 2.3.0 =
|
||||
Renamed LatestActivities classes to ActivitiesList.
|
||||
Added exception handling to authorization process.
|
||||
|
||||
|
||||
= 2.2.0 =
|
||||
Added rudimentary gutenberg block for single Activity.
|
||||
Changed all Strava links to HTTPS.
|
||||
|
||||
+2
-2
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Activities List.
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
class WPStrava_LatestActivities {
|
||||
/**
|
||||
* Activities List class for shortcode and widget.
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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.
|
||||
@@ -12,6 +22,8 @@ class WPStrava_LatestActivities {
|
||||
'strava_club_id' => null,
|
||||
'quantity' => 5,
|
||||
'som' => WPStrava::get_instance()->settings->som,
|
||||
'date_start' => '',
|
||||
'date_end' => '',
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
@@ -21,7 +33,7 @@ class WPStrava_LatestActivities {
|
||||
$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();
|
||||
}
|
||||
+5
-5
@@ -1,16 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Latest Activities Shortcode [activities].
|
||||
* Activities List Shortcode [activities].
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
/**
|
||||
* Latest Activities Shortcode class (converted from LatestRides).
|
||||
* Activities List Shortcode class (converted from LatestActivitiesShortcode).
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1,16 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* Activities List Widget.
|
||||
* @package WPStrava
|
||||
*/
|
||||
|
||||
/**
|
||||
* WP Strava Latest Activities Widget Class
|
||||
* Activities List Widget class (converted from LatestActivitiesWidget).
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class WPStrava_LatestActivitiesWidget extends WP_Widget {
|
||||
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' ) );
|
||||
}
|
||||
|
||||
@@ -22,7 +29,7 @@ class WPStrava_LatestActivitiesWidget 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,
|
||||
@@ -35,7 +42,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
|
||||
}
|
||||
@@ -53,7 +60,7 @@ class WPStrava_LatestActivitiesWidget 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'] ) : '';
|
||||
@@ -83,4 +90,4 @@ class WPStrava_LatestActivitiesWidget extends WP_Widget {
|
||||
<?php
|
||||
}
|
||||
|
||||
} // class LatestActivitiesWidget
|
||||
}
|
||||
+36
-11
@@ -23,23 +23,49 @@ class WPStrava_Activity {
|
||||
* Get activity list from Strava API.
|
||||
*
|
||||
* @author Justin Foell <justin@foell.org>
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 );
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
<?php
|
||||
<?php // phpcs:disable Generic.Files.OneClassPerFile.MultipleFound, Generic.Classes.DuplicateClassName.Found, Generic.Files.OneObjectStructurePerFile.MultipleFound
|
||||
/**
|
||||
* WPStrava Exception(s).
|
||||
*/
|
||||
|
||||
// phpcs:disable Generic.Files.OneClassPerFile.MultipleFound, Generic.Classes.DuplicateClassName.Found
|
||||
|
||||
/*
|
||||
* PHP 5.2 Nonsense
|
||||
* @see http://php.net/manual/en/exception.getprevious.php#106020
|
||||
@@ -30,6 +28,9 @@ if ( version_compare( PHP_VERSION, '5.3.0', '>=' ) ) {
|
||||
|
||||
/*
|
||||
* 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 <justin@foell.org>
|
||||
* @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
|
||||
|
||||
@@ -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'] ) ) {
|
||||
|
||||
@@ -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];
|
||||
|
||||
+2
-2
@@ -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__ ) );
|
||||
|
||||
Reference in New Issue
Block a user