From 85d55e80fcfb5899c01d6705d8090fa0f9202fcd Mon Sep 17 00:00:00 2001 From: Marcus Quinn <6428977+marcusquinn@users.noreply.github.com> Date: Fri, 24 Apr 2026 08:34:47 +0100 Subject: [PATCH] feat: implement PSR-4 autoloader, remove manual require_once calls (#27) - Add spl_autoload_register in Plugin::register_autoloader() mapping WPALLSTARS\FixPluginDoesNotExistNotices\Admin\ to admin/lib/ and WPALLSTARS\FixPluginDoesNotExistNotices\ to includes/ - Remove manual require_once calls for Core, Admin, and Modal classes - Rename admin/lib/admin.php -> Admin.php and modal.php -> Modal.php for PSR-4 filename convention (class name must match file name) - Fix composer.json: correct namespace case (WPAllStars -> WPALLSTARS) and add admin/lib/ directory mapping for Admin sub-namespace - Fix wp-fix-plugin-does-not-exist-notices.php require_once to use correct case (plugin.php -> Plugin.php) Resolves #25 --- admin/lib/{admin.php => Admin.php} | 0 admin/lib/{modal.php => Modal.php} | 0 composer.json | 3 +- includes/Plugin.php | 46 +++++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 8 deletions(-) rename admin/lib/{admin.php => Admin.php} (100%) rename admin/lib/{modal.php => Modal.php} (100%) diff --git a/admin/lib/admin.php b/admin/lib/Admin.php similarity index 100% rename from admin/lib/admin.php rename to admin/lib/Admin.php diff --git a/admin/lib/modal.php b/admin/lib/Modal.php similarity index 100% rename from admin/lib/modal.php rename to admin/lib/Modal.php diff --git a/composer.json b/composer.json index 3ff3577..0b85957 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ }, "autoload": { "psr-4": { - "WPAllStars\\FixPluginDoesNotExistNotices\\": "includes/" + "WPALLSTARS\\FixPluginDoesNotExistNotices\\Admin\\": "admin/lib/", + "WPALLSTARS\\FixPluginDoesNotExistNotices\\": "includes/" } } } diff --git a/includes/Plugin.php b/includes/Plugin.php index 5096cd7..957680e 100644 --- a/includes/Plugin.php +++ b/includes/Plugin.php @@ -76,6 +76,7 @@ class Plugin { $this->plugin_url = plugin_dir_url($plugin_file); $this->define_constants(); + $this->register_autoloader(); $this->load_dependencies(); $this->init_components(); } @@ -97,23 +98,54 @@ class Plugin { } } + /** + * Register the PSR-4 autoloader + * + * Maps plugin namespaces to their corresponding directories so that + * new class files are loaded automatically without manual require_once calls. + * + * @return void + */ + private function register_autoloader() { + $plugin_dir = $this->plugin_dir; + + spl_autoload_register(function ($class) use ($plugin_dir) { + // Ordered most-specific prefix first so Admin\ resolves before the root namespace. + $namespace_map = array( + 'WPALLSTARS\\FixPluginDoesNotExistNotices\\Admin\\' => $plugin_dir . 'admin/lib/', + 'WPALLSTARS\\FixPluginDoesNotExistNotices\\' => $plugin_dir . 'includes/', + ); + + foreach ($namespace_map as $prefix => $base_dir) { + if (strncmp($prefix, $class, strlen($prefix)) !== 0) { + continue; + } + + $relative_class = substr($class, strlen($prefix)); + $file = $base_dir . str_replace('\\', DIRECTORY_SEPARATOR, $relative_class) . '.php'; + + if (file_exists($file)) { + require_once $file; + return; + } + } + }); + } + /** * Load dependencies * + * Loads the Composer autoloader when available (vendor installs). Project + * classes are resolved by the PSR-4 autoloader registered in register_autoloader(). + * * @return void */ private function load_dependencies() { - // Load composer autoloader if it exists + // Load composer autoloader if it exists (vendor/ directory present). $autoloader = $this->plugin_dir . 'vendor/autoload.php'; if (file_exists($autoloader)) { require_once $autoloader; } - - // Load required files - require_once $this->plugin_dir . 'includes/Core.php'; - require_once $this->plugin_dir . 'includes/Updater.php'; - require_once $this->plugin_dir . 'admin/lib/admin.php'; - require_once $this->plugin_dir . 'admin/lib/modal.php'; } /**