• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.72
/src/Type/ObjectType/RootQuery.php
1
<?php
2

3
namespace WPGraphQL\Type\ObjectType;
4

5
use GraphQL\Error\UserError;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use GraphQLRelay\Relay;
8
use WPGraphQL\AppContext;
9
use WPGraphQL\Data\Connection\ContentTypeConnectionResolver;
10
use WPGraphQL\Data\Connection\EnqueuedScriptsConnectionResolver;
11
use WPGraphQL\Data\Connection\EnqueuedStylesheetConnectionResolver;
12
use WPGraphQL\Data\Connection\MenuConnectionResolver;
13
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
14
use WPGraphQL\Data\Connection\ThemeConnectionResolver;
15
use WPGraphQL\Data\Connection\UserRoleConnectionResolver;
16
use WPGraphQL\Data\DataSource;
17
use WPGraphQL\Model\Post;
18
use WPGraphQL\Type\Connection\PostObjects;
19
use WPGraphQL\Utils\Utils;
20

21
/**
22
 * Class RootQuery
23
 *
24
 * @package WPGraphQL\Type\Object
25
 */
26
class RootQuery {
27

28
        /**
29
         * Register the RootQuery type
30
         *
31
         * @return void
32
         */
33
        public static function register_type() {
593✔
34
                register_graphql_object_type(
593✔
35
                        'RootQuery',
593✔
36
                        [
593✔
37
                                'description' => static function () {
593✔
38
                                        return __( 'The root entry point into the Graph', 'wp-graphql' );
69✔
39
                                },
593✔
40
                                'connections' => [
593✔
41
                                        'contentTypes'          => [
593✔
42
                                                'toType'  => 'ContentType',
593✔
43
                                                'resolve' => static function ( $source, $args, $context, $info ) {
593✔
44
                                                        $resolver = new ContentTypeConnectionResolver( $source, $args, $context, $info );
8✔
45

46
                                                        return $resolver->get_connection();
8✔
47
                                                },
593✔
48
                                        ],
593✔
49
                                        'menus'                 => [
593✔
50
                                                'toType'         => 'Menu',
593✔
51
                                                'connectionArgs' => [
593✔
52
                                                        'id'       => [
593✔
53
                                                                'type'        => 'Int',
593✔
54
                                                                'description' => static function () {
593✔
55
                                                                        return __( 'The database ID of the object', 'wp-graphql' );
15✔
56
                                                                },
593✔
57
                                                        ],
593✔
58
                                                        'location' => [
593✔
59
                                                                'type'        => 'MenuLocationEnum',
593✔
60
                                                                'description' => static function () {
593✔
61
                                                                        return __( 'The menu location for the menu being queried', 'wp-graphql' );
15✔
62
                                                                },
593✔
63
                                                        ],
593✔
64
                                                        'slug'     => [
593✔
65
                                                                'type'        => 'String',
593✔
66
                                                                'description' => static function () {
593✔
67
                                                                        return __( 'The slug of the menu to query items for', 'wp-graphql' );
15✔
68
                                                                },
593✔
69
                                                        ],
593✔
70
                                                ],
593✔
71
                                                'resolve'        => static function ( $source, $args, $context, $info ) {
593✔
72
                                                        $resolver = new MenuConnectionResolver( $source, $args, $context, $info, 'nav_menu' );
7✔
73

74
                                                        return $resolver->get_connection();
7✔
75
                                                },
593✔
76
                                        ],
593✔
77
                                        'plugins'               => [
593✔
78
                                                'toType'         => 'Plugin',
593✔
79
                                                'connectionArgs' => [
593✔
80
                                                        'search' => [
593✔
81
                                                                'name'        => 'search',
593✔
82
                                                                'type'        => 'String',
593✔
83
                                                                'description' => static function () {
593✔
84
                                                                        return __( 'Show plugin based on a keyword search.', 'wp-graphql' );
15✔
85
                                                                },
593✔
86
                                                        ],
593✔
87
                                                        'status' => [
593✔
88
                                                                'type'        => 'PluginStatusEnum',
593✔
89
                                                                'description' => static function () {
593✔
90
                                                                        return __( 'Show plugins with a specific status.', 'wp-graphql' );
15✔
91
                                                                },
593✔
92
                                                        ],
593✔
93
                                                        'stati'  => [
593✔
94
                                                                'type'        => [ 'list_of' => 'PluginStatusEnum' ],
593✔
95
                                                                'description' => static function () {
593✔
96
                                                                        return __( 'Retrieve plugins where plugin status is in an array.', 'wp-graphql' );
15✔
97
                                                                },
593✔
98
                                                        ],
593✔
99
                                                ],
593✔
100
                                                'resolve'        => static function ( $root, $args, $context, $info ) {
593✔
101
                                                        return DataSource::resolve_plugins_connection( $root, $args, $context, $info );
7✔
102
                                                },
593✔
103
                                        ],
593✔
104
                                        'registeredScripts'     => [
593✔
105
                                                'toType'  => 'EnqueuedScript',
593✔
106
                                                'resolve' => static function ( $source, $args, $context, $info ) {
593✔
107

108
                                                        // The connection resolver expects the source to include
109
                                                        // enqueuedScriptsQueue
110
                                                        $source                       = new \stdClass();
4✔
111
                                                        $source->enqueuedScriptsQueue = [];
4✔
112
                                                        global $wp_scripts;
4✔
113
                                                        do_action( 'wp_enqueue_scripts' );
4✔
114
                                                        $source->enqueuedScriptsQueue = array_keys( $wp_scripts->registered );
4✔
115
                                                        $resolver                     = new EnqueuedScriptsConnectionResolver( $source, $args, $context, $info );
4✔
116

117
                                                        return $resolver->get_connection();
4✔
118
                                                },
593✔
119
                                        ],
593✔
120
                                        'registeredStylesheets' => [
593✔
121
                                                'toType'  => 'EnqueuedStylesheet',
593✔
122
                                                'resolve' => static function ( $source, $args, $context, $info ) {
593✔
123

124
                                                        // The connection resolver expects the source to include
125
                                                        // enqueuedStylesheetsQueue
126
                                                        $source                           = new \stdClass();
4✔
127
                                                        $source->enqueuedStylesheetsQueue = [];
4✔
128
                                                        global $wp_styles;
4✔
129
                                                        do_action( 'wp_enqueue_scripts' );
4✔
130
                                                        $source->enqueuedStylesheetsQueue = array_keys( $wp_styles->registered );
4✔
131
                                                        $resolver                         = new EnqueuedStylesheetConnectionResolver( $source, $args, $context, $info );
4✔
132

133
                                                        return $resolver->get_connection();
4✔
134
                                                },
593✔
135
                                        ],
593✔
136
                                        'themes'                => [
593✔
137
                                                'toType'  => 'Theme',
593✔
138
                                                'resolve' => static function ( $root, $args, $context, $info ) {
593✔
139
                                                        $resolver = new ThemeConnectionResolver( $root, $args, $context, $info );
7✔
140

141
                                                        return $resolver->get_connection();
7✔
142
                                                },
593✔
143
                                        ],
593✔
144
                                        'revisions'             => [
593✔
145
                                                'toType'         => 'ContentNode',
593✔
146
                                                'queryClass'     => 'WP_Query',
593✔
147
                                                'connectionArgs' => PostObjects::get_connection_args(),
593✔
148
                                                'resolve'        => static function ( $root, $args, $context, $info ) {
593✔
149
                                                        $resolver = new PostObjectConnectionResolver( $root, $args, $context, $info, 'revision' );
2✔
150

151
                                                        return $resolver->get_connection();
2✔
152
                                                },
593✔
153
                                        ],
593✔
154
                                        'userRoles'             => [
593✔
155
                                                'toType'        => 'UserRole',
593✔
156
                                                'fromFieldName' => 'userRoles',
593✔
157
                                                'resolve'       => static function ( $user, $args, $context, $info ) {
593✔
158
                                                        $resolver = new UserRoleConnectionResolver( $user, $args, $context, $info );
5✔
159

160
                                                        return $resolver->get_connection();
5✔
161
                                                },
593✔
162
                                        ],
593✔
163
                                ],
593✔
164
                                'fields'      => static function () {
593✔
165
                                        return [
593✔
166
                                                'allSettings' => [
593✔
167
                                                        'type'        => 'Settings',
593✔
168
                                                        'description' => static function () {
593✔
169
                                                                return __( 'Entry point to get all settings for the site', 'wp-graphql' );
69✔
170
                                                        },
593✔
171
                                                        'resolve'     => static function () {
593✔
172
                                                                return true;
1✔
173
                                                        },
593✔
174
                                                ],
593✔
175
                                                'comment'     => [
593✔
176
                                                        'type'        => 'Comment',
593✔
177
                                                        'description' => static function () {
593✔
178
                                                                return __( 'Returns a Comment', 'wp-graphql' );
69✔
179
                                                        },
593✔
180
                                                        'args'        => [
593✔
181
                                                                'id'     => [
593✔
182
                                                                        'type'        => [
593✔
183
                                                                                'non_null' => 'ID',
593✔
184
                                                                        ],
593✔
185
                                                                        'description' => static function () {
593✔
186
                                                                                return __( 'Unique identifier for the comment node.', 'wp-graphql' );
69✔
187
                                                                        },
593✔
188
                                                                ],
593✔
189
                                                                'idType' => [
593✔
190
                                                                        'type'        => 'CommentNodeIdTypeEnum',
593✔
191
                                                                        'description' => static function () {
593✔
192
                                                                                return __( 'Type of unique identifier to fetch a comment by. Default is Global ID', 'wp-graphql' );
69✔
193
                                                                        },
593✔
194
                                                                ],
593✔
195
                                                        ],
593✔
196
                                                        'resolve'     => static function ( $_source, array $args, AppContext $context ) {
593✔
197
                                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
13✔
198

199
                                                                switch ( $id_type ) {
200
                                                                        case 'database_id':
13✔
201
                                                                                $id = absint( $args['id'] );
1✔
202
                                                                                break;
1✔
203
                                                                        default:
204
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
13✔
205
                                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
13✔
206
                                                                                        throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
×
207
                                                                                }
208
                                                                                $id = absint( $id_components['id'] );
13✔
209

210
                                                                                break;
13✔
211
                                                                }
212

213
                                                                return $context->get_loader( 'comment' )->load_deferred( $id );
13✔
214
                                                        },
593✔
215
                                                ],
593✔
216
                                                'contentNode' => [
593✔
217
                                                        'type'        => 'ContentNode',
593✔
218
                                                        'description' => static function () {
593✔
219
                                                                return __( 'A node used to manage content', 'wp-graphql' );
69✔
220
                                                        },
593✔
221
                                                        'args'        => [
593✔
222
                                                                'id'          => [
593✔
223
                                                                        'type'        => [
593✔
224
                                                                                'non_null' => 'ID',
593✔
225
                                                                        ],
593✔
226
                                                                        'description' => static function () {
593✔
227
                                                                                return __( 'Unique identifier for the content node.', 'wp-graphql' );
69✔
228
                                                                        },
593✔
229
                                                                ],
593✔
230
                                                                'idType'      => [
593✔
231
                                                                        'type'        => 'ContentNodeIdTypeEnum',
593✔
232
                                                                        'description' => static function () {
593✔
233
                                                                                return __( 'Type of unique identifier to fetch a content node by. Default is Global ID', 'wp-graphql' );
69✔
234
                                                                        },
593✔
235
                                                                ],
593✔
236
                                                                'contentType' => [
593✔
237
                                                                        'type'        => 'ContentTypeEnum',
593✔
238
                                                                        'description' => static function () {
593✔
239
                                                                                return __( 'The content type the node is used for. Required when idType is set to "name" or "slug"', 'wp-graphql' );
68✔
240
                                                                        },
593✔
241
                                                                ],
593✔
242
                                                                'asPreview'   => [
593✔
243
                                                                        'type'        => 'Boolean',
593✔
244
                                                                        'description' => static function () {
593✔
245
                                                                                return __( 'Whether to return the Preview Node instead of the Published Node. When the ID of a Node is provided along with asPreview being set to true, the preview node with un-published changes will be returned instead of the published node. If no preview node exists or the requester doesn\'t have proper capabilities to preview, no node will be returned. If the ID provided is a URI and has a preview query arg, it will be used as a fallback if the "asPreview" argument is not explicitly provided as an argument.', 'wp-graphql' );
69✔
246
                                                                        },
593✔
247
                                                                ],
593✔
248
                                                        ],
593✔
249
                                                        'resolve'     => static function ( $_root, $args, AppContext $context ) {
593✔
250
                                                                $idType = $args['idType'] ?? 'global_id';
18✔
251
                                                                switch ( $idType ) {
252
                                                                        case 'uri':
18✔
253
                                                                                return $context->node_resolver->resolve_uri(
7✔
254
                                                                                        $args['id'],
7✔
255
                                                                                        [
7✔
256
                                                                                                'nodeType' => 'ContentNode',
7✔
257
                                                                                                'asPreview' => $args['asPreview'] ?? null,
7✔
258
                                                                                        ]
7✔
259
                                                                                );
7✔
260
                                                                        case 'database_id':
17✔
261
                                                                                $post_id = absint( $args['id'] );
15✔
262
                                                                                break;
15✔
263
                                                                        case 'global_id':
2✔
264
                                                                        default:
265
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
2✔
266
                                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
2✔
267
                                                                                        throw new UserError( esc_html__( 'The ID input is invalid. Make sure you set the proper idType for your input.', 'wp-graphql' ) );
×
268
                                                                                }
269
                                                                                $post_id = absint( $id_components['id'] );
2✔
270
                                                                                break;
2✔
271
                                                                }
272

273
                                                                if ( isset( $args['asPreview'] ) && true === $args['asPreview'] ) {
17✔
274
                                                                        $post_id = Utils::get_post_preview_id( $post_id );
6✔
275
                                                                }
276

277
                                                                $allowed_post_types   = \WPGraphQL::get_allowed_post_types();
17✔
278
                                                                $allowed_post_types[] = 'revision';
17✔
279

280
                                                                return absint( $post_id ) ? $context->get_loader( 'post' )->load_deferred( $post_id )->then(
17✔
281
                                                                        static function ( $post ) use ( $allowed_post_types ) {
17✔
282

283
                                                                                // if the post isn't an instance of a Post model, return
284
                                                                                if ( ! $post instanceof Post ) {
17✔
285
                                                                                        return null;
1✔
286
                                                                                }
287

288
                                                                                if ( ! isset( $post->post_type ) || ! in_array( $post->post_type, $allowed_post_types, true ) ) {
17✔
289
                                                                                        return null;
×
290
                                                                                }
291

292
                                                                                return $post;
17✔
293
                                                                        }
17✔
294
                                                                ) : null;
17✔
295
                                                        },
593✔
296
                                                ],
593✔
297
                                                'contentType' => [
593✔
298
                                                        'type'        => 'ContentType',
593✔
299
                                                        'description' => static function () {
593✔
300
                                                                return __( 'Fetch a Content Type node by unique Identifier', 'wp-graphql' );
69✔
301
                                                        },
593✔
302
                                                        'args'        => [
593✔
303
                                                                'id'     => [
593✔
304
                                                                        'type'        => [ 'non_null' => 'ID' ],
593✔
305
                                                                        'description' => static function () {
593✔
306
                                                                                return __( 'Unique Identifier for the Content Type node.', 'wp-graphql' );
69✔
307
                                                                        },
593✔
308
                                                                ],
593✔
309
                                                                'idType' => [
593✔
310
                                                                        'type'        => 'ContentTypeIdTypeEnum',
593✔
311
                                                                        'description' => static function () {
593✔
312
                                                                                return __( 'Type of unique identifier to fetch a content type by. Default is Global ID', 'wp-graphql' );
69✔
313
                                                                        },
593✔
314
                                                                ],
593✔
315
                                                        ],
593✔
316
                                                        'resolve'     => static function ( $_root, $args, $context ) {
593✔
317
                                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
×
318

319
                                                                $id = null;
×
320
                                                                switch ( $id_type ) {
321
                                                                        case 'name':
×
322
                                                                                $id = $args['id'];
×
323
                                                                                break;
×
324
                                                                        case 'id':
×
325
                                                                        default:
326
                                                                                $id_parts = Relay::fromGlobalId( $args['id'] );
×
327
                                                                                if ( isset( $id_parts['id'] ) ) {
×
328
                                                                                        $id = $id_parts['id'];
×
329
                                                                                }
330
                                                                }
331

332
                                                                return ! empty( $id ) ? $context->get_loader( 'post_type' )->load_deferred( $id ) : null;
×
333
                                                        },
593✔
334
                                                ],
593✔
335
                                                'taxonomy'    => [
593✔
336
                                                        'type'        => 'Taxonomy',
593✔
337
                                                        'description' => static function () {
593✔
338
                                                                return __( 'Fetch a Taxonomy node by unique Identifier', 'wp-graphql' );
69✔
339
                                                        },
593✔
340
                                                        'args'        => [
593✔
341
                                                                'id'     => [
593✔
342
                                                                        'type'        => [ 'non_null' => 'ID' ],
593✔
343
                                                                        'description' => static function () {
593✔
344
                                                                                return __( 'Unique Identifier for the Taxonomy node.', 'wp-graphql' );
69✔
345
                                                                        },
593✔
346
                                                                ],
593✔
347
                                                                'idType' => [
593✔
348
                                                                        'type'        => 'TaxonomyIdTypeEnum',
593✔
349
                                                                        'description' => static function () {
593✔
350
                                                                                return __( 'Type of unique identifier to fetch a taxonomy by. Default is Global ID', 'wp-graphql' );
69✔
351
                                                                        },
593✔
352
                                                                ],
593✔
353
                                                        ],
593✔
354
                                                        'resolve'     => static function ( $_root, $args, $context ) {
593✔
355
                                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
×
356

357
                                                                $id = null;
×
358
                                                                switch ( $id_type ) {
359
                                                                        case 'name':
×
360
                                                                                $id = $args['id'];
×
361
                                                                                break;
×
362
                                                                        case 'id':
×
363
                                                                        default:
364
                                                                                $id_parts = Relay::fromGlobalId( $args['id'] );
×
365
                                                                                if ( isset( $id_parts['id'] ) ) {
×
366
                                                                                        $id = $id_parts['id'];
×
367
                                                                                }
368
                                                                }
369

370
                                                                return ! empty( $id ) ? $context->get_loader( 'taxonomy' )->load_deferred( $id ) : null;
×
371
                                                        },
593✔
372
                                                ],
593✔
373
                                                'node'        => [
593✔
374
                                                        'type'        => 'Node',
593✔
375
                                                        'description' => static function () {
593✔
376
                                                                return __( 'Fetches an object given its ID', 'wp-graphql' );
69✔
377
                                                        },
593✔
378
                                                        'args'        => [
593✔
379
                                                                'id' => [
593✔
380
                                                                        'type'        => 'ID',
593✔
381
                                                                        'description' => static function () {
593✔
382
                                                                                return __( 'The unique identifier of the node', 'wp-graphql' );
69✔
383
                                                                        },
593✔
384
                                                                ],
593✔
385
                                                        ],
593✔
386
                                                        'resolve'     => static function ( $root, $args, AppContext $context, ResolveInfo $info ) {
593✔
387
                                                                return ! empty( $args['id'] ) ? DataSource::resolve_node( $args['id'], $context, $info ) : null;
17✔
388
                                                        },
593✔
389
                                                ],
593✔
390
                                                'nodeByUri'   => [
593✔
391
                                                        'type'        => 'UniformResourceIdentifiable',
593✔
392
                                                        'description' => static function () {
593✔
393
                                                                return __( 'Fetches an object given its Unique Resource Identifier', 'wp-graphql' );
69✔
394
                                                        },
593✔
395
                                                        'args'        => [
593✔
396
                                                                'uri' => [
593✔
397
                                                                        'type'        => [ 'non_null' => 'String' ],
593✔
398
                                                                        'description' => static function () {
593✔
399
                                                                                return __( 'Unique Resource Identifier in the form of a path or permalink for a node. Ex: "/hello-world"', 'wp-graphql' );
69✔
400
                                                                        },
593✔
401
                                                                ],
593✔
402
                                                        ],
593✔
403
                                                        'resolve'     => static function ( $root, $args, AppContext $context ) {
593✔
404
                                                                return ! empty( $args['uri'] ) ? $context->node_resolver->resolve_uri( $args['uri'] ) : null;
34✔
405
                                                        },
593✔
406
                                                ],
593✔
407
                                                'menu'        => [
593✔
408
                                                        'type'        => 'Menu',
593✔
409
                                                        'description' => static function () {
593✔
410
                                                                return __( 'A WordPress navigation menu', 'wp-graphql' );
69✔
411
                                                        },
593✔
412
                                                        'args'        => [
593✔
413
                                                                'id'     => [
593✔
414
                                                                        'type'        => [
593✔
415
                                                                                'non_null' => 'ID',
593✔
416
                                                                        ],
593✔
417
                                                                        'description' => static function () {
593✔
418
                                                                                return __( 'The globally unique identifier of the menu.', 'wp-graphql' );
69✔
419
                                                                        },
593✔
420
                                                                ],
593✔
421
                                                                'idType' => [
593✔
422
                                                                        'type'        => 'MenuNodeIdTypeEnum',
593✔
423
                                                                        'description' => static function () {
593✔
424
                                                                                return __( 'Type of unique identifier to fetch a menu by. Default is Global ID', 'wp-graphql' );
69✔
425
                                                                        },
593✔
426
                                                                ],
593✔
427
                                                        ],
593✔
428
                                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
593✔
429
                                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
3✔
430

431
                                                                switch ( $id_type ) {
432
                                                                        case 'database_id':
3✔
433
                                                                                $id = absint( $args['id'] );
1✔
434
                                                                                break;
1✔
435
                                                                        case 'location':
3✔
436
                                                                                $locations = get_nav_menu_locations();
1✔
437

438
                                                                                if ( ! isset( $locations[ $args['id'] ] ) || ! absint( $locations[ $args['id'] ] ) ) {
1✔
439
                                                                                        throw new UserError( esc_html__( 'No menu set for the provided location', 'wp-graphql' ) );
×
440
                                                                                }
441

442
                                                                                $id = absint( $locations[ $args['id'] ] );
1✔
443
                                                                                break;
1✔
444
                                                                        case 'name':
2✔
445
                                                                                $menu = new \WP_Term_Query(
1✔
446
                                                                                        [
1✔
447
                                                                                                'taxonomy' => 'nav_menu',
1✔
448
                                                                                                'fields'   => 'ids',
1✔
449
                                                                                                'name'     => $args['id'],
1✔
450
                                                                                                'include_children' => false,
1✔
451
                                                                                                'count'    => false,
1✔
452
                                                                                        ]
1✔
453
                                                                                );
1✔
454
                                                                                $id   = ! empty( $menu->terms ) ? (int) $menu->terms[0] : null;
1✔
455
                                                                                break;
1✔
456
                                                                        case 'slug':
2✔
457
                                                                                $menu = new \WP_Term_Query(
2✔
458
                                                                                        [
2✔
459
                                                                                                'taxonomy' => 'nav_menu',
2✔
460
                                                                                                'fields'   => 'ids',
2✔
461
                                                                                                'slug'     => $args['id'],
2✔
462
                                                                                                'include_children' => false,
2✔
463
                                                                                                'count'    => false,
2✔
464
                                                                                        ]
2✔
465
                                                                                );
2✔
466
                                                                                $id   = ! empty( $menu->terms ) ? (int) $menu->terms[0] : null;
2✔
467
                                                                                break;
2✔
468
                                                                        default:
469
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
1✔
470
                                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
1✔
471
                                                                                        throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
×
472
                                                                                }
473
                                                                                $id = absint( $id_components['id'] );
1✔
474

475
                                                                                break;
1✔
476
                                                                }
477

478
                                                                return ! empty( $id ) ? $context->get_loader( 'term' )->load_deferred( absint( $id ) ) : null;
3✔
479
                                                        },
593✔
480
                                                ],
593✔
481
                                                'menuItem'    => [
593✔
482
                                                        'type'        => 'MenuItem',
593✔
483
                                                        'description' => static function () {
593✔
484
                                                                return __( 'A WordPress navigation menu item', 'wp-graphql' );
69✔
485
                                                        },
593✔
486
                                                        'args'        => [
593✔
487
                                                                'id'     => [
593✔
488
                                                                        'type'        => [
593✔
489
                                                                                'non_null' => 'ID',
593✔
490
                                                                        ],
593✔
491
                                                                        'description' => static function () {
593✔
492
                                                                                return __( 'The globally unique identifier of the menu item.', 'wp-graphql' );
69✔
493
                                                                        },
593✔
494
                                                                ],
593✔
495
                                                                'idType' => [
593✔
496
                                                                        'type'        => 'MenuItemNodeIdTypeEnum',
593✔
497
                                                                        'description' => static function () {
593✔
498
                                                                                return __( 'Type of unique identifier to fetch a menu item by. Default is Global ID', 'wp-graphql' );
69✔
499
                                                                        },
593✔
500
                                                                ],
593✔
501
                                                        ],
593✔
502
                                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
593✔
503
                                                                $id_type = isset( $args['idType'] ) ? $args['idType'] : 'id';
4✔
504

505
                                                                switch ( $id_type ) {
506
                                                                        case 'database_id':
4✔
507
                                                                                $id = absint( $args['id'] );
4✔
508
                                                                                break;
4✔
509
                                                                        default:
510
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
2✔
511
                                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
2✔
512
                                                                                        throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
×
513
                                                                                }
514
                                                                                $id = absint( $id_components['id'] );
2✔
515

516
                                                                                break;
2✔
517
                                                                }
518

519
                                                                return $context->get_loader( 'post' )->load_deferred( absint( $id ) );
4✔
520
                                                        },
593✔
521
                                                ],
