Skip to content

Commit 36f3a7e

Browse files
authored
Merge pull request #3082 from wp-graphql/chore/connection-resolvers-cleanup
chore: prepare ConnectionResolver classes for v2 backport
2 parents 19c3665 + 6da42cf commit 36f3a7e

11 files changed

+586
-610
lines changed

src/Data/Connection/AbstractConnectionResolver.php

Lines changed: 507 additions & 529 deletions
Large diffs are not rendered by default.

src/Data/Connection/CommentConnectionResolver.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* @package WPGraphQL\Data\Connection
1313
*/
1414
class CommentConnectionResolver extends AbstractConnectionResolver {
15-
1615
/**
1716
* {@inheritDoc}
1817
*
@@ -23,7 +22,7 @@ class CommentConnectionResolver extends AbstractConnectionResolver {
2322
/**
2423
* {@inheritDoc}
2524
*
26-
* @throws \GraphQL\Error\UserError If there is a problem with the $args.
25+
* @throws \GraphQL\Error\UserError
2726
*/
2827
public function get_query_args() {
2928

@@ -178,21 +177,6 @@ public function get_ids_from_query() {
178177
return $ids;
179178
}
180179

181-
/**
182-
* {@inheritDoc}
183-
*
184-
* For example, if the $source were a post_type that didn't support comments, we could prevent
185-
* the connection query from even executing. In our case, we prevent comments from even showing
186-
* in the Schema for post types that don't have comment support, so we don't need to worry
187-
* about that, but there may be other situations where we'd need to prevent it.
188-
*
189-
* @return bool
190-
*/
191-
public function should_execute() {
192-
return true;
193-
}
194-
195-
196180
/**
197181
* Filters the GraphQL args before they are used in get_query_args().
198182
*
@@ -253,7 +237,6 @@ static function ( $id ) {
253237
}
254238

255239
/**
256-
*
257240
* Filters the GraphQL args before they are used in get_query_args().
258241
*
259242
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
@@ -329,4 +312,11 @@ public function sanitize_input_fields( array $args ) {
329312
public function is_valid_offset( $offset ) {
330313
return ! empty( get_comment( $offset ) );
331314
}
315+
316+
/**
317+
* {@inheritDoc}
318+
*/
319+
public function should_execute() {
320+
return true;
321+
}
332322
}

src/Data/Connection/ContentTypeConnectionResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function get_query_args() {
4040
return [];
4141
}
4242

43-
4443
/**
4544
* {@inheritDoc}
4645
*

src/Data/Connection/EnqueuedScriptsConnectionResolver.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function get_query_args() {
6060
return [];
6161
}
6262

63-
6463
/**
6564
* {@inheritDoc}
6665
*
@@ -78,11 +77,9 @@ public function get_loader_name() {
7877
}
7978

8079
/**
81-
* Determine if the model is valid
82-
*
83-
* @param ?\_WP_Dependency $model
80+
* {@inheritDoc}
8481
*
85-
* @return bool
82+
* @param ?\_WP_Dependency $model The model to check.
8683
*/
8784
protected function is_valid_model( $model ) {
8885
return isset( $model->handle );

src/Data/Connection/EnqueuedStylesheetConnectionResolver.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ static function ( $max, $source, $args, $context, ResolveInfo $info ) {
4141
}
4242

4343
/**
44-
* Get the IDs from the source
45-
*
46-
* @return mixed[]
44+
* {@inheritDoc}
4745
*/
4846
public function get_ids_from_query() {
4947
$ids = [];
@@ -68,9 +66,8 @@ public function get_query_args() {
6866
return [];
6967
}
7068

71-
7269
/**
73-
* Get the items from the source
70+
* {@inheritDoc}
7471
*
7572
* @return string[]
7673
*/
@@ -79,20 +76,16 @@ public function get_query() {
7976
}
8077

8178
/**
82-
* The name of the loader to load the data
83-
*
84-
* @return string
79+
* {@inheritDoc}
8580
*/
8681
public function get_loader_name() {
8782
return 'enqueued_stylesheet';
8883
}
8984

9085
/**
91-
* Determine if the model is valid
86+
* {@inheritDoc}
9287
*
9388
* @param ?\_WP_Dependency $model
94-
*
95-
* @return bool
9689
*/
9790
protected function is_valid_model( $model ) {
9891
return isset( $model->handle );
@@ -107,7 +100,7 @@ public function is_valid_offset( $offset ) {
107100
}
108101

109102
/**
110-
* D{@inheritDoc}
103+
* {@inheritDoc}
111104
*/
112105
public function should_execute() {
113106
return true;

src/Data/Connection/PluginConnectionResolver.php

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
*/
1010
class 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
}

src/Data/Connection/PostObjectConnectionResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class PostObjectConnectionResolver extends AbstractConnectionResolver {
2828
* @var \WP_Query|object
2929
*/
3030
protected $query;
31+
3132
/**
3233
* {@inheritDoc}
3334
*
@@ -450,7 +451,6 @@ public function sanitize_input_fields( array $where_args ) {
450451
* @return string[]|null
451452
*/
452453
public function sanitize_post_stati( $stati ) {
453-
454454
/**
455455
* If no stati is explicitly set by the input, default to publish. This will be the
456456
* most common scenario.
@@ -572,7 +572,6 @@ static function ( $id ) {
572572
}
573573

574574
/**
575-
*
576575
* Filters the GraphQL args before they are used in get_query_args().
577576
*
578577
* @param array<string,mixed> $args The GraphQL args passed to the resolver.

src/Data/Connection/TaxonomyConnectionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public function get_ids_from_query() {
3636
* {@inheritDoc}
3737
*/
3838
public function get_query_args() {
39-
// If any args are added to filter/sort the connection
39+
// If any args are added to filter/sort the connection.
4040
return [];
4141
}
4242

4343

4444
/**
45-
* Get the items from the source
45+
* {@inheritDoc}
4646
*
4747
* @return string[]
4848
*/

src/Data/Connection/TermObjectConnectionResolver.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function get_query_args() {
5252
$taxonomy = array_intersect( $all_taxonomies, $requested_taxonomies );
5353
}
5454

55-
5655
$query_args = [
5756
'taxonomy' => $taxonomy,
5857
];
@@ -179,15 +178,6 @@ public function get_loader_name() {
179178
return 'term';
180179
}
181180

182-
/**
183-
* {@inheritDoc}
184-
*
185-
* Default is true, meaning any time a TermObjectConnection resolver is asked for, it will execute.
186-
*/
187-
public function should_execute() {
188-
return true;
189-
}
190-
191181
/**
192182
* This maps the GraphQL "friendly" args to get_terms $args.
193183
* There's probably a cleaner/more dynamic way to approach this, but this was quick. I'd be down
@@ -286,7 +276,6 @@ static function ( $id ) {
286276
}
287277

288278
/**
289-
*
290279
* Filters the GraphQL args before they are used in get_query_args().
291280
*
292281
* @param array<string,mixed> $args The GraphQL args passed to the resolver.
@@ -306,4 +295,13 @@ static function ( $id ) {
306295
public function is_valid_offset( $offset ) {
307296
return get_term( absint( $offset ) ) instanceof \WP_Term;
308297
}
298+
299+
/**
300+
* {@inheritDoc}
301+
*
302+
* Default is true, meaning any time a TermObjectConnection resolver is asked for, it will execute.
303+
*/
304+
public function should_execute() {
305+
return true;
306+
}
309307
}

src/Data/Connection/ThemeConnectionResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public function get_query_args() {
4242
];
4343
}
4444

45-
4645
/**
47-
* Get the items from the source
46+
* {@inheritDoc}
4847
*
4948
* @return string[]
5049
*/

0 commit comments

Comments
 (0)