-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoloader.php
More file actions
90 lines (77 loc) · 2.22 KB
/
Copy pathAutoloader.php
File metadata and controls
90 lines (77 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Includes the composer Autoloader used for packages and classes in the src/ directory.
*
* @package WPGraphQL\Webhooks
* @since 0.1.4
*/
declare( strict_types = 1 );
namespace WPGraphQL\Webhooks;
/**
* Class - Autoloader
*
* @internal
*/
class Autoloader {
/**
* Whether the autoloader has been loaded.
*
* @var bool
*/
protected static bool $is_loaded = false;
/**
* Attempts to autoload the Composer dependencies.
*/
public static function autoload(): bool {
// If we're not *supposed* to autoload anything, then return true.
if ( defined( 'WPGRAPHQL_HEADLESS_WEBHOOKS_AUTOLOAD' ) && false === WPGRAPHQL_HEADLESS_WEBHOOKS_AUTOLOAD ) {
return true;
}
if ( self::$is_loaded ) {
return self::$is_loaded;
}
$autoloader = dirname( __DIR__ ) . '/vendor/autoload.php';
self::$is_loaded = self::require_autoloader( $autoloader );
return self::$is_loaded;
}
/**
* Attempts to load the autoloader file, if it exists.
*
* @param string $autoloader_file The path to the autoloader file.
*/
protected static function require_autoloader( string $autoloader_file ): bool {
if ( ! is_readable( $autoloader_file ) ) {
self::missing_autoloader_notice();
return false;
}
return (bool) require_once $autoloader_file; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable -- Autoloader is a Composer file.
}
/**
* Displays a notice if the autoloader is missing.
*/
protected static function missing_autoloader_notice(): void {
$error_message = 'Headless Webhooks for WPGraphQL: The Composer autoloader was not found. If you installed the plugin from the GitHub source, make sure to run `composer install`.';
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( esc_html( $error_message ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a development notice.
}
$hooks = [
'admin_notices',
'network_admin_notices',
];
foreach ( $hooks as $hook ) {
/** @psalm-suppress HookNotFound */
add_action(
$hook,
static function () use ( $error_message ) {
?>
<div class="error notice">
<p>
<?php echo esc_html( $error_message ); ?>
</p>
</div>
<?php
}
);
}
}
}