593✔
522
                                                'plugin'      => [
593✔
523
                                                        'type'        => 'Plugin',
593✔
524
                                                        'description' => static function () {
593✔
525
                                                                return __( 'A WordPress plugin', 'wp-graphql' );
69✔
526
                                                        },
593✔
527
                                                        'args'        => [
593✔
528
                                                                'id' => [
593✔
529
                                                                        'type'        => [
593✔
530
                                                                                'non_null' => 'ID',
593✔
531
                                                                        ],
593✔
532
                                                                        'description' => static function () {
593✔
533
                                                                                return __( 'The globally unique identifier of the plugin.', 'wp-graphql' );
69✔
534
                                                                        },
593✔
535
                                                                ],
593✔
536
                                                        ],
593✔
537
                                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
593✔
538
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
2✔
539

540
                                                                return ! empty( $id_components['id'] ) ? $context->get_loader( 'plugin' )->load_deferred( $id_components['id'] ) : null;
2✔
541
                                                        },
593✔
542
                                                ],
593✔
543
                                                'termNode'    => [
593✔
544
                                                        'type'        => 'TermNode',
593✔
545
                                                        'description' => static function () {
593✔
546
                                                                return __( 'A node in a taxonomy used to group and relate content nodes', 'wp-graphql' );
69✔
547
                                                        },
593✔
548
                                                        'args'        => [
593✔
549
                                                                'id'       => [
593✔
550
                                                                        'type'        => [
593✔
551
                                                                                'non_null' => 'ID',
593✔
552
                                                                        ],
593✔
553
                                                                        'description' => static function () {
593✔
554
                                                                                return __( 'Unique identifier for the term node.', 'wp-graphql' );
69✔
555
                                                                        },
593✔
556
                                                                ],
593✔
557
                                                                'idType'   => [
593✔
558
                                                                        'type'        => 'TermNodeIdTypeEnum',
593✔
559
                                                                        'description' => static function () {
593✔
560
                                                                                return __( 'Type of unique identifier to fetch a term node by. Default is Global ID', 'wp-graphql' );
69✔
561
                                                                        },
593✔
562
                                                                ],
593✔
563
                                                                'taxonomy' => [
593✔
564
                                                                        'type'        => 'TaxonomyEnum',
593✔
565
                                                                        'description' => static function () {
593✔
566
                                                                                return __( 'The taxonomy of the tern node. Required when idType is set to "name" or "slug"', 'wp-graphql' );
69✔
567
                                                                        },
593✔
568
                                                                ],
593✔
569
                                                        ],
593✔
570
                                                        'resolve'     => static function ( $root, $args, AppContext $context ) {
593✔
571
                                                                $idType  = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
6✔
572
                                                                $term_id = null;
6✔
573

574
                                                                switch ( $idType ) {
575
                                                                        case 'slug':
6✔
576
                                                                        case 'name':
5✔
577
                                                                        case 'database_id':
4✔
578
                                                                                $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : null;
3✔
579
                                                                                if ( empty( $taxonomy ) && in_array(
3✔
580
                                                                                        $idType,
3✔
581
                                                                                        [
3✔
582
                                                                                                'name',
3✔
583
                                                                                                'slug',
3✔
584
                                                                                        ],
3✔
585
                                                                                        true
3✔
586
                                                                                ) ) {
3✔
587
                                                                                        throw new UserError( esc_html__( 'When fetching a Term Node by "slug" or "name", the "taxonomy" also needs to be set as an input.', 'wp-graphql' ) );
×
588
                                                                                }
589
                                                                                if ( 'database_id' === $idType ) {
3✔
590
                                                                                        $term = get_term( absint( $args['id'] ) );
1✔
591
                                                                                } else {
592
                                                                                        $term = get_term_by( $idType, $args['id'], $taxonomy );
2✔
593
                                                                                }
594
                                                                                $term_id = isset( $term->term_id ) ? absint( $term->term_id ) : null;
3✔
595

596
                                                                                break;
3✔
597
                                                                        case 'uri':
3✔
598
                                                                                return $context->node_resolver->resolve_uri(
2✔
599
                                                                                        $args['id'],
2✔
600
                                                                                        [
2✔
601
                                                                                                'nodeType' => 'TermNode',
2✔
602
                                                                                        ]
2✔
603
                                                                                );
2✔
604
                                                                        case 'global_id':
1✔
605
                                                                        default:
606
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
1✔
607
                                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
1✔
608
                                                                                        throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
×
609
                                                                                }
610
                                                                                $term_id = absint( $id_components['id'] );
1✔
611
                                                                                break;
1✔
612
                                                                }
613

614
                                                                return ! empty( $term_id ) ? $context->get_loader( 'term' )->load_deferred( $term_id ) : null;
4✔
615
                                                        },
