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