$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 $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 '

' . esc_html__( 'Weather data unavailable.', 'fontdelbosc-eltemps' ) . '

'; } wp_enqueue_style( 'fdb-weather' ); return self::load_template( $template, $data ); } /** * Buffers and returns a template. * * @param string $template Template file name. * @param array $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(); } }