593✔
616
                                                ],
593✔
617
                                                'theme'       => [
593✔
618
                                                        'type'        => 'Theme',
593✔
619
                                                        'description' => static function () {
593✔
620
                                                                return __( 'A Theme object', 'wp-graphql' );
69✔
621
                                                        },
593✔
622
                                                        'args'        => [
593✔
623
                                                                'id' => [
593✔
624
                                                                        'type'        => [
593✔
625
                                                                                'non_null' => 'ID',
593✔
626
                                                                        ],
593✔
627
                                                                        'description' => static function () {
593✔
628
                                                                                return __( 'The globally unique identifier of the theme.', 'wp-graphql' );
69✔
629
                                                                        },
593✔
630
                                                                ],
593✔
631
                                                        ],
593✔
632
                                                        'resolve'     => static function ( $source, array $args ) {
593✔
633
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
2✔
634

635
                                                                return DataSource::resolve_theme( $id_components['id'] );
2✔
636
                                                        },
593✔
637
                                                ],
593✔
638
                                                'user'        => [
593✔
639
                                                        'type'        => 'User',
593✔
640
                                                        'description' => static function () {
593✔
641
                                                                return __( 'Returns a user', 'wp-graphql' );
69✔
642
                                                        },
593✔
643
                                                        'args'        => [
593✔
644
                                                                'id'     => [
593✔
645
                                                                        'type'        => [
593✔
646
                                                                                'non_null' => 'ID',
593✔
647
                                                                        ],
593✔
648
                                                                        'description' => static function () {
593✔
649
                                                                                return __( 'The globally unique identifier of the user.', 'wp-graphql' );
69✔
650
                                                                        },
593✔
651
                                                                ],
593✔
652
                                                                'idType' => [
593✔
653
                                                                        'type'        => 'UserNodeIdTypeEnum',
593✔
654
                                                                        'description' => static function () {
593✔
655
                                                                                return __( 'Type of unique identifier to fetch a user by. Default is Global ID', 'wp-graphql' );
69✔
656
                                                                        },
593✔
657
                                                                ],
593✔
658
                                                        ],
593✔
659
                                                        'resolve'     => static function ( $source, array $args, $context ) {
593✔
660
                                                                $idType = isset( $args['idType'] ) ? $args['idType'] : 'id';
21✔
661

662
                                                                switch ( $idType ) {
663
                                                                        case 'database_id':
21✔
664
                                                                                $id = absint( $args['id'] );
10✔
665
                                                                                break;
10✔
666
                                                                        case 'uri':
14✔
667
                                                                                return $context->node_resolver->resolve_uri(
4✔
668
                                                                                        $args['id'],
4✔
669
                                                                                        [
4✔
670
                                                                                                'nodeType' => 'User',
4✔
671
                                                                                        ]
4✔
672
                                                                                );
4✔
673
                                                                        case 'login':
13✔
674
                                                                                $current_user = wp_get_current_user();
3✔
675
                                                                                if ( $current_user->user_login !== $args['id'] ) {
3✔
676
                                                                                        if ( ! current_user_can( 'list_users' ) ) {
2✔
677
                                                                                                throw new UserError( esc_html__( 'You do not have permission to request a User by Username', 'wp-graphql' ) );
2✔
678
                                                                                        }
679
                                                                                }
680

681
                                                                                $user = get_user_by( 'login', $args['id'] );
2✔
682
                                                                                $id   = isset( $user->ID ) ? $user->ID : null;
2✔
683
                                                                                break;
2✔
684
                                                                        case 'email':
13✔
685
                                                                                $current_user = wp_get_current_user();
3✔
686
                                                                                if ( $current_user->user_email !== $args['id'] ) {
3✔
687
                                                                                        if ( ! current_user_can( 'list_users' ) ) {
2✔
688
                                                                                                throw new UserError( esc_html__( 'You do not have permission to request a User by Email', 'wp-graphql' ) );
2✔
689
                                                                                        }
690
                                                                                }
691

692
                                                                                $user = get_user_by( 'email', $args['id'] );
2✔
693
                                                                                $id   = isset( $user->ID ) ? $user->ID : null;
2✔
694
                                                                                break;
2✔
695
                                                                        case 'slug':
13✔
696
                                                                                $user = get_user_by( 'slug', $args['id'] );
3✔
697
                                                                                $id   = isset( $user->ID ) ? $user->ID : null;
3✔
698
                                                                                break;
3✔
699
                                                                        case 'id':
13✔
700
                                                                        default:
701
                                                                                $id_components = Relay::fromGlobalId( $args['id'] );
13✔
702
                                                                                $id            = absint( $id_components['id'] );
13✔
703
                                                                                break;
13✔
704
                                                                }
705

706
                                                                return ! empty( $id ) ? $context->get_loader( 'user' )->load_deferred( $id ) : null;
20✔
707
                                                        },
593✔
708
                                                ],
