• 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

90.0
/src/Mutation/TermObjectUpdate.php
1
<?php
2
namespace WPGraphQL\Mutation;
3

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

11
/**
12
 * Class TermObjectUpdate
13
 *
14
 * @package WPGraphQL\Mutation
15
 */
16
class TermObjectUpdate {
17
        /**
18
         * Registers the TermObjectUpdate mutation.
19
         *
20
         * @param \WP_Taxonomy $taxonomy The Taxonomy the mutation is registered for.
21
         *
22
         * @return void
23
         */
24
        public static function register_mutation( WP_Taxonomy $taxonomy ) {
593✔
25
                $mutation_name = 'update' . ucwords( $taxonomy->graphql_single_name );
593✔
26
                register_graphql_mutation(
593✔
27
                        $mutation_name,
593✔
28
                        [
593✔
29
                                'inputFields'         => self::get_input_fields( $taxonomy ),
593✔
30
                                'outputFields'        => self::get_output_fields( $taxonomy ),
593✔
31
                                'mutateAndGetPayload' => self::mutate_and_get_payload( $taxonomy, $mutation_name ),
593✔
32
                        ]
593✔
33
                );
593✔
34
        }
35

36
        /**
37
         * Defines the mutation input field configuration.
38
         *
39
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
40
         *
41
         * @return array<string,array<string,mixed>>
42
         */
43
        public static function get_input_fields( WP_Taxonomy $taxonomy ) {
593✔
44
                return array_merge(
593✔
45
                        TermObjectCreate::get_input_fields( $taxonomy ),
593✔
46
                        [
593✔
47
                                'name' => [
593✔
48
                                        'type'        => 'String',
593✔
49
                                        'description' => static function () use ( $taxonomy ) {
593✔
50
                                                // Translators: The placeholder is the name of the taxonomy for the object being mutated
51
                                                return sprintf( __( 'The name of the %1$s object to mutate', 'wp-graphql' ), $taxonomy->name );
15✔
52
                                        },
593✔
53
                                ],
593✔
54
                                'id'   => [
593✔
55
                                        'type'        => [
593✔
56
                                                'non_null' => 'ID',
593✔
57
                                        ],
593✔
58
                                        'description' => static function () use ( $taxonomy ) {
593✔
59
                                                // Translators: The placeholder is the taxonomy of the term being updated
60
                                                return sprintf( __( 'The ID of the %1$s object to update', 'wp-graphql' ), $taxonomy->graphql_single_name );
15✔
61
                                        },
593✔
62
                                ],
593✔
63
                        ]
593✔
64
                );
593✔
65
        }
66

67
        /**
68
         * Defines the mutation output field configuration.
69
         *
70
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
71
         *
72
         * @return array<string,array<string,mixed>>
73
         */
74
        public static function get_output_fields( WP_Taxonomy $taxonomy ) {
593✔
75
                return TermObjectCreate::get_output_fields( $taxonomy );
593✔
76
        }
77

78
        /**
79
         * Defines the mutation data modification closure.
80
         *
81
         * @param \WP_Taxonomy $taxonomy The taxonomy type of the mutation.
82
         * @param string       $mutation_name  The name of the mutation.
83
         *
84
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
85
         */
86
        public static function mutate_and_get_payload( WP_Taxonomy $taxonomy, $mutation_name ) {
593✔
87
                return static function ( $input, AppContext $context, ResolveInfo $info ) use ( $taxonomy, $mutation_name ) {
593✔
88
                        $term_id = Utils::get_database_id_from_id( $input['id'] );
7✔
89

90
                        /**
91
                         * Ensure the type for the Global ID matches the type being mutated
92
                         */
93
                        if ( empty( $term_id ) ) {
7✔
94
                                // Translators: The placeholder is the name of the taxonomy for the term being edited
95
                                throw new UserError( esc_html( sprintf( __( 'The ID passed is not for a %1$s object', 'wp-graphql' ), $taxonomy->graphql_single_name ) ) );
1✔
96
                        }
97

98
                        /**
99
                         * Get the existing term
100
                         */
101
                        $existing_term = get_term( $term_id, $taxonomy->name );
7✔
102

103
                        /**
104
                         * If there was an error getting the existing term, return the error message
105
                         */
106
                        if ( ! $existing_term instanceof \WP_Term ) {
7✔
107
                                if ( is_wp_error( $existing_term ) ) {
3✔
108
                                        $error_message = $existing_term->get_error_message();
×
109
                                        if ( ! empty( $error_message ) ) {
×
110
                                                throw new UserError( esc_html( $error_message ) );
×
111
                                        } else {
112
                                                // Translators: The placeholder is the name of the taxonomy for the term being deleted
113
                                                throw new UserError( esc_html( sprintf( __( 'The %1$s node failed to update', 'wp-graphql' ), $taxonomy->name ) ) );
×
114
                                        }
115
                                }
116

117
                                // Translators: The placeholder is the name of the taxonomy for the term being deleted
118
                                throw new UserError( esc_html( sprintf( __( 'The %1$s node failed to update', 'wp-graphql' ), $taxonomy->name ) ) );
3✔
119
                        }
120

121
                        if ( $taxonomy->name !== $existing_term->taxonomy ) {
6✔
122
                                // translators: The first placeholder is an ID and the second placeholder is the name of the post type being edited
123
                                throw new UserError( esc_html( sprintf( __( 'The id %1$d is not of the type "%2$s"', 'wp-graphql' ), $term_id, $taxonomy->name ) ) );
×
124
                        }
125

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

134
                        /**
135
                         * Prepare the $args for mutation
136
                         */
137
                        $args = TermObjectMutation::prepare_object( $input, $taxonomy, $mutation_name );
6✔
138

139
                        if ( ! empty( $args ) ) {
5✔
140

141
                                /**
142
                                 * Update the term
143
                                 */
144
                                $update = wp_update_term( $existing_term->term_id, $taxonomy->name, wp_slash( (array) $args ) );
5✔
145

146
                                /**
147
                                 * Respond with any errors
148
                                 */
149
                                if ( is_wp_error( $update ) ) {
5✔
150
                                        // Translators: the placeholder is the name of the taxonomy
151
                                        throw new UserError( esc_html( sprintf( __( 'The %1$s failed to update', 'wp-graphql' ), $taxonomy->name ) ) );
×
152
                                }
153
                        }
154

155
                        /**
156
                         * Fires an action when a term is updated via a GraphQL Mutation
157
                         *
158
                         * @param int                                  $term_id       The ID of the term object that was mutated
159
                         * @param \WP_Taxonomy                         $taxonomy      The taxonomy of the term being updated
160
                         * @param array<string,mixed>                  $args          The args used to update the term
161
                         * @param string                               $mutation_name The name of the mutation being performed (create, update, delete, etc)
162
                         * @param \WPGraphQL\AppContext                $context       The AppContext passed down the resolve tree
163
                         * @param \GraphQL\Type\Definition\ResolveInfo $info          The ResolveInfo passed down the resolve tree
164
                         */
165
                        do_action( 'graphql_update_term', $existing_term->term_id, $taxonomy, $args, $mutation_name, $context, $info );
5✔
166

167
                        /**
168
                         * Fires an action when a term is updated via a GraphQL Mutation
169
                         *
170
                         * @param int                                  $term_id       The ID of the term object that was mutated
171
                         * @param array<string,mixed>                  $args          The args used to update the term
172
                         * @param string                               $mutation_name The name of the mutation being performed (create, update, delete, etc)
173
                         * @param \WPGraphQL\AppContext                $context       The AppContext passed down the resolve tree
174
                         * @param \GraphQL\Type\Definition\ResolveInfo $info          The ResolveInfo passed down the resolve tree
175
                         */
176
                        do_action( "graphql_update_{$taxonomy->name}", $existing_term->term_id, $args, $mutation_name, $context, $info );
5✔
177

178
                        /**
179
                         * Return the payload
180
                         */
181
                        return [
5✔
182
                                'termId' => $existing_term->term_id,
5✔
183
                        ];
5✔
184
                };
593✔
185
        }
186
}
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