PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.15.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.15.9
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 2.0.7 All 340 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.15.9, at front-end/class-formbuilder.php

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