• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
You are now the owner of this repo.

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

86.15
/src/Mutation/UserRegister.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
class UserRegister {
11
        /**
12
         * Registers the CommentCreate mutation.
13
         *
14
         * @return void
15
         */
16
        public static function register_mutation() {
593✔
17
                register_graphql_mutation(
593✔
18
                        'registerUser',
593✔
19
                        [
593✔
20
                                'inputFields'         => self::get_input_fields(),
593✔
21
                                'outputFields'        => self::get_output_fields(),
593✔
22
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
23
                        ]
593✔
24
                );
593✔
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() {
593✔
33
                $input_fields = array_merge(
593✔
34
                        UserCreate::get_input_fields(),
593✔
35
                        [
593✔
36
                                'username' => [
593✔
37
                                        'type'        => [
593✔
38
                                                'non_null' => 'String',
593✔
39
                                        ],
593✔
40
                                        // translators: the placeholder is the name of the type of object being updated
41
                                        'description' => static function () {
593✔
42
                                                return __( 'A string that contains the user\'s username.', 'wp-graphql' );
15✔
43
                                        },
593✔
44
                                ],
593✔
45
                                'email'    => [
593✔
46
                                        'type'        => 'String',
593✔
47
                                        'description' => static function () {
593✔
48
                                                return __( 'A string containing the user\'s email address.', 'wp-graphql' );
15✔
49
                                        },
593✔
50
                                ],
593✔
51
                        ]
593✔
52
                );
593✔
53

54
                /**
55
                 * Make sure we don't allow input for role or roles
56
                 */
57
                unset( $input_fields['role'], $input_fields['roles'] );
593✔
58

59
                return $input_fields;
593✔
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() {
593✔
68
                return UserCreate::get_output_fields();
593✔
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() {
593✔
77
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
78
                        if ( ! get_option( 'users_can_register' ) ) {
3✔
79
                                throw new UserError( esc_html__( 'User registration is currently not allowed.', 'wp-graphql' ) );
2✔
80
                        }
81

82
                        if ( empty( $input['username'] ) ) {
1✔
83
                                throw new UserError( esc_html__( 'A username was not provided.', 'wp-graphql' ) );
×
84
                        }
85

86
                        if ( empty( $input['email'] ) ) {
1✔
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
                        $user_args = UserMutation::prepare_user_object( $input, 'registerUser' );
1✔
94

95
                        /**
96
                         * Register the new user
97
                         */
98
                        $user_id = register_new_user( $user_args['user_login'], $user_args['user_email'] );
1✔
99

100
                        /**
101
                         * Throw an exception if the user failed to register
102
                         */
103
                        if ( is_wp_error( $user_id ) ) {
1✔
104
                                $error_message = $user_id->get_error_message();
×
105
                                if ( ! empty( $error_message ) ) {
×
106
                                        throw new UserError( esc_html( $error_message ) );
×
107
                                } else {
108
                                        throw new UserError( esc_html__( 'The user failed to register but no error was provided', 'wp-graphql' ) );
×
109
                                }
110
                        }
111

112
                        /**
113
                         * If the $user_id is empty, we should throw an exception
114
                         */
115
                        if ( empty( $user_id ) ) {
1✔
116
                                throw new UserError( esc_html__( 'The user failed to create', 'wp-graphql' ) );
×
117
                        }
118

119
                        /**
120
                         * If the client isn't already authenticated, set the state in the current session to
121
                         * the user they just registered. This is mostly so that they can get a response from
122
                         * the mutation about the user they just registered after the user object passes
123
                         * through the user model.
124
                         */
125
                        if ( ! is_user_logged_in() ) {
1✔
126
                                wp_set_current_user( $user_id );
1✔
127
                        }
128

129
                        /**
130
                         * Set the ID of the user to be used in the update
131
                         */
132
                        $user_args['ID'] = absint( $user_id );
1✔
133

134
                        /**
135
                         * Make sure we don't accept any role input during registration
136
                         */
137
                        unset( $user_args['role'] );
1✔
138

139
                        /**
140
                         * Prevent "Password Changed" emails from being sent.
141
                         */
142
                        add_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
1✔
143

144
                        /**
145
                         * Update the registered user with the additional input (firstName, lastName, etc) from the mutation
146
                         */
147
                        wp_update_user( $user_args );
1✔
148

149
                        /**
150
                         * Remove filter preventing "Password Changed" emails.
151
                         */
152
                        remove_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
1✔
153

154
                        /**
155
                         * Update additional user data
156
                         */
157
                        UserMutation::update_additional_user_object_data( $user_id, $input, 'registerUser', $context, $info );
1✔
158

159
                        /**
160
                         * Return the new user ID
161
                         */
162
                        return [
1✔
163
                                'id'   => $user_id,
1✔
164
                                'user' => $context->get_loader( 'user' )->load_deferred( $user_id ),
1✔
165
                        ];
1✔
166
                };
593✔
167
        }
168

169
        /**
170
         * @return bool False.
171
         */
172
        public static function return_false(): bool {
×
173
                return false;
×
174
        }
175
}
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