2013-02-03 20:38:55 -06:00
< ? php
2018-04-27 21:19:27 -05:00
/**
* Activity Shortcode [activity].
* @package WPStrava
*/
2013-02-03 20:38:55 -06:00
2018-04-27 21:19:27 -05:00
/**
* Activity Shortcode class (converted from Ride).
*
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
2017-09-29 10:52:57 -05:00
class WPStrava_ActivityShortcode {
2013-02-03 20:38:55 -06:00
2018-04-27 21:19:27 -05:00
/**
* Whether or not to enqueue styles (if shortcode is present).
*
* @var boolean
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
private $add_script = false ;
/**
* Constructor (converted from static init()).
*
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
public function __construct () {
add_shortcode ( 'ride' , array ( $this , 'handler' ) ); // @deprecated 1.1
add_shortcode ( 'activity' , array ( $this , 'handler' ) );
add_action ( 'wp_footer' , array ( $this , 'print_scripts' ) );
2013-02-03 20:38:55 -06:00
}
2018-04-27 21:19:27 -05:00
/**
* Shortcode handler for [activity].
*
* [activity id=id som=metric map_width="100%" map_height="400px" markers=false]
*
* @param array $atts Array of attributes (id, map_width, etc.).
* @return string Shortcode output
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
public function handler ( $atts ) {
$this -> add_script = true ;
2013-02-03 20:38:55 -06:00
2014-11-01 22:53:32 -05:00
$defaults = array (
2017-09-29 10:52:57 -05:00
'id' => 0 ,
'som' => WPStrava :: get_instance () -> settings -> som ,
'map_width' => '480' ,
'map_height' => '320' ,
2019-06-03 16:07:15 -05:00
'client_id' => WPStrava :: get_instance () -> settings -> get_default_id (),
2017-12-26 14:18:13 -06:00
'markers' => false ,
2019-02-01 11:59:00 -06:00
'image_only' => false ,
2014-11-01 22:53:32 -05:00
);
2017-05-26 10:53:24 -05:00
2019-02-01 11:59:00 -06:00
$atts = shortcode_atts ( $defaults , $atts , 'activity' );
2019-06-03 16:07:15 -05:00
if ( isset ( $atts [ 'athlete_token' ] ) ) {
// Translators: Message shown when using deprecated athlete_token parameter.
2019-10-04 16:07:07 -05:00
return __ ( 'The <code>athlete_token</code> parameter is deprecated as of WP-Strava version 2 and should be replaced with <code>client_id</code>.' , 'wp-strava' );
2019-06-03 16:07:15 -05:00
}
2019-02-01 11:59:00 -06:00
/* Make sure boolean values are actually boolean
* @see https://wordpress.stackexchange.com/a/119299
*/
$atts [ 'markers' ] = filter_var ( $atts [ 'markers' ], FILTER_VALIDATE_BOOLEAN );
$atts [ 'image_only' ] = filter_var ( $atts [ 'image_only' ], FILTER_VALIDATE_BOOLEAN );
2017-05-26 10:53:24 -05:00
2018-01-26 13:27:57 -06:00
$activity = WPStrava :: get_instance () -> activity ;
2018-04-27 21:19:27 -05:00
$activity_details = null ;
2018-01-26 13:27:57 -06:00
2018-04-27 21:19:27 -05:00
try {
2019-06-03 16:07:15 -05:00
$activity_details = $activity -> get_activity ( $atts [ 'client_id' ], $atts [ 'id' ] );
2018-11-09 15:18:27 -06:00
} catch ( WPStrava_Exception $e ) {
2018-04-27 21:19:27 -05:00
return $e -> to_html ();
2018-01-26 13:27:57 -06:00
}
2013-02-03 20:38:55 -06:00
2019-02-01 14:16:28 -06:00
$activity_output = '' ;
2018-01-26 13:27:57 -06:00
if ( $activity_details ) {
2019-02-01 14:16:28 -06:00
$activity_output .= '<div id="activity-header-' . $atts [ 'id' ] . '" class="wp-strava-activity-container">' ;
2019-02-01 11:59:00 -06:00
if ( ! $atts [ 'image_only' ] ) {
2019-02-01 13:19:47 -06:00
$activity_output .= $this -> get_table ( $activity_details , $atts [ 'som' ] );
2019-02-01 11:59:00 -06:00
}
2019-02-01 14:16:28 -06:00
// Sanitize width & height.
$map_width = str_replace ( '%' , '' , $atts [ 'map_width' ] );
$map_height = str_replace ( '%' , '' , $atts [ 'map_height' ] );
$map_width = str_replace ( 'px' , '' , $map_width );
$map_height = str_replace ( 'px' , '' , $map_height );
2019-02-01 11:59:00 -06:00
$activity_output .= '<a title="' . $activity_details -> name . '" href="' . WPStrava_Activity :: ACTIVITIES_URL . $activity_details -> id . '">' .
WPStrava_StaticMap :: get_image_tag ( $activity_details , $map_height , $map_width , $atts [ 'markers' ] ) .
'</a>
</div>' ;
2018-01-26 13:27:57 -06:00
} // End if( $activity_details ).
2019-02-01 14:16:28 -06:00
return $activity_output ;
2018-01-26 13:27:57 -06:00
}
2013-02-03 20:38:55 -06:00
2019-02-01 13:19:47 -06:00
/**
* The the activity details in in HTML table.
*
* @param string $activity_details Activity details from the activity class.
* @param string $som System of measure (english/metric).
* @return string HTML Table of activity details.
* @author Justin Foell <justin@foell.org>
* @author Sebastian Erb <mail@sebastianerb.com>
2019-02-01 14:22:25 -06:00
* @since 1.7.0
2019-02-01 13:19:47 -06:00
*/
private function get_table ( $activity_details , $som ) {
$strava_som = WPStrava_SOM :: get_som ( $som );
$strava_activitytype = WPStrava_ActivityType :: get_type_group ( $activity_details -> type );
2019-08-02 14:23:46 -05:00
$avg_title = '<th>' . __ ( 'Average Speed' , 'wp-strava' ) . '</th>' ;
$max_title = '<th>' . __ ( 'Max Speed' , 'wp-strava' ) . '</th>' ;
$elevation_title = '<th>' . __ ( 'Elevation Gain' , 'wp-strava' ) . '</th>' ;
2019-02-01 13:19:47 -06:00
$avg_speed = '' ;
$max_speed = '' ;
2019-08-02 14:23:46 -05:00
$elevation = '<td>' . $strava_som -> elevation ( $activity_details -> total_elevation_gain ) . '</td>' ;
2019-02-01 13:19:47 -06:00
$speed_label = '' ;
2019-08-02 14:23:46 -05:00
$elevation_label = '<td>' . $strava_som -> get_elevation_label () . '</td>' ;
2019-02-01 13:19:47 -06:00
switch ( $strava_activitytype ) {
case WPStrava_ActivityType :: TYPE_GROUP_PACE :
$avg_speed = '<td>' . $strava_som -> pace ( $activity_details -> average_speed ) . '</td>' ;
$max_speed = '<td>' . $strava_som -> pace ( $activity_details -> max_speed ) . '</td>' ;
$speed_label = '<td>' . $strava_som -> get_pace_label () . '</td>' ;
break ;
case WPStrava_ActivityType :: TYPE_GROUP_SPEED :
$avg_speed = '<td>' . $strava_som -> speed ( $activity_details -> average_speed ) . '</td>' ;
$max_speed = '<td>' . $strava_som -> speed ( $activity_details -> max_speed ) . '</td>' ;
$speed_label = '<td>' . $strava_som -> get_speed_label () . '</td>' ;
break ;
case WPStrava_ActivityType :: TYPE_GROUP_PACE :
$avg_speed = '<td>' . $strava_som -> swimpace ( $activity_details -> average_speed ) . '</td>' ;
$max_speed = '<td>' . $strava_som -> swimpace ( $activity_details -> max_speed ) . '</td>' ;
$speed_label = '<td>' . $strava_som -> get_swimpace_label () . '</td>' ;
break ;
default :
$avg_title = '' ;
$max_title = '' ;
break ;
}
2019-08-02 14:23:46 -05:00
if ( WPStrava :: get_instance () -> settings -> hide_elevation ) {
$elevation = '' ;
$elevation_title = '' ;
$elevation_label = '' ;
}
2019-02-01 13:19:47 -06:00
return '
<table id="activity-details-table">
<thead>
<tr>
<th>' . __ ( 'Elapsed Time' , 'wp-strava' ) . '</th>
<th>' . __ ( 'Moving Time' , 'wp-strava' ) . '</th>
<th>' . __ ( 'Distance' , 'wp-strava' ) . '</th>
' . $avg_title . '
' . $max_title . '
2019-08-02 14:23:46 -05:00
' . $elevation_title . '
2019-02-01 13:19:47 -06:00
</tr>
</thead>
<tbody>
<tr class="activity-details-table-info">
<td>' . $strava_som -> time ( $activity_details -> elapsed_time ) . '</td>
<td>' . $strava_som -> time ( $activity_details -> moving_time ) . '</td>
<td>' . $strava_som -> distance ( $activity_details -> distance ) . '</td>
' . $avg_speed . '
' . $max_speed . '
2019-08-02 14:23:46 -05:00
' . $elevation . '
2019-02-01 13:19:47 -06:00
</tr>
<tr class="activity-details-table-units">
<td>' . $strava_som -> get_time_label () . '</td>
<td>' . $strava_som -> get_time_label () . '</td>
<td>' . $strava_som -> get_distance_label () . '</td>
' . $speed_label . '
' . $speed_label . '
2019-08-02 14:23:46 -05:00
' . $elevation_label . '
2019-02-01 13:19:47 -06:00
</tr>
</tbody>
</table>
' ;
}
2018-04-27 21:19:27 -05:00
/**
* Enqueue style if shortcode is being used.
*
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
public function print_scripts () {
if ( $this -> add_script ) {
2017-05-26 10:53:24 -05:00
wp_enqueue_style ( 'wp-strava-style' );
2013-02-03 20:38:55 -06:00
}
}
}