PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
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 / default-fields / checkbox / checkbox.php

checkbox.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.0, at front-end/default-fields/checkbox/checkbox.php

139 lines 7.7 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 /* handle field output */
6 function wppb_checkbox_handler( $output, $form_location, $field, $user_id, $field_check_errors, $request_data ){
7 if ( $field['field'] == 'Checkbox' ){
8 $item_title = apply_filters( 'wppb_'.$form_location.'_checkbox_custom_field_'.$field['id'].'_item_title', wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_title_translation', $field['field-title'], true ) );
9 $item_description = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_description_translation', $field['description'], true );
10 $item_option_labels = wppb_icl_t( 'plugin profile-builder-pro', 'custom_field_'.$field['id'].'_labels_translation', $field['labels'], true );
11
12 $checkbox_labels = apply_filters( 'wppb_checkbox_labels_array', explode( ',', $item_option_labels ), $field, $form_location, $user_id, $request_data );
13 $checkbox_values = apply_filters( 'wppb_checkbox_options_array', explode( ',', $field['options'] ), $field, $form_location, $user_id, $request_data );
14
15 if( $form_location != 'register' )
16 $input_value = ( ( wppb_user_meta_exists ( $user_id, $field['meta-name'] ) != null ) ? array_map( 'trim', explode( ',', stripslashes(get_user_meta( $user_id, $field['meta-name'], true )) ) ) : array_map( 'trim', explode( ',', $field['default-options'] ) ) );
17 else
18 $input_value = ( !empty( $field['default-options'] ) ? array_map( 'trim', explode( ',', $field['default-options'] ) ) : array() );
19
20 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) && !empty( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ){
21 $input_value = $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ];
22 }
23
24 $input_value = apply_filters( 'wppb_form_checkbox_field_value', $input_value, $field, $form_location );
25
26 $extra_attr = apply_filters( 'wppb_extra_attribute', '', $field, $form_location );
27
28 if ( $form_location != 'back_end' ){
29 $error_mark = ( ( $field['required'] == 'Yes' ) ? '<span class="wppb-required" title="'.wppb_required_field_error($field["field-title"]).'">*</span>' : '' );
30
31 if ( array_key_exists( $field['id'], $field_check_errors ) )
32 $error_mark = '<img src="'.WPPB_PLUGIN_URL.'assets/images/pencil_delete.png" title="'.wppb_required_field_error($field["field-title"]).'"/>';
33
34 $output = '
35 <label for="'.$field['meta-name'].'">'.$item_title.$error_mark.'</label>';
36 $output .= '<ul class="wppb-checkboxes">';
37 $output .= '<li class="wppb-hidden"><input type="hidden" value="" name="' . $field['meta-name'] . '"></li>';
38 foreach( $checkbox_values as $key => $value ) {
39 if ( $value !== "" ) {
40 $output .= '<li><input value="' . esc_attr(trim($value)) . '" class="custom_field_checkbox" name="' . $field['meta-name'] . '[]" id="' . Wordpress_Creation_Kit_PB::wck_generate_slug(trim($value)) . '_' . $field['id'] . '" type="checkbox" ' . $extra_attr . ' ';
41
42 if (in_array(trim($value), $input_value))
43 $output .= ' checked';
44
45 $output .= ' /><label for="' . Wordpress_Creation_Kit_PB::wck_generate_slug(trim($value)) . '_' . $field['id'] . '" class="wppb-rc-value">' . ((!isset($checkbox_labels[$key]) || !$checkbox_labels[$key]) ? trim($checkbox_values[$key]) : trim($checkbox_labels[$key])) . '</label></li>';
46 }
47 }
48 $output .= '</ul>';
49 if( !empty( $item_description ) )
50 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
51
52 }else{
53 $item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title );
54 $output = '
55 <table class="form-table">
56 <tr>
57 <th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th>
58 <td>';
59 $output .= '<ul class="wppb-checkboxes">';
60 $output .= '<li class="wppb-hidden"><input type="hidden" value="" name="' . $field['meta-name'] . '"></li>';
61 foreach( $checkbox_values as $key => $value ) {
62 if ( $value !== "" ) {
63 $output .= '<li><input value="' . esc_attr(trim($value)) . '" class="custom_field_checkbox ' . apply_filters('wppb_fields_extra_css_class', '', $field) . '" name="' . $field['meta-name'] . '[]" id="' . Wordpress_Creation_Kit_PB::wck_generate_slug(trim($value)) . '_' . $field['id'] . '" type="checkbox"';
64
65 if (in_array(trim($value), $input_value))
66 $output .= ' checked';
67
68 $output .= ' /><label for="' . Wordpress_Creation_Kit_PB::wck_generate_slug(trim($value)) . '_' . $field['id'] . '" class="wppb-rc-value">' . ((!isset($checkbox_labels[$key]) || !$checkbox_labels[$key]) ? trim($checkbox_values[$key]) : trim($checkbox_labels[$key])) . '</label></li>';
69 }
70 }
71 $output .= '</ul>
72 <span class="wppb-description-delimiter">'.$item_description.'</span>
73 </td>
74 </tr>
75 </table>';
76 }
77
78 return apply_filters( 'wppb_'.$form_location.'_checkbox_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value );
79 }
80 }
81 add_filter( 'wppb_output_form_field_checkbox', 'wppb_checkbox_handler', 10, 6 );
82 add_filter( 'wppb_admin_output_form_field_checkbox', 'wppb_checkbox_handler', 10, 6 );
83
84
85 /* handle field save */
86 function wppb_save_checkbox_value( $field, $user_id, $request_data, $form_location ){
87 if( $field['field'] == 'Checkbox' ){
88 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ) {
89 $checkbox_values = wppb_process_checkbox_value($field, $request_data);
90 update_user_meta($user_id, $field['meta-name'], $checkbox_values);
91 }
92 }
93 }
94 add_action( 'wppb_save_form_field', 'wppb_save_checkbox_value', 10, 4 );
95 add_action( 'wppb_backend_save_form_field', 'wppb_save_checkbox_value', 10, 4 );
96
97
98 function wppb_process_checkbox_value( $field, $request_data ){
99 $checkbox_values = '';
100
101 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) && is_array( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ){
102
103 // sanitize values
104 foreach( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] as $key => $value ){
105 $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ][$key] = sanitize_text_field( $value );
106 }
107
108 $checkbox_values = implode( ',', $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] );
109 }
110
111 return trim( $checkbox_values, ',' );
112 }
113
114
115 function wppb_add_checkbox_for_user_signup( $field_value, $field, $request_data ){
116 return wppb_process_checkbox_value( $field, $request_data );
117 }
118 add_filter( 'wppb_add_to_user_signup_form_field_checkbox', 'wppb_add_checkbox_for_user_signup', 10, 3 );
119
120
121 /* handle field validation */
122 function wppb_check_checkbox_value( $message, $field, $request_data, $form_location ){
123
124 if( $field['field'] == 'Checkbox' ){
125 $checked_values = '';
126
127 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) && is_array( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) )
128 $checked_values = implode( ',', $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] );
129
130 if ( ( $field['required'] == 'Yes' ) && empty( $checked_values ) ){
131 return wppb_required_field_error($field["field-title"]);
132 }
133
134 }
135
136 return $message;
137 }
138 add_filter( 'wppb_check_form_field_checkbox', 'wppb_check_checkbox_value', 10, 4 );
139