PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.21
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.21
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.21, at classes/models/fields/FrmFieldName.php

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