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

228 lines 5.7 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 public function __construct( $field = '', $type = '' ) {
24 parent::__construct( $field, $type );
25
26 $this->register_sub_fields(
27 array(
28 'first' => __( 'First', 'formidable' ),
29 'middle' => __( 'Middle', 'formidable' ),
30 'last' => __( 'Last', 'formidable' ),
31 )
32 );
33 }
34
35 /**
36 * Gets processed sub fields.
37 * This should return the list of sub fields after sorting or show/hide based of some options.
38 *
39 * @return array
40 */
41 protected function get_processed_sub_fields() {
42 $name_layout = $this->get_name_layout();
43 $names = explode( '_', $name_layout );
44 $col_class = 'frm' . intval( 12 / count( $names ) );
45
46 $result = array();
47
48 foreach ( $names as $name ) {
49 if ( empty( $this->sub_fields[ $name ] ) ) {
50 continue;
51 }
52
53 if ( ! isset( $this->sub_fields[ $name ]['wrapper_classes'] ) ) {
54 $this->sub_fields[ $name ]['wrapper_classes'] = $col_class;
55 } elseif ( is_array( $this->sub_fields[ $name ]['wrapper_classes'] ) ) {
56 $this->sub_fields[ $name ]['wrapper_classes'] = implode( ' ', $this->sub_fields[ $name ]['wrapper_classes'] ) . ' ' . $col_class;
57 } else {
58 $this->sub_fields[ $name ]['wrapper_classes'] .= ' ' . $col_class;
59 }
60
61 $result[ $name ] = $this->sub_fields[ $name ];
62 }
63
64 return $result;
65 }
66
67 /**
68 * Gets name layout option value.
69 *
70 * @return string
71 */
72 protected function get_name_layout() {
73 $name_layout = FrmField::get_option( $this->field, 'name_layout' );
74 if ( ! $name_layout ) {
75 $name_layout = 'first_last';
76 }
77 return $name_layout;
78 }
79
80 /**
81 * Gets extra field options.
82 *
83 * @return string[]
84 */
85 protected function extra_field_opts() {
86 $extra_options = parent::extra_field_opts();
87
88 $extra_options['name_layout'] = 'first_last';
89
90 // Default desc.
91 foreach ( $this->sub_fields as $name => $sub_field ) {
92 $extra_options[ $name . '_desc' ] = $sub_field['label'];
93 }
94
95 return $extra_options;
96 }
97
98 /**
99 * Shows primary options.
100 *
101 * @since 4.0
102 *
103 * @param array $args Includes 'field', 'display', and 'values'.
104 */
105 public function show_primary_options( $args ) {
106 $field = (array) $args['field'];
107 include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/name/primary-options.php';
108
109 parent::show_primary_options( $args );
110 }
111
112 /**
113 * Prepares the display value.
114 * This also handles the shortcode output. Support [id], [id show=first], [id show=last], [id show=middle].
115 *
116 * @param mixed $value Field value before processing.
117 * @param array $atts Shortcode attributes.
118 * @return string Most of cases, this will return string.
119 */
120 protected function prepare_display_value( $value, $atts ) {
121 if ( ! is_array( $value ) ) {
122 return $value;
123 }
124
125 $name_layout = $this->get_name_layout();
126
127 if ( ! empty( $atts['show'] ) ) {
128 return isset( $value[ $atts['show'] ] ) ? $value[ $atts['show'] ] : '';
129 }
130
131 $value = wp_parse_args(
132 $value,
133 array(
134 'first' => '',
135 'middle' => '',
136 'last' => '',
137 )
138 );
139
140 switch ( $name_layout ) {
141 case 'last_first':
142 $value = $value['last'] . ' ' . $value['first'];
143 break;
144
145 case 'first_middle_last':
146 $value = $value['first'] . ' ' . $value['middle'] . ' ' . $value['last'];
147 break;
148
149 default:
150 $value = $value['first'] . ' ' . $value['last'];
151 }
152
153 return trim( $value );
154 }
155
156 /**
157 * @since 4.0.04
158 */
159 public function sanitize_value( &$value ) {
160 FrmAppHelper::sanitize_value( 'sanitize_text_field', $value );
161 }
162
163 /**
164 * Validate field.
165 *
166 * @param array $args Arguments. Includes `errors`, `value`.
167 * @return array Errors array.
168 */
169 public function validate( $args ) {
170 /**
171 * If users fill just HTML tag, it passes the validation but value is empty in the database because of the
172 * sanitization. So we need to sanitize the value before validating.
173 */
174 $this->sanitize_value( $args['value'] );
175 return parent::validate( $args );
176 }
177
178 /**
179 * Loads processed args for field output.
180 *
181 * @param array $args {
182 * Arguments.
183 *
184 * @type array $field Field array.
185 * @type string $html_id HTML ID.
186 * @type string $field_name Field name attribute.
187 * @type array $shortcode_atts Shortcode attributes.
188 * @type array $errors Field errors.
189 * @type bool $remove_names Remove name attribute or not.
190 * }
191 */
192 protected function process_args_for_field_output( &$args ) {
193 parent::process_args_for_field_output( $args );
194
195 // Show all subfields in form builder then use JS to show or hide them.
196 if ( $this->should_print_hidden_sub_fields() && count( $args['sub_fields'] ) !== count( $this->sub_fields ) ) {
197 $hidden_fields = array_diff_key( $this->sub_fields, $args['sub_fields'] );
198 $args['sub_fields'] = $this->sub_fields;
199
200 foreach ( $hidden_fields as $name => $hidden_field ) {
201 $args['sub_fields'][ $name ]['wrapper_classes'] .= ' frm_hidden';
202 }
203 }
204 }
205
206 /**
207 * Checks if should print hidden subfields and hide them. This is useful to use js to show or hide sub fields.
208 *
209 * @return bool
210 */
211 protected function should_print_hidden_sub_fields() {
212 // phpcs:ignore WordPress.Security.NonceVerification.Missing
213 return FrmAppHelper::is_form_builder_page() || FrmAppHelper::doing_ajax() && isset( $_POST['action'] ) && 'frm_insert_field' === $_POST['action'];
214 }
215
216 /**
217 * Gets inputs container attributes.
218 *
219 * @return array
220 */
221 protected function get_inputs_container_attrs() {
222 $attrs = parent::get_inputs_container_attrs();
223
224 $attrs['data-name-layout'] = $this->get_name_layout();
225 return $attrs;
226 }
227 }
228