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

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

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

92.31
/src/Mutation/TermObjectDelete.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use GraphQLRelay\Relay;
7
use WPGraphQL\Model\Term;
8
use WPGraphQL\Utils\Utils;
9
use WP_Taxonomy;
10

11
/**
12
 * Class TermObjectDelete
13
 *
14
 * @package WPGraphQL\Mutation
15
 */
16
class TermObjectDelete {
17
        /**
18
         * Registers the TermObjectDelete mutation.
19
         *
20
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
21
         *
22
         * @return void
23
         */
24
        public static function register_mutation( WP_Taxonomy $taxonomy ) {
593✔
25
                $mutation_name = 'delete' . ucfirst( $taxonomy->graphql_single_name );
593✔
26

27
                register_graphql_mutation(
593✔
28
                        $mutation_name,
593✔
29
                        [
593✔
30
                                'inputFields'         => self::get_input_fields( $taxonomy ),
593✔
31
                                'outputFields'        => self::get_output_fields( $taxonomy ),
593✔
32
                                'mutateAndGetPayload' => self::mutate_and_get_payload( $taxonomy, $mutation_name ),
593✔
33
                        ]
593✔
34
                );
593✔
35
        }
36

37
        /**
38
         * Defines the mutation input field configuration.
39
         *
40
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
41
         *
42
         * @return array<string,array<string,mixed>>
43
         */
44
        public static function get_input_fields( WP_Taxonomy $taxonomy ) {
593✔
45
                return [
593✔
46
                        'id' => [
593✔
47
                                'type'        => [
593✔
48
                                        'non_null' => 'ID',
593✔
49
                                ],
593✔
50
                                'description' => static function () use ( $taxonomy ) {
593✔
51
                                        // translators: The placeholder is the name of the taxonomy for the term being deleted
52
                                        return sprintf( __( 'The ID of the %1$s to delete', 'wp-graphql' ), $taxonomy->graphql_single_name );
15✔
53
                                },
593✔
54
                        ],
593✔
55
                ];
593✔
56
        }
57

58
        /**
59
         * Defines the mutation output field configuration.
60
         *
61
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
62
         *
63
         * @return array<string,array<string,mixed>>
64
         */
65
        public static function get_output_fields( WP_Taxonomy $taxonomy ) {
593✔
66
                return [
593✔
67
                        'deletedId'                    => [
593✔
68
                                'type'        => 'ID',
593✔
69
                                'description' => static function () {
593✔
70
                                        return __( 'The ID of the deleted object', 'wp-graphql' );
15✔
71
                                },
593✔
72
                                'resolve'     => static function ( $payload ) {
593✔
73
                                        $deleted = (object) $payload['termObject'];
1✔
74

75
                                        return ! empty( $deleted->term_id ) ? Relay::toGlobalId( 'term', $deleted->term_id ) : null;
1✔
76
                                },
593✔
77
                        ],
593✔
78
                        $taxonomy->graphql_single_name => [
593✔
79
                                'type'        => $taxonomy->graphql_single_name,
593✔
80
                                'description' => static function () {
593✔
81
                                        return __( 'The deleted term object', 'wp-graphql' );
15✔
82
                                },
593✔
83
                                'resolve'     => static function ( $payload ) {
593✔
84
                                        return new Term( $payload['termObject'] );
2✔
85
                                },
593✔
86
                        ],
593✔
87
                ];
593✔
88
        }
89

90
        /**
91
         * Defines the mutation data modification closure.
92
         *
93
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
94
         * @param string       $mutation_name The name of the mutation.
95
         *
96
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
97
         */
98
        public static function mutate_and_get_payload( WP_Taxonomy $taxonomy, string $mutation_name ) {
593✔
99
                return static function ( $input ) use ( $taxonomy ) {
593✔
100
                        // Get the database ID for the comment.
101
                        $term_id = Utils::get_database_id_from_id( $input['id'] );
3✔
102

103
                        if ( empty( $term_id ) ) {
3✔
104
                                // Translators: The placeholder is the name of the taxonomy for the term being deleted
105
                                throw new UserError( esc_html( sprintf( __( 'The ID for the %1$s was not valid', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) );
1✔
106
                        }
107

108
                        /**
109
                         * Get the term before deleting it
110
                         */
111
                        $term_object = get_term( $term_id, $taxonomy->name );
3✔
112

113
                        if ( ! $term_object instanceof \WP_Term ) {
3✔
114
                                throw new UserError( esc_html__( 'The ID passed is invalid', 'wp-graphql' ) );
2✔
115
                        }
116

117
                        /**
118
                         * Ensure the type for the Global ID matches the type being mutated
119
                         */
120
                        if ( $taxonomy->name !== $term_object->taxonomy ) {
2✔
121
                                // Translators: The placeholder is the name of the taxonomy for the term being edited
122
                                throw new UserError( esc_html( sprintf( __( 'The ID passed is not for a %1$s object', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) );
×
123
                        }
124

125
                        /**
126
                         * Ensure the user can delete terms of this taxonomy
127
                         */
128
                        if ( ! current_user_can( 'delete_term', $term_object->term_id ) ) {
2✔
129
                                // Translators: The placeholder is the name of the taxonomy for the term being deleted
130
                                throw new UserError( esc_html( sprintf( __( 'You do not have permission to delete %1$s', 'wp-graphql' ), $taxonomy->graphql_plural_name ) ) );
1✔
131
                        }
132

133
                        /**
134
                         * Delete the term and get the response
135
                         */
136
                        $deleted = wp_delete_term( $term_id, $taxonomy->name );
2✔
137

138
                        /**
139
                         * If there was an error deleting the term, get the error message and return it
140
                         */
141
                        if ( is_wp_error( $deleted ) ) {
2✔
142
                                $error_message = $deleted->get_error_message();
×
143
                                if ( ! empty( $error_message ) ) {
×
144
                                        throw new UserError( esc_html( $error_message ) );
×
145
                                } else {
146
                                        // Translators: The placeholder is the name of the taxonomy for the term being deleted
147
                                        throw new UserError( esc_html( sprintf( __( 'The %1$s failed to delete but no error was provided', 'wp-graphql' ), $taxonomy->name ) ) );
×
148
                                }
149
                        }
150

151
                        /**
152
                         * Return the term object that was retrieved prior to deletion
153
                         */
154
                        return [
2✔
155
                                'termObject' => $term_object,
2✔
156
                        ];
2✔
157
                };
593✔
158
        }
159
}
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

© 2025 Coveralls, Inc