v1.0.0
This commit is contained in:
commit
06153e387d
47 changed files with 5116 additions and 0 deletions
168
includes/class-fontdelbosc-plugin.php
Normal file
168
includes/class-fontdelbosc-plugin.php
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
<?php
|
||||
/**
|
||||
* Bootstrap class. Single entry point that wires every component.
|
||||
*
|
||||
* @package FontdelboscEltemps
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Wires every plugin component and holds the typed options accessor.
|
||||
*/
|
||||
final class Fontdelbosc_Plugin {
|
||||
|
||||
/**
|
||||
* Options shape.
|
||||
*
|
||||
* @var array{
|
||||
* aemet_api_key: string,
|
||||
* meteocat_api_key: string,
|
||||
* meteocat_station_code: string,
|
||||
* cache_ttl_current: int,
|
||||
* cache_ttl_forecast: int,
|
||||
* cache_ttl_historical: int,
|
||||
* display_attribution: bool,
|
||||
* delete_data_on_uninstall: bool
|
||||
* }|null
|
||||
*/
|
||||
private static ?array $options = null;
|
||||
|
||||
/**
|
||||
* Singleton instance.
|
||||
*
|
||||
* @var Fontdelbosc_Plugin|null
|
||||
*/
|
||||
private static ?Fontdelbosc_Plugin $instance = null;
|
||||
|
||||
/**
|
||||
* Returns the singleton instance.
|
||||
*
|
||||
* @return Fontdelbosc_Plugin
|
||||
*/
|
||||
public static function instance(): Fontdelbosc_Plugin {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers every hook. Private constructor: use instance().
|
||||
*/
|
||||
private function __construct() {
|
||||
add_action( 'init', array( $this, 'load_textdomain' ) );
|
||||
add_action( 'init', array( $this, 'init_components' ) );
|
||||
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );
|
||||
add_action( 'fontdelbosc_refresh_weather', array( 'Fontdelbosc_Data_Manager', 'refresh' ) );
|
||||
|
||||
if ( is_admin() ) {
|
||||
require_once FONTDELBOSC_ELTEMPS_PLUGIN_DIR . 'admin/class-fontdelbosc-admin.php';
|
||||
Fontdelbosc_Admin::init();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the plugin text domain. Hooked on init to avoid the
|
||||
* just-in-time loading notice on WordPress 6.7+.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_textdomain(): void {
|
||||
load_plugin_textdomain(
|
||||
'fontdelbosc-eltemps',
|
||||
false,
|
||||
dirname( plugin_basename( FONTDELBOSC_ELTEMPS_PLUGIN_FILE ) ) . '/languages/'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the frontend components.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init_components(): void {
|
||||
Fontdelbosc_Shortcode::init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the sidebar widget.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_widgets(): void {
|
||||
register_widget( 'Fontdelbosc_Widget' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers (not enqueues) the public stylesheet, so it is only
|
||||
* printed on pages that actually render weather output.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_assets(): void {
|
||||
wp_register_style(
|
||||
'fdb-weather',
|
||||
FONTDELBOSC_ELTEMPS_PLUGIN_URL . 'public/css/fdb-weather.css',
|
||||
array(),
|
||||
FONTDELBOSC_ELTEMPS_VERSION
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules the hourly cache-refresh event on activation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function activate(): void {
|
||||
if ( ! wp_next_scheduled( 'fontdelbosc_refresh_weather' ) ) {
|
||||
wp_schedule_event( time(), 'hourly', 'fontdelbosc_refresh_weather' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the scheduled cache-refresh event on deactivation.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function deactivate(): void {
|
||||
wp_clear_scheduled_hook( 'fontdelbosc_refresh_weather' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin options merged with their defaults, with every
|
||||
* value narrowed to its documented type.
|
||||
*
|
||||
* @return array{
|
||||
* aemet_api_key: string,
|
||||
* meteocat_api_key: string,
|
||||
* meteocat_station_code: string,
|
||||
* cache_ttl_current: int,
|
||||
* cache_ttl_forecast: int,
|
||||
* cache_ttl_historical: int,
|
||||
* display_attribution: bool,
|
||||
* delete_data_on_uninstall: bool
|
||||
* }
|
||||
*/
|
||||
public static function get_options(): array {
|
||||
if ( null === self::$options ) {
|
||||
$stored = get_option( 'fontdelbosc_options', array() );
|
||||
$stored = is_array( $stored ) ? $stored : array();
|
||||
|
||||
self::$options = array(
|
||||
'aemet_api_key' => Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $stored, 'aemet_api_key' ) ) ?? '',
|
||||
'meteocat_api_key' => Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $stored, 'meteocat_api_key' ) ) ?? '',
|
||||
'meteocat_station_code' => Fontdelbosc_Payload::str( Fontdelbosc_Payload::get( $stored, 'meteocat_station_code' ) ) ?? '',
|
||||
'cache_ttl_current' => Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $stored, 'cache_ttl_current' ) ) ?? 600,
|
||||
'cache_ttl_forecast' => Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $stored, 'cache_ttl_forecast' ) ) ?? 3600,
|
||||
'cache_ttl_historical' => Fontdelbosc_Payload::int( Fontdelbosc_Payload::get( $stored, 'cache_ttl_historical' ) ) ?? 86400,
|
||||
'display_attribution' => array_key_exists( 'display_attribution', $stored ) ? ! empty( $stored['display_attribution'] ) : true,
|
||||
'delete_data_on_uninstall' => ! empty( $stored['delete_data_on_uninstall'] ),
|
||||
);
|
||||
}
|
||||
|
||||
return self::$options;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue