| 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 |
|