/
vmfeu-front-end-lost-password.php
298 lines (248 loc) · 9.27 KB
/
vmfeu-front-end-lost-password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Class to handle the lost/retrieve password form
*
* @author verkkomuikku
*
*/
class Vmfeu_Lostpassword {
/**
* Status for lost password
*
* @var boolean
*/
public $lost_password = false;
/**
* Messages for user feedback
*
* @var array
*/
public $messages = array();
public function __construct() {
add_action('template_redirect', array(&$this, 'lost_password'));
add_action('init', array(&$this, 'generate_messages'));
}
/**
* Check request and do password reset
*
* Similar to what wp-login.php does.
*
* @uses vmfeu_retrieve_password()
* @uses vmfeu_check_password_reset_key()
* @uses vmfeu_reset_password()
*
*/
public function lost_password() {
global $wp_query, $vmfeu;
// Return if not in lost password page
if (!$wp_query || !$wp_query->queried_object || "page" != $wp_query->queried_object->post_type || $vmfeu->lost_password_page != $wp_query->queried_object->ID)
return;
// No password retrieval for logged in users
if (is_user_logged_in() && !current_user_can('create_users')) {
wp_redirect(home_url());
exit;
}
$http_post = ( 'POST' == $_SERVER['REQUEST_METHOD'] );
$action = $_REQUEST['action'];
switch ( $action ) {
case 'lostpassword' :
case 'retrievepassword' :
if ( $http_post ) {
$errors = vmfeu_retrieve_password();
if ( !is_wp_error( $errors ) ) {
$redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : $this->get_current_url( 'checkemail=confirm' );
wp_safe_redirect( $redirect_to );
exit();
}
}
if ( isset( $_REQUEST['error'] ) && 'invalidkey' == $_REQUEST['error'] )
$errors = new WP_error( 'invalidkey', __( 'Sorry, that key does not appear to be valid.', 'verkkomuikku' ) );
if (is_wp_error($errors)) {
$this->lost_password = $errors;
owelapro_add_message($errors, 'error');
}
break;
case 'resetpass' :
case 'rp' :
$user = vmfeu_check_password_reset_key($_GET['key'], $_GET['login']);
if ( is_wp_error($user) ) {
// Show user feedback
owelapro_add_message($errors, 'error');
owelapro_transfer_messages();
wp_redirect( site_url('wp-login.php?action=lostpassword&error=invalidkey') );
exit;
}
$errors = '';
if ( isset($_POST['pass1']) && $_POST['pass1'] != $_POST['pass2'] ) {
$errors = new WP_Error('password_reset_mismatch', __('The passwords do not match.'));
} elseif ( isset($_POST['pass1']) && !empty($_POST['pass1']) ) {
vmfeu_reset_password($user, $_POST['pass1']);
$redirect_to = $this->get_current_url( 'resetpass=complete' );
wp_safe_redirect( $redirect_to );
exit;
}
if (is_wp_error($errors)) {
$this->lost_password = $errors;
owelapro_add_message($errors, 'error');
}
break;
default:
break;
}
}
/**
* Helper function to get current url
*
* @param string $query
*
* @return string $url
*/
public function get_current_url( $query = '' ) {
$url = remove_query_arg( array( 'instance', 'action', 'checkemail', 'error', 'loggedout', 'registered', 'redirect_to', 'updated', 'key', '_wpnonce', 'reauth', 'login' ) );
if ( !empty( $query ) ) {
$r = wp_parse_args( $query );
foreach ( $r as $k => $v ) {
if ( strpos( $v, ' ' ) !== false )
$r[$k] = rawurlencode( $v );
}
$url = add_query_arg( $r, $url );
}
return $url;
}
/**
* Check if certain parameters are as argument in the url to display user feedback.
*
*/
public function generate_messages() {
$messages = array();
if (isset($_GET['checkemail']) && "confirm" == $_GET['checkemail']) {
$messages[] = __("Check your email for password change confirmation link.", 'verkkomuikku');
} elseif (isset($_GET['resetpass']) && "complete" == $_GET['resetpass']) {
$messages[] = __("Password changed! You may now log in with your new password.", 'verkkomuikku');
}
$this->messages = $messages;
return;
}
/**
* Lost password form - copied from wp-login.php
*
*/
public function lost_password_template() {
// Flag used for checking if login and register links should be shown
$show_nav = false;
// Display message to check email if user already sent the lostpassword form.
if (!empty($this->messages)) {
if (isset($this->messages["resetpass"]))
$show_nav = true;
$messages = $this->messages;
} else { ?>
<div class="lost-password-form">
<form name="lostpasswordform" id="lostpasswordform" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" method="post">
<label class="input">
<span><?php _e('Your username or e-mail', 'verkkomuikku') ?></span>
<input type="text" name="user_login" id="user_login" class="input" value="<?php echo esc_attr($user_login); ?>" size="30" tabindex="<?php echo apply_filters('vmfeu_tabindex', 1); ?>" />
</label>
<input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php esc_attr_e('Get New Password'); ?>" tabindex="<?php echo apply_filters('vmfeu_tabindex', 2); ?>" />
<input type="hidden" name="redirect_to" value="<?php echo esc_attr( $redirect_to ); ?>" />
<?php do_action('lostpassword_form'); ?>
</form>
</div>
<?php } // endif; ?>
<?php if ($show_nav || !empty($messages)) : ?>
<div class="messages">
<?php
if (!empty($messages)) {
foreach ($messages as $message)
echo '<p class="success">'.$message.'</p>';
}
?>
<?php if ($show_nav) : ?>
<p id="nav">
<a href="<?php echo site_url('wp-login.php', 'login') ?>"><?php _e('Log in', 'verkkomuikku') ?></a>
<?php if (get_option('users_can_register')) : ?>
| <a href="<?php echo site_url('wp-login.php?action=register', 'login') ?>"><?php _e('Register', 'verkkomuikku') ?></a>
<?php endif; ?>
</p>
<?php endif; ?>
</div>
<?php endif;
}
/**
* Reset password form template - copied from wp-login.php
*
*/
public function reset_password_template() {
?>
<div class="reset-password-form">
<form name="resetpassform" id="resetpassform" action="<?php echo site_url('wp-login.php?action=resetpass&key=' . urlencode($_GET['key']) . '&login=' . urlencode($_GET['login']), 'login_post') ?>" method="post">
<input type="hidden" id="user_login" value="<?php echo esc_attr( $_GET['login'] ); ?>" autocomplete="off" />
<?php echo sprintf(__("Change password for user %s", 'verkkomuikku'), htmlspecialchars($_GET['login'], ENT_COMPAT, 'UTF-8')); ?>
<label class="input">
<span><?php _e('New password', 'verkkomuikku') ?></span>
<input type="password" name="pass1" id="pass1" class="input" size="20" value="" autocomplete="off" tabindex="<?php echo apply_filters('vmfeu_tabindex', 1); ?>"/>
</label>
<label class="input">
<span><?php _e('Confirm new password', 'verkkomuikku') ?></span>
<input type="password" name="pass2" id="pass2" class="input" size="20" value="" autocomplete="off" tabindex="<?php echo apply_filters('vmfeu_tabindex', 2); ?>"/>
</label>
<?php /*?>
<div id="pass-strength-result" class="hide-if-no-js"><?php _e('Strength indicator', 'verkkomuikku'); ?></div>
<p class="description indicator-hint"><?php _e('Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).', 'verkkomuikku'); ?></p>
<?php */ ?>
<input type="submit" name="wp-submit" id="wp-submit" class="button-primary" value="<?php _e('Change password', 'verkkomuikku'); ?>" tabindex="<?php echo apply_filters('vmfeu_tabindex', 3); ?>" />
</form>
</div>
<?php
}
}
global $vmfeu_lostpassword;
$vmfeu_lostpassword = new Vmfeu_Lostpassword();
/**
* Shortcode handler to display lost password / reset password form.
*
* @param array $atts - standard WP shortcode atts, no use here
* @param string $content - standard WP shortcode $content, no use here
*
* @return string $form HTML
*/
function vmfeu_front_end_lost_password($atts = array(), $content = ''){
global $vmfeu_lostpassword;
$defaults = array(
"show_errors" => true,
);
extract(wp_parse_args($atts, $defaults), EXTR_SKIP);
if (!is_a($vmfeu_lostpassword, "Vmfeu_Lostpassword"))
$vmfeu_lostpassword = new Vmfeu_Lostpassword();
$action = $_REQUEST['action'];
// This is shortcode so return the form, not echo
ob_start();
?>
<div class="vmfeu_wrapper" id="vmfeu_lostpassword">
<?php
// Display errors
if ($show_errors && is_wp_error($vmfeu_lostpassword->lost_password)) {
if (is_wp_error($vmfeu_lostpassword->lost_password)) {
?>
<div class="messages">
<p class="error">
<?php echo implode('<br/><br/>', $vmfeu_lostpassword->lost_password->get_error_messages()); ?>
</p>
</div>
<?php
}
}
// Display lost password or reset password form
// Lost password form template is shown for confirmation, check email etc.
if (empty($action) || $action == 'lostpassword' || $action == 'retrievepassword')
$vmfeu_lostpassword->lost_password_template();
else if ($action == 'resetpass' || $action == 'rp')
$vmfeu_lostpassword->reset_password_template();
?>
</div>
<?php
$form = ob_get_contents();
ob_end_clean();
return $form;
}