Skip to content

Commit f8e488a

Browse files
committed
- refactor graphql_can_load_plugin
- remove files from autoloader that are required before autoloading is loaded - define WPGRAPHQL_AUTOLOAD in constants.php - update graphql_activation and graphql_deactivation
1 parent d639d80 commit f8e488a

File tree

5 files changed

+83
-41
lines changed

5 files changed

+83
-41
lines changed

activation.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
* @return void
66
*/
77
function graphql_activation_callback() {
8+
89
do_action( 'graphql_activate' );
910

11+
if ( ! defined( 'WPGRAPHQL_VERSION' ) ) {
12+
return;
13+
}
14+
1015
// store the current version of WPGraphQL
1116
update_option( 'wp_graphql_version', WPGRAPHQL_VERSION );
1217
}

composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@
6060
}
6161
},
6262
"autoload": {
63-
"files": [
64-
"access-functions.php",
65-
"constants.php",
66-
"activation.php",
67-
"deactivation.php"
68-
],
63+
"files": [],
6964
"psr-4": {
7065
"WPGraphQL\\": "src/"
7166
}

constants.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
*/
1010
function graphql_setup_constants() {
1111

12+
// Whether to autoload the files or not.
13+
// This must be defined here and not within the WPGraphQL.php because this constant
14+
// determines whether to autoload classes or not
15+
if ( ! defined( 'WPGRAPHQL_AUTOLOAD' ) ) {
16+
define( 'WPGRAPHQL_AUTOLOAD', true );
17+
}
18+
1219
// Plugin version.
1320
if ( ! defined( 'WPGRAPHQL_VERSION' ) ) {
1421
define( 'WPGRAPHQL_VERSION', '1.16.0' );

deactivation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
*/
99
function graphql_deactivation_callback() {
1010

11+
if ( ! graphql_can_load_plugin() ) {
12+
return;
13+
}
14+
1115
// Fire an action when WPGraphQL is de-activating
1216
do_action( 'graphql_deactivate' );
1317

@@ -22,6 +26,10 @@ function graphql_deactivation_callback() {
2226
*/
2327
function delete_graphql_data() {
2428

29+
if ( ! class_exists( 'WPGraphQL' ) ) {
30+
return;
31+
}
32+
2533
// Check if the plugin is set to delete data or not
2634
$delete_data = get_graphql_setting( 'delete_data_on_deactivate' );
2735

wp-graphql.php

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,50 @@
3232
require_once __DIR__ . '/c3.php';
3333
}
3434

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';
4047
}
4148

4249
// Run this function when WPGraphQL is de-activated
4350
register_deactivation_hook( __FILE__, 'graphql_deactivation_callback' );
4451
register_activation_hook( __FILE__, 'graphql_activation_callback' );
4552

46-
// Bootstrap the plugin
47-
if ( ! class_exists( 'WPGraphQL' ) ) {
48-
require_once __DIR__ . '/src/WPGraphQL.php';
49-
}
50-
5153
/**
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+
*
5267
* @return bool
5368
*/
5469
function graphql_can_load_plugin(): bool {
5570

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+
5679
/**
5780
* WPGRAPHQL_AUTOLOAD can be set to "false" to prevent the autoloader from running.
5881
* In most cases, this is not something that should be disabled, but some environments
@@ -68,38 +91,27 @@ function graphql_can_load_plugin(): bool {
6891
// IF WPGRAPHQL_AUTOLOAD is defined as false,
6992
// but the WPGraphQL Class exists, we can assume the dependencies
7093
// are loaded from the parent project.
71-
if ( class_exists( '\WPGraphQL' ) ) {
72-
return true;
73-
}
94+
return true;
95+
7496
}
7597

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 ) ) {
7899
// 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+
}
80105
return true;
81-
// If the autoload file doesn't exist
82-
// manually load the individual files defined
83-
// in the composer.json
84106
}
85107

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;
101114

102-
return false;
103115
}
104116

105117
if ( ! function_exists( 'graphql_init' ) ) {
@@ -112,6 +124,8 @@ function graphql_init() {
112124

113125
// if the plugin can't be loaded, bail
114126
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' );
115129
return null;
116130
}
117131

@@ -123,6 +137,19 @@ function graphql_init() {
123137
}
124138
graphql_init();
125139

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+
126153
if ( defined( 'WP_CLI' ) && WP_CLI ) {
127154
require_once plugin_dir_path( __FILE__ ) . 'cli/wp-cli.php';
128155
}

0 commit comments

Comments
 (0)