99 lines
3.8 KiB
PHP
99 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* Sidebar widget wrapping the shared view renderer.
|
|
*
|
|
* @package FontdelboscEltemps
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Sidebar widget for current conditions or today's forecast.
|
|
*/
|
|
final class Fontdelbosc_Widget extends WP_Widget {
|
|
|
|
/**
|
|
* Registers the widget with WordPress.
|
|
*/
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'fontdelbosc_weather',
|
|
'Font del Bosc — El Temps',
|
|
array(
|
|
'description' => __( 'Current weather and forecast for Font del Bosc (Mediona).', 'fontdelbosc-eltemps' ),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Renders the widget output.
|
|
*
|
|
* @param array<string,mixed> $args Widget arguments.
|
|
* @param array<string,mixed> $instance Saved widget values.
|
|
* @return void
|
|
*/
|
|
public function widget( $args, $instance ): void {
|
|
$title = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $instance, 'title' ) ) ?? '';
|
|
$title = '' !== $title ? Fontdelbosc_Payload::str( apply_filters( 'widget_title', $title, $instance, $this->id_base ) ) ?? $title : '';
|
|
$view = 'today' === Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $instance, 'view' ) ) ? 'today' : 'current';
|
|
|
|
$before_widget = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $args, 'before_widget' ) ) ?? '';
|
|
$after_widget = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $args, 'after_widget' ) ) ?? '';
|
|
$before_title = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $args, 'before_title' ) ) ?? '';
|
|
$after_title = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $args, 'after_title' ) ) ?? '';
|
|
|
|
echo wp_kses_post( $before_widget );
|
|
|
|
if ( $title ) {
|
|
echo wp_kses_post( $before_title . esc_html( $title ) . $after_title );
|
|
}
|
|
|
|
if ( 'today' === $view ) {
|
|
echo wp_kses_post( Fontdelbosc_Shortcode::render_view( 'daily', array( 'days' => 1 ) ) );
|
|
} else {
|
|
echo wp_kses_post( Fontdelbosc_Shortcode::render_view( 'current' ) );
|
|
}
|
|
|
|
echo wp_kses_post( $after_widget );
|
|
}
|
|
|
|
/**
|
|
* Renders the widget settings form.
|
|
*
|
|
* @param array<string,mixed> $instance Saved widget values.
|
|
* @return string|null
|
|
*/
|
|
public function form( $instance ): ?string {
|
|
$title = Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $instance, 'title' ) ) ?? '';
|
|
$view = 'today' === Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $instance, 'view' ) ) ? 'today' : 'current';
|
|
?>
|
|
<p>
|
|
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'fontdelbosc-eltemps' ); ?></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( 'view' ) ); ?>"><?php esc_html_e( 'View', 'fontdelbosc-eltemps' ); ?></label>
|
|
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'view' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'view' ) ); ?>">
|
|
<option value="current" <?php selected( $view, 'current' ); ?>><?php esc_html_e( 'Current conditions', 'fontdelbosc-eltemps' ); ?></option>
|
|
<option value="today" <?php selected( $view, 'today' ); ?>><?php esc_html_e( "Today's forecast", 'fontdelbosc-eltemps' ); ?></option>
|
|
</select>
|
|
</p>
|
|
<?php
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Sanitizes the widget settings on save.
|
|
*
|
|
* @param array<string,mixed> $new_instance New values.
|
|
* @param array<string,mixed> $old_instance Old values.
|
|
* @return array<string,mixed>
|
|
*/
|
|
public function update( $new_instance, $old_instance ): array {
|
|
return array(
|
|
'title' => sanitize_text_field( Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $new_instance, 'title' ) ) ?? '' ),
|
|
'view' => 'today' === Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $new_instance, 'view' ) ) ? 'today' : 'current',
|
|
);
|
|
}
|
|
}
|