Skip to content

Commit

Permalink
Move shortcode adds to main class.
Browse files Browse the repository at this point in the history
Fixed to avoid undefined index/property with abridged club activity
data.
  • Loading branch information
jrfoell committed Mar 23, 2018
1 parent 55073b2 commit 3f08061
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
9 changes: 9 additions & 0 deletions lib/WPStrava.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ private function __construct() {
if ( is_admin() ) {
$this->settings->hook();
} else {
add_action( 'init', array( $this, 'register_shortcodes') );
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
}

// Register widgets.
add_action( 'widgets_init', array( $this, 'register_widgets' ) );

}

public static function get_instance() {
Expand Down Expand Up @@ -82,4 +84,11 @@ public function register_widgets() {
register_widget( 'WPStrava_LatestActivitiesWidget' );
register_widget( 'WPStrava_LatestMapWidget' );
}

public function register_shortcodes() {
add_shortcode( 'ride', array( 'WPStrava_ActivityShortcode', 'handler' ) ); // @deprecated 1.1
add_shortcode( 'activity', array( 'WPStrava_ActivityShortcode', 'handler' ) );
add_shortcode( 'activities', array( 'WPStrava_LatestActivitiesShortcode', 'handler' ) );
add_shortcode( 'route', array( 'WPStrava_RouteShortcode', 'handler' ) );
}
}
2 changes: 0 additions & 2 deletions lib/WPStrava/ActivityShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class WPStrava_ActivityShortcode {
private static $add_script;

public static function init() {
add_shortcode( 'ride', array( __CLASS__, 'handler' ) ); // @deprecated 1.1
add_shortcode( 'activity', array( __CLASS__, 'handler' ) );
add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) );
}

Expand Down
18 changes: 13 additions & 5 deletions lib/WPStrava/LatestActivities.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ public static function get_activities_html( $args ) {
$response = "<ul id='activities'>";
foreach ( $activities as $activity ) {
$response .= "<li class='activity'>";
$response .= "<a href='" . WPStrava_Activity::ACTIVITIES_URL . $activity->id . "'>" . $activity->name . '</a>';
$response .= empty( $activity->id ) ?
$activity->name :
"<a href='" . WPStrava_Activity::ACTIVITIES_URL . $activity->id . "'>" . $activity->name . '</a>';
$response .= "<div class='activity-item'>";
$unixtime = strtotime( $activity->start_date_local );
// Translators: Shows something like "On <date> <[went 10 miles] [during 2 hours] [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 ( ! empty( $activity->start_date_local ) ) {
$unixtime = strtotime( $activity->start_date_local );
// Translators: Shows something like "On <date> <[went 10 miles] [during 2 hours] [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( $args['strava_club_id'] ) ) {
$response .= " <a href='" . WPStrava_Activity::ATHLETES_URL . $activity->athlete->id . "'>" . $activity->athlete->firstname . ' ' . $activity->athlete->lastname . '</a>';
$name = $activity->athlete->firstname . ' ' . $activity->athlete->lastname;
$response .= empty( $activity->athlete->id ) ?
" {$name}" :
" <a href='" . WPStrava_Activity::ATHLETES_URL . $activity->athlete->id . "'>" . $name . '</a>';
}

// Translators: "went 10 miles"
Expand Down
1 change: 0 additions & 1 deletion lib/WPStrava/LatestActivitiesShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class WPStrava_LatestActivitiesShortcode {
private static $add_script;

public static function init() {
add_shortcode( 'activities', array( __CLASS__, 'handler' ) );
add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) );
}

Expand Down
16 changes: 9 additions & 7 deletions lib/WPStrava/LatestMapWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function form( $instance ) {

?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></label>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'wp-strava' ); ?></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>
Expand All @@ -36,11 +36,11 @@ public function form( $instance ) {
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>"><?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?></label>
<label for="<?php echo $this->get_field_id( 'distance_min' ); ?>"><?php echo sprintf( __( 'Min. Distance (%s):', 'wp-strava' ), $this->som->get_distance_label() ); ?></label>
<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 esc_html_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
Expand Down Expand Up @@ -78,7 +78,7 @@ public function widget( $args, $instance ) {

$activity = $activity_transient ? $activity_transient : null;

if ( ! $activity ) {
if ( ! $activity || empty( $activity->map ) ) {
$strava_activity = WPStrava::get_instance()->activity;
$activities = $strava_activity->get_activities( $athlete_token, $strava_club_id );

Expand Down Expand Up @@ -127,9 +127,11 @@ public function widget( $args, $instance ) {
echo $args['before_title'] . $title . $args['after_title'];
}

echo "<a title='{$activity->name}' href='" . WPStrava_Activity::ACTIVITIES_URL . "{$activity->id}'>";
echo $this->get_static_image( $id, $activity, $build_new );
echo '</a>';
echo empty( $activity->map ) ?
sprintf( __( 'Map not available for activity "%s"', 'wp-strava' ), $activity->name ) :
"<a title='{$activity->name}' href='" . WPStrava_Activity::ACTIVITIES_URL . "{$activity->id}'>" .
$this->get_static_image( $id, $activity, $build_new ) .
'</a>';
echo $args['after_widget'];
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/WPStrava/RouteShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class WPStrava_RouteShortcode {
private static $add_script;

public static function init() {
add_shortcode( 'route', array( __CLASS__, 'handler' ) );
add_action( 'wp_footer', array( __CLASS__, 'print_scripts' ) );
}

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ If your API key works with other Google Maps plugins but not WP Strava, you may

= 1.5.0 =

Added additional checks for abridged club data to avoid undefined index/property errors https://wordpress.org/support/topic/club-activities-bugs-strava/
Added composer with PSR-0 autoloader (will switch to PSR-4 once WP's PHP 5.2 requirement goes away).
Moved files into appropriate place to support autoloader.
Added WordPress-Extra coding standards rule definition to project
Expand Down
2 changes: 1 addition & 1 deletion wp-strava.php
Original file line number Diff line number Diff line change
Expand Up @@ -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: 1.5.0-r1
* Version: 1.5.0-rc2
* Author: Carlos Santa Cruz, Justin Foell, Lance Willett, Daniel Lintott
* License: GPL2
* Text Domain: wp-strava
Expand Down

0 comments on commit 3f08061

Please sign in to comment.