PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
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 / FrmFieldValueSelector.php

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

327 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * A class for the field value selector
8 * Used in field conditional logic, action conditional logic, Mailchimp action, etc.
9 *
10 * @since 2.03.05
11 */
12 class FrmFieldValueSelector {
13
14 /**
15 * @var int
16 *
17 * @since 2.03.05
18 */
19 protected $field_id = 0;
20
21 /**
22 * @var string
23 *
24 * @since 2.03.05
25 */
26 protected $field_key = '';
27
28 /**
29 * @var FrmProFieldSettings|null
30 *
31 * @since 2.03.05
32 */
33 protected $field_settings;
34
35 /**
36 * @var array
37 *
38 * @since 2.03.05
39 */
40 protected $options = array();
41
42 /**
43 * @var string
44 *
45 * @since 2.03.05
46 */
47 protected $html_name = '';
48
49 /**
50 * @var string
51 *
52 * @since 2.03.05
53 */
54 protected $value = '';
55
56 /**
57 * @var string
58 *
59 * @since 2.03.05
60 */
61 protected $source = 'unknown';
62
63 /**
64 * @var string
65 *
66 * @since 2.03.05
67 */
68 protected $blank_option_label = '';
69
70 /**
71 * @var object|null
72 *
73 * @since 2.03.05
74 */
75 protected $db_row;
76
77 /**
78 * Field dropdowns will truncate at 25 characters by default.
79 *
80 * @var int|null
81 *
82 * @since 6.16.2
83 */
84 protected $truncate;
85
86 /**
87 * FrmFieldValueSelector constructor
88 *
89 * @param int|string $field_id Field ID.
90 * @param array $args Arguments for configuring the value selector.
91 */
92 public function __construct( $field_id, $args ) {
93 $this->set_html_name( $args );
94 $this->set_value( $args );
95 $this->set_source( $args );
96 $this->set_truncate( $args );
97
98 $this->field_id = (int) $field_id;
99
100 if ( $this->field_id === 0 ) {
101 return;
102 }
103
104 $this->set_db_row();
105
106 if ( ! $this->has_db_row() ) {
107 return;
108 }
109
110 $this->set_field_key();
111 $this->set_field_settings();
112 $this->set_options();
113 }
114
115 /**
116 * Set the db_row property
117 *
118 * @since 2.03.05
119 *
120 * @return void
121 */
122 private function set_db_row() {
123 $where = array(
124 'id' => $this->field_id,
125 );
126
127 $this->db_row = FrmDb::get_row( 'frm_fields', $where );
128
129 if ( ! is_object( $this->db_row ) ) {
130 $this->db_row = null;
131 }
132 }
133
134 /**
135 * Set the field_key property
136 *
137 * @since 2.03.05
138 *
139 * @return void
140 */
141 private function set_field_key() {
142 $this->field_key = $this->db_row->field_key;
143 }
144
145 /**
146 * Set the field_settings property
147 *
148 * @since 2.03.05
149 *
150 * @return void
151 */
152 protected function set_field_settings() {
153 // Leave as null for free version
154 }
155
156 /**
157 * Set the options property
158 *
159 * @since 2.03.05
160 *
161 * @return void
162 */
163 protected function set_options() {
164 $field_obj = FrmFieldFactory::get_field_object( $this->db_row );
165 $this->options = $field_obj->get_options( array() );
166 }
167
168 /**
169 * Set the html_name property
170 *
171 * @since 2.03.05
172 *
173 * @param array $args
174 *
175 * @return void
176 */
177 protected function set_html_name( $args ) {
178 if ( isset( $args['html_name'] ) ) {
179 $this->html_name = (string) $args['html_name'];
180 }
181 }
182
183 /**
184 * Set the selected_value property
185 *
186 * @since 2.03.05
187 *
188 * @param array $args
189 *
190 * @return void
191 */
192 protected function set_value( $args ) {
193 if ( isset( $args['value'] ) ) {
194 $this->value = (string) $args['value'];
195 }
196 }
197
198 /**
199 * Set the source property
200 *
201 * @since 2.03.05
202 *
203 * @param array $args
204 *
205 * @return void
206 */
207 protected function set_source( $args ) {
208 if ( isset( $args['source'] ) ) {
209 $this->source = (string) $args['source'];
210 }
211 }
212
213 /**
214 * @since 6.16.2
215 *
216 * @param array $args
217 *
218 * @return void
219 */
220 protected function set_truncate( $args ) {
221 if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) {
222 $this->truncate = (int) $args['truncate'];
223 }
224 }
225
226 /**
227 * Check if object has any options
228 *
229 * @since 2.03.05
230 *
231 * @return bool
232 */
233 final protected function has_options() {
234 return ! empty( $this->options );
235 }
236
237 /**
238 * Check if a field is connected to the value selector
239 *
240 * @since 2.03.05
241 *
242 * @return bool
243 */
244 final protected function has_db_row() {
245 return $this->db_row !== null;
246 }
247
248 /**
249 * Display the field value selector (dropdown or text field)
250 *
251 * @since 2.03.05
252 *
253 * @return void
254 */
255 public function display() {
256 if ( $this->has_options() ) {
257 $this->display_dropdown();
258 } else {
259 $this->display_text_box();
260 }
261 }
262
263 /**
264 * Print the field value text box
265 *
266 * @since 2.03.05
267 *
268 * @return void
269 */
270 public function display_text_box() {
271 echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />';
272 }
273
274 /**
275 * Display the field value selector
276 *
277 * @since 2.03.05
278 *
279 * @return void
280 */
281 protected function display_dropdown() {
282 echo '<select name="' . esc_attr( $this->html_name ) . '">';
283 echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>';
284
285 if ( $this->options ) {
286 $truncate = $this->truncate ?? 25;
287
288 foreach ( $this->options as $key => $value ) {
289 // phpcs:ignore Universal.Operators.StrictComparisons
290 if ( $value == '' ) {
291 continue;
292 }
293
294 $option = $this->get_single_field_option( $key, $value );
295 $option->print_single_option( $this->value, $truncate, $this->use_value_as_label_in_dropdown() );
296 }
297 }
298
299 echo '</select>';
300 }
301
302 /**
303 * Whether to use the field value as the option label in the dropdown when the label is empty.
304 *
305 * @since 6.25.1
306 *
307 * @return bool
308 */
309 protected function use_value_as_label_in_dropdown() {
310 return false;
311 }
312
313 /**
314 * Get an instance of FrmFieldOption
315 *
316 * @since 2.03.05
317 *
318 * @param string $key
319 * @param string $value
320 *
321 * @return FrmFieldOption
322 */
323 protected function get_single_field_option( $key, $value ) {
324 return new FrmFieldOption( $key, $value );
325 }
326 }
327