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

236 lines 5.9 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', 'formidable' ),
37 'middle' => __( 'Middle', 'formidable' ),
38 'last' => __( 'Last', '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 public function show_primary_options( $args ) {
114 $field = (array) $args['field'];
115 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/name/primary-options.php';
116
117 parent::show_primary_options( $args );
118 }
119
120 /**
121 * Prepares the display value.
122 * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle].
123 *
124 * @param mixed $value Field value before processing.
125 * @param array $atts Shortcode attributes.
126 * @return string Most of cases, this will return string.
127 */
128 protected function prepare_display_value( $value, $atts ) {
129 if ( ! is_array( $value ) ) {
130 return $value;
131 }
132
133 $name_layout = $this->get_name_layout();
134
135 if ( ! empty( $atts['show'] ) ) {
136 return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : '';
137 }
138
139 $value = wp_parse_args(
140 $value,
141 array(
142 'first' => '',
143 'middle' => '',
144 'last' => '',
145 )
146 );
147
148 switch ( $name_layout ) {
149 case 'last_first':
150 $value = $value['last'] . ' ' . $value['first'];
151 break;
152
153 case 'first_middle_last':
154 $value = $value['first'] . ' ' . $value['middle'] . ' ' . $value['last'];
155 break;
156
157 default:
158 $value = $value['first'] . ' ' . $value['last'];
159 }
160
161 return trim( $value );
162 }
163
164 /**
165 * @since 4.0.04
166 */
167 public function sanitize_value( &$value ) {
168 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
169 }
170
171 /**
172 * Validate field.
173 *
174 * @param array $args Arguments. Includes `errors`, `value`.
175 * @return array Errors array.
176 */
177 public function validate( $args ) {
178 /**
179 * If users fill just HTML tag, it passes the validation but value is empty in the database because of the
180 * sanitization. So we need to sanitize the value before validating.
181 */
182 $this->sanitize_value( $args['value'] );
183 return parent::validate( $args );
184 }
185
186 /**
187 * Loads processed args for field output.
188 *
189 * @param array $args {
190 * Arguments.
191 *
192 * @type array $field Field array.
193 * @type string $html_id HTML ID.
194 * @type string $field_name Field name attribute.
195 * @type array $shortcode_atts Shortcode attributes.
196 * @type array $errors Field errors.
197 * @type bool $remove_names Remove name attribute or not.
198 * }
199 */
200 protected function process_args_for_field_output( &$args ) {
201 parent::process_args_for_field_output( $args );
202
203 // Show all subfields in form builder then use JS to show or hide them.
204 if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) {
205 $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] );
206 $args['sub_fields'] = $this->sub_fields;
207
208 foreach ( $hidden_fields as $name => $hidden_field ) {
209 $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden';
210 }
211 }
212 }
213
214 /**
215 * Checks if should print hidden subfields and hide them. This is useful to use js to show or hide sub fields.
216 *
217 * @return bool
218 */
219 protected function should_print_hidden_sub_fields() {
220 // phpcs:ignore WordPress.Security.NonceVerification.Missing
221 return FrmAppHelper::is_form_builder_page() || FrmAppHelper::doing_ajax() && isset( $_POST['action'] ) && 'frm_insert_field' === $_POST['action'];
222 }
223
224 /**
225 * Gets inputs container attributes.
226 *
227 * @return array
228 */
229 protected function get_inputs_container_attrs() {
230 $attrs = parent::get_inputs_container_attrs();
231
232 $attrs['data-name-layout'] = $this->get_name_layout();
233 return $attrs;
234 }
235 }
236