GetCoupon.php
1 month ago
GetProduct.php
1 month ago
ListCoupons.php
1 month ago
ListProducts.php
1 month ago
GetProduct.php
84 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | // THIS FILE IS AUTO-GENERATED. DO NOT EDIT MANUALLY. |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLQueries; |
| 7 | |
| 8 | use Automattic\WooCommerce\Api\Queries\Products\GetProduct as GetProductCommand; |
| 9 | use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor; |
| 10 | use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers; |
| 11 | use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Interfaces\Product as ProductInterface; |
| 12 | use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo; |
| 13 | use Automattic\WooCommerce\Api\Infrastructure\Schema\Type; |
| 14 | |
| 15 | class GetProduct { |
| 16 | public static function get_field_definition(): array { |
| 17 | return array( |
| 18 | 'type' => ProductInterface::get(), |
| 19 | 'description' => __( 'Retrieve a single product by ID.', 'woocommerce' ), |
| 20 | 'authorization' => array( |
| 21 | array( |
| 22 | 'attribute' => 'RequiredCapability', |
| 23 | 'args' => array( |
| 24 | 0 => 'read_product', |
| 25 | ), |
| 26 | ), |
| 27 | ), |
| 28 | 'args' => array( |
| 29 | 'id' => array( |
| 30 | 'type' => Type::nonNull( Type::int() ), |
| 31 | 'description' => __( 'The ID of the product to retrieve.', 'woocommerce' ), |
| 32 | ), |
| 33 | ), |
| 34 | 'resolve' => array( self::class, 'resolve' ), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed { |
| 39 | // Publish the root query's metadata so downstream field-level |
| 40 | // authorization gates can read it via `$_metadata['query']`. |
| 41 | // $context is an ArrayObject (see GraphQLController::process_request()) |
| 42 | // so the mutation propagates to nested resolvers. |
| 43 | $context['_query_metadata'] = array(); |
| 44 | |
| 45 | $command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( GetProductCommand::class ); |
| 46 | |
| 47 | $query_info = QueryInfoExtractor::extract_from_info( $info, $args ); |
| 48 | $execute_args = array(); |
| 49 | if ( array_key_exists( 'id', $args ) ) { |
| 50 | $execute_args['id'] = $args['id']; |
| 51 | } |
| 52 | $execute_args['_query_info'] = $query_info; |
| 53 | |
| 54 | if ( ! ResolverHelpers::authorize_command( |
| 55 | $command, |
| 56 | array( |
| 57 | 'id' => $execute_args['id'], |
| 58 | '_preauthorized' => self::compute_preauthorized( $context['principal'] ), |
| 59 | ) |
| 60 | ) ) { |
| 61 | throw ResolverHelpers::build_authorization_error( $context['principal'] ); |
| 62 | } |
| 63 | |
| 64 | $result = ResolverHelpers::execute_command( $command, $execute_args ); |
| 65 | |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Compute the value `_preauthorized` would carry for a given principal — |
| 71 | * the AND of the autodiscovered authorization attributes' authorize() |
| 72 | * outcomes on this command. Single source of truth for both the resolver's |
| 73 | * own gates and external (code-API) callers asking about authorization |
| 74 | * without going through GraphQL execution. |
| 75 | * |
| 76 | * Returns true vacuously when the command has no authorization attributes |
| 77 | * (in that case authorize() on the command is the sole guard, and that |
| 78 | * method should be consulted instead). |
| 79 | */ |
| 80 | public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool { |
| 81 | return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'read_product' ) )->authorize( $principal ); |
| 82 | } |
| 83 | } |
| 84 |