/
vmfeu-front-end-register.php
338 lines (271 loc) · 11.7 KB
/
vmfeu-front-end-register.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* Class to handle user registration
*
* @author verkkomuikku
*
*/
class Vmfeu_Register {
/**
* Contains user feedback messages
*
* @var array
*/
public $messages = array();
/**
* Constructor
*
*/
public function __construct() {
add_action('template_redirect', array(&$this, 'register_user'), 102);
add_action('template_redirect', array(&$this, 'generate_messages'), 105);
}
/**
* Do registration on init
*
* Registration is done before page loads.
* We need to pass some errors and other info forward until the form template is shown.
*
* If Multisite, the whole multisite signup thing is bypassed, no multisite
* activation etc. See wp-includes/ms-functions.php wpmu_activate_signup()
* and wpmu_create_user() for updates.
* WP 3.4.1
*
* @uses owelapro_transfer_messages()
*/
function register_user() {
global $uifieldset, $vmfeu;
// Check that we are on registration page
if (!is_page($vmfeu->registration_page) && !VMFEU_AJAX)
return;
// Form was sent with user info.
// Create new user if all fields validate.
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'vmfeu_add_user' && wp_verify_nonce($_POST['register_nonce_field'],'verify_true_registration') ) {
// Init fieldset only once
if (!$uifieldset)
$uifieldset = new Vmfeu_User_Info_Fieldset();
// Validate fields
$vmfeu->registration_errors = $uifieldset->validate();
if (!is_wp_error($vmfeu->registration_errors)) {
// Ok, no validation errors. Try to insert user
$vmfeu->registration_mail_status = NULL;
$vmfeu->user_registered = $uifieldset->insert_user($vmfeu->registration_mail_status);
if (is_wp_error($vmfeu->user_registered)) {
$vmfeu->registration_errors = $vmfeu->user_registered;
// Redirect to login page, unless an admin just added a user.
} elseif (!is_user_logged_in()) {
if (apply_filters('vmfeu_show_login_link_after_registration', true)) {
// Will add to owelapro messages to display the "Success" for the user
$this->generate_messages();
// Transfers the notifications over the redirect
$key = owelapro_transfer_messages();
// If user is not logged in (as is the case here), append the argument to be able
// to trasnfer the messages.
$url = add_query_arg(array('mskey' => $key), get_permalink($vmfeu->login_page));
wp_redirect($url);
exit();
}
}
}
}
}
/**
* Generate userfeedback messages
*
*/
public function generate_messages() {
global $vmfeu, $uifieldset;
// Don't bother
if (!is_page($vmfeu->registration_page)) {
return;
}
$messages = array();
$message_class = array();
if ( is_user_logged_in() && !current_user_can( 'create_users' ) ) {
// Display message for logged in users
// if they wind up into registration site (except admins)
global $user_ID;
$user = get_userdata( $user_ID );
$display_name = apply_filters('vmfeu_user_display_name', $user->display_name, $user);
$messages["already_logged_in"] = sprintf( __('You are logged in as <a href="%1$s" title="%2$s" rel="nofollow">%2$s</a>. You don\'t need another account.', 'verkkomuikku'), get_edit_profile_url( $user->ID ), $display_name ).' <a href="'.wp_logout_url( get_permalink() ).'" title="'.__('Log out of this account', 'verkkomuikku').'" rel="nofollow">'.__('Logout »', 'verkkomuikku').'</a>';
$message_class["already_logged_in"] = "logged-in";
// Registration was successfull
} elseif ( !is_wp_error($vmfeu->registration_errors) && $vmfeu->user_registered ) {
// New user was created
$user = get_userdata($vmfeu->user_registered);
$display_name = apply_filters('vmfeu_user_display_name', $user->display_name, $user);
if ( current_user_can( 'create_users' ) ) {
$messages["registration_success"] = sprintf($uifieldset->get_text("registration_success_admin"), $display_name );
$registration_url = wp_registration_url(); // get_permalink($vmfeu->registration_page);
$messages["registration_success"] .= '<br/><a href="'.$registration_url.'" rel="nofollow">'.__("Add another user", 'verkkomuikku').'</a>';
// Display notice, that account must be activated before they can log in.
} elseif($vmfeu->settings["require_account_activation"]) {
$messages["registration_success"] = sprintf($uifieldset->get_text("registration_success_user_activation_required"), $display_name );
} else {
$messages["registration_success"] = sprintf($uifieldset->get_text("registration_success_user"), $display_name );
// With Owela, the user is redirected to the login page so no need for the link
$messages["registration_success"] .= '<br/>'.__("You may now log in with your username and password.", "verkkomuikku");
/*
if (apply_filters('vmfeu_show_login_link_after_registration', true)) {
$login_url = get_permalink($vmfeu->login_page);
$messages["registration_success"] .= '<br/><a href="'.$login_url.'" rel="nofollow">'.__("Log in with your new user account", 'verkkomuikku').'</a>';
} else {
$front_page_url = get_bloginfo('home');
$messages["registration_success"] .= '<br/><a href="'.$front_page_url.'" rel="nofollow">'.__("To frontpage", 'verkkomuikku').'</a>';
}
*/
}
$message_class["registration_success"] = "success";
if(isset($_POST['send_password'])){
if (true === $vmfeu->registration_mail_status) {
if (current_user_can('create_users'))
$messages["password_sent"] = $uifieldset->get_text("registration_email_sent_admin");
// No need for this I believe
//else
// $messages["password_sent"] = $uifieldset->get_text("registration_email_sent_user");
$message_class["password_sent"] = "success";
} elseif (false === $vmfeu->registration_mail_status) {
if (current_user_can('create_users'))
$messages["password_sent"] = $uifieldset->get_text("registration_email_error_admin");
else
$messages["password_sent"] = $uifieldset->get_text("registration_email_error_user");
$message_class["password_sent"] = "error";
}
}
}
// Registration errors
if ( is_wp_error($vmfeu->registration_errors) ) {
$e = $vmfeu->registration_errors->get_error_messages();
$messages["registration_errors"] = implode('<br/>', $e);
$message_class["registration_errors"] = "error";
}
$this->messages["messages"] = $messages;
$this->messages["message_class"] = $message_class;
foreach ($messages as $name => $message) {
$class = isset($message_class[$name]) ? $message_class[$name] : 'notice';
owelapro_add_message($message, $class);
}
}
/**
* Displays registration form
*
* @param array $atts - standard WP shortcode atts, no use here
* @param string $content - standard WP shortcode $content. Put text that is displayed
* above form, but not anymore when user gets feedback about the sent form.
*
* @return string $form
*/
public function register_template($atts = array(), $content = ''){
global $uifieldset, $vmfeu;
$defaults = array(
"show_messages" => false,
);
extract(wp_parse_args($atts, $defaults), EXTR_SKIP);
// Let plugins change the $content
$content = apply_filters('vmfeu_registration_welcome_message', $content);
// Init fieldset only once
if (!$uifieldset)
$uifieldset = new Vmfeu_User_Info_Fieldset();
// Check if users can register.
$registration = get_option( 'users_can_register' );
// This is shortcode so we should return the form instead of echoing
ob_start();
?>
<div class="vmfeu_wrapper" id="vmfeu_register">
<?php
echo apply_filters('the_content', $content);
// Start echoing the messages. The following notices are also
// in the same <div wrapper
?>
<div class="messages">
<?php
// Show messages
if ($show_messages) {
$messages = $this->messages["messages"];
$message_class = $this->messages["message_class"];
foreach ($messages as $name => $message) {
$class = isset($message_class[$name]) ? $message_class[$name] : '';
echo '<p class="'.$class.'">'.$message.'</p>';
}
}
?>
<?php if ( current_user_can( 'create_users' ) && $registration ) : ?>
<p class="alert">
<?php
_e('Users can register themselves or you can manually create users here.', 'verkkomuikku');
if ($vmfeu->settings["require_account_activation"]) {
echo "<br/>".__('Users have to activate their account before they can log in. Admin created accounts aren\'t required activation.', 'verkkomuikku');
}
?>
</p>
<?php elseif ( current_user_can( 'create_users' ) ) : ?>
<p class="alert">
<?php _e('Users cannot currently register themselves, but you can manually create users here.', 'verkkomuikku'); ?>
</p>
<?php elseif ( !current_user_can( 'create_users' ) && !$registration) : ?>
<p class="alert">
<?php _e('Only an administrator can add new users.', 'verkkomuikku'); ?>
</p>
<?php endif; ?>
</div>
<?php if ((is_wp_error($vmfeu->registration_errors) || !$vmfeu->user_registered)) : ?>
<?php if ( ($registration && !is_user_logged_in()) || current_user_can( 'create_users' )) : ?>
<div class="registration-form">
<?php $action_url = apply_filters('vmfeu_registration_action_url', wp_registration_url()); ?>
<form method="post" id="vmfeu_add_user" class="user-forms" action="<?php echo $action_url; ?>">
<?php $mandatory_nag = $uifieldset->print_fields(); ?>
<?php
// Ask admin if he wants user password to be sent via email
// Default is yes
if ( current_user_can( 'delete_users' ) ) : ?>
<dl>
<dd> </dd>
<dt>
<input id="send_password" type="checkbox" name="send_password" value="1" <?php echo isset($_POST['send_password']) || !isset($_POST['vmfeu_add_user']) ? 'checked="checked"' : ''; ?>/>
<label for="send_password"><?php _e(' Send these credentials via email.', 'verkkomuikku') ?></label>
</dt>
</dl>
<?php
// Always send if user registers
else: ?>
<input type="hidden" name="send_password" value="1"/>
<?php endif; ?>
<dl>
<dd><?php echo $mandatory_nag; ?></dd>
<dt>
<input name="vmfeu_add_user" type="submit" id="vmfeu_add_user_submit" class="submit button" value="<?php echo current_user_can( 'create_users' ) ? $uifieldset->get_text("registration_button_add_user") : $uifieldset->get_text("registration_button_register"); ?>" />
<input name="action" type="hidden" id="action" value="vmfeu_add_user" />
</dt>
</dl>
<?php wp_nonce_field('verify_true_registration','register_nonce_field'); ?>
</form>
</div>
<?php endif; // current_user_can ('create_users') || $registration ?>
<?php endif; // User has just registered ?>
</div>
<?php
$form = ob_get_contents();
ob_end_clean();
return $form;
}
}
global $vmfeu_register;
$vmfeu_register = new Vmfeu_Register();
/**
* Shortcode handler to display registration form.
*
* @param array $atts - standard WP shortcode atts, no use here
* @param string $content - standard WP shortcode $content. Put text that is displayed
* above form, but not anymore when user gets feedback about the sent form.
*
* @return string $form
*/
function vmfeu_front_end_register($atts = array(), $content = ''){
global $vmfeu_register;
if (!is_a($vmfeu_register, 'Vmfeu_Register'))
$vmfeu_register = new Vmfeu_Register();
return $vmfeu_register->register_template($atts, $content);
}
?>