PluginProbe
WPGraphQL / trunk
WPGraphQL vtrunk
2.22.3 2.22.2 2.22.1 2.22.0 2.21.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.15.0 2.14.1 2.14.0 2.13.0 2.2.0 2.3.0 2.3.3 2.3.6 2.3.8 2.5.0 2.5.1 2.5.2 2.5.3 All 177 releases
wp-graphql / src / Mutation / UserCreate.php

UserCreate.php in WPGraphQL trunk, at src/Mutation/UserCreate.php

227 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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() {
22 register_graphql_mutation(
23 'createUser',
24 [
25 'inputFields' => array_merge(
26 [
27 'username' => [
28 'type' => [
29 'non_null' => 'String',
30 ],
31 // translators: the placeholder is the name of the type of post object being updated
32 'description' => static function () {
33 return __( 'A string that contains the user\'s username for logging in.', 'wp-graphql' );
34 },
35 ],
36 ],
37 self::get_input_fields()
38 ),
39 'outputFields' => self::get_output_fields(),
40 'mutateAndGetPayload' => self::mutate_and_get_payload(),
41 ]
42 );
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() {
51 return [
52 'password' => [
53 'type' => 'String',
54 'description' => static function () {
55 return __( 'A string that contains the plain text password for the user.', 'wp-graphql' );
56 },
57 ],
58 'nicename' => [
59 'type' => 'String',
60 'description' => static function () {
61 return __( 'A string that contains a URL-friendly name for the user. The default is the user\'s username.', 'wp-graphql' );
62 },
63 ],
64 'websiteUrl' => [
65 'type' => 'String',
66 'description' => static function () {
67 return __( 'A string containing the user\'s URL for the user\'s web site.', 'wp-graphql' );
68 },
69 ],
70 'email' => [
71 'type' => 'String',
72 'description' => static function () {
73 return __( 'A string containing the user\'s email address.', 'wp-graphql' );
74 },
75 ],
76 'displayName' => [
77 'type' => 'String',
78 'description' => static function () {
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' );
80 },
81 ],
82 'nickname' => [
83 'type' => 'String',
84 'description' => static function () {
85 return __( 'The user\'s nickname, defaults to the user\'s username.', 'wp-graphql' );
86 },
87 ],
88 'firstName' => [
89 'type' => 'String',
90 'description' => static function () {
91 return __( 'The user\'s first name.', 'wp-graphql' );
92 },
93 ],
94 'lastName' => [
95 'type' => 'String',
96 'description' => static function () {
97 return __( 'The user\'s last name.', 'wp-graphql' );
98 },
99 ],
100 'description' => [
101 'type' => 'String',
102 'description' => static function () {
103 return __( 'A string containing content about the user.', 'wp-graphql' );
104 },
105 ],
106 'richEditing' => [
107 'type' => 'String',
108 'description' => static function () {
109 return __( 'A string for whether to enable the rich editor or not. False if not empty.', 'wp-graphql' );
110 },
111 ],
112 'registered' => [
113 'type' => 'String',
114 'description' => static function () {
115 return __( 'The date the user registered. Format is Y-m-d H:i:s.', 'wp-graphql' );
116 },
117 ],
118 'roles' => [
119 'type' => [
120 'list_of' => 'String',
121 ],
122 'description' => static function () {
123 return __( 'An array of roles to be assigned to the user.', 'wp-graphql' );
124 },
125 ],
126 'jabber' => [
127 'type' => 'String',
128 'description' => static function () {
129 return __( 'User\'s Jabber account.', 'wp-graphql' );
130 },
131 ],
132 'aim' => [
133 'type' => 'String',
134 'description' => static function () {
135 return __( 'User\'s AOL IM account.', 'wp-graphql' );
136 },
137 ],
138 'yim' => [
139 'type' => 'String',
140 'description' => static function () {
141 return __( 'User\'s Yahoo IM account.', 'wp-graphql' );
142 },
143 ],
144 'locale' => [
145 'type' => 'String',
146 'description' => static function () {
147 return __( 'User\'s locale.', 'wp-graphql' );
148 },
149 ],
150 ];
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() {
159 return [
160 'user' => [
161 'type' => 'User',
162 'description' => static function () {
163 return __( 'The User object mutation type.', 'wp-graphql' );
164 },
165 ],
166 ];
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() {
175 return static function ( $input, AppContext $context, ResolveInfo $info ) {
176 if ( ! current_user_can( 'create_users' ) ) {
177 throw new UserError( esc_html__( 'Sorry, you are not allowed to create a new user.', 'wp-graphql' ) );
178 }
179
180 /**
181 * Map all of the args from GQL to WP friendly
182 */
183 // Flag this as a create operation so prepare_user_object() will generate
184 // a password if one isn't provided (required by WP 6.9+).
185 $input['is_create'] = true;
186 $user_args = UserMutation::prepare_user_object( $input, 'createUser' );
187
188 /**
189 * Create the new user
190 */
191 $user_id = wp_insert_user( $user_args );
192
193 /**
194 * Throw an exception if the post failed to create
195 */
196 if ( is_wp_error( $user_id ) ) {
197 $error_message = $user_id->get_error_message();
198 if ( ! empty( $error_message ) ) {
199 throw new UserError( esc_html( $error_message ) );
200 } else {
201 throw new UserError( esc_html__( 'The object failed to create but no error was provided', 'wp-graphql' ) );
202 }
203 }
204
205 /**
206 * If the $post_id is empty, we should throw an exception
207 */
208 if ( empty( $user_id ) ) {
209 throw new UserError( esc_html__( 'The object failed to create', 'wp-graphql' ) );
210 }
211
212 /**
213 * Update additional user data
214 */
215 UserMutation::update_additional_user_object_data( $user_id, $input, 'createUser', $context, $info );
216
217 /**
218 * Return the new user ID
219 */
220 return [
221 'id' => $user_id,
222 'user' => $context->get_loader( 'user' )->load_deferred( $user_id ),
223 ];
224 };
225 }
226 }
227