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 / UserRegister.php

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

179 lines 4.8 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 class UserRegister {
11 /**
12 * Registers the CommentCreate mutation.
13 *
14 * @return void
15 */
16 public static function register_mutation() {
17 register_graphql_mutation(
18 'registerUser',
19 [
20 'inputFields' => self::get_input_fields(),
21 'outputFields' => self::get_output_fields(),
22 'mutateAndGetPayload' => self::mutate_and_get_payload(),
23 ]
24 );
25 }
26
27 /**
28 * Defines the mutation input field configuration.
29 *
30 * @return array<string,array<string,mixed>>
31 */
32 public static function get_input_fields() {
33 $input_fields = array_merge(
34 UserCreate::get_input_fields(),
35 [
36 'username' => [
37 'type' => [
38 'non_null' => 'String',
39 ],
40 // translators: the placeholder is the name of the type of object being updated
41 'description' => static function () {
42 return __( 'A string that contains the user\'s username.', 'wp-graphql' );
43 },
44 ],
45 'email' => [
46 'type' => 'String',
47 'description' => static function () {
48 return __( 'A string containing the user\'s email address.', 'wp-graphql' );
49 },
50 ],
51 ]
52 );
53
54 /**
55 * Make sure we don't allow input for role or roles
56 */
57 unset( $input_fields['role'], $input_fields['roles'] );
58
59 return $input_fields;
60 }
61
62 /**
63 * Defines the mutation output field configuration.
64 *
65 * @return array<string,array<string,mixed>>
66 */
67 public static function get_output_fields() {
68 return UserCreate::get_output_fields();
69 }
70
71 /**
72 * Defines the mutation data modification closure.
73 *
74 * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
75 */
76 public static function mutate_and_get_payload() {
77 return static function ( $input, AppContext $context, ResolveInfo $info ) {
78 if ( ! get_option( 'users_can_register' ) ) {
79 throw new UserError( esc_html__( 'User registration is currently not allowed.', 'wp-graphql' ) );
80 }
81
82 if ( empty( $input['username'] ) ) {
83 throw new UserError( esc_html__( 'A username was not provided.', 'wp-graphql' ) );
84 }
85
86 if ( empty( $input['email'] ) ) {
87 throw new UserError( esc_html__( 'An email address was not provided.', 'wp-graphql' ) );
88 }
89
90 /**
91 * Map all of the args from GQL to WP friendly
92 */
93 // Flag this as a create operation so prepare_user_object() will generate
94 // a password if one isn't provided (required by WP 6.9+).
95 $input['is_create'] = true;
96 $user_args = UserMutation::prepare_user_object( $input, 'registerUser' );
97
98 /**
99 * Register the new user
100 */
101 $user_id = register_new_user( $user_args['user_login'], $user_args['user_email'] );
102
103 /**
104 * Throw an exception if the user failed to register
105 */
106 if ( is_wp_error( $user_id ) ) {
107 $error_message = $user_id->get_error_message();
108 if ( ! empty( $error_message ) ) {
109 throw new UserError( esc_html( $error_message ) );
110 } else {
111 throw new UserError( esc_html__( 'The user failed to register but no error was provided', 'wp-graphql' ) );
112 }
113 }
114
115 /**
116 * If the $user_id is empty, we should throw an exception
117 */
118 if ( empty( $user_id ) ) {
119 throw new UserError( esc_html__( 'The user failed to create', 'wp-graphql' ) );
120 }
121
122 /**
123 * If the client isn't already authenticated, set the state in the current session to
124 * the user they just registered. This is mostly so that they can get a response from
125 * the mutation about the user they just registered after the user object passes
126 * through the user model.
127 */
128 if ( ! is_user_logged_in() ) {
129 wp_set_current_user( $user_id );
130 }
131
132 /**
133 * Set the ID of the user to be used in the update
134 */
135 $user_args['ID'] = absint( $user_id );
136
137 /**
138 * Make sure we don't accept any role input during registration
139 */
140 unset( $user_args['role'] );
141
142 /**
143 * Prevent "Password Changed" emails from being sent.
144 */
145 add_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
146
147 /**
148 * Update the registered user with the additional input (firstName, lastName, etc) from the mutation
149 */
150 wp_update_user( $user_args );
151
152 /**
153 * Remove filter preventing "Password Changed" emails.
154 */
155 remove_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
156
157 /**
158 * Update additional user data
159 */
160 UserMutation::update_additional_user_object_data( $user_id, $input, 'registerUser', $context, $info );
161
162 /**
163 * Return the new user ID
164 */
165 return [
166 'id' => $user_id,
167 'user' => $context->get_loader( 'user' )->load_deferred( $user_id ),
168 ];
169 };
170 }
171
172 /**
173 * @return bool False.
174 */
175 public static function return_false(): bool {
176 return false;
177 }
178 }
179