PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / wordpress / mcp-adapter / includes / Autoloader.php

Autoloader.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.1, at vendor/wordpress/mcp-adapter/includes/Autoloader.php

92 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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