44/**
55 * Class PluginConnectionResolver - Connects plugins to other objects
66 *
7- * @package WPGraphQL\Data\Resolvers
7+ * @package WPGraphQL\Data\Connection
88 * @since 0.0.5
99 */
1010class PluginConnectionResolver extends AbstractConnectionResolver {
@@ -15,6 +15,13 @@ class PluginConnectionResolver extends AbstractConnectionResolver {
1515 */
1616 protected $ query ;
1717
18+ /**
19+ * A list of all the installed plugins, keyed by their type.
20+ *
21+ * @var ?array{site:array<string,mixed>,mustuse:array<string,mixed>,dropins:array<string,mixed>}
22+ */
23+ protected $ all_plugins ;
24+
1825 /**
1926 * {@inheritDoc}
2027 */
@@ -52,14 +59,10 @@ public function get_query_args() {
5259 * @return array<string,array<string,mixed>>
5360 */
5461 public function get_query () {
55- // File has not loaded.
56- require_once ABSPATH . 'wp-admin/includes/plugin.php ' ;
57- // This is missing must use and drop in plugins, so we need to fetch and merge them separately.
58- $ site_plugins = apply_filters ( 'all_plugins ' , get_plugins () );
59- $ mu_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'mustuse ' ) ? get_mu_plugins () : [];
60- $ dropin_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'dropins ' ) ? get_dropins () : [];
62+ // Get all plugins.
63+ $ plugins = $ this ->get_all_plugins ();
6164
62- $ all_plugins = array_merge ( $ site_plugins , $ mu_plugins , $ dropin_plugins );
65+ $ all_plugins = array_merge ( $ plugins [ ' site ' ] , $ plugins [ ' mustuse ' ] , $ plugins [ ' dropins ' ] );
6366
6467 // Bail early if no plugins.
6568 if ( empty ( $ all_plugins ) ) {
@@ -68,8 +71,8 @@ public function get_query() {
6871
6972 // Holds the plugin names sorted by status. The other ` status => [ plugin_names ] ` will be added later.
7073 $ plugins_by_status = [
71- 'mustuse ' => array_flip ( array_keys ( $ mu_plugins ) ),
72- 'dropins ' => array_flip ( array_keys ( $ dropin_plugins ) ),
74+ 'mustuse ' => array_flip ( array_keys ( $ plugins [ ' mustuse ' ] ) ),
75+ 'dropins ' => array_flip ( array_keys ( $ plugins [ ' mustuse ' ] ) ),
7376 ];
7477
7578 // Permissions.
@@ -230,14 +233,9 @@ public function get_loader_name() {
230233 * {@inheritDoc}
231234 */
232235 public function is_valid_offset ( $ offset ) {
233- // File has not loaded.
234- require_once ABSPATH . 'wp-admin/includes/plugin.php ' ;
235- // This is missing must use and drop in plugins, so we need to fetch and merge them separately.
236- $ site_plugins = apply_filters ( 'all_plugins ' , get_plugins () );
237- $ mu_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'mustuse ' ) ? get_mu_plugins () : [];
238- $ dropin_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'dropins ' ) ? get_dropins () : [];
236+ $ plugins = $ this ->get_all_plugins ();
239237
240- $ all_plugins = array_merge ( $ site_plugins , $ mu_plugins , $ dropin_plugins );
238+ $ all_plugins = array_merge ( $ plugins [ ' site ' ] , $ plugins [ ' mustuse ' ] , $ plugins [ ' dropins ' ] );
241239
242240 return array_key_exists ( $ offset , $ all_plugins );
243241 }
@@ -258,4 +256,31 @@ public function should_execute() {
258256
259257 return true ;
260258 }
259+
260+ /**
261+ * Gets all the installed plugins, including must use and drop in plugins.
262+ *
263+ * The result is cached in the ConnectionResolver instance.
264+ *
265+ * @return array{site:array<string,mixed>,mustuse:array<string,mixed>,dropins:array<string,mixed>}
266+ */
267+ protected function get_all_plugins (): array {
268+ if ( ! isset ( $ this ->all_plugins ) ) {
269+ // File has not loaded.
270+ require_once ABSPATH . 'wp-admin/includes/plugin.php ' ;
271+
272+ // This is missing must use and drop in plugins, so we need to fetch and merge them separately.
273+ $ site_plugins = apply_filters ( 'all_plugins ' , get_plugins () );
274+ $ mu_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'mustuse ' ) ? get_mu_plugins () : [];
275+ $ dropin_plugins = apply_filters ( 'show_advanced_plugins ' , true , 'dropins ' ) ? get_dropins () : [];
276+
277+ $ this ->all_plugins = [
278+ 'site ' => is_array ( $ site_plugins ) ? $ site_plugins : [],
279+ 'mustuse ' => $ mu_plugins ,
280+ 'dropins ' => $ dropin_plugins ,
281+ ];
282+ }
283+
284+ return $ this ->all_plugins ;
285+ }
261286}
0 commit comments