Autogenerated
1 month ago
GraphQLEndpointRegistrar.php
1 month ago
OpcacheFileExpiry.php
1 month ago
QueryCache.php
1 month ago
QueryComplexityRule.php
1 month ago
QueryDepthRule.php
1 month ago
Settings.php
1 month ago
StatusResolverFailedException.php
1 month ago
GraphQLEndpointRegistrar.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Api; |
| 6 | |
| 7 | use Automattic\WooCommerce\Api\Infrastructure\Main; |
| 8 | |
| 9 | /** |
| 10 | * Deferred-registration helper for GraphQL endpoints declared via |
| 11 | * {@see Main::register_graphql_endpoint()}. |
| 12 | * |
| 13 | * Each instance captures the arguments of a single registration request and |
| 14 | * exposes {@see self::handle_rest_api_init()} as the callback target for the |
| 15 | * rest_api_init action, so Main doesn't have to carry per-registration state |
| 16 | * through a closure. |
| 17 | */ |
| 18 | class GraphQLEndpointRegistrar { |
| 19 | /** |
| 20 | * Capture the arguments of a single register_graphql_endpoint() call. |
| 21 | * |
| 22 | * @param string $controller_class_name Fully-qualified name of a concrete GraphQLController subclass. |
| 23 | * @param string $route_namespace REST namespace passed to register_rest_route(). |
| 24 | * @param string $route REST route path passed to register_rest_route(). |
| 25 | * @param string[] $methods HTTP methods accepted on the endpoint. |
| 26 | */ |
| 27 | public function __construct( |
| 28 | private readonly string $controller_class_name, |
| 29 | private readonly string $route_namespace, |
| 30 | private readonly string $route, |
| 31 | private readonly array $methods |
| 32 | ) {} |
| 33 | |
| 34 | /** |
| 35 | * Hook callback for rest_api_init. Instantiates the controller and |
| 36 | * registers the REST route. |
| 37 | * |
| 38 | * The caller-declared methods are narrowed by |
| 39 | * {@see Main::filter_methods_against_settings()} so plugin endpoints honour |
| 40 | * the same site-wide settings (e.g. the GET-endpoint toggle) as |
| 41 | * WooCommerce core's `/wc/graphql`. If the filter empties the list the |
| 42 | * endpoint is not registered. |
| 43 | */ |
| 44 | public function handle_rest_api_init(): void { |
| 45 | $methods = Main::filter_methods_against_settings( $this->methods ); |
| 46 | if ( empty( $methods ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $controller = Main::instantiate_graphql_controller( $this->controller_class_name ); |
| 51 | if ( null === $controller ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | register_rest_route( |
| 56 | $this->route_namespace, |
| 57 | $this->route, |
| 58 | array( |
| 59 | 'methods' => $methods, |
| 60 | 'callback' => array( $controller, 'handle_request' ), |
| 61 | // Auth is handled per-query/mutation. |
| 62 | 'permission_callback' => '__return_true', |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | } |
| 67 |