73 lines
1.5 KiB
PHP
73 lines
1.5 KiB
PHP
<?php
|
|
/**
|
|
* Shortcode [fontdelbosc_combustible] and shared view renderer.
|
|
*
|
|
* @package FontdelboscCombustible
|
|
*/
|
|
|
|
declare( strict_types=1 );
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Renders the station list via the templates.
|
|
*/
|
|
final class Fontdelbosc_Combustible_Shortcode {
|
|
|
|
/**
|
|
* Registers the shortcode.
|
|
*/
|
|
public static function init(): void {
|
|
add_shortcode( 'fontdelbosc_combustible', array( self::class, 'render' ) );
|
|
}
|
|
|
|
/**
|
|
* Renders the shortcode.
|
|
*
|
|
* @param array<string> $atts Shortcode attributes.
|
|
*/
|
|
public static function render( $atts = array() ): string {
|
|
$atts = shortcode_atts(
|
|
array(
|
|
'radius' => '',
|
|
'max' => '',
|
|
'fuel' => '',
|
|
'sort' => '',
|
|
),
|
|
$atts,
|
|
'fontdelbosc_combustible'
|
|
);
|
|
|
|
wp_enqueue_style( 'fdbc-fuel' );
|
|
|
|
$data = Fontdelbosc_Combustible_Data_Manager::get_stations(
|
|
array(
|
|
'radius' => $atts['radius'],
|
|
'max' => $atts['max'],
|
|
'fuel' => $atts['fuel'],
|
|
'sort' => $atts['sort'],
|
|
)
|
|
);
|
|
|
|
return self::view( 'station-list', $data );
|
|
}
|
|
|
|
/**
|
|
* Renders a template by name with the given data.
|
|
*
|
|
* @param string $name Template name.
|
|
* @param array<string,mixed> $fontdelbosc_data Template data.
|
|
*/
|
|
public static function view( string $name, array $fontdelbosc_data ): string {
|
|
$file = FONTDELBOSC_COMBUSTIBLE_DIR . 'templates/' . $name . '.php';
|
|
|
|
if ( ! is_file( $file ) ) {
|
|
return '';
|
|
}
|
|
|
|
ob_start();
|
|
include $file;
|
|
|
|
return (string) ob_get_clean();
|
|
}
|
|
}
|