| 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 |
'redirect_url' => '', |
| 9 |
'logout_redirect_url' => '', //used only for the register-form settings |
| 10 |
'redirect_priority' => 'normal', |
| 11 |
'ID' => null |
| 12 |
); |
| 13 |
public $args; |
| 14 |
|
| 15 |
|
| 16 |
// Constructor method for the class |
| 17 |
function __construct( $args ) { |
| 18 |
|
| 19 |
/* we should stop the execution of the forms if they are in the wp_head hook because it should not be there. |
| 20 |
SEO plugins can execute shortcodes in the auto generated descriptions */ |
| 21 |
global $wp_current_filter; |
| 22 |
if( !empty( $wp_current_filter ) && is_array( $wp_current_filter ) ){ |
| 23 |
foreach( $wp_current_filter as $filter ){ |
| 24 |
if( $filter == 'wp_head' ) |
| 25 |
return; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
// Merge the input arguments and the defaults |
| 30 |
$this->args = wp_parse_args( $args, $this->defaults ); |
| 31 |
|
| 32 |
/* set up the ID here if it is a multi form */ |
| 33 |
if( $this->args['form_name'] != 'unspecified' ){ |
| 34 |
$this->args['ID'] = Profile_Builder_Form_Creator::wppb_get_form_id_from_form_name( $this->args['form_name'], $this->args['form_type'] ); |
| 35 |
} |
| 36 |
|
| 37 |
global $wppb_shortcode_on_front; |
| 38 |
$wppb_shortcode_on_front = true; |
| 39 |
|
| 40 |
if( empty( $this->args['form_fields'] ) ) |
| 41 |
$this->args['form_fields'] = apply_filters( 'wppb_change_form_fields', get_option( 'wppb_manage_fields' ), $this->args ); |
| 42 |
|
| 43 |
if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/default-fields/default-fields.php' ) ) |
| 44 |
require_once( WPPB_PLUGIN_DIR.'/front-end/default-fields/default-fields.php' ); |
| 45 |
|
| 46 |
if ( file_exists ( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' ) ) |
| 47 |
require_once( WPPB_PLUGIN_DIR.'/front-end/extra-fields/extra-fields.php' ); |
| 48 |
|
| 49 |
$this->wppb_retrieve_custom_settings(); |
| 50 |
|
| 51 |
if( ( !is_multisite() && current_user_can( 'edit_users' ) ) || ( is_multisite() && current_user_can( 'manage_network' ) ) ) |
| 52 |
add_action( 'wppb_before_edit_profile_fields', array( &$this, 'wppb_edit_profile_select_user_to_edit' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @param $form_name The "slug" generated from the current Form Title |
| 57 |
* @param $form_type the form type of the form: register, edit_profile |
| 58 |
* @return null |
| 59 |
*/ |
| 60 |
static function wppb_get_form_id_from_form_name( $form_name, $form_type ){ |
| 61 |
global $wpdb; |
| 62 |
|
| 63 |
if( $form_type == 'edit_profile' ){ |
| 64 |
$post_type = 'wppb-epf-cpt'; |
| 65 |
}elseif( $form_type == 'register' ){ |
| 66 |
$post_type = 'wppb-rf-cpt'; |
| 67 |
} |
| 68 |
|
| 69 |
$all_forms = $wpdb->get_results( |
| 70 |
" |
| 71 |
SELECT ID, post_title |
| 72 |
FROM $wpdb->posts |
| 73 |
WHERE post_status = 'publish' |
| 74 |
AND post_type = '$post_type' |
| 75 |
" |
| 76 |
); |
| 77 |
|
| 78 |
if( !empty( $all_forms ) ) { |
| 79 |
foreach ($all_forms as $form) { |
| 80 |
if( empty( $form->post_title ) ) |
| 81 |
$form->post_title = '(no title)'; |
| 82 |
|
| 83 |
if ($form_name == Wordpress_Creation_Kit_PB::wck_generate_slug($form->post_title)) { |
| 84 |
return $form->ID; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
function wppb_retrieve_custom_settings(){ |
| 93 |
$this->args['login_after_register'] = apply_filters( 'wppb_automatically_login_after_register', 'No' ); //used only for the register-form settings |
| 94 |
$this->args['redirect_activated'] = apply_filters( 'wppb_redirect_default_setting', '-' ); |
| 95 |
$this->args['redirect_url'] = apply_filters( 'wppb_redirect_default_location', ( $this->args['redirect_url'] != '' ) ? $this->args['redirect_url'] : '' ); |
| 96 |
$this->args['logout_redirect_url'] = apply_filters( 'wppb_logout_redirect_default_location', ( $this->args['logout_redirect_url'] != '' ) ? $this->args['logout_redirect_url'] : '' ); |
| 97 |
$this->args['redirect_delay'] = apply_filters( 'wppb_redirect_default_duration', 3 ); |
| 98 |
|
| 99 |
if ( !is_null( $this->args['ID'] ) ){ |
| 100 |
$meta_name = ( ( $this->args['form_type'] == 'register' ) ? 'wppb_rf_page_settings' : 'wppb_epf_page_settings' ); |
| 101 |
|
| 102 |
$page_settings = get_post_meta( $this->args['ID'], $meta_name, true ); |
| 103 |
|
| 104 |
if( !empty( $page_settings[0]['set-role'] ) ){ |
| 105 |
if( $page_settings[0]['set-role'] == 'default role' ){ |
| 106 |
$selected_role = trim( get_option( 'default_role' ) ); |
| 107 |
} |
| 108 |
else |
| 109 |
$selected_role = $page_settings[0]['set-role']; |
| 110 |
} |
| 111 |
|
| 112 |
$this->args['role'] = ( isset( $selected_role ) ? $selected_role : $this->args['role'] ); |
| 113 |
$this->args['login_after_register'] = ( isset( $page_settings[0]['automatically-log-in'] ) ? $page_settings[0]['automatically-log-in'] : $this->args['login_after_register'] ); |
| 114 |
$this->args['redirect_activated'] = ( isset( $page_settings[0]['redirect'] ) ? $page_settings[0]['redirect'] : $this->args['redirect_activated'] ); |
| 115 |
$this->args['redirect_url'] = ( ! empty( $page_settings[0]['url'] ) && $this->args['redirect_activated'] == 'Yes' && $this->args['redirect_priority'] != 'top' ? $page_settings[0]['url'] : $this->args['redirect_url'] ); |
| 116 |
$this->args['redirect_delay'] = ( isset( $page_settings[0]['display-messages'] ) && $this->args['redirect_activated'] == 'Yes' ? $page_settings[0]['display-messages'] : $this->args['redirect_delay'] ); |
| 117 |
} |
| 118 |
|
| 119 |
if( !empty( $this->args['role'] ) ){ |
| 120 |
$role_in_arg = get_role( $this->args['role'] ); |
| 121 |
if( !empty( $role_in_arg->capabilities['manage_options'] ) || !empty( $role_in_arg->capabilities['remove_users'] ) ){ |
| 122 |
if( !current_user_can( 'manage_options' ) || !current_user_can( 'remove_users' ) ){ |
| 123 |
$this->args['role'] = get_option('default_role'); |
| 124 |
echo apply_filters( 'wppb_register_pre_form_user_role_message', '<p class="alert" id="wppb_general_top_error_message">'.__( 'The role of the created user set to the default role. Only an administrator can register a user with the role assigned to this form.', 'profile-builder').'</p>' ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
function wppb_form_logic() { |
| 131 |
if( $this->args['form_type'] == 'register' ){ |
| 132 |
$registration = apply_filters ( 'wppb_register_setting_override', get_option( 'users_can_register' ) ); |
| 133 |
|
| 134 |
if ( !is_user_logged_in() ){ |
| 135 |
if ( !$registration ) |
| 136 |
echo apply_filters( 'wppb_register_pre_form_message', '<p class="alert" id="wppb_register_pre_form_message">'.__( 'Only an administrator can add new users.', 'profile-builder').'</p>' ); |
| 137 |
|
| 138 |
elseif ( $registration ){ |
| 139 |
$this->wppb_form_content( apply_filters( 'wppb_register_pre_form_message', '' ) ); |
| 140 |
} |
| 141 |
|
| 142 |
}else{ |
| 143 |
$current_user_capability = apply_filters ( 'wppb_registration_user_capability', 'create_users' ); |
| 144 |
|
| 145 |
if ( current_user_can( $current_user_capability ) && $registration ) |
| 146 |
$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.', 'profile-builder'). '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.__( 'This message is only visible by administrators', 'profile-builder' ).'"/>' . '</p>' ) ); |
| 147 |
|
| 148 |
elseif ( current_user_can( $current_user_capability ) && !$registration ) |
| 149 |
$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.', 'profile-builder'). '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.__( 'This message is only visible by administrators', 'profile-builder' ).'"/>' . '</p>' ) ); |
| 150 |
|
| 151 |
elseif ( !current_user_can( $current_user_capability ) ){ |
| 152 |
global $user_ID; |
| 153 |
|
| 154 |
$userdata = get_userdata( $user_ID ); |
| 155 |
$display_name = ( ( $userdata->data->display_name == '' ) ? $userdata->data->user_login : $userdata->data->display_name ); |
| 156 |
|
| 157 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 158 |
if ( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ) |
| 159 |
$display_name = $userdata->data->user_email; |
| 160 |
|
| 161 |
if( empty( $this->args['logout_redirect_url'] ) ) { |
| 162 |
$this->args['logout_redirect_url'] = get_permalink(); |
| 163 |
} |
| 164 |
|
| 165 |
// CHECK FOR REDIRECT |
| 166 |
$this->args['logout_redirect_url'] = wppb_get_redirect_url( $this->args['redirect_priority'], 'after_logout', $this->args['logout_redirect_url'], $userdata ); |
| 167 |
$this->args['logout_redirect_url'] = apply_filters( 'wppb_after_logout_redirect_url', $this->args['logout_redirect_url'] ); |
| 168 |
|
| 169 |
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", 'profile-builder' ), '<a href="'.get_author_posts_url( $user_ID ).'" title="'.$display_name.'">'.$display_name.'</a>', '<a href="'.wp_logout_url( $this->args['logout_redirect_url'] ).'" title="'.__( 'Log out of this account.', 'profile-builder' ).'">'.__( 'Logout', 'profile-builder' ).' »</a>' ).'</p>', $user_ID ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
}elseif ( $this->args['form_type'] == 'edit_profile' ){ |
| 174 |
if ( !is_user_logged_in() ) |
| 175 |
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.', 'profile-builder' ) .'</p>' ); |
| 176 |
|
| 177 |
elseif ( is_user_logged_in() ) |
| 178 |
$this->wppb_form_content( apply_filters( 'wppb_edit_profile_logged_in_user_message', '' ) ); |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// Function used to automatically log in a user after register if that option is set on yes in register form settings |
| 184 |
function wppb_log_in_user( $redirect, $redirect_old ) { |
| 185 |
if( is_user_logged_in() ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 190 |
|
| 191 |
if ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ) { |
| 192 |
return $redirect_old; |
| 193 |
} |
| 194 |
|
| 195 |
/* get user id */ |
| 196 |
$user = get_user_by( 'email', trim( sanitize_email( $_POST['email'] ) ) ); |
| 197 |
$nonce = wp_create_nonce( 'autologin-'. $user->ID .'-'. (int)( time() / 60 ) ); |
| 198 |
|
| 199 |
if ( isset( $wppb_general_settings['adminApproval'] ) && ( $wppb_general_settings['adminApproval'] == 'yes' ) ) { |
| 200 |
if( !empty( $wppb_general_settings['adminApprovalOnUserRole'] ) ) { |
| 201 |
foreach ($user->roles as $role) { |
| 202 |
if ( in_array( $role, $wppb_general_settings['adminApprovalOnUserRole'] ) ) { |
| 203 |
return $redirect_old; |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
else { |
| 208 |
return $redirect_old; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/* define redirect location */ |
| 213 |
if( $this->args['redirect_activated'] == 'No' ) { |
| 214 |
if( isset( $_POST['_wp_http_referer'] ) ) { |
| 215 |
$redirect = esc_url_raw($_POST['_wp_http_referer']); |
| 216 |
} else { |
| 217 |
$redirect = home_url(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
$redirect = apply_filters( 'wppb_login_after_reg_redirect_url', $redirect, $this ); |
| 222 |
|
| 223 |
$redirect = add_query_arg( array( 'autologin' => 'true', 'uid' => $user->ID, '_wpnonce' => $nonce ), $redirect ); |
| 224 |
|
| 225 |
// CHECK FOR REDIRECT |
| 226 |
if( $this->args['redirect_activated'] == 'No' || ( empty( $this->args['redirect_delay'] ) || $this->args['redirect_delay'] == '0' ) ) { |
| 227 |
$redirect = wppb_build_redirect( $redirect, 0, 'register', $this->args ); |
| 228 |
} else { |
| 229 |
$redirect = wppb_build_redirect( $redirect, $this->args['redirect_delay'], 'register', $this->args ); |
| 230 |
} |
| 231 |
return $redirect; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Function to get redirect for Register and Edit Profile forms |
| 236 |
* |
| 237 |
* @param string $form_type - type of the form |
| 238 |
* @param string $redirect_type - type of the redirect |
| 239 |
* @param string $user - username or user email |
| 240 |
* @param string $user_role - user Role |
| 241 |
* |
| 242 |
* @return string $redirect |
| 243 |
*/ |
| 244 |
function wppb_get_redirect( $form_type, $redirect_type, $user, $user_role ) { |
| 245 |
$this->args['redirect_delay'] = apply_filters( 'wppb_'. $form_type .'_redirect_delay', $this->args['redirect_delay'], $user, $this->args ); |
| 246 |
if( $this->args['redirect_activated'] == '-' ) { |
| 247 |
$this->args['redirect_url'] = wppb_get_redirect_url( $this->args['redirect_priority'], $redirect_type, $this->args['redirect_url'], $user, $user_role ); |
| 248 |
$redirect = wppb_build_redirect( $this->args['redirect_url'], $this->args['redirect_delay'], $form_type, $this->args ); |
| 249 |
} elseif( $this->args['redirect_activated'] == 'Yes' ) { |
| 250 |
$redirect = wppb_build_redirect( $this->args['redirect_url'], $this->args['redirect_delay'], $form_type, $this->args ); |
| 251 |
} else { |
| 252 |
$redirect = ''; |
| 253 |
} |
| 254 |
|
| 255 |
return $redirect; |
| 256 |
} |
| 257 |
|
| 258 |
function wppb_form_content( $message ) { |
| 259 |
$field_check_errors = array(); |
| 260 |
|
| 261 |
if( isset( $_REQUEST['action'] ) && $_REQUEST['form_name'] == $this->args['form_name'] ) { |
| 262 |
$field_check_errors = $this->wppb_test_required_form_values( $_REQUEST ); |
| 263 |
if( empty( $field_check_errors ) ) { |
| 264 |
|
| 265 |
do_action( 'wppb_before_saving_form_values',$_REQUEST, $this->args ); |
| 266 |
|
| 267 |
// we only have a $user_id on default registration (no email confirmation, no multisite) |
| 268 |
$user_id = $this->wppb_save_form_values( $_REQUEST ); |
| 269 |
|
| 270 |
if( ( 'POST' == $_SERVER['REQUEST_METHOD'] ) && ( $_POST['action'] == $this->args['form_type'] ) ) { |
| 271 |
|
| 272 |
$form_message_tpl_start = apply_filters( 'wppb_form_message_tpl_start', '<p class="alert" id="wppb_form_success_message">' ); |
| 273 |
$form_message_tpl_end = apply_filters( 'wppb_form_message_tpl_end', '</p>' ); |
| 274 |
|
| 275 |
if( ! current_user_can( 'manage_options' ) && $this->args['form_type'] != 'edit_profile' && isset( $_POST['custom_field_user_role'] ) ) { |
| 276 |
$user_role = sanitize_text_field($_POST['custom_field_user_role']); |
| 277 |
} elseif( ! current_user_can( 'manage_options' ) && $this->args['form_type'] != 'edit_profile' && isset( $this->args['role'] ) ) { |
| 278 |
$user_role = $this->args['role']; |
| 279 |
} else { |
| 280 |
$user_role = NULL; |
| 281 |
} |
| 282 |
|
| 283 |
if( isset( $_POST['username'] ) && ( trim( $_POST['username'] ) != '' ) ) { |
| 284 |
$account_name = sanitize_user( $_POST['username'] ); |
| 285 |
} elseif( isset( $_POST['email'] ) && ( trim( $_POST['email'] ) != '' ) ) { |
| 286 |
$account_name = sanitize_email( $_POST['email'] ); |
| 287 |
}else{ |
| 288 |
/* we are in the edit form with no username or email field */ |
| 289 |
$current_user = wp_get_current_user(); |
| 290 |
if( !empty( $current_user ) ) |
| 291 |
$account_name = $current_user->user_login; |
| 292 |
} |
| 293 |
|
| 294 |
if( $this->args['form_type'] == 'register' ) { |
| 295 |
// ec = email confirmation setting |
| 296 |
// aa = admin approval setting |
| 297 |
$wppb_general_settings = get_option( 'wppb_general_settings', 'false' ); |
| 298 |
if ( $wppb_general_settings ) { |
| 299 |
if( !empty( $wppb_general_settings['emailConfirmation'] ) ) |
| 300 |
$wppb_email_confirmation = $wppb_general_settings['emailConfirmation']; |
| 301 |
else |
| 302 |
$wppb_email_confirmation = 'no'; |
| 303 |
|
| 304 |
if( !empty( $wppb_general_settings['adminApproval'] ) ) |
| 305 |
$wppb_admin_approval = $wppb_general_settings['adminApproval']; |
| 306 |
else |
| 307 |
$wppb_admin_approval = 'no'; |
| 308 |
$account_management_settings = 'ec-' . $wppb_email_confirmation . '_' . 'aa-' . $wppb_admin_approval; |
| 309 |
} else { |
| 310 |
$account_management_settings = 'ec-no_aa-no'; |
| 311 |
} |
| 312 |
|
| 313 |
switch( $account_management_settings ) { |
| 314 |
case 'ec-no_aa-no': |
| 315 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "The account %1s has been successfully created!", 'profile-builder' ), $account_name ), $account_name ); |
| 316 |
break; |
| 317 |
case 'ec-yes_aa-no': |
| 318 |
$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.", 'profile-builder' ), $account_name ), $account_name ); |
| 319 |
break; |
| 320 |
case 'ec-no_aa-yes': |
| 321 |
if( current_user_can( 'delete_users' ) ) { |
| 322 |
$wppb_register_success_message = apply_filters( 'wppb_register_success_message', sprintf( __( "The account %1s has been successfully created!", 'profile-builder' ), $account_name ), $account_name ); |
| 323 |
} else { |
| 324 |
$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.", 'profile-builder' ), $account_name ), $account_name ); |
| 325 |
} |
| 326 |
break; |
| 327 |
case 'ec-yes_aa-yes': |
| 328 |
$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.", 'profile-builder' ), $account_name ), $account_name ); |
| 329 |
break; |
| 330 |
} |
| 331 |
|
| 332 |
// CHECK FOR REDIRECT |
| 333 |
$redirect = $this->wppb_get_redirect( 'register', 'after_registration', $account_name, $user_role ); |
| 334 |
|
| 335 |
if( $this->args['login_after_register'] == 'Yes' ) { |
| 336 |
$redirect = $this->wppb_log_in_user( $this->args['redirect_url'], $redirect ); |
| 337 |
} |
| 338 |
|
| 339 |
echo $form_message_tpl_start . $wppb_register_success_message . $form_message_tpl_end . $redirect; |
| 340 |
//action hook after registration success |
| 341 |
do_action( 'wppb_register_success', $_REQUEST, $this->args['form_name'], $user_id ); |
| 342 |
return; |
| 343 |
} elseif( $this->args['form_type'] == 'edit_profile' ) { |
| 344 |
// CHECK FOR REDIRECT |
| 345 |
$redirect = $this->wppb_get_redirect( 'edit_profile', 'after_edit_profile', $account_name, $user_role ); |
| 346 |
|
| 347 |
echo $form_message_tpl_start . apply_filters( 'wppb_edit_profile_success_message', __( 'Your profile has been successfully updated!', 'profile-builder' ) ) . $form_message_tpl_end . $redirect; |
| 348 |
|
| 349 |
//action hook after edit profile success |
| 350 |
do_action( 'wppb_edit_profile_success', $_REQUEST, $this->args['form_name'], $user_id ); |
| 351 |
if( apply_filters( 'wppb_no_form_after_profile_update', false ) ) |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
}else |
| 358 |
echo $message.apply_filters( 'wppb_general_top_error_message', '<p id="wppb_general_top_error_message">'.__( 'There was an error in the submitted form', 'profile-builder' ).'</p>' ); |
| 359 |
|
| 360 |
}else |
| 361 |
echo $message; |
| 362 |
|
| 363 |
// use this action hook to add extra content before the register form |
| 364 |
do_action( 'wppb_before_'.$this->args['form_type'].'_fields', $this->args['form_name'], $this->args['ID'], $this->args['form_type'] ); |
| 365 |
|
| 366 |
$wppb_user_role_class = ''; |
| 367 |
if( is_user_logged_in() ) { |
| 368 |
$wppb_user = wp_get_current_user(); |
| 369 |
|
| 370 |
if( $wppb_user && isset( $wppb_user->roles ) ) { |
| 371 |
foreach( $wppb_user->roles as $wppb_user_role ) { |
| 372 |
$wppb_user_role_class .= ' wppb-user-role-'. $wppb_user_role; |
| 373 |
} |
| 374 |
} |
| 375 |
} else { |
| 376 |
$wppb_user_role_class = ' wppb-user-logged-out'; |
| 377 |
} |
| 378 |
$wppb_user_role_class = apply_filters( 'wppb_user_role_form_class', $wppb_user_role_class ); |
| 379 |
|
| 380 |
/* set up form id */ |
| 381 |
$wppb_form_id = ''; |
| 382 |
if( $this->args['form_type'] == 'register' ) |
| 383 |
$wppb_form_id = 'wppb-register-user'; |
| 384 |
elseif( $this->args['form_type'] == 'edit_profile' ) |
| 385 |
$wppb_form_id = 'wppb-edit-user'; |
| 386 |
if( isset($this->args['form_name']) && $this->args['form_name'] != "unspecified" ) |
| 387 |
$wppb_form_id .= '-' . $this->args['form_name']; |
| 388 |
|
| 389 |
/* set up form class */ |
| 390 |
$wppb_form_class = 'wppb-user-forms'; |
| 391 |
if( $this->args['form_type'] == 'register' ) |
| 392 |
$wppb_form_class .= ' wppb-register-user'; |
| 393 |
elseif( $this->args['form_type'] == 'edit_profile' ) |
| 394 |
$wppb_form_class .= ' wppb-edit-user'; |
| 395 |
$wppb_form_class .= $wppb_user_role_class; |
| 396 |
|
| 397 |
?> |
| 398 |
<form enctype="multipart/form-data" method="post" id="<?php echo apply_filters( 'wppb_form_id', $wppb_form_id, $this ); ?>" class="<?php echo apply_filters( 'wppb_form_class', $wppb_form_class, $this ); ?>" action="<?php echo apply_filters( 'wppb_form_action', '' ); ?>"> |
| 399 |
<?php |
| 400 |
do_action( 'wppb_form_args_before_output', $this->args ); |
| 401 |
|
| 402 |
echo apply_filters( 'wppb_before_form_fields', '<ul>', $this->args['form_type'], $this->args['ID'] ); |
| 403 |
echo $this->wppb_output_form_fields( $_REQUEST, $field_check_errors, $this->args['form_fields'] ); |
| 404 |
echo apply_filters( 'wppb_after_form_fields', '</ul>', $this->args['form_type'], $this->args['ID'] ); |
| 405 |
|
| 406 |
echo apply_filters( 'wppb_before_send_credentials_checkbox', '<ul>', $this->args['form_type'], $this->args['ID'] ); |
| 407 |
$this->wppb_add_send_credentials_checkbox( $_REQUEST, $this->args['form_type'] ); |
| 408 |
echo apply_filters( 'wppb_after_send_credentials_checkbox', '</ul>', $this->args['form_type'] ); |
| 409 |
|
| 410 |
$wppb_form_submit_extra_attr = apply_filters( 'wppb_form_submit_extra_attr', '', $this->args['form_type'], $this->args['ID'] ); |
| 411 |
?> |
| 412 |
<p class="form-submit" <?php echo $wppb_form_submit_extra_attr; ?> > |
| 413 |
<?php |
| 414 |
if( $this->args['form_type'] == 'register' ) |
| 415 |
$button_name = ( current_user_can( 'create_users' ) ? __( 'Add User', 'profile-builder' ) : __( 'Register', 'profile-builder' ) ); |
| 416 |
|
| 417 |
elseif( $this->args['form_type'] == 'edit_profile' ) |
| 418 |
$button_name = __( 'Update', 'profile-builder' ); |
| 419 |
?> |
| 420 |
<?php do_action( 'wppb_form_before_submit_button', $this->args ); ?> |
| 421 |
<input name="<?php echo $this->args['form_type']; ?>" type="submit" id="<?php echo $this->args['form_type']; ?>" class="<?php echo apply_filters( 'wppb_'. $this->args['form_type'] .'_submit_class', "submit button" );?>" value="<?php echo apply_filters( 'wppb_'. $this->args['form_type'] .'_button_name', $button_name ); ?>" <?php echo apply_filters( 'wppb_form_submit_button_extra_attributes', '', $this->args['form_type'] );?>/> |
| 422 |
<?php do_action( 'wppb_form_after_submit_button', $this->args ); ?> |
| 423 |
<input name="action" type="hidden" id="action" value="<?php echo $this->args['form_type']; ?>" /> |
| 424 |
<input name="form_name" type="hidden" id="form_name" value="<?php echo $this->args['form_name']; ?>" /> |
| 425 |
<?php |
| 426 |
$wppb_module_settings = get_option( 'wppb_module_settings' ); |
| 427 |
|
| 428 |
if( isset( $wppb_module_settings['wppb_customRedirect'] ) && $wppb_module_settings['wppb_customRedirect'] == 'show' ) { |
| 429 |
if( isset( $_POST['wppb_referer_url'] ) ) |
| 430 |
$referer = $_POST['wppb_referer_url']; |
| 431 |
elseif( isset( $_SERVER['HTTP_REFERER'] ) ) |
| 432 |
$referer = $_SERVER['HTTP_REFERER']; |
| 433 |
else |
| 434 |
$referer = ''; |
| 435 |
|
| 436 |
echo '<input type="hidden" name="wppb_referer_url" value="'.esc_url( $referer ).'"/>'; |
| 437 |
} |
| 438 |
?> |
| 439 |
</p><!-- .form-submit --> |
| 440 |
<?php wp_nonce_field( 'verify_form_submission', $this->args['form_type'].'_nonce_field' ); ?> |
| 441 |
</form> |
| 442 |
<?php |
| 443 |
// use this action hook to add extra content after the register form |
| 444 |
do_action( 'wppb_after_'. $this->args['form_type'] .'_fields', $this->args['form_name'], $this->args['ID'], $this->args['form_type'] ); |
| 445 |
|
| 446 |
} |
| 447 |
|
| 448 |
function wppb_output_form_fields( $global_request, $field_check_errors, $form_fields, $called_from = NULL ){ |
| 449 |
$output_fields = ''; |
| 450 |
|
| 451 |
if( !empty( $form_fields ) ){ |
| 452 |
$output_fields .= apply_filters( 'wppb_output_before_first_form_field', '', $this->args['ID'], $this->args['form_type'], $form_fields, $called_from ); |
| 453 |
foreach( $form_fields as $field ){ |
| 454 |
$error_var = ( ( array_key_exists( $field['id'], $field_check_errors ) ) ? ' wppb-field-error' : '' ); |
| 455 |
$specific_message = ( ( array_key_exists( $field['id'], $field_check_errors ) ) ? $field_check_errors[$field['id']] : '' ); |
| 456 |
|
| 457 |
$display_field = apply_filters( 'wppb_output_display_form_field', true, $field, $this->args['form_type'], $this->args['role'], $this->wppb_get_desired_user_id() ); |
| 458 |
|
| 459 |
if( $display_field == false ) |
| 460 |
continue; |
| 461 |
|
| 462 |
$css_class = apply_filters( 'wppb_field_css_class', 'wppb-form-field wppb-'. Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ) .$error_var, $field, $error_var ); |
| 463 |
$output_fields .= apply_filters( 'wppb_output_before_form_field', '<li class="'. $css_class .'" id="wppb-form-element-'. $field['id'] .'">', $field, $error_var, $this->args['role'] ); |
| 464 |
$output_fields .= apply_filters( 'wppb_output_form_field_'.Wordpress_Creation_Kit_PB::wck_generate_slug( $field['field'] ), '', $this->args['form_type'], $field, $this->wppb_get_desired_user_id(), $field_check_errors, $global_request, $this->args['role'], $this ); |
| 465 |
$output_fields .= apply_filters( 'wppb_output_specific_error_message', $specific_message ); |
| 466 |
$output_fields .= apply_filters( 'wppb_output_after_form_field', '</li>', $field, $this->args['ID'], $this->args['form_type'], $called_from ); |
| 467 |
} |
| 468 |
|
| 469 |
$output_fields .= apply_filters( 'wppb_output_after_last_form_field', '', $this->args['ID'], $this->args['form_type'], $called_from ); |
| 470 |
} |
| 471 |
|
| 472 |
return apply_filters( 'wppb_output_fields_filter', $output_fields ); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
function wppb_add_send_credentials_checkbox ( $request_data, $form ){ |
| 477 |
if ( $form == 'edit_profile' ) |
| 478 |
echo ''; |
| 479 |
|
| 480 |
else{ |
| 481 |
$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.', 'profile-builder').'</label></li>', $request_data, $form ); |
| 482 |
|
| 483 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 484 |
echo ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ? '' : $checkbox ); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
function wppb_test_required_form_values( $global_request ){ |
| 490 |
$output_field_errors = array(); |
| 491 |
$form_fields = apply_filters( 'wppb_form_fields', $this->args['form_fields'], array( 'global_request' => $global_request, 'context' => 'validate_frontend', 'global_request' => $global_request, 'form_type' => $this->args['form_type'], 'role' => $this->args['role'], 'user_id' => $this->wppb_get_desired_user_id() ) ); |
| 492 |
if( !empty( $form_fields ) ){ |
| 493 |
foreach( $form_fields as $field ){ |
| 494 |
$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'], $this->args['role'], $this->wppb_get_desired_user_id() ); |
| 495 |
|
| 496 |
if( !empty( $error_for_field ) ) |
| 497 |
$output_field_errors[$field['id']] = '<span class="wppb-form-error">' . $error_for_field . '</span>'; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
return apply_filters( 'wppb_output_field_errors_filter', $output_field_errors, $this->args['form_fields'], $global_request, $this->args['form_type'] ); |
| 502 |
} |
| 503 |
|
| 504 |
function wppb_save_form_values( $global_request ){ |
| 505 |
$user_id = $this->wppb_get_desired_user_id(); |
| 506 |
$userdata = apply_filters( 'wppb_build_userdata', array(), $global_request ); |
| 507 |
$new_user_signup = false; |
| 508 |
|
| 509 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 510 |
|
| 511 |
if( $this->args['form_type'] == 'register' ){ |
| 512 |
|
| 513 |
$result = $this->wppb_register_user( $global_request, $userdata ); |
| 514 |
$user_id = $result['user_id']; |
| 515 |
$userdata = $result['userdata']; |
| 516 |
$new_user_signup = $result['new_user_signup']; |
| 517 |
|
| 518 |
}elseif( $this->args['form_type'] == 'edit_profile' ){ |
| 519 |
if( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ){ |
| 520 |
$user_info = get_userdata( $user_id ); |
| 521 |
$userdata['user_login'] = $user_info->user_login; |
| 522 |
} |
| 523 |
|
| 524 |
$userdata['ID'] = $this->wppb_get_desired_user_id(); |
| 525 |
$userdata = wp_unslash( $userdata ); |
| 526 |
/* if the user changes his password then we can't send it to the wp_update_user() function or |
| 527 |
the user will be logged out and won't be logged in again because we call wp_update_user() after |
| 528 |
the headers were sent( in the content as a shortcode ) */ |
| 529 |
if( isset( $userdata['user_pass'] ) && !empty( $userdata['user_pass'] ) ){ |
| 530 |
unset($userdata['user_pass']); |
| 531 |
} |
| 532 |
|
| 533 |
if( current_user_can( 'manage_options' ) && isset( $userdata['role'] ) && is_array( $userdata['role'] ) ) { |
| 534 |
$user_data = get_userdata( $user_id ); |
| 535 |
$user_data->remove_all_caps(); |
| 536 |
|
| 537 |
foreach( $userdata['role'] as $role ) { |
| 538 |
$user_data->add_role( $role ); |
| 539 |
} |
| 540 |
|
| 541 |
unset( $userdata['role'] ); |
| 542 |
} |
| 543 |
|
| 544 |
wp_update_user( $userdata ); |
| 545 |
} |
| 546 |
|
| 547 |
if( !empty( $this->args['form_fields'] ) && !$new_user_signup ){ |
| 548 |
foreach( $this->args['form_fields'] as $field ){ |
| 549 |
do_action( 'wppb_save_form_field', $field, $user_id, $global_request, $this->args['form_type'] ); |
| 550 |
} |
| 551 |
|
| 552 |
if ( $this->args['form_type'] == 'register' ){ |
| 553 |
if ( !is_wp_error( $user_id ) ){ |
| 554 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 555 |
if( isset( $global_request['send_credentials_via_email'] ) && ( $global_request['send_credentials_via_email'] == 'sending' ) ) |
| 556 |
$send_credentials_via_email = 'sending'; |
| 557 |
else |
| 558 |
$send_credentials_via_email = ''; |
| 559 |
wppb_notify_user_registration_email( get_bloginfo( 'name' ), ( isset( $userdata['user_login'] ) ? trim( $userdata['user_login'] ) : trim( $userdata['user_email'] ) ), trim( $userdata['user_email'] ), $send_credentials_via_email, trim( $userdata['user_pass'] ), ( isset( $wppb_general_settings['adminApproval'] ) ? $wppb_general_settings['adminApproval'] : 'no' ) ); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
return $user_id; |
| 564 |
} |
| 565 |
|
| 566 |
function wppb_register_user( $global_request, $userdata ){ |
| 567 |
$wppb_module_settings = get_option( 'wppb_module_settings' ); |
| 568 |
$wppb_general_settings = get_option( 'wppb_general_settings' ); |
| 569 |
$user_id = null; |
| 570 |
$new_user_signup = false; |
| 571 |
|
| 572 |
if( isset( $wppb_general_settings['loginWith'] ) && ( $wppb_general_settings['loginWith'] == 'email' ) ){ |
| 573 |
$userdata['user_login'] = apply_filters( 'wppb_generated_random_username', Wordpress_Creation_Kit_PB::wck_generate_slug( trim( $userdata['user_email'] ) ), $userdata['user_email'] ); |
| 574 |
} |
| 575 |
|
| 576 |
/* filter so we can bypass Email Confirmation on register */ |
| 577 |
$wppb_general_settings['emailConfirmation'] = apply_filters( 'wppb_email_confirmation_on_register', $wppb_general_settings['emailConfirmation'], $global_request ); |
| 578 |
|
| 579 |
if ( isset( $wppb_general_settings['emailConfirmation'] ) && ( $wppb_general_settings['emailConfirmation'] == 'yes' ) ){ |
| 580 |
$new_user_signup = true; |
| 581 |
|
| 582 |
$userdata = $this->wppb_add_custom_field_values( $global_request, $userdata, $this->args['form_fields'] ); |
| 583 |
|
| 584 |
if( ! isset( $userdata['role'] ) ) { |
| 585 |
$userdata['role'] = $this->args['role']; |
| 586 |
} |
| 587 |
|
| 588 |
$userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] ); |
| 589 |
|
| 590 |
if( is_multisite() ){ |
| 591 |
/* since version 2.0.7 add this meta so we know on what blog the user registered */ |
| 592 |
$userdata['registered_for_blog_id'] = get_current_blog_id(); |
| 593 |
$userdata = wp_unslash( $userdata ); |
| 594 |
} |
| 595 |
|
| 596 |
wppb_signup_user( $userdata['user_login'], $userdata['user_email'], $userdata ); |
| 597 |
}else{ |
| 598 |
if( ! isset( $userdata['role'] ) ) { |
| 599 |
$userdata['role'] = $this->args['role']; |
| 600 |
} |
| 601 |
|
| 602 |
$userdata = wp_unslash( $userdata ); |
| 603 |
|
| 604 |
// change User Registered date and time according to timezone selected in WordPress settings |
| 605 |
$wppb_get_date = wppb_get_date_by_timezone(); |
| 606 |
|
| 607 |
if( isset( $wppb_get_date ) ) { |
| 608 |
$userdata['user_registered'] = $wppb_get_date; |
| 609 |
} |
| 610 |
|
| 611 |
// insert user to database |
| 612 |
$user_id = wp_insert_user( $userdata ); |
| 613 |
} |
| 614 |
|
| 615 |
return array( 'userdata' => $userdata, 'user_id' => $user_id, 'new_user_signup' => $new_user_signup ); |
| 616 |
} |
| 617 |
|
| 618 |
function wppb_add_custom_field_values( $global_request, $meta, $form_properties ){ |
| 619 |
$form_fields = apply_filters( 'wppb_form_fields', $this->args['form_fields'], array( 'meta' => $meta, 'global_request' => $global_request, 'context' => 'user_signup' ) ); |
| 620 |
if( !empty( $form_fields ) ){ |
| 621 |
foreach( $form_fields as $field ){ |
| 622 |
if( !empty( $field['meta-name'] ) ){ |
| 623 |
$posted_value = ( !empty( $global_request[$field['meta-name']] ) ? $global_request[$field['meta-name']] : '' ); |
| 624 |
$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 ); |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
return apply_filters( 'wppb_add_to_user_signup_form_meta', $meta, $global_request, $this->args['role'] ); |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* Function that returns the id for the current logged in user or for edit profile forms for administrator it can return the id of a selected user |
| 634 |
*/ |
| 635 |
function wppb_get_desired_user_id(){ |
| 636 |
if( $this->args['form_type'] == 'edit_profile' ){ |
| 637 |
//only admins |
| 638 |
if( ( !is_multisite() && current_user_can( 'edit_users' ) ) || ( is_multisite() && current_user_can( 'manage_network' ) ) ) { |
| 639 |
if( isset( $_GET['edit_user'] ) && ! empty( $_GET['edit_user'] ) ){ |
| 640 |
return absint( $_GET['edit_user'] ); |
| 641 |
} |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
return get_current_user_id(); |
| 646 |
} |
| 647 |
|
| 648 |
function wppb_edit_profile_select_user_to_edit(){ |
| 649 |
|
| 650 |
$display_edit_users_dropdown = apply_filters( 'wppb_display_edit_other_users_dropdown', true ); |
| 651 |
if( !$display_edit_users_dropdown ) |
| 652 |
return; |
| 653 |
|
| 654 |
/* add a hard cap: if we have more than 5000 users don't display the dropdown for performance considerations */ |
| 655 |
$user_count = count_users(); |
| 656 |
if( $user_count['total_users'] > apply_filters( 'wppb_edit_other_users_count_limit', 5000 ) ) |
| 657 |
return; |
| 658 |
|
| 659 |
if( isset( $_GET['edit_user'] ) && ! empty( $_GET['edit_user'] ) ) |
| 660 |
$selected = absint( $_GET['edit_user'] ); |
| 661 |
else |
| 662 |
$selected = get_current_user_id(); |
| 663 |
|
| 664 |
$query_args['fields'] = array( 'ID', 'user_login', 'display_name' ); |
| 665 |
$query_args['role'] = apply_filters( 'wppb_edit_profile_user_dropdown_role', '' ); |
| 666 |
$users = get_users( apply_filters( 'wppb_edit_other_users_dropdown_query_args', $query_args ) ); |
| 667 |
if( !empty( $users ) ) { |
| 668 |
|
| 669 |
/* turn it in a select2 */ |
| 670 |
wp_enqueue_script( 'wppb_select2_js', WPPB_PLUGIN_URL .'assets/js/select2/select2.min.js', array( 'jquery' ), PROFILE_BUILDER_VERSION ); |
| 671 |
wp_enqueue_style( 'wppb_select2_css', WPPB_PLUGIN_URL .'assets/css/select2/select2.min.css', array(), PROFILE_BUILDER_VERSION ); |
| 672 |
?> |
| 673 |
<form method="GET" action="" id="select_user_to_edit_form"> |
| 674 |
<p class="wppb-form-field"> |
| 675 |
<label for="edit_user"><?php _e('User to edit:', 'profile-builder') ?></label> |
| 676 |
<select id="wppb-user-to-edit" class="wppb-user-to-edit" name="edit_user"> |
| 677 |
<?php |
| 678 |
foreach( $users as $user ){ |
| 679 |
?> |
| 680 |
<option value="<?php echo $user->ID; ?>" <?php selected( $selected, $user->ID ); ?>><?php echo $user->display_name; ?></option> |
| 681 |
<?php |
| 682 |
} |
| 683 |
?> |
| 684 |
</select> |
| 685 |
</p> |
| 686 |
<script type="text/javascript">jQuery('#wppb-user-to-edit').change(function () { |
| 687 |
window.location.href = "<?php echo htmlspecialchars_decode( esc_js( esc_url_raw( add_query_arg( array( 'edit_user' => '=' ) ) ) ) ) ?>" + jQuery(this).val(); |
| 688 |
}); |
| 689 |
jQuery(function(){jQuery('.wppb-user-to-edit').select2(); })</script> |
| 690 |
</form> |
| 691 |
<?php |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
/** |
| 696 |
* Handle toString method |
| 697 |
* |
| 698 |
* @since 2.0 |
| 699 |
* |
| 700 |
* @return string $html html for the form. |
| 701 |
*/ |
| 702 |
public function __toString() { |
| 703 |
try { |
| 704 |
ob_start(); |
| 705 |
$this->wppb_form_logic(); |
| 706 |
$html = ob_get_clean(); |
| 707 |
return "{$html}"; |
| 708 |
} catch (Exception $exception) { |
| 709 |
return __( 'Something went wrong. Please try again!', 'profile-builder'); |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
/* set action for automatic login after registration */ |
| 715 |
add_action( 'init', 'wppb_autologin_after_registration' ); |
| 716 |
function wppb_autologin_after_registration(){ |
| 717 |
if( isset( $_GET['autologin'] ) && isset( $_GET['uid'] ) ){ |
| 718 |
$uid = absint( $_GET['uid'] ); |
| 719 |
$nonce = $_REQUEST['_wpnonce']; |
| 720 |
|
| 721 |
$arr_params = array( 'autologin', 'uid', '_wpnonce' ); |
| 722 |
$current_page_url = remove_query_arg( $arr_params, wppb_curpageurl() ); |
| 723 |
|
| 724 |
if ( ! ( wp_verify_nonce( $nonce , 'autologin-'.$uid.'-'.(int)( time() / 60 ) ) || wp_verify_nonce( $nonce , 'autologin-'.$uid.'-'.(int)( time() / 60 ) - 1 ) ) ){ |
| 725 |
wp_redirect( $current_page_url ); |
| 726 |
exit; |
| 727 |
} else { |
| 728 |
wp_set_auth_cookie( $uid ); |
| 729 |
wp_redirect( $current_page_url ); |
| 730 |
exit; |
| 731 |
} |
| 732 |
} |
| 733 |
} |
| 734 |
|