| 1 |
<?php |
| 2 |
// add hidden input to our form to identify that is a profile builder form |
| 3 |
function wppb_login_form_bottom( $form_part, $args ){ |
| 4 |
// we set this id in the wp_login_form() function |
| 5 |
if( $args['id_submit'] == 'wppb-submit' ){ |
| 6 |
if( in_the_loop() ) |
| 7 |
$form_location = 'page'; |
| 8 |
else |
| 9 |
$form_location = 'widget'; |
| 10 |
|
| 11 |
$form_part = '<input type="hidden" name="wppb_login" value="true"/>'; |
| 12 |
$form_part .= '<input type="hidden" name="wppb_form_location" value="'. $form_location .'"/>'; |
| 13 |
$form_part .= '<input type="hidden" name="wppb_request_url" value="'.wppb_curpageurl().'"/>'; |
| 14 |
} |
| 15 |
|
| 16 |
return $form_part; |
| 17 |
} |
| 18 |
add_filter( 'login_form_bottom', 'wppb_login_form_bottom', 10, 2 ); |
| 19 |
|
| 20 |
// when email login is enabled we need to change the post data for the username |
| 21 |
function wppb_change_login_with_email(){ |
| 22 |
if( !empty( $_POST['log'] ) ){ |
| 23 |
// only do this for our form |
| 24 |
if( isset( $_POST['wppb_login'] ) ){ |
| 25 |
global $wpdb, $_POST; |
| 26 |
|
| 27 |
$wppb_generalSettings = get_option( 'wppb_general_settings' ); |
| 28 |
|
| 29 |
// if this setting is active, the posted username is, in fact the user's email |
| 30 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){ |
| 31 |
$username = $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM $wpdb->users WHERE user_email= %s LIMIT 1", trim( $_POST['log'] ) ) ); |
| 32 |
|
| 33 |
if( !empty( $username ) ) |
| 34 |
$_POST['log'] = $username; |
| 35 |
|
| 36 |
else{ |
| 37 |
// 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 |
| 38 |
$_POST['log'] = 'this_is_an_invalid_email'.time(); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
add_action( 'login_init', 'wppb_change_login_with_email' ); |
| 45 |
|
| 46 |
// login redirect filter. used to redirect from wp-login.php if it errors out |
| 47 |
function wppb_login_redirect( $redirect_to, $redirect_url, $user ){ |
| 48 |
// if login action initialized by our form |
| 49 |
if( isset( $_POST['wppb_login'] ) ){ |
| 50 |
if( is_wp_error( $user ) ){ |
| 51 |
// if we don't have a succesfull login we must redirect to the url of the form, so make sure this happens |
| 52 |
$redirect_to = $_POST['wppb_request_url']; |
| 53 |
$request_form_location = $_POST['wppb_form_location']; |
| 54 |
$error_string = $user->get_error_message(); |
| 55 |
|
| 56 |
// if login with email is enabled change the word username with email |
| 57 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 58 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){ |
| 59 |
$siteURL = home_url( '/wp-login.php?action=lostpassword' ); |
| 60 |
$siteURL = apply_filters( 'wppb_pre_login_url_filter', $siteURL ); |
| 61 |
|
| 62 |
if( $user->get_error_code() == 'incorrect_password' ){ |
| 63 |
$error_string = '<strong>'. __( 'ERROR', 'profilebuilder' ) .'</strong>: '. __( 'The password you entered is incorrect.', 'profilebuilder' ) .' '; |
| 64 |
$error_string .= '<a href="'. $siteURL .'" title="'. __( 'Password Lost and Found.', 'profilebuilder' ) .'">'. __( 'Lost your password', 'profilebuilder' ) .'</a>?'; |
| 65 |
} |
| 66 |
|
| 67 |
/* maybe also change the recover password link */ |
| 68 |
$error_string = str_replace( home_url( '/wp-login.php?action=lostpassword' ), $siteURL, $error_string ); |
| 69 |
|
| 70 |
$error_string = str_replace( 'username', 'email', $error_string ); |
| 71 |
} |
| 72 |
|
| 73 |
// if the error string is empty it means that none of the fields were completed |
| 74 |
if( empty( $error_string ) ){ |
| 75 |
$error_string = '<strong>'. __( 'ERROR', 'profilebuilder' ) .'</strong>: '. __( 'Both fields are empty.', 'profilebuilder' ) .' '; |
| 76 |
$error_string = apply_filters( 'wppb_login_empty_fields_error_message', $error_string ); |
| 77 |
} |
| 78 |
|
| 79 |
$error_string = apply_filters( 'wppb_login_wp_error_message', $error_string, $user ); |
| 80 |
|
| 81 |
// encode the error string and send it as a GET parameter |
| 82 |
$arr_params = array ( 'loginerror' => urlencode( base64_encode( $error_string ) ), 'request_form_location' => $request_form_location ); |
| 83 |
$redirect_to = add_query_arg( $arr_params, $redirect_to ); |
| 84 |
wp_safe_redirect( $redirect_to ); |
| 85 |
} |
| 86 |
else{ |
| 87 |
// we don't have an error make sure to remove the error from the query arg |
| 88 |
$redirect_to = remove_query_arg( 'loginerror', $redirect_to ); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $redirect_to; |
| 93 |
} |
| 94 |
add_filter( 'login_redirect', 'wppb_login_redirect', 10, 3 ); |
| 95 |
|
| 96 |
|
| 97 |
/* shortcode function */ |
| 98 |
function wppb_front_end_login( $atts ){ |
| 99 |
extract( shortcode_atts( array( 'display' => true, 'redirect' => '' ), $atts ) ); |
| 100 |
|
| 101 |
$wppb_generalSettings = get_option('wppb_general_settings'); |
| 102 |
|
| 103 |
if( !is_user_logged_in() ){ |
| 104 |
// set up the form arguments |
| 105 |
$form_args = array( 'echo' => false, 'id_submit' => 'wppb-submit' ); |
| 106 |
|
| 107 |
// maybe set up the redirect argument |
| 108 |
if( empty( $redirect ) ){ |
| 109 |
$wppb_module_settings = get_option( 'wppb_module_settings' ); |
| 110 |
if( $wppb_module_settings['wppb_customRedirect'] == 'show' ){ |
| 111 |
//check to see if the redirect location is not an empty string and is activated |
| 112 |
$login_redirect_settings = get_option( 'customRedirectSettings' ); |
| 113 |
|
| 114 |
// set up the redirect argument to our redirect page |
| 115 |
if( ( trim( $login_redirect_settings['afterLoginTarget'] ) != '' ) && ( $login_redirect_settings['afterLogin'] == 'yes' ) ){ |
| 116 |
$redirect_to = trim( $login_redirect_settings['afterLoginTarget'] ); |
| 117 |
if( wppb_check_missing_http( $redirect_to ) ) |
| 118 |
$redirect_to = 'http://'. $redirect_to; |
| 119 |
|
| 120 |
$form_args['redirect'] = $redirect_to; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
else |
| 125 |
$form_args['redirect'] = trim( $redirect ); |
| 126 |
|
| 127 |
// change the label argument for username is login with email is enabled |
| 128 |
if ( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ) |
| 129 |
$form_args['label_username'] = __( 'Email', 'profilebuilder' ); |
| 130 |
|
| 131 |
// initialize our form variable |
| 132 |
$login_form = ''; |
| 133 |
|
| 134 |
// display our login errors |
| 135 |
if( isset( $_GET['loginerror'] ) || isset( $_POST['loginerror'] ) ){ |
| 136 |
$loginerror = isset( $_GET['loginerror'] ) ? $_GET['loginerror'] : $_POST['loginerror']; |
| 137 |
$loginerror = '<p class="wppb-error">'.urldecode( base64_decode( $loginerror ) ).'</p><!-- .error -->'; |
| 138 |
if( isset( $_GET['request_form_location'] ) ){ |
| 139 |
if( $_GET['request_form_location'] == 'widget' && !in_the_loop() ){ |
| 140 |
$login_form .= $loginerror; |
| 141 |
} |
| 142 |
elseif( $_GET['request_form_location'] == 'page' && in_the_loop() ){ |
| 143 |
$login_form .= $loginerror; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// build our form |
| 149 |
$login_form .= '<div id="wppb-login-wrap" class="wppb-user-forms">'; |
| 150 |
$login_form .= wp_login_form( $form_args ); |
| 151 |
$login_form .= '</div>'; |
| 152 |
|
| 153 |
return $login_form; |
| 154 |
|
| 155 |
}else{ |
| 156 |
$user_ID = get_current_user_id(); |
| 157 |
$wppb_user = get_userdata( $user_ID ); |
| 158 |
|
| 159 |
if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ) |
| 160 |
$display_name = $wppb_user->user_email; |
| 161 |
|
| 162 |
elseif($wppb_user->display_name !== '') |
| 163 |
$display_name = $wppb_user->user_login; |
| 164 |
|
| 165 |
else |
| 166 |
$display_name = $wppb_user->display_name; |
| 167 |
|
| 168 |
$loged_in_message = '<p class="wppb-alert">'.sprintf(__( 'You are currently logged in as %1$s. %2$s', 'profilebuilder' ), '<a href="'.$authorPostsUrl = get_author_posts_url( $wppb_user->ID ).'" title="'.$display_name.'">'.$display_name.'</a>', '<a href="'.wp_logout_url( $redirectTo = wppb_curpageurl() ).'" title="'.__( 'Log out of this account', 'profilebuilder' ).'">'. __( 'Log out', 'profilebuilder').' »</a>' ) . '</p><!-- .alert-->'; |
| 169 |
|
| 170 |
return apply_filters( 'wppb_login_message', $loged_in_message, $wppb_user->ID, $display_name ); |
| 171 |
|
| 172 |
} |
| 173 |
} |