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

wp-graphql / wp-graphql-woocommerce / 11714183904

07 Nov 2024 12:23AM UTC coverage: 83.665% (-0.8%) from 84.451%
11714183904

push

github

web-flow
devops: Name officially changed to "WPGraphQL for WooCommerce" (#900)

* devops: Name officially changed to "WPGraphQL for WooCommerce"

* devops: GA workflow environment updated

* devops: GA workflow environment updated

* devops: composer-git-hooks downgraded to "2.8.5"

* devops: Unstable session manager tests skipped

* chore: cleanup applied

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

2 of 8 new or added lines in 5 files covered. (25.0%)

268 existing lines in 20 files now uncovered.

12431 of 14858 relevant lines covered (83.67%)

71.79 hits per line

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

84.06
/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(
141✔
32
                        'deleteOrder',
141✔
33
                        [
141✔
34
                                'inputFields'         => self::get_input_fields(),
141✔
35
                                'outputFields'        => self::get_output_fields(),
141✔
36
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
141✔
37
                        ]
141✔
38
                );
141✔
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(
141✔
48
                        [
141✔
49
                                'id'          => [
141✔
50
                                        'type'        => 'ID',
141✔
51
                                        'description' => __( 'Database ID or global ID of the order', 'wp-graphql-woocommerce' ),
141✔
52
                                ],
141✔
53
                                'orderId'     => [
141✔
54
                                        'type'              => 'Int',
141✔
55
                                        'description'       => __( 'Order WP ID', 'wp-graphql-woocommerce' ),
141✔
56
                                        'deprecationReason' => __( 'Use "id" field instead.', 'wp-graphql-woocommerce' ),
141✔
57
                                ],
141✔
58
                                'forceDelete' => [
141✔
59
                                        'type'        => 'Boolean',
141✔
60
                                        'description' => __( 'Delete or simply place in trash.', 'wp-graphql-woocommerce' ),
141✔
61
                                ],
141✔
62
                        ]
141✔
63
                );
141✔
64
        }
65

66
        /**
67
         * Defines the mutation output field configuration
68
         *
69
         * @return array
70
         */
71
        public static function get_output_fields() {
72
                return [
141✔
73
                        'order' => [
141✔
74
                                'type'    => 'Order',
141✔
75
                                'resolve' => static function ( $payload ) {
141✔
76
                                        return $payload['order'];
1✔
77
                                },
141✔
78
                        ],
141✔
79
                ];
141✔
80
        }
81

82
        /**
83
         * Defines the mutation data modification closure.
84
         *
85
         * @return callable
86
         */
87
        public static function mutate_and_get_payload() {
88
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
141✔
89
                        // Retrieve order ID.
90
                        $order_id = false;
1✔
91
                        if ( ! empty( $input['id'] ) ) {
1✔
92
                                $order_id = Utils::get_database_id_from_id( $input['id'] );
1✔
93
                        } elseif ( ! empty( $input['orderId'] ) ) {
×
UNCOV
94
                                $order_id = absint( $input['orderId'] );
×
95
                        } else {
96
                                throw new UserError( __( 'Order ID provided is missing or invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
97
                        }
98

99
                        if ( ! $order_id ) {
1✔
UNCOV
100
                                throw new UserError( __( 'Order ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
101
                        }
102

103
                        // Check if authorized to delete this order.
104
                        if ( ! Order_Mutation::authorized( $input, $context, $info, 'delete', $order_id ) ) {
1✔
105
                                throw new UserError( __( 'User does not have the capabilities necessary to delete an order.', 'wp-graphql-woocommerce' ) );
1✔
106
                        }
107

108
                        $force_delete = false;
1✔
109
                        if ( ! empty( $input['forceDelete'] ) ) {
1✔
110
                                $force_delete = $input['forceDelete'];
1✔
111
                        }
112

113
                        /**
114
                         * Get Order model instance for output.
115
                         *
116
                         * @var \WC_Order $order
117
                         */
118
                        $order = new Order( $order_id );
1✔
119

120
                        // Cache items to prevent null value errors.
121
                        // @codingStandardsIgnoreStart
122
                        $order->get_downloadable_items();
1✔
123
                        $order->get_items();
1✔
124
                        $order->get_items( 'fee' );
1✔
125
                        $order->get_items( 'shipping' );
1✔
126
                        $order->get_items( 'tax' );
1✔
127
                        $order->get_items( 'coupon' );
1✔
128
                        // @codingStandardsIgnoreEnd.
129

130
                        /**
131
                         * Action called before order is deleted.
132
                         *
133
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order   Order model instance.
134
                         * @param array           $input   Input data describing order.
135
                         * @param \WPGraphQL\AppContext      $context Request AppContext instance.
136
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info    Request ResolveInfo instance.
137
                         */
138
                        do_action( 'graphql_woocommerce_before_order_delete', $order, $input, $context, $info );
1✔
139

140
                        // Delete order.
141
                        $order_to_be_deleted = WC_Order_Factory::get_order( $order->get_id() );
1✔
142

143
                        if ( ! is_object( $order_to_be_deleted ) ) {
1✔
UNCOV
144
                                throw new UserError( __( 'Order to be deleted could not be found.', 'wp-graphql-woocommerce' ) );
×
145
                        }
146

147
                        $success = Order_Mutation::purge( $order_to_be_deleted, $force_delete );
1✔
148

149
                        if ( ! $success ) {
1✔
150
                                throw new UserError(
×
UNCOV
151
                                        sprintf(
×
152
                                                /* translators: Deletion failed message */
153
                                                __( 'Removal of Order %d failed', 'wp-graphql-woocommerce' ),
×
154
                                                $order->get_id()
×
155
                                        )
×
UNCOV
156
                                );
×
157
                        }
158

159
                        /**
160
                         * Action called before order is deleted.
161
                         *
162
                         * @param \WC_Order|\WPGraphQL\WooCommerce\Model\Order $order   Order model instance.
163
                         * @param array           $input   Input data describing order
164
                         * @param \WPGraphQL\AppContext      $context Request AppContext instance.
165
                         * @param \GraphQL\Type\Definition\ResolveInfo     $info    Request ResolveInfo instance.
166
                         */
167
                        do_action( 'graphql_woocommerce_after_order_delete', $order, $input, $context, $info );
1✔
168

169
                        return [ 'order' => $order ];
1✔
170
                };
141✔
171
        }
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc