CreateCoupon.php
1 month ago
CreateProduct.php
1 month ago
DeleteCoupon.php
1 month ago
DeleteProduct.php
1 month ago
UpdateCoupon.php
1 month ago
UpdateProduct.php
1 month ago
UpdateCoupon.php
166 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\GraphQLMutations; |
| 7 | |
| 8 | use Automattic\WooCommerce\Api\Mutations\Coupons\UpdateCoupon as UpdateCouponCommand; |
| 9 | use Automattic\WooCommerce\Api\Infrastructure\QueryInfoExtractor; |
| 10 | use Automattic\WooCommerce\Api\Infrastructure\ResolverHelpers; |
| 11 | use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Output\Coupon as CouponType; |
| 12 | use Automattic\WooCommerce\Internal\Api\Autogenerated\GraphQLTypes\Input\UpdateCoupon as UpdateCouponInput; |
| 13 | use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo; |
| 14 | use Automattic\WooCommerce\Api\Infrastructure\Schema\Type; |
| 15 | |
| 16 | class UpdateCoupon { |
| 17 | public static function get_field_definition(): array { |
| 18 | return array( |
| 19 | 'type' => Type::nonNull( CouponType::get() ), |
| 20 | 'description' => __( 'Update an existing coupon.', 'woocommerce' ), |
| 21 | 'authorization' => array( |
| 22 | array( |
| 23 | 'attribute' => 'RequiredCapability', |
| 24 | 'args' => array( |
| 25 | 0 => 'manage_woocommerce', |
| 26 | ), |
| 27 | ), |
| 28 | ), |
| 29 | 'args' => array( |
| 30 | 'input' => array( |
| 31 | 'type' => Type::nonNull( UpdateCouponInput::get() ), |
| 32 | 'description' => __( 'The fields to update.', 'woocommerce' ), |
| 33 | ), |
| 34 | ), |
| 35 | 'resolve' => array( self::class, 'resolve' ), |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | public static function resolve( mixed $root, array $args, mixed $context, ResolveInfo $info ): mixed { |
| 40 | // Standalone authorization gate: no authorize() method on the command, |
| 41 | // so the autodiscovered authorization attributes are the sole guard. |
| 42 | if ( ! self::compute_preauthorized( $context['principal'] ) ) { |
| 43 | throw ResolverHelpers::build_authorization_error( $context['principal'] ); |
| 44 | } |
| 45 | |
| 46 | // Publish the root query's metadata so downstream field-level |
| 47 | // authorization gates can read it via `$_metadata['query']`. |
| 48 | // $context is an ArrayObject (see GraphQLController::process_request()) |
| 49 | // so the mutation propagates to nested resolvers. |
| 50 | $context['_query_metadata'] = array(); |
| 51 | |
| 52 | $command = \Automattic\WooCommerce\Api\Infrastructure\ClassResolver::resolve_class( UpdateCouponCommand::class ); |
| 53 | |
| 54 | $execute_args = array(); |
| 55 | if ( array_key_exists( 'input', $args ) ) { |
| 56 | $execute_args['input'] = self::convert_update_coupon_input( $args['input'] ); |
| 57 | } |
| 58 | |
| 59 | $result = ResolverHelpers::execute_command( $command, $execute_args ); |
| 60 | |
| 61 | return $result; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Compute the value `_preauthorized` would carry for a given principal — |
| 66 | * the AND of the autodiscovered authorization attributes' authorize() |
| 67 | * outcomes on this command. Single source of truth for both the resolver's |
| 68 | * own gates and external (code-API) callers asking about authorization |
| 69 | * without going through GraphQL execution. |
| 70 | * |
| 71 | * Returns true vacuously when the command has no authorization attributes |
| 72 | * (in that case authorize() on the command is the sole guard, and that |
| 73 | * method should be consulted instead). |
| 74 | */ |
| 75 | public static function compute_preauthorized( \Automattic\WooCommerce\Api\Infrastructure\Principal $principal ): bool { |
| 76 | return ( new \Automattic\WooCommerce\Api\Attributes\RequiredCapability( 'manage_woocommerce' ) )->authorize( $principal ); |
| 77 | } |
| 78 | |
| 79 | private static function convert_update_coupon_input( array $data ): \Automattic\WooCommerce\Api\InputTypes\Coupons\UpdateCouponInput { |
| 80 | $input = new \Automattic\WooCommerce\Api\InputTypes\Coupons\UpdateCouponInput(); |
| 81 | |
| 82 | if ( array_key_exists( 'id', $data ) ) { |
| 83 | $input->mark_provided( 'id' ); |
| 84 | $input->id = $data['id']; |
| 85 | } |
| 86 | if ( array_key_exists( 'code', $data ) ) { |
| 87 | $input->mark_provided( 'code' ); |
| 88 | $input->code = $data['code']; |
| 89 | } |
| 90 | if ( array_key_exists( 'description', $data ) ) { |
| 91 | $input->mark_provided( 'description' ); |
| 92 | $input->description = $data['description']; |
| 93 | } |
| 94 | if ( array_key_exists( 'discount_type', $data ) ) { |
| 95 | $input->mark_provided( 'discount_type' ); |
| 96 | $input->discount_type = $data['discount_type']; |
| 97 | } |
| 98 | if ( array_key_exists( 'amount', $data ) ) { |
| 99 | $input->mark_provided( 'amount' ); |
| 100 | $input->amount = $data['amount']; |
| 101 | } |
| 102 | if ( array_key_exists( 'status', $data ) ) { |
| 103 | $input->mark_provided( 'status' ); |
| 104 | $input->status = $data['status']; |
| 105 | } |
| 106 | if ( array_key_exists( 'date_expires', $data ) ) { |
| 107 | $input->mark_provided( 'date_expires' ); |
| 108 | $input->date_expires = $data['date_expires']; |
| 109 | } |
| 110 | if ( array_key_exists( 'individual_use', $data ) ) { |
| 111 | $input->mark_provided( 'individual_use' ); |
| 112 | $input->individual_use = $data['individual_use']; |
| 113 | } |
| 114 | if ( array_key_exists( 'product_ids', $data ) ) { |
| 115 | $input->mark_provided( 'product_ids' ); |
| 116 | $input->product_ids = $data['product_ids']; |
| 117 | } |
| 118 | if ( array_key_exists( 'excluded_product_ids', $data ) ) { |
| 119 | $input->mark_provided( 'excluded_product_ids' ); |
| 120 | $input->excluded_product_ids = $data['excluded_product_ids']; |
| 121 | } |
| 122 | if ( array_key_exists( 'usage_limit', $data ) ) { |
| 123 | $input->mark_provided( 'usage_limit' ); |
| 124 | $input->usage_limit = $data['usage_limit']; |
| 125 | } |
| 126 | if ( array_key_exists( 'usage_limit_per_user', $data ) ) { |
| 127 | $input->mark_provided( 'usage_limit_per_user' ); |
| 128 | $input->usage_limit_per_user = $data['usage_limit_per_user']; |
| 129 | } |
| 130 | if ( array_key_exists( 'limit_usage_to_x_items', $data ) ) { |
| 131 | $input->mark_provided( 'limit_usage_to_x_items' ); |
| 132 | $input->limit_usage_to_x_items = $data['limit_usage_to_x_items']; |
| 133 | } |
| 134 | if ( array_key_exists( 'free_shipping', $data ) ) { |
| 135 | $input->mark_provided( 'free_shipping' ); |
| 136 | $input->free_shipping = $data['free_shipping']; |
| 137 | } |
| 138 | if ( array_key_exists( 'product_categories', $data ) ) { |
| 139 | $input->mark_provided( 'product_categories' ); |
| 140 | $input->product_categories = $data['product_categories']; |
| 141 | } |
| 142 | if ( array_key_exists( 'excluded_product_categories', $data ) ) { |
| 143 | $input->mark_provided( 'excluded_product_categories' ); |
| 144 | $input->excluded_product_categories = $data['excluded_product_categories']; |
| 145 | } |
| 146 | if ( array_key_exists( 'exclude_sale_items', $data ) ) { |
| 147 | $input->mark_provided( 'exclude_sale_items' ); |
| 148 | $input->exclude_sale_items = $data['exclude_sale_items']; |
| 149 | } |
| 150 | if ( array_key_exists( 'minimum_amount', $data ) ) { |
| 151 | $input->mark_provided( 'minimum_amount' ); |
| 152 | $input->minimum_amount = $data['minimum_amount']; |
| 153 | } |
| 154 | if ( array_key_exists( 'maximum_amount', $data ) ) { |
| 155 | $input->mark_provided( 'maximum_amount' ); |
| 156 | $input->maximum_amount = $data['maximum_amount']; |
| 157 | } |
| 158 | if ( array_key_exists( 'email_restrictions', $data ) ) { |
| 159 | $input->mark_provided( 'email_restrictions' ); |
| 160 | $input->email_restrictions = $data['email_restrictions']; |
| 161 | } |
| 162 | |
| 163 | return $input; |
| 164 | } |
| 165 | } |
| 166 |