v1.0.0
This commit is contained in:
commit
e1f5a47c96
22 changed files with 3155 additions and 0 deletions
156
includes/class-fontdelbosc-combustible-widget.php
Normal file
156
includes/class-fontdelbosc-combustible-widget.php
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
<?php
|
||||
/**
|
||||
* Sidebar widget: nearest stations for one fuel type.
|
||||
*
|
||||
* @package FontdelboscCombustible
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Compact list of the three nearest stations.
|
||||
*/
|
||||
final class Fontdelbosc_Combustible_Widget extends WP_Widget {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'fontdelbosc_combustible',
|
||||
__( 'Font del Bosc — Fuel Prices', 'fontdelbosc-combustible' ),
|
||||
array(
|
||||
'description' => __( 'Nearest petrol stations with the price of one fuel type.', 'fontdelbosc-combustible' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the widget.
|
||||
*
|
||||
* @param array<string,mixed> $args Sidebar args.
|
||||
* @param array<string,mixed> $instance Saved settings.
|
||||
*/
|
||||
public function widget( $args, $instance ): void {
|
||||
$fuel = max( 1, Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $instance, 'fuel' ) ) ?? Fontdelbosc_Combustible_Fuels::DEFAULT_IDS[0] );
|
||||
$data = Fontdelbosc_Combustible_Data_Manager::get_stations(
|
||||
array(
|
||||
'fuel' => (string) $fuel,
|
||||
'max' => 3,
|
||||
'sort' => 'distance',
|
||||
)
|
||||
);
|
||||
$field = $data['fields'][ $fuel ] ?? null;
|
||||
|
||||
wp_enqueue_style( 'fdbc-fuel' );
|
||||
|
||||
$before_widget = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $args, 'before_widget' ) ) ?? '';
|
||||
$after_widget = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $args, 'after_widget' ) ) ?? '';
|
||||
$before_title = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $args, 'before_title' ) ) ?? '';
|
||||
$after_title = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $args, 'after_title' ) ) ?? '';
|
||||
$title = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $instance, 'title' ) ) ?? '';
|
||||
|
||||
$title = '' !== $title ? Fontdelbosc_Combustible_Payload::str( apply_filters( 'widget_title', $title, $instance, $this->id_base ) ) ?? $title : '';
|
||||
|
||||
echo wp_kses_post( $before_widget );
|
||||
|
||||
if ( '' !== $title ) {
|
||||
echo wp_kses_post( $before_title . esc_html( $title ) . $after_title );
|
||||
}
|
||||
|
||||
if ( array() === $data['stations'] ) {
|
||||
echo '<p class="fdbc-widget__empty">' . esc_html__( 'Data unavailable.', 'fontdelbosc-combustible' ) . '</p>';
|
||||
} else {
|
||||
echo '<ul class="fdbc-widget__list">';
|
||||
foreach ( $data['stations'] as $station ) {
|
||||
$brand = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $station, 'brand' ) ) ?? '';
|
||||
$muni = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $station, 'municipality' ) ) ?? '';
|
||||
$dist = Fontdelbosc_Combustible_Payload::float( Fontdelbosc_Combustible_Payload::get( $station, 'distance' ) ) ?? 0.0;
|
||||
$price = null === $field ? null : Fontdelbosc_Combustible_Payload::float(
|
||||
Fontdelbosc_Combustible_Payload::get( Fontdelbosc_Combustible_Payload::arr( Fontdelbosc_Combustible_Payload::get( $station, 'prices' ) ), $field )
|
||||
);
|
||||
|
||||
echo '<li class="fdbc-widget__item">';
|
||||
echo '<span class="fdbc-widget__brand">' . esc_html( $brand ) . '</span> ';
|
||||
echo '<span class="fdbc-widget__meta">' . esc_html( $muni ) . '</span>';
|
||||
if ( null !== $price ) {
|
||||
echo ' <span class="fdbc-widget__price">' . esc_html( number_format_i18n( $price, 3 ) ) . '</span>';
|
||||
}
|
||||
/* translators: %s: distance in kilometers. */
|
||||
echo ' <span class="fdbc-widget__dist">' . esc_html( sprintf( __( '%s km away', 'fontdelbosc-combustible' ), number_format_i18n( $dist, 1 ) ) ) . '</span>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
}
|
||||
|
||||
echo wp_kses_post( $after_widget );
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the settings form.
|
||||
*
|
||||
* @param array<string,mixed> $instance Saved settings.
|
||||
*/
|
||||
public function form( $instance ): ?string {
|
||||
$title = Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $instance, 'title' ) ) ?? __( 'Fuel prices near Font del Bosc', 'fontdelbosc-combustible' );
|
||||
$fuel = Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $instance, 'fuel' ) ) ?? Fontdelbosc_Combustible_Fuels::DEFAULT_IDS[0];
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'fontdelbosc-combustible' ); ?></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 ); ?>" />
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr( $this->get_field_id( 'fuel' ) ); ?>"><?php esc_html_e( 'Fuel type', 'fontdelbosc-combustible' ); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'fuel' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'fuel' ) ); ?>">
|
||||
<?php foreach ( $this->fuel_choices() as $id => $label ) : ?>
|
||||
<option value="<?php echo esc_attr( (string) $id ); ?>" <?php selected( $id, $fuel ); ?>><?php echo esc_html( $label ); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the settings.
|
||||
*
|
||||
* @param array<string,mixed> $new_instance New settings.
|
||||
* @param array<string,mixed> $old_instance Old settings.
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ): array {
|
||||
$instance = $old_instance;
|
||||
|
||||
$instance['title'] = sanitize_text_field( Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $new_instance, 'title' ) ) ?? '' );
|
||||
$fuel = Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $new_instance, 'fuel' ) );
|
||||
|
||||
if ( null !== $fuel && $fuel > 0 ) {
|
||||
$instance['fuel'] = $fuel;
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns fuel choices for the form select.
|
||||
*
|
||||
* @return array<int,string>
|
||||
*/
|
||||
private function fuel_choices(): array {
|
||||
$choices = array();
|
||||
|
||||
foreach ( Fontdelbosc_Combustible_Data_Manager::get_products() as $product ) {
|
||||
$choices[ $product['id'] ] = Fontdelbosc_Combustible_Fuels::label( $product['name'] );
|
||||
}
|
||||
|
||||
if ( array() === $choices ) {
|
||||
foreach ( Fontdelbosc_Combustible_Fuels::DEFAULT_IDS as $id ) {
|
||||
$choices[ $id ] = (string) $id;
|
||||
}
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue