PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Experimental / Extensions.php

Extensions.php in WPGraphQL trunk, at src/Experimental/Extensions.php

100 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPGraphQL\Experimental;
4
5 use WPGraphQL\Experimental\ExperimentRegistry;
6
7 /**
8 * Class Extensions
9 *
10 * Handles adding active experiments to GraphQL response extensions.
11 *
12 * @package WPGraphQL\Experimental
13 * @since 2.3.8
14 */
15 class Extensions {
16
17 /**
18 * Initialize the extensions functionality.
19 */
20 public function init(): void {
21 add_filter(
22 'graphql_request_results',
23 [
24 $this,
25 'add_experiments_to_response_extensions',
26 ],
27 10,
28 5
29 );
30 }
31
32 /**
33 * Add active experiments to GraphQL response extensions.
34 *
35 * @param mixed|array<string,mixed>|object $response The response of the GraphQL Request being executed
36 * @param \WPGraphQL\WPSchema $schema The WPGraphQL Schema
37 * @param string|null $operation_name The operation name being executed
38 * @param string|null $request The GraphQL Request being made
39 * @param array<string,mixed>|null $variables The variables sent with the request
40 *
41 * @return mixed|array<string,mixed>|object
42 */
43 public function add_experiments_to_response_extensions( $response, $schema, ?string $operation_name, ?string $request, ?array $variables ) {
44 // Only show experiments in extensions when GraphQL debugging is enabled
45 $should = \WPGraphQL::debug();
46
47 /**
48 * Filter whether experiments should be shown in GraphQL response extensions.
49 *
50 * @param bool $should Whether experiments should be displayed in the Extensions output. Defaults to true if GraphQL debugging is enabled.
51 * @param mixed|array<string,mixed>|object $response The response of the WPGraphQL Request being executed
52 * @param \WPGraphQL\WPSchema $schema The WPGraphQL Schema
53 * @param string|null $operation_name The operation name being executed
54 * @param string|null $request The GraphQL Request being made
55 * @param array<string,mixed>|null $variables The variables sent with the request
56 *
57 * @hookGroup settings
58 * @since 2.3.8
59 */
60 $should_show_experiments_in_extensions = apply_filters(
61 'graphql_should_show_experiments_in_extensions',
62 $should,
63 $response,
64 $schema,
65 $operation_name,
66 $request,
67 $variables
68 );
69
70 // If experiments extensions are disabled, return unmodified response
71 if ( false === $should_show_experiments_in_extensions ) {
72 return $response;
73 }
74
75 // Get active experiments from the registry instance
76 $registry = ExperimentRegistry::get_instance();
77 $active_experiments = $registry ? $registry->get_active_experiments() : [];
78
79 // If no experiments are active, don't add anything to extensions
80 if ( empty( $active_experiments ) ) {
81 return $response;
82 }
83
84 // Extract just the experiment slugs for the extensions
85 $experiment_slugs = array_keys( $active_experiments );
86
87 // Add experiments to response extensions
88 if ( ! empty( $response ) ) {
89 if ( is_array( $response ) ) {
90 $response['extensions']['experiments'] = $experiment_slugs;
91 } elseif ( is_object( $response ) ) {
92 // @phpstan-ignore-next-line
93 $response->extensions['experiments'] = $experiment_slugs;
94 }
95 }
96
97 return $response;
98 }
99 }
100