PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.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.26.1, at classes/models/FrmFieldValueSelector.php

324 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 $this->set_field_key();
108 $this->set_field_settings();
109 $this->set_options();
110 }
111 }
112
113 /**
114 * Set the db_row property
115 *
116 * @since 2.03.05
117 *
118 * @return void
119 */
120 private function set_db_row() {
121 $where = array(
122 'id' => $this->field_id,
123 );
124
125 $this->db_row = FrmDb::get_row( 'frm_fields', $where );
126
127 if ( ! is_object( $this->db_row ) ) {
128 $this->db_row = null;
129 }
130 }
131
132 /**
133 * Set the field_key property
134 *
135 * @since 2.03.05
136 *
137 * @return void
138 */
139 private function set_field_key() {
140 $this->field_key = $this->db_row->field_key;
141 }
142
143 /**
144 * Set the field_settings property
145 *
146 * @since 2.03.05
147 *
148 * @return void
149 */
150 protected function set_field_settings() {
151 // Leave as null for free version
152 }
153
154 /**
155 * Set the options property
156 *
157 * @since 2.03.05
158 *
159 * @return void
160 */
161 protected function set_options() {
162 $field_obj = FrmFieldFactory::get_field_object( $this->db_row );
163 $this->options = $field_obj->get_options( array() );
164 }
165
166 /**
167 * Set the html_name property
168 *
169 * @since 2.03.05
170 *
171 * @param array $args
172 *
173 * @return void
174 */
175 protected function set_html_name( $args ) {
176 if ( isset( $args['html_name'] ) ) {
177 $this->html_name = (string) $args['html_name'];
178 }
179 }
180
181 /**
182 * Set the selected_value property
183 *
184 * @since 2.03.05
185 *
186 * @param array $args
187 *
188 * @return void
189 */
190 protected function set_value( $args ) {
191 if ( isset( $args['value'] ) ) {
192 $this->value = (string) $args['value'];
193 }
194 }
195
196 /**
197 * Set the source property
198 *
199 * @since 2.03.05
200 *
201 * @param array $args
202 *
203 * @return void
204 */
205 protected function set_source( $args ) {
206 if ( isset( $args['source'] ) ) {
207 $this->source = (string) $args['source'];
208 }
209 }
210
211 /**
212 * @since 6.16.2
213 *
214 * @param array $args
215 *
216 * @return void
217 */
218 protected function set_truncate( $args ) {
219 if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) {
220 $this->truncate = (int) $args['truncate'];
221 }
222 }
223
224 /**
225 * Check if object has any options
226 *
227 * @since 2.03.05
228 *
229 * @return bool
230 */
231 final protected function has_options() {
232 return ! empty( $this->options );
233 }
234
235 /**
236 * Check if a field is connected to the value selector
237 *
238 * @since 2.03.05
239 *
240 * @return bool
241 */
242 final protected function has_db_row() {
243 return $this->db_row !== null;
244 }
245
246 /**
247 * Display the field value selector (dropdown or text field)
248 *
249 * @since 2.03.05
250 *
251 * @return void
252 */
253 public function display() {
254 if ( $this->has_options() ) {
255 $this->display_dropdown();
256 } else {
257 $this->display_text_box();
258 }
259 }
260
261 /**
262 * Print the field value text box
263 *
264 * @since 2.03.05
265 *
266 * @return void
267 */
268 public function display_text_box() {
269 echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />';
270 }
271
272 /**
273 * Display the field value selector
274 *
275 * @since 2.03.05
276 *
277 * @return void
278 */
279 protected function display_dropdown() {
280 echo '<select name="' . esc_attr( $this->html_name ) . '">';
281 echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>';
282
283 if ( ! empty( $this->options ) ) {
284 $truncate = $this->truncate ?? 25;
285
286 foreach ( $this->options as $key => $value ) {
287 if ( $value == '' ) {
288 continue;
289 }
290
291 $option = $this->get_single_field_option( $key, $value );
292 $option->print_single_option( $this->value, $truncate, $this->use_value_as_label_in_dropdown() );
293 }
294 }
295
296 echo '</select>';
297 }
298
299 /**
300 * Whether to use the field value as the option label in the dropdown when the label is empty.
301 *
302 * @since 6.25.1
303 *
304 * @return bool
305 */
306 protected function use_value_as_label_in_dropdown() {
307 return false;
308 }
309
310 /**
311 * Get an instance of FrmFieldOption
312 *
313 * @since 2.03.05
314 *
315 * @param string $key
316 * @param string $value
317 *
318 * @return FrmFieldOption
319 */
320 protected function get_single_field_option( $key, $value ) {
321 return new FrmFieldOption( $key, $value );
322 }
323 }
324