mirror of
https://github.com/10h30/wp-strava.git
synced 2026-07-18 14:14:01 +09:00
Fixed latest rides widget and settings workflow
This commit is contained in:
+2
-2
@@ -20,7 +20,7 @@ class WPStrava_API {
|
|||||||
'body' => http_build_query( $data ),
|
'body' => http_build_query( $data ),
|
||||||
'sslverify' => false,
|
'sslverify' => false,
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer: ' . $this->access_token,
|
'Authorization' => 'Bearer ' . $this->access_token,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ class WPStrava_API {
|
|||||||
|
|
||||||
$get_args = array(
|
$get_args = array(
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer: ' . $this->access_token,
|
'Authorization' => 'Bearer ' . $this->access_token,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -6,73 +6,58 @@
|
|||||||
class WPStrava_LatestRidesWidget extends WP_Widget {
|
class WPStrava_LatestRidesWidget extends WP_Widget {
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$widget_ops = array('classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.') );
|
$widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.' ) );
|
||||||
parent::__construct('wp-strava', $name = 'Strava Latest Rides', $widget_ops);
|
parent::__construct( 'wp-strava', $name = 'Strava Latest Rides', $widget_ops );
|
||||||
wp_enqueue_style('wp-strava'); //TODO only load this when wigit is loaded
|
wp_enqueue_style( 'wp-strava' ); //TODO only load this when wigit is loaded
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see WP_Widget::widget */
|
/** @see WP_Widget::widget */
|
||||||
public function widget( $args, $instance ) {
|
public function widget( $args, $instance ) {
|
||||||
extract($args);
|
extract( $args );
|
||||||
|
|
||||||
//$widget_id = $args['widget_id'];
|
//$widget_id = $args['widget_id'];
|
||||||
$title = apply_filters('widget_title', empty($instance['title']) ? _e('Rides', 'wp-strava') : $instance['title']);
|
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Rides', 'wp-strava' ) : $instance['title'] );
|
||||||
$strava_search_option = empty($instance['strava_search_option']) ? 'athlete' : $instance['strava_search_option'];
|
$strava_club_id = empty( $instance['strava_club_id'] ) ? '' : $instance['strava_club_id'];
|
||||||
$strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id'];
|
$quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity'];
|
||||||
$quantity = empty($instance['quantity']) ? '5' : $instance['quantity'];
|
|
||||||
|
|
||||||
$this->som = WPStrava_SOM::get_som();
|
$this->som = WPStrava_SOM::get_som();
|
||||||
?>
|
?>
|
||||||
<?php echo $before_widget; ?>
|
<?php echo $before_widget; ?>
|
||||||
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
|
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
|
||||||
<?php echo $this->strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?>
|
<?php echo $this->strava_request_handler( $strava_club_id, $strava_som_option, $quantity ); ?>
|
||||||
<?php echo $after_widget; ?>
|
<?php echo $after_widget; ?>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see WP_Widget::update */
|
/** @see WP_Widget::update */
|
||||||
public function update($new_instance, $old_instance) {
|
public function update( $new_instance, $old_instance ) {
|
||||||
$instance = $old_instance;
|
$instance = $old_instance;
|
||||||
$instance['title'] = strip_tags($new_instance['title']);
|
$instance['title'] = strip_tags( $new_instance['title'] );
|
||||||
if(in_array($new_instance['strava_search_option'], array('athlete', 'club'))) {
|
if( in_array( $new_instance['strava_som_option'], array( 'metric', 'english' ) ) ) {
|
||||||
$instance['strava_search_option'] = $new_instance['strava_search_option'];
|
|
||||||
} else {
|
|
||||||
$instance['strava_search_option'] = 'athlete';
|
|
||||||
}
|
|
||||||
if(in_array($new_instance['strava_som_option'], array('metric', 'english'))) {
|
|
||||||
$instance['strava_som_option'] = $new_instance['strava_som_option'];
|
$instance['strava_som_option'] = $new_instance['strava_som_option'];
|
||||||
} else {
|
} else {
|
||||||
$instance['strava_som_option'] = 'metric';
|
$instance['strava_som_option'] = 'metric';
|
||||||
}
|
}
|
||||||
$instance['strava_search_id'] = strip_tags($new_instance['strava_search_id']);
|
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
|
||||||
$instance['quantity'] = $new_instance['quantity'];
|
$instance['quantity'] = $new_instance['quantity'];
|
||||||
|
|
||||||
return $instance;
|
return $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @see WP_Widget::form */
|
/** @see WP_Widget::form */
|
||||||
public function form($instance) {
|
public function form( $instance ) {
|
||||||
$title = isset($instance['title']) ? esc_attr($instance['title']) : _e('Rides', 'wp-strava');
|
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Rides', 'wp-strava' );
|
||||||
$strava_search_option = isset($instance['strava_search_option']) ? esc_attr($instance['strava_search_option']) : "athlete";
|
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
|
||||||
$strava_search_id = isset($instance['strava_search_id']) ? esc_attr($instance['strava_search_id']) : "";
|
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
|
||||||
$quantity = isset($instance['quantity']) ? absint($instance['quantity']) : 5;
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
<p>
|
<p>
|
||||||
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
|
||||||
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
|
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
|
||||||
</p>
|
|
||||||
<!-- TODO: make an 'advanced' option -->
|
|
||||||
<p>
|
|
||||||
<label for="<?php echo $this->get_field_id('strava_search_option'); ?>"><?php _e('Search Option:'); ?></label>
|
|
||||||
<select class="widefat" id="<?php echo $this->get_field_id('strava_search_option'); ?>" name="<?php echo $this->get_field_name('strava_search_option'); ?>">
|
|
||||||
<option value="athlete" <?php selected($strava_search_option, 'athlete'); ?>><?php _e("Athlete", "wp-strava")?></option>
|
|
||||||
<option value="club" <?php selected($strava_search_option, 'club'); ?>><?php _e("Club", "wp-strava")?></option>
|
|
||||||
</select>
|
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="<?php echo $this->get_field_id('strava_search_id'); ?>"><?php _e('Search Id:'); ?></label>
|
<label for="<?php echo $this->get_field_id('strava_club_id'); ?>"><?php _e('Club ID (leave blank to show Athlete):'); ?></label>
|
||||||
<input class="widefat" id="<?php echo $this->get_field_id('strava_search_id'); ?>" name="<?php echo $this->get_field_name('strava_search_id'); ?>" type="text" value="<?php echo $strava_search_id; ?>" />
|
<input class="widefat" id="<?php echo $this->get_field_id('strava_club_id'); ?>" name="<?php echo $this->get_field_name('strava_club_id'); ?>" type="text" value="<?php echo $strava_club_id; ?>" />
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
<label for="<?php echo $this->get_field_id('quantity'); ?>"><?php _e('Quantity:'); ?></label>
|
<label for="<?php echo $this->get_field_id('quantity'); ?>"><?php _e('Quantity:'); ?></label>
|
||||||
@@ -83,41 +68,29 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
|
|||||||
|
|
||||||
// The handler to the ajax call, we will avoid this if Strava support jsonp request and we can do it
|
// The handler to the ajax call, we will avoid this if Strava support jsonp request and we can do it
|
||||||
// the parsing directly on the jQuery ajax call, the returned text will be enclosed in the $response variable.
|
// the parsing directly on the jQuery ajax call, the returned text will be enclosed in the $response variable.
|
||||||
private function strava_request_handler( $strava_search_option, $strava_search_id, $strava_som_option, $quantity ) {
|
private function strava_request_handler( $strava_club_id, $strava_som_option, $quantity ) {
|
||||||
|
|
||||||
//Check if the username is empty.
|
|
||||||
if ( empty( $strava_search_id ) )
|
|
||||||
return __("Please configure the Strava search id on the widget options.", "wp-strava");
|
|
||||||
//else
|
|
||||||
$strava_rides = WPStrava::get_instance()->rides;
|
$strava_rides = WPStrava::get_instance()->rides;
|
||||||
|
|
||||||
$rides = $strava_rides->getRidesSimple( $strava_search_option, $strava_search_id );
|
$rides = $strava_rides->getRides( $quantity, $strava_club_id );
|
||||||
if ( is_wp_error( $rides ) )
|
if ( is_wp_error( $rides ) )
|
||||||
return $rides->get_error_message();
|
return $rides->get_error_message();
|
||||||
|
|
||||||
//adjust quantity
|
|
||||||
$rides = array_slice( $rides, 0, $quantity );
|
|
||||||
|
|
||||||
$rides_details = $strava_rides->getRidesDetails( $rides );
|
|
||||||
if ( is_wp_error( $rides_details ) )
|
|
||||||
return $rides_details->get_error_message();
|
|
||||||
|
|
||||||
$response = "<ul id='rides'>";
|
$response = "<ul id='rides'>";
|
||||||
foreach($rides_details as $ride_obj) {
|
foreach( $rides as $ride ) {
|
||||||
$ride = $ride_obj->ride;
|
|
||||||
$response .= "<li class='ride'>";
|
$response .= "<li class='ride'>";
|
||||||
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' >" . $ride->name . "</a>";
|
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' >" . $ride->name . "</a>";
|
||||||
$response .= "<div class='ride-item'>";
|
$response .= "<div class='ride-item'>";
|
||||||
$unixtime = strtotime( $ride->start_date_local );
|
$unixtime = strtotime( $ride->start_date_local );
|
||||||
$response .= sprintf( __("On %s %s", "wp-strava"), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );
|
$response .= sprintf( __("On %s %s", "wp-strava"), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );
|
||||||
|
|
||||||
if ($strava_search_option == "club") {
|
if ( is_numeric( $strava_club_id ) ) {
|
||||||
$response .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . "</a>";
|
$response .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . "</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
$response .= sprintf( __(" rode %s %s", "wp-strava"), $this->som->distance( $ride->distance ), $this->som->get_distance_label() );
|
$response .= sprintf( __(" rode %s %s", "wp-strava"), $this->som->distance( $ride->distance ), $this->som->get_distance_label() );
|
||||||
$response .= sprintf( __( " during %s %s", "wp-strava" ), $this->som->time( $ride->elapsed_time ), $this->som->get_time_label() );
|
$response .= sprintf( __( " during %s %s", "wp-strava" ), $this->som->time( $ride->elapsed_time ), $this->som->get_time_label() );
|
||||||
$response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->elevation_gain ), $this->som->get_elevation_label() );
|
$response .= sprintf( __( " climbing %s %s", "wp-strava" ), $this->som->elevation( $ride->total_elevation_gain ), $this->som->get_elevation_label() );
|
||||||
$response .= "</div>";
|
$response .= "</div>";
|
||||||
$response .= "</li>";
|
$response .= "</li>";
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-14
@@ -8,7 +8,7 @@ class WPStrava_Rides {
|
|||||||
const ATHLETES_URL = "http://app.strava.com/athletes/";
|
const ATHLETES_URL = "http://app.strava.com/athletes/";
|
||||||
|
|
||||||
public function getRideDetails( $rideId ) {
|
public function getRideDetails( $rideId ) {
|
||||||
return WPStrava::get_instance()->api->get( "rides/{$rideId}" );
|
return WPStrava::get_instance()->api->get( "activities/{$rideId}" );
|
||||||
} // getRideDetails
|
} // getRideDetails
|
||||||
|
|
||||||
public function getRidesDetails( $rides ) {
|
public function getRidesDetails( $rides ) {
|
||||||
@@ -24,32 +24,26 @@ class WPStrava_Rides {
|
|||||||
return $rides_details;
|
return $rides_details;
|
||||||
} // getRidesDetails
|
} // getRidesDetails
|
||||||
|
|
||||||
public function getRidesSimple( $searchOption, $searchId ) {
|
public function getRides( $quantity, $club_id = NULL ) {
|
||||||
$api = WPStrava::get_instance()->api;
|
$api = WPStrava::get_instance()->api;
|
||||||
|
|
||||||
$data = NULL;
|
$data = NULL;
|
||||||
//Get the json results using the constructor specified values.
|
//Get the json results using the constructor specified values.
|
||||||
if ( $searchOption == 'athlete' ) {
|
if ( is_numeric( $club_id ) ) {
|
||||||
if ( is_numeric( $searchId ) ) {
|
$data = $api->get( "clubs/{$club_id}/activities", array( 'per_page' => $quantity ) );
|
||||||
$data = $api->get( 'rides', array( 'athleteId' => $searchId ), 1 );
|
|
||||||
} else {
|
|
||||||
$data = $api->get( 'rides', array( 'athleteName' => $searchId ), 1 );
|
|
||||||
}
|
|
||||||
} elseif ($searchOption == 'club' && is_numeric($searchId)) {
|
|
||||||
$data = $api->get( 'rides', array( 'clubId' => $searchId ), 1 );
|
|
||||||
} else {
|
} else {
|
||||||
return new WP_Error( 'wp-strava_options', __("There's an error in your simple options.", 'wp-strava') );
|
$data = $api->get( 'athlete/activities', array( 'per_page' => $quantity ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( is_wp_error( $data ) )
|
if ( is_wp_error( $data ) )
|
||||||
return $data;
|
return $data;
|
||||||
|
|
||||||
if ( isset( $data->rides ) )
|
if ( is_array( $data ) )
|
||||||
return $data->rides;
|
return $data;
|
||||||
|
|
||||||
return array();
|
return array();
|
||||||
|
|
||||||
} // getRidesSimple
|
} // getRides
|
||||||
|
|
||||||
public function getRidesAdvanced( $params ) {
|
public function getRidesAdvanced( $params ) {
|
||||||
$data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1
|
$data = WPStrava::get_instance()->api->get( 'rides', $params, 1 ); //version 1
|
||||||
|
|||||||
+20
-17
@@ -20,24 +20,9 @@ class WPStrava_Settings {
|
|||||||
public function hook() {
|
public function hook() {
|
||||||
add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
|
add_action( 'admin_init', array( $this, 'register_strava_settings' ) );
|
||||||
add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
|
add_action( 'admin_menu', array( $this, 'add_strava_menu' ) );
|
||||||
add_action( 'current_screen', array( $this, 'current_screen' ) );
|
|
||||||
add_action( 'option_home', array( $this, 'option_home' ) );
|
add_action( 'option_home', array( $this, 'option_home' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function current_screen( $screen ) {
|
|
||||||
if ( $screen->id = 'settings_page_' . $this->page_name ) {
|
|
||||||
if ( isset( $_GET['code'] ) ) {
|
|
||||||
$token = $this->get_token( $_GET['code'] );
|
|
||||||
if ( $token ) {
|
|
||||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $this->feedback ) , 'updated' );
|
|
||||||
update_option( 'strava_token', $token );
|
|
||||||
} else {
|
|
||||||
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This runs after options are saved
|
* This runs after options are saved
|
||||||
*/
|
*/
|
||||||
@@ -47,9 +32,13 @@ class WPStrava_Settings {
|
|||||||
$errors = get_settings_errors();
|
$errors = get_settings_errors();
|
||||||
if ( ! empty( $errors ) )
|
if ( ! empty( $errors ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
//clearing to start-over
|
||||||
|
if ( isset( $_POST['strava_token'] ) && empty( $_POST['strava_token'] ) )
|
||||||
|
return;
|
||||||
|
|
||||||
$client_id = get_option( 'strava_client_id' );
|
$client_id = get_option( 'strava_client_id' );
|
||||||
$client_secret = get_option( 'strava_client_secret' );
|
$client_secret = get_option( 'strava_client_secret' );
|
||||||
|
|
||||||
if ( $client_id && $client_secret ) {
|
if ( $client_id && $client_secret ) {
|
||||||
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
|
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
|
||||||
@@ -68,8 +57,22 @@ class WPStrava_Settings {
|
|||||||
array( $this, 'print_strava_options' ) );
|
array( $this, 'print_strava_options' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function init() {
|
||||||
|
if ( isset( $_GET['page'] ) && $_GET['page'] == $this->page_name && isset( $_GET['code'] ) ) {
|
||||||
|
$token = $this->get_token( $_GET['code'] );
|
||||||
|
if ( $token ) {
|
||||||
|
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
|
||||||
|
update_option( 'strava_token', $token );
|
||||||
|
} else {
|
||||||
|
add_settings_error( 'strava_token', 'strava_token', $this->feedback );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->token = get_option( 'strava_token' );
|
||||||
|
}
|
||||||
|
|
||||||
public function register_strava_settings() {
|
public function register_strava_settings() {
|
||||||
$this->token = get_option( 'strava_token' );
|
$this->init();
|
||||||
|
|
||||||
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed
|
add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); //NULL / NULL no section label needed
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user