PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.2
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.2
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 4.0.2, at front-end/class-formbuilder.php

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