fontdelbosc-combustible/admin/class-fontdelbosc-combustible-admin.php
2026-10-12 13:25:09 +00:00

122 lines
3.9 KiB
PHP

<?php
/**
* Admin settings page: Settings → Font del Bosc Combustible.
*
* @package FontdelboscCombustible
*/
declare( strict_types=1 );
defined( 'ABSPATH' ) || exit;
/**
* Registers the settings screen and the cache-flush action.
*/
final class Fontdelbosc_Combustible_Admin {
/**
* Wires the admin hooks.
*/
public static function init(): void {
add_action( 'admin_menu', array( self::class, 'add_menu' ) );
add_action( 'admin_init', array( self::class, 'register_settings' ) );
add_action( 'admin_post_fontdelbosc_combustible_flush_cache', array( self::class, 'handle_flush' ) );
}
/**
* Adds the options page under Settings.
*/
public static function add_menu(): void {
add_options_page(
__( 'Font del Bosc Combustible', 'fontdelbosc-combustible' ),
__( 'Font del Bosc Combustible', 'fontdelbosc-combustible' ),
'manage_options',
'fontdelbosc-combustible',
array( self::class, 'render_page' )
);
}
/**
* Registers the single options row.
*/
public static function register_settings(): void {
register_setting(
'fontdelbosc_combustible',
Fontdelbosc_Combustible_Data_Manager::OPTIONS_OPTION,
array(
'type' => 'array',
'sanitize_callback' => array( self::class, 'sanitize' ),
'default' => Fontdelbosc_Combustible_Data_Manager::defaults(),
'show_in_rest' => false,
)
);
}
/**
* Sanitizes the settings form.
*
* @param mixed $input Raw input.
* @return array<string,mixed>
*/
public static function sanitize( mixed $input ): array {
$input = is_array( $input ) ? $input : array();
$fuels = array();
foreach ( Fontdelbosc_Combustible_Payload::arr( Fontdelbosc_Combustible_Payload::get( $input, 'fuel_types' ) ) as $id ) {
$id = Fontdelbosc_Combustible_Payload::int( $id );
if ( null !== $id && $id > 0 ) {
$fuels[] = $id;
}
}
return array(
'search_radius_km' => max( 5, min( 50, Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $input, 'search_radius_km' ) ) ?? 15 ) ),
'max_stations' => max( 5, min( 20, Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $input, 'max_stations' ) ) ?? 10 ) ),
'fuel_types' => array() === $fuels ? Fontdelbosc_Combustible_Fuels::DEFAULT_IDS : array_values( array_unique( $fuels ) ),
'show_only_public' => ! empty( Fontdelbosc_Combustible_Payload::get( $input, 'show_only_public' ) ),
'cache_ttl' => max( 600, min( 86400, Fontdelbosc_Combustible_Payload::int( Fontdelbosc_Combustible_Payload::get( $input, 'cache_ttl' ) ) ?? 3600 ) ),
'show_distance' => ! empty( Fontdelbosc_Combustible_Payload::get( $input, 'show_distance' ) ),
'show_schedule' => ! empty( Fontdelbosc_Combustible_Payload::get( $input, 'show_schedule' ) ),
'sort_by' => ( Fontdelbosc_Combustible_Payload::str( Fontdelbosc_Combustible_Payload::get( $input, 'sort_by' ) ) === 'price_asc' ) ? 'price_asc' : 'distance',
'delete_data_on_uninstall' => ! empty( Fontdelbosc_Combustible_Payload::get( $input, 'delete_data_on_uninstall' ) ),
);
}
/**
* Renders the settings page.
*/
public static function render_page(): void {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
require FONTDELBOSC_COMBUSTIBLE_DIR . 'admin/views/settings-page.php';
}
/**
* Handles the flush-cache button.
*/
public static function handle_flush(): void {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Insufficient permissions.', 'fontdelbosc-combustible' ) );
}
check_admin_referer( 'fontdelbosc_combustible_flush_cache' );
Fontdelbosc_Combustible_Data_Manager::flush_cache();
Fontdelbosc_Combustible_Data_Manager::get_payload( true );
wp_safe_redirect(
add_query_arg(
array(
'page' => 'fontdelbosc-combustible',
'fontdelbosc_flushed' => 1,
),
admin_url( 'options-general.php' )
)
);
exit;
}
}