32
32
require_once __DIR__ . '/c3.php ' ;
33
33
}
34
34
35
- // Whether to autoload the files or not.
36
- // This must be defined here and not within the WPGraphQL.php because this constant
37
- // determines whether to autoload classes or not
38
- if ( ! defined ( 'WPGRAPHQL_AUTOLOAD ' ) ) {
39
- define ( 'WPGRAPHQL_AUTOLOAD ' , true );
35
+ // Bootstrap files that are neded before autoloader is bootstrapped
36
+ if ( file_exists ( __DIR__ . '/access-functions.php ' ) ) {
37
+ require_once __DIR__ . '/access-functions.php ' ;
38
+ }
39
+ if ( file_exists ( __DIR__ . '/constants.php ' ) ) {
40
+ require_once __DIR__ . '/constants.php ' ;
41
+ }
42
+ if ( file_exists ( __DIR__ . '/activation.php ' ) ) {
43
+ require_once __DIR__ . '/activation.php ' ;
44
+ }
45
+ if ( file_exists ( __DIR__ . '/deactivation.php ' ) ) {
46
+ require_once __DIR__ . '/deactivation.php ' ;
40
47
}
41
48
42
49
// Run this function when WPGraphQL is de-activated
43
50
register_deactivation_hook ( __FILE__ , 'graphql_deactivation_callback ' );
44
51
register_activation_hook ( __FILE__ , 'graphql_activation_callback ' );
45
52
46
- // Bootstrap the plugin
47
- if ( ! class_exists ( 'WPGraphQL ' ) ) {
48
- require_once __DIR__ . '/src/WPGraphQL.php ' ;
49
- }
50
-
51
53
/**
54
+ * test env:
55
+ * - WPGRAPHQL_AUTOLOAD: false
56
+ * - autoload installed and manually added in test env
57
+ *
58
+ * Bedrock
59
+ * - WPGRAPHQL_AUTOLOAD: not defined
60
+ * - composer deps installed outside of the plugin
61
+ *
62
+ * Normal (.org repo install)
63
+ * - WPGRAPHQL_AUTOLOAD: not defined
64
+ * - composer deps installed INSIDE the plugin
65
+ *
66
+ *
52
67
* @return bool
53
68
*/
54
69
function graphql_can_load_plugin (): bool {
55
70
71
+
72
+ $ can_load = false ;
73
+ $ autoload_file = plugin_dir_path ( __FILE__ ) . 'vendor/autoload.php ' ;
74
+
75
+ if ( class_exists ( 'WPGraphQL ' ) ) {
76
+ return true ;
77
+ }
78
+
56
79
/**
57
80
* WPGRAPHQL_AUTOLOAD can be set to "false" to prevent the autoloader from running.
58
81
* In most cases, this is not something that should be disabled, but some environments
@@ -68,38 +91,27 @@ function graphql_can_load_plugin(): bool {
68
91
// IF WPGRAPHQL_AUTOLOAD is defined as false,
69
92
// but the WPGraphQL Class exists, we can assume the dependencies
70
93
// are loaded from the parent project.
71
- if ( class_exists ( '\WPGraphQL ' ) ) {
72
- return true ;
73
- }
94
+ return true ;
95
+
74
96
}
75
97
76
- // If the autoload file exists, load it
77
- if ( file_exists ( plugin_dir_path ( __FILE__ ) . 'vendor/autoload.php ' ) ) {
98
+ if ( ( ! defined ( 'WPGRAPHQL_AUTOLOAD ' ) || true === WPGRAPHQL_AUTOLOAD ) && file_exists ( $ autoload_file ) ) {
78
99
// Autoload Required Classes.
79
- require_once plugin_dir_path ( __FILE__ ) . 'vendor/autoload.php ' ;
100
+ require_once ( $ autoload_file );
101
+ // Bootstrap the plugin
102
+ if ( ! class_exists ( 'WPGraphQL ' ) ) {
103
+ require_once __DIR__ . '/src/WPGraphQL.php ' ;
104
+ }
80
105
return true ;
81
- // If the autoload file doesn't exist
82
- // manually load the individual files defined
83
- // in the composer.json
84
106
}
85
107
86
- add_action (
87
- 'admin_notices ' ,
88
- static function () {
89
- if ( ! current_user_can ( 'manage_options ' ) ) {
90
- return ;
91
- }
92
-
93
- printf (
94
- '<div class="notice notice-error"> ' .
95
- '<p>%s</p> ' .
96
- '</div> ' ,
97
- esc_html__ ( 'WPGraphQL appears to have been installed without it \'s dependencies. It will not work properly until dependencies are installed. This likely means you have cloned WPGraphQL from Github and need to run the command `composer install`. ' , 'wp-graphql ' )
98
- );
99
- }
100
- );
108
+ // Bootstrap the plugin
109
+ if ( ! class_exists ( '\WPGraphQL ' ) ) {
110
+ return false ;
111
+ }
112
+
113
+ return true ;
101
114
102
- return false ;
103
115
}
104
116
105
117
if ( ! function_exists ( 'graphql_init ' ) ) {
@@ -112,6 +124,8 @@ function graphql_init() {
112
124
113
125
// if the plugin can't be loaded, bail
114
126
if ( false === graphql_can_load_plugin () ) {
127
+ add_action ( 'network_admin_notices ' , 'graphql_cannot_load_admin_notice_callback ' );
128
+ add_action ( 'admin_notices ' , 'graphql_cannot_load_admin_notice_callback ' );
115
129
return null ;
116
130
}
117
131
@@ -123,6 +137,19 @@ function graphql_init() {
123
137
}
124
138
graphql_init ();
125
139
140
+ function graphql_cannot_load_admin_notice_callback () {
141
+ if ( ! current_user_can ( 'manage_options ' ) ) {
142
+ return ;
143
+ }
144
+
145
+ printf (
146
+ '<div class="notice notice-error"> ' .
147
+ '<p>%s</p> ' .
148
+ '</div> ' ,
149
+ esc_html__ ( 'WPGraphQL appears to have been installed without it \'s dependencies. It will not work properly until dependencies are installed. This likely means you have cloned WPGraphQL from Github and need to run the command `composer install`. ' , 'wp-graphql ' )
150
+ );
151
+ }
152
+
126
153
if ( defined ( 'WP_CLI ' ) && WP_CLI ) {
127
154
require_once plugin_dir_path ( __FILE__ ) . 'cli/wp-cli.php ' ;
128
155
}
0 commit comments