• 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

96.81
/src/Mutation/TermObjectCreate.php
1
<?php
2

3
namespace WPGraphQL\Mutation;
4

5
use GraphQL\Error\UserError;
6
use GraphQL\Type\Definition\ResolveInfo;
7
use WPGraphQL\AppContext;
8
use WPGraphQL\Data\TermObjectMutation;
9
use WP_Taxonomy;
10

11
class TermObjectCreate {
12
        /**
13
         * Registers the TermObjectCreate mutation.
14
         *
15
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
16
         *
17
         * @return void
18
         */
19
        public static function register_mutation( WP_Taxonomy $taxonomy ) {
593✔
20
                $mutation_name = 'create' . ucwords( $taxonomy->graphql_single_name );
593✔
21

22
                register_graphql_mutation(
593✔
23
                        $mutation_name,
593✔
24
                        [
593✔
25
                                'inputFields'         => array_merge(
593✔
26
                                        self::get_input_fields( $taxonomy ),
593✔
27
                                        [
593✔
28
                                                'name' => [
593✔
29
                                                        'type'        => [
593✔
30
                                                                'non_null' => 'String',
593✔
31
                                                        ],
593✔
32
                                                        'description' => static function () use ( $taxonomy ) {
593✔
33
                                                                // translators: The placeholder is the name of the taxonomy for the object being mutated
34
                                                                return sprintf( __( 'The name of the %1$s object to mutate', 'wp-graphql' ), $taxonomy->name );
15✔
35
                                                        },
593✔
36
                                                ],
593✔
37
                                        ]
593✔
38
                                ),
593✔
39
                                'outputFields'        => self::get_output_fields( $taxonomy ),
593✔
40
                                'mutateAndGetPayload' => self::mutate_and_get_payload( $taxonomy, $mutation_name ),
593✔
41
                        ]
593✔
42
                );
593✔
43
        }
44

45
        /**
46
         * Defines the mutation input field configuration.
47
         *
48
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
49
         *
50
         * @return array<string,array<string,mixed>>
51
         */
52
        public static function get_input_fields( WP_Taxonomy $taxonomy ) {
593✔
53
                $fields = [
593✔
54
                        'aliasOf'     => [
593✔
55
                                'type'        => 'String',
593✔
56
                                'description' => static function () use ( $taxonomy ) {
593✔
57
                                        // translators: The placeholder is the name of the taxonomy for the object being mutated
58
                                        return sprintf( __( 'The slug that the %1$s will be an alias of', 'wp-graphql' ), $taxonomy->name );
15✔
59
                                },
593✔
60
                        ],
593✔
61
                        'description' => [
593✔
62
                                'type'        => 'String',
593✔
63
                                'description' => static function () use ( $taxonomy ) {
593✔
64
                                        // translators: The placeholder is the name of the taxonomy for the object being mutated
65
                                        return sprintf( __( 'The description of the %1$s object', 'wp-graphql' ), $taxonomy->name );
15✔
66
                                },
593✔
67
                        ],
593✔
68
                        'slug'        => [
593✔
69
                                'type'        => 'String',
593✔
70
                                'description' => static function () use ( $taxonomy ) {
593✔
71
                                        // translators: The placeholder is the name of the taxonomy for the object being mutated
72
                                        return sprintf( __( 'If this argument exists then the slug will be checked to see if it is not an existing valid term. If that check succeeds (it is not a valid term), then it is added and the term id is given. If it fails, then a check is made to whether the taxonomy is hierarchical and the parent argument is not empty. If the second check succeeds, the term will be inserted and the term id will be given. If the slug argument is empty, then it will be calculated from the term name.', 'wp-graphql' ), $taxonomy->name );
15✔
73
                                },
593✔
74
                        ],
593✔
75
                ];
593✔
76

77
                /**
78
                 * Add a parentId field to hierarchical taxonomies to allow parents to be set
79
                 */
80
                if ( true === $taxonomy->hierarchical ) {
593✔
81
                        $fields['parentId']         = [
593✔
82
                                'type'        => 'ID',
593✔
83
                                'description' => static function () use ( $taxonomy ) {
593✔
84
                                        // translators: The placeholder is the name of the taxonomy for the object being mutated
85
                                        return sprintf( __( 'The ID of the %1$s that should be set as the parent. This field cannot be used in conjunction with parentDatabaseId', 'wp-graphql' ), $taxonomy->name );
15✔
86
                                },
593✔
87
                        ];
593✔
88
                        $fields['parentDatabaseId'] = [
593✔
89
                                'type'        => 'Int',
593✔
90
                                'description' => static function () use ( $taxonomy ) {
593✔
91
                                        // translators: The placeholder is the name of the taxonomy for the object being mutated
92
                                        return sprintf( __( 'The database ID of the %1$s that should be set as the parent. This field cannot be used in conjunction with parentId', 'wp-graphql' ), $taxonomy->name );
15✔
93
                                },
593✔
94
                        ];
593✔
95
                }
96

97
                return $fields;
593✔
98
        }
99

100
        /**
101
         * Defines the mutation output field configuration.
102
         *
103
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
104
         *
105
         * @return array<string,array<string,mixed>>
106
         */
107
        public static function get_output_fields( WP_Taxonomy $taxonomy ) {
593✔
108
                return [
593✔
109
                        $taxonomy->graphql_single_name => [
593✔
110
                                'type'        => $taxonomy->graphql_single_name,
593✔
111
                                'description' => static function () use ( $taxonomy ) {
593✔
112
                                        // translators: Placeholder is the name of the taxonomy
113
                                        return sprintf( __( 'The created %s', 'wp-graphql' ), $taxonomy->name );
15✔
114
                                },
593✔
115
                                'resolve'     => static function ( $payload, $_args, AppContext $context ) {
593✔
116
                                        $id = isset( $payload['termId'] ) ? absint( $payload['termId'] ) : null;
7✔
117

118
                                        return $context->get_loader( 'term' )->load_deferred( $id );
7✔
119
                                },
593✔
120
                        ],
593✔
121
                ];
593✔
122
        }
123

124
        /**
125
         * Defines the mutation data modification closure.
126
         *
127
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
128
         * @param string       $mutation_name The name of the mutation.
129
         *
130
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
131
         */
132
        public static function mutate_and_get_payload( WP_Taxonomy $taxonomy, string $mutation_name ) {
593✔
133
                return static function ( $input, AppContext $context, ResolveInfo $info ) use ( $taxonomy, $mutation_name ) {
593✔
134

135
                        /**
136
                         * Ensure the user can edit_terms
137
                         */
138
                        if ( ! isset( $taxonomy->cap->edit_terms ) || ! current_user_can( $taxonomy->cap->edit_terms ) ) {
8✔
139
                                // translators: the $taxonomy->graphql_plural_name placeholder is the name of the object being mutated
140
                                throw new UserError( esc_html( sprintf( __( 'Sorry, you are not allowed to create %1$s', 'wp-graphql' ), $taxonomy->graphql_plural_name ) ) );
2✔
141
                        }
142

143
                        /**
144
                         * Prepare the object for insertion
145
                         */
146
                        $args = TermObjectMutation::prepare_object( $input, $taxonomy, $mutation_name );
6✔
147

148
                        /**
149
                         * Ensure a name was provided
150
                         */
151
                        if ( empty( $args['name'] ) ) {
6✔
152
                                // translators: The placeholder is the name of the taxonomy of the term being mutated
153
                                throw new UserError( esc_html( sprintf( __( 'A name is required to create a %1$s', 'wp-graphql' ), $taxonomy->name ) ) );
×
154
                        }
155

156
                        $term_name = wp_slash( $args['name'] );
6✔
157

158
                        if ( ! is_string( $term_name ) ) {
6✔
159
                                // translators: The placeholder is the name of the taxonomy of the term being mutated
160
                                throw new UserError( esc_html( sprintf( __( 'A valid name is required to create a %1$s', 'wp-graphql' ), $taxonomy->name ) ) );
×
161
                        }
162

163
                        /**
164
                         * Insert the term
165
                         */
166
                        $term = wp_insert_term( $term_name, $taxonomy->name, wp_slash( (array) $args ) );
6✔
167

168
                        /**
169
                         * If it was an error, return the message as an exception
170
                         */
171
                        if ( is_wp_error( $term ) ) {
6✔
172
                                $error_message = $term->get_error_message();
1✔
173
                                if ( ! empty( $error_message ) ) {
1✔
174
                                        throw new UserError( esc_html( $error_message ) );
1✔
175
                                } else {
176
                                        throw new UserError( esc_html__( 'The object failed to update but no error was provided', 'wp-graphql' ) );
×
177
                                }
178
                        }
179

180
                        /**
181
                         * If the response to creating the term didn't respond with a term_id, throw an exception
182
                         */
183
                        if ( empty( $term['term_id'] ) ) {
6✔
184
                                throw new UserError( esc_html__( 'The object failed to create', 'wp-graphql' ) );
1✔
185
                        }
186

187
                        /**
188
                         * Fires after a single term is created or updated via a GraphQL mutation
189
                         *
190
                         * @param int                                  $term_id       Inserted term object
191
                         * @param \WP_Taxonomy                         $taxonomy      The taxonomy of the term being updated
192
                         * @param array<string,mixed>                  $args          The args used to insert the term
193
                         * @param string                               $mutation_name The name of the mutation being performed
194
                         * @param \WPGraphQL\AppContext                $context       The AppContext passed down the resolve tree
195
                         * @param \GraphQL\Type\Definition\ResolveInfo $info          The ResolveInfo passed down the resolve tree
196
                         */
197
                        do_action( 'graphql_insert_term', $term['term_id'], $taxonomy, $args, $mutation_name, $context, $info );
5✔
198

199
                        /**
200
                         * Fires after a single term is created or updated via a GraphQL mutation
201
                         *
202
                         * The dynamic portion of the hook name, `$taxonomy->name` refers to the taxonomy of the term being mutated
203
                         *
204
                         * @param int                                  $term_id       Inserted term object
205
                         * @param array<string,mixed>                  $args          The args used to insert the term
206
                         * @param string                               $mutation_name The name of the mutation being performed
207
                         * @param \WPGraphQL\AppContext                $context       The AppContext passed down the resolve tree
208
                         * @param \GraphQL\Type\Definition\ResolveInfo $info          The ResolveInfo passed down the resolve tree
209
                         */
210
                        do_action( "graphql_insert_{$taxonomy->name}", $term['term_id'], $args, $mutation_name, $context, $info );
5✔
211

212
                        return [
5✔
213
                                'termId' => $term['term_id'],
5✔
214
                        ];
5✔
215
                };
593✔
216
        }
217
}
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