feat: plugin bootstrap, autoloader, activation/deactivation hooks
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Cf7stg;
|
||||
|
||||
final class Plugin
|
||||
{
|
||||
private static ?Plugin $instance = null;
|
||||
|
||||
public static function instance(): Plugin
|
||||
{
|
||||
if (self::$instance === null) self::$instance = new self();
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public static function register_autoloader(): void
|
||||
{
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
if (strpos($class, 'Cf7stg\\') !== 0) return;
|
||||
$relative = substr($class, strlen('Cf7stg\\'));
|
||||
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
|
||||
// Class names may use underscores (Settings_Page, Interface_Spam_Source);
|
||||
// normalize both word-boundary conventions to hyphens.
|
||||
$slug = str_replace('_', '-', $slug);
|
||||
$candidates = [
|
||||
dirname(__DIR__) . '/includes/class-' . $slug . '.php',
|
||||
dirname(__DIR__) . '/includes/sources/class-' . $slug . '.php',
|
||||
dirname(__DIR__) . '/admin/class-' . $slug . '.php',
|
||||
// Fallback: treat slug as full filename (for Interface_* classes
|
||||
// whose slug already becomes 'interface-xxx').
|
||||
dirname(__DIR__) . '/includes/' . $slug . '.php',
|
||||
dirname(__DIR__) . '/includes/sources/' . $slug . '.php',
|
||||
dirname(__DIR__) . '/admin/' . $slug . '.php',
|
||||
];
|
||||
foreach ($candidates as $path) {
|
||||
if (is_file($path)) {
|
||||
require_once $path;
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
// Register runtime hooks
|
||||
Dispatcher::register_hooks();
|
||||
(new CF7Source())->register();
|
||||
|
||||
// Admin
|
||||
if (is_admin()) {
|
||||
(new Settings_Page())->register();
|
||||
(new Dashboard_Widget())->register();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user