| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/** |
| 5 |
* Function that changes the auto generated password with the one selected by the user. |
| 6 |
*/ |
| 7 |
function wppb_signup_password_random_password_filter( $password ) { |
| 8 |
global $wpdb; |
| 9 |
|
| 10 |
$key = ( !empty( $_GET['key'] ) ? sanitize_text_field( $_GET['key'] ) : null ); |
| 11 |
$key = ( !empty( $_POST['key'] ) ? sanitize_text_field( $_POST['key'] ) : $key ); |
| 12 |
|
| 13 |
if ( !is_null( $key ) ) { |
| 14 |
$signup = ( is_multisite() ? $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->signups . " WHERE activation_key = %s", $key ) ) : $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->base_prefix . "signups WHERE activation_key = %s", $key ) ) ); |
| 15 |
|
| 16 |
if ( empty( $signup ) || $signup->active ) { |
| 17 |
//bad key or already active |
| 18 |
} else { |
| 19 |
//check for password in signup meta |
| 20 |
$meta = unserialize( $signup->meta ); |
| 21 |
|
| 22 |
if ( !empty($meta['user_pass']) ) |
| 23 |
$password = $meta['user_pass']; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return apply_filters( 'wppb_generated_random_password', $password, $key ); |
| 28 |
} |
| 29 |
add_filter( 'random_password', 'wppb_signup_password_random_password_filter' ); |
| 30 |
|
| 31 |
/** |
| 32 |
* Activate a signup. |
| 33 |
* |
| 34 |
* |
| 35 |
* @param string $key The activation key provided to the user. |
| 36 |
* @return array An array containing information about the activated user and/or blog |
| 37 |
*/ |
| 38 |
function wppb_activate_signup( $key ) { |
| 39 |
global $wpdb; |
| 40 |
$bloginfo = get_bloginfo( 'name' ); |
| 41 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 42 |
|
| 43 |
$login_after_register = ( isset( $wppb_general_settings['automaticallyLogIn'] ) ? $wppb_general_settings['automaticallyLogIn'] : apply_filters( 'wppb_automatically_login_after_register', 'No' ) ); |
| 44 |
|
| 45 |
$signup = ( is_multisite() ? $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) ) : $wpdb->get_row( $wpdb->prepare( "SELECT * FROM ".$wpdb->base_prefix."signups WHERE activation_key = %s", $key ) ) ); |
| 46 |
|
| 47 |
if( empty( $signup ) ) |
| 48 |
return apply_filters( 'wppb_register_activate_user_error_message6', '<p class="error" role="alert">'.__( 'Could not find registration. Contact administrator.', 'profile-builder' ).'</p>'); |
| 49 |
|
| 50 |
$user_login = ( ( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ) ? trim( $signup->user_email ) : trim( $signup->user_login ) ); |
| 51 |
|
| 52 |
$user_email = esc_sql( $signup->user_email ); |
| 53 |
/* Signup meta holds the real password hash, applied after user creation. WordPress requires a non-empty user_pass when creating users. */ |
| 54 |
$password = ''; |
| 55 |
|
| 56 |
$user_id = ( ( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ) ? email_exists( $user_login ) : username_exists( $user_login ) ); |
| 57 |
|
| 58 |
if ( empty( $signup ) ) |
| 59 |
return apply_filters( 'wppb_register_activate_user_error_message1', '<p class="error" role="alert">'.__( 'Invalid activation key!', 'profile-builder' ).'</p>'); |
| 60 |
|
| 61 |
if ( $signup->active ) |
| 62 |
if ( empty( $signup->domain ) ) |
| 63 |
return apply_filters( 'wppb_register_activate_user_error_message2', '<p class="wppb-success" role="alert">'.__( 'This username is now active!', 'profile-builder' ).'</p>', $user_id ); |
| 64 |
|
| 65 |
$meta = unserialize( $signup->meta ); |
| 66 |
|
| 67 |
if( strcasecmp($login_after_register, 'Yes') === 0 ) { |
| 68 |
$login_after_register = true; |
| 69 |
} elseif( isset( $meta [ 'wppb_login_after_register_'.$meta['user_login'] ] ) ) { |
| 70 |
$login_after_register = $meta [ 'wppb_login_after_register_'.$meta['user_login'] ]; |
| 71 |
unset( $meta [ 'wppb_login_after_register_'.$meta['user_login'] ] ); |
| 72 |
} else { |
| 73 |
$login_after_register = false; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! $user_id ) { |
| 77 |
$user_id = wppb_create_user( $user_login, wp_generate_password( 24, true, true ), $user_email ); |
| 78 |
} |
| 79 |
else |
| 80 |
$user_already_exists = true; |
| 81 |
|
| 82 |
if ( ! $user_id ) |
| 83 |
return apply_filters( 'wppb_register_activate_user_error_message4', '<p class="error" role="alert">'.__('Could not create user!', 'profile-builder').'</p>' ); |
| 84 |
|
| 85 |
elseif ( isset( $user_already_exists ) && ( $user_already_exists == true ) ) |
| 86 |
return apply_filters( 'wppb_register_activate_user_error_message5', '<p class="error" role="alert">'.__( 'This username is already activated!', 'profile-builder' ).'</p>' ); |
| 87 |
|
| 88 |
else{ |
| 89 |
$inserted_user = ( is_multisite() ? $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => current_time( 'mysql', true ) ), array( 'activation_key' => $key ) ) : $wpdb->update( $wpdb->base_prefix.'signups', array( 'active' => 1, 'activated' => current_time( 'mysql', true ) ), array( 'activation_key' => $key ) ) ); |
| 90 |
|
| 91 |
wppb_add_meta_to_user_on_activation( $user_id, '', $meta ); |
| 92 |
|
| 93 |
// if admin approval is activated, then block the user until he gets approved |
| 94 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 95 |
if( wppb_get_admin_approval_option_value() === 'yes' ){ |
| 96 |
wppb_update_user_status_to_pending( $user_id, $wppb_generalSettings ); |
| 97 |
} |
| 98 |
|
| 99 |
if ( !isset( $wppb_generalSettings['adminApproval'] ) ) |
| 100 |
$wppb_generalSettings['adminApproval'] = 'no'; |
| 101 |
|
| 102 |
/* copy the hashed password from signup meta to wp user table */ |
| 103 |
if( !empty( $meta['user_pass'] ) ){ |
| 104 |
/* we might still have the base64 encoded password in signups and not the hash */ |
| 105 |
if( base64_encode(base64_decode($meta['user_pass'], true)) === $meta['user_pass'] ) |
| 106 |
$meta['user_pass'] = wp_hash_password( $meta['user_pass'] ); |
| 107 |
|
| 108 |
$wpdb->update( $wpdb->users, array('user_pass' => $meta['user_pass'] ), array('ID' => $user_id) ); |
| 109 |
wp_cache_delete( $user_id, 'users' ); |
| 110 |
} |
| 111 |
|
| 112 |
wppb_notify_user_registration_email($bloginfo, $user_login, $user_email, 'sending', $password, wppb_get_admin_approval_option_value() ); |
| 113 |
|
| 114 |
do_action( 'wppb_activate_user', $user_id, $password, $meta ); |
| 115 |
|
| 116 |
if( $inserted_user ) { |
| 117 |
// CHECK FOR REDIRECT |
| 118 |
$redirect_url = wppb_get_redirect_url( 'normal', 'after_success_email_confirmation', '', $user_login ); |
| 119 |
|
| 120 |
$redirect_delay = apply_filters( 'wppb_success_email_confirmation_redirect_delay', 3, $user_id ); |
| 121 |
$redirect_message = wppb_build_redirect( $redirect_url, $redirect_delay, 'after_success_email_confirmation' ); |
| 122 |
$redirect_message = apply_filters( 'wppb_ec_sucess_message_redirect', $redirect_message, $meta ); |
| 123 |
|
| 124 |
$success_message = apply_filters( 'wppb_success_email_confirmation', '<p class="wppb-success" role="alert">' . __( 'Your email was successfully confirmed.', 'profile-builder' ) . '</p><!-- .success -->', $user_id ); |
| 125 |
$admin_approval_message = apply_filters( 'wppb_email_confirmation_with_admin_approval', '<p class="alert" role="alert">' . __( 'Before you can access your account, an administrator needs to approve it. You will be notified via email.', 'profile-builder' ) . '</p>', $user_id ); |
| 126 |
|
| 127 |
$wppb_general_settings = get_option( 'wppb_general_settings', 'false' ); |
| 128 |
|
| 129 |
if ( wppb_get_admin_approval_option_value() === 'yes' ){ |
| 130 |
$user_data = get_userdata( $user_id ); |
| 131 |
|
| 132 |
if( $wppb_general_settings != 'not_found' && ! empty( $wppb_general_settings['adminApprovalOnUserRole'] ) ) { |
| 133 |
foreach( $user_data->roles as $role ) { |
| 134 |
if( in_array( $role, $wppb_general_settings['adminApprovalOnUserRole'] ) ) { |
| 135 |
return $success_message . $admin_approval_message . ( ! empty ( $redirect_message ) ? $redirect_message : '' ); |
| 136 |
} else { |
| 137 |
wp_set_object_terms( $user_id, NULL, 'user_status' ); |
| 138 |
clean_object_term_cache( $user_id, 'user_status' ); |
| 139 |
|
| 140 |
if( $login_after_register ) { |
| 141 |
if( empty( $redirect_url ) ) { |
| 142 |
$redirect_url = wppb_curpageurl(); |
| 143 |
} |
| 144 |
$redirect_message = wppb_activate_signup_autologin_redirect_url($user_id, $redirect_url, $redirect_delay); |
| 145 |
$redirect_message = apply_filters( 'wppb_ec_sucess_message_redirect', $redirect_message, $meta ); |
| 146 |
} |
| 147 |
|
| 148 |
return $success_message . ( ! empty ( $redirect_message ) ? $redirect_message : '' ); |
| 149 |
} |
| 150 |
} |
| 151 |
} else { |
| 152 |
return $success_message . $admin_approval_message . ( ! empty ( $redirect_message ) ? $redirect_message : '' ); |
| 153 |
} |
| 154 |
} else { |
| 155 |
wp_set_object_terms( $user_id, NULL, 'user_status' ); |
| 156 |
clean_object_term_cache( $user_id, 'user_status' ); |
| 157 |
|
| 158 |
if( $login_after_register ) { |
| 159 |
if( empty( $redirect_url ) ) { |
| 160 |
$redirect_url = wppb_curpageurl(); |
| 161 |
} |
| 162 |
$redirect_message = wppb_activate_signup_autologin_redirect_url($user_id, $redirect_url, $redirect_delay); |
| 163 |
$redirect_message = apply_filters( 'wppb_ec_sucess_message_redirect', $redirect_message, $meta ); |
| 164 |
} |
| 165 |
|
| 166 |
return $success_message . ( ! empty ( $redirect_message ) ? $redirect_message : '' ); |
| 167 |
} |
| 168 |
} else { |
| 169 |
return apply_filters('wppb_register_failed_user_activation', '<p class="error" role="alert">'. __('There was an error while trying to activate the user.', 'profile-builder') .'</p><!-- .error -->'); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
//function that generates the redirect message when the user should be automatically logged in |
| 175 |
function wppb_activate_signup_autologin_redirect_url( $user_id, $redirect_url, $redirect_delay ){ |
| 176 |
$redirect_url = remove_query_arg( 'activation_key' ,$redirect_url ); |
| 177 |
|
| 178 |
$redirect_url = apply_filters( 'wppb_login_after_reg_redirect_url', $redirect_url ); |
| 179 |
$redirect_url = add_query_arg( wppb_get_autologin_query_args( $user_id ), $redirect_url ); |
| 180 |
|
| 181 |
return wppb_build_redirect( $redirect_url, $redirect_delay, 'after_success_email_confirmation' ); |
| 182 |
} |
| 183 |
|
| 184 |
//function to display the registration page |
| 185 |
function wppb_front_end_register( $atts ){ |
| 186 |
|
| 187 |
$atts = shortcode_atts( array( |
| 188 |
'role' => get_option( 'default_role' ), |
| 189 |
'form_name' => 'unspecified', |
| 190 |
'redirect_url' => '', |
| 191 |
'logout_redirect_url' => '', |
| 192 |
'automatic_login' => '', |
| 193 |
'redirect_priority' => 'normal', |
| 194 |
'ajax' => false |
| 195 |
), $atts, 'wppb-register' ); |
| 196 |
|
| 197 |
$form = new Profile_Builder_Form_Creator( array( 'form_type' => 'register', 'form_name' => $atts['form_name'], 'role' => ( is_object( get_role( $atts['role'] ) ) ? $atts['role'] : get_option( 'default_role' ) ) , 'redirect_url' => $atts['redirect_url'], 'logout_redirect_url' => $atts['logout_redirect_url'], 'automatic_login' => $atts['automatic_login'], 'redirect_priority' => $atts['redirect_priority'], 'ajax' => $atts['ajax'] ) ); |
| 198 |
|
| 199 |
return $form; |
| 200 |
} |
| 201 |
|
| 202 |
// function to choose whether to display the registration page or the validation message |
| 203 |
function wppb_front_end_register_handler( $atts ){ |
| 204 |
return ( isset( $_GET['activation_key'] ) ? wppb_activate_signup ( sanitize_text_field( $_GET['activation_key'] ) ) : wppb_front_end_register( $atts ) ); |
| 205 |
} |
| 206 |
|
| 207 |
add_action( 'user_register', 'wppbc_disable_admin_approval_for_user_role', 99, 1 ); |
| 208 |
function wppbc_disable_admin_approval_for_user_role( $user_id ) { |
| 209 |
if ( current_user_can( 'delete_users' ) ) { |
| 210 |
wp_set_object_terms( $user_id, NULL, 'user_status' ); |
| 211 |
clean_object_term_cache( $user_id, 'user_status' ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/* authors and contributors shouldn't be allowed to create pages with the register shortcode in them */ |
| 216 |
add_filter( 'the_content', 'wppb_maybe_remove_register_shortcode' ); |
| 217 |
function wppb_maybe_remove_register_shortcode( $content ){ |
| 218 |
if ( has_shortcode( $content, 'wppb-register' ) ){ |
| 219 |
$author_id = get_the_author_meta( 'ID' ); |
| 220 |
if( !empty( $author_id ) ){ |
| 221 |
if( !user_can( $author_id, 'edit_others_posts' ) ) { |
| 222 |
remove_shortcode('wppb-register'); |
| 223 |
|
| 224 |
$content = str_replace('[wppb-register]', __( 'Only an administrator can create pages with the register shortcode.', 'profile-builder' ), $content); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return $content; |
| 230 |
} |
| 231 |
|
| 232 |
/* custom redirect after registration on wp default register form */ |
| 233 |
function wppb_default_registration_redirect( $user_id ) { |
| 234 |
|
| 235 |
$user_data = get_userdata( $user_id ); |
| 236 |
|
| 237 |
// CHECK FOR REDIRECT |
| 238 |
if( isset( $_POST['redirect_to'] ) ) |
| 239 |
$_POST['redirect_to'] = apply_filters( 'wppb_after_registration_redirect_url', wppb_get_redirect_url( 'normal', 'after_registration', wppb_sanitize_request_url( $_POST['redirect_to'] ), $user_data ) ); |
| 240 |
|
| 241 |
} |
| 242 |
add_action( 'register_new_user', 'wppb_default_registration_redirect' ); |
| 243 |
|