PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
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.16.0, at features/email-confirmation/email-confirmation.php

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