PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / fields / FrmFieldName.php

FrmFieldName.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.16, at classes/models/fields/FrmFieldName.php

283 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Name field
4 *
5 * @package Formidable
6 * @since 4.11
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die( 'You are not allowed to call this page directly.' );
11 }
12
13 class FrmFieldName extends FrmFieldCombo {
14
15 /**
16 * Field name.
17 *
18 * @var string
19 * @since 3.0
20 */
21 protected $type = 'name';
22
23 /**
24 * Could this field hold email values?
25 *
26 * @var bool
27 * @since 3.0
28 */
29 protected $holds_email_values = true;
30
31 public function __construct( $field = '', $type = '' ) {
32 parent::__construct( $field, $type );
33
34 $this->register_sub_fields(
35 array(
36 'first' => __( 'First Name', 'formidable' ),
37 'middle' => __( 'Middle Name', 'formidable' ),
38 'last' => __( 'Last Name', 'formidable' ),
39 )
40 );
41 }
42
43 /**
44 * Gets processed sub fields.
45 * This should return the list of sub fields after sorting or show/hide based of some options.
46 *
47 * @return array
48 */
49 protected function get_processed_sub_fields() {
50 $name_layout = $this->get_name_layout();
51 $names = explode( '_', $name_layout );
52 $col_class = 'frm' . intval( 12 / count( $names ) );
53
54 $result = array();
55
56 foreach ( $names as $name ) {
57 if ( empty( $this->sub_fields[ $name ] ) ) {
58 continue;
59 }
60
61 if ( ! isset( $this->sub_fields[ $name ]['wrapper_classes'] ) ) {
62 $this->sub_fields[ $name ]['wrapper_classes'] = $col_class;
63 } elseif ( is_array( $this->sub_fields[ $name ]['wrapper_classes'] ) ) {
64 $this->sub_fields[ $name ]['wrapper_classes'] = implode( ' ', $this->sub_fields[ $name ]['wrapper_classes'] ) . ' ' . $col_class;
65 } else {
66 $this->sub_fields[ $name ]['wrapper_classes'] .= ' ' . $col_class;
67 }
68
69 $result[ $name ] = $this->sub_fields[ $name ];
70 }
71
72 return $result;
73 }
74
75 /**
76 * Gets name layout option value.
77 *
78 * @return string
79 */
80 protected function get_name_layout() {
81 $name_layout = FrmField::get_option( $this->field, 'name_layout' );
82 if ( ! $name_layout ) {
83 $name_layout = 'first_last';
84 }
85 return $name_layout;
86 }
87
88 /**
89 * Gets extra field options.
90 *
91 * @return string[]
92 */
93 protected function extra_field_opts() {
94 $extra_options = parent::extra_field_opts();
95
96 $extra_options['name_layout'] = 'first_last';
97
98 // Default desc.
99 foreach ( $this->sub_fields as $name => $sub_field ) {
100 $extra_options[ $name . '_desc' ] = $sub_field['label'];
101 }
102
103 return $extra_options;
104 }
105
106 /**
107 * Shows primary options.
108 *
109 * @since 4.0
110 *
111 * @param array $args Includes 'field', 'display', and 'values'.
112 *
113 * @return void
114 */
115 public function show_primary_options( $args ) {
116 $field = (array) $args['field'];
117 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/name/primary-options.php';
118
119 parent::show_primary_options( $args );
120 }
121
122 /**
123 * Prepares the display value.
124 * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle].
125 *
126 * @param mixed $value Field value before processing.
127 * @param array $atts Shortcode attributes.
128 * @return string Most of cases, this will return string.
129 */
130 protected function prepare_display_value( $value, $atts ) {
131 if ( ! is_array( $value ) ) {
132 return $value;
133 }
134
135 $name_layout = $this->get_name_layout();
136
137 if ( ! empty( $atts['show'] ) ) {
138 return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : '';
139 }
140
141 $value = wp_parse_args(
142 $value,
143 array(
144 'first' => '',
145 'middle' => '',
146 'last' => '',
147 )
148 );
149
150 switch ( $name_layout ) {
151 case 'last_first':
152 $value = $value['last'] . ' ' . $value['first'];
153 break;
154
155 case 'first_middle_last':
156 $value = $value['first'] . ' ' . $value['middle'] . ' ' . $value['last'];
157 break;
158
159 default:
160 $value = $value['first'] . ' ' . $value['last'];
161 }
162
163 return trim( $value );
164 }
165
166 /**
167 * @since 4.0.04
168 *
169 * @return void
170 */
171 public function sanitize_value( &$value ) {
172 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
173 }
174
175 /**
176 * Validate field.
177 *
178 * @param array $args Arguments. Includes `errors`, `value`.
179 * @return array Errors array.
180 */
181 public function validate( $args ) {
182 /**
183 * If users fill just HTML tag, it passes the validation but value is empty in the database because of the
184 * sanitization. So we need to sanitize the value before validating.
185 */
186 $this->sanitize_value( $args['value'] );
187 return parent::validate( $args );
188 }
189
190 /**
191 * Loads processed args for field output.
192 *
193 * @param array $args {
194 * Arguments.
195 *
196 * @type array $field Field array.
197 * @type string $html_id HTML ID.
198 * @type string $field_name Field name attribute.
199 * @type array $shortcode_atts Shortcode attributes.
200 * @type array $errors Field errors.
201 * @type bool $remove_names Remove name attribute or not.
202 * }
203 *
204 * @return void
205 */
206 protected function process_args_for_field_output( &$args ) {
207 parent::process_args_for_field_output( $args );
208
209 // Show all subfields in form builder then use JS to show or hide them.
210 if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) {
211 $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] );
212 $args['sub_fields'] = $this->sub_fields;
213
214 foreach ( $hidden_fields as $name => $hidden_field ) {
215 $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden';
216 }
217 }
218 }
219
220 /**
221 * Checks if should print hidden subfields and hide them. This is useful to use js to show or hide sub fields.
222 *
223 * @return bool
224 */
225 protected function should_print_hidden_sub_fields() {
226 // phpcs:ignore WordPress.Security.NonceVerification.Missing
227 return FrmAppHelper::is_form_builder_page() || FrmAppHelper::doing_ajax() && isset( $_POST['action'] ) && 'frm_insert_field' === $_POST['action'];
228 }
229
230 /**
231 * Gets inputs container attributes.
232 *
233 * @return array
234 */
235 protected function get_inputs_container_attrs() {
236 $attrs = parent::get_inputs_container_attrs();
237
238 $attrs['data-name-layout'] = $this->get_name_layout();
239 return $attrs;
240 }
241
242 /**
243 * Maybe show a warning if a name field is using a description that is not descriptive enough.
244 * Prior to version 6.16, First and Last were the default values, but this has been updated to
245 * improve accessibility.
246 *
247 * @since 6.16
248 *
249 * @param array $args
250 * @return void
251 */
252 public function show_after_default( $args ) {
253 parent::show_after_default( $args );
254
255 /**
256 * @var array $field
257 */
258 $field = $args['field'];
259
260 $show_warning = false;
261 foreach ( $this->sub_fields as $name => $sub_field ) {
262 $description = FrmField::get_option( $field, $sub_field['name'] . '_desc' );
263 if ( in_array( $description, array( 'First', 'Last' ), true ) ) {
264 $show_warning = true;
265 break;
266 }
267 }
268
269 if ( ! $show_warning ) {
270 return;
271 }
272 ?>
273 <div class="frm_warning_style">
274 <?php
275 FrmAppHelper::icon_by_class( 'frm_icon_font frm_alert_icon', array( 'style' => 'width:24px' ) );
276 echo ' ';
277 esc_html_e( 'Subfield descriptions are read by screen readers. Enhance accessibility by using complete labels, like "First Name" instead of "First".', 'formidable' );
278 ?>
279 </div>
280 <?php
281 }
282 }
283