| 1 |
<?php |
| 2 |
class Profile_Builder_Form_Creator{ |
| 3 |
private $defaults = array( |
| 4 |
'form_type' => '', |
| 5 |
'form_fields' => array(), |
| 6 |
'form_name' => '', |
| 7 |
'role' => '', //used only for the register-form settings |
| 8 |
); |
| 9 |
private $args; |
| 10 |
|
| 11 |
|
| 12 |
// Constructor method for the class |
| 13 |
function __construct( $args ) { |
| 14 |
// Merge the input arguments and the defaults |
| 15 |
$this->args = wp_parse_args( $args, $this->defaults ); |
| 16 |
|
| 17 |
global $wppb_shortcode_on_front; |
| 18 |
$wppb_shortcode_on_front = true; |
| 19 |
|
| 20 |
if( empty( $this->args['form_fields'] ) ) |
| 21 |
$this->args['form_fields'] = apply_filters( 'wppb_change_form_fields', get_option( 'wppb_manage_fields' ), $this->args ); |
| 22 |
|
| 23 |
if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/default-fields/default-fields.php' ) ) |
| 24 |
require_once( WPPB_PLUGIN_DIR.'/front-end/default-fields/default-fields.php' ); |
| 25 |
|
| 26 |
if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' ) ) |
| 27 |
require_once( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' ); |
| 28 |
|
| 29 |
$this->wppb_retrieve_custom_settings(); |
| 30 |
|
| 31 |
add_action( 'wp_footer', array( &$this, 'wppb_print_script' ) ); //print scripts |
| 32 |
} |
| 33 |
|
| 34 |
function wppb_retrieve_custom_settings(){ |
| 35 |
$this->args['login_after_register'] = apply_filters( 'wppb_automatically_login_after_register', 'No' ); //used only for the register-form settings |
| 36 |
$this->args['redirect_activated'] = apply_filters( 'wppb_redirect_default_setting', '' ); |
| 37 |
$this->args['redirect_url'] = apply_filters( 'wppb_redirect_default_location', wppb_curpageurl() ); |
| 38 |
/* for register forms check to see if we have a custom redirect "Redirect After Register" */ |
| 39 |
if( $this->args['form_type'] == 'register' ){ |
| 40 |
$wppb_module_settings = get_option( 'wppb_module_settings' ); |
| 41 |
if ( isset( $wppb_module_settings['wppb_customRedirect'] ) && ( $wppb_module_settings['wppb_customRedirect'] == 'show' ) ){ |
| 42 |
$custom_redirect = get_option( 'customRedirectSettings' ); |
| 43 |
if ( isset( $custom_redirect['afterRegister'] ) && ( $custom_redirect['afterRegister'] == 'yes' ) && ( trim( $custom_redirect['afterRegisterTarget'] ) != '' ) ){ |
| 44 |
$this->args['redirect_url'] = $this->args['custom_redirect_after_register_url'] = apply_filters( 'wppb_redirect_default_location', $custom_redirect['afterRegisterTarget'] ); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
$this->args['redirect_delay'] = apply_filters( 'wppb_redirect_default_duration', 3 ); |
| 49 |
|
| 50 |
if ( $this->args['form_name'] != 'unspecified' ){ |
| 51 |
$post_type = ( ( $this->args['form_type'] == 'register' ) ? 'wppb-rf-cpt' : 'wppb-epf-cpt' ); |
| 52 |
$meta_name = ( ( $this->args['form_type'] == 'register' ) ? 'wppb_rf_page_settings' : 'wppb_epf_page_settings' ); |
| 53 |
|
| 54 |
$ep_r_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' => apply_filters ( 'wppb_get_ep_r_posts_on_front_end', array( 'publish' ) ), 'post_type' => $post_type, 'orderby' => 'post_date', 'order' => 'ASC' ) ); |
| 55 |
foreach ( $ep_r_posts as $key => $value ){ |
| 56 |
if ( trim( Wordpress_Creation_Kit_PB::wck_generate_slug( $value->post_title ) ) == $this->args['form_name'] ){ |
| 57 |
$page_settings = get_post_meta( $value->ID, $meta_name, true ); |
| 58 |
|
| 59 |
if( !empty( $page_settings[0]['set-role'] ) ){ |
| 60 |
if( $page_settings[0]['set-role'] == 'default role' ){ |
| 61 |
$selected_role = trim( get_option( 'default_role' ) ); |
| 62 |
} |
| 63 |
else |
| 64 |
$selected_role = $page_settings[0]['set-role']; |
| 65 |
} |
| 66 |
|
| 67 |
$this->args['role'] = ( isset( $selected_role ) ? $selected_role : $this->args['role'] ); |
| 68 |
$this->args['login_after_register'] = ( isset( $page_settings[0]['automatically-log-in'] ) ? $page_settings[0]['automatically-log-in'] : $this->args['login_after_register'] ); |
| 69 |
$this->args['redirect_activated'] = ( isset( $page_settings[0]['redirect'] ) ? $page_settings[0]['redirect'] : $this->args['redirect_activated'] ); |
| 70 |
$this->args['redirect_url'] = ( isset( $page_settings[0]['url'] ) ? $page_settings[0]['url'] : $this->args['redirect_url'] ); |
| 71 |
$this->args['redirect_delay'] = ( isset( $page_settings[0]['display-messages'] ) ? $page_settings[0]['display-messages'] : $this->args['redirect_delay'] ); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
function wppb_form_logic() { |
| 78 |
if( $this->args['form_type'] == 'register' ){ |
| 79 |
$registration = apply_filters ( 'wppb_register_setting_override', get_option( 'users_can_register' ) ); |
| 80 |
|
| 81 |
if ( !is_user_logged_in() ){ |
| 82 |
if ( !$registration ) |
| 83 |
echo apply_filters( 'wppb_register_pre_form_message', '<p class="alert" id="wppb_register_pre_form_message">'.__( 'Only an administrator can add new users.', 'profilebuilder').'</p>' ); |
| 84 |
|
| 85 |
elseif ( $registration ){ |
| 86 |
$this->wppb_form_content( apply_filters( 'wppb_register_pre_form_message', '' ) ); |
| 87 |
} |
| 88 |
|
| 89 |
}else{ |
| 90 |
$current_user_capability = apply_filters ( 'wppb_registration_user_capability', 'create_users' ); |
| 91 |
|
| 92 |
if ( current_user_can( $current_user_capability ) && $registration ) |
| 93 |
$this->wppb_form_content( apply_filters( 'wppb_register_pre_form_message', '<p class="alert" id="wppb_register_pre_form_message">'.__( 'Users can register themselves or you can manually create users here.', 'profilebuilder').'</p>' ) ); |
| 94 |
|
| 95 |
elseif ( current_user_can( $current_user_capability ) && !$registration ) |
| 96 |
$this->wppb_form_content( apply_filters( 'wppb_register_pre_form_message', '<p class="alert" id="wppb_register_pre_form_message">'.__( 'Users cannot currently register themselves, but you can manually create users here.', 'profilebuilder').'</p>' ) ); |
| 97 |
|
| 98 |
elseif ( !current_user_can( $current_user_capability ) ){ |
| 99 |
global $user_ID; |
| 100 |
|
| 101 |
$userdata = get_userdata( $user_ID ); |
| 102 |
$display_name = ( ( $userdata->data->display_name == '' ) ? $userdata->data->user_login : $userdata->data->display_name ); |
| 103 |
|
| 104 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 105 |
if ( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ) |
| 106 |
$display_name = $userdata->data->user_email; |
| 107 |
|
| 108 |
echo apply_filters( 'wppb_register_pre_form_message', '<p class="alert" id="wppb_register_pre_form_message">'.sprintf( __( "You are currently logged in as %1s. You don't need another account. %2s", 'profilebuilder' ), '<a href="'.get_author_posts_url( $user_ID ).'" title="'.$display_name.'">'.$display_name.'</a>', '<a href="'.wp_logout_url( get_permalink() ).'" title="'.__( 'Log out of this account.', 'profilebuilder' ).'">'.__( 'Logout', 'profilebuilder' ).' »</a>' ).'</p>', $user_ID ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
}elseif ( $this->args['form_type'] == 'edit_profile' ){ |
| 113 |
if ( !is_user_logged_in() ) |
| 114 |
echo apply_filters( 'wppb_edit_profile_user_not_logged_in_message', '<p class="warning" id="wppb_edit_profile_user_not_logged_in_message">'.__( 'You must be logged in to edit your profile.', 'profilebuilder' ) .'</p>' ); |
| 115 |
|
| 116 |
elseif ( is_user_logged_in() ) |
| 117 |
$this->wppb_form_content( apply_filters( 'wppb_edit_profile_logged_in_user_message', '' ) ); |
| 118 |
|
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
function wppb_get_redirect(){ |
| 123 |
if ( $this->args['login_after_register'] == 'Yes' ) |
| 124 |
return $this->wppb_log_in_user(); |
| 125 |
if ( $this->args['redirect_activated'] == 'No' || ( $this->args['form_type'] == 'edit_profile' && $this->args['form_name'] == 'unspecified' ) || ( $this->args['form_type'] == 'register' && $this->args['form_name'] == 'unspecified' && wppb_curpageurl() == $this->args['redirect_url'] ) ) |
| 126 |
return ''; |
| 127 |
|
| 128 |
/* if we don't have a preference on the form for redirect then if we have a custom redirect "after register redirect" option redirect to that if not don't do anything */ |
| 129 |
if ( $this->args['redirect_activated'] == '-' ){ |
| 130 |
if( !empty( $this->args['custom_redirect_after_register_url'] ) ) |
| 131 |
$this->args['redirect_url'] = $this->args['custom_redirect_after_register_url']; |
| 132 |
else return ''; |
| 133 |
} |
| 134 |
|
| 135 |
$redirect_location = ( wppb_check_missing_http( $this->args['redirect_url'] ) ? 'http://'.$this->args['redirect_url'] : $this->args['redirect_url'] ); |
| 136 |
|
| 137 |
$redirect_url = apply_filters( 'wppb_redirect_url', '<a href="'.$redirect_location.'">'.__( 'here', 'profilebuilder' ).'</a>' ); |
| 138 |
|
| 139 |
return apply_filters ( 'wppb_redirect_message_before_returning', '<p class="redirect_message">'.sprintf( __( 'You will soon be redirected automatically. If you see this page for more than %1$d seconds, please click %2$s.%3$s', 'profilebuilder' ), $this->args['redirect_delay'], $redirect_url, '<meta http-equiv="Refresh" content="'.$this->args['redirect_delay'].';url='.$redirect_location.'" />' ).'</p>', $this->args ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
function wppb_log_in_user(){ |
| 144 |
if ( is_user_logged_in() ) |
| 145 |
return; |
| 146 |
|
| 147 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 148 |
if ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ) |
| 149 |
return; |
| 150 |
|
| 151 |
if ( isset( $wppb_general_settings['adminApproval'] ) && ( $wppb_general_settings['adminApproval'] == 'yes' ) ) |
| 152 |
return; |
| 153 |
|
| 154 |
if( !empty( $_POST['username'] ) ) |
| 155 |
$username = trim( $_POST['username'] ); |
| 156 |
$password = trim( $_POST['passw1'] ); |
| 157 |
|
| 158 |
if( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ){ |
| 159 |
$username = apply_filters( 'wppb_generated_random_username', Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $_POST['email'] ) ), $_POST['email'] ); |
| 160 |
} |
| 161 |
|
| 162 |
/* get user id */ |
| 163 |
$user = get_user_by( 'login', $username ); |
| 164 |
$nonce = wp_create_nonce( 'autologin-'.$user->ID.'-'.(int)( time() / 60 ) ); |
| 165 |
|
| 166 |
/* define redirect location */ |
| 167 |
if ( $this->args['redirect_activated'] == 'No' ){ |
| 168 |
$location = home_url(); |
| 169 |
}else if ( $this->args['redirect_activated'] == '-' ){ |
| 170 |
if( !empty( $this->args['custom_redirect_after_register_url'] ) ) |
| 171 |
$location = $this->args['custom_redirect_after_register_url']; |
| 172 |
else |
| 173 |
$location = home_url(); |
| 174 |
} |
| 175 |
else{ |
| 176 |
$location = ( wppb_check_missing_http( $this->args['redirect_url'] ) ? 'http://'.$this->args['redirect_url'] : $this->args['redirect_url'] ); |
| 177 |
} |
| 178 |
|
| 179 |
$location .= "/?autologin=true&uid=$user->ID&_wpnonce=$nonce"; |
| 180 |
|
| 181 |
return "<script> window.location.replace('$location'); </script>"; |
| 182 |
} |
| 183 |
|
| 184 |
function wppb_form_content( $message ){ |
| 185 |
$field_check_errors = array(); |
| 186 |
|
| 187 |
if( isset( $_REQUEST['action'] ) ){ |
| 188 |
$field_check_errors = $this->wppb_test_required_form_values( $_REQUEST ); |
| 189 |
if( empty( $field_check_errors ) ){ |
| 190 |
// we only have a $user_id on default registration (no email confirmation, no multisite) |
| 191 |
$user_id = $this->wppb_save_form_values( $_REQUEST ); |
| 192 |
|
| 193 |
if ( ( 'POST' == $_SERVER['REQUEST_METHOD'] ) && ( $_POST['action'] == $this->args['form_type'] ) ){ |
| 194 |
|
| 195 |
$form_message_tpl_start = apply_filters( 'wppb_form_message_tpl_start', '<p class="alert" id="wppb_form_success_message">'); |
| 196 |
$form_message_tpl_end = apply_filters( 'wppb_form_message_tpl_end', '</p>'); |
| 197 |
|
| 198 |
if( $this->args['form_type'] == 'register' ){ |
| 199 |
// ec = email confirmation setting |
| 200 |
// aa = admin approval setting |
| 201 |
$wppb_general_settings = get_option( 'wppb_general_settings', 'false' ); |
| 202 |
if ( $wppb_general_settings ){ |
| 203 |
if( !empty( $wppb_general_settings['emailConfirmation'] ) ) |
| 204 |
$wppb_email_confirmation = $wppb_general_settings['emailConfirmation']; |
| 205 |
else |
| 206 |
$wppb_email_confirmation = 'no'; |
| 207 |
if( !empty( $wppb_general_settings['adminApproval'] ) ) |
| 208 |
$wppb_admin_approval = $wppb_general_settings['adminApproval']; |
| 209 |
else |
| 210 |
$wppb_admin_approval = 'no'; |
| 211 |
$account_management_settings = 'ec-' . $wppb_email_confirmation . '_' . 'aa-' . $wppb_admin_approval; |
| 212 |
} else { |
| 213 |
$account_management_settings = 'ec-no_aa-no'; |
| 214 |
} |
| 215 |
|
| 216 |
if ( isset( $_POST['username'] ) && ( trim( $_POST['username'] ) != '' ) ){ |
| 217 |
$account_name = trim( $_POST['username'] ); |
| 218 |
} elseif( isset( $_POST['email'] ) && ( trim( $_POST['email'] ) != '' ) ) { |
| 219 |
$account_name = trim( $_POST['email'] ); |
| 220 |
} |
| 221 |
|
| 222 |
switch ( $account_management_settings ){ |
| 223 |
case 'ec-no_aa-no': |
| 224 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "The account %1s has been successfully created!", 'profilebuilder' ), $account_name ) ); |
| 225 |
break; |
| 226 |
case 'ec-yes_aa-no': |
| 227 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "Before you can access your account %1s, you need to confirm your email address. Please check your inbox and click the activation link.", 'profilebuilder' ), $account_name ) ); |
| 228 |
break; |
| 229 |
case 'ec-no_aa-yes': |
| 230 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "Before you can access your account %1s, an administrator has to approve it. You will be notified via email.", 'profilebuilder' ), $account_name ) ); |
| 231 |
break; |
| 232 |
case 'ec-yes_aa-yes': |
| 233 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "Before you can access your account %1s, you need to confirm your email address. Please check your inbox and click the activation link.", 'profilebuilder' ), $account_name ) ); |
| 234 |
break; |
| 235 |
} |
| 236 |
$redirect = apply_filters( 'wppb_register_redirect', $this->wppb_get_redirect() ); |
| 237 |
echo $form_message_tpl_start . $wppb_register_success_message . $form_message_tpl_end . $redirect; |
| 238 |
//action hook after registration success |
| 239 |
do_action('wppb_register_success', $_REQUEST, $this->args['form_name'], $user_id); |
| 240 |
return; |
| 241 |
} elseif ( $this->args['form_type'] == 'edit_profile' ){ |
| 242 |
$redirect = apply_filters( 'wppb_edit_profile_redirect', $this->wppb_get_redirect() ); |
| 243 |
echo $form_message_tpl_start . apply_filters( 'wppb_edit_profile_success_message', __( 'Your profile has been successfully updated!', 'profilebuilder' ) ) . $form_message_tpl_end . $redirect; |
| 244 |
//action hook after edit profile success |
| 245 |
do_action('wppb_edit_profile_success', $_REQUEST, $this->args['form_name'], $user_id); |
| 246 |
if ( apply_filters( 'wppb_no_form_after_profile_update', false ) ) |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
}else |
| 253 |
echo $message.apply_filters( 'wppb_general_top_error_message', '<p id="wppb_general_top_error_message">'.__( 'There was an error in the submitted form', 'profilebuilder' ).'</p>' ); |
| 254 |
|
| 255 |
}else |
| 256 |
echo $message; |
| 257 |
|
| 258 |
// use this action hook to add extra content before the register form |
| 259 |
do_action( 'wppb_before_'.$this->args['form_type'].'_fields' ); |
| 260 |
?> |
| 261 |
<form enctype="multipart/form-data" method="post" id="<?php if( $this->args['form_type'] == 'register' ) echo 'wppb-register-user'; else if( $this->args['form_type'] == 'edit_profile' ) echo 'wppb-edit-user' ?>" class="wppb-user-forms" action="<?php echo wppb_curpageurl(); ?>"> |
| 262 |
<?php |
| 263 |
echo apply_filters( 'wppb_before_form_fields', '<ul>' ); |
| 264 |
$this->wppb_output_form_fields( $_REQUEST, $field_check_errors ); |
| 265 |
echo apply_filters( 'wppb_after_form_fields', '</ul>' ); |
| 266 |
|
| 267 |
echo apply_filters( 'wppb_before_send_credentials_checkbox', '<ul>' ); |
| 268 |
$this->wppb_add_send_credentials_checkbox( $_REQUEST, $this->args['form_type'] ); |
| 269 |
echo apply_filters( 'wppb_after_send_credentials_checkbox', '</ul>' ); |
| 270 |
?> |
| 271 |
<p class="form-submit"> |
| 272 |
<?php |
| 273 |
if( $this->args['form_type'] == 'register' ) |
| 274 |
$button_name = ( current_user_can( 'create_user' ) ? __( 'Add User', 'profilebuilder' ) : __( 'Register', 'profilebuilder' ) ); |
| 275 |
|
| 276 |
elseif( $this->args['form_type'] == 'edit_profile' ) |
| 277 |
$button_name = __( 'Update', 'profilebuilder' ); |
| 278 |
?> |
| 279 |
<input name="<?php echo $this->args['form_type']; ?>" type="submit" id="<?php echo $this->args['form_type']; ?>" class="submit button" value="<?php echo apply_filters( 'wppb_'. $this->args['form_type'] .'_button_name', $button_name ); ?>" /> |
| 280 |
<input name="action" type="hidden" id="action" value="<?php echo $this->args['form_type']; ?>" /> |
| 281 |
<input name="form_name" type="hidden" id="form_name" value="<?php echo $this->args['form_name']; ?>" /> |
| 282 |
</p><!-- .form-submit --> |
| 283 |
<?php wp_nonce_field( 'verify_form_submission', $this->args['form_type'].'_nonce_field' ); ?> |
| 284 |
</form> |
| 285 |
<?php |
| 286 |
// use this action hook to add extra content after the register form |
| 287 |
do_action( 'wppb_after_'. $this->args['form_type'] .'_fields' ); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
function wppb_output_form_fields( $global_request, $field_check_errors ){ |
| 292 |
|
| 293 |
$output_fields = ''; |
| 294 |
|
| 295 |
if( !empty( $this->args['form_fields'] ) ){ |
| 296 |
foreach( $this->args['form_fields'] as $field ){ |
| 297 |
$error_var = ( ( array_key_exists( $field['id'], $field_check_errors ) ) ? ' wppb-field-error' : '' ); |
| 298 |
$specific_message = ( ( array_key_exists( $field['id'], $field_check_errors ) ) ? $field_check_errors[$field['id']] : '' ); |
| 299 |
|
| 300 |
$output_fields .= apply_filters( 'wppb_output_before_form_field', '<li class="wppb-form-field wppb-'. Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ) .$error_var.'" id="wppb-form-element-'. $field['id'] .'">', $field, $error_var ); |
| 301 |
$output_fields .= apply_filters( 'wppb_output_form_field_'.Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ), '', $this->args['form_type'], $field, get_current_user_id(), $field_check_errors, $global_request ); |
| 302 |
$output_fields .= apply_filters( 'wppb_output_specific_error_message', $specific_message ); |
| 303 |
$output_fields .= apply_filters( 'wppb_output_after_form_field', '</li>', $field ); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
echo apply_filters( 'wppb_output_fields_filter', $output_fields ); |
| 308 |
} |
| 309 |
|
| 310 |
function wppb_print_script(){ |
| 311 |
if( $this->args['form_type'] == 'edit_profile' ){ |
| 312 |
wp_register_script( 'wppb-edit-profile-scripts', WPPB_PLUGIN_URL . 'assets/js/jquery-edit-profile.js', array( 'jquery' ), PROFILE_BUILDER_VERSION ); |
| 313 |
|
| 314 |
$translation_array = array( 'avatar' => __( 'The avatar was successfully deleted!', 'profilebuilder' ), 'attachment' => __( 'The following attachment was successfully deleted:', 'profilebuilder' ) ); |
| 315 |
wp_localize_script( 'wppb-edit-profile-scripts', 'ep', $translation_array ); |
| 316 |
|
| 317 |
wp_print_scripts( 'wppb-edit-profile-scripts' ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
function wppb_add_send_credentials_checkbox ( $request_data, $form ){ |
| 322 |
if ( $form == 'edit_profile' ) |
| 323 |
echo ''; |
| 324 |
|
| 325 |
else{ |
| 326 |
$checkbox = apply_filters( 'wppb_send_credentials_checkbox_logic', '<li class="wppb-form-field wppb-send-credentials-checkbox"><label for="send_credentials_via_email"><input id="send_credentials_via_email" type="checkbox" name="send_credentials_via_email" value="sending"'.( ( isset( $request_data['send_credentials_via_email'] ) && ( $request_data['send_credentials_via_email'] == 'sending' ) ) ? ' checked' : '' ).'/>'.__( 'Send these credentials via email.', 'profilebuilder').'</label></li>', $request_data, $form ); |
| 327 |
|
| 328 |
if ( !is_multisite() ){ |
| 329 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 330 |
|
| 331 |
echo ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ? '' : $checkbox ); |
| 332 |
|
| 333 |
}else |
| 334 |
echo ''; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
function wppb_test_required_form_values( $global_request ){ |
| 340 |
$output_field_errors = array(); |
| 341 |
|
| 342 |
if( !empty( $this->args['form_fields'] ) ){ |
| 343 |
foreach( $this->args['form_fields'] as $field ){ |
| 344 |
$error_for_field = apply_filters( 'wppb_check_form_field_'.Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ), '', $field, $global_request, $this->args['form_type'] ); |
| 345 |
|
| 346 |
if( !empty( $error_for_field ) ) |
| 347 |
$output_field_errors[$field['id']] = '<span class="wppb-form-error">' . $error_for_field . '</span>'; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return apply_filters( 'wppb_output_field_errors_filter', $output_field_errors ); |
| 352 |
} |
| 353 |
|
| 354 |
function wppb_save_form_values( $global_request ){ |
| 355 |
$user_id = get_current_user_id(); |
| 356 |
$userdata = apply_filters( 'wppb_build_userdata', array(), $global_request ); |
| 357 |
$new_user_signup = false; |
| 358 |
|
| 359 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 360 |
if( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ){ |
| 361 |
$userdata['user_login'] = apply_filters( 'wppb_generated_random_username', Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $userdata['user_email'] ) ), $userdata['user_email'] ); |
| 362 |
} |
| 363 |
|
| 364 |
if( $this->args['form_type'] == 'register' ){ |
| 365 |
if ( !is_multisite() ){ |
| 366 |
if ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ){ |
| 367 |
$new_user_signup = true; |
| 368 |
$multisite_message = true; |
| 369 |
$userdata = $this->wppb_add_custom_field_values( $global_request, $userdata, $this->args['form_fields'] ); |
| 370 |
$userdata['role'] = $this->args['role']; |
| 371 |
$userdata['user_pass'] = base64_encode( $userdata['user_pass'] ); |
| 372 |
wppb_signup_user( $userdata['user_login'], $userdata['user_email'], $userdata ); |
| 373 |
|
| 374 |
}else{ |
| 375 |
$userdata['role'] = $this->args['role']; |
| 376 |
$userdata = wp_unslash( $userdata ); |
| 377 |
$user_id = wp_insert_user( $userdata ); |
| 378 |
} |
| 379 |
|
| 380 |
}else{ |
| 381 |
$new_user_signup = true; |
| 382 |
$multisite_message = true; |
| 383 |
$userdata = $this->wppb_add_custom_field_values( $global_request, $userdata, $this->args['form_fields'] ); |
| 384 |
$userdata['role'] = $this->args['role']; |
| 385 |
$userdata['user_pass'] = base64_encode( $userdata['user_pass'] ); |
| 386 |
$userdata = wp_unslash( $userdata ); |
| 387 |
wppb_signup_user( $userdata['user_login'], $userdata['user_email'], $userdata ); |
| 388 |
} |
| 389 |
|
| 390 |
}elseif( $this->args['form_type'] == 'edit_profile' ){ |
| 391 |
$userdata['ID'] = get_current_user_id(); |
| 392 |
$userdata = wp_unslash( $userdata ); |
| 393 |
/* if the user changes his password then we can't send it to the wp_update_user() function or |
| 394 |
the user will be logged out and won't be logged in again because we call wp_update_user() after |
| 395 |
the headers were sent( in the content as a shortcode ) */ |
| 396 |
if( isset( $userdata['user_pass'] ) && !empty( $userdata['user_pass'] ) ){ |
| 397 |
unset($userdata['user_pass']); |
| 398 |
} |
| 399 |
wp_update_user( $userdata ); |
| 400 |
} |
| 401 |
|
| 402 |
if( !empty( $this->args['form_fields'] ) && !$new_user_signup ){ |
| 403 |
foreach( $this->args['form_fields'] as $field ){ |
| 404 |
do_action( 'wppb_save_form_field', $field, $user_id, $global_request, $this->args['form_type'] ); |
| 405 |
} |
| 406 |
|
| 407 |
if ( $this->args['form_type'] == 'register' ){ |
| 408 |
if ( !is_wp_error( $user_id ) ){ |
| 409 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 410 |
if( isset( $global_request['send_credentials_via_email'] ) && ( $global_request['send_credentials_via_email'] == 'sending' ) ) |
| 411 |
$send_credentials_via_email = 'sending'; |
| 412 |
else |
| 413 |
$send_credentials_via_email = ''; |
| 414 |
wppb_notify_user_registration_email( get_bloginfo( 'name' ), ( isset( $global_request['username'] ) ? trim( $global_request['username'] ) : trim( $global_request['email'] ) ), trim( $global_request['email'] ), $send_credentials_via_email, trim( $global_request['passw1'] ), ( isset( $wppb_general_settings['adminApproval'] ) ? $wppb_general_settings['adminApproval'] : 'no' ) ); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
return $user_id; |
| 419 |
} |
| 420 |
|
| 421 |
function wppb_add_custom_field_values( $global_request, $meta, $form_properties ){ |
| 422 |
if( !empty( $this->args['form_fields'] ) ){ |
| 423 |
foreach( $this->args['form_fields'] as $field ){ |
| 424 |
if( !empty( $field['meta-name'] ) ){ |
| 425 |
$posted_value = ( !empty( $global_request[$field['meta-name']] ) ? $global_request[$field['meta-name']] : '' ); |
| 426 |
$meta[$field['meta-name']] = apply_filters( 'wppb_add_to_user_signup_form_field_'.Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ), $posted_value, $field, $global_request ); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
return $meta; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Handle toString method |
| 436 |
* |
| 437 |
* @since 2.0 |
| 438 |
* |
| 439 |
* @return string $html html for the form. |
| 440 |
*/ |
| 441 |
public function __toString() { |
| 442 |
ob_start(); |
| 443 |
$this->wppb_form_logic(); |
| 444 |
$html = ob_get_clean(); |
| 445 |
return "{$html}"; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/* set action for automatic login after registration */ |
| 450 |
add_action( 'init', 'wppb_autologin_after_registration' ); |
| 451 |
function wppb_autologin_after_registration(){ |
| 452 |
if( isset( $_GET['autologin'] ) && isset( $_GET['uid'] ) ){ |
| 453 |
$uid = $_GET['uid']; |
| 454 |
$nonce = $_REQUEST['_wpnonce']; |
| 455 |
|
| 456 |
$arr_params = array( 'autologin', 'uid', '_wpnonce' ); |
| 457 |
$current_page_url = remove_query_arg( $arr_params, wppb_curpageurl() ); |
| 458 |
|
| 459 |
if ( ! ( wp_verify_nonce( $nonce , 'autologin-'.$uid.'-'.(int)( time() / 60 ) ) || wp_verify_nonce( $nonce , 'autologin-'.$uid.'-'.(int)( time() / 60 ) - 1 ) ) ){ |
| 460 |
wp_redirect( $current_page_url ); |
| 461 |
exit; |
| 462 |
} else { |
| 463 |
wp_set_auth_cookie( $uid ); |
| 464 |
wp_redirect( $current_page_url ); |
| 465 |
exit; |
| 466 |
} |
| 467 |
} |
| 468 |
} |