feat: plugin bootstrap, autoloader, activation/deactivation hooks
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: CF7 Spam → Telegram
|
||||||
|
* Description: Пересылает spam-заявки Contact Form 7 в Telegram-группу с эвристической оценкой.
|
||||||
|
* Version: 1.0.0
|
||||||
|
* Author: Vladimir Bryzgalov
|
||||||
|
* Requires PHP: 7.4
|
||||||
|
* Requires at least: 6.0
|
||||||
|
* License: GPLv2 or later
|
||||||
|
* Text Domain: cf7-spam-to-telegram
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
define('CF7STG_PLUGIN_FILE', __FILE__);
|
||||||
|
define('CF7STG_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||||
|
define('CF7STG_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||||
|
define('CF7STG_VERSION', '1.0.0');
|
||||||
|
|
||||||
|
require_once CF7STG_PLUGIN_DIR . 'includes/class-plugin.php';
|
||||||
|
\Cf7stg\Plugin::register_autoloader();
|
||||||
|
|
||||||
|
register_activation_hook(__FILE__, ['\\Cf7stg\\Activator', 'activate']);
|
||||||
|
register_deactivation_hook(__FILE__, ['\\Cf7stg\\Activator', 'deactivate']);
|
||||||
|
|
||||||
|
add_action('plugins_loaded', static function (): void {
|
||||||
|
\Cf7stg\Plugin::instance()->boot();
|
||||||
|
});
|
||||||
@@ -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