Files
wp-strava/src/WPStrava/ActivitiesListWidget.php
T

94 lines
4.3 KiB
PHP
Raw Normal View History

2017-12-26 15:26:32 -06:00
<?php
/**
* Activities List Widget.
* @package WPStrava
*/
2017-12-26 15:26:32 -06:00
/**
* Activities List Widget class (converted from LatestActivitiesWidget).
*
* @author Justin Foell <justin@foell.org>
* @since 2.3.0
*/
class WPStrava_ActivitiesListWidget extends WP_Widget {
2017-12-26 15:26:32 -06:00
public function __construct() {
$widget_ops = array(
'classname' => 'wp-strava-activities-list-widget',
'description' => __( 'Show a list of activities from strava.com.', 'wp-strava' ),
2017-12-26 15:26:32 -06:00
);
parent::__construct( 'wp-strava', __( 'Strava Activities List', 'wp-strava' ), $widget_ops );
2017-12-26 15:26:32 -06:00
add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue' ) );
}
public function maybe_enqueue() {
if ( is_active_widget( false, false, $this->id_base ) ) {
2019-10-05 22:04:01 -05:00
wp_enqueue_style( 'wp-strava-style' ); // Only load this when widget is loaded.
2017-12-26 15:26:32 -06:00
}
}
/** @see WP_Widget::widget */
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Activities List', 'wp-strava' ) : $instance['title'] );
2017-12-26 15:26:32 -06:00
$activities_args = array(
'client_id' => isset( $instance['client_id'] ) ? $instance['client_id'] : null,
2017-12-26 15:26:32 -06:00
'strava_club_id' => isset( $instance['strava_club_id'] ) ? $instance['strava_club_id'] : null,
'quantity' => isset( $instance['quantity'] ) ? $instance['quantity'] : null,
);
2019-11-01 14:26:57 -05:00
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Widget OK.
2017-12-26 15:26:32 -06:00
echo $args['before_widget'];
if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo WPStrava_ActivitiesList::get_activities_html( $activities_args );
2017-12-26 15:26:32 -06:00
echo $args['after_widget'];
2019-11-01 14:26:57 -05:00
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
2017-12-26 15:26:32 -06:00
}
/** @see WP_Widget::update */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
2019-10-05 22:04:01 -05:00
$instance['title'] = wp_strip_all_tags( $new_instance['title'] );
$instance['client_id'] = wp_strip_all_tags( $new_instance['client_id'] );
$instance['strava_club_id'] = wp_strip_all_tags( $new_instance['strava_club_id'] );
2017-12-26 15:26:32 -06:00
$instance['quantity'] = $new_instance['quantity'];
return $instance;
}
/** @see WP_Widget::form */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : __( 'Activities List', 'wp-strava' );
$all_ids = WPStrava::get_instance()->settings->get_all_ids();
$client_id = isset( $instance['client_id'] ) ? esc_attr( $instance['client_id'] ) : WPStrava::get_instance()->settings->get_default_id();
2017-12-26 15:26:32 -06:00
$strava_club_id = isset( $instance['strava_club_id'] ) ? esc_attr( $instance['strava_club_id'] ) : '';
$quantity = isset( $instance['quantity'] ) ? absint( $instance['quantity'] ) : 5;
?>
<p>
2019-11-01 14:26:57 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
2017-12-26 15:26:32 -06:00
</p>
<p>
2019-11-01 14:26:57 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'client_id' ) ); ?>"><?php esc_html_e( 'Athlete:', 'wp-strava' ); ?></label>
<select name="<?php echo esc_attr( $this->get_field_name( 'client_id' ) ); ?>">
<?php foreach ( $all_ids as $id => $nickname ) : ?>
2019-11-01 14:26:57 -05:00
<option value="<?php echo esc_attr( $id ); ?>"<?php selected( $id, $client_id ); ?>><?php echo esc_html( $nickname ); ?></option>
2017-12-26 15:26:32 -06:00
<?php endforeach; ?>
</select>
</p>
<p>
2019-11-01 14:26:57 -05:00
<label for="<?php echo esc_attr( $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 esc_attr( $this->get_field_id( 'strava_club_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'strava_club_id' ) ); ?>" type="text" value="<?php echo esc_attr( $strava_club_id ); ?>" />
2017-12-26 15:26:32 -06:00
</p>
<p>
2019-11-01 14:26:57 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'quantity' ) ); ?>"><?php esc_html_e( 'Quantity:', 'wp-strava' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'quantity' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'quantity' ) ); ?>" type="text" value="<?php echo esc_html( $quantity ); ?>" />
2017-12-26 15:26:32 -06:00
</p>
<?php
}
}