docs+tests: fix autoloader — underscores → hyphens + slug fallback

The regex '/([a-z0-9])([A-Z])/' does not split on underscores, so
Interface_Spam_Source, Settings_Page, Dashboard_Widget stayed as
'interface_spam_source', 'settings_page', 'dashboard_widget' after
strtolower — no file matched. Added str_replace('_', '-', ...).

Then Interface_Spam_Source's slug became 'interface-spam-source',
and the candidate 'includes/sources/interface-' . \$slug doubled
the prefix into 'interface-interface-spam-source.php'. Replaced
that candidate family with a slug-as-filename fallback that maps
interface-* slugs directly to their files.

Sanity check on plugin bootstrap: 17/17 classes/interfaces resolve.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vladimir Bryzgalov
2026-04-17 23:02:53 +05:00
parent 0c1be5b4cd
commit 2e0a829e06
2 changed files with 14 additions and 4 deletions
@@ -127,8 +127,10 @@ spl_autoload_register(static function (string $class): void {
return; return;
} }
$relative = substr($class, strlen('Cf7stg\\')); $relative = substr($class, strlen('Cf7stg\\'));
// Turn "PhoneParser" into "phone-parser" // Turn "PhoneParser" into "phone-parser"; also normalize underscores
// ("Settings_Page" → "settings-page").
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative)); $slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
$slug = str_replace('_', '-', $slug);
$path = __DIR__ . '/../includes/class-' . $slug . '.php'; $path = __DIR__ . '/../includes/class-' . $slug . '.php';
if (is_file($path)) { if (is_file($path)) {
require_once $path; require_once $path;
@@ -2293,12 +2295,18 @@ final class Plugin
if (strpos($class, 'Cf7stg\\') !== 0) return; if (strpos($class, 'Cf7stg\\') !== 0) return;
$relative = substr($class, strlen('Cf7stg\\')); $relative = substr($class, strlen('Cf7stg\\'));
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative)); $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 = [ $candidates = [
dirname(__DIR__) . '/includes/class-' . $slug . '.php', dirname(__DIR__) . '/includes/class-' . $slug . '.php',
dirname(__DIR__) . '/includes/sources/class-' . $slug . '.php', dirname(__DIR__) . '/includes/sources/class-' . $slug . '.php',
dirname(__DIR__) . '/includes/sources/interface-' . $slug . '.php',
dirname(__DIR__) . '/includes/interface-' . $slug . '.php',
dirname(__DIR__) . '/admin/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) { foreach ($candidates as $path) {
if (is_file($path)) { if (is_file($path)) {
+3 -1
View File
@@ -8,8 +8,10 @@ spl_autoload_register(static function (string $class): void {
return; return;
} }
$relative = substr($class, strlen('Cf7stg\\')); $relative = substr($class, strlen('Cf7stg\\'));
// Turn "PhoneParser" into "phone-parser" // Turn "PhoneParser" into "phone-parser"; also normalize underscores
// ("Settings_Page" → "settings-page").
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative)); $slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
$slug = str_replace('_', '-', $slug);
$path = __DIR__ . '/../includes/class-' . $slug . '.php'; $path = __DIR__ . '/../includes/class-' . $slug . '.php';
if (is_file($path)) { if (is_file($path)) {
require_once $path; require_once $path;