• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 27386231983

12 Jun 2026 12:25AM UTC coverage: 91.791%. Remained the same
27386231983

Pull #1019

github

web-flow
Merge 46a421a18 into 01876f534
Pull Request #1019: fix: address WordPress.org plugin review (rename + prefixing + headers)

1327 of 1584 new or added lines in 200 files covered. (83.78%)

1 existing line in 1 file now uncovered.

18505 of 20160 relevant lines covered (91.79%)

151.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.33
/includes/mutation/class-order-delete.php
1
<?php
2
/**
3
 * Mutation - deleteOrder
4
 *
5
 * Registers mutation for delete an order.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 0.2.0
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WC_Order_Factory;
16
use WPGraphQL\AppContext;
17
use WPGraphQL\Utils\Utils;
18
use WPGraphQL\WooCommerce\Data\Mutation\Order_Mutation;
19
use WPGraphQL\WooCommerce\Model\Order;
20

21
/**
22
 * Class Order_Delete
23
 */
24
class Order_Delete {
25
        /**
26
         * Registers mutation
27
         *
28
         * @return void
29
         */
30
        public static function register_mutation() {
31
                register_graphql_mutation(
296✔
32
                        'deleteOrder',
296✔
33
                        [
296✔
34
                                'inputFields'         => self::get_input_fields(),
296✔
35
                                'outputFields'        => self::get_output_fields(),
296✔
36
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
296✔
37
                        ]
296✔
38
                );
296✔
39
        }
40

41
        /**
42
         * Defines the mutation input field configuration
43
         *
44
         * @return array
45
         */
46
        public static function get_input_fields() {
47
                return array_merge(
296✔
48
                        [
296✔
49
                                'id'          => [
296✔
50
                                        'type'        => 'ID',
296✔
51
                                        'description' => static function () {
296✔
52
                                                return __( 'Database ID or global ID of the order', 'graphql-for-ecommerce' );
2✔
53
                                        },
296✔
54
                                ],
296✔
55
                                'orderId'     => [
296✔
56
                                        'type'              => 'Int',
296✔
57
                                        'description'       => static function () {
296✔
58
                                                return __( 'Order WP ID', 'graphql-for-ecommerce' );
2✔
59
                                        },
296✔
60
                                        'deprecationReason' => __( 'Use "id" field instead.', 'graphql-for-ecommerce' ),
296✔
61
                                ],
296✔
62
                                'forceDelete' => [
296✔
63
                                        'type'        => 'Boolean',
296✔
64
                                        'description' => static function () {
296✔
65
                                                return __( 'Delete or simply place in trash.', 'graphql-for-ecommerce' );
2✔
66
                                        },
296✔
67
                                ],
296✔
68
                        ]
296✔
69
                );
296✔
70
        }
71

72
        /**
73
         * Defines the mutation output field configuration
74
         *
75
         * @return array
76
         */
77
        public static function get_output_fields() {
78
                return [
296✔
79
                        'order' => [
296✔
80
                                'type'    => 'Order',
296✔
81
                                'resolve' => static function ( $payload ) {
296✔
82
                                        return $payload['order'];
1✔
83
                                },
296✔
84
                        ],
296✔
85
                ];
296✔
86
        }
87

88
        /**
89
         * Defines the mutation data modification closure.
90
         *
91
         * @return callable
92
         */
93
        public static function mutate_and_get_payload() {
94
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
296✔
95
                        // Retrieve order ID.
96
                        $order_id = false;
1✔
97
                        if ( ! empty( $input['id'] ) ) {
1✔
98
                                $order_id = Utils::get_database_id_from_id( $input['id'] );
1✔
99
                        } elseif ( ! empty( $input['orderId'] ) ) {
×
100
                                $order_id = absint( $input['orderId'] );
×
101
                        } else {
NEW
102
                                throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
×
103
                        }
104

105
                        if ( ! $order_id ) {
1✔
NEW
106
                                throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
×
107
                        }
108

109
                        // Check if authorized to delete this order.
110
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete', $order_id ) ) {
1✔
111
                                throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'graphql-for-ecommerce' ) );
1✔
112
                        }
113

114
                        $force_delete = false;
1✔
115
                        if ( ! empty( $input['forceDelete'] ) ) {
1✔
116
                                $force_delete = $input['forceDelete'];
1✔
117
                        }
118

119
                        /**
120
                         * Get Order model instance for output.
121
                         *
122
                         * @var \WC_Order $order
123
                         */
124
                        $order = new Order( $order_id );
1✔
125

126
                        // Cache items to prevent null value errors.
127
                        // @codingStandardsIgnoreStart
128
                        $order->get_downloadable_items();
1✔
129
                        $order->get_items();
1✔
130
                        $order->get_items( 'fee' );
1✔
131
                        $order->get_items( 'shipping' );
1✔
132
                        $order->get_items( 'tax' );
1✔
133
                        $order->get_items( 'coupon' );
1✔
134
                        // @codingStandardsIgnoreEnd.
135

136
                        /**
137
                         * Action called before order is deleted.
138
                         *
139
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order   Order model instance.
140
                         * @param array           $input   Input data describing order.
141
                         * @param \WPGraphQL\AppContext      $context Request AppContext instance.
142
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info    Request ResolveInfo instance.
143
                         */
144
                        do_action( 'graphql_woocommerce_before_order_delete', $order, $input, $context, $info );
1✔
145

146
                        // Delete order.
147
                        $order_to_be_deleted = WC_Order_Factory::get_order( $order->get_id() );
1✔
148

149
                        if ( ! is_object( $order_to_be_deleted ) ) {
1✔
NEW
150
                                throw new UserError( __( 'Order to be deleted could not be found.', 'graphql-for-ecommerce' ) );
×
151
                        }
152

153
                        $success = Order_Mutation::purge( $order_to_be_deleted, $force_delete );
1✔
154

155
                        if ( ! $success ) {
1✔
156
                                throw new UserError(
×
157
                                        sprintf(
×
158
                                                /* translators: Deletion failed message */
NEW
159
                                                __( 'Removal of Order %d failed', 'graphql-for-ecommerce' ),
×
160
                                                $order->get_id()
×
161
                                        )
×
162
                                );
×
163
                        }
164

165
                        /**
166
                         * Action called before order is deleted.
167
                         *
168
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order   Order model instance.
169
                         * @param array           $input   Input data describing order
170
                         * @param \WPGraphQL\AppContext      $context Request AppContext instance.
171
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info    Request ResolveInfo instance.
172
                         */
173
                        do_action( 'graphql_woocommerce_after_order_delete', $order, $input, $context, $info );
1✔
174

175
                        return [ 'order' => $order ];
1✔
176
                };
296✔
177
        }
178
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc