__( 'Nearest petrol stations with the price of one fuel type.', 'fontdelbosc-combustible' ), ) ); } /** * Renders the widget. * * @param array $args Sidebar args. * @param array $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 '

' . esc_html__( 'Data unavailable.', 'fontdelbosc-combustible' ) . '

'; } else { echo ''; } echo wp_kses_post( $after_widget ); } /** * Renders the settings form. * * @param array $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]; ?>

$new_instance New settings. * @param array $old_instance Old settings. * @return array */ 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 */ 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; } }