Files
wp-multi-city/includes/class-plugin.php
T
Vladimir Bryzgalov 38bb7dae55 feat(wpmc S2): make Plugin::boot() idempotent
Add private \$booted flag; guard skips all re-registration on subsequent
calls. TDD: RED (3x boot -> expectAdded->once() fails) then GREEN.
Drop deprecated setAccessible() call (no-op since PHP 8.1).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-12 18:01:43 +05:00

66 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace WPMultiCity;
final class Plugin
{
private static ?Plugin $instance = null;
private bool $booted = false;
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, 'WPMultiCity\\') !== 0) return;
$relative = substr($class, strlen('WPMultiCity\\'));
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
$slug = str_replace('_', '-', $slug);
$candidates = [
dirname(__DIR__) . '/includes/class-' . $slug . '.php',
dirname(__DIR__) . '/includes/' . $slug . '.php',
dirname(__DIR__) . '/admin/class-' . $slug . '.php',
];
foreach ($candidates as $path) {
if (is_file($path)) {
require_once $path;
return;
}
}
});
}
public function boot(): void
{
if ($this->booted) {
return;
}
$this->booted = true;
if (!$this->dependencies_satisfied()) {
add_action('admin_notices', [$this, 'render_dependency_notice']);
return;
}
Taxonomy::register();
PostType::register();
FieldGroup::register();
}
private function dependencies_satisfied(): bool
{
return class_exists('ACF') || function_exists('acf_add_local_field_group');
}
public function render_dependency_notice(): void
{
if (!current_user_can('activate_plugins')) return;
echo '<div class="notice notice-error"><p><strong>WP Multi City:</strong> требуется активный плагин Advanced Custom Fields Pro.</p></div>';
}
}