| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
add_action( 'init', 'wppb_process_login' ); |
| 5 |
function wppb_process_login(){ |
| 6 |
|
| 7 |
if( !isset($_REQUEST['wppb_login']) ) |
| 8 |
return; |
| 9 |
|
| 10 |
do_action( 'login_init' ); |
| 11 |
do_action( "login_form_login" ); |
| 12 |
do_action( 'wppb_process_login_start' ); |
| 13 |
|
| 14 |
if( !isset( $_POST['CSRFToken-wppb'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['CSRFToken-wppb'] ), 'wppb_login' ) ) |
| 15 |
return; |
| 16 |
|
| 17 |
$secure_cookie = ''; |
| 18 |
// If the user wants ssl but the session is not ssl, force a secure cookie. |
| 19 |
if ( !empty($_POST['log']) && !force_ssl_admin() ) { |
| 20 |
$user_name = sanitize_user($_POST['log']); |
| 21 |
$user = get_user_by( 'login', $user_name ); |
| 22 |
|
| 23 |
if ( ! $user && strpos( $user_name, '@' ) ) { |
| 24 |
$user = get_user_by( 'email', $user_name ); |
| 25 |
} |
| 26 |
|
| 27 |
if ( $user ) { |
| 28 |
if ( get_user_option('use_ssl', $user->ID) ) { |
| 29 |
$secure_cookie = true; |
| 30 |
force_ssl_admin(true); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
$redirect_to = ''; |
| 36 |
if ( isset( $_REQUEST['redirect_to'] ) ) { |
| 37 |
$redirect_to = esc_url_raw( $_REQUEST['redirect_to'] ); |
| 38 |
} |
| 39 |
|
| 40 |
$user = wp_signon( array(), $secure_cookie ); |
| 41 |
|
| 42 |
if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) { |
| 43 |
if ( headers_sent() ) { |
| 44 |
/* translators: 1: Browser cookie documentation URL, 2: Support forums URL */ |
| 45 |
$user = new WP_Error( 'test_cookie', sprintf( __( '<strong>ERROR:</strong> Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try the <a href="%2$s">support forums</a>.', 'profile-builder' ), |
| 46 |
'https://codex.wordpress.org/Cookies', 'https://wordpress.org/support/' ) ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$requested_redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( $_REQUEST['redirect_to'] ) : ''; |
| 51 |
/** |
| 52 |
* Filters the login redirect URL. |
| 53 |
*/ |
| 54 |
$redirect_to = apply_filters( 'login_redirect', $redirect_to, $requested_redirect_to, $user ); |
| 55 |
|
| 56 |
do_action( 'wppb_process_login_end' ); |
| 57 |
|
| 58 |
if ( !is_wp_error($user) ) { |
| 59 |
if ( $redirect_to == 'wp-admin/' || $redirect_to == admin_url() ) { |
| 60 |
// If the user doesn't belong to a blog, send them to user admin. If the user can't edit posts, send them to their profile. |
| 61 |
if ( is_multisite() && !get_active_blog_for_user($user->ID) && !is_super_admin( $user->ID ) ) |
| 62 |
$redirect_to = user_admin_url(); |
| 63 |
elseif ( is_multisite() && !$user->has_cap('read') ) |
| 64 |
$redirect_to = get_dashboard_url( $user->ID ); |
| 65 |
elseif ( !$user->has_cap('edit_posts') ) |
| 66 |
$redirect_to = $user->has_cap( 'read' ) ? admin_url( 'profile.php' ) : home_url(); |
| 67 |
|
| 68 |
wp_redirect( $redirect_to ); |
| 69 |
exit(); |
| 70 |
} |
| 71 |
wp_safe_redirect($redirect_to); |
| 72 |
exit(); |
| 73 |
} |
| 74 |
else{ |
| 75 |
wp_safe_redirect($redirect_to); |
| 76 |
exit(); |
| 77 |
} |
| 78 |
} |
| 79 |
/** |
| 80 |
* Provides a simple login form |
| 81 |
* |
| 82 |
* The login format HTML is echoed by default. Pass a false value for `$echo` to return it instead. |
| 83 |
* |
| 84 |
* @param array $args { |
| 85 |
* Optional. Array of options to control the form output. Default empty array. |
| 86 |
* |
| 87 |
* @type bool $echo Whether to display the login form or return the form HTML code. |
| 88 |
* Default true (echo). |
| 89 |
* @type string $redirect URL to redirect to. Must be absolute, as in "https://example.com/mypage/". |
| 90 |
* Default is to redirect back to the request URI. |
| 91 |
* @type string $form_id ID attribute value for the form. Default 'loginform'. |
| 92 |
* @type string $label_username Label for the username or email address field. Default 'Username or Email Address'. |
| 93 |
* @type string $label_username Label for the username or email address field. Default 'Username or Email Address'. |
| 94 |
* @type string $login_username_input_type Type of input field for the username or email address. |
| 95 |
* @type string $label_remember Label for the remember field. Default 'Remember Me'. |
| 96 |
* @type string $label_log_in Label for the submit button. Default 'Log In'. |
| 97 |
* @type string $id_username ID attribute value for the username field. Default 'user_login'. |
| 98 |
* @type string $id_password ID attribute value for the password field. Default 'user_pass'. |
| 99 |
* @type string $id_remember ID attribute value for the remember field. Default 'rememberme'. |
| 100 |
* @type string $id_submit ID attribute value for the submit button. Default 'wp-submit'. |
| 101 |
* @type bool $remember Whether to display the "rememberme" checkbox in the form. |
| 102 |
* @type string $value_username Default value for the username field. Default empty. |
| 103 |
* @type bool $value_remember Whether the "Remember Me" checkbox should be checked by default. |
| 104 |
* Default false (unchecked). |
| 105 |
* |
| 106 |
* } |
| 107 |
* @return string|void String when retrieving. |
| 108 |
*/ |
| 109 |
function wppb_login_form( $args = array() ) { |
| 110 |
|
| 111 |
$default_redirect = ''; |
| 112 |
if( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) |
| 113 |
$default_redirect = esc_url_raw( ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); |
| 114 |
|
| 115 |
$defaults = array( |
| 116 |
'echo' => true, |
| 117 |
// Default 'redirect' value takes the user back to the request URI. |
| 118 |
'redirect' => $default_redirect, |
| 119 |
'form_id' => 'wppb-loginform', |
| 120 |
'form_classes' => array(), |
| 121 |
'label_username' => __( 'Username or Email Address', 'profile-builder' ), |
| 122 |
'login_username_input_type' => 'text', |
| 123 |
'label_password' => __( 'Password', 'profile-builder' ), |
| 124 |
'label_remember' => __( 'Remember Me', 'profile-builder' ), |
| 125 |
'label_log_in' => __( 'Log In', 'profile-builder' ), |
| 126 |
'id_username' => 'wppb_user_login', |
| 127 |
'id_password' => 'wppb_user_pass', |
| 128 |
'id_remember' => 'rememberme', |
| 129 |
'id_submit' => 'wp-submit', |
| 130 |
'remember' => true, |
| 131 |
'value_username' => '', |
| 132 |
// Set 'value_remember' to true to default the "Remember me" checkbox to checked. |
| 133 |
'value_remember' => false, |
| 134 |
'is_ajax_form' => false, |
| 135 |
); |
| 136 |
|
| 137 |
/** |
| 138 |
* Filters the default login form output arguments. |
| 139 |
*/ |
| 140 |
$args = wp_parse_args( $args, apply_filters( 'login_form_defaults', $defaults ) ); |
| 141 |
|
| 142 |
/** |
| 143 |
* Filters content to display at the top of the login form. |
| 144 |
*/ |
| 145 |
$login_form_top = apply_filters( 'login_form_top', '', $args ); |
| 146 |
|
| 147 |
/** |
| 148 |
* Filters content to display in the middle of the login form. |
| 149 |
*/ |
| 150 |
$login_form_middle = apply_filters( 'login_form_middle', '', $args ); |
| 151 |
|
| 152 |
/** |
| 153 |
* Filters content to display at the bottom of the login form. |
| 154 |
*/ |
| 155 |
$login_form_bottom = apply_filters( 'login_form_bottom', '', $args ); |
| 156 |
|
| 157 |
if( !empty( $args['is_ajax_form'] ) ){ |
| 158 |
$args['form_classes'][] = 'wppb-ajax-form'; |
| 159 |
} |
| 160 |
|
| 161 |
$args['form_classes'] = implode( ' ', $args['form_classes'] ); |
| 162 |
|
| 163 |
if( in_the_loop() ) |
| 164 |
$form_location = 'page'; |
| 165 |
else |
| 166 |
$form_location = 'widget'; |
| 167 |
|
| 168 |
// if an error is being shown pass the original referer forward |
| 169 |
if( isset( $_GET['wppb_referer_url'] ) ){ |
| 170 |
$wppb_referer_url = esc_url_raw ( $_GET['wppb_referer_url'] ); |
| 171 |
} else { |
| 172 |
$wppb_referer_url = esc_url_raw ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ); |
| 173 |
} |
| 174 |
|
| 175 |
$form = ' |
| 176 |
<form name="' . esc_attr( $args['form_id'] ) . '" id="' . esc_attr( $args['form_id'] ) . '" class="' . esc_attr( $args['form_classes'] ) . '" action="'. esc_url( wppb_curpageurl() ) .'" method="post"> |
| 177 |
' . $login_form_top . ' |
| 178 |
<p class="wppb-form-field login-username'. apply_filters( 'wppb_login_field_extra_css_class', '', $args['id_username']) .'"> |
| 179 |
<label for="' . esc_attr( $args['id_username'] ) . '">' . esc_html( $args['label_username'] ) . '</label> |
| 180 |
<input type="' . esc_attr( $args['login_username_input_type'] ) . '" name="log" id="' . esc_attr( $args['id_username'] ) . '" class="input" value="' . esc_attr( $args['value_username'] ) . '" size="20" /> |
| 181 |
</p> |
| 182 |
<p class="wppb-form-field login-password'. apply_filters( 'wppb_login_field_extra_css_class', '', $args['id_password']) .'"> |
| 183 |
<label for="' . esc_attr( $args['id_password'] ) . '">' . esc_html( $args['label_password'] ) . '</label> |
| 184 |
<span class="wppb-password-field-container"> |
| 185 |
<input type="password" name="pwd" id="' . esc_attr( $args['id_password'] ) . '" class="input" value="" size="20" '. apply_filters( 'wppb_login_password_extra_attributes', '' ) .'/> |
| 186 |
'. wppb_password_visibility_toggle_html() .' <!-- add the HTML for the visibility toggle --> |
| 187 |
</span> |
| 188 |
</p>'; |
| 189 |
|
| 190 |
$form .=' |
| 191 |
|
| 192 |
' . $login_form_middle . ' |
| 193 |
' . ( $args['remember'] ? '<p class="wppb-form-field login-remember"><input name="rememberme" type="checkbox" id="' . esc_attr( $args['id_remember'] ) . '" value="forever"' . ( $args['value_remember'] ? ' checked="checked"' : '' ) . ' /><label for="' . esc_attr( $args['id_remember'] ) . '">' . esc_html( $args['label_remember'] ) . '</label></p>' : '' ) . ' |
| 194 |
<p class="login-submit form-submit"> |
| 195 |
<input type="submit" name="wp-submit" id="' . esc_attr( $args['id_submit'] ) . '" class="'. esc_attr( apply_filters( 'wppb_login_submit_class', "button button-primary" ) ) . '" value="' . esc_attr( $args['label_log_in'] ) . '"' . apply_filters( 'wppb_login_submit_button_extra_attributes', '' ) . '" /> |
| 196 |
<input type="hidden" name="redirect_to" value="' . esc_url( $args['redirect'] ) . '" /> |
| 197 |
</p> |
| 198 |
<input type="hidden" name="wppb_login" value="true"/> |
| 199 |
<input type="hidden" name="wppb_form_location" value="'. esc_attr( $form_location ) .'"/> |
| 200 |
<input type="hidden" name="wppb_request_url" value="'. esc_url( wppb_curpageurl() ).'"/> |
| 201 |
<input type="hidden" name="wppb_lostpassword_url" value="'.esc_url( $args['lostpassword_url'] ).'"/> |
| 202 |
<input type="hidden" name="wppb_redirect_priority" value="'. esc_attr( isset( $args['redirect_priority'] ) ? $args['redirect_priority'] : '' ) .'"/> |
| 203 |
<input type="hidden" name="wppb_referer_url" value="'. esc_url( $wppb_referer_url ) .'"/> |
| 204 |
'. wp_nonce_field( 'wppb_login', 'CSRFToken-wppb', true, false ) .' |
| 205 |
<input type="hidden" name="wppb_redirect_check" value="true"/> |
| 206 |
' . $login_form_bottom . ' |
| 207 |
</form>'; |
| 208 |
|
| 209 |
// Remove whitespace if login_username_input_type is set to email |
| 210 |
if ( $args['login_username_input_type'] == 'email' ) { |
| 211 |
$form .= '<script>window.onload = function() { jQuery(document).ready(function($) { $("input[name=\"log\"]").on("keyup", function() { $(this).val( $.trim($(this).val()) ); }); }); }</script>'; |
| 212 |
} |
| 213 |
|
| 214 |
if ( $args['echo'] ) |
| 215 |
echo $form; /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped */ /* escaped above */ |
| 216 |
else |
| 217 |
return $form; |
| 218 |
} |
| 219 |
|
| 220 |
// when email login is enabled we need to change the post data for the username |
| 221 |
function wppb_change_login_with_email(){ |
| 222 |
if( !empty( $_POST['log'] ) ){ |
| 223 |
// only do this for our form |
| 224 |
if( isset( $_POST['wppb_login'] ) ){ |
| 225 |
global $wpdb, $_POST, $wp_version; |
| 226 |
// apply filter to allow stripping slashes if necessary |
| 227 |
$_POST['log'] = apply_filters( 'wppb_before_processing_email_from_forms', sanitize_text_field( $_POST['log'] ) ); |
| 228 |
|
| 229 |
/* since version 4.5 there is in the core the option to login with email so we don't need the bellow code but for backward compatibility we will keep it */ |
| 230 |
if( version_compare( $wp_version, '4.5.0' ) >= 0 && apply_filters( 'wppb_allow_login_with_username_when_is_set_to_email', false ) ) |
| 231 |
return; |
| 232 |
|
| 233 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 234 |
|
| 235 |
// if this setting is active, the posted username is, in fact the user's email |
| 236 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){ |
| 237 |
if( !is_email( $_POST['log'] ) && !apply_filters( 'wppb_allow_login_with_username_when_is_set_to_email', false ) ){ |
| 238 |
$_POST['log'] = 'this_is_an_invalid_email' . time(); |
| 239 |
} |
| 240 |
else { |
| 241 |
$username = $wpdb->get_var($wpdb->prepare("SELECT user_login FROM $wpdb->users WHERE user_email= %s LIMIT 1", sanitize_email($_POST['log']))); |
| 242 |
|
| 243 |
if (!empty($username)) |
| 244 |
$_POST['log'] = $username; |
| 245 |
|
| 246 |
else { |
| 247 |
// if we don't have a username for the email entered we can't have an empty username because we will receive a field empty error |
| 248 |
$_POST['log'] = 'this_is_an_invalid_email' . time(); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
// if this setting is active, the posted username is, in fact the user's email or username |
| 254 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'usernameemail' ) ) { |
| 255 |
if( is_email( $_POST['log'] ) ) { |
| 256 |
|
| 257 |
$username = $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM $wpdb->users WHERE user_email= %s LIMIT 1", sanitize_email( $_POST['log'] ) ) ); |
| 258 |
|
| 259 |
// the username can have the format of an email address, so if we can't find a user that has an account with the detected email, we set the username as that email |
| 260 |
if( empty( $username ) ) |
| 261 |
$username = sanitize_user( $_POST['log'] ); |
| 262 |
|
| 263 |
} else { |
| 264 |
$username = sanitize_user( $_POST['log'] ); |
| 265 |
} |
| 266 |
|
| 267 |
if( !empty( $username ) ) |
| 268 |
$_POST['log'] = $username; |
| 269 |
else { |
| 270 |
// if we don't have a username for the email entered we can't have an empty username because we will receive a field empty error |
| 271 |
$_POST['log'] = 'this_is_an_invalid_email'.time(); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
add_action( 'login_init', 'wppb_change_login_with_email' ); |
| 278 |
|
| 279 |
function wppb_resend_confirmation_email() { |
| 280 |
if( !isset( $_GET['wppb-action'] ) || $_GET['wppb-action'] != 'resend_email_confirmation' || !isset( $_GET['email'] )) |
| 281 |
return; |
| 282 |
|
| 283 |
$user_email = base64_decode( sanitize_text_field( $_GET['email'] )); |
| 284 |
|
| 285 |
$transient_check_key = Wordpress_Creation_Kit_PB::wck_generate_slug( $user_email ); |
| 286 |
$transient_check = get_transient('wppb_confirmation_email_already_sent_'.$transient_check_key); |
| 287 |
|
| 288 |
if ( $transient_check === false ) { |
| 289 |
|
| 290 |
if ( !isset( $_GET['_wpnonce'] ) || !wp_verify_nonce(sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_confirmation_url_nonce' )) |
| 291 |
return; |
| 292 |
|
| 293 |
include_once(plugin_dir_path(__FILE__) . '../features/email-confirmation/email-confirmation.php'); |
| 294 |
|
| 295 |
if ( file_exists( WPPB_PLUGIN_DIR . '/assets/lib/class-mustache-templates/class-mustache-templates.php' ) ) |
| 296 |
include_once( WPPB_PLUGIN_DIR . '/assets/lib/class-mustache-templates/class-mustache-templates.php' ); |
| 297 |
|
| 298 |
global $wpdb; |
| 299 |
$sql_result = $wpdb->get_row( $wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", $user_email ), ARRAY_A ); |
| 300 |
|
| 301 |
// if the email address exists in wp_signups table, resend Confirmation Email and redirect to display notification |
| 302 |
if ( $sql_result ) { |
| 303 |
wppb_signup_user_notification( sanitize_text_field( $sql_result['user_login'] ), sanitize_email( $sql_result['user_email'] ), $sql_result['activation_key'], $sql_result['meta'] ); |
| 304 |
$transient_key = Wordpress_Creation_Kit_PB::wck_generate_slug( $user_email ); |
| 305 |
set_transient('wppb_confirmation_email_already_sent_' . $transient_key, true, 900 ); |
| 306 |
$error_string = '<strong>' . __( 'SUCCESS: ', 'profile-builder') . '</strong>' . sprintf( __( 'Activation email sent to %s', 'profile-builder' ), $user_email ); |
| 307 |
$wppb_success_message_nonce = wp_create_nonce( 'wppb_login_error_'.$error_string); |
| 308 |
$current_url = wppb_curpageurl(); |
| 309 |
$arr_params = array('loginerror' => urlencode(base64_encode($error_string)), '_wpnonce' => $wppb_success_message_nonce, 'request_form_location' => 'page', 'wppb_message_type' => 'success'); |
| 310 |
$redirect_to = add_query_arg($arr_params, $current_url); |
| 311 |
wp_safe_redirect($redirect_to); |
| 312 |
exit(); |
| 313 |
} |
| 314 |
|
| 315 |
} |
| 316 |
} |
| 317 |
add_action('init', 'wppb_resend_confirmation_email'); |
| 318 |
|
| 319 |
function wppb_change_error_message($error_message) { |
| 320 |
|
| 321 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 322 |
|
| 323 |
if (empty( $wppb_generalSettings['emailConfirmation'] ) || $wppb_generalSettings['emailConfirmation'] !== 'yes') |
| 324 |
return $error_message; |
| 325 |
|
| 326 |
if( isset( $_REQUEST['log'] ) ){ |
| 327 |
global $wpdb; |
| 328 |
$check_user = sanitize_text_field( $_REQUEST['log'] ); |
| 329 |
|
| 330 |
if ( is_email( $check_user )) |
| 331 |
$sql_result = $wpdb->get_row( $wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_email = %s", sanitize_email( $check_user )), ARRAY_A ); |
| 332 |
else { |
| 333 |
$sql_result = $wpdb->get_row( $wpdb->prepare("SELECT * FROM " . $wpdb->base_prefix . "signups WHERE user_login = %s", sanitize_user( $check_user )), ARRAY_A ); |
| 334 |
if ( $sql_result ) |
| 335 |
$check_user = $sql_result['user_email']; |
| 336 |
} |
| 337 |
|
| 338 |
// if the email address exists in wp_signups table, display message and link to resend Confirmation Email |
| 339 |
if ( isset($sql_result) ) { |
| 340 |
$confirmation_url_nonce = wp_create_nonce( 'wppb_confirmation_url_nonce' ); |
| 341 |
$current_url = strtok( wppb_curpageurl(), '?' ); |
| 342 |
$arr_params = array('email' => base64_encode( $check_user ), 'wppb-action' => 'resend_email_confirmation', '_wpnonce' => $confirmation_url_nonce); |
| 343 |
$confirmation_url = add_query_arg($arr_params, $current_url); |
| 344 |
$error_message = '<strong>' . __('ERROR: ', 'profile-builder') . '</strong>' . sprintf( __( 'You need to confirm your Email Address before logging in! </br>To resend the Confirmation Email %1$sclick here%2$s.', 'profile-builder' ), '<a href="' . esc_url( $confirmation_url ) . '" title="Resend Confirmation Email">', '</a>' ); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
return $error_message; |
| 349 |
|
| 350 |
} |
| 351 |
add_filter('wppb_login_invalid_username_error_message', 'wppb_change_error_message'); |
| 352 |
|
| 353 |
/** |
| 354 |
* Remove email login when username login is selected |
| 355 |
* inspiration from https://wordpress.org/plugins/no-login-by-email-address/ |
| 356 |
*/ |
| 357 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 358 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'username' ) ) { |
| 359 |
function wppb_login_username_label() |
| 360 |
{ |
| 361 |
add_filter('gettext', 'wppb_login_username_label_change', 20, 3); |
| 362 |
function wppb_login_username_label_change($translated_text, $text, $domain) |
| 363 |
{ |
| 364 |
if ($text === 'Username or Email') { |
| 365 |
$translated_text = __( 'Username', 'profile-builder' ); |
| 366 |
} |
| 367 |
return $translated_text; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
add_action('login_head', 'wppb_login_username_label'); |
| 372 |
|
| 373 |
/** |
| 374 |
* Filter wp_login_form username default |
| 375 |
* |
| 376 |
*/ |
| 377 |
function wppb_change_login_username_label($defaults) |
| 378 |
{ |
| 379 |
$defaults['label_username'] = __( 'Username', 'profile-builder' ); |
| 380 |
return $defaults; |
| 381 |
} |
| 382 |
|
| 383 |
add_filter('login_form_defaults', 'wppb_change_login_username_label'); |
| 384 |
|
| 385 |
/** |
| 386 |
* Remove email/password authentication |
| 387 |
* |
| 388 |
*/ |
| 389 |
remove_filter('authenticate', 'wp_authenticate_email_password', 20); |
| 390 |
} |
| 391 |
|
| 392 |
// login redirect filter. used to redirect from wp-login.php if it errors out |
| 393 |
function wppb_login_redirect( $redirect_to, $requested_redirect_to, $user ){ |
| 394 |
// custom redirect after login on default wp login form |
| 395 |
if( ! isset( $_POST['wppb_login'] ) && ! is_wp_error( $user ) ) { |
| 396 |
$original_redirect_to = $redirect_to; |
| 397 |
|
| 398 |
// we don't have an error make sure to remove the error from the query arg |
| 399 |
$redirect_to = remove_query_arg( 'loginerror', $redirect_to ); |
| 400 |
|
| 401 |
// CHECK FOR REDIRECT |
| 402 |
$redirect_to = wppb_get_redirect_url( 'normal', 'after_login', $redirect_to, $user ); |
| 403 |
$redirect_to = apply_filters( 'wppb_after_login_redirect_url', $redirect_to ); |
| 404 |
|
| 405 |
if ( $redirect_to === '' ){ |
| 406 |
$redirect_to = $original_redirect_to; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
// if login action initialized by our form |
| 411 |
if( isset( $_POST['wppb_login'] ) ){ |
| 412 |
if( is_wp_error( $user ) ) { |
| 413 |
// if we don't have a successful login we must redirect to the url of the form, so make sure this happens |
| 414 |
if( isset( $_POST['wppb_request_url'] ) ) |
| 415 |
$redirect_to = esc_url_raw( $_POST['wppb_request_url'] ); |
| 416 |
if( isset( $_POST['wppb_form_location'] ) ) |
| 417 |
$request_form_location = sanitize_text_field( $_POST['wppb_form_location'] ); |
| 418 |
$error_string = $user->get_error_message(); |
| 419 |
|
| 420 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 421 |
|
| 422 |
if (isset($wppb_generalSettings['loginWith'])) { |
| 423 |
|
| 424 |
$lost_pass_url = site_url('/wp-login.php?action=lostpassword'); |
| 425 |
// if the Login shortcode has a lostpassword argument set, give the lost password error link that value |
| 426 |
if (!empty($_POST['wppb_lostpassword_url'])) { |
| 427 |
$lost_pass_url = esc_url_raw( $_POST['wppb_lostpassword_url'] ); |
| 428 |
if ( wppb_check_missing_http( $lost_pass_url ) ) |
| 429 |
$lost_pass_url = "http://" . $lost_pass_url; |
| 430 |
} |
| 431 |
//apply filter to allow changing Lost your Password link |
| 432 |
$lost_pass_url = apply_filters('wppb_pre_login_url_filter', $lost_pass_url); |
| 433 |
|
| 434 |
/* start building the error string */ |
| 435 |
if( in_array( $user->get_error_code(), array( 'empty_username', 'empty_password', 'invalid_username', 'incorrect_password' ) ) ) |
| 436 |
$error_string = '<strong>' . __('ERROR: ', 'profile-builder') . '</strong>'; |
| 437 |
|
| 438 |
if ( $user->get_error_code() == 'empty_password' ) { |
| 439 |
$error_string .= __( 'The password field is empty.', 'profile-builder' ) . ' '; |
| 440 |
} |
| 441 |
|
| 442 |
if ( $user->get_error_code() == 'empty_username' ) { |
| 443 |
if ($wppb_generalSettings['loginWith'] == 'email')// if login with email is enabled change the word username with email |
| 444 |
$error_string .= __('The email field is empty.', 'profile-builder') . ' '; |
| 445 |
else if( $wppb_generalSettings['loginWith'] == 'usernameemail' )// if login with username and email is enabled change the word username with username or email |
| 446 |
$error_string .= __('The username/email field is empty', 'profile-builder') . ' '; |
| 447 |
else |
| 448 |
$error_string .= __('The username field is empty', 'profile-builder') . ' '; |
| 449 |
} |
| 450 |
|
| 451 |
if( apply_filters( 'wppb_login_use_old_error_messages', false ) ) { |
| 452 |
|
| 453 |
if ( $user->get_error_code() == 'incorrect_password' ) { |
| 454 |
$error_string .= __('The password you entered is incorrect.', 'profile-builder') . ' '; |
| 455 |
} |
| 456 |
|
| 457 |
if ( $user->get_error_code() == 'invalid_username' ) { |
| 458 |
if ($wppb_generalSettings['loginWith'] == 'email')// if login with email is enabled change the word username with email |
| 459 |
$error_string .= __('Invalid email.', 'profile-builder') . ' '; |
| 460 |
else if( $wppb_generalSettings['loginWith'] == 'usernameemail' )// if login with username and email is enabled change the word username with username or email |
| 461 |
$error_string .= __('Invalid username or email.', 'profile-builder') . ' '; |
| 462 |
else |
| 463 |
$error_string .= __('Invalid username.', 'profile-builder') . ' '; |
| 464 |
|
| 465 |
$error_string = apply_filters('wppb_login_invalid_username_error_message', $error_string); |
| 466 |
} |
| 467 |
|
| 468 |
} else if( in_array( $user->get_error_code(), array( 'incorrect_password', 'invalid_username' ) ) ) { |
| 469 |
$error_string .= __( 'The credentials you entered are incorrect.', 'profile-builder' ) . ' '; |
| 470 |
} |
| 471 |
|
| 472 |
if( $user->get_error_code() == 'incorrect_password' || $user->get_error_code() == 'invalid_username' && empty( $message_check = apply_filters('wppb_login_invalid_username_error_message', '' ))) |
| 473 |
$error_string .= '<a href="' . esc_url( $lost_pass_url ) . '" title="' . __('Password Lost and Found.', 'profile-builder') . '">' . __('Lost your password?', 'profile-builder') . '</a>'; |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
// if the error string is empty it means that none of the fields were completed |
| 478 |
if (empty($error_string) || ( in_array( 'empty_username', $user->get_error_codes() ) && in_array( 'empty_password', $user->get_error_codes() ) ) ) { |
| 479 |
$error_string = '<strong>' . __('ERROR: ', 'profile-builder') . '</strong>' . __('Both fields are empty.', 'profile-builder') . ' '; |
| 480 |
$error_string = apply_filters('wppb_login_empty_fields_error_message', $error_string); |
| 481 |
} |
| 482 |
|
| 483 |
$error_string = apply_filters('wppb_login_wp_error_message', $error_string, $user); |
| 484 |
$wppb_error_string_nonce = wp_create_nonce( 'wppb_login_error_'.$error_string ); |
| 485 |
|
| 486 |
// encode the error string and send it as a GET parameter |
| 487 |
if ( isset($_POST['wppb_referer_url']) && $_POST['wppb_referer_url'] !== '' ) { |
| 488 |
$arr_params = array('loginerror' => urlencode(base64_encode($error_string)), '_wpnonce' => $wppb_error_string_nonce, 'request_form_location' => $request_form_location, 'wppb_referer_url' => urlencode(esc_url_raw( $_POST['wppb_referer_url'] ))); |
| 489 |
} else { |
| 490 |
$arr_params = array('loginerror' => urlencode(base64_encode($error_string)), '_wpnonce' => $wppb_error_string_nonce, 'request_form_location' => $request_form_location); |
| 491 |
} |
| 492 |
|
| 493 |
if ($user->get_error_code() == 'wppb_login_auth') { |
| 494 |
$arr_params['login_auth'] = 'true'; |
| 495 |
} |
| 496 |
|
| 497 |
$redirect_to = add_query_arg($arr_params, $redirect_to); |
| 498 |
} |
| 499 |
else{ |
| 500 |
// we don't have an error make sure to remove the error from the query arg |
| 501 |
$redirect_to = remove_query_arg( 'loginerror', $redirect_to ); |
| 502 |
|
| 503 |
// CHECK FOR REDIRECT |
| 504 |
if( isset( $_POST['wppb_redirect_priority'] ) ) |
| 505 |
$redirect_to = wppb_get_redirect_url( sanitize_text_field( $_POST['wppb_redirect_priority'] ), 'after_login', $redirect_to, $user ); |
| 506 |
|
| 507 |
$redirect_to = apply_filters( 'wppb_after_login_redirect_url', $redirect_to ); |
| 508 |
|
| 509 |
// This should not be empty, if we don't have a redirect, set it to the current page URL |
| 510 |
if( empty( $redirect_to ) ) |
| 511 |
$redirect_to = wppb_curpageurl(); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
// if "wppb_message_type = success" is present the message will show up in a green box instead of red |
| 516 |
if ( isset( $_GET['wppb_message_type'] ) && $_GET['wppb_message_type'] == 'success' ) |
| 517 |
$redirect_to = remove_query_arg( 'wppb_message_type', $redirect_to ); |
| 518 |
|
| 519 |
return $redirect_to; |
| 520 |
} |
| 521 |
add_filter( 'login_redirect', 'wppb_login_redirect', 20, 3 ); |
| 522 |
|
| 523 |
|
| 524 |
/* shortcode function */ |
| 525 |
function wppb_front_end_login( $atts ){ |
| 526 |
global $wppb_shortcode_on_front; |
| 527 |
$wppb_shortcode_on_front = true; |
| 528 |
global $wppb_login_shortcode_on_front; |
| 529 |
$wppb_login_shortcode_on_front = true; |
| 530 |
/* define a global so we now we have the shortcode login present */ |
| 531 |
global $wppb_login_shortcode; |
| 532 |
$wppb_login_shortcode = true; |
| 533 |
|
| 534 |
$atts = shortcode_atts( array( |
| 535 |
'display' => true, |
| 536 |
'redirect' => '', |
| 537 |
'redirect_url' => '', |
| 538 |
'logout_redirect_url' => wppb_curpageurl(), |
| 539 |
'redirect_priority' => 'normal', |
| 540 |
'register_url' => '', |
| 541 |
'lostpassword_url' => '', |
| 542 |
'show_2fa_field' => '', |
| 543 |
'block' => false, |
| 544 |
'ajax' => false, |
| 545 |
), $atts, 'wppb-login' ); |
| 546 |
|
| 547 |
$display = $atts['display']; |
| 548 |
$redirect = $atts['redirect']; |
| 549 |
$redirect_url = $atts['redirect_url']; |
| 550 |
$logout_redirect_url = $atts['logout_redirect_url']; |
| 551 |
$redirect_priority = $atts['redirect_priority']; |
| 552 |
$register_url = $atts['register_url']; |
| 553 |
$lostpassword_url = $atts['lostpassword_url']; |
| 554 |
$show_2fa_field = $atts['show_2fa_field']; |
| 555 |
$block = $atts['block']; |
| 556 |
$ajax = $atts['ajax']; |
| 557 |
|
| 558 |
$is_ajax_form = false; |
| 559 |
if( defined( 'WPPB_PAID_PLUGIN_DIR' ) && $ajax === 'true' && file_exists( WPPB_PAID_PLUGIN_DIR . '/features/ajax/assets/forms-ajax-validation.js' ) ) { |
| 560 |
wp_enqueue_script( 'wppb-forms-ajax-validation-script', WPPB_PAID_PLUGIN_URL . 'features/ajax/assets/forms-ajax-validation.js', array( 'jquery' ), PROFILE_BUILDER_VERSION, true ); |
| 561 |
wp_localize_script( 'wppb-forms-ajax-validation-script', 'submitButtonData', array( 'processingText' => __('Processing...', 'profile-builder') ) ); |
| 562 |
$is_ajax_form = true; |
| 563 |
} |
| 564 |
|
| 565 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 566 |
|
| 567 |
// check if the form is being displayed in the Elementor editor |
| 568 |
$is_elementor_edit_mode_or_divi_ajax = false; |
| 569 |
if( class_exists ( '\Elementor\Plugin' ) ){ |
| 570 |
$is_elementor_edit_mode_or_divi_ajax = \Elementor\Plugin::$instance->editor->is_edit_mode(); |
| 571 |
} |
| 572 |
|
| 573 |
if ( is_array( $_POST ) && array_key_exists( 'action', $_POST ) && $_POST['action'] === 'wppb_divi_extension_ajax' ) { |
| 574 |
$is_elementor_edit_mode_or_divi_ajax = true; |
| 575 |
} |
| 576 |
|
| 577 |
if( !is_user_logged_in() || $is_elementor_edit_mode_or_divi_ajax || $block === 'true' ){ |
| 578 |
// set up the form arguments |
| 579 |
$form_args = array( 'echo' => false, 'id_submit' => 'wppb-submit', 'is_ajax_form' => $is_ajax_form ); |
| 580 |
|
| 581 |
// maybe set up the redirect argument |
| 582 |
if( ! empty( $redirect ) ) { |
| 583 |
$redirect_url = $redirect; |
| 584 |
} |
| 585 |
|
| 586 |
if ( ! empty( $redirect_url ) ) { |
| 587 |
if( $redirect_priority == 'top' ) { |
| 588 |
$form_args['redirect_priority'] = 'top'; |
| 589 |
} else { |
| 590 |
$form_args['redirect_priority'] = 'normal'; |
| 591 |
} |
| 592 |
|
| 593 |
$form_args['redirect'] = trim( $redirect_url ); |
| 594 |
} |
| 595 |
|
| 596 |
$form_args['login_username_input_type'] = 'text'; |
| 597 |
|
| 598 |
// change the label argument for username is login with email is enabled |
| 599 |
if ( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ) { |
| 600 |
$form_args['label_username'] = __('Email', 'profile-builder'); |
| 601 |
$form_args['login_username_input_type'] = 'email'; |
| 602 |
} |
| 603 |
|
| 604 |
if ( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'username' ) ) { |
| 605 |
$form_args['label_username'] = __('Username', 'profile-builder'); |
| 606 |
} |
| 607 |
|
| 608 |
// change the label argument for username on login with username or email when Username and Email is enabled |
| 609 |
if ( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'usernameemail' ) ) |
| 610 |
$form_args['label_username'] = __( 'Username or Email', 'profile-builder' ); |
| 611 |
|
| 612 |
// Check if 2fa is required |
| 613 |
if( class_exists( 'WPPB_Two_Factor_Authenticator' ) ){ |
| 614 |
$wppb_auth = new WPPB_Two_Factor_Authenticator; |
| 615 |
$wppb_two_factor_authentication_settings = get_option( 'wppb_two_factor_authentication_settings', 'not_found' ); |
| 616 |
if ( ( isset( $_GET['login_auth'] ) && $_GET['login_auth'] === 'true' ) || |
| 617 |
( ( isset($wppb_two_factor_authentication_settings['enabled']) && $wppb_two_factor_authentication_settings['enabled'] === 'yes' ) && $show_2fa_field === 'yes' ) ){ |
| 618 |
add_action( 'login_form_middle', array( $wppb_auth, 'auth_code_field') ); |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
// initialize our form variable |
| 623 |
$login_form = ''; |
| 624 |
|
| 625 |
// display our login errors |
| 626 |
if( ( isset( $_GET['loginerror'] ) || isset( $_POST['loginerror'] ) ) && isset( $_GET['_wpnonce'] ) ){ |
| 627 |
$error_string = urldecode( base64_decode( isset( $_GET['loginerror'] ) ? sanitize_text_field( $_GET['loginerror'] ) : sanitize_text_field( $_POST['loginerror'] ) ) ); |
| 628 |
if( wp_verify_nonce( sanitize_text_field( $_GET['_wpnonce'] ), 'wppb_login_error_'. $error_string ) ) { |
| 629 |
if ( isset( $_GET['wppb_message_type'] ) && $_GET['wppb_message_type'] == 'success' ) |
| 630 |
$message_type = 'wppb-success'; |
| 631 |
else $message_type = 'wppb-error'; |
| 632 |
$loginerror = '<p class="'. $message_type .'">' . wp_kses_post(str_replace( '-wppb-plus-', '+', $error_string)) . '</p><!-- .error -->'; |
| 633 |
if (isset($_GET['request_form_location'])) { |
| 634 |
if ($_GET['request_form_location'] === 'widget' && !in_the_loop()) { |
| 635 |
$login_form .= $loginerror; |
| 636 |
} elseif ($_GET['request_form_location'] === 'page' && in_the_loop()) { |
| 637 |
$login_form .= $loginerror; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
// build our form |
| 643 |
$login_form .= '<div id="wppb-login-wrap" class="wppb-user-forms">'; |
| 644 |
|
| 645 |
if ( empty( $lostpassword_url ) ) |
| 646 |
$lostpassword_url = ( !empty( $wppb_generalSettings['lost_password_page'] ) ) ? $wppb_generalSettings['lost_password_page'] : ''; |
| 647 |
|
| 648 |
$form_args['lostpassword_url'] = $lostpassword_url; |
| 649 |
$login_form .= wppb_login_form( apply_filters( 'wppb_login_form_args', $form_args ) ); |
| 650 |
|
| 651 |
if ((!empty($register_url)) || (!empty($lostpassword_url))) { |
| 652 |
$login_form .= '<p class="login-register-lost-password">'; |
| 653 |
$i = 0; |
| 654 |
if (!empty($register_url)) { |
| 655 |
if ( wppb_check_missing_http( $register_url ) ) $register_url = "http://" . $register_url; |
| 656 |
$login_form .= '<a class="login-register" href="' . esc_url($register_url) . '">'. apply_filters('wppb_login_register_text', __('Register','profile-builder')) .'</a>'; |
| 657 |
$i++; |
| 658 |
} |
| 659 |
if (!empty($lostpassword_url)) { |
| 660 |
if ($i != 0) $login_form .= '<span class="login-separator"> | </span>'; |
| 661 |
if ( wppb_check_missing_http( $lostpassword_url ) ) $lostpassword_url = "http://" . $lostpassword_url; |
| 662 |
$login_form .= '<a class="login-lost-password" href="'. esc_url($lostpassword_url) .'">'. apply_filters('wppb_login_lostpass_text', __('Lost your password?','profile-builder')) .'</a>'; |
| 663 |
} |
| 664 |
$login_form .= '</p>'; |
| 665 |
} |
| 666 |
|
| 667 |
$login_form .= apply_filters( 'wppb_login_form_bottom', '', $form_args ); |
| 668 |
|
| 669 |
$login_form .= '</div>'; |
| 670 |
return apply_filters('wppb_login_form_before_content_output', $login_form, $form_args); |
| 671 |
|
| 672 |
}else{ |
| 673 |
$user_ID = get_current_user_id(); |
| 674 |
$wppb_user = get_userdata( $user_ID ); |
| 675 |
|
| 676 |
$login_with = isset( $wppb_generalSettings['loginWith'] ) ? $wppb_generalSettings['loginWith'] : ''; |
| 677 |
|
| 678 |
// Email login: always show the email. Username+email: show email when the login is the auto-generated slug from that email. |
| 679 |
if ( $login_with === 'email' ) { |
| 680 |
$display_name = $wppb_user->user_email; |
| 681 |
} elseif ( $login_with === 'usernameemail' |
| 682 |
&& $wppb_user->user_login === Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $wppb_user->user_email ) ) ) { |
| 683 |
$display_name = $wppb_user->user_email; |
| 684 |
} elseif ( $wppb_user->display_name !== '' ) { |
| 685 |
$display_name = $wppb_user->display_name; |
| 686 |
} else { |
| 687 |
$display_name = $wppb_user->user_login; |
| 688 |
} |
| 689 |
|
| 690 |
$logged_in_message = '<p class="wppb-alert">'; |
| 691 |
|
| 692 |
// CHECK FOR REDIRECT |
| 693 |
$logout_redirect_url = wppb_get_redirect_url( $redirect_priority, 'after_logout', $logout_redirect_url, $wppb_user ); |
| 694 |
$logout_redirect_url = apply_filters( 'wppb_after_logout_redirect_url', $logout_redirect_url ); |
| 695 |
|
| 696 |
$logout_url = '<a href="'.wp_logout_url( $logout_redirect_url ).'" class="wppb-logout-url" title="'.__( 'Log out of this account', 'profile-builder' ).'">'. __('Log out »','profile-builder').'</a>'; |
| 697 |
$logged_in_message .= sprintf(__( 'You are currently logged in as %1$s. %2$s', 'profile-builder' ), $display_name, $logout_url ); |
| 698 |
|
| 699 |
$logged_in_message .= '</p><!-- .wppb-alert-->'; |
| 700 |
|
| 701 |
return apply_filters( 'wppb_login_message', $logged_in_message, $wppb_user->ID, $display_name ); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
function wppb_login_security_check( $user, $password ) { |
| 706 |
if( apply_filters( 'wppb_enable_csrf_token_login_form', false ) ){ |
| 707 |
if (isset($_POST['wppb_login'])) { |
| 708 |
if (!isset($_POST['CSRFToken-wppb']) || !wp_verify_nonce( sanitize_text_field( $_POST['CSRFToken-wppb'] ), 'wppb_login')) { |
| 709 |
$errorMessage = __('You are not allowed to do this.', 'profile-builder'); |
| 710 |
return new WP_Error('wppb_login_csrf_token_error', $errorMessage); |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
return $user; |
| 716 |
} |
| 717 |
add_filter( 'wp_authenticate_user', 'wppb_login_security_check', 10, 2 ); |
| 718 |
|
| 719 |
|
| 720 |
// include missing scripts needed on Elementor Pages (Form inside an Elementor Popup) |
| 721 |
function wppb_login_scripts_and_styles() { |
| 722 |
if ( is_plugin_active('elementor-pro/elementor-pro.php') && defined( 'WPPB_PAID_PLUGIN_URL' ) ) |
| 723 |
wp_enqueue_script( 'wppb_elementor_popup_script', WPPB_PAID_PLUGIN_URL . 'features/elementor-pro/assets/js/elementor-popup.js', array('jquery') ); |
| 724 |
} |
| 725 |
add_action( 'elementor/frontend/after_enqueue_scripts', 'wppb_login_scripts_and_styles' ); |