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

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

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