Lots of formatting (code standards)
Bumped version #
This commit is contained in:
Justin Foell
2017-12-26 14:18:13 -06:00
parent b6abb1912f
commit bd40fe4021
10 changed files with 197 additions and 165 deletions
+19 -15
View File
@@ -17,9 +17,9 @@ class WPStrava_API {
$url = self::STRAVA_V3_API;
$args = array(
'body' => http_build_query( $data ),
'body' => http_build_query( $data ),
'sslverify' => false,
'headers' => array(),
'headers' => array(),
);
if ( $this->access_token ) {
@@ -28,17 +28,19 @@ class WPStrava_API {
$response = wp_remote_post( $url . $uri, $args );
if ( is_wp_error( $response ) )
if ( is_wp_error( $response ) ) {
return $response;
}
if ( $response['response']['code'] != 200 ) {
if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
if ( ! empty( $body->error ) ) {
$error = $body->error;
else
$error = print_r( $response, true );
} else {
$error = print_r( $response, true ); // @codingStandardsIgnoreLine
}
return new WP_Error(
'wp-strava_post',
@@ -69,19 +71,21 @@ class WPStrava_API {
$response = wp_remote_get( $url, $get_args );
if ( is_wp_error( $response ) )
if ( is_wp_error( $response ) ) {
return $response;
}
if ( $response['response']['code'] != 200 ) {
if ( 200 != $response['response']['code'] ) {
//see if there's useful info in the body
$body = json_decode( $response['body'] );
$body = json_decode( $response['body'] );
$error = '';
if ( ! empty( $body->error ) )
if ( ! empty( $body->error ) ) {
$error = $body->error;
else if ( $response['response']['code'] == 503 )
} elseif ( 503 == $response['response']['code'] ) {
$error = __( 'Strava Temporarily Unavailable', 'wp-strava' );
else
$error = print_r( $response, true );
} else {
$error = print_r( $response, true ); // @codingStandardsIgnoreLine
}
return new WP_Error(
'wp-strava_get',
+4 -7
View File
@@ -10,7 +10,7 @@ class WPStrava_ActivityShortcode {
}
// Shortcode handler function
// [ride id=id som=metric map_width="100%" map_height="400px" markers=false]
// [activity id=id som=metric map_width="100%" map_height="400px" markers=false]
public static function handler( $atts ) {
self::$add_script = true;
@@ -20,13 +20,13 @@ class WPStrava_ActivityShortcode {
'map_width' => '480',
'map_height' => '320',
'athlete_token' => WPStrava::get_instance()->settings->get_default_token(),
'markers' => false,
'markers' => false,
);
extract( shortcode_atts( $defaults, $atts ) );
$strava_som = WPStrava_SOM::get_som( $som );
$activity = WPStrava::get_instance()->rides;
$strava_som = WPStrava_SOM::get_som( $som );
$activity = WPStrava::get_instance()->rides;
$ride_details = $activity->getRide( $athlete_token, $id );
//sanitize width & height
@@ -76,9 +76,6 @@ class WPStrava_ActivityShortcode {
public static function print_scripts() {
if ( self::$add_script ) {
wp_enqueue_style( 'wp-strava-style' );
//wp_print_scripts('google-maps');
//wp_print_scripts('wp-strava-script');
}
}
}
+32 -28
View File
@@ -8,7 +8,7 @@ class WPStrava_LatestMapWidget extends WP_Widget {
$this->som = WPStrava_SOM::get_som();
parent::__construct(
false,
false,
__( 'Strava Latest Map', 'wp-strava' ), // Name
array( 'description' => __( 'Strava latest activity using static google map image', 'wp-strava' ) ) // Args.
);
@@ -16,15 +16,12 @@ class WPStrava_LatestMapWidget extends WP_Widget {
public function form( $instance ) {
// outputs the options form on admin
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity Map', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Latest Activity Map', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$distance_min = isset( $instance['distance_min'] ) ? esc_attr( $instance['distance_min'] ) : '';
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
//provide some defaults
//$ride_index_params = $ride_index_params ?: 'athleteId=21';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></label>
@@ -33,8 +30,8 @@ class WPStrava_LatestMapWidget extends WP_Widget {
<p>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ): ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php esc_attr_e( $nickname ); ?></option>
<?php foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
@@ -43,47 +40,50 @@ class WPStrava_LatestMapWidget extends WP_Widget {
<input class="widefat" id="<?php echo $this->get_field_id( 'distance_min' ); ?>" name="<?php echo $this->get_field_name( 'distance_min' ); ?>" type="text" value="<?php echo $distance_min; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php _e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php esc_html_e( 'Club ID (leave blank to show Athlete):', 'wp-strava' ); ?></label>
<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>
<?php
}
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved from the admin
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
// Processes widget options to be saved from the admin.
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
$instance['distance_min'] = strip_tags( $new_instance['distance_min'] );
return $instance;
}
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] );
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Latest Activity Map', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$distance_min = empty( $instance['distance_min'] ) ? 0 : absint( $instance['distance_min'] );
$strava_club_id = empty( $instance['strava_club_id'] ) ? null : $instance['strava_club_id'];
$build_new = false;
$build_new = false;
// Try our transient first.
$ride_transient = get_transient( 'strava_latest_map_activity_' . $athlete_token );
$ride_option = get_option( 'strava_latest_map_activity_' . $athlete_token );
$ride_option = get_option( 'strava_latest_map_activity_' . $athlete_token );
$ride = $ride_transient ? $ride_transient : null;
if ( ! $ride ) {
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id );
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id );
if ( is_wp_error( $rides ) ) {
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
if ( $title ) {
echo $before_title . $title . $after_title;
}
if ( WPSTRAVA_DEBUG ) {
echo '<pre>';
print_r($rides);
print_r( $rides ); // @codingStandardsIgnoreLine
echo '</pre>';
} else {
echo $rides->get_error_message();
@@ -94,8 +94,9 @@ class WPStrava_LatestMapWidget extends WP_Widget {
if ( ! empty( $rides ) ) {
if ( ! empty( $distance_min ) )
if ( ! empty( $distance_min ) ) {
$rides = $strava_rides->getRidesLongerThan( $rides, $distance_min );
}
$ride = current( $rides );
@@ -115,10 +116,13 @@ class WPStrava_LatestMapWidget extends WP_Widget {
if ( $ride ) {
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
?><a title="<?php echo $ride->name ?>" target="_blank" href="http://app.strava.com/activities/<?php echo $ride->id ?>"><?php
if ( $title ) {
echo $before_title . $title . $after_title;
}
echo "<a title='{$ride->name}' target='_blank' href='http://app.strava.com/activities/{$ride->id}'>";
echo $this->getStaticImage( $athlete_token, $ride, $build_new );
?></a><?php
echo '</a>';
echo $after_widget;
}
}
+42 -37
View File
@@ -6,8 +6,11 @@
class WPStrava_LatestRidesWidget extends WP_Widget {
public function __construct() {
$widget_ops = array( 'classname' => 'LatestRidesWidget', 'description' => __( 'Will publish your latest rides activity from strava.com.', 'wp-strava' ) );
parent::__construct( 'wp-strava', $name = __( 'Strava Latest Activity List', 'wp-strava' ), $widget_ops );
$widget_ops = array(
'classname' => 'LatestRidesWidget',
'description' => __( 'Will publish your latest rides activity from strava.com.', 'wp-strava' ),
);
parent::__construct( 'wp-strava', __( 'Strava Latest Activity List', 'wp-strava' ), $widget_ops );
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) );
}
@@ -21,38 +24,37 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
public function widget( $args, $instance ) {
extract( $args );
//$widget_id = $args['widget_id'];
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activity', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activity', 'wp-strava' ) : $instance['title'] );
$athlete_token = isset( $instance['athlete_token'] ) ? $instance['athlete_token'] : WPStrava::get_instance()->settings->get_default_token();
$strava_club_id = empty( $instance['strava_club_id'] ) ? '' : $instance['strava_club_id'];
$quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity'];
$quantity = empty( $instance['quantity'] ) ? '5' : $instance['quantity'];
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php echo $this->strava_request_handler( $athlete_token, $strava_club_id, $quantity ); ?>
<?php echo $after_widget; ?>
<?php
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
echo $this->strava_request_handler( $athlete_token, $strava_club_id, $quantity );
echo $after_widget;
}
/** @see WP_Widget::update */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['athlete_token'] = strip_tags( $new_instance['athlete_token'] );
$instance['strava_club_id'] = strip_tags( $new_instance['strava_club_id'] );
$instance['quantity'] = $new_instance['quantity'];
$instance['quantity'] = $new_instance['quantity'];
return $instance;
}
/** @see WP_Widget::form */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Activity', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Activity', 'wp-strava' );
$all_tokens = WPStrava::get_instance()->settings->get_all_tokens();
$athlete_token = isset( $instance['athlete_token'] ) ? esc_attr( $instance['athlete_token'] ) : WPStrava::get_instance()->settings->get_default_token();
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
?>
<p>
@@ -62,17 +64,17 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
<p>
<label for="<?php echo $this->get_field_id( 'athlete_token' ); ?>"><?php _e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo $this->get_field_name( 'athlete_token' ); ?>">
<?php foreach ( $all_tokens as $token => $nickname ): ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php esc_attr_e( $nickname ); ?></option>
<?php foreach ( $all_tokens as $token => $nickname ) : ?>
<option value="<?php echo $token; ?>"<?php selected( $token, $athlete_token ); ?>><?php echo $nickname; ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php _e( 'Club ID (leave blank to show single Athlete):', 'wp-strava' ); ?></label>
<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; ?>" />
<label for="<?php echo $this->get_field_id( 'strava_club_id' ); ?>"><?php esc_html_e( 'Club ID (leave blank to show single Athlete):', 'wp-strava' ); ?></label>
<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>
<label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php _e( 'Quantity:', 'wp-strava' ); ?></label>
<label for="<?php echo $this->get_field_id( 'quantity' ); ?>"><?php esc_html_e( 'Quantity:', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'quantity' ); ?>" name="<?php echo $this->get_field_name( 'quantity' ); ?>" type="text" value="<?php echo $quantity; ?>" />
</p>
<?php
@@ -82,34 +84,37 @@ class WPStrava_LatestRidesWidget extends WP_Widget {
// the parsing directly on the jQuery ajax call, the returned text will be enclosed in the $response variable.
private function strava_request_handler( $athlete_token, $strava_club_id, $quantity ) {
$som = WPStrava_SOM::get_som();
$som = WPStrava_SOM::get_som();
$strava_rides = WPStrava::get_instance()->rides;
$rides = $strava_rides->getRides( $athlete_token, $strava_club_id, $quantity );
if ( is_wp_error( $rides ) )
if ( is_wp_error( $rides ) ) {
return $rides->get_error_message();
}
$response = "<ul id='rides'>";
foreach ( $rides as $ride ) {
$response .= "<li class='ride'>";
$response .= "<a href='" . WPStrava_Rides::RIDES_URL . $ride->id . "' target='_blank'>" . $ride->name . '</a>';
$response .= "<a href='" . WPStrava_Rides::ACTIVITIES_URL . $ride->id . "' target='_blank'>" . $ride->name . '</a>';
$response .= "<div class='ride-item'>";
$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 ) );
$unixtime = strtotime( $ride->start_date_local );
// translators: Shows something like "On <date> <name [went 10 miles] [during 1 hour] [climbing 100 feet]>."
$response .= sprintf( __( 'On %1$s %2$s', 'wp-strava' ), date_i18n( get_option( 'date_format' ), $unixtime ), date_i18n( get_option( 'time_format' ), $unixtime ) );
if ( is_numeric( $strava_club_id ) ) {
$response .= " <a href='" . WPStrava_Rides::ATHLETES_URL . $ride->athlete_id . "'>" . $ride->athlete_name . '</a>';
}
$response .= sprintf( __( ' rode %s %s', 'wp-strava' ), $som->distance( $ride->distance ), $som->get_distance_label() );
$response .= sprintf( __( ' during %s %s', 'wp-strava' ), $som->time( $ride->elapsed_time ), $som->get_time_label() );
$response .= sprintf( __( ' climbing %s %s', 'wp-strava' ), $som->elevation( $ride->total_elevation_gain ), $som->get_elevation_label() );
$response .= "</div>";
$response .= "</li>";
// translators: "went 10 miles"
$response .= sprintf( __( ' went %1$s %2$s', 'wp-strava' ), $som->distance( $ride->distance ), $som->get_distance_label() );
// translators: "during 1 hour"
$response .= sprintf( __( ' during %1$s %2$s', 'wp-strava' ), $som->time( $ride->elapsed_time ), $som->get_time_label() );
// translators: "climbing 100 feet"
$response .= sprintf( __( ' climbing %1$s %2$s', 'wp-strava' ), $som->elevation( $ride->total_elevation_gain ), $som->get_elevation_label() );
$response .= '</div></li>';
}
$response .= "</ul>";
$response .= '</ul>';
return $response;
} // Function strava_request_handler
} // class LatestRidesWidget
+3 -3
View File
@@ -4,8 +4,8 @@
*/
class WPStrava_Rides {
const RIDES_URL = 'http://app.strava.com/rides/';
const ATHLETES_URL = 'http://app.strava.com/athletes/';
const ACTIVITIES_URL = 'http://app.strava.com/activities/';
const ATHLETES_URL = 'http://app.strava.com/athletes/';
/**
* Get single activity by ID.
@@ -56,7 +56,7 @@ class WPStrava_Rides {
} // getRides
public function getRidesLongerThan( $rides, $dist ) {
$som = WPStrava_SOM::get_som();
$som = WPStrava_SOM::get_som();
$meters = $som->distance_inverse( $dist );
$long_rides = array();
+2 -2
View File
@@ -4,10 +4,10 @@ abstract class WPStrava_SOM {
public static function get_som( $som = null ) {
$som = $som ? $som : WPStrava::get_instance()->settings->som;
if ( $som == 'english' ) {
if ( 'english' === $som ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMEnglish.class.php';
return new WPStrava_SOMEnglish();
} else { //default to metric
} else { // Default to metric.
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOMMetric.class.php';
return new WPStrava_SOMMetric();
}
+1 -1
View File
@@ -44,4 +44,4 @@ class WPStrava_SOMMetric extends WPStrava_SOM {
public function get_elevation_label() {
return __( 'meters', 'wp-strava' );
}
}
}
+59 -45
View File
@@ -12,9 +12,9 @@
class WPStrava_Settings {
private $feedback;
private $tokens = array();
private $page_name = 'wp-strava-options';
private $option_page = 'wp-strava-settings-group';
private $tokens = array();
private $page_name = 'wp-strava-options';
private $option_page = 'wp-strava-settings-group';
private $adding_athlete = true;
//register admin menus
@@ -24,34 +24,28 @@ class WPStrava_Settings {
add_filter( 'pre_set_transient_settings_errors', array( $this, 'maybe_oauth' ) );
add_filter( 'plugin_action_links_' . WPSTRAVA_PLUGIN_NAME, array( $this, 'settings_link' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'settings_scripts' ) );
//for process debugging
//add_action( 'all', array( $this, 'hook_debug' ) );
//add_filter( 'all', array( $this, 'hook_debug' ) );
}
public function hook_debug( $name ) {
echo "<!-- {$name} -->\n";
}
/**
* This runs after options are saved
*/
public function maybe_oauth( $value ) {
public function maybe_oauth( $value ) {
// User is clearing to start-over, don't oauth, ignore other errors.
if ( isset( $_POST['strava_token'] ) && $this->tokens_empty( $_POST['strava_token'] ) )
return array();
if ( isset( $_POST['strava_token'] ) && $this->tokens_empty( $_POST['strava_token'] ) ) {
return array();
}
// Redirect only if all the right options are in place.
if ( isset( $value[0]['type'] ) && 'updated' == $value[0]['type'] ) { // Make sure there were no settings errors.
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == $this->option_page ) { // Make sure we're on our settings page.
if ( isset( $value[0]['type'] ) && 'updated' === $value[0]['type'] ) { // Make sure there were no settings errors.
if ( isset( $_POST['option_page'] ) && $_POST['option_page'] === $this->option_page ) { // Make sure we're on our settings page.
// Only re-auth if client ID and secret were saved.
if ( ! empty( $_POST['strava_client_id'] ) && ! empty( $_POST['strava_client_secret'] ) ) {
$client_id = $_POST['strava_client_id'];
$client_id = $_POST['strava_client_id'];
$client_secret = $_POST['strava_client_secret'];
$redirect = admin_url( "options-general.php?page={$this->page_name}" );
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
$url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force";
wp_redirect( $url );
exit();
}
@@ -77,11 +71,12 @@ class WPStrava_Settings {
$this->adding_athlete = ! ( empty( $_POST['strava_client_id'] ) && empty( $_POST['strava_client_secret'] ) );
//only update when redirected back from strava
if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] == $this->page_name ) {
if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] === $this->page_name ) {
if ( isset( $_GET['code'] ) ) {
$token = $this->fetch_token( $_GET['code'] );
if ( $token ) {
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ) , 'updated' );
// Translators: strava token
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ), 'updated' );
$this->add_token( $token );
update_option( 'strava_token', $this->tokens );
} else {
@@ -108,7 +103,7 @@ class WPStrava_Settings {
add_settings_field( 'strava_client_secret', __( 'Strava Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' );
add_settings_field( 'strava_nickname', __( 'Strava Nickname', 'wp-strava' ), array( $this, 'print_nickname_input' ), 'wp-strava', 'strava_api' );
} else {
register_setting( $this->option_page, 'strava_token', array( $this, 'sanitize_token' ) );
register_setting( $this->option_page, 'strava_token', array( $this, 'sanitize_token' ) );
add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' );
// Add additional fields
@@ -138,13 +133,17 @@ class WPStrava_Settings {
}
public function print_api_instructions() {
$signup_url = 'http://www.strava.com/developers';
$signup_url = 'http://www.strava.com/developers';
$settings_url = 'https://www.strava.com/settings/api';
$icon_url = 'https://plugins.svn.wordpress.org/wp-strava/assets/icon-128x128.png';
$blog_name = get_bloginfo( 'name' );
$app_name = sprintf( esc_html( '%s Strava', 'wp-strava' ), $blog_name );
$icon_url = 'https://plugins.svn.wordpress.org/wp-strava/assets/icon-128x128.png';
$blog_name = get_bloginfo( 'name' );
// Translators: Strava "app" name
$app_name = sprintf( __( '%s Strava', 'wp-strava' ), $blog_name );
$site_url = site_url();
$description = 'WP-Strava for ' . $blog_name;
// Translators: Strava "app" description
$description = sprintf( __( 'WP-Strava for %s', 'wp-strava' ), $blog_name );
printf( __( "<p>Steps:</p>
<ol>
<li>Create your free API Application/Connection here: <a href='%1\$s' target='_blank'>%2\$s</a> using the following information:</li>
@@ -200,16 +199,22 @@ class WPStrava_Settings {
}
public function print_client_input() {
?><input type="text" id="strava_client_id" name="strava_client_id" value="" /><?php
?>
<input type="text" id="strava_client_id" name="strava_client_id" value="" />
<?php
}
public function print_secret_input() {
?><input type="text" id="strava_client_secret" name="strava_client_secret" value="" /><?php
?>
<input type="text" id="strava_client_secret" name="strava_client_secret" value="" />
<?php
}
public function print_nickname_input() {
$nickname = $this->tokens_empty( $this->tokens ) ? __( 'Default', 'wp-strava' ) : '';
?><input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" /><?php
?>
<input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" />
<?php
}
public function print_token_input() {
@@ -240,7 +245,7 @@ class WPStrava_Settings {
return $client_secret;
}
if ( '' == trim( $client_secret ) ) {
if ( '' === trim( $client_secret ) ) {
add_settings_error( 'strava_client_secret', 'strava_client_secret', __( 'Client Secret is required.', 'wp-strava' ) );
}
return $client_secret;
@@ -253,7 +258,7 @@ class WPStrava_Settings {
$nicknames = array_slice( $nicknames, 0, count( $_POST['strava_token'] ) );
// Remove indexes from $nicknames that have empty tokens.
foreach( $_POST['strava_token'] as $index => $token ) {
foreach ( $_POST['strava_token'] as $index => $token ) {
$token = trim( $token );
if ( empty( $token ) ) {
unset( $nicknames[ $index ] );
@@ -265,7 +270,7 @@ class WPStrava_Settings {
}
foreach ( $nicknames as $index => $nickname ) {
if ( '' == trim( $nickname ) ) {
if ( '' === trim( $nickname ) ) {
add_settings_error( 'strava_nickname', 'strava_nickname', __( 'Nickname is required.', 'wp-strava' ) );
return $nicknames;
}
@@ -278,7 +283,7 @@ class WPStrava_Settings {
}
private function fetch_token( $code ) {
$client_id = $this->client_id;
$client_id = $this->client_id;
$client_secret = $this->client_secret;
delete_option( 'strava_client_id' );
@@ -286,8 +291,12 @@ class WPStrava_Settings {
if ( $client_id && $client_secret ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
$api = new WPStrava_API();
$data = array( 'client_id' => $client_id, 'client_secret' => $client_secret, 'code' => $code );
$api = new WPStrava_API();
$data = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $code,
);
$strava_info = $api->post( 'oauth/token', $data );
@@ -300,7 +309,8 @@ class WPStrava_Settings {
return false;
}
} else {
$this->feedback .= __( sprintf( 'There was an error receiving data from Strava: %s', print_r( $strava_info, true ) ), 'wp-strava' );
// Translators: error message from Strava
$this->feedback .= sprintf( __( 'There was an error receiving data from Strava: %s', 'wp-strava' ), print_r( $strava_info, true ) ); // @codingStandardsIgnoreLine
return false;
}
} else {
@@ -310,7 +320,9 @@ class WPStrava_Settings {
}
public function print_gmaps_key_input() {
?><input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo $this->gmaps_key; ?>" /><?php
?>
<input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo $this->gmaps_key; ?>" />
<?php
}
public function sanitize_gmaps_key( $key ) {
@@ -320,8 +332,8 @@ class WPStrava_Settings {
public function print_som_input() {
?>
<select id="strava_som" name="strava_som">
<option value="metric" <?php selected( $this->som, 'metric' ); ?>><?php esc_html_e( 'Metric', 'wp-strava' )?></option>
<option value="english" <?php selected( $this->som, 'english' ); ?>><?php esc_html_e( 'English', 'wp-strava' )?></option>
<option value="metric" <?php selected( $this->som, 'metric' ); ?>><?php esc_html_e( 'Metric', 'wp-strava' ); ?></option>
<option value="english" <?php selected( $this->som, 'english' ); ?>><?php esc_html_e( 'English', 'wp-strava' ); ?></option>
</select>
<?php
}
@@ -331,7 +343,9 @@ class WPStrava_Settings {
}
public function print_clear_input() {
?><input type="checkbox" id="strava_cache_clear" name="strava_cache_clear" /><?php
?>
<input type="checkbox" id="strava_cache_clear" name="strava_cache_clear" />
<?php
}
public function sanitize_cache_clear( $checked ) {
@@ -393,10 +407,10 @@ class WPStrava_Settings {
* @since 1.2.0
*/
public function get_all_tokens() {
$tokens = $this->get_tokens();
$tokens = $this->get_tokens();
$nicknames = $this->nickname;
$all = array();
$number = 1;
$all = array();
$number = 1;
foreach ( $tokens as $index => $token ) {
if ( ! empty( $nicknames[ $index ] ) ) {
$all[ $token ] = $nicknames[ $index ];
@@ -419,7 +433,7 @@ class WPStrava_Settings {
*/
private function get_default_nickname( $number = 1 ) {
// Translators: Athelete number if no nickname present.
return ( 1 == $number ) ? __( 'Default', 'wp-strava' ) : sprintf( __( 'Athlete %s', 'wp-strava' ), $number );
return ( 1 === $number ) ? __( 'Default', 'wp-strava' ) : sprintf( __( 'Athlete %s', 'wp-strava' ), $number );
}
/**
@@ -437,7 +451,7 @@ class WPStrava_Settings {
}
if ( is_array( $tokens ) ) {
foreach( $tokens as $token ) {
foreach ( $tokens as $token ) {
if ( ! empty( $token ) ) {
return false;
}
@@ -471,7 +485,7 @@ class WPStrava_Settings {
public function settings_link( $links ) {
$settings_link = '<a href="' . admin_url( "options-general.php?page={$this->page_name}" ) . '">' . __( 'Settings', 'wp-strava' ) . '</a>';
$links[] = $settings_link;
$links[] = $settings_link;
return $links;
}
+30 -24
View File
@@ -2,8 +2,6 @@
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Settings.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/SOM.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/ActivityShortcode.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/RouteShortcode.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/StaticMap.class.php';
@@ -11,10 +9,10 @@ require_once WPSTRAVA_PLUGIN_DIR . 'lib/StaticMap.class.php';
class WPStrava {
private static $instance = null;
private $settings = null;
private $api = array(); // Holds an array of APIs.
private $rides = null;
private $routes = null;
private $settings = null;
private $api = array(); // Holds an array of APIs.
private $rides = null;
private $routes = null;
private function __construct() {
$this->settings = new WPStrava_Settings();
@@ -25,14 +23,13 @@ class WPStrava {
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
}
// Register StravaLatestRidesWidget widget
add_action( 'widgets_init', create_function( '', 'return register_widget( "WPStrava_LatestRidesWidget" );' ) );
add_action( 'widgets_init', create_function( '', 'return register_widget( "WPStrava_LatestMapWidget" );' ) );
// Register widgets.
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
}
public static function get_instance() {
if ( ! self::$instance ) {
$class = __CLASS__;
$class = __CLASS__;
self::$instance = new $class();
}
return self::$instance;
@@ -40,11 +37,13 @@ class WPStrava {
public function __get( $name ) {
// On-demand classes.
if ( $name == 'rides' ) {
if ( 'rides' === $name ) {
return $this->get_rides();
} elseif ( $name == 'routes' ) {
return $this->get_routes();
}
}
if ( 'routes' === $name ) {
return $this->get_routes();
}
if ( isset( $this->{$name} ) ) {
return $this->{$name};
@@ -60,10 +59,10 @@ class WPStrava {
if ( empty( $this->api[ $token ] ) ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/API.class.php';
$this->api[$token] = new WPStrava_API( $token );
$this->api[ $token ] = new WPStrava_API( $token );
}
return $this->api[$token];
return $this->api[ $token ];
}
public function get_rides() {
@@ -75,16 +74,23 @@ class WPStrava {
return $this->rides;
}
public function get_routes() {
if ( ! $this->routes ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Routes.class.php';
$this->routes = new WPStrava_Routes();
}
return $this->routes;
}
public function get_routes() {
if ( ! $this->routes ) {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Routes.class.php';
$this->routes = new WPStrava_Routes();
}
return $this->routes;
}
public function register_scripts() {
// Register a personalized stylesheet
// Register a personalized stylesheet.
wp_register_style( 'wp-strava-style', WPSTRAVA_PLUGIN_URL . 'css/wp-strava.css' );
}
public function register_widgets() {
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php';
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestMapWidget.class.php';
register_widget( 'WPStrava_LatestRidesWidget' );
register_widget( 'WPStrava_LatestMapWidget' );
}
}
+5 -3
View File
@@ -2,8 +2,8 @@
/*
* Plugin Name: WP Strava
* Plugin URI: https://wordpress.org/plugins/wp-strava/
* Description: Plugin to show your strava.com information in your wordpress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license.
* Version: 1.2.0
* Description: Plugin to show your strava.com information in your WordPress blog. Some Icons are Copyright © Yusuke Kamiyamane. All rights reserved. Licensed under a Creative Commons Attribution 3.0 license.
* Version: 1.3.0
* Author: Carlos Santa Cruz, Justin Foell, Lance Willet
* License: GPL2
* Text Domain: wp-strava
@@ -30,7 +30,9 @@
define( 'WPSTRAVA_PLUGIN_DIR', trailingslashit( dirname( __FILE__ ) ) );
define( 'WPSTRAVA_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
define( 'WPSTRAVA_PLUGIN_NAME', plugin_basename( __FILE__ ) );
if ( ! defined( 'WPSTRAVA_DEBUG' ) ) define( 'WPSTRAVA_DEBUG', false );
if ( ! defined( 'WPSTRAVA_DEBUG' ) ) {
define( 'WPSTRAVA_DEBUG', false );
}
// Load the multilingual support.
function wp_strava_load_plugin_textdomain() {