• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.25
/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' => __( 'A string that contains the user\'s username.', 'wp-graphql' ),
593✔
42
                                ],
593✔
43
                                'email'    => [
593✔
44
                                        'type'        => 'String',
593✔
45
                                        'description' => __( 'A string containing the user\'s email address.', 'wp-graphql' ),
593✔
46
                                ],
593✔
47
                        ]
593✔
48
                );
593✔
49

50
                /**
51
                 * Make sure we don't allow input for role or roles
52
                 */
53
                unset( $input_fields['role'], $input_fields['roles'] );
593✔
54

55
                return $input_fields;
593✔
56
        }
57

58
        /**
59
         * Defines the mutation output field configuration.
60
         *
61
         * @return array<string,array<string,mixed>>
62
         */
63
        public static function get_output_fields() {
593✔
64
                return UserCreate::get_output_fields();
593✔
65
        }
66

67
        /**
68
         * Defines the mutation data modification closure.
69
         *
70
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
71
         */
72
        public static function mutate_and_get_payload() {
593✔
73
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
74
                        if ( ! get_option( 'users_can_register' ) ) {
3✔
75
                                throw new UserError( esc_html__( 'User registration is currently not allowed.', 'wp-graphql' ) );
2✔
76
                        }
77

78
                        if ( empty( $input['username'] ) ) {
1✔
79
                                throw new UserError( esc_html__( 'A username was not provided.', 'wp-graphql' ) );
×
80
                        }
81

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

86
                        /**
87
                         * Map all of the args from GQL to WP friendly
88
                         */
89
                        $user_args = UserMutation::prepare_user_object( $input, 'registerUser' );
1✔
90

91
                        /**
92
                         * Register the new user
93
                         */
94
                        $user_id = register_new_user( $user_args['user_login'], $user_args['user_email'] );
1✔
95

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

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

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

125
                        /**
126
                         * Set the ID of the user to be used in the update
127
                         */
128
                        $user_args['ID'] = absint( $user_id );
1✔
129

130
                        /**
131
                         * Make sure we don't accept any role input during registration
132
                         */
133
                        unset( $user_args['role'] );
1✔
134

135
                        /**
136
                         * Prevent "Password Changed" emails from being sent.
137
                         */
138
                        add_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
1✔
139

140
                        /**
141
                         * Update the registered user with the additional input (firstName, lastName, etc) from the mutation
142
                         */
143
                        wp_update_user( $user_args );
1✔
144

145
                        /**
146
                         * Remove filter preventing "Password Changed" emails.
147
                         */
148
                        remove_filter( 'send_password_change_email', [ self::class, 'return_false' ] );
1✔
149

150
                        /**
151
                         * Update additional user data
152
                         */
153
                        UserMutation::update_additional_user_object_data( $user_id, $input, 'registerUser', $context, $info );
1✔
154

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

165
        /**
166
         * @return bool False.
167
         */
UNCOV
168
        public static function return_false(): bool {
×
169
                return false;
×
170
        }
171
}
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

© 2026 Coveralls, Inc