This commit is contained in:
Javier Casares 2026-10-12 11:55:23 +00:00
commit 06153e387d
47 changed files with 5116 additions and 0 deletions

View file

@ -0,0 +1,145 @@
<?php
/**
* Shortcode [fontdelbosc_eltemps] and the shared view renderer used by
* both the shortcode and the widget.
*
* Attributes:
* - view: current (default), hourly, daily, historical
* - hours: 1-48, hours to show in the hourly view (default 24)
* - days: 1-7, days to show in the daily view (default 7)
* - year/month: month shown in the historical view (default: current)
*
* @package FontdelboscEltemps
*/
defined( 'ABSPATH' ) || exit;
/**
* Renders the weather views for the shortcode and the widget.
*/
final class Fontdelbosc_Shortcode {
/**
* Registers the shortcode hook.
*
* @return void
*/
public static function init(): void {
add_shortcode( 'fontdelbosc_eltemps', array( self::class, 'render' ) );
}
/**
* Renders the shortcode.
*
* @param array<string> $atts Shortcode attributes.
* @param string|null $content Enclosed content (unused).
* @param string $tag Shortcode tag (unused).
* @return string
*/
public static function render( $atts = array(), ?string $content = null, string $tag = '' ): string {
$atts = shortcode_atts(
array(
'view' => 'current',
'hours' => 24,
'days' => 7,
'year' => 0,
'month' => 0,
),
$atts,
'fontdelbosc_eltemps'
);
$view = sanitize_key( Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $atts, 'view' ) ) ?? 'current' );
if ( ! in_array( $view, array( 'current', 'hourly', 'daily', 'historical' ), true ) ) {
$view = 'current';
}
return self::render_view(
$view,
array(
'hours' => max( 1, min( 48, Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $atts, 'hours' ) ) ?? 24 ) ),
'days' => max( 1, min( 7, Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $atts, 'days' ) ) ?? 7 ) ),
'year' => Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $atts, 'year' ) ) ?? 0,
'month' => max( 0, min( 12, Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $atts, 'month' ) ) ?? 0 ) ),
)
);
}
/**
* Renders a weather view. Shared by shortcode and widget.
*
* @param string $view View name.
* @param array<string,mixed> $args Optional view arguments.
* @return string
*/
public static function render_view( string $view, array $args = array() ): string {
$args = wp_parse_args(
$args,
array(
'hours' => 24,
'days' => 7,
'year' => 0,
'month' => 0,
)
);
$template = 'current.php';
$data = null;
$now = new DateTimeImmutable( 'now', new DateTimeZone( FONTDELBOSC_ELTEMPS_TIMEZONE ) );
switch ( $view ) {
case 'hourly':
$template = 'forecast-hourly.php';
$data = Fontdelbosc_Data_Manager::get_hourly( Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $args, 'hours' ) ) ?? 24 );
break;
case 'daily':
$template = 'forecast-daily.php';
$data = Fontdelbosc_Data_Manager::get_daily( Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $args, 'days' ) ) ?? 7 );
break;
case 'historical':
$template = 'historical.php';
$year = Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $args, 'year' ) ) ?? 0;
$month = Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $args, 'month' ) ) ?? 0;
if ( $year > 0 || $month > 0 ) {
$data = Fontdelbosc_Data_Manager::get_historical(
$year > 0 ? $year : (int) $now->format( 'Y' ),
$month > 0 ? $month : (int) $now->format( 'n' )
);
} else {
$previous = $now->modify( 'first day of previous month' );
$data = Fontdelbosc_Data_Manager::get_historical(
(int) $previous->format( 'Y' ),
(int) $previous->format( 'n' )
);
}
break;
default:
$data = Fontdelbosc_Data_Manager::get_current();
}
if ( null === $data ) {
return '<p class="fdb-weather fdb-unavailable">' . esc_html__( 'Weather data unavailable.', 'fontdelbosc-eltemps' ) . '</p>';
}
wp_enqueue_style( 'fdb-weather' );
return self::load_template( $template, $data );
}
/**
* Buffers and returns a template.
*
* @param string $template Template file name.
* @param array<string,mixed> $data Normalized view data.
* @return string
*/
private static function load_template( string $template, array $data ): string {
ob_start();
include FONTDELBOSC_ELTEMPS_PLUGIN_DIR . 'templates/' . $template;
return (string) ob_get_clean();
}
}