PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / features / email-confirmation / email-confirmation.php

email-confirmation.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at features/email-confirmation/email-confirmation.php

722 lines 33.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 //functions needed for the email-confirmation on single-sites (to create the "_signups" table)
5 function wppb_signup_schema( $oldVal, $newVal ){
6 // Declare these as global in case schema.php is included from a function.
7 global $wpdb, $wp_queries, $charset_collate;
8
9 if ( !empty( $wppb_general_settings['emailConfirmation'] ) && $newVal['emailConfirmation'] == 'yes'){
10
11 //The database character collate.
12 $charset_collate = '';
13
14 if ( ! empty( $wpdb->charset ) )
15 $charset_collate = "DEFAULT CHARACTER SET ".$wpdb->charset;
16 if ( ! empty( $wpdb->collate ) )
17 $charset_collate .= " COLLATE ".$wpdb->collate;
18 $tableName = $wpdb->prefix.'signups';
19
20 $sql = "
21 CREATE TABLE $tableName (
22 domain varchar(191) NOT NULL default '',
23 path varchar(100) NOT NULL default '',
24 title longtext NOT NULL,
25 user_login varchar(60) NOT NULL default '',
26 user_email varchar(100) NOT NULL default '',
27 registered datetime NOT NULL default '0000-00-00 00:00:00',
28 activated datetime NOT NULL default '0000-00-00 00:00:00',
29 active tinyint(1) NOT NULL default '0',
30 activation_key varchar(50) NOT NULL default '',
31 meta longtext,
32 KEY activation_key (activation_key),
33 KEY domain (domain)
34 ) $charset_collate;";
35
36 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
37 $res = dbDelta($sql);
38 }
39 }
40 add_action( 'update_option_wppb_general_settings', 'wppb_signup_schema', 10, 2 );
41
42
43 //function to add new tab in the default WP userlisting with all the users who didn't confirm their account yet
44 function wppb_add_pending_users_header_script(){
45 ?>
46 <script type="text/javascript">
47 jQuery(document).ready(function() {
48 jQuery.post( ajaxurl , { action:"wppb_get_unconfirmed_email_number"}, function(response) {
49 jQuery('.wrap ul.subsubsub').append('<span id="separatorID"> |</span> <li class="listUsersWithUncofirmedEmail"><a class="unconfirmedEmailUsers" href="?page=unconfirmed_emails"><?php esc_html_e('Users with Unconfirmed Email Address', 'profile-builder');?></a> <font id="unconfirmedEmailNo" color="grey">('+response.number+')</font></li>');
50 });
51 });
52
53 function confirmECActionBulk( URL, message ) {
54 if ( confirm(message) )
55 window.location=URL;
56 }
57
58 // script to create a confirmation box for the user upon approving/unapproving a user
59 function confirmECAction( URL, todo, user_email, actionText ) {
60 actionText = '<?php esc_html_e( 'Do you want to', 'profile-builder' ); ?>' + ' ' + actionText;
61
62 if (confirm(actionText)) {
63 jQuery.post( ajaxurl , { action:"wppb_handle_email_confirmation_cases", URL:URL, todo:todo, user_email:user_email, nonce:"<?php echo esc_js( wp_create_nonce( 'wppb_handle_email_confirmation' ) ) ?>" }, function(response) {
64 if (response.trim() == 'ok')
65 window.location=URL;
66
67 else
68 alert( response.trim() );
69 });
70 }
71 }
72 </script>
73 <?php
74 }
75
76 function wppb_get_unconfirmed_email_number(){
77 global $wpdb;
78
79 /* since version 2.0.7 for multisite we add a 'registered_for_blog_id' meta in the registration process
80 so we can count only the users registered on that blog. Also for backwards compatibility we display the users that don't have that meta at all */
81 $number_of_users = 0;
82 $results = $wpdb->get_results("SELECT * FROM ".$wpdb->base_prefix."signups WHERE active = 0");
83 foreach ($results as $result){
84 if( !empty( $result->meta ) ){
85 $user_meta = maybe_unserialize( $result->meta );
86 if( empty( $user_meta['registered_for_blog_id'] ) || $user_meta['registered_for_blog_id'] == get_current_blog_id() ){
87 $number_of_users++;
88 }
89 }
90 }
91
92 header( 'Content-type: application/json' );
93 die( json_encode( array( 'number' => $number_of_users ) ) );
94 }
95
96
97 function wppb_handle_email_confirmation_cases() {
98 global $wpdb;
99
100 if( !isset( $_POST['nonce'] ) || !wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'wppb_handle_email_confirmation' ) )
101 die( esc_html__("You either don't have permission for that action or there was an error!", "profile-builder") );
102
103 $todo = isset( $_POST['todo'] ) ? sanitize_text_field( $_POST['todo'] ) : '';
104 $user_email = isset( $_POST['user_email'] ) ? sanitize_email($_POST['user_email']) : '';
105
106 if ( current_user_can( apply_filters( 'wppb_email_confirmation_user_capability', 'manage_options' ) ) )
107 if ( ( $todo != '' ) && ( $user_email != '' ) ){
108
109 $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM " . $wpdb->base_prefix . "signups WHERE active = 0 AND user_email = %s", $user_email ) );
110
111 if ( count( $results ) != 1 )
112 die( esc_html__( "There was an error performing that action!", "profile-builder" ) );
113
114 elseif ( $todo == 'delete' ){
115 $sql_result = $wpdb->delete( $wpdb->base_prefix.'signups', array( 'user_login' => $results[0]->user_login, 'user_email' => $results[0]->user_email ) );
116 if ( $sql_result )
117 die( 'ok' );
118
119 else
120 die( esc_html__( "The selected user couldn't be deleted", "profile-builder" ) );
121
122 }elseif ( $todo == 'confirm' ){
123 die( wppb_manual_activate_signup( $results[0]->activation_key ) );/* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* escaped inside the function */
124
125 }elseif ( $todo == 'resend' ){
126 $sql_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_login = %s AND user_email = %s", $results[0]->user_login, $results[0]->user_email ), ARRAY_A );
127
128 if ( $sql_result ){
129 wppb_signup_user_notification( trim( $sql_result['user_login'] ), trim( $sql_result['user_email'] ), $sql_result['activation_key'], $sql_result['meta'] );
130
131 die( esc_html__( "Email notification resent to user", "profile-builder" ) );
132 }
133
134 }
135 }
136
137 die( esc_html__("You either don't have permission for that action or there was an error!", "profile-builder") );
138 }
139
140
141
142 // FUNCTIONS USED BOTH ON THE REGISTRATION PAGE AND THE EMAIL CONFIRMATION TABLE
143
144 // Hook to add AP user meta after signup autentification
145 add_action( 'wpmu_activate_user', 'wppb_add_meta_to_user_on_activation', 10, 3 );
146
147 //function that adds AP user meta after signup autentification and it is also used when editing the user uprofile
148 function wppb_add_meta_to_user_on_activation( $user_id, $password, $meta ){
149 $user = new WP_User( $user_id );
150
151 //copy the data from the meta field (default fields)
152 if( !empty( $meta['first_name'] ) )
153 update_user_meta( $user_id, 'first_name', $meta['first_name'] );
154 if( !empty( $meta['last_name'] ) )
155 update_user_meta( $user_id, 'last_name', $meta['last_name'] );
156 if( !empty( $meta['nickname'] ) )
157 update_user_meta( $user_id, 'nickname', $meta['nickname'] );
158 if( !empty( $meta['user_url'] ) )
159 wp_update_user(array('ID' => $user_id, 'user_url' => $meta['user_url']));
160 if( !empty( $meta['aim'] ) )
161 update_user_meta( $user_id, 'aim', $meta['aim'] );
162 if( !empty( $meta['yim'] ) )
163 update_user_meta( $user_id, 'yim', $meta['yim'] );
164 if( !empty( $meta['jabber'] ) )
165 update_user_meta( $user_id, 'jabber', $meta['jabber'] );
166 if( !empty( $meta['description'] ) )
167 update_user_meta( $user_id, 'description', $meta['description'] );
168
169 if( !empty( $meta['role'] ) )
170 $user->set_role( $meta['role'] ); //update the users role (s)he registered for
171
172 if( !empty( $meta['first_name'] ) && !empty( $meta['last_name'] ) )
173 wp_update_user(array('ID' => $user_id, 'display_name' => $meta['first_name'].' '.$meta['last_name'] ));
174 else if( !empty( $meta['nickname'] ) )
175 wp_update_user(array('ID' => $user_id, 'display_name' => $meta['nickname'] ));
176
177 //copy the data from the meta fields (custom fields)
178 $manage_fields = apply_filters( 'wppb_form_fields', get_option( 'wppb_manage_fields', 'not_set' ), array( 'meta' => $meta, 'user_id' => $user_id, 'context' => 'user_activation' ) );
179 if ( $manage_fields != 'not_set' ){
180 foreach ( $manage_fields as $key => $value){
181 switch ( $value['field'] ) {
182 case 'Input':
183 case 'Input (Hidden)':
184 case 'Checkbox':
185 case 'Checkbox (Terms and Conditions)':
186 case 'Radio':
187 case 'Select':
188 case 'Select (Country)':
189 case 'Select (Multiple)':
190 case 'Select (Timezone)':
191 case 'Datepicker':
192 case 'Textarea':{
193 if ( isset( $meta[$value['meta-name']] ) )
194 update_user_meta( $user_id, $value['meta-name'], trim( $meta[$value['meta-name']] ) );
195 break;
196 }
197 case 'Timepicker':{
198 if ( isset( $meta[$value['meta-name']] ) ) {
199 $time = $meta[$value['meta-name']];
200 if( !empty( $time['hours'] ) && !empty( $time['minutes'] ) ) {
201 update_user_meta( $user_id, $value['meta-name'], $time['hours'] . ':' . $time['minutes'] );
202 }
203
204 }
205 break;
206 }
207 case 'Map':{
208 if ( isset( $meta[$value['meta-name']] ) ) {
209 // Add new markers
210 if( is_array( $meta[$value['meta-name']] ) ) {
211 foreach( $meta[$value['meta-name']] as $key => $position )
212 update_user_meta( $user_id, $value['meta-name'] . '_' . $key, $position );
213 }
214 }
215 break;
216 }
217 case 'Upload':{
218 if ( isset( $meta[$value['meta-name']] ) ){
219 if( !empty( $meta[$value['meta-name']] ) ) {
220 if (is_numeric($meta[$value['meta-name']])) {
221 update_user_meta($user_id, $value['meta-name'], trim($meta[$value['meta-name']]));
222
223 // use this to update the post author to the correct user
224 wp_update_post( array(
225 'ID' => trim( $meta[$value['meta-name']] ),
226 'post_author' => $user_id
227 ) );
228 } else {
229 $wp_upload_array = wp_upload_dir(); // Array of key => value pairs
230
231 $file = trim($meta[$value['meta-name']]);
232 $file_name = substr($file, strpos($file, '_attachment_') + 12);
233
234 $random_user_number = apply_filters('wppb_register_wpmu_upload_random_user_number2', substr(md5($user->data->user_email), 0, 12), $user, $meta);
235
236 $old_path_on_disk = $wp_upload_array['basedir'] . '/profile_builder/attachments/' . substr($file, strpos($file, 'wpmuRandomID_'));
237 if (PHP_OS == "WIN32" || PHP_OS == "WINNT")
238 $old_path_on_disk = str_replace('\\', '/', $old_path_on_disk);
239
240 $new_path_on_disk = $wp_upload_array['basedir'] . '/profile_builder/attachments/userID_' . $user_id . '_attachment_' . $file_name;
241 if (PHP_OS == "WIN32" || PHP_OS == "WINNT")
242 $new_path_on_disk = str_replace('\\', '/', $new_path_on_disk);
243
244 if (rename($old_path_on_disk, $new_path_on_disk))
245 update_user_meta($user_id, $value['meta-name'], $wp_upload_array['baseurl'] . '/profile_builder/attachments/userID_' . $user_id . '_attachment_' . $file_name);
246 }
247 }
248 else{
249 update_user_meta( $user_id, $value['meta-name'], '' );
250 }
251 }
252 break;
253 }
254 case 'Avatar':{
255 if ( isset( $meta[$value['meta-name']] ) ) {
256 if( !empty( $meta[$value['meta-name']] ) ) {
257 if (is_numeric($meta[$value['meta-name']])) {
258 update_user_meta($user_id, $value['meta-name'], trim($meta[$value['meta-name']]));
259
260 // use this to update the post author to the correct user
261 wp_update_post( array(
262 'ID' => trim( $meta[$value['meta-name']] ),
263 'post_author' => $user_id
264 ) );
265 } else {
266 $wp_upload_array = wp_upload_dir(); // Array of key => value pairs
267
268 $file = trim($meta[$value['meta-name']]);
269 $file_name = substr($file, strpos($file, '_originalAvatar_') + 16);
270
271 $random_user_number = apply_filters('wppb_register_wpmu_avatar_random_user_number2', substr(md5($user->data->user_email), 0, 12), $user, $meta);
272
273 $old_path_on_disk = $wp_upload_array['basedir'] . '/profile_builder/avatars/' . substr($file, strpos($file, 'wpmuRandomID_'));
274 if (PHP_OS == "WIN32" || PHP_OS == "WINNT")
275 $old_path_on_disk = str_replace('\\', '/', $old_path_on_disk);
276
277 $new_path_on_disk = $wp_upload_array['basedir'] . '/profile_builder/avatars/userID_' . $user_id . '_originalAvatar_' . $file_name;
278 if (PHP_OS == "WIN32" || PHP_OS == "WINNT")
279 $new_path_on_disk = str_replace('\\', '/', $new_path_on_disk);
280
281 if (rename($old_path_on_disk, $new_path_on_disk)) {
282 $wp_filetype = wp_check_filetype(basename($file_name), null);
283 $attachment = array('post_mime_type' => $wp_filetype['type'],
284 'post_title' => $file_name,
285 'post_content' => '',
286 'post_status' => 'inherit'
287 );
288
289 $attach_id = wp_insert_attachment($attachment, $new_path_on_disk);
290
291 $avatar = image_downsize($attach_id, 'thumbnail');
292
293 update_user_meta($user_id, $value['meta-name'], $avatar[0]);
294 update_user_meta($user_id, 'avatar_directory_path_' . $value['id'], $new_path_on_disk);
295 update_user_meta($user_id, 'resized_avatar_' . $value['id'] . '_relative_path', $new_path_on_disk);
296 wppb_resize_avatar($user_id);
297 }
298 }
299 }
300 else{
301 update_user_meta( $user_id, $value['meta-name'], '' );
302 }
303 break;
304 }
305 }
306 case 'Default - Blog Details':{
307 if ( is_multisite() && isset( $meta['wppb_create_new_site_checkbox'] ) && $meta['wppb_create_new_site_checkbox'] == 'yes' ) {
308 $blog_url = $meta['wppb_blog_url'];
309 $blog_title = $meta['wppb_blog_title'];
310
311 $privacy['public'] = ( isset( $meta['wppb_blog_privacy'] ) && 'Yes' == $meta['wppb_blog_privacy'] ) ? true : false;
312 $blog_details = wpmu_validate_blog_signup( $blog_url, $blog_title );
313 if ( empty($blog_details['errors']->errors['blogname']) && empty($blog_details['errors']->errors['blog_title'])) {
314 wpmu_create_blog( $blog_details['domain'], $blog_details['path'], $blog_details['blog_title'], $user_id, $privacy );
315 }
316 }
317 }
318 case 'Select2 (Multiple)':{
319
320 $selected_values = '';
321 if (!empty($meta[wppb_handle_meta_name($value['meta-name'])]) && is_array($meta[wppb_handle_meta_name($value['meta-name'])])) {
322 foreach ($meta[wppb_handle_meta_name($value['meta-name'])] as $selected_key => $selected_value)
323 $selected_values .= sanitize_text_field($selected_value) . ',';
324 }
325
326 update_user_meta( $user_id, $value['meta-name'], trim( $selected_values, ',' ) );
327 }
328 default: {
329 if ( isset( $meta[$value['meta-name']] ) ) {
330 update_user_meta($user_id, $value['meta-name'], $meta[$value['meta-name']]);
331 }
332 do_action('wppb_add_meta_on_user_activation_' . Wordpress_Creation_Kit_PB::wck_generate_slug($value['field']), $user_id, $password, $meta, $value);
333 }
334 }
335 }
336 }
337 do_action( 'wppb_add_other_meta_on_user_activation', $user_id, $meta );
338 }
339
340
341 // function to add the new user to the signup table if email confirmation is selected as active or it is a wpmu installation
342 function wppb_signup_user( $username, $user_email, $login_after_register, $meta = '' ) {
343 global $wpdb;
344
345 // check for automatic login
346 // using case-insensitive string comparison to allow for both 'Yes' and 'yes'
347 if( strcasecmp($login_after_register, 'Yes') === 0 ) {
348 $login_after_register = true;
349 } else {
350 $login_after_register = false;
351 }
352 $meta [ 'wppb_login_after_register_'.$meta['user_login'] ] = $login_after_register;
353
354 // Format data
355 $user = sanitize_user( $username, true );
356 if( is_multisite() )
357 $user = preg_replace( '/\s+/', '', $user );
358
359 $user_email = sanitize_email( $user_email );
360 $activation_key = substr( md5( time() . rand() . $user_email ), 0, 16 );
361 $meta = serialize( $meta );
362
363 // change User Registered date and time according to timezone selected in WordPress settings
364 $wppb_get_date = wppb_get_register_date();
365
366 if( isset( $wppb_get_date ) ) {
367 $wppb_user_registered = $wppb_get_date;
368 } else {
369 $wppb_user_registered = current_time( 'mysql', true );
370 }
371
372 if ( is_multisite() )
373 $wpdb->insert( $wpdb->signups, array('domain' => '', 'path' => '', 'title' => '', 'user_login' => $user, 'user_email' => $user_email, 'registered' => $wppb_user_registered, 'activation_key' => $activation_key, 'meta' => $meta ) );
374 else
375 $wpdb->insert( $wpdb->prefix.'signups', array('domain' => '', 'path' => '', 'title' => '', 'user_login' => $user, 'user_email' => $user_email, 'registered' => $wppb_user_registered, 'activation_key' => $activation_key, 'meta' => $meta ) );
376
377 do_action ( 'wppb_signup_user', $username, $user_email, $activation_key, $meta );
378
379 wppb_signup_user_notification( $username, $user_email, $activation_key, $meta );
380 }
381
382 /**
383 * Notify user of signup success.
384 *
385 * Filter 'wppb_signup_user_notification_filter' to bypass this function or
386 * replace it with your own notification behavior.
387 *
388 * Filter 'wppb_signup_user_notification_email' and
389 * 'wppb_signup_user_notification_subject' to change the content
390 * and subject line of the email sent to newly registered users.
391 *
392 * @param string $user The user's login name.
393 * @param string $user_email The user's email address.
394 * @param array $meta By default, an empty array.
395 * @param string $activation_key The activation key created in wppb_signup_user()
396 * @return bool
397 */
398 function wppb_signup_user_notification( $user, $user_email, $activation_key, $meta = '' ) {
399 if ( !apply_filters( 'wppb_signup_user_notification_filter', $user, $user_email, $activation_key, $meta ) )
400 return false;
401
402 $wppb_general_settings = get_option( 'wppb_general_settings' );
403 $admin_email = get_site_option( 'admin_email' );
404
405 $from_name = apply_filters ( 'wppb_signup_user_notification_email_from_field', get_bloginfo( 'name' ) );
406
407 //we don't use this anymore do we ?
408 /*$message_headers = apply_filters ( 'wppb_signup_user_notification_from', "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n" );*/
409
410 if( isset( $wppb_general_settings['activationLandingPage'] ) && !empty( $wppb_general_settings['activationLandingPage'] ) ) {
411 $registration_page_url = add_query_arg( array('activation_key' => $activation_key), get_permalink( $wppb_general_settings['activationLandingPage'] ) );
412 } else {
413 $registration_page_url = 'not_set';
414 }
415
416 $registration_page_url = apply_filters( 'wppb_ec_landing_page', $registration_page_url, $user, $activation_key, $meta );
417
418 if ( $registration_page_url == 'not_set' ){
419 global $post;
420 if( !empty( $post->ID ) )
421 $permalink = get_permalink( $post->ID );
422 else
423 $permalink = get_bloginfo( 'url' );
424
425 if( !empty( $post->post_content ) )
426 $post_content = $post->post_content;
427 else
428 $post_content = '';
429
430 $registration_page_url = ( ( ( strpos( $post_content, '[wppb-register' ) !== false ) || ( strpos( $post_content, '<!-- wp:wppb/register' ) !== false ) ) ? add_query_arg( array( 'activation_key' => $activation_key ), $permalink ) : add_query_arg( array( 'activation_key' => $activation_key ), get_bloginfo( 'url' ) ) );
431 }
432
433 $wppb_generalSettings = get_option( 'wppb_general_settings' );
434
435 if( !empty( $wppb_generalSettings['loginWith'] ) && $wppb_generalSettings['loginWith'] == 'email' ) {
436 $display_username_email = $user_email;
437 }
438 else {
439 $display_username_email = $user;
440 }
441
442 $subject = sprintf( __( '[%1$s] Activate %2$s', 'profile-builder'), $from_name, $display_username_email );
443 $subject = apply_filters( 'wppb_signup_user_notification_email_subject', $subject, $user_email, $user, $activation_key, $registration_page_url, $meta, $from_name, 'wppb_user_emailc_registr_w_email_confirm_email_subject' );
444
445 $message = sprintf( __( "To activate your user, please click the following link:<br><br>%s%s%s<br><br>After you activate it you will receive yet *another email* with your login.", "profile-builder" ), '<a href="'.$registration_page_url.'">', $registration_page_url, '</a>' );
446 $message = apply_filters( 'wppb_signup_user_notification_email_content', $message, $user_email, $user, $activation_key, $registration_page_url, $meta, $from_name, 'wppb_user_emailc_registr_w_email_confirm_email_content' );
447
448 $message_context = 'email_user_activate';
449
450 wppb_mail( $user_email, $subject, $message, $from_name, $message_context );
451
452 return true;
453 }
454
455
456 /**
457 * Activate a signup.
458 *
459 *
460 * @param string $activation_key The activation key provided to the user.
461 * @return array An array containing information about the activated user and/or blog
462 */
463 function wppb_manual_activate_signup( $activation_key ) {
464 global $wpdb;
465
466 if ( is_multisite() )
467 $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $activation_key ) );
468 else
469 $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM ".$wpdb->prefix."signups WHERE activation_key = %s", $activation_key) );
470
471 if ( !empty( $signup ) && !$signup->active ){
472 $meta = unserialize( $signup->meta );
473 $user_login = esc_sql( $signup->user_login );
474 $user_email = esc_sql( $signup->user_email );
475 /* the password is in hashed form in the signup table and we will copy it later to the user */
476 $password = NULL;
477
478 $user_id = username_exists($user_login);
479
480 if ( ! $user_id )
481 $user_id = wppb_create_user( $user_login, $password, $user_email );
482 else
483 $user_already_exists = true;
484
485 if ( !$user_id )
486 return __( 'Could not create user!', 'profile-builder' );
487
488 elseif ( isset( $user_already_exists ) && ( $user_already_exists == true ) )
489 return __( 'That username is already activated!', 'profile-builder' );
490
491 else{
492 $now = current_time('mysql', true);
493
494 $retVal = ( is_multisite() ? $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key) ) : $wpdb->update( $wpdb->base_prefix.'signups', array('active' => 1, 'activated' => $now), array('activation_key' => $activation_key) ) );
495
496 wppb_add_meta_to_user_on_activation( $user_id, '', $meta );
497
498 /* copy the hashed password from signup meta to wp user table */
499 if( !empty( $meta['user_pass'] ) ){
500 /* we might still have the base64 encoded password in signups and not the hash */
501 if( base64_encode(base64_decode($meta['user_pass'], true)) === $meta['user_pass'] )
502 $meta['user_pass'] = wp_hash_password( $meta['user_pass'] );
503
504 $wpdb->update( $wpdb->users, array('user_pass' => $meta['user_pass'] ), array('ID' => $user_id) );
505 //This is required so that the APC cached data is updated with the new password. Thanks to @foliovision
506 wp_cache_delete( $user_id, 'users' );
507 }
508
509 wppb_notify_user_registration_email( get_bloginfo( 'name' ), $user_login, $user_email, 'sending', $password, ( wppb_get_admin_approval_option_value() === 'yes' ? 'yes' : 'no' ) );
510
511 do_action('wppb_activate_user', $user_id, $password, $meta);
512 return ( $retVal ? 'ok' : __( 'There was an error while trying to activate the user', 'profile-builder' ) );
513 }
514 }
515 }
516
517 /**
518 * Create a user.
519 *
520 * @param string $user_name The new user's login name.
521 * @param string $password The new user's password.
522 * @param string $email The new user's email address.
523 * @return mixed Returns false on failure, or int $user_id on success
524 */
525 function wppb_create_user( $user_name, $password, $email) {
526 if( is_email( $user_name ) )
527 $user_name = apply_filters( 'wppb_generated_random_username', Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $user_name ) ), $user_name );
528 else {
529 $user_name = sanitize_user($user_name, true);
530 if( is_multisite() )
531 $user_name = preg_replace( '/\s+/', '', $user_name );
532 }
533
534
535 $user_id = wp_create_user( $user_name, $password, $email );
536 if ( is_wp_error($user_id) )
537 return false;
538
539 // Newly created users have no roles or caps until they are added to a blog.
540 delete_user_option( $user_id, 'capabilities' );
541 delete_user_option( $user_id, 'user_level' );
542
543 do_action( 'wppb_new_user', $user_id );
544
545 return $user_id;
546 }
547
548 //send an email to the admin regarding each and every new subscriber, and - if selected - to the user himself
549 function wppb_notify_user_registration_email( $bloginfo, $user_name, $email, $send_credentials_via_email, $password, $adminApproval ){
550
551 /* if login with email is enabled user_name gets the value of the users email */
552 $wppb_general_settings = get_option( 'wppb_general_settings' );
553 if ( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ) {
554 $user_name = $email;
555 }
556
557 //send email to the admin
558 $message_from = apply_filters( 'wppb_register_from_email_message_admin_email', $bloginfo );
559
560 $message_subject = '['.$message_from.'] '.__( 'A new subscriber has (been) registered!', 'profile-builder' );
561 $message_subject = apply_filters ('wppb_register_admin_email_subject_without_admin_approval', $message_subject, $email, $password, $message_from, 'wppb_admin_emailc_default_registration_email_subject' );
562
563 $message_content = sprintf( __( 'New subscriber on %1$s.<br/><br/>Username:%2$s<br/>Email:%3$s<br/>', 'profile-builder'), $message_from, $user_name, $email );
564
565 $message_context = 'email_admin_new_subscriber';
566
567 if ( $adminApproval == 'yes' ) {
568 $user_data = get_user_by( 'email', $email );
569
570 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
571
572 $adminApproval_mailAdmin = 0;
573
574 if( $wppb_generalSettings != 'not_found' && ! empty( $wppb_generalSettings['adminApprovalOnUserRole'] ) ) {
575 foreach( $user_data->roles as $role ) {
576 if( in_array( $role, $wppb_generalSettings['adminApprovalOnUserRole'] ) ) {
577 if( ! current_user_can( 'delete_users' ) ) {
578 $adminApproval_mailAdmin = 1;
579 }
580 }
581 }
582 } else {
583 if( ! current_user_can( 'delete_users' ) ) {
584 $adminApproval_mailAdmin = 1;
585 }
586 }
587
588 if( $adminApproval_mailAdmin == 1 ) {
589 $message_subject = apply_filters( 'wppb_register_admin_email_subject_with_admin_approval', $message_subject, $email, $password, $message_from, 'wppb_admin_emailc_registration_with_admin_approval_email_subject' );
590
591 $message_content .= wppb_adminApproval_adminEmailContent();
592 $message_content = apply_filters( 'wppb_register_admin_email_message_with_admin_approval', $message_content, $email, $password, $message_from, 'wppb_admin_emailc_registration_with_admin_approval_email_content' );
593
594 $message_context = 'email_admin_approve';
595 }
596 else{
597 $message_content = apply_filters( 'wppb_register_admin_email_message_without_admin_approval', $message_content, $email, $password, $message_from, 'wppb_admin_emailc_default_registration_email_content' );
598 }
599 } else {
600 $message_content = apply_filters( 'wppb_register_admin_email_message_without_admin_approval', $message_content, $email, $password, $message_from, 'wppb_admin_emailc_default_registration_email_content' );
601 }
602
603 if ( trim( $message_content ) != '' ){
604 $admin_email = apply_filters('wppb_send_to_admin_email', get_option('admin_email'), get_user_by( 'email', $email ), $message_context);
605 wppb_mail( $admin_email, $message_subject, $message_content, $message_from, $message_context );
606 }
607
608
609
610 //send an email to the newly registered user, if this option was selected
611 if ( isset( $send_credentials_via_email ) && ( $send_credentials_via_email == 'sending' ) ){
612 $user_message_from = apply_filters( 'wppb_register_from_email_message_user_email', $bloginfo );
613
614 $user_message_subject = sprintf( __( '[%1$s] Your new account information', 'profile-builder' ), $user_message_from, $user_name, $password );
615 $user_message_subject = apply_filters( 'wppb_register_user_email_subject_without_admin_approval', $user_message_subject, $email, $password, $user_message_from, 'wppb_user_emailc_default_registration_email_subject' );
616
617 if ( $password === NULL ) {
618 $password = __( 'Your selected password at signup', 'profile-builder' );
619 }
620
621 $send_password = apply_filters( 'wppb_send_password_in_default_email_message', false );
622 $site_url = '<a href="'. home_url() .'"> ' . home_url() . ' </a>';
623 if( !$send_password )
624 $user_message_content = sprintf( __( 'Welcome to %1$s!<br/><br/><br/>Your username is: %2$s and your password is the one that you have selected during registration.<br/><br/>Access your account: %3$s ', 'profile-builder' ), $user_message_from, $user_name, $site_url );
625 else
626 $user_message_content = sprintf( __( 'Welcome to %1$s!<br/><br/><br/>Your username is: %2$s and the password: %3$s<br/><br/>Access your account: %4$s ', 'profile-builder' ), $user_message_from, $user_name, $password, $site_url );
627
628 if ( $password === __( 'Your selected password at signup', 'profile-builder' ) ) {
629 $password = NULL;
630 }
631
632 $user_message_context = 'email_user_account_info';
633
634 if ( $adminApproval == 'yes' ){
635 $user_data = get_user_by( 'email', $email );
636
637 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
638
639 $adminApproval_mailUser = 0;
640
641 if( $wppb_generalSettings != 'not_found' && ! empty( $wppb_generalSettings['adminApprovalOnUserRole'] ) ) {
642 foreach( $user_data->roles as $role ) {
643 if( in_array( $role, $wppb_generalSettings['adminApprovalOnUserRole'] ) ) {
644 if( ! current_user_can( 'delete_users' ) ) {
645 $adminApproval_mailUser = 1;
646 }
647 }
648 }
649 } else {
650 if( ! current_user_can( 'delete_users' ) ) {
651 $adminApproval_mailUser = 1;
652 }
653 }
654
655 if( $adminApproval_mailUser == 1 ) {
656 $user_message_subject = apply_filters( 'wppb_register_user_email_subject_with_admin_approval', $user_message_subject, $email, $password, $user_message_subject, 'wppb_user_emailc_registration_with_admin_approval_email_subject' );
657
658 $user_message_content .= wppb_adminApproval_userEmailContent();
659
660 $user_message_context = 'email_user_need_approval';
661
662 $user_message_content = apply_filters( 'wppb_register_user_email_message_with_admin_approval', $user_message_content, $email, $password, $user_message_subject, 'wppb_user_emailc_registration_with_admin_approval_email_content' );
663 }
664 else{
665 $user_message_content = apply_filters( 'wppb_register_user_email_message_without_admin_approval', $user_message_content, $email, $password, $user_message_subject, 'wppb_user_emailc_default_registration_email_content' );
666 }
667 } else
668 $user_message_content = apply_filters( 'wppb_register_user_email_message_without_admin_approval', $user_message_content, $email, $password, $user_message_subject, 'wppb_user_emailc_default_registration_email_content' );
669
670 $message_sent = wppb_mail( $email, $user_message_subject, $user_message_content, $user_message_from, $user_message_context );
671
672 return ( ( $message_sent ) ? 2 : 1 );
673 }
674 }
675
676
677 // function with content to be added in email sent to admin if "Admin Approval" is on
678 function wppb_adminApproval_adminEmailContent() {
679 $emailContent = '<br/>' . __( 'The "Admin Approval" feature was activated at the time of registration, so please remember that you need to approve this user before he/she can log in!', 'profile-builder') ."\r\n";
680
681 return $emailContent;
682 }
683
684
685 // function with content to be added in email sent to user if "Admin Approval" is on
686 function wppb_adminApproval_userEmailContent() {
687 $emailContent = '<br/><br/>' . __( 'Before you can access your account, an administrator needs to approve it. You will be notified via email.', 'profile-builder' );
688
689 return $emailContent;
690 }
691 // END FUNCTIONS USED BOTH ON THE REGISTRATION PAGE AND THE EMAIL CONFIRMATION TABLE
692
693
694 // Set up the AJAX hooks
695 add_action( 'wp_ajax_wppb_get_unconfirmed_email_number', 'wppb_get_unconfirmed_email_number' );
696 add_action( 'wp_ajax_wppb_handle_email_confirmation_cases', 'wppb_handle_email_confirmation_cases' );
697
698
699 $wppb_general_settings = get_option( 'wppb_general_settings', 'not_found' );
700 if( $wppb_general_settings != 'not_found' ){
701
702 if( !empty($wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ){
703 if ( is_multisite() ){
704 /* don't display on network admin */
705 if( isset($_SERVER['SCRIPT_NAME']) && strpos( sanitize_text_field( $_SERVER['SCRIPT_NAME'] ), 'users.php') && !is_network_admin() ){ //global $pagenow doesn't seem to work
706 add_action( 'admin_head', 'wppb_add_pending_users_header_script' );
707 }
708 }else{
709 global $pagenow;
710 // the Unconfirmed Email Address submenu page is added to profile.php if the user does not have the
711 // list_users capability so we also check for it
712 if ( $pagenow == 'users.php' || $pagenow == 'profile.php' ){
713 add_action( 'admin_head', 'wppb_add_pending_users_header_script' );
714 }
715 }
716
717 if ( defined( 'WPPB_PAID_PLUGIN_DIR' ) && file_exists ( WPPB_PAID_PLUGIN_DIR . '/features/admin-approval/admin-approval.php' ) )
718 add_action( 'user_register', 'wppb_update_user_status_on_admin_registration' );
719 }
720
721 }
722