| 1 |
<?php |
| 2 |
/** |
| 3 |
* PSR-4 Autoloader for PHP classes inside plugin. |
| 4 |
* |
| 5 |
* Ensures that autoloaders are present, and logs an Admin notice if not. |
| 6 |
* |
| 7 |
* Can be bypassed by defining the WP_MCP_AUTOLOAD constant to false. |
| 8 |
* |
| 9 |
* @package WP\MCP |
| 10 |
*/ |
| 11 |
|
| 12 |
declare( strict_types=1 ); |
| 13 |
|
| 14 |
namespace WP\MCP; |
| 15 |
|
| 16 |
// Exit if accessed directly. |
| 17 |
defined( 'ABSPATH' ) || exit; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class - Autoloader |
| 21 |
*/ |
| 22 |
final class Autoloader { |
| 23 |
/** |
| 24 |
* Whether the autoloader has been loaded. |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
protected static bool $is_loaded = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Attempt to autoload the Composer dependencies. |
| 32 |
*/ |
| 33 |
public static function autoload(): bool { |
| 34 |
// If we're not *supposed* to autoload anything, then return true. |
| 35 |
if ( defined( 'WP_MCP_AUTOLOAD' ) && false === WP_MCP_AUTOLOAD ) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
if ( self::$is_loaded ) { |
| 40 |
return self::$is_loaded; |
| 41 |
} |
| 42 |
|
| 43 |
// Jetpack Autoloader uses `autoload_packages.php` instead of `autoload.php`. |
| 44 |
$autoloader = WP_MCP_DIR . '/vendor/autoload_packages.php'; |
| 45 |
self::$is_loaded = self::require_autoloader( $autoloader ); |
| 46 |
|
| 47 |
return self::$is_loaded; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Attempts to load the autoloader file, if it exists. |
| 52 |
* |
| 53 |
* @param string $autoloader_file The path to the autoloader file. |
| 54 |
*/ |
| 55 |
private static function require_autoloader( string $autoloader_file ): bool { |
| 56 |
if ( ! is_readable( $autoloader_file ) ) { |
| 57 |
self::missing_autoloader_notice(); |
| 58 |
|
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
return (bool) require_once $autoloader_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Autoloader is a Composer file. |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Displays a notice if the autoloader is missing. |
| 67 |
*/ |
| 68 |
private static function missing_autoloader_notice(): void { |
| 69 |
|
| 70 |
$hooks = array( |
| 71 |
'admin_notices', |
| 72 |
'network_admin_notices', |
| 73 |
); |
| 74 |
|
| 75 |
foreach ( $hooks as $hook ) { |
| 76 |
add_action( |
| 77 |
$hook, |
| 78 |
static function () { |
| 79 |
$error_message = __( 'MCP Adapter: The Composer autoloader was not found. If you installed the plugin from the GitHub source code, make sure to run `composer install`.', 'mcp-adapter' ); |
| 80 |
|
| 81 |
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 82 |
error_log( esc_html( $error_message ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice. |
| 83 |
} |
| 84 |
?> |
| 85 |
<div class="error notice"> |
| 86 |
<p> |
| 87 |
<?php echo esc_html( $error_message ); ?> |
| 88 |
</p> |
| 89 |
</div> |
| 90 |
<?php |
| 91 |
} |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|