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 / form-builder / multiple-forms / multiple-forms.php

multiple-forms.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 4.0.2, at form-builder/multiple-forms/multiple-forms.php

487 lines 19.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) ) exit;
4
5 // NOTE: this file mirrors add-ons/multiple-forms/multiple-forms.php, differing
6 // only in using __DIR__ in place of the WPPB_PAID_PLUGIN_DIR path — kept close
7 // to that file so upstream changes port easily. Behavior the form-builder needs
8 // on top (always-on filter registration, editor-chrome cleanup) lives in
9 // form-builder-side files.
10
11 $wppb_module_settings = get_option( 'wppb_module_settings', 'not_found' );
12 if ( $wppb_module_settings != 'not_found' ){
13 if ( isset( $wppb_module_settings['wppb_multipleEditProfileForms'] ) && ( $wppb_module_settings['wppb_multipleEditProfileForms'] == 'show' ) && file_exists( __DIR__ . '/edit-profile-forms.php' ) )
14 include_once( __DIR__ . '/edit-profile-forms.php' );
15
16 if ( isset( $wppb_module_settings['wppb_multipleRegistrationForms'] ) && ( $wppb_module_settings['wppb_multipleRegistrationForms'] == 'show' ) && file_exists( __DIR__ . '/register-forms.php' ) )
17 include_once( __DIR__ . '/register-forms.php' );
18
19 if ( ( isset( $wppb_module_settings['wppb_multipleEditProfileForms'] ) && ( $wppb_module_settings['wppb_multipleEditProfileForms'] == 'show' ) ) || ( isset( $wppb_module_settings['wppb_multipleRegistrationForms'] ) && ( $wppb_module_settings['wppb_multipleRegistrationForms'] == 'show' ) ) ){
20 add_filter( 'wppb_change_form_fields', 'wppb_multiple_forms_change_fields', 10, 2 );
21 }
22 }
23
24 /**
25 * Function that is applied on "wppb_change_form_fields" filter to change the manage fields( all the fields defined ) according to the current form.
26 *
27 *
28 * @param array $fields All the fields from the manage fields section.
29 * @return array $args the arguments array for the form
30 */
31 function wppb_multiple_forms_change_fields( $fields, $args ){
32 //if we have a edit_profile form set up the post type and meta name accordingly
33 if( $args['form_type'] == 'edit_profile' ){
34 $meta_name = 'wppb_epf_fields';
35
36 }elseif( $args['form_type'] == 'register' ){
37 $meta_name = 'wppb_rf_fields';
38 }
39
40 // let's get the fields that we should display on that form
41 if( isset( $args['ID'], $meta_name ) ) {
42 $this_forms_fields = get_post_meta($args['ID'], $meta_name, true);
43 if (!empty($this_forms_fields)) {
44 $this_forms_fields_ids = array();
45 $returned_fields = array();
46
47 // keep the ids of those fields as they are the "unique key" and we will search for them in all the fields
48 foreach ($this_forms_fields as $this_forms_field) {
49 if ( is_array( $this_forms_field ) && isset( $this_forms_field['id'] ) )
50 $this_forms_fields_ids[] = $this_forms_field['id'];
51 }
52
53 // rearrange the fields based on the ids we got before. sort them and remove the ones we don't need.
54 if (!empty($this_forms_fields_ids) && !empty($fields)) {
55 foreach ($this_forms_fields_ids as $this_forms_fields_id) {
56 foreach ($fields as $field) {
57 if ($field['id'] == $this_forms_fields_id) {
58 $returned_fields[] = $field;
59 }
60 }
61 }
62 }
63 }
64 }
65
66 // if we have any rearranged fields return them else we return the original fields
67 if( !empty( $returned_fields ) )
68 return $returned_fields;
69
70 else
71 return $fields;
72 }
73
74
75 /**
76 * Prepopulate the 2 CPT's with the required fields at the moment somebody tries to create a new Register form or Edit Profile form
77 *
78 * @since v.2.0
79 *
80 * @param integer $post_id
81 * @param object $post
82 *
83 * @return void
84 */
85 function wppb_add_prepopulated_default_fields( $post_id, $post ){
86 // When the form-builder (Gutenberg) editor is active for this CPT,
87 // the mandatory-field seeding is owned by wppb_fb_seed_new_form_mandatory_fields()
88 // in form-builder-defaults.php. Running this legacy full-list prepopulation too
89 // would append a SECOND (non-unique add_post_meta) wppb_rf_fields/wppb_epf_fields
90 // row, masked by get_post_meta(..., true). Skip it in form-builder mode; classic
91 // mode keeps the legacy behavior.
92 if ( function_exists( 'wppb_fb_is_active_for' ) && wppb_fb_is_active_for( $post->post_type ) ) {
93 return;
94 }
95 if ( 'auto-draft' == $post->post_status ) {
96 $all_fields = get_option ( 'wppb_manage_fields' );
97 $all_fields_new = array();
98
99 foreach ( $all_fields as $key => $value )
100 array_push( $all_fields_new, array( 'field' => wppb_field_format( $value['field-title'], $value['field'] ), 'id' => $value['id'] ) );
101
102 if ( 'wppb-rf-cpt' == $post->post_type ) {
103 // Remove "Display name publicly as" from register forms
104 foreach( $all_fields_new as $key => $value ) {
105 if( strpos( $value['field'], 'Display name publicly as' ) !== false ) {
106 unset( $all_fields_new[$key] );
107 }
108 }
109 $all_fields_new = array_values( $all_fields_new );
110 add_post_meta( $post->ID, 'wppb_rf_fields', $all_fields_new );
111
112 } elseif ( 'wppb-epf-cpt' == $post->post_type ){
113 // remove reCAPTCHA and Validation fields from the list
114 foreach ( $all_fields_new as $key => $value ){
115 if ( strpos ( $value['field'], '( reCAPTCHA )' ) !== false || strpos ( $value['field'], '( Validation )' ) !== false ) {
116 unset( $all_fields_new[$key] );
117 }
118 }
119 $all_fields_new = array_values( $all_fields_new );
120 add_post_meta( $post->ID, 'wppb_epf_fields', $all_fields_new );
121 }
122 }
123 }
124 add_action( 'wp_insert_post', 'wppb_add_prepopulated_default_fields', 10, 2 );
125
126
127 /**
128 * Function that adds the internal ID of the given field on the RF and EPF CPT pages, via AJAX
129 *
130 * @since v.2.0
131 *
132 * @return void
133 */
134 function wppb_handle_rf_epf_id_change(){
135
136 // Only form editors should be able to resolve a field label to its global id.
137 check_ajax_referer( 'wppb-epf-rf-id-change' );
138 if( ! current_user_can( 'manage_options' ) )
139 die('');
140
141 if( !isset( $_POST['field'] ) || $_POST['field'] == '' )
142 die('');
143
144 $all_fields = get_option ( 'wppb_manage_fields', 'not_set' );
145
146 if ( ( $all_fields != 'not_set' ) && ( ( is_array( $all_fields ) ) && ( !empty( $all_fields ) ) ) ){
147
148 foreach ( $all_fields as $key => $value ){
149
150 if( wppb_field_format( $value['field-title'], $value['field'] ) == stripslashes( sanitize_text_field( $_POST['field'] ) ) ){
151 die( (string)$value['id'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
152 }
153
154 }
155 } else
156 die('');
157
158 }
159 add_action( 'wp_ajax_wppb_handle_rf_epf_id_change', 'wppb_handle_rf_epf_id_change' );
160
161
162 /**
163 * Function that iterates in both the EPF and RF CTP meta's on manage fields update
164 *
165 * @since v.2.0
166 *
167 * @param string $meta
168 * @param integer $id
169 * @param array $array_after_update
170 * @param integer $element_id
171 *
172 * @return void
173 */
174 function wppb_check_epf_rf_cptpms_update( $ep_r_posts, $cpt, $cpt_meta, $internal_id, $array_after_update ){
175 foreach ( $ep_r_posts as $key => $value ){
176 if ( $value->post_type == $cpt ){
177 $post_meta = get_post_meta( $value->ID, $cpt_meta, true );
178
179 if ( !empty( $post_meta ) ){
180 foreach ( $post_meta as $this_post_meta_key => $this_post_meta_value ){
181 if ( $this_post_meta_value['id'] == $internal_id ){
182 $post_meta[$this_post_meta_key]['field'] = wppb_field_format( $array_after_update['field-title'], $array_after_update['field'] );
183 }
184 }
185
186 update_post_meta( $value->ID, $cpt_meta, $post_meta );
187 }
188 }
189 }
190 }
191
192
193 /**
194 * Function that checks if, after an update, a change has been made to the lists, and if we need to make those changes to all the postmeta's also
195 *
196 * @since v.2.0
197 *
198 * @param string $meta
199 * @param integer $id
200 * @param array $array_after_update
201 * @param integer $element_id
202 *
203 * @return void
204 */
205 function wppb_fields_list_update( $meta, $id, $array_after_update, $element_id ){
206 if ( $meta == 'wppb_manage_fields' ){
207 $all_fields = get_option ( $meta );
208 $array_before_update = $all_fields[$element_id];
209
210 if ( ( trim( $array_before_update['field'] ) != trim( $array_after_update['field'] ) ) || ( trim( $array_before_update['field-title'] ) != trim( $array_after_update['field-title'] ) ) ){
211 $ep_r_posts = get_posts( array( 'posts_per_page' => -1, 'post_status' => apply_filters ( 'wppb_get_ep_r_posts', array( 'publish', 'pending', 'draft', 'future', 'private', 'trash' ) ), 'post_type' => array( 'wppb-epf-cpt', 'wppb-rf-cpt' ) ) );
212
213 wppb_check_epf_rf_cptpms_update( $ep_r_posts, 'wppb-rf-cpt', 'wppb_rf_fields', $array_before_update['id'], $array_after_update );
214 wppb_check_epf_rf_cptpms_update( $ep_r_posts, 'wppb-epf-cpt', 'wppb_epf_fields', $array_after_update['id'], $array_after_update );
215 }
216 }
217 }
218 add_action ( 'wck_before_update_meta', 'wppb_fields_list_update', 10, 4 );
219
220 /**
221 * Function that queries the 2 CPTs and checks whether or not the given cpt has <= 1 posts added
222 *
223 * @since v.2.0
224 *
225 * @return array
226 */
227 function wppb_get_post_number ( $cpt, $action ){
228 $this_cpt = get_posts( array( 'posts_per_page' => -1, 'post_status' => apply_filters ( 'wppb_check_'.$cpt.$action, array( 'any' ) ) , 'post_type' => $cpt ) );
229
230 if ( count( $this_cpt ) <= 1 )
231 return true;
232 else
233 return false;
234 }
235
236 /**
237 * Function that prevents users to submit forms without a form title.
238 *
239 */
240 function wppb_multiple_forms_publish_admin_hook(){
241 global $post;
242
243 if ( is_admin() && ( ( $post->post_type == 'wppb-epf-cpt' ) || ( $post->post_type == 'wppb-rf-cpt' ) ) ){
244 ?>
245 <script language="javascript" type="text/javascript">
246 jQuery(document).ready(function() {
247 jQuery(document).on( 'click', '#publish', function(){
248 var post_title = jQuery( '#title' ).val();
249
250 if ( post_title.trim() == '' ){
251 alert ( '<?php esc_html_e( 'You need to specify the title of the form before creating it', 'profile-builder' ); ?>' );
252
253 jQuery( '#ajax-loading' ).hide();
254 jQuery( '.spinner' ).hide();
255 jQuery( '#publish' ).removeClass( 'button-primary-disabled' );
256 jQuery( '#save-post' ).removeClass('button-disabled' );
257
258 return false;
259 }
260
261 return true;
262 });
263 });
264 </script>
265 <?php
266 }
267 }
268 add_action( 'admin_head-post.php', 'wppb_multiple_forms_publish_admin_hook' );
269 add_action( 'admin_head-post-new.php', 'wppb_multiple_forms_publish_admin_hook' );
270
271
272 /* when deleting fields from manage-fields we need to delete them from the register/edit profile forms as well */
273 add_action( 'wck_before_remove_meta', 'wppb_delete_fields_from_forms_as_well', 10, 3 );
274 function wppb_delete_fields_from_forms_as_well( $meta, $id, $element_id ){
275 if( $meta == 'wppb_manage_fields' ){
276 $all_fields = get_option( $meta);
277 $deleted_element = $all_fields[$element_id];
278 $deleted_element_id = $deleted_element['id'];
279
280 /* delete from forms */
281 $form_types = array( 'register_forms' => 'rf', 'edit_forms' => 'epf' );
282 foreach( $form_types as $form_type ){
283 $args = array(
284 'post_type' => 'wppb-'.$form_type.'-cpt',
285 'numberposts' => -1,
286 'posts_per_page' => -1,
287 'post_status' => 'any'
288 );
289 $all_forms = get_posts( $args );
290 if( !empty( $all_forms ) ){
291 foreach( $all_forms as $form ){
292 $fields_in_form = get_post_meta( $form->ID, 'wppb_'.$form_type.'_fields', true );
293 $delete_this_key = '';
294 if( !empty( $fields_in_form ) ){
295 foreach( $fields_in_form as $key => $form_field ){
296 if( $form_field['id'] == $deleted_element_id ){
297 $delete_this_key = $key;
298 break;
299 }
300 }
301
302 if( !empty( $delete_this_key ) ){
303 unset( $fields_in_form[$delete_this_key] );
304 $fields_in_form = array_values( $fields_in_form );
305 update_post_meta( $form->ID, 'wppb_'.$form_type.'_fields', $fields_in_form );
306 }
307 }
308 }
309 }
310 }
311 }
312 }
313
314
315 /**
316 * Function that adds the data-id attribute to each option of the add new field to the list select drop-down
317 *
318 * @since v.2.0.2
319 *
320 * @return void
321 */
322 function wppb_rf_epf_set_field_ids_on_field_select() {
323 global $all_fields;
324 $all_fields = get_option ( 'wppb_manage_fields', 'not_set' );
325
326 // remove certain fields from the Field drop-down on edit profile form
327 if( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'wppb-epf-cpt' ) || ( isset( $_GET['post'] ) && get_post_type( absint( $_GET['post'] ) ) == 'wppb-epf-cpt' ) || ( isset( $_POST['meta'] ) && $_POST['meta'] == 'wppb_epf_fields' ) ) {
328 foreach( $all_fields as $key => $field ) {
329 $unwanted_fields = array( 'reCAPTCHA', 'Validation' );
330 if( in_array( $field['field'], $unwanted_fields) ) {
331 unset( $all_fields[$key] );
332 }
333 }
334 $all_fields = array_values( $all_fields );
335 }
336
337 // remove certain fields from the Field drop-down on register form
338 if( ( isset( $_GET['post_type'] ) && $_GET['post_type'] == 'wppb-rf-cpt' ) || ( isset( $_GET['post'] ) && get_post_type( absint( $_GET['post'] ) ) == 'wppb-rf-cpt' ) || ( isset( $_POST['meta'] ) && $_POST['meta'] == 'wppb_rf_fields' ) ) {
339 foreach( $all_fields as $key => $field ) {
340 if( $field['field'] == 'Default - Display name publicly as' ) {
341 unset( $all_fields[$key] );
342 }
343 }
344 $all_fields = array_values( $all_fields );
345 }
346
347 if( $all_fields !== 'not_set ') {
348
349 if( !function_exists( 'wppb_rf_epf_set_field_id_select_option' ) ) {
350 function wppb_rf_epf_set_field_id_select_option( $content, $field_id ){
351 global $all_fields;
352 $output = str_replace( '<option', '<option ' . 'data-id="' . $all_fields[$field_id]['id'] . '"', $content );
353 return $output;
354 }
355 }
356 foreach($all_fields as $key => $value ) {
357 add_filter('wck_select_wppb_epf_fields_field_option_' . $key, 'wppb_rf_epf_set_field_id_select_option', 10 ,2);
358 add_filter('wck_select_wppb_rf_fields_field_option_' . $key, 'wppb_rf_epf_set_field_id_select_option', 10 ,2);
359 }
360
361 }
362 }
363 add_action( 'wck_before_add_form_wppb_rf_fields_element_0', 'wppb_rf_epf_set_field_ids_on_field_select');
364 add_action( 'wck_before_add_form_wppb_epf_fields_element_0', 'wppb_rf_epf_set_field_ids_on_field_select');
365 add_action( 'wck_before_adding_form_wppb_rf_fields', 'wppb_rf_epf_set_field_ids_on_field_select' );
366 add_action( 'wck_before_adding_form_wppb_epf_fields', 'wppb_rf_epf_set_field_ids_on_field_select' );
367
368
369 /**
370 * Function that disables the select field options that are also present in the table bellow the drop-down,
371 * - after the edit field is added
372 * - after the list refreshes;
373 * - after a field is refreshed
374 * - after the forms is added;
375 *
376 * @since v.2.0.2
377 *
378 * @return void
379 */
380 function wppb_rf_epf_disable_select_field_options() {
381 echo "<script type=\"text/javascript\">wppb_disable_select_field_options();</script>";
382 }
383 add_action( "wck_ajax_add_form_wppb_rf_fields", "wppb_rf_epf_disable_select_field_options" );
384 add_action( "wck_ajax_add_form_wppb_epf_fields", "wppb_rf_epf_disable_select_field_options" );
385 add_action( "wck_refresh_list_wppb_rf_fields", "wppb_rf_epf_disable_select_field_options" );
386 add_action( "wck_refresh_list_wppb_epf_fields", "wppb_rf_epf_disable_select_field_options" );
387 add_action( "wck_refresh_entry_wppb_rf_fields", "wppb_rf_epf_disable_select_field_options" );
388 add_action( "wck_refresh_entry_wppb_epf_fields", "wppb_rf_epf_disable_select_field_options" );
389 add_action( "wck_after_adding_form_wppb_rf_fields", "wppb_rf_epf_disable_select_field_options" );
390 add_action( "wck_after_adding_form_wppb_epf_fields", "wppb_rf_epf_disable_select_field_options" );
391
392
393 /**
394 * Function that calls a JS function that disables the Save Changes button when the Edit Form is added, if the field
395 * drop-down selected option is also disabled
396 *
397 * @since v.2.0.2
398 *
399 * @return void
400 */
401 function wppb_rf_epf_edit_field_disable_saving() {
402 echo "<script type=\"text/javascript\">wppb_check_update_field_options_disabled();</script>";
403 }
404 add_action( "wck_after_adding_form_wppb_rf_fields", "wppb_rf_epf_edit_field_disable_saving" );
405 add_action( "wck_after_adding_form_wppb_epf_fields", "wppb_rf_epf_edit_field_disable_saving" );
406
407
408 /**
409 * Function that calls a JS function that disables the Save Changes button in an edit panel,
410 * if another field with same ID has been saved in another edit panel
411 *
412 * @since v.2.0.2
413 *
414 * @return void
415 */
416 function wppb_rf_epf_edit_field_check_disabled_options() {
417 echo "<script type=\"text/javascript\">wppb_check_options_disabled_edit_field();</script>";
418 }
419 add_action( "wck_refresh_entry_wppb_rf_fields", "wppb_rf_epf_edit_field_check_disabled_options" );
420 add_action( "wck_refresh_entry_wppb_epf_fields", "wppb_rf_epf_edit_field_check_disabled_options" );
421
422
423 /**
424 * Function that modifies the table header in Edit Profile Forms and Register Forms
425 *
426 * @since v.2.0
427 *
428 * @param $list, $id
429 *
430 * @return string
431 */
432 function wppb_multiple_forms_header( $list_header ){
433 $delete_all_nonce = wp_create_nonce( 'wppb-delete-all-entries' );
434
435 return '<thead><tr><th class="wck-number">#</th><th class="wck-content">'. __( '<pre>Title (Type)</pre>', 'profile-builder' ) .'</th><th class="wck-edit">'. esc_html__( 'Edit', 'profile-builder' ) .'</th><th class="wck-delete"><a id="wppb-delete-all-fields" class="wppb-delete-all-fields" onclick="wppb_rf_epf_delete_all_fields(event, this.id, \'' . esc_js($delete_all_nonce) . '\')" title="' . esc_html__('Delete all items', 'profile-builder') . '" href="#">'. esc_html__( 'Delete all', 'profile-builder' ) .'</a></th></tr></thead>'; // phpcs:ignore WordPress.WP.I18n.NoHtmlWrappedStrings
436 }
437 add_action( 'wck_metabox_content_header_wppb_epf_fields', 'wppb_multiple_forms_header' );
438 add_action( 'wck_metabox_content_header_wppb_rf_fields', 'wppb_multiple_forms_header' );
439
440
441 /**
442 * Function that removes all fields from the form through Ajax
443 *
444 * @since v.2.0.5
445 *
446 * @return void
447 */
448 add_action("wp_ajax_wppb_rf_epf_delete_all_fields", 'wppb_rf_epf_delete_all_fields_callback' );
449 function wppb_rf_epf_delete_all_fields_callback(){
450 check_ajax_referer( "wppb-delete-all-entries" );
451
452 // Only form editors may bulk-delete a form's fields. The nonce alone is
453 // surfaced only on the manage-fields admin screen (manage_options), but a
454 // valid-nonce request from any authenticated user should still be refused.
455 if( ! current_user_can( 'manage_options' ) )
456 die('');
457
458 if( !empty($_POST['id']) )
459 $post_id = absint( $_POST['id'] );
460 else
461 $post_id = '';
462
463 if( !empty( $_POST['meta'] ) )
464 $meta_name = sanitize_text_field( $_POST['meta'] );
465 else
466 $meta_name = '';
467
468 do_action( 'wppb_before_remove_all_fields', $meta_name, $post_id );
469
470 if( $meta_name == 'wppb_rf_fields' ) {
471 $post_meta_array = get_post_meta( $post_id, $meta_name, true );
472
473 foreach( $post_meta_array as $key => $item ) {
474 if( !strpos( $item['field'], "( Default - Username )" ) && !strpos( $item['field'], "( Default - E-mail )" ) && !strpos( $item['field'], "( Default - Password )" ) ) {
475 unset( $post_meta_array[$key] );
476 }
477 }
478
479 $post_meta = array_values($post_meta_array);
480 } else {
481 $post_meta = '';
482 }
483
484 update_post_meta( $post_id, $meta_name, $post_meta);
485
486 exit;
487 }