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