Files

132 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2021-07-05 13:51:52 -04:00
<?php
/**
* Mutation - deleteCoupon
*
* Registers mutation for deleting an coupon.
*
* @package WPGraphQL\WooCommerce\Mutation
* @since 0.9.0
*/
namespace WPGraphQL\WooCommerce\Mutation;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Utils\Utils;
2021-07-05 13:51:52 -04:00
use WPGraphQL\WooCommerce\Model\Coupon;
/**
* Class Coupon_Delete
*/
class Coupon_Delete {
/**
* Registers mutation
2023-06-13 23:17:02 +03:00
*
* @return void
2021-07-05 13:51:52 -04:00
*/
public static function register_mutation() {
register_graphql_mutation(
'deleteCoupon',
[
2021-07-05 13:51:52 -04:00
'inputFields' => self::get_input_fields(),
'outputFields' => self::get_output_fields(),
'mutateAndGetPayload' => [ self::class, 'mutate_and_get_payload' ],
]
2021-07-05 13:51:52 -04:00
);
}
/**
* Defines the mutation input field configuration
*
* @return array
*/
public static function get_input_fields() {
return [
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => static function () {
return __( 'Unique identifier for the object.', 'graphql-for-ecommerce' );
},
],
'forceDelete' => [
2021-07-05 13:51:52 -04:00
'type' => 'Boolean',
'description' => static function () {
return __( 'Delete the object. Set to "false" by default.', 'graphql-for-ecommerce' );
},
],
];
2021-07-05 13:51:52 -04:00
}
/**
* Defines the mutation output field configuration
*
* @return array
*/
public static function get_output_fields() {
return array_merge(
Coupon_Create::get_output_fields(),
[
'coupon' => [
2021-07-05 13:51:52 -04:00
'type' => 'Coupon',
'resolve' => static function ( $payload ) {
2021-07-05 13:51:52 -04:00
return ! empty( $payload['coupon'] ) ? $payload['coupon'] : null;
},
],
]
2021-07-05 13:51:52 -04:00
);
}
/**
* Defines the mutation data modification closure.
*
* @param array $input Mutation input.
* @param \WPGraphQL\AppContext $context AppContext instance.
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo instance. Can be
2021-07-05 13:51:52 -04:00
* use to get info about the current node in the GraphQL tree.
*
* @throws \GraphQL\Error\UserError Invalid ID provided | Lack of capabilities.
2021-07-05 13:51:52 -04:00
*
2023-06-13 23:17:02 +03:00
* @return array
2021-07-05 13:51:52 -04:00
*/
public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
// Retrieve order ID.
$coupon_id = Utils::get_database_id_from_id( $input['id'] );
if ( empty( $coupon_id ) ) {
throw new UserError( __( 'Coupon ID provided is missing or invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
2021-07-05 13:51:52 -04:00
}
2021-07-05 13:51:52 -04:00
$coupon = new Coupon( $coupon_id );
if ( ! $coupon->ID ) {
throw new UserError( __( 'Invalid ID.', 'graphql-for-ecommerce' ) );
2021-07-05 13:51:52 -04:00
}
if ( ! wc_rest_check_post_permissions( 'shop_coupon', 'delete', $coupon->ID ) ) {
2023-06-13 23:17:02 +03:00
/**
* Get coupon post type.
*
* @var \WP_Post_Type $post_type_object
*/
$post_type_object = get_post_type_object( 'shop_coupon' );
2021-07-05 13:51:52 -04:00
throw new UserError(
sprintf(
/* translators: %s: post type */
__( 'Sorry, you are not allowed to delete %s.', 'graphql-for-ecommerce' ),
2023-06-13 23:17:02 +03:00
lcfirst( $post_type_object->label )
2021-07-05 13:51:52 -04:00
)
);
}
$fields_to_cache = $info->getFieldSelection( 2 );
foreach ( $fields_to_cache['coupon'] as $field => $_ ) {
2023-06-13 23:17:02 +03:00
$cached = $coupon->$field;
2021-07-05 13:51:52 -04:00
}
$force_delete = isset( $input['forceDelete'] ) ? $input['forceDelete'] : false;
$coupon->delete( $force_delete );
return compact( 'coupon' );
}
}