86 lines
4.2 KiB
PHP
86 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* Historical data template: monthly stats plus a pure-CSS bar chart
|
|
* (daily max/min temperatures, precipitation in tooltips).
|
|
*
|
|
* @var array<string,mixed> $data Normalized monthly history.
|
|
*
|
|
* @package FontdelboscEltemps
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
$fontdelbosc_show_attribution = ! empty( Fontdelbosc_Plugin::get_options()['display_attribution'] );
|
|
$fontdelbosc_maxes = array();
|
|
$fontdelbosc_mins = array();
|
|
|
|
foreach ( $data['days'] as $fontdelbosc_day ) {
|
|
if ( is_numeric( $fontdelbosc_day['temp_max'] ?? null ) ) {
|
|
$fontdelbosc_maxes[] = (float) $fontdelbosc_day['temp_max'];
|
|
}
|
|
|
|
if ( is_numeric( $fontdelbosc_day['temp_min'] ?? null ) ) {
|
|
$fontdelbosc_mins[] = (float) $fontdelbosc_day['temp_min'];
|
|
}
|
|
}
|
|
|
|
$fontdelbosc_scale_min = array() === $fontdelbosc_mins ? 0 : floor( min( $fontdelbosc_mins ) );
|
|
$fontdelbosc_scale_max = array() === $fontdelbosc_maxes ? 1 : ceil( max( $fontdelbosc_maxes ) );
|
|
|
|
if ( $fontdelbosc_scale_max <= $fontdelbosc_scale_min ) {
|
|
$fontdelbosc_scale_max = $fontdelbosc_scale_min + 1;
|
|
}
|
|
|
|
$fontdelbosc_range = $fontdelbosc_scale_max - $fontdelbosc_scale_min;
|
|
?>
|
|
<div class="fdb-weather fdb-historical">
|
|
<h3 class="fdb-weather__title"><?php echo esc_html__( 'Historical data', 'fontdelbosc-eltemps' ); ?> — <?php echo esc_html( Fontdelbosc_Conditions::month( (int) $data['month'] ) . ' ' . (string) $data['year'] ); ?></h3>
|
|
|
|
<ul class="fdb-historical__stats">
|
|
<?php if ( null !== ( $data['stats']['avg_max'] ?? null ) ) : ?>
|
|
<li><?php esc_html_e( 'Average max.', 'fontdelbosc-eltemps' ); ?> <?php echo esc_html( number_format_i18n( (float) $data['stats']['avg_max'], 1 ) ); ?>°C</li>
|
|
<?php endif; ?>
|
|
<?php if ( null !== ( $data['stats']['avg_min'] ?? null ) ) : ?>
|
|
<li><?php esc_html_e( 'Average min.', 'fontdelbosc-eltemps' ); ?> <?php echo esc_html( number_format_i18n( (float) $data['stats']['avg_min'], 1 ) ); ?>°C</li>
|
|
<?php endif; ?>
|
|
<li><?php esc_html_e( 'Total precipitation', 'fontdelbosc-eltemps' ); ?> <?php echo esc_html( number_format_i18n( (float) $data['stats']['total_precip'], 1 ) ); ?> mm</li>
|
|
</ul>
|
|
|
|
<div class="fdb-historical__chart" aria-hidden="true">
|
|
<?php foreach ( $data['days'] as $fontdelbosc_day ) : ?>
|
|
<?php
|
|
try {
|
|
$fontdelbosc_date = DateTimeImmutable::createFromFormat( 'Y-m-d', (string) $fontdelbosc_day['date'], new DateTimeZone( FONTDELBOSC_ELTEMPS_TIMEZONE ) );
|
|
} catch ( Exception ) {
|
|
$fontdelbosc_date = false;
|
|
}
|
|
|
|
$fontdelbosc_timestamp = $fontdelbosc_date instanceof DateTimeImmutable ? $fontdelbosc_date->getTimestamp() : time();
|
|
|
|
$fontdelbosc_height_max = is_numeric( $fontdelbosc_day['temp_max'] ?? null ) ? round( ( ( (float) $fontdelbosc_day['temp_max'] - $fontdelbosc_scale_min ) / $fontdelbosc_range ) * 100, 1 ) : 0;
|
|
$fontdelbosc_height_min = is_numeric( $fontdelbosc_day['temp_min'] ?? null ) ? round( ( ( (float) $fontdelbosc_day['temp_min'] - $fontdelbosc_scale_min ) / $fontdelbosc_range ) * 100, 1 ) : 0;
|
|
|
|
$fontdelbosc_title_parts = array( wp_date( 'j/n', $fontdelbosc_timestamp ) );
|
|
|
|
if ( is_numeric( $fontdelbosc_day['temp_max'] ?? null ) ) {
|
|
$fontdelbosc_title_parts[] = number_format_i18n( round( (float) $fontdelbosc_day['temp_max'] ) ) . '° / ' . number_format_i18n( round( (float) $fontdelbosc_day['temp_min'] ) ) . '°';
|
|
}
|
|
|
|
if ( is_numeric( $fontdelbosc_day['precipitation'] ?? null ) ) {
|
|
$fontdelbosc_title_parts[] = number_format_i18n( (float) $fontdelbosc_day['precipitation'], 1 ) . ' mm';
|
|
}
|
|
?>
|
|
<div class="fdb-historical__col" title="<?php echo esc_attr( implode( ' · ', $fontdelbosc_title_parts ) ); ?>">
|
|
<div class="fdb-historical__bars">
|
|
<div class="fdb-historical__bar fdb-historical__bar--max" style="height:<?php echo esc_attr( (string) $fontdelbosc_height_max ); ?>%"></div>
|
|
<div class="fdb-historical__bar fdb-historical__bar--min" style="height:<?php echo esc_attr( (string) $fontdelbosc_height_min ); ?>%"></div>
|
|
</div>
|
|
<span class="fdb-historical__day"><?php echo esc_html( wp_date( 'j', $fontdelbosc_timestamp ) ); ?></span>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<?php if ( $fontdelbosc_show_attribution ) : ?>
|
|
<p class="fdb-attribution"><?php echo esc_html( Fontdelbosc_Data_Manager::attribution( (string) $data['source'] ) ); ?></p>
|
|
<?php endif; ?>
|
|
</div>
|