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

wp-graphql / wp-graphql / 18790791685

24 Oct 2025 08:03PM UTC coverage: 83.207% (-1.4%) from 84.575%
18790791685

push

github

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

2 of 4 new or added lines in 2 files covered. (50.0%)

189 existing lines in 10 files now uncovered.

16143 of 19401 relevant lines covered (83.21%)

257.79 hits per line

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

68.42
/src/AppContext.php
1
<?php
2

3
namespace WPGraphQL;
4

5
use GraphQL\Error\UserError;
6
use WPGraphQL\Data\Loader\CommentAuthorLoader;
7
use WPGraphQL\Data\Loader\CommentLoader;
8
use WPGraphQL\Data\Loader\EnqueuedScriptLoader;
9
use WPGraphQL\Data\Loader\EnqueuedStylesheetLoader;
10
use WPGraphQL\Data\Loader\PluginLoader;
11
use WPGraphQL\Data\Loader\PostObjectLoader;
12
use WPGraphQL\Data\Loader\PostTypeLoader;
13
use WPGraphQL\Data\Loader\TaxonomyLoader;
14
use WPGraphQL\Data\Loader\TermObjectLoader;
15
use WPGraphQL\Data\Loader\ThemeLoader;
16
use WPGraphQL\Data\Loader\UserLoader;
17
use WPGraphQL\Data\Loader\UserRoleLoader;
18
use WPGraphQL\Data\NodeResolver;
19

20
/**
21
 * Class AppContext
22
 * Creates an object that contains all of the context for the GraphQL query
23
 * This class gets instantiated and populated in the main WPGraphQL class.
24
 *
25
 * The context is passed to each resolver during execution.
26
 *
27
 * Resolvers have the ability to read and write to context to pass info to nested resolvers.
28
 *
29
 * @package WPGraphQL
30
 */
