Automattic\WooCommerce
Autoloader::build_woocommerce_psr4_fallback
Build a WooCommerce-scoped Composer PSR-4 ClassLoader to use as a fallback to the Jetpack autoloader.
The Jetpack autoloader reads its classmap into an in-memory snapshot once per request and never refreshes it. During a WordPress in-place upgrade the plugin files are swapped mid-request, so a class that is new in the upgraded version cannot be found in the snapshot and the request fatals. This loader, registered as an appended (lowest-priority) fallback, resolves such classes from disk via PSR-4.
Scoped to the first-party Automattic\WooCommerce\ (src/) namespace only — the family that actually fatals during an in-place upgrade (e.g. Enums\DefaultCustomerAddress). Every other prefix in the Composer map is deliberately excluded: bundled third-party packages (Automattic\WooCommerce\Vendor\ → lib/packages) so the fallback can never load WooCommerce's bundled copy over the version the Jetpack autoloader coordinates across plugins, and the non-runtime prefixes (Blueprint, tests, build tooling) which never fatal during a front-end upgrade request.
Returns the configured (but NOT registered) loader so the caller controls registration and tests can exercise it without touching the global SPL stack.
Метод класса: Autoloader{}
Хуков нет.
Возвращает
ClassLoader|null. The loader, or null if the Composer files are unavailable or a foreign ClassLoader shape is present.
Использование
$result = Autoloader::build_woocommerce_psr4_fallback(): ?ClassLoader;
Список изменений
| С версии 11.0.0 | Введена. |
Код Autoloader::build_woocommerce_psr4_fallback() Autoloader::build woocommerce psr4 fallback WC 11.0.1
public static function build_woocommerce_psr4_fallback(): ?ClassLoader {
$base = dirname( __DIR__ );
$psr4_map = $base . '/vendor/composer/autoload_psr4.php';
if ( ! is_readable( $psr4_map ) ) {
return null;
}
try {
// Reuse an already-loaded ClassLoader (another plugin or wp-cli may have
// loaded it from a different path); requiring our copy then would fatal
// with "Cannot declare class ... already in use". Kept inside the try so a
// torn/partially-written ClassLoader.php during a vendor-bundle upgrade
// degrades to a null fallback instead of fataling the bootstrap.
if ( ! class_exists( ClassLoader::class, false ) ) {
$classloader_file = $base . '/vendor/composer/ClassLoader.php';
if ( ! is_readable( $classloader_file ) ) {
return null;
}
require_once $classloader_file;
}
$psr4_entries = require $psr4_map;
if ( ! is_array( $psr4_entries ) ) {
return null;
}
$loader = new ClassLoader();
foreach ( $psr4_entries as $namespace => $paths ) {
// First-party src/ only — exclude bundled Vendor\ and non-runtime prefixes.
if ( 'Automattic\\WooCommerce\\' === $namespace ) {
$loader->setPsr4( $namespace, $paths );
}
}
return $loader;
} catch ( \Throwable $e ) {
// Foreign/ancient ClassLoader shape, or a torn Composer file — skip the
// fallback rather than fatal the bootstrap.
return null;
}
}