Abilities
2 months ago
Cli
2 months ago
Core
2 months ago
Domain
2 months ago
Handlers
2 months ago
Infrastructure
2 months ago
Servers
2 months ago
Transport
2 months ago
Autoloader.php
2 months ago
Plugin.php
2 months ago
Autoloader.php
92 lines
| 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 | /** |
| 17 | * Class - Autoloader |
| 18 | */ |
| 19 | final class Autoloader { |
| 20 | /** |
| 21 | * Whether the autoloader has been loaded. |
| 22 | * |
| 23 | * @var bool |
| 24 | */ |
| 25 | protected static bool $is_loaded = false; |
| 26 | |
| 27 | /** |
| 28 | * Attempt to autoload the Composer dependencies. |
| 29 | */ |
| 30 | public static function autoload(): bool { |
| 31 | // If we're not *supposed* to autoload anything, then return true. |
| 32 | if ( defined( 'WP_MCP_AUTOLOAD' ) && false === WP_MCP_AUTOLOAD ) { |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | if ( self::$is_loaded ) { |
| 37 | return self::$is_loaded; |
| 38 | } |
| 39 | |
| 40 | $autoloader = WP_MCP_DIR . '/vendor/autoload.php'; |
| 41 | self::$is_loaded = self::require_autoloader( $autoloader ); |
| 42 | |
| 43 | return self::$is_loaded; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Attempts to load the autoloader file, if it exists. |
| 48 | * |
| 49 | * @param string $autoloader_file The path to the autoloader file. |
| 50 | */ |
| 51 | private static function require_autoloader( string $autoloader_file ): bool { |
| 52 | if ( ! is_readable( $autoloader_file ) ) { |
| 53 | self::missing_autoloader_notice(); |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | return (bool) require_once $autoloader_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Autoloader is a Composer file. |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Displays a notice if the autoloader is missing. |
| 63 | */ |
| 64 | private static function missing_autoloader_notice(): void { |
| 65 | |
| 66 | $hooks = array( |
| 67 | 'admin_notices', |
| 68 | 'network_admin_notices', |
| 69 | ); |
| 70 | |
| 71 | foreach ( $hooks as $hook ) { |
| 72 | add_action( |
| 73 | $hook, |
| 74 | static function () { |
| 75 | $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' ); |
| 76 | |
| 77 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 78 | error_log( esc_html( $error_message ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice. |
| 79 | } |
| 80 | ?> |
| 81 | <div class="error notice"> |
| 82 | <p> |
| 83 | <?php echo esc_html( $error_message ); ?> |
| 84 | </p> |
| 85 | </div> |
| 86 | <?php |
| 87 | } |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 |