• 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

98.67
/src/Mutation/UserCreate.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\UserMutation;
9

10
/**
11
 * Class UserCreate
12
 *
13
 * @package WPGraphQL\Mutation
14
 */
15
class UserCreate {
16
        /**
17
         * Registers the CommentCreate mutation.
18
         *
19
         * @return void
20
         */
21
        public static function register_mutation() {
593✔
22
                register_graphql_mutation(
593✔
23
                        'createUser',
593✔
24
                        [
593✔
25
                                'inputFields'         => array_merge(
593✔
26
                                        [
593✔
27
                                                'username' => [
593✔
28
                                                        'type'        => [
593✔
29
                                                                'non_null' => 'String',
593✔
30
                                                        ],
593✔
31
                                                        // translators: the placeholder is the name of the type of post object being updated
32
                                                        'description' => static function () {
593✔
33
                                                                return __( 'A string that contains the user\'s username for logging in.', 'wp-graphql' );
15✔
34
                                                        },
593✔
35
                                                ],
593✔
36
                                        ],
593✔
37
                                        self::get_input_fields()
593✔
38
                                ),
593✔
39
                                'outputFields'        => self::get_output_fields(),
593✔
40
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
41
                        ]
593✔
42
                );
593✔
43
        }
44

45
        /**
46
         * Defines the mutation input field configuration.
47
         *
48
         * @return array<string,array<string,mixed>>
49
         */
50
        public static function get_input_fields() {
593✔
51
                return [
593✔
52
                        'password'    => [
593✔
53
                                'type'        => 'String',
593✔
54
                                'description' => static function () {
593✔
55
                                        return __( 'A string that contains the plain text password for the user.', 'wp-graphql' );
15✔
56
                                },
593✔
57
                        ],
593✔
58
                        'nicename'    => [
593✔
59
                                'type'        => 'String',
593✔
60
                                'description' => static function () {
593✔
61
                                        return __( 'A string that contains a URL-friendly name for the user. The default is the user\'s username.', 'wp-graphql' );
15✔
62
                                },
593✔
63
                        ],
593✔
64
                        'websiteUrl'  => [
593✔
65
                                'type'        => 'String',
593✔
66
                                'description' => static function () {
593✔
67
                                        return __( 'A string containing the user\'s URL for the user\'s web site.', 'wp-graphql' );
15✔
68
                                },
593✔
69
                        ],
593✔
70
                        'email'       => [
593✔
71
                                'type'        => 'String',
593✔
72
                                'description' => static function () {
593✔
73
                                        return __( 'A string containing the user\'s email address.', 'wp-graphql' );
15✔
74
                                },
593✔
75
                        ],
593✔
76
                        'displayName' => [
593✔
77
                                'type'        => 'String',
593✔
78
                                'description' => static function () {
593✔
79
                                        return __( 'A string that will be shown on the site. Defaults to user\'s username. It is likely that you will want to change this, for both appearance and security through obscurity (that is if you dont use and delete the default admin user).', 'wp-graphql' );
15✔
80
                                },
593✔
81
                        ],
593✔
82
                        'nickname'    => [
593✔
83
                                'type'        => 'String',
593✔
84
                                'description' => static function () {
593✔
85
                                        return __( 'The user\'s nickname, defaults to the user\'s username.', 'wp-graphql' );
15✔
86
                                },
593✔
87
                        ],
593✔
88
                        'firstName'   => [
593✔
89
                                'type'        => 'String',
593✔
90
                                'description' => static function () {
593✔
91
                                        return __( 'The user\'s first name.', 'wp-graphql' );
15✔
92
                                },
593✔
93
                        ],
593✔
94
                        'lastName'    => [
593✔
95
                                'type'        => 'String',
593✔
96
                                'description' => static function () {
593✔
97
                                        return __( 'The user\'s last name.', 'wp-graphql' );
15✔
98
                                },
593✔
99
                        ],
593✔
100
                        'description' => [
593✔
101
                                'type'        => 'String',
593✔
102
                                'description' => static function () {
593✔
103
                                        return __( 'A string containing content about the user.', 'wp-graphql' );
15✔
104
                                },
593✔
105
                        ],
593✔
106
                        'richEditing' => [
593✔
107
                                'type'        => 'String',
593✔
108
                                'description' => static function () {
593✔
109
                                        return __( 'A string for whether to enable the rich editor or not. False if not empty.', 'wp-graphql' );
15✔
110
                                },
593✔
111
                        ],
593✔
112
                        'registered'  => [
593✔
113
                                'type'        => 'String',
593✔
114
                                'description' => static function () {
593✔
115
                                        return __( 'The date the user registered. Format is Y-m-d H:i:s.', 'wp-graphql' );
15✔
116
                                },
593✔
117
                        ],
593✔
118
                        'roles'       => [
593✔
119
                                'type'        => [
593✔
120
                                        'list_of' => 'String',
593✔
121
                                ],
593✔
122
                                'description' => static function () {
593✔
123
                                        return __( 'An array of roles to be assigned to the user.', 'wp-graphql' );
15✔
124
                                },
593✔
125
                        ],
593✔
126
                        'jabber'      => [
593✔
127
                                'type'        => 'String',
593✔
128
                                'description' => static function () {
593✔
129
                                        return __( 'User\'s Jabber account.', 'wp-graphql' );
15✔
130
                                },
593✔
131
                        ],
593✔
132
                        'aim'         => [
593✔
133
                                'type'        => 'String',
593✔
134
                                'description' => static function () {
593✔
135
                                        return __( 'User\'s AOL IM account.', 'wp-graphql' );
15✔
136
                                },
593✔
137
                        ],
593✔
138
                        'yim'         => [
593✔
139
                                'type'        => 'String',
593✔
140
                                'description' => static function () {
593✔
141
                                        return __( 'User\'s Yahoo IM account.', 'wp-graphql' );
15✔
142
                                },
593✔
143
                        ],
593✔
144
                        'locale'      => [
593✔
145
                                'type'        => 'String',
593✔
146
                                'description' => static function () {
593✔
147
                                        return __( 'User\'s locale.', 'wp-graphql' );
15✔
148
                                },
593✔
149
                        ],
593✔
150
                ];
593✔
151
        }
152

153
        /**
154
         * Defines the mutation output field configuration.
155
         *
156
         * @return array<string,array<string,mixed>>
157
         */
158
        public static function get_output_fields() {
593✔
159
                return [
593✔
160
                        'user' => [
593✔
161
                                'type'        => 'User',
593✔
162
                                'description' => static function () {
593✔
163
                                        return __( 'The User object mutation type.', 'wp-graphql' );
15✔
164
                                },
593✔
165
                        ],
593✔
166
                ];
593✔
167
        }
168

169
        /**
170
         * Defines the mutation data modification closure.
171
         *
172
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
173
         */
174
        public static function mutate_and_get_payload() {
593✔
175
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
176
                        if ( ! current_user_can( 'create_users' ) ) {
7✔
177
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to create a new user.', 'wp-graphql' ) );
1✔
178
                        }
179

180
                        /**
181
                         * Map all of the args from GQL to WP friendly
182
                         */
183
                        $user_args = UserMutation::prepare_user_object( $input, 'createUser' );
6✔
184

185
                        /**
186
                         * Create the new user
187
                         */
188
                        $user_id = wp_insert_user( $user_args );
5✔
189

190
                        /**
191
                         * Throw an exception if the post failed to create
192
                         */
193
                        if ( is_wp_error( $user_id ) ) {
5✔
194
                                $error_message = $user_id->get_error_message();
2✔
195
                                if ( ! empty( $error_message ) ) {
2✔
196
                                        throw new UserError( esc_html( $error_message ) );
2✔
197
                                } else {
198
                                        throw new UserError( esc_html__( 'The object failed to create but no error was provided', 'wp-graphql' ) );
×
199
                                }
200
                        }
201

202
                        /**
203
                         * If the $post_id is empty, we should throw an exception
204
                         */
205
                        if ( empty( $user_id ) ) {
3✔
206
                                throw new UserError( esc_html__( 'The object failed to create', 'wp-graphql' ) );
×
207
                        }
208

209
                        /**
210
                         * Update additional user data
211
                         */
212
                        UserMutation::update_additional_user_object_data( $user_id, $input, 'createUser', $context, $info );
3✔
213

214
                        /**
215
                         * Return the new user ID
216
                         */
217
                        return [
3✔
218
                                'id'   => $user_id,
3✔
219
                                'user' => $context->get_loader( 'user' )->load_deferred( $user_id ),
3✔
220
                        ];
3✔
221
                };
593✔
222
        }
223
}
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