mirror of
https://github.com/10h30/wp-strava.git
synced 2026-06-05 15:10:01 +09:00
Compartmentalized classes
Restructured settings to use the WP Settings API
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* WP Strava Latest Rides Widget Class
|
||||
*/
|
||||
class WPStrava_LatestRidesWidget extends WP_Widget {
|
||||
function __construct() {
|
||||
$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);
|
||||
wp_enqueue_style('wp-strava');
|
||||
}
|
||||
|
||||
/** @see WP_Widget::widget */
|
||||
function widget($args, $instance) {
|
||||
extract($args);
|
||||
|
||||
//$widget_id = $args['widget_id'];
|
||||
$title = apply_filters('widget_title', empty($instance['title']) ? _e('Rides', 'wp-strava') : $instance['title']);
|
||||
$strava_search_option = empty($instance['strava_search_option']) ? 'athlete' : $instance['strava_search_option'];
|
||||
$strava_som_option = empty($instance['strava_som_option']) ? 'metric' : $instance['strava_som_option'];
|
||||
$strava_search_id = empty($instance['strava_search_id']) ? '' : $instance['strava_search_id'];
|
||||
$quantity = empty($instance['quantity']) ? '5' : $instance['quantity'];
|
||||
|
||||
?>
|
||||
<?php echo $before_widget; ?>
|
||||
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
|
||||
<?php echo strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity); ?>
|
||||
<?php echo $after_widget; ?>
|
||||
<?php
|
||||
}
|
||||
|
||||
/** @see WP_Widget::update */
|
||||
function update($new_instance, $old_instance) {
|
||||
$instance = $old_instance;
|
||||
$instance['title'] = strip_tags($new_instance['title']);
|
||||
if(in_array($new_instance['strava_search_option'], array('athlete', 'club'))) {
|
||||
$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'];
|
||||
} else {
|
||||
$instance['strava_som_option'] = 'metric';
|
||||
}
|
||||
$instance['strava_search_id'] = strip_tags($new_instance['strava_search_id']);
|
||||
$instance['quantity'] = $new_instance['quantity'];
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/** @see WP_Widget::form */
|
||||
function form($instance) {
|
||||
$title = isset($instance['title']) ? esc_attr($instance['title']) : _e('Rides', 'wp-strava');
|
||||
$strava_search_option = isset($instance['strava_search_option']) ? esc_attr($instance['strava_search_option']) : "athlete";
|
||||
$strava_som_option = isset($instance['strava_som_option']) ? esc_attr($instance['strava_som_option']) : "metric";
|
||||
$strava_search_id = isset($instance['strava_search_id']) ? esc_attr($instance['strava_search_id']) : "";
|
||||
$quantity = isset($instance['quantity']) ? absint($instance['quantity']) : 5;
|
||||
|
||||
?>
|
||||
<p>
|
||||
<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; ?>" />
|
||||
</p>
|
||||
<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>
|
||||
<label for="<?php echo $this->get_field_id('strava_som_option'); ?>"><?php _e('System of Measurement:'); ?></label>
|
||||
<select class="widefat" id="<?php echo $this->get_field_id('strava_som_option'); ?>" name="<?php echo $this->get_field_name('strava_som_option'); ?>">
|
||||
<option value="metric" <?php selected($strava_som_option, 'metric'); ?>><?php _e("Metric", "wp-strava")?></option>
|
||||
<option value="english" <?php selected($strava_som_option, 'english'); ?>><?php _e("English", "wp-strava")?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id('strava_search_id'); ?>"><?php _e('Search Id:'); ?></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; ?>" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo $this->get_field_id('quantity'); ?>"><?php _e('Quantity:'); ?></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
|
||||
}
|
||||
} // class LatestRidesWidget
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
class WPStrava_RideShortcode {
|
||||
static $add_script;
|
||||
|
||||
static function init() {
|
||||
add_shortcode('ride', array(__CLASS__, 'handler'));
|
||||
|
||||
add_action('init', array(__CLASS__, 'registerScripts'));
|
||||
add_action('wp_footer', array(__CLASS__, 'printScripts'));
|
||||
}
|
||||
|
||||
// Shortcode handler function
|
||||
// [ride id=id som=metric efforts=false threshold=5 map-width="100%" map-height="400px"] tag
|
||||
function handler($atts) {
|
||||
self::$add_script = true;
|
||||
|
||||
$token = get_option('strava_token');
|
||||
|
||||
if($token) {
|
||||
$pairs = array(
|
||||
'id' => 0,
|
||||
'som' => "metric",
|
||||
'efforts' => false,
|
||||
'threshold' => 0,
|
||||
'map_width' => "100%",
|
||||
'map_height' => "400px"
|
||||
);
|
||||
|
||||
extract(shortcode_atts($pairs, $atts));
|
||||
|
||||
if (isset($som)) {
|
||||
$strava_som = $som;
|
||||
} else {
|
||||
$strava_som = get_option('strava_som_option', 'metric');
|
||||
}
|
||||
|
||||
$ride = new Rides();
|
||||
$rideDetails = $ride->getRideDetails($id, $strava_som);
|
||||
$rideCoordinates = $ride->getRideMap($id, $token, $efforts, $threshold);
|
||||
|
||||
if ($strava_som == "metric") {
|
||||
$units = array(
|
||||
'elapsedTime' => __('hours','wp-strava'),
|
||||
'movingTime' => __('hours','wp-strava'),
|
||||
'distance' => __('km','wp-strava'),
|
||||
'averageSpeed' => __('km/h','wp-strava'),
|
||||
//'maximumSpeed' => __('km/h','wp-strava'),
|
||||
'elevationGain' => __('meters','wp-strava')
|
||||
);
|
||||
} elseif ($strava_som == "english") {
|
||||
$units = array(
|
||||
'elapsedTime' => __('hours','wp-strava'),
|
||||
'movingTime' => __('hours','wp-strava'),
|
||||
'distance' => __('miles','wp-strava'),
|
||||
'averageSpeed' => __('miles/h','wp-strava'),
|
||||
//'maximumSpeed' => __('miles/h','wp-strava'),
|
||||
'elevationGain' => __('feet','wp-strava')
|
||||
);
|
||||
}
|
||||
|
||||
if($rideCoordinates) {
|
||||
return "
|
||||
<div id='ride-header-{$id}' class='table'>
|
||||
<table id='ride-details-table'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Elapsed Time</th>
|
||||
<th>Moving Time</th>
|
||||
<th>Distance</th>
|
||||
<th>Average Speed</th>
|
||||
<th>Elevation Gain</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr class='ride-details-table-info'>
|
||||
<td>{$rideDetails['elapsedTime']}</td>
|
||||
<td>{$rideDetails['movingTime']}</td>
|
||||
<td>{$rideDetails['distance']}</td>
|
||||
<td>{$rideDetails['averageSpeed']}</td>
|
||||
<td>{$rideDetails['elevationGain']}</td>
|
||||
</tr>
|
||||
<tr class='ride-details-table-units'>
|
||||
<td>{$units['elapsedTime']}</td>
|
||||
<td>{$units['movingTime']}</td>
|
||||
<td>{$units['distance']}</td>
|
||||
<td>{$units['averageSpeed']}</td>
|
||||
<td>{$units['elevationGain']}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div id='{$id}' class='map' style='width: {$map_width}; height: {$map_height}; border: 1px solid lightgrey;'></div>
|
||||
<script type='text/javascript'>
|
||||
if (window.coordinates === undefined) {
|
||||
window.coordinates = [];
|
||||
}
|
||||
window.coordinates[{$id}] = eval({$rideCoordinates});
|
||||
</script>
|
||||
";
|
||||
}
|
||||
} else {
|
||||
return _e('Please first get your strava token using the settings wp strava page.', 'wp-strava');
|
||||
}
|
||||
} // handler
|
||||
|
||||
static function registerScripts() {
|
||||
wp_register_style('wp-strava-style', plugins_url('css/wp-strava.css', __FILE__));
|
||||
|
||||
wp_register_script('wp-strava-script', plugins_url('js/wp-strava.js', __FILE__), array('jquery'), '1.0', true);
|
||||
wp_register_script('google-maps', 'http://maps.google.com/maps/api/js?sensor=false');
|
||||
}
|
||||
|
||||
static function printScripts() {
|
||||
if (self::$add_script) {
|
||||
wp_enqueue_style('wp-strava-style');
|
||||
wp_enqueue_script('jquery');
|
||||
|
||||
wp_print_scripts('google-maps');
|
||||
wp_print_scripts('wp-strava-script');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize short code
|
||||
RideShortcode::init();
|
||||
Executable
+154
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/*
|
||||
* Rides is a class wrapper for the Strava REST API functions.
|
||||
*/
|
||||
class WPStrava_Rides {
|
||||
private $rideUrl = "http://www.strava.com/api/v1/rides/:id";
|
||||
private $rideUrlV2 = "http://www.strava.com/api/v2/rides/:id";
|
||||
private $ridesUrl = "http://www.strava.com/api/v1/rides";
|
||||
private $authenticationUrl = "https://www.strava.com/api/v1/authentication/login";
|
||||
private $authenticationUrlV2 = "https://www.strava.com/api/v2/authentication/login";
|
||||
private $rideMapDetailsUrl = "http://www.strava.com/api/v1/rides/:id/map_details";
|
||||
private $rideMapDetailsUrlV2 = "http://www.strava.com/api/v2/rides/:id/map_details";
|
||||
|
||||
public $ridesLinkUrl = "http://app.strava.com/rides/";
|
||||
public $athletesLinkUrl = "http://app.strava.com/athletes/";
|
||||
public $stravaRides;
|
||||
public $feedback;
|
||||
|
||||
public function __construct() {
|
||||
// Empty constructor
|
||||
} // __construct
|
||||
|
||||
public function getRideDetails($rideId, $systemOfMeasurement) {
|
||||
$url = preg_replace('/:id/', $rideId, $this->rideUrl);
|
||||
$json = file_get_contents($url);
|
||||
|
||||
if($json) {
|
||||
$strava_ride = json_decode($json);
|
||||
|
||||
//Transform data to a ready to be displayed format
|
||||
$startDate = date("F j, Y - H:i a", strtotime($strava_ride->ride->startDateLocal));
|
||||
$elapsedTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->elapsedTime));
|
||||
$movingTime = date("H:i:s", mktime(0, 0, $strava_ride->ride->movingTime));
|
||||
|
||||
if ($systemOfMeasurement == "metric") {
|
||||
//To km
|
||||
$distance = number_format($strava_ride->ride->distance/1000, 2);
|
||||
//To km/h
|
||||
$averageSpeed = number_format($strava_ride->ride->averageSpeed * 3.6, 2);
|
||||
//To km/h
|
||||
// Removed on version 2 of the Strava API
|
||||
//$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1000, 2);
|
||||
//It is already in meters
|
||||
$elevationGain = number_format($strava_ride->ride->elevationGain, 2);
|
||||
} elseif ($systemOfMeasurement == "english") {
|
||||
//To miles
|
||||
$distance = number_format($strava_ride->ride->distance/1609.34, 2);
|
||||
//To miles/h
|
||||
$averageSpeed = number_format($strava_ride->ride->averageSpeed * 2.2369, 2);
|
||||
//To miles/h
|
||||
// Removed on version 2 of the Strava API
|
||||
//$maximumSpeed = number_format($strava_ride->ride->maximumSpeed/1609.34, 2);
|
||||
//To foot
|
||||
$elevationGain = number_format($strava_ride->ride->elevationGain/0.3048, 2);
|
||||
}
|
||||
|
||||
$ride_details = array(
|
||||
'id' => $rideId,
|
||||
'name' => $strava_ride->ride->name,
|
||||
'athleteId' => $strava_ride->ride->athlete->id,
|
||||
'athleteName' => $strava_ride->ride->athlete->name,
|
||||
'athleteUserName' => $strava_ride->ride->athlete->username,
|
||||
'startDate' => $startDate,
|
||||
'elapsedTime' => $elapsedTime,
|
||||
'movingTime' => $movingTime,
|
||||
'distance' => $distance,
|
||||
'averageSpeed' => $averageSpeed,
|
||||
//'maximumSpeed' => $maximumSpeed,
|
||||
'elevationGain' => $elevationGain
|
||||
);
|
||||
return $ride_details;
|
||||
} else {
|
||||
$this->feedback .= _e("Could not get information from strava.com for the ride id: ") . $stravaRide->id;
|
||||
return false;
|
||||
}
|
||||
} // getRideDetails
|
||||
|
||||
public function getRidesDetails($systemOfMeasurement) {
|
||||
if($this->stravaRides) {
|
||||
$rides_details = array();
|
||||
foreach($this->stravaRides as $stravaRide) {
|
||||
$rides_details[] = $this->getRideDetails($stravaRide->id, $systemOfMeasurement);
|
||||
}
|
||||
return $rides_details;
|
||||
} else {
|
||||
$this->feedback .= _e("Please provide the rides array to be processed.", "wp-strava");
|
||||
return false;
|
||||
}
|
||||
} // getRidesDetails
|
||||
|
||||
public function getLatestRides($searchOption, $searchId, $quantity) {
|
||||
$url = $this->ridesUrl;
|
||||
//Get the json results using the constructor specified values.
|
||||
if($searchOption == "athlete") {
|
||||
if(is_numeric($searchId)) {
|
||||
$json = file_get_contents($url . '?athleteId=' . urlencode($searchId));
|
||||
} else {
|
||||
$json = file_get_contents($url . '?athleteName=' . urlencode($searchId));
|
||||
}
|
||||
} elseif ($searchOption == "club" AND is_numeric($searchId)) {
|
||||
$json = file_get_contents($url . '?clubId=' . urlencode($searchId));
|
||||
} else {
|
||||
$this->feedback .= _e("There's an error on the widget options combination.", "wp-strava");
|
||||
}
|
||||
if($json) {
|
||||
$strava_rides = json_decode($json);
|
||||
$this->stravaRides = array_slice($strava_rides->rides, 0, $quantity);
|
||||
} else {
|
||||
$this->feedback .= _e("There was an error pulling data of strava.com.", "wp-strava");
|
||||
return false;
|
||||
}
|
||||
} // getLatestRides
|
||||
|
||||
public function getAuthenticationToken($email, $password) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Util.class.php';
|
||||
$util = new WPStrava_Util();
|
||||
$data = array('email' => $email, 'password' => $password);
|
||||
$json = $util->makePostRequest($this->authenticationUrlV2, $data);
|
||||
|
||||
if($json) {
|
||||
$strava_login = json_decode($json);
|
||||
if(!isset($strava_login->error)) {
|
||||
$this->feedback .= __('Successfully authenticated.', 'wp-strava');
|
||||
return $strava_login->token;
|
||||
} else {
|
||||
$this->feedback .= __('Authentication failed, please check your credentials.', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= __('There was an error pulling data of strava.com.', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
} // getAuthenticationToken
|
||||
|
||||
public function getRideMap($rideId, $token, $efforts, $threshold) {
|
||||
if($rideId != 0 AND $token != "") {
|
||||
$url = preg_replace('/:id/', $rideId, $this->rideMapDetailsUrlV2);
|
||||
$json = file_get_contents($url . '?token=' . $token . '&threshold=' . $threshold);
|
||||
|
||||
if($json) {
|
||||
//$map_details = json_decode($json);
|
||||
//return $map_details;
|
||||
return $json;
|
||||
} else {
|
||||
$this->feedback .= _e("There was an error pulling data of strava.com.", "wp-strava");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= _e("You need to provide both parameters to complete the call.", "wp-strava");
|
||||
return false;
|
||||
}
|
||||
} // getRideDetails
|
||||
} // class Rides
|
||||
?>
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
class WPStrava_Settings {
|
||||
|
||||
//register admin menus
|
||||
public function hook() {
|
||||
add_action( 'admin_init', array( $this, 'registerStravaSettings' ) );
|
||||
add_action( 'admin_menu', array( $this, 'addStravaMenu' ) );
|
||||
}
|
||||
|
||||
public function addStravaMenu() {
|
||||
add_options_page( __( 'Strava Settings', 'wp-strava' ),
|
||||
__( 'Strava', 'wp-strava' ),
|
||||
'manage_options',
|
||||
'wp-strava-options',
|
||||
array( $this, 'printStravaOptions' ) );
|
||||
|
||||
}
|
||||
|
||||
public function registerStravaSettings() {
|
||||
register_setting('wp-strava-settings-group','strava_email', array( $this, 'sanitizeEmail' ) );
|
||||
register_setting('wp-strava-settings-group','strava_password', '__return_null' );
|
||||
register_setting('wp-strava-settings-group','strava_token', array( $this, 'sanitizeToken' ) );
|
||||
|
||||
add_settings_section( 'strava_api', NULL, '__return_null', 'wp-strava' ); //NULL / __return_null no section label needed
|
||||
add_settings_field( 'strava_email', 'Strava Email', array( $this, 'printEmailInput' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_password', 'Strava Password', array( $this, 'printPasswordInput' ), 'wp-strava', 'strava_api' );
|
||||
add_settings_field( 'strava_token', 'Strava Token', array( $this, 'printTokenInput' ), 'wp-strava', 'strava_api' );
|
||||
|
||||
}
|
||||
|
||||
public function printStravaOptions() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<div id="icon-options-general" class="icon32"><br/></div>
|
||||
<h2><?php _e( 'Strava Options', 'wp-strava' ); ?></h2>
|
||||
<p><?php _e( 'Please specify the options below in order to obtain an authentication token, this will work with the Strava shortcodes supported by this plugin, the widget options are independant.', 'wp-strava');?> </p>
|
||||
|
||||
<form method="post" action="<?php echo admin_url( 'options.php' ); ?>">
|
||||
<?php settings_fields( 'wp-strava-settings-group' ); ?>
|
||||
<?php do_settings_sections( 'wp-strava' ); ?>
|
||||
|
||||
<p class="submit">
|
||||
<input type="submit" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
|
||||
</p>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function printEmailInput() {
|
||||
?><input type="text" id="strava_email" name="strava_email" value="<?php echo get_option('strava_email'); ?>" /><?php
|
||||
}
|
||||
|
||||
public function printPasswordInput() {
|
||||
?>
|
||||
<input type="password" id="strava_password" name="strava_password" value="" />
|
||||
<p class="description"><?php _e( 'Your Password WILL NOT be saved. Only enter your password if you wish to retrieve a new API Token', 'wp-strava' ); ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function printTokenInput() {
|
||||
?><input type="text" id="strava_token" name="strava_token" value="<?php echo get_option('strava_token'); ?>" /><?php
|
||||
}
|
||||
|
||||
public function sanitizeEmail( $email ) {
|
||||
if ( is_email( $email ) )
|
||||
return $email;
|
||||
add_settings_error( 'strava_email', 'strava_email', 'Invalid Email' );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
public function sanitizeToken( $token ) {
|
||||
if ( ! empty( $_POST['strava_password'] ) ) {
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Rides.class.php';
|
||||
$ride = new WPStrava_Rides();
|
||||
$email = get_option( 'strava_email' );
|
||||
$token = $ride->getAuthenticationToken( $email, $_POST['strava_password'] );
|
||||
if ( $token ) {
|
||||
add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava Token Retrieved: %s', 'wp-strava' ), $ride->feedback ) , 'updated' );
|
||||
return $token;
|
||||
} else {
|
||||
add_settings_error( 'strava_token', 'strava_token', $ride->feedback );
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/Settings.class.php';
|
||||
require_once WPSTRAVA_PLUGIN_DIR . 'lib/LatestRidesWidget.class.php';
|
||||
|
||||
class WPStrava {
|
||||
|
||||
private static $instance = NULL;
|
||||
public $settings = NULL;
|
||||
|
||||
private function __construct() {
|
||||
$this->settings = new WPStrava_Settings();
|
||||
|
||||
if ( is_admin() ) {
|
||||
$this->settings->hook();
|
||||
}
|
||||
|
||||
// Register StravaLatestRidesWidget widget
|
||||
add_action('widgets_init', function() { return register_widget('WPStrava_LatestRidesWidget'); });
|
||||
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if ( ! self::$instance )
|
||||
self::$instance = new WPStrava();
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
// 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.
|
||||
function strava_request_handler($strava_search_option, $strava_search_id, $strava_som_option, $quantity) {
|
||||
$response = "";
|
||||
|
||||
//Check if the username is empty.
|
||||
if (empty($strava_search_id)) {
|
||||
$response .= __("Please configure the Strava search id on the widget options.", "wp-strava");
|
||||
} else {
|
||||
$strava_rides = new Rides();
|
||||
$strava_rides->getLatestRides($strava_search_option, $strava_search_id, $quantity);
|
||||
$rides_details = $strava_rides->getRidesDetails($strava_som_option);
|
||||
|
||||
if ($strava_som_option == "metric") {
|
||||
$units = array(
|
||||
'elapsedTime' => __('hours','wp-strava'),
|
||||
'movingTime' => __('hours','wp-strava'),
|
||||
'distance' => __('km','wp-strava'),
|
||||
'averageSpeed' => __('km/h','wp-strava'),
|
||||
'maximumSpeed' => __('km/h','wp-strava'),
|
||||
'elevationGain' => __('meters','wp-strava')
|
||||
);
|
||||
} elseif ($strava_som_option == "english") {
|
||||
$units = array(
|
||||
'elapsedTime' => __('hours','wp-strava'),
|
||||
'movingTime' => __('hours','wp-strava'),
|
||||
'distance' => __('miles','wp-strava'),
|
||||
'averageSpeed' => __('miles/h','wp-strava'),
|
||||
'maximumSpeed' => __('miles/h','wp-strava'),
|
||||
'elevationGain' => __('feet','wp-strava')
|
||||
);
|
||||
}
|
||||
|
||||
$response .= "<ul id='rides'>";
|
||||
foreach($rides_details as $ride) {
|
||||
$response .= "<li class='ride'>";
|
||||
$response .= "<a href='" . $strava_rides->ridesLinkUrl . $ride['id'] . "' >" . $ride['name'] . "</a>";
|
||||
$response .= "<div class='ride-item'>";
|
||||
$response .= __("On ", "wp-strava") . $ride['startDate'];
|
||||
if ($strava_search_option == "club") {
|
||||
$response .= " <a href='" . $strava_rides->athletesLinkUrl . $ride['athleteId'] . "'>" . $ride['athleteName'] . "</a>";
|
||||
}
|
||||
$response .= __(" rode ", "wp-strava") . $ride['distance'] . " " . $units['distance'];
|
||||
$response .= __(" during ", "wp-strava") . $ride['elapsedTime'] . " " . $units['elapsedTime'];
|
||||
$response .= __(" climbing ", "wp-strava") . $ride['elevationGain'] . " " . $units['elevationGain'] . ".";
|
||||
$response .= "</div>";
|
||||
$response .= "</li>";
|
||||
}
|
||||
$response .= "</ul>";
|
||||
}
|
||||
return $response;
|
||||
} // Function strava_request_handler
|
||||
|
||||
}
|
||||
Executable
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* Util is a class with all the utility methods.
|
||||
*/
|
||||
class WPStrava_Util {
|
||||
public function makePostRequest ($url, $data) {
|
||||
$data = http_build_query($data);
|
||||
$url = parse_url($url);
|
||||
|
||||
if ($url['scheme'] == "http") {
|
||||
$port = 80;
|
||||
$domain = "tcp://";
|
||||
} elseif ($url['scheme'] == "https") {
|
||||
$port = 443;
|
||||
$domain = "ssl://";
|
||||
} else {
|
||||
$this->feedback .= __('This function only support http and https', 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
|
||||
$host = $url['host'];
|
||||
$path = $url['path'];
|
||||
$protocol = $url['scheme'] . "://";
|
||||
|
||||
// Open a socket connection to the specified port - timeout 30 seconds
|
||||
$fp = fsockopen($domain . $host, $port, $error_number, $error_string, 30);
|
||||
|
||||
if ($fp) {
|
||||
// Build the request headers and data
|
||||
$request = "POST " . $protocol . $host . $path . " HTTP/1.0\r\n";
|
||||
$request .= "Host: " . $host . "\r\n";
|
||||
$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||
$request .= "Content-Length: " . strlen($data) . "\r\n";
|
||||
$request .= "Connection: close\r\n\r\n";
|
||||
$request .= $data;
|
||||
|
||||
fputs($fp, $request);
|
||||
|
||||
$result = "";
|
||||
while (!feof($fp)) {
|
||||
$result .= fgets($fp, 128);
|
||||
}
|
||||
} else {
|
||||
$this->feedback .= __('ERROR - ' . $error_string . '-' . $error_number, 'wp-strava');
|
||||
return false;
|
||||
}
|
||||
|
||||
fclose($fp);
|
||||
|
||||
// Split the result header from the content
|
||||
$result = explode("\r\n\r\n", $result, 2);
|
||||
$header = isset($result[0]) ? $result[0] : '';
|
||||
$content = isset($result[1]) ? $result[1] : '';
|
||||
|
||||
return $content;
|
||||
} // makePostRequest
|
||||
} // class Util
|
||||
Reference in New Issue
Block a user