1 | <?php |
---|
2 | |
---|
3 | function wporg_login_rest_routes() { |
---|
4 | register_rest_route( 'wporg/v1', '/username-available/(?P<login>.*)', array( |
---|
5 | 'methods' => WP_REST_Server::READABLE, |
---|
6 | 'callback' => 'wporg_login_rest_username_exists', |
---|
7 | 'permission_callback' => '__return_true', |
---|
8 | ) ); |
---|
9 | register_rest_route( 'wporg/v1', '/username-available/?', array( |
---|
10 | 'methods' => WP_REST_Server::READABLE, |
---|
11 | 'callback' => 'wporg_login_rest_username_exists', |
---|
12 | 'permission_callback' => '__return_true', |
---|
13 | ) ); |
---|
14 | |
---|
15 | register_rest_route( 'wporg/v1', '/email-in-use/(?P<email>.*)', array( |
---|
16 | 'methods' => WP_REST_Server::READABLE, |
---|
17 | 'callback' => 'wporg_login_rest_email_in_use', |
---|
18 | 'permission_callback' => '__return_true', |
---|
19 | ) ); |
---|
20 | register_rest_route( 'wporg/v1', '/email-in-use/?', array( |
---|
21 | 'methods' => WP_REST_Server::READABLE, |
---|
22 | 'callback' => 'wporg_login_rest_email_in_use', |
---|
23 | 'permission_callback' => '__return_true', |
---|
24 | ) ); |
---|
25 | |
---|
26 | register_rest_route( 'wporg/v1', '/resend-confirmation-email/?', array( |
---|
27 | 'methods' => WP_REST_Server::EDITABLE, |
---|
28 | 'callback' => 'wporg_login_rest_resend_confirmation_email', |
---|
29 | 'permission_callback' => '__return_true', |
---|
30 | ) ); |
---|
31 | } |
---|
32 | add_action( 'rest_api_init', 'wporg_login_rest_routes' ); |
---|
33 | |
---|
34 | function wporg_login_rest_username_exists( $request ) { |
---|
35 | $login = trim( urldecode( $request['login'] ) ); |
---|
36 | |
---|
37 | $validate_signup = wpmu_validate_user_signup( $login, 'placeholder@placeholder.domain' ); |
---|
38 | |
---|
39 | // We're going to enforce that you can't have a user_login which matches another users user_nicename.. just because sanity. |
---|
40 | if ( ($user = get_user_by( 'login', $login )) || ($user = get_user_by( 'slug', $login )) ) { |
---|
41 | return [ |
---|
42 | 'available' => false, |
---|
43 | 'error' => __( 'That username is already in use.', 'wporg' ) . '<br>' . |
---|
44 | __( 'Is it yours? <a href="/lostpassword">Reset your password</a>.', 'wporg' ), |
---|
45 | 'avatar' => get_avatar( $user, 64 ), |
---|
46 | ]; |
---|
47 | } |
---|
48 | |
---|
49 | // Check we don't have a pending registration for that username. |
---|
50 | if ( $pending = wporg_get_pending_user( $login ) ) { |
---|
51 | return [ |
---|
52 | 'available' => false, |
---|
53 | 'error' => __( 'That username is already in use.', 'wporg' ) . '<br>' . |
---|
54 | __( 'The registration is still pending, please check your email for the confirmation link.', 'wporg' ) . '<br>' . |
---|
55 | '<a href="#" class="resend">' . __( 'Resend confirmation email.', 'wporg' ) . '</a>', |
---|
56 | 'avatar' => get_avatar( $pending['user_email'], 64 ), |
---|
57 | ]; |
---|
58 | } |
---|
59 | |
---|
60 | // Perform general validations. |
---|
61 | $validate_signup_error = $validate_signup['errors']->get_error_message( 'user_name' ); |
---|
62 | |
---|
63 | if ( $validate_signup_error ) { |
---|
64 | return [ |
---|
65 | 'available' => false, |
---|
66 | 'error' => $validate_signup_error, |
---|
67 | 'avatar' => false, |
---|
68 | ]; |
---|
69 | } |
---|
70 | |
---|
71 | return [ 'available' => true ]; |
---|
72 | } |
---|
73 | |
---|
74 | function wporg_login_rest_email_in_use( $request ) { |
---|
75 | $email = trim( urldecode( $request['email'] ) ); |
---|
76 | |
---|
77 | if ( ! is_email( $email ) ) { |
---|
78 | return [ |
---|
79 | 'available' => false, |
---|
80 | 'error' => __( 'That email address appears to be invalid.', 'wporg' ), |
---|
81 | 'avatar' => false, |
---|
82 | ]; |
---|
83 | } |
---|
84 | |
---|
85 | if ( $user = get_user_by( 'email', $email ) ) { |
---|
86 | return [ |
---|
87 | 'available' => false, |
---|
88 | 'error' => __( 'That email address already has an account.', 'wporg' ) . '<br>' . |
---|
89 | __( 'Is it yours? <a href="/lostpassword">Reset your password</a>.', 'wporg' ), |
---|
90 | 'avatar' => get_avatar( $user, 64 ), |
---|
91 | ]; |
---|
92 | } |
---|
93 | |
---|
94 | // Check we don't have a pending registration for that email. |
---|
95 | $pending = wporg_get_pending_user( $email ); |
---|
96 | |
---|
97 | // And that there's no pending account signups for other emails for that inbox. |
---|
98 | if ( ! $pending && str_contains( $email, '+' ) ) { |
---|
99 | $pending = wporg_get_pending_user_by_email_wildcard( $email ); |
---|
100 | } |
---|
101 | |
---|
102 | if ( $pending ) { |
---|
103 | return [ |
---|
104 | 'available' => false, |
---|
105 | 'error' => __( 'That email address already has an account.', 'wporg' ) . '<br>' . |
---|
106 | __( 'The registration is still pending, please check your email for the confirmation link.', 'wporg' ) . '<br>' . |
---|
107 | '<a href="#" class="resend">' . __( 'Resend confirmation email.', 'wporg' ) . '</a>', |
---|
108 | 'avatar' => get_avatar( $email, 64 ), |
---|
109 | ]; |
---|
110 | } |
---|
111 | |
---|
112 | $validate_signup = wpmu_validate_user_signup( '', $email ); |
---|
113 | $validate_signup_error = $validate_signup['errors']->get_error_message( 'user_email' ); |
---|
114 | if ( $validate_signup_error ) { |
---|
115 | return [ |
---|
116 | 'available' => false, |
---|
117 | 'error' => $validate_signup_error, |
---|
118 | 'avatar' => false, |
---|
119 | ]; |
---|
120 | } |
---|
121 | |
---|
122 | return [ 'available' => true ]; |
---|
123 | } |
---|
124 | |
---|
125 | /* |
---|
126 | * Resend a confirmation email to create an account. |
---|
127 | * |
---|
128 | * This API intentionally doesn't report if it performs the action, always returning the success message. |
---|
129 | */ |
---|
130 | function wporg_login_rest_resend_confirmation_email( $request ) { |
---|
131 | $account = $request['account']; |
---|
132 | |
---|
133 | $success_message = sprintf( |
---|
134 | __( 'Please check your email %s for a confirmation link to set your password.', 'wporg' ), |
---|
135 | '<code>' . esc_html( $account ) . '</code>' |
---|
136 | ); |
---|
137 | |
---|
138 | $pending_user = wporg_get_pending_user( $request['account'] ); |
---|
139 | if ( ! $pending_user || $pending_user['created'] || ! $pending_user['user_activation_key'] ) { |
---|
140 | return $success_message; |
---|
141 | } |
---|
142 | |
---|
143 | // Allow for w.org plugins to block the action. |
---|
144 | if ( null !== ( $pre_register_error = apply_filters( 'wporg_login_pre_registration', null, $pending_user['user_login'], $pending_user['user_email'], $pending_user['meta']['user_mailinglist'] ) ) ) { |
---|
145 | return $success_message; |
---|
146 | } |
---|
147 | |
---|
148 | // Only one email per.. |
---|
149 | // - 1 minute for brand new accounts (<15min) |
---|
150 | // - 5 minutes for new accounts (<1hr) |
---|
151 | // - 3 hours there after |
---|
152 | list( $requested_time, ) = explode( ':', $pending_user['user_activation_key'] ); |
---|
153 | $time_limit = 3 * HOUR_IN_SECONDS; |
---|
154 | |
---|
155 | if ( time() - strtotime( $pending_user['user_registered'] ) < HOUR_IN_SECONDS ) { |
---|
156 | $time_limit = 5 * MINUTE_IN_SECONDS; |
---|
157 | } |
---|
158 | |
---|
159 | if ( time() - strtotime( $pending_user['user_registered'] ) < 15 * MINUTE_IN_SECONDS ) { |
---|
160 | $time_limit = MINUTE_IN_SECONDS; |
---|
161 | } |
---|
162 | |
---|
163 | if ( ( time() - $requested_time ) < $time_limit ) { |
---|
164 | return $success_message; |
---|
165 | } |
---|
166 | |
---|
167 | wporg_login_send_confirmation_email( $pending_user ); |
---|
168 | |
---|
169 | return $success_message; |
---|
170 | } |
---|