31
#[\AllowDynamicProperties]
32
class AppContext {
33
        /**
34
         * The default loaders for the AppContext.
35
         */
36
        private const DEFAULT_LOADERS = [
37
                'comment_author'      => CommentAuthorLoader::class,
38
                'comment'             => CommentLoader::class,
39
                'enqueued_script'     => EnqueuedScriptLoader::class,
40
                'enqueued_stylesheet' => EnqueuedStylesheetLoader::class,
41
                'plugin'              => PluginLoader::class,
42
                'nav_menu_item'       => PostObjectLoader::class,
43
                'post'                => PostObjectLoader::class,
44
                'post_type'           => PostTypeLoader::class,
45
                'taxonomy'            => TaxonomyLoader::class,
46
                'term'                => TermObjectLoader::class,
47
                'theme'               => ThemeLoader::class,
48
                'user'                => UserLoader::class,
49
                'user_role'           => UserRoleLoader::class,
50
        ];
51

52
        /**
53
         * Stores the class to use for the connection query.
54
         *
55
         * @var \WP_Query|null
56
         */
57
        public $connection_query_class = null;
58

59
        /**
60
         * Stores the url string for the current site
61
         *
62
         * @var string $root_url
63
         */
64
        public $root_url;
65

66
        /**
67
         * Stores the WP_User object of the current user
68
         *
69
         * @var \WP_User $viewer
70
         */
71
        public $viewer;
72

73
        /**
74
         * @var \WPGraphQL\Registry\TypeRegistry
75
         */
76
        public $type_registry;
77

78
        /**
79
         * Stores everything from the $_REQUEST global
80
         *
81
         * @var mixed $request
82
         */
83
        public $request;
84

85
        /**
86
         * Stores additional $config properties
87
         *
88
         * @var mixed $config
89
         */
90
        public $config;
91

92
        /**
93
         * Passes context about the current connection being resolved
94
         *
95
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
96
         *
97
         * @var mixed|string|null
98
         */
99
        public $currentConnection = null;
100

101
        /**
102
         * Passes context about the current connection
103
         *
104
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
105
         *
106
         * @var array<string,mixed>
107
         */
108
        public $connectionArgs = [];
109

110
        /**
111
         * Stores the loaders for the class
112
         *
113
         * @var array<string,\WPGraphQL\Data\Loader\AbstractDataLoader>
114
         *
115
         * phpcs:disable SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation, -- For phpstan type hinting
116
         *
117
         * @template T of key-of<self::DEFAULT_LOADERS>
118
         *
119
         * @phpstan-var array<T, new<self::DEFAULT_LOADERS[T]>>|array<string,\WPGraphQL\Data\Loader\AbstractDataLoader>
120
         *
121
         * phpcs:enable
122
         */
123
        public $loaders = [];
124

125
        /**
126
         * Instance of the NodeResolver class to resolve nodes by URI
127
         *
128
         * @var \WPGraphQL\Data\NodeResolver
129
         */
130
        public $node_resolver;
131

132
        /**
133
         * The loader classes, before they are instantiated.
134
         *
135
         * @var array<string,class-string<\WPGraphQL\Data\Loader\AbstractDataLoader>>
136
         */
137
        private $loader_classes = self::DEFAULT_LOADERS;
138

139
        /**
140
         * Stores custom data with namespace isolation.
141
         *
142
         * This is a key-value store where data is organized by namespace to prevent collisions
143
         * between different plugins/extensions.
144
         *
145
         * INTENDED USE: Store temporary, request-scoped state that needs to be passed between
146
         * different phases of GraphQL execution (e.g., directive hooks, middleware, resolver chains).
147
         *
148
         * NOT INTENDED: Storing permanent configuration or replacing existing AppContext properties.
149
         * For configuration, use the 'graphql_app_context_config' filter instead.
150
         *
151
         * @var array<string,array<string,mixed>>
152
         */
153
        private $store = [];
154

155
        /**
156
         * AppContext constructor.
157
         */
158
        public function __construct() {
817✔
159

160
                // Prime the loader classes (and their instances) for the AppContext.
161
                $this->prepare_data_loaders();
817✔
162

163
                /**
164
                 * This sets up the NodeResolver to allow nodes to be resolved by URI
165
                 */
166
                $this->node_resolver = new NodeResolver( $this );
817✔
167

168
                /**
169
                 * This filters the config for the AppContext.
170
                 *
171
                 * This can be used to store additional context config, which is available to resolvers
172
                 * throughout the resolution of a GraphQL request.
173
                 *
174
                 * @param mixed[] $config The config array of the AppContext object
175
                 */
176
                $this->config = apply_filters( 'graphql_app_context_config', $this->config );
817✔
177
        }
178

179
        /**
180
         * Prepares the data loaders for the AppContext.
181
         *
182
         * This method instantiates the loader classes and prepares them for use in the AppContext.
183
         * It also applies filters to allow customization of the loader classes.
184
         *
185
         * @uses graphql_data_loader_classes filter.
186
         * @uses graphql_data_loaders filter (deprecated).
187
         */
188
        private function prepare_data_loaders(): void {
817✔
189
                /**
190
                 * Filter to change the data loader classes.
191
                 *
192
                 * This allows for additional loaders to be added to the AppContext or replaced as needed.
193
                 *
194
                 * @param array<string,class-string<\WPGraphQL\Data\Loader\AbstractDataLoader>> $loader_classes The loader classes accessible in the AppContext
195
                 * @param \WPGraphQL\AppContext                                                $context        The AppContext
196
                 */
197
                $this->loader_classes = apply_filters( 'graphql_data_loader_classes', $this->loader_classes, $this );
817✔
198

199
                /**
200
                 * Prime the loaders if needed
201
                 *
202
                 * @todo Remove this when the loaders are instantiated on demand.
203
                 */
204
                if ( has_filter( 'graphql_data_loaders' ) ) {
817✔
205
                        $loaders = array_map(
×
206
                                function ( $loader_class ) {
×
207
                                        return new $loader_class( $this );
×
208
                                },
×
209
                                $this->loader_classes
×
UNCOV
210
                        );
×
211

212
                        /**
213
                         * @deprecated next-version in favor of graphql_data_loader_classes.
214
                         * @todo Remove in a future version.
215
                         *
216
                         * @param array<string,\WPGraphQL\Data\Loader\AbstractDataLoader> $loaders The loaders accessible in the AppContext
217
                         * @param \WPGraphQL\AppContext                                   $context The AppContext
218
                         */
UNCOV
219
                        $this->loaders = apply_filters_deprecated(
×
UNCOV
220
                                'graphql_data_loaders',
×
UNCOV
221
                                [ $loaders, $this ],
×
UNCOV
222
                                '2.3.2',
×
UNCOV
223
                                'graphql_data_loader_classes',
×
UNCOV
224
                                esc_html__( 'The graphql_data_loaders filter is deprecated and will be removed in a future version. Instead, use the graphql_data_loader_classes filter to add/change data loader classes before they are instantiated.', 'wp-graphql' ),
×
UNCOV
225
                        );
×
226
                }
227
        }
228

229
        /**
230
         * Retrieves loader assigned to $key
231
         *
232
         * @param string $key The name of the loader to get
233
         *
234
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
235
         *
236
         * @deprecated Use get_loader instead.
237
         */
238
        public function getLoader( $key ) {
1✔
239
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_loader()' );
1✔
240
                return $this->get_loader( $key );
1✔
241
        }
242

243
        /**
244
         * Retrieves loader assigned to $key
245
         *
246
         * @template T of key-of<self::DEFAULT_LOADERS>
247
         *
248
         * @param T|string $key The name of the loader to get.
249
         *
250
         * @return \WPGraphQL\Data\Loader\AbstractDataLoader
251
         * @throws \GraphQL\Error\UserError If the loader is not found.
252
         *
253
         * @phpstan-return ( $key is T ? new<self::DEFAULT_LOADERS[T]> : \WPGraphQL\Data\Loader\AbstractDataLoader )
254
         */
255
        public function get_loader( $key ) {
579✔
256
                // @todo: Remove the isset() when `graphql_data_loaders` is removed.
257
                if ( ! array_key_exists( $key, $this->loader_classes ) && ! isset( $this->loaders[ $key ] ) ) {
579✔
258
                        // translators: %s is the key of the loader that was not found.
259
                        throw new UserError( esc_html( sprintf( __( 'No loader assigned to the key %s', 'wp-graphql' ), $key ) ) );
1✔
260
                }
261

262
                // If the loader is not instantiated, instantiate it.
263
                if ( ! isset( $this->loaders[ $key ] ) ) {
578✔
264
                        try {
265
                                $this->loaders[ $key ] = new $this->loader_classes[ $key ]( $this );
578✔
UNCOV
266
                        } catch ( \Throwable $e ) {
×
267
                                // translators: %s is the key of the loader that failed to instantiate.
UNCOV
268
                                throw new UserError( esc_html( sprintf( __( 'Failed to instantiate %1$s: %2$s', 'wp-graphql' ), $this->loader_classes[ $key ], $e->getMessage() ) ) );
×
269
                        }
270
                }
271

272
                /** @var \WPGraphQL\Data\Loader\AbstractDataLoader $loader */
273
                $loader = $this->loaders[ $key ];
578✔
274
                return $loader;
578✔
275
        }
276

277
        /**
278
         * Magic getter used to warn about accessing the loaders property directly.
279
         *
280
         * @todo Remove this when we change the property visibility.
281
         *
282
         * @param string $key The name of the property to get.
283
         * @return mixed
284
         */
UNCOV
285
        public function __get( $key ) {
×
286
                // Use default handling if the key is not a loader.
UNCOV
287
                if ( 'loaders' !== $key ) {
×
288
                        return $this->$key;
×
289
                }
290

291
                // Warn about accessing the loaders property directly.
UNCOV
292
                _doing_it_wrong(
×
UNCOV
293
                        __METHOD__,
×
UNCOV
294
                        esc_html__( 'Accessing the AppContext::$loaders property from outside the AppContext class is deprecated and will throw an error in a future version. Use AppContext::get_loader() instead.', 'wp-graphql' ), //phpcs:ignore PHPCS.Functions.VersionParameter.InvalidVersion -- @todo Fix this smell.
×
UNCOV
295
                        '2.3.2' // phpcs:ignore PHPCS.Functions.VersionParameter.OldVersionPlaceholder -- @todo Fix this smell.
×
UNCOV
296
                );
×
297

298
                // Return the actual loaders array.
UNCOV
299
                return $this->loaders;
×
300
        }
301

302
        /**
303
         * Returns the $args for the connection the field is a part of
304
         *
305
         * @deprecated use get_connection_args() instead
306
         * @return mixed[]|mixed
307
         */
308
        public function getConnectionArgs() {
1✔
309
                _deprecated_function( __METHOD__, '0.8.4', self::class . '::get_connection_args()' );
1✔
310
                return $this->get_connection_args();
1✔
311
        }
312

313
        /**
314
         * Returns the $args for the connection the field is a part of
315
         *
316
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
317
         *
318
         * @return mixed[]|mixed
319
         */
320
        public function get_connection_args() {
2✔
321
                return isset( $this->currentConnection ) && isset( $this->connectionArgs[ $this->currentConnection ] ) ? $this->connectionArgs[ $this->currentConnection ] : [];
2✔
322
        }
323

324
        /**
325
         * Returns the current connection
326
         *
327
         * @todo These properties and methods are unused. We should consider deprecating/removing them.
328
         *
329
         * @return mixed|string|null
330
         */
331
        public function get_current_connection() {
2✔
332
                return isset( $this->currentConnection ) ? $this->currentConnection : null;
2✔
333
        }
334

335
        /**
336
         * @return mixed|string|null
337
         * @deprecated use get_current_connection instead.
338
         */
339
        public function getCurrentConnection() {
1✔
340
                return $this->get_current_connection();
1✔
341
        }
342

343
        /**
344
         * Magic setter to warn about setting dynamic properties on AppContext.
345
         *
346
         * This maintains backward compatibility while warning developers to use the new set() method.
347
         *
348
         * @param string $name  The name of the property being set.
349
         * @param mixed  $value The value being assigned to the property.
350
         * @return void
351
         */
352
        public function __set( $name, $value ) {
3✔
353
                // Only warn for truly dynamic properties, not existing defined properties
354
                if ( ! property_exists( $this, $name ) ) {
3✔
355
                        _doing_it_wrong(
3✔
356
                                __METHOD__,
3✔
357
                                sprintf(
3✔
358
                                        // translators: %s is the name of the property being set.
359
                                        esc_html__( 'Setting dynamic properties on AppContext is deprecated. Use AppContext::set() instead. Attempted to set property: %s', 'wp-graphql' ),
3✔
360
                                        esc_html( $name )
3✔
361
                                ),
3✔
362
                                '@since 2.3.8'
3✔
363
                        );
3✔
364
                }
365

366
                // Still set the property for backward compatibility
367
                $this->$name = $value;
3✔
368
        }
369

370
        /**
371
         * Sets a value in the context store with namespace isolation.
372
         *
373
         * It's strongly recommended to use a unique namespace to avoid collisions with other plugins.
374
         * A good practice is to use your plugin's text domain or a similar unique identifier.
375
         *
376
         * Example:
377
         * ```php
378
         * $context->set( 'my-plugin', 'user-language', 'fr' );
379
         * $context->set( 'my-plugin', 'original-locale', get_locale() );
380
         * ```
381
         *
382
         * @param string $namespace The namespace to store the value under (e.g., 'my-plugin').
383
         * @param string $key       The key to store the value under within the namespace.
384
         * @param mixed  $value     The value to store.
385
         * @since 2.3.8
386
         */
387
        public function set( string $namespace, string $key, $value ): void { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- Namespace is semantically appropriate.
8✔
388
                if ( ! isset( $this->store[ $namespace ] ) ) {
8✔
389
                        $this->store[ $namespace ] = [];
8✔
390
                }
391

392
                $this->store[ $namespace ][ $key ] = $value;
8✔
393
        }
394

395
        /**
396
         * Gets a value from the context store.
397
         *
398
         * Example:
399
         * ```php
400
         * $language = $context->get( 'my-plugin', 'user-language', 'en' );
401
         * $locale = $context->get( 'my-plugin', 'original-locale' );
402
         * ```
403
         *
404
         * @param string $namespace The namespace to retrieve the value from.
405
         * @param string $key       The key to retrieve within the namespace.
406
         * @param mixed  $default   Optional. The default value to return if the key doesn't exist. Default null.
407
         * @return mixed The value if it exists, otherwise the default value.
408
         * @since 2.3.8
409
         */
410
        public function get( string $namespace, string $key, $default = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound, Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound -- Semantically appropriate.
5✔
411
                return $this->store[ $namespace ][ $key ] ?? $default;
5✔
412
        }
413

414
        /**
415
         * Checks if a key exists in the context store.
416
         *
417
         * Example:
418
         * ```php
419
         * if ( $context->has( 'my-plugin', 'user-language' ) ) {
420
         *     $language = $context->get( 'my-plugin', 'user-language' );
421
         * }
422
         * ```
423
         *
424
         * @param string $namespace The namespace to check.
425
         * @param string $key       The key to check within the namespace.
426
         * @return bool True if the key exists, false otherwise.
427
         * @since 2.3.8
428
         */
429
        public function has( string $namespace, string $key ): bool { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- Namespace is semantically appropriate.
4✔
430
                return isset( $this->store[ $namespace ] ) && array_key_exists( $key, $this->store[ $namespace ] );
4✔
431
        }
432

433
        /**
434
         * Removes a specific key from the context store.
435
         *
436
         * Example:
437
         * ```php
438
         * $context->remove( 'my-plugin', 'temporary-data' );
439
         * ```
440
         *
441
         * @param string $namespace The namespace containing the key.
442
         * @param string $key       The key to remove.
443
         * @since 2.3.8
444
         */
445
        public function remove( string $namespace, string $key ): void { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- Namespace is semantically appropriate.
3✔
446
                if ( isset( $this->store[ $namespace ] ) ) {
3✔
447
                        unset( $this->store[ $namespace ][ $key ] );
3✔
448
                }
449
        }
450

451
        /**
452
         * Clears all data in a specific namespace.
453
         *
454
         * This removes all keys associated with the given namespace.
455
         *
456
         * Example:
457
         * ```php
458
         * // Clear all data for 'my-plugin' namespace
459
         * $context->clear( 'my-plugin' );
460
         * ```
461
         *
462
         * @param string $namespace The namespace to clear.
463
         * @since 2.3.8
464
         */
465
        public function clear( string $namespace ): void { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- Namespace is semantically appropriate.
3✔
466
                unset( $this->store[ $namespace ] );
3✔
467
        }
468

469
        /**
470
         * Gets all data stored in a specific namespace.
471
         *
472
         * Returns an associative array of all key-value pairs in the namespace.
473
         *
474
         * Example:
475
         * ```php
476
         * $all_data = $context->all( 'my-plugin' );
477
         * foreach ( $all_data as $key => $value ) {
478
         *     // Process each key-value pair
479
         * }
480
         * ```
481
         *
482
         * @param string $namespace The namespace to retrieve data from.
483
         * @return array<string,mixed> An array of all key-value pairs in the namespace, or empty array if namespace doesn't exist.
484
         * @since 2.3.8
485
         */
486
        public function all( string $namespace ): array { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.namespaceFound -- Namespace is semantically appropriate.
1✔
487
                return $this->store[ $namespace ] ?? [];
1✔
488
        }
489
}
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