• 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

92.45
/src/Mutation/ResetUserPassword.php
1
<?php
2
namespace WPGraphQL\Mutation;
3

4
use GraphQL\Error\UserError;
5
use WPGraphQL\AppContext;
6

7
class ResetUserPassword {
8
        /**
9
         * Registers the ResetUserPassword mutation.
10
         *
11
         * @return void
12
         * @throws \Exception
13
         */
14
        public static function register_mutation() {
593✔
15
                register_graphql_mutation(
593✔
16
                        'resetUserPassword',
593✔
17
                        [
593✔
18
                                'inputFields'         => self::get_input_fields(),
593✔
19
                                'outputFields'        => self::get_output_fields(),
593✔
20
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
21
                        ]
593✔
22
                );
593✔
23
        }
24

25
        /**
26
         * Defines the mutation input field configuration.
27
         *
28
         * @return array<string,array<string,mixed>>
29
         */
30
        public static function get_input_fields() {
593✔
31
                return [
593✔
32
                        'key'      => [
593✔
33
                                'type'        => 'String',
593✔
34
                                'description' => static function () {
593✔
35
                                        return __( 'Password reset key', 'wp-graphql' );
15✔
36
                                },
593✔
37
                        ],
593✔
38
                        'login'    => [
593✔
39
                                'type'        => 'String',
593✔
40
                                'description' => static function () {
593✔
41
                                        return __( 'The user\'s login (username).', 'wp-graphql' );
15✔
42
                                },
593✔
43
                        ],
593✔
44
                        'password' => [
593✔
45
                                'type'        => 'String',
593✔
46
                                'description' => static function () {
593✔
47
                                        return __( 'The new password.', 'wp-graphql' );
15✔
48
                                },
593✔
49
                        ],
593✔
50
                ];
593✔
51
        }
52

53
        /**
54
         * Defines the mutation output field configuration.
55
         *
56
         * @return array<string,array<string,mixed>>
57
         */
58
        public static function get_output_fields() {
593✔
59
                return UserCreate::get_output_fields();
593✔
60
        }
61

62
        /**
63
         * Defines the mutation data modification closure.
64
         *
65
         * @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed>
66
         */
67
        public static function mutate_and_get_payload() {
593✔
68
                return static function ( $input, AppContext $context ) {
593✔
69
                        if ( empty( $input['key'] ) ) {
4✔
70
                                throw new UserError( esc_html__( 'A password reset key is required.', 'wp-graphql' ) );
×
71
                        }
72

73
                        if ( empty( $input['login'] ) ) {
4✔
74
                                throw new UserError( esc_html__( 'A user login is required.', 'wp-graphql' ) );
×
75
                        }
76

77
                        if ( empty( $input['password'] ) ) {
4✔
78
                                throw new UserError( esc_html__( 'A new password is required.', 'wp-graphql' ) );
×
79
                        }
80

81
                        $user = check_password_reset_key( $input['key'], $input['login'] );
4✔
82

83
                        /**
84
                         * If the password reset key check returns an error
85
                         */
86
                        if ( is_wp_error( $user ) ) {
4✔
87

88
                                /**
89
                                 * Determine the message to return
90
                                 */
91
                                if ( 'expired_key' === $user->get_error_code() ) {
2✔
92
                                        $message = __( 'Password reset link has expired.', 'wp-graphql' );
×
93
                                } else {
94
                                        $message = __( 'Password reset link is invalid.', 'wp-graphql' );
2✔
95
                                }
96

97
                                /**
98
                                 * Throw an error with the message
99
                                 */
100
                                throw new UserError( esc_html( $message ) );
2✔
101
                        }
102

103
                        /**
104
                         * Reset the password
105
                         */
106
                        reset_password( $user, $input['password'] );
2✔
107

108
                        // Log in the user, since they already authenticated with the reset key.
109
                        wp_set_current_user( $user->ID );
2✔
110

111
                        /**
112
                         * Return the user ID
113
                         */
114
                        return [
2✔
115
                                'id'   => $user->ID,
2✔
116
                                'user' => $context->get_loader( 'user' )->load_deferred( $user->ID ),
2✔
117
                        ];
2✔
118
                };
593✔
119
        }
120
}
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