PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 4.0.1
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v4.0.1
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 4.0.1, at front-end/default-fields/checkbox/checkbox.php

141 lines 7.8 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 $question_id = 'wppb-question-' . $field['id'];
35
36 $output = '
37 <label id="'. esc_attr( $question_id ) .'">'.$item_title.$error_mark.'</label>';
38 $output .= '<ul class="wppb-checkboxes">';
39 $output .= '<li class="wppb-hidden"><input type="hidden" value="" name="' . $field['meta-name'] . '"></li>';
40 foreach( $checkbox_values as $key => $value ) {
41 if ( $value !== "" ) {
42 $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" aria-describedby="' . esc_attr( $question_id ) . '" ' . $extra_attr . ' ';
43
44 if (in_array(trim($value), $input_value))
45 $output .= ' checked';
46
47 $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>';
48 }
49 }
50 $output .= '</ul>';
51 if( !empty( $item_description ) )
52 $output .= '<span class="wppb-description-delimiter">'.$item_description.'</span>';
53
54 }else{
55 $item_title = ( ( $field['required'] == 'Yes' ) ? $item_title .' <span class="description">('. __( 'required', 'profile-builder' ) .')</span>' : $item_title );
56 $output = '
57 <table class="form-table">
58 <tr>
59 <th><label for="'.$field['meta-name'].'">'.$item_title.'</label></th>
60 <td>';
61 $output .= '<ul class="wppb-checkboxes">';
62 $output .= '<li class="wppb-hidden"><input type="hidden" value="" name="' . $field['meta-name'] . '"></li>';
63 foreach( $checkbox_values as $key => $value ) {
64 if ( $value !== "" ) {
65 $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"';
66
67 if (in_array(trim($value), $input_value))
68 $output .= ' checked';
69
70 $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>';
71 }
72 }
73 $output .= '</ul>
74 <span class="wppb-description-delimiter">'.$item_description.'</span>
75 </td>
76 </tr>
77 </table>';
78 }
79
80 return apply_filters( 'wppb_'.$form_location.'_checkbox_custom_field_'.$field['id'], $output, $form_location, $field, $user_id, $field_check_errors, $request_data, $input_value );
81 }
82 }
83 add_filter( 'wppb_output_form_field_checkbox', 'wppb_checkbox_handler', 10, 6 );
84 add_filter( 'wppb_admin_output_form_field_checkbox', 'wppb_checkbox_handler', 10, 6 );
85
86
87 /* handle field save */
88 function wppb_save_checkbox_value( $field, $user_id, $request_data, $form_location ){
89 if( $field['field'] == 'Checkbox' ){
90 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ) {
91 $checkbox_values = wppb_process_checkbox_value($field, $request_data);
92 update_user_meta($user_id, $field['meta-name'], $checkbox_values);
93 }
94 }
95 }
96 add_action( 'wppb_save_form_field', 'wppb_save_checkbox_value', 10, 4 );
97 add_action( 'wppb_backend_save_form_field', 'wppb_save_checkbox_value', 10, 4 );
98
99
100 function wppb_process_checkbox_value( $field, $request_data ){
101 $checkbox_values = '';
102
103 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) && is_array( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) ){
104
105 // sanitize values
106 foreach( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] as $key => $value ){
107 $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ][$key] = sanitize_text_field( $value );
108 }
109
110 $checkbox_values = implode( ',', $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] );
111 }
112
113 return trim( $checkbox_values, ',' );
114 }
115
116
117 function wppb_add_checkbox_for_user_signup( $field_value, $field, $request_data ){
118 return wppb_process_checkbox_value( $field, $request_data );
119 }
120 add_filter( 'wppb_add_to_user_signup_form_field_checkbox', 'wppb_add_checkbox_for_user_signup', 10, 3 );
121
122
123 /* handle field validation */
124 function wppb_check_checkbox_value( $message, $field, $request_data, $form_location ){
125
126 if( $field['field'] == 'Checkbox' ){
127 $checked_values = '';
128
129 if( isset( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) && is_array( $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] ) )
130 $checked_values = implode( ',', $request_data[ wppb_handle_meta_name( $field['meta-name'] ) ] );
131
132 if ( ( $field['required'] == 'Yes' ) && empty( $checked_values ) ){
133 return wppb_required_field_error($field["field-title"]);
134 }
135
136 }
137
138 return $message;
139 }
140 add_filter( 'wppb_check_form_field_checkbox', 'wppb_check_checkbox_value', 10, 4 );
141