PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / front-end / class-formbuilder.php

class-formbuilder.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at front-end/class-formbuilder.php

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