|
1 | | -<?php |
2 | | - |
3 | | -/** |
4 | | - * Text Field Class |
5 | | - */ |
6 | | -class WeForms_Form_Field_Checkbox extends WeForms_Field_Contract { |
7 | | - |
8 | | - public function __construct() { |
9 | | - $this->name = __( 'Checkbox', 'weforms' ); |
10 | | - $this->input_type = 'checkbox_field'; |
11 | | - $this->icon = 'check-square-o'; |
12 | | - } |
13 | | - |
14 | | - /** |
15 | | - * Render the text field |
16 | | - * |
17 | | - * @param array $field_settings |
18 | | - * @param int $form_id |
19 | | - * |
20 | | - * @return void |
21 | | - */ |
22 | | - public function render( $field_settings, $form_id ) { |
23 | | - $use_theme_css = isset( $form_settings['use_theme_css'] ) ? $form_settings['use_theme_css'] : 'wpuf-style'; |
24 | | - $selected = !empty( $field_settings['selected'] ) ? $field_settings['selected'] : []; ?> |
25 | | - <li <?php $this->print_list_attributes( $field_settings ); ?>> |
26 | | - <?php $this->print_label( $field_settings, $form_id ); ?> |
27 | | - <?php do_action( 'weforms_checkbox_field_after_label', $field_settings ); ?> |
28 | | - <div class="wpuf-fields" data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" data-type="radio" data-style="<?php echo esc_attr( $use_theme_css ); ?>"> |
29 | | - <?php |
30 | | - if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
31 | | - foreach ( $field_settings['options'] as $value => $option ) { |
32 | | - ?> |
33 | | - <label <?php echo $field_settings['inline'] == 'yes' ? 'class="wpuf-checkbox-inline"' : 'class="wpuf-checkbox-block"'; ?>> |
34 | | - <input type="checkbox" class="<?php echo 'wpuf_' . esc_attr( $field_settings['name'] ). '_'. esc_attr($form_id); ?>" name="<?php echo esc_attr( $field_settings['name'] ); ?>[]" value="<?php echo esc_attr( $value ); ?>"<?php echo in_array( $value, $selected ) ? ' checked="checked"' : ''; ?> /> |
35 | | - <?php echo $option; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
36 | | - </label> |
37 | | - <?php |
38 | | - } |
39 | | - } ?> |
40 | | - |
41 | | - <?php $this->help_text( $field_settings ); ?> |
42 | | - |
43 | | - </div> |
44 | | - </li> |
45 | | - <?php |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * Get field options setting |
50 | | - * |
51 | | - * @return array |
52 | | - */ |
53 | | - public function get_options_settings() { |
54 | | - $default_options = $this->get_default_option_settings( true, [ 'width' ] ); |
55 | | - $dropdown_options = [ |
56 | | - $this->get_default_option_dropdown_settings( true ), |
57 | | - |
58 | | - [ |
59 | | - 'name' => 'inline', |
60 | | - 'title' => __( 'Show in inline list', 'weforms' ), |
61 | | - 'type' => 'radio', |
62 | | - 'options' => [ |
63 | | - 'yes' => __( 'Yes', 'weforms' ), |
64 | | - 'no' => __( 'No', 'weforms' ), |
65 | | - ], |
66 | | - 'default' => 'no', |
67 | | - 'inline' => true, |
68 | | - 'section' => 'advanced', |
69 | | - 'priority' => 23, |
70 | | - 'help_text' => __( 'Show this option in an inline list', 'weforms' ), |
71 | | - ], |
72 | | - ]; |
73 | | - |
74 | | - $dropdown_options = apply_filters( 'weforms_checkbox_field_option_settings', $dropdown_options ); |
75 | | - |
76 | | - return array_merge( $default_options, $dropdown_options ); |
77 | | - } |
78 | | - |
79 | | - /** |
80 | | - * Get the field props |
81 | | - * |
82 | | - * @return array |
83 | | - */ |
84 | | - public function get_field_props() { |
85 | | - $defaults = $this->default_attributes(); |
86 | | - $props = [ |
87 | | - 'selected' => [], |
88 | | - 'inline' => 'no', |
89 | | - 'options' => [ 'Option' => __( 'Option', 'weforms' ) ], |
90 | | - ]; |
91 | | - |
92 | | - $props = apply_filters( 'weforms_checkbox_field_props', $props ); |
93 | | - |
94 | | - return array_merge( $defaults, $props ); |
95 | | - } |
96 | | - |
97 | | - /** |
98 | | - * Prepare entry |
99 | | - * |
100 | | - * @param $field |
101 | | - * |
102 | | - * @return mixed |
103 | | - */ |
104 | | - public function prepare_entry( $field, $args = [] ) { |
105 | | - if( empty( $_POST['_wpnonce'] ) ) { |
106 | | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
107 | | - } |
108 | | - |
109 | | - if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
110 | | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
111 | | - } |
112 | | - |
113 | | - $args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
114 | | - $entry_value = ( is_array( $args[ $field[ 'name' ] ] ) && $args[ $field[ 'name' ] ] ) ? $args[ $field[ 'name' ] ] : array(); |
115 | | - |
116 | | - if ( $entry_value ) { |
117 | | - $new_val = []; |
118 | | - |
119 | | - foreach ( $entry_value as $option_key ) { |
120 | | - $new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] : $option_key; |
121 | | - } |
122 | | - |
123 | | - $entry_value = implode( WeForms::$field_separator, $new_val ); |
124 | | - } else { |
125 | | - $entry_value = ''; |
126 | | - } |
127 | | - |
128 | | - return $entry_value; |
129 | | - } |
130 | | -} |
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Text Field Class |
| 5 | + */ |
| 6 | +class WeForms_Form_Field_Checkbox extends WeForms_Field_Contract { |
| 7 | + |
| 8 | + public function __construct() { |
| 9 | + $this->name = __( 'Checkbox', 'weforms' ); |
| 10 | + $this->input_type = 'checkbox_field'; |
| 11 | + $this->icon = 'check-square-o'; |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Render the text field |
| 16 | + * |
| 17 | + * @param array $field_settings |
| 18 | + * @param int $form_id |
| 19 | + * |
| 20 | + * @return void |
| 21 | + */ |
| 22 | + public function render( $field_settings, $form_id ) { |
| 23 | + $selected = !empty( $field_settings['selected'] ) ? $field_settings['selected'] : []; ?> |
| 24 | + <li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 25 | + <?php $this->print_label( $field_settings, $form_id ); ?> |
| 26 | + |
| 27 | + <?php do_action( 'weforms_checkbox_field_after_label', $field_settings ); ?> |
| 28 | + <div class="wpuf-fields" data-required="<?php echo esc_attr( $field_settings['required'] ) ?>" data-type="radio"> |
| 29 | + |
| 30 | + <?php |
| 31 | + if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
| 32 | + foreach ( $field_settings['options'] as $value => $option ) { |
| 33 | + ?> |
| 34 | + <label <?php echo $field_settings['inline'] == 'yes' ? 'class="wpuf-checkbox-inline"' : 'class="wpuf-checkbox-block"'; ?>> |
| 35 | + <input type="checkbox" class="<?php echo 'wpuf_' . esc_attr( $field_settings['name'] ). '_'. esc_attr($form_id); ?>" name="<?php echo esc_attr( $field_settings['name'] ); ?>[]" value="<?php echo esc_attr( $value ); ?>"<?php echo in_array( $value, $selected ) ? ' checked="checked"' : ''; ?> /> |
| 36 | + <?php echo $option; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 37 | + </label> |
| 38 | + <?php |
| 39 | + } |
| 40 | + } ?> |
| 41 | + |
| 42 | + <?php $this->help_text( $field_settings ); ?> |
| 43 | + |
| 44 | + </div> |
| 45 | + </li> |
| 46 | + <?php |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get field options setting |
| 51 | + * |
| 52 | + * @return array |
| 53 | + */ |
| 54 | + public function get_options_settings() { |
| 55 | + $default_options = $this->get_default_option_settings( true, [ 'width' ] ); |
| 56 | + $dropdown_options = [ |
| 57 | + $this->get_default_option_dropdown_settings( true ), |
| 58 | + |
| 59 | + [ |
| 60 | + 'name' => 'inline', |
| 61 | + 'title' => __( 'Show in inline list', 'weforms' ), |
| 62 | + 'type' => 'radio', |
| 63 | + 'options' => [ |
| 64 | + 'yes' => __( 'Yes', 'weforms' ), |
| 65 | + 'no' => __( 'No', 'weforms' ), |
| 66 | + ], |
| 67 | + 'default' => 'no', |
| 68 | + 'inline' => true, |
| 69 | + 'section' => 'advanced', |
| 70 | + 'priority' => 23, |
| 71 | + 'help_text' => __( 'Show this option in an inline list', 'weforms' ), |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + $dropdown_options = apply_filters( 'weforms_checkbox_field_option_settings', $dropdown_options ); |
| 76 | + |
| 77 | + return array_merge( $default_options, $dropdown_options ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the field props |
| 82 | + * |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function get_field_props() { |
| 86 | + $defaults = $this->default_attributes(); |
| 87 | + $props = [ |
| 88 | + 'selected' => [], |
| 89 | + 'inline' => 'no', |
| 90 | + 'options' => [ 'Option' => __( 'Option', 'weforms' ) ], |
| 91 | + ]; |
| 92 | + |
| 93 | + $props = apply_filters( 'weforms_checkbox_field_props', $props ); |
| 94 | + |
| 95 | + return array_merge( $defaults, $props ); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Prepare entry |
| 100 | + * |
| 101 | + * @param $field |
| 102 | + * |
| 103 | + * @return mixed |
| 104 | + */ |
| 105 | + public function prepare_entry( $field, $args = [] ) { |
| 106 | + if( empty( $_POST['_wpnonce'] ) ) { |
| 107 | + wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 108 | + } |
| 109 | + |
| 110 | + if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { |
| 111 | + wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); |
| 112 | + } |
| 113 | + |
| 114 | + $args = ! empty( $args ) ? $args : weforms_clean( $_POST ); |
| 115 | + $entry_value = ( is_array( $args[ $field[ 'name' ] ] ) && $args[ $field[ 'name' ] ] ) ? $args[ $field[ 'name' ] ] : array(); |
| 116 | + |
| 117 | + if ( $entry_value ) { |
| 118 | + $new_val = []; |
| 119 | + |
| 120 | + foreach ( $entry_value as $option_key ) { |
| 121 | + $new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] . ' , ' . $option_key : $option_key; |
| 122 | + } |
| 123 | + |
| 124 | + $entry_value = implode( WeForms::$field_separator, $new_val ); |
| 125 | + } else { |
| 126 | + $entry_value = ''; |
| 127 | + } |
| 128 | + |
| 129 | + return $entry_value; |
| 130 | + } |
| 131 | +} |
0 commit comments