593✔
709
                                                'userRole'    => [
593✔
710
                                                        'type'        => 'UserRole',
593✔
711
                                                        'description' => static function () {
593✔
712
                                                                return __( 'Returns a user role', 'wp-graphql' );
69✔
713
                                                        },
593✔
714
                                                        'args'        => [
593✔
715
                                                                'id' => [
593✔
716
                                                                        'type'        => [
593✔
717
                                                                                'non_null' => 'ID',
593✔
718
                                                                        ],
593✔
719
                                                                        'description' => static function () {
593✔
720
                                                                                return __( 'The globally unique identifier of the user object.', 'wp-graphql' );
69✔
721
                                                                        },
593✔
722
                                                                ],
593✔
723
                                                        ],
593✔
724
                                                        'resolve'     => static function ( $source, array $args ) {
593✔
725
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
2✔
726

727
                                                                return DataSource::resolve_user_role( $id_components['id'] );
2✔
728
                                                        },
593✔
729
                                                ],
593✔
730
                                                'viewer'      => [
593✔
731
                                                        'type'        => 'User',
593✔
732
                                                        'description' => static function () {
593✔
733
                                                                return __( 'Returns the current user', 'wp-graphql' );
69✔
734
                                                        },
593✔
735
                                                        'resolve'     => static function ( $source, array $args, AppContext $context ) {
593✔
736
                                                                return ! empty( $context->viewer->ID ) ? $context->get_loader( 'user' )->load_deferred( $context->viewer->ID ) : null;
2✔
737
                                                        },
593✔
738
                                                ],
593✔
739
                                        ];
593✔
740
                                },
593✔
741
                        ]
593✔
742
                );
593✔
743
        }
744

745
        /**
746
         * Register RootQuery fields for Post Objects of supported post types
747
         *
748
         * @return void
749
         */
750
        public static function register_post_object_fields() {
594✔
751
                $allowed_post_types = \WPGraphQL::get_allowed_post_types( 'objects', [ 'graphql_register_root_field' => true ] );
593✔
752

753
                foreach ( $allowed_post_types as $post_type_object ) {
593✔
754
                        register_graphql_field(
594✔
755
                                'RootQuery',
594✔
756
                                $post_type_object->graphql_single_name,
594✔
757
                                [
594✔
758
                                        'type'        => $post_type_object->graphql_single_name,
594✔
759
                                        'description' => static function () use ( $post_type_object ) {
594✔
760
                                                return sprintf(
69✔
761
                                                        // translators: %1$s is the post type GraphQL name, %2$s is the post type description
762
                                                        __( 'An object of the %1$s Type. %2$s', 'wp-graphql' ),
69✔
763
                                                        $post_type_object->graphql_single_name,
69✔
764
                                                        $post_type_object->description
69✔
765
                                                );
69✔
766
                                        },
594✔
767
                                        'args'        => [
594✔
768
                                                'id'        => [
594✔
769
                                                        'type'        => [
594✔
770
                                                                'non_null' => 'ID',
594✔
771
                                                        ],
594✔
772
                                                        'description' => static function () {
594✔
773
                                                                return __( 'The globally unique identifier of the object.', 'wp-graphql' );
69✔
774
                                                        },
594✔
775
                                                ],
594✔
776
                                                'idType'    => [
594✔
777
                                                        'type'        => $post_type_object->graphql_single_name . 'IdType',
594✔
778
                                                        'description' => static function () {
594✔
779
                                                                return __( 'Type of unique identifier to fetch by. Default is Global ID', 'wp-graphql' );
69✔
780
                                                        },
594✔
781
                                                ],
594✔
782
                                                'asPreview' => [
594✔
783
                                                        'type'        => 'Boolean',
594✔
784
                                                        'description' => static function () {
594✔
785
                                                                return __( 'Whether to return the Preview Node instead of the Published Node. When the ID of a Node is provided along with asPreview being set to true, the preview node with un-published changes will be returned instead of the published node. If no preview node exists or the requester doesn\'t have proper capabilities to preview, no node will be returned. If the ID provided is a URI and has a preview query arg, it will be used as a fallback if the "asPreview" argument is not explicitly provided as an argument.', 'wp-graphql' );
69✔
786
                                                        },
594✔
787
                                                ],
594✔
788
                                        ],
594✔
789
                                        'resolve'     => static function ( $source, array $args, AppContext $context ) use ( $post_type_object ) {
594✔
790
                                                $idType  = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
105✔
791
                                                $post_id = null;
105✔
792
                                                switch ( $idType ) {
793
                                                        case 'slug':
105✔
794
                                                                return $context->node_resolver->resolve_uri(
20✔
795
                                                                        $args['id'],
20✔
796
                                                                        [
20✔
797
                                                                                'name'      => $args['id'],
20✔
798
                                                                                'post_type' => $post_type_object->name,
20✔
799
                                                                                'nodeType'  => 'ContentNode',
20✔
800
                                                                                'asPreview' => $args['asPreview'] ?? null,
20✔
801
                                                                        ]
20✔
802
                                                                );
20✔
803
                                                        case 'uri':
97✔
804
                                                                return $context->node_resolver->resolve_uri(
21✔
805
                                                                        $args['id'],
21✔
806
                                                                        [
21✔
807
                                                                                'post_type' => $post_type_object->name,
21✔
808
                                                                                'archive'   => false,
21✔
809
                                                                                'nodeType'  => 'ContentNode',
21✔
810
                                                                                'asPreview' => $args['asPreview'] ?? null,
21✔
811
                                                                        ]
21✔
812
                                                                );
21✔
813
                                                        case 'database_id':
89✔
814
                                                                $post_id = absint( $args['id'] );
58✔
815
                                                                break;
58✔
816
                                                        case 'source_url':
34✔
817
                                                                $url     = $args['id'];
2✔
818
                                                                $post_id = attachment_url_to_postid( $url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid
2✔
819
                                                                if ( empty( $post_id ) ) {
2✔
820
                                                                        return null;
1✔
821
                                                                }
822
                                                                $post_id = absint( attachment_url_to_postid( $url ) ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.attachment_url_to_postid_attachment_url_to_postid
1✔
823
                                                                break;
1✔
824
                                                        case 'global_id':
32✔
825
                                                        default:
826
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
32✔
827
                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
32✔
828
                                                                        throw new UserError( esc_html__( 'The ID input is invalid. Make sure you set the proper idType for your input.', 'wp-graphql' ) );
1✔
829
                                                                }
830
                                                                $post_id = absint( $id_components['id'] );
31✔
831
                                                                break;
31✔
832
                                                }
833

834
                                                if ( isset( $args['asPreview'] ) && true === $args['asPreview'] ) {
87✔
835
                                                        $post_id = Utils::get_post_preview_id( $post_id );
8✔
836
                                                }
837

838
                                                return absint( $post_id ) ? $context->get_loader( 'post' )->load_deferred( $post_id )->then(
87✔
839
                                                        static function ( $post ) use ( $post_type_object ) {
87✔
840

841
                                                                // if the post isn't an instance of a Post model, return
842
                                                                if ( ! $post instanceof Post ) {
87✔
843
                                                                        return null;
4✔
844
                                                                }
845

846
                                                                if ( ! isset( $post->post_type ) || ! in_array(
84✔
847
                                                                        $post->post_type,
84✔
848
                                                                        [
84✔
849
                                                                                'revision',
84✔
850
                                                                                $post_type_object->name,
84✔
851
                                                                        ],
84✔
852
                                                                        true
84✔
853
                                                                ) ) {
84✔
854
                                                                        return null;
1✔
855
                                                                }
856

857
                                                                return $post;
84✔
858
                                                        }
87✔
859
                                                ) : null;
87✔
860
                                        },
594✔
861
                                ]
594✔
862
                        );
594✔
863
                        $post_by_args = [
593✔
864
                                'id'  => [
593✔
865
                                        'type'        => 'ID',
593✔
866
                                        'description' => static function () use ( $post_type_object ) {
593✔
867
                                                return sprintf(
69✔
868
                                                        // translators: %s is the post type's GraphQL name.
869
                                                        __( 'Get the %s object by its global ID', 'wp-graphql' ),
69✔
870
                                                        $post_type_object->graphql_single_name
69✔
871
                                                );
69✔
872
                                        },
593✔
873
                                ],
593✔
874
                                $post_type_object->graphql_single_name . 'Id' => [
593✔
875
                                        'type'        => 'Int',
593✔
876
                                        'description' => static function () use ( $post_type_object ) {
593✔
877
                                                return sprintf(
69✔
878
                                                        // translators: %s is the post type's GraphQL name.
879
                                                        __( 'Get the %s by its database ID', 'wp-graphql' ),
69✔
880
                                                        $post_type_object->graphql_single_name
69✔
881
                                                );
69✔
882
                                        },
593✔
883
                                ],
593✔
884
                                'uri' => [
593✔
885
                                        'type'        => 'String',
593✔
886
                                        'description' => static function () use ( $post_type_object ) {
593✔
887
                                                return sprintf(
69✔
888
                                                        // translators: %s is the post type's GraphQL name.
889
                                                        __( 'Get the %s by its uri', 'wp-graphql' ),
69✔
890
                                                        $post_type_object->graphql_single_name
69✔
891
                                                );
69✔
892
                                        },
593✔
893
                                ],
593✔
894
                        ];
593✔
895
                        if ( false === $post_type_object->hierarchical ) {
593✔
896
                                $post_by_args['slug'] = [
593✔
897
                                        'type'        => 'String',
593✔
898
                                        'description' => static function () use ( $post_type_object ) {
593✔
899
                                                return sprintf(
69✔
900
                                                        // translators: %s is the post type's GraphQL name.
901
                                                        __( 'Get the %s by its slug (only available for non-hierarchical types)', 'wp-graphql' ),
69✔
902
                                                        $post_type_object->graphql_single_name
69✔
903
                                                );
69✔
904
                                        },
593✔
905
                                ];
593✔
906
                        }
907

908
                        /**
909
                         * @deprecated Deprecated in favor of single node entry points
910
                         */
911
                        register_graphql_field(
594✔
912
                                'RootQuery',
594✔
913
                                $post_type_object->graphql_single_name . 'By',
594✔
914
                                [
594✔
915
                                        'type'              => $post_type_object->graphql_single_name,
594✔
916
                                        'deprecationReason' => static function () {
594✔
917
                                                return __( 'Deprecated in favor of using the single entry point for this type with ID and IDType fields. For example, instead of postBy( id: "" ), use post(id: "" idType: "")', 'wp-graphql' );
69✔
918
                                        },
594✔
919
                                        'description'       => static function () use ( $post_type_object ) {
594✔
920
                                                return sprintf(
69✔
921
                                                        // translators: %s is the post type's GraphQL name.
922
                                                        __( 'A %s object', 'wp-graphql' ),
69✔
923
                                                        $post_type_object->graphql_single_name
69✔
924
                                                );
69✔
925
                                        },
594✔
926
                                        'args'              => $post_by_args,
594✔
927
                                        'resolve'           => static function ( $source, array $args, $context ) use ( $post_type_object ) {
594✔
928
                                                $post_id = 0;
15✔
929

930
                                                if ( ! empty( $args['id'] ) ) {
15✔
931
                                                        $id_components = Relay::fromGlobalId( $args['id'] );
8✔
932
                                                        if ( empty( $id_components['id'] ) || empty( $id_components['type'] ) ) {
8✔
933
                                                                throw new UserError( esc_html__( 'The "id" is invalid', 'wp-graphql' ) );
1✔
934
                                                        }
935
                                                        $post_id = absint( $id_components['id'] );
7✔
936
                                                } elseif ( ! empty( $args[ lcfirst( $post_type_object->graphql_single_name . 'Id' ) ] ) ) {
10✔
937
                                                        $id      = $args[ lcfirst( $post_type_object->graphql_single_name . 'Id' ) ];
8✔
938
                                                        $post_id = absint( $id );
8✔
939
                                                } elseif ( ! empty( $args['uri'] ) ) {
5✔
940
                                                        return $context->node_resolver->resolve_uri(
4✔
941
                                                                $args['uri'],
4✔
942
                                                                [
4✔
943
                                                                        'post_type' => $post_type_object->name,
4✔
944
                                                                        'archive'   => false,
4✔
945
                                                                        'nodeType'  => 'ContentNode',
4✔
946
                                                                ]
4✔
947
                                                        );
4✔
948
                                                } elseif ( ! empty( $args['slug'] ) ) {
2✔
949
                                                        $slug = esc_html( $args['slug'] );
2✔
950

951
                                                        return $context->node_resolver->resolve_uri(
2✔
952
                                                                $slug,
2✔
953
                                                                [
2✔
954
                                                                        'name'      => $slug,
2✔
955
                                                                        'post_type' => $post_type_object->name,
2✔
956
                                                                        'nodeType'  => 'ContentNode',
2✔
957
                                                                ]
2✔
958
                                                        );
2✔
959
                                                }
960

961
                                                return $context->get_loader( 'post' )->load_deferred( $post_id )->then(
12✔
962
                                                        static function ( $post ) use ( $post_type_object ) {
12✔
963

964
                                                                // if the post type object isn't an instance of WP_Post_Type, return
965
                                                                if ( ! $post_type_object instanceof \WP_Post_Type ) {
12✔
966
                                                                        return null;
×
967
                                                                }
968

969
                                                                // if the post isn't an instance of a Post model, return
970
                                                                if ( ! $post instanceof Post ) {
12✔
971
                                                                        return null;
3✔
972
                                                                }
973

974
                                                                if ( ! isset( $post->post_type ) || ! in_array(
10✔
975
                                                                        $post->post_type,
10✔
976
                                                                        [
10✔
977
                                                                                'revision',
10✔
978
                                                                                $post_type_object->name,
10✔
979
                                                                        ],
10✔
980
                                                                        true
10✔
981
                                                                ) ) {
10✔
982
                                                                        return null;
1✔
983
                                                                }
984

985
                                                                return $post;
9✔
986
                                                        }
12✔
987
                                                );
12✔
988
                                        },
594✔
989
                                ]
594✔
990
                        );
594✔
991
                }
992
        }
993

994
        /**
995
         * Register RootQuery fields for Term Objects of supported taxonomies
996
         *
997
         * @return void
998
         */
999
        public static function register_term_object_fields() {
601✔
1000
                $allowed_taxonomies = \WPGraphQL::get_allowed_taxonomies( 'objects', [ 'graphql_register_root_field' => true ] );
593✔
1001

1002
                foreach ( $allowed_taxonomies as $tax_object ) {
593✔
1003
                        register_graphql_field(
601✔
1004
                                'RootQuery',
601✔
1005
                                $tax_object->graphql_single_name,
601✔
1006
                                [
601✔
1007
                                        'type'        => $tax_object->graphql_single_name,
601✔
1008
                                        'description' => static function () use ( $tax_object ) {
601✔
1009
                                                return sprintf(
69✔
1010
                                                        // translators: %s is the taxonomys' GraphQL name.
1011
                                                        __( 'A % object', 'wp-graphql' ),
69✔
1012
                                                        $tax_object->graphql_single_name
69✔
1013
                                                );
69✔
1014
                                        },
601✔
1015
                                        'args'        => [
601✔
1016
                                                'id'     => [
601✔
1017
                                                        'type'        => [
601✔
1018
                                                                'non_null' => 'ID',
601✔
1019
                                                        ],
601✔
1020
                                                        'description' => static function () {
601✔
1021
                                                                return __( 'The globally unique identifier of the object.', 'wp-graphql' );
69✔
1022
                                                        },
601✔
1023
                                                ],
601✔
1024
                                                'idType' => [
601✔
1025
                                                        'type'        => $tax_object->graphql_single_name . 'IdType',
601✔
1026
                                                        'description' => static function () {
601✔
1027
                                                                return __( 'Type of unique identifier to fetch by. Default is Global ID', 'wp-graphql' );
69✔
1028
                                                        },
601✔
1029
                                                ],
601✔
1030
                                        ],
601✔
1031
                                        'resolve'     => static function ( $_source, array $args, $context ) use ( $tax_object ) {
601✔
1032
                                                $idType  = isset( $args['idType'] ) ? $args['idType'] : 'global_id';
54✔
1033
                                                $term_id = null;
54✔
1034

1035
                                                switch ( $idType ) {
1036
                                                        case 'slug':
54✔
1037
                                                        case 'name':
52✔
1038
                                                        case 'database_id':
50✔
1039
                                                                if ( 'database_id' === $idType ) {
42✔
1040
                                                                        $idType = 'id';
38✔
1041
                                                                }
1042
                                                                $term    = get_term_by( $idType, $args['id'], $tax_object->name );
42✔
1043
                                                                $term_id = isset( $term->term_id ) ? absint( $term->term_id ) : null;
42✔
1044
                                                                break;
42✔
1045
                                                        case 'uri':
12✔
1046
                                                                return $context->node_resolver->resolve_uri(
5✔
1047
                                                                        $args['id'],
5✔
1048
                                                                        [
5✔
1049
                                                                                'nodeType' => 'TermNode',
5✔
1050
                                                                                'taxonomy' => $tax_object->name,
5✔
1051
                                                                        ]
5✔
1052
                                                                );
5✔
1053
                                                        case 'global_id':
7✔
1054
                                                        default:
1055
                                                                $id_components = Relay::fromGlobalId( $args['id'] );
7✔
1056
                                                                if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
7✔
1057
                                                                        throw new UserError( esc_html__( 'The ID input is invalid', 'wp-graphql' ) );
1✔
1058
                                                                }
1059
                                                                $term_id = absint( $id_components['id'] );
6✔
1060
                                                                break;
6✔
1061
                                                }
1062

1063
                                                return ! empty( $term_id ) ? $context->get_loader( 'term' )->load_deferred( (int) $term_id ) : null;
48✔
1064
                                        },
601✔
1065
                                ]
601✔
1066
                        );
601✔
1067
                }
1068
        }
1069
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc