← All changes
|
features/email-confirmation/email-confirmation.php
+133
-25
3.9.8
→
4.0.3
View file →
| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | function wppb_signup_schema( $oldVal, $newVal ){ |
| 6 | 6 | // Declare these as global in case schema.php is included from a function. |
| 7 | 7 | global $wpdb, $wp_queries, $charset_collate; |
| 8 | 8 | |
| 9 | - if ($newVal['emailConfirmation'] == 'yes'){ | |
| 9 | + if ( !empty( $newVal['emailConfirmation'] ) && $newVal['emailConfirmation'] == 'yes'){ | |
| 10 | 10 | |
| 11 | 11 | //The database character collate. |
| 12 | 12 | $charset_collate = ''; |
| 13 | 13 | |
| @@ -16,10 +16,24 @@ | ||
| 16 | 16 | if ( ! empty( $wpdb->collate ) ) |
| 17 | 17 | $charset_collate .= " COLLATE ".$wpdb->collate; |
| 18 | 18 | $tableName = $wpdb->prefix.'signups'; |
| 19 | 19 | |
| 20 | + // If a legacy signups table exists without the signup_id column, add the | |
| 21 | + // AUTO_INCREMENT column and PRIMARY KEY together. dbDelta would otherwise | |
| 22 | + // split this into two ALTER statements and MySQL rejects an AUTO_INCREMENT | |
| 23 | + // column that isn't simultaneously declared a key. | |
| 24 | + $table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $tableName ) ); | |
| 25 | + if ( $table_exists ) { | |
| 26 | + $column_exists = $wpdb->get_results( "SHOW COLUMNS FROM `{$tableName}` LIKE 'signup_id'" ); | |
| 27 | + if ( empty( $column_exists ) ) { | |
| 28 | + $wpdb->query( "ALTER TABLE `{$tableName}` ADD COLUMN signup_id bigint(20) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (signup_id)" ); | |
| 29 | + update_option( 'wppb_signups_table_updated', 'yes' ); | |
| 30 | + } | |
| 31 | + } | |
| 32 | + | |
| 20 | 33 | $sql = " |
| 21 | 34 | CREATE TABLE $tableName ( |
| 35 | + signup_id bigint(20) NOT NULL AUTO_INCREMENT, | |
| 22 | 36 | domain varchar(191) NOT NULL default '', |
| 23 | 37 | path varchar(100) NOT NULL default '', |
| 24 | 38 | title longtext NOT NULL, |
| 25 | 39 | user_login varchar(60) NOT NULL default '', |
| @@ -28,8 +42,9 @@ | ||
| 28 | 42 | activated datetime NOT NULL default '0000-00-00 00:00:00', |
| 29 | 43 | active tinyint(1) NOT NULL default '0', |
| 30 | 44 | activation_key varchar(50) NOT NULL default '', |
| 31 | 45 | meta longtext, |
| 46 | + PRIMARY KEY (signup_id), | |
| 32 | 47 | KEY activation_key (activation_key), |
| 33 | 48 | KEY domain (domain) |
| 34 | 49 | ) $charset_collate;"; |
| 35 | 50 | |
| @@ -38,9 +53,68 @@ | ||
| 38 | 53 | } |
| 39 | 54 | } |
| 40 | 55 | add_action( 'update_option_wppb_general_settings', 'wppb_signup_schema', 10, 2 ); |
| 41 | 56 | |
| 57 | +// Function to update existing signups table to add primary key | |
| 58 | +function wppb_update_signup_table_schema() { | |
| 59 | + // Check if we've already updated the table | |
| 60 | + $table_updated = get_option('wppb_signups_table_updated', 'no'); | |
| 61 | + if ($table_updated === 'yes') { | |
| 62 | + return; // Table has already been updated, no need to run again | |
| 63 | + } | |
| 42 | 64 | |
| 65 | + global $wpdb; | |
| 66 | + $tableName = $wpdb->prefix.'signups'; | |
| 67 | + | |
| 68 | + // Check if the table exists | |
| 69 | + $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'"); | |
| 70 | + if (!$table_exists) { | |
| 71 | + return; // Table doesn't exist, nothing to update | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Check if email confirmation is enabled | |
| 75 | + $wppb_general_settings = get_option( 'wppb_general_settings', 'not_found' ); | |
| 76 | + if( $wppb_general_settings == 'not_found' || empty($wppb_general_settings['emailConfirmation']) || $wppb_general_settings['emailConfirmation'] != 'yes' ) { | |
| 77 | + return; // Email confirmation is not enabled, no need to update the table | |
| 78 | + } | |
| 79 | + | |
| 80 | + // Check if the signup_id column exists | |
| 81 | + $column_exists = $wpdb->get_results("SHOW COLUMNS FROM $tableName LIKE 'signup_id'"); | |
| 82 | + if (empty($column_exists)) { | |
| 83 | + // Column doesn't exist, add it with PRIMARY KEY in a single operation | |
| 84 | + $result = $wpdb->query("ALTER TABLE $tableName ADD COLUMN signup_id bigint(20) NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (signup_id)"); | |
| 85 | + | |
| 86 | + // Successfully updated the table | |
| 87 | + if ($result !== false) { | |
| 88 | + update_option('wppb_signups_table_updated', 'yes'); | |
| 89 | + } | |
| 90 | + } else { | |
| 91 | + // Column exists, check if it's a primary key | |
| 92 | + $primary_key = $wpdb->get_results("SHOW KEYS FROM $tableName WHERE Key_name = 'PRIMARY'"); | |
| 93 | + if (empty($primary_key)) { | |
| 94 | + // No primary key, add it | |
| 95 | + $result = $wpdb->query("ALTER TABLE $tableName ADD PRIMARY KEY (signup_id)"); | |
| 96 | + | |
| 97 | + // Successfully updated the table | |
| 98 | + if ($result !== false) { | |
| 99 | + update_option('wppb_signups_table_updated', 'yes'); | |
| 100 | + } | |
| 101 | + } else { | |
| 102 | + // Table already has primary key, mark as updated | |
| 103 | + update_option('wppb_signups_table_updated', 'yes'); | |
| 104 | + } | |
| 105 | + } | |
| 106 | +} | |
| 107 | + | |
| 108 | +// Run the update function only when the plugin is activated | |
| 109 | +function wppb_run_signup_table_update_on_activation() { | |
| 110 | + // Delete the option to force the update to run again on activation | |
| 111 | + delete_option('wppb_signups_table_updated'); | |
| 112 | + // Run the update function | |
| 113 | + wppb_update_signup_table_schema(); | |
| 114 | +} | |
| 115 | +register_activation_hook(__FILE__, 'wppb_run_signup_table_update_on_activation'); | |
| 116 | + | |
| 43 | 117 | //function to add new tab in the default WP userlisting with all the users who didn't confirm their account yet |
| 44 | 118 | function wppb_add_pending_users_header_script(){ |
| 45 | 119 | ?> |
| 46 | 120 | <script type="text/javascript"> |
| @@ -68,13 +142,28 @@ | ||
| 68 | 142 | alert( response.trim() ); |
| 69 | 143 | }); |
| 70 | 144 | } |
| 71 | 145 | } |
| 146 | + | |
| 147 | + jQuery(document).on('click', 'a.wppb-ec-action', function(e) { | |
| 148 | + e.preventDefault(); | |
| 149 | + var link = jQuery(this); | |
| 150 | + confirmECAction( | |
| 151 | + link.attr('data-url'), | |
| 152 | + link.attr('data-todo'), | |
| 153 | + link.attr('data-email'), | |
| 154 | + link.attr('data-message') | |
| 155 | + ); | |
| 156 | + }); | |
| 72 | 157 | </script> |
| 73 | 158 | <?php |
| 74 | 159 | } |
| 75 | 160 | |
| 76 | 161 | function wppb_get_unconfirmed_email_number(){ |
| 162 | + | |
| 163 | + if( !current_user_can( 'edit_users' ) ) | |
| 164 | + die(); | |
| 165 | + | |
| 77 | 166 | global $wpdb; |
| 78 | 167 | |
| 79 | 168 | /* since version 2.0.7 for multisite we add a 'registered_for_blog_id' meta in the registration process |
| 80 | 169 | 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 */ |
| @@ -111,12 +200,15 @@ | ||
| 111 | 200 | if ( count( $results ) != 1 ) |
| 112 | 201 | die( esc_html__( "There was an error performing that action!", "profile-builder" ) ); |
| 113 | 202 | |
| 114 | 203 | 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 ) ); | |
| 204 | + | |
| 205 | + if( !empty( $results[0]->user_login ) && !empty( $results[0]->user_email ) ){ | |
| 206 | + $sql_result = $wpdb->delete( $wpdb->base_prefix.'signups', array( 'user_login' => $results[0]->user_login, 'user_email' => $results[0]->user_email ) ); | |
| 207 | + } | |
| 208 | + | |
| 116 | 209 | if ( $sql_result ) |
| 117 | 210 | die( 'ok' ); |
| 118 | - | |
| 119 | 211 | else |
| 120 | 212 | die( esc_html__( "The selected user couldn't be deleted", "profile-builder" ) ); |
| 121 | 213 | |
| 122 | 214 | }elseif ( $todo == 'confirm' ){ |
| @@ -217,15 +309,9 @@ | ||
| 217 | 309 | case 'Upload':{ |
| 218 | 310 | if ( isset( $meta[$value['meta-name']] ) ){ |
| 219 | 311 | if( !empty( $meta[$value['meta-name']] ) ) { |
| 220 | 312 | 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 | - ) ); | |
| 313 | + wppb_save_attachment_id( trim( $meta[$value['meta-name']] ), $value, $user_id ); | |
| 228 | 314 | } else { |
| 229 | 315 | $wp_upload_array = wp_upload_dir(); // Array of key => value pairs |
| 230 | 316 | |
| 231 | 317 | $file = trim($meta[$value['meta-name']]); |
| @@ -254,15 +340,9 @@ | ||
| 254 | 340 | case 'Avatar':{ |
| 255 | 341 | if ( isset( $meta[$value['meta-name']] ) ) { |
| 256 | 342 | if( !empty( $meta[$value['meta-name']] ) ) { |
| 257 | 343 | 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 | - ) ); | |
| 344 | + wppb_save_attachment_id( trim( $meta[$value['meta-name']] ), $value, $user_id ); | |
| 265 | 345 | } else { |
| 266 | 346 | $wp_upload_array = wp_upload_dir(); // Array of key => value pairs |
| 267 | 347 | |
| 268 | 348 | $file = trim($meta[$value['meta-name']]); |
| @@ -324,8 +404,18 @@ | ||
| 324 | 404 | } |
| 325 | 405 | |
| 326 | 406 | update_user_meta( $user_id, $value['meta-name'], trim( $selected_values, ',' ) ); |
| 327 | 407 | } |
| 408 | + case 'GDPR Checkbox':{ | |
| 409 | + if ( isset($meta[wppb_handle_meta_name($value['meta-name'])]) ) { | |
| 410 | + 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') { | |
| 411 | + update_user_meta($user_id, $value['meta-name'], $meta[$value['meta-name']]); | |
| 412 | + if (isset($meta['gdpr_agreement_time']) && !empty($meta['gdpr_agreement_time'])) { | |
| 413 | + update_user_meta($user_id, 'gdpr_agreement_time', $meta['gdpr_agreement_time']); | |
| 414 | + } | |
| 415 | + } | |
| 416 | + } | |
| 417 | + } | |
| 328 | 418 | default: { |
| 329 | 419 | if ( isset( $meta[$value['meta-name']] ) ) { |
| 330 | 420 | update_user_meta($user_id, $value['meta-name'], $meta[$value['meta-name']]); |
| 331 | 421 | } |
| @@ -350,8 +440,13 @@ | ||
| 350 | 440 | $login_after_register = false; |
| 351 | 441 | } |
| 352 | 442 | $meta [ 'wppb_login_after_register_'.$meta['user_login'] ] = $login_after_register; |
| 353 | 443 | |
| 444 | + // save the gdpr_agreement_time if necessary | |
| 445 | + if ( array_key_exists('user_consent_gdpr', $meta) && $meta [ 'user_consent_gdpr' ] === 'agree' ) { | |
| 446 | + $meta [ 'gdpr_agreement_time' ] = time(); | |
| 447 | + } | |
| 448 | + | |
| 354 | 449 | // Format data |
| 355 | 450 | $user = sanitize_user( $username, true ); |
| 356 | 451 | if( is_multisite() ) |
| 357 | 452 | $user = preg_replace( '/\s+/', '', $user ); |
| @@ -471,15 +566,16 @@ | ||
| 471 | 566 | if ( !empty( $signup ) && !$signup->active ){ |
| 472 | 567 | $meta = unserialize( $signup->meta ); |
| 473 | 568 | $user_login = esc_sql( $signup->user_login ); |
| 474 | 569 | $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; | |
| 570 | + /* Signup meta holds the real password hash, applied after user creation. WordPress requires a non-empty user_pass when creating users. */ | |
| 571 | + $password = ''; | |
| 477 | 572 | |
| 478 | 573 | $user_id = username_exists($user_login); |
| 479 | 574 | |
| 480 | - if ( ! $user_id ) | |
| 481 | - $user_id = wppb_create_user( $user_login, $password, $user_email ); | |
| 575 | + if ( ! $user_id ) { | |
| 576 | + $user_id = wppb_create_user( $user_login, wp_generate_password( 24, true, true ), $user_email ); | |
| 577 | + } | |
| 482 | 578 | else |
| 483 | 579 | $user_already_exists = true; |
| 484 | 580 | |
| 485 | 581 | if ( !$user_id ) |
| @@ -494,8 +590,16 @@ | ||
| 494 | 590 | $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 | 591 | |
| 496 | 592 | wppb_add_meta_to_user_on_activation( $user_id, '', $meta ); |
| 497 | 593 | |
| 594 | + if( apply_filters( 'wppb_admin_email_confirmation_maybe_use_admin_approval', false ) ) { | |
| 595 | + // if admin approval is activated, then block the user until he gets approved | |
| 596 | + $wppb_generalSettings = get_option('wppb_general_settings'); | |
| 597 | + if( wppb_get_admin_approval_option_value() === 'yes' ){ | |
| 598 | + wppb_update_user_status_to_pending( $user_id, $wppb_generalSettings ); | |
| 599 | + } | |
| 600 | + } | |
| 601 | + | |
| 498 | 602 | /* copy the hashed password from signup meta to wp user table */ |
| 499 | 603 | if( !empty( $meta['user_pass'] ) ){ |
| 500 | 604 | /* we might still have the base64 encoded password in signups and not the hash */ |
| 501 | 605 | if( base64_encode(base64_decode($meta['user_pass'], true)) === $meta['user_pass'] ) |
| @@ -571,15 +675,19 @@ | ||
| 571 | 675 | |
| 572 | 676 | $adminApproval_mailAdmin = 0; |
| 573 | 677 | |
| 574 | 678 | 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; | |
| 679 | + | |
| 680 | + if( !empty( $user_data->roles ) ) { | |
| 681 | + foreach( $user_data->roles as $role ) { | |
| 682 | + if( in_array( $role, $wppb_generalSettings['adminApprovalOnUserRole'] ) ) { | |
| 683 | + if( ! current_user_can( 'delete_users' ) ) { | |
| 684 | + $adminApproval_mailAdmin = 1; | |
| 685 | + } | |
| 579 | 686 | } |
| 580 | 687 | } |
| 581 | 688 | } |
| 689 | + | |
| 582 | 690 | } else { |
| 583 | 691 | if( ! current_user_can( 'delete_users' ) ) { |
| 584 | 692 | $adminApproval_mailAdmin = 1; |
| 585 | 693 | } |