PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / trunk
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment vtrunk
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / Form / Fields / Field.php

Field.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment trunk, at Inc/Core/Form/Fields/Field.php

251 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 3.1.13 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\Form\Fields;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 class Field
20 {
21 /**
22 * Field type. Set by subclasses.
23 *
24 * @var string
25 */
26 protected $type = '';
27
28 /**
29 * Field options.
30 *
31 * @var array
32 */
33 protected $options = [];
34
35 /**
36 * Validation message that appears when a field fails validation.
37 *
38 * @var string
39 */
40 protected $validation_message = '';
41
42 /**
43 * The form data.
44 *
45 * @var array
46 */
47 protected $data = [];
48
49 public function __construct($options = [], $data = [])
50 {
51 // Merge default options with given options
52 $this->options = array_merge($this->getDefaultOptions(), $options);
53
54 // Then merge new options with each field options
55 $this->options = array_merge($this->options, $this->getFieldOptions());
56
57 $this->data = $data;
58 }
59
60 protected function getFieldOptions()
61 {
62 return [
63 'required' => isset($this->options['required']) ? $this->options['required'] : true
64 ];
65 }
66
67 public function getType()
68 {
69 return $this->type;
70 }
71
72 /**
73 * Returns the default field options.
74 *
75 * @return array
76 */
77 private function getDefaultOptions()
78 {
79 return [
80 'type' => $this->type,
81 'name' => '',
82 'placeholder' => ''
83 ];
84 }
85
86 /**
87 * Validate the field.
88 *
89 * @param mixed $value
90 *
91 * @return bool
92 */
93 public function validate(&$value = '')
94 {
95 if ($this->isRequired() && empty($value))
96 {
97 $this->validation_message = firebox()->_('FB_THIS_IS_A_REQUIRED_FIELD');
98 return false;
99 }
100
101 return true;
102 }
103
104 /**
105 * Returns whether the field is required.
106 *
107 * @return bool
108 */
109 public function isRequired()
110 {
111 return $this->options['required'];
112 }
113
114 /**
115 * Prepare value to be displayed to the user as plain text
116 *
117 * @param mixed $value
118 *
119 * @return string
120 */
121 public function prepareValue($value)
122 {
123 if (is_bool($value))
124 {
125 return $value ? '1' : '0';
126 }
127
128 if (is_array($value))
129 {
130 return implode(', ', array_map('\FireBox\Core\Helpers\Form\Field::escape', $value));
131 }
132
133 // Check if the value is a JSON string
134 if (is_string($value) && ($json = json_decode($value, true)) && is_array($json))
135 {
136 return implode(', ', array_map('\FireBox\Core\Helpers\Form\Field::escape', $json));
137 }
138
139 // Strings and numbers
140 return \FireBox\Core\Helpers\Form\Field::escape($value);
141 }
142
143 public function getValue()
144 {
145 return isset($this->options['value']) ? $this->options['value'] : null;
146 }
147
148 public function prepareValueHTML($value)
149 {
150 return $this->prepareValue($value);
151 }
152
153 public function prepareRawValue($value)
154 {
155 return $value;
156 }
157
158 public function getValueRaw()
159 {
160 return $this->getValue();
161 }
162
163 public function getValueHTML()
164 {
165 return $this->prepareValueHTML($this->getValueRaw());
166 }
167
168 public function getValidationMessage()
169 {
170 return $this->validation_message;
171 }
172
173 public function setValue($value = '')
174 {
175 if (!$value)
176 {
177 return;
178 }
179
180 $this->options['value'] = $value;
181 }
182
183 public function setOptionValue($key = '', $value = '')
184 {
185 if (!$key)
186 {
187 return;
188 }
189
190 $this->options[$key] = $value;
191 }
192
193 public function getOptionValue($key = '', $default = '')
194 {
195 if (!$key)
196 {
197 return $default;
198 }
199
200 return isset($this->options[$key]) ? $this->options[$key] : $default;
201 }
202
203 public function addInputCSSClass($css = '')
204 {
205 $this->options['input_css_class'][] = $css;
206 }
207
208 public function setData($data = [])
209 {
210 $this->data = $data;
211 }
212
213 public function getData()
214 {
215 return $this->data;
216 }
217
218 /**
219 * Return the field label.
220 *
221 * @return string
222 */
223 public function getLabel()
224 {
225 return $this->getOptionValue('label');
226 }
227
228 /**
229 * Returns the field input.
230 *
231 * @return void
232 */
233 protected function getInput() {}
234
235 public function render()
236 {
237 ob_start();
238 $this->getInput();
239 $input = ob_get_contents();
240 ob_end_clean();
241
242 $payload = array_merge(
243 $this->options,
244 [
245 'input' => $input
246 ]
247 );
248
249 return fpframework()->renderer->render('form/fields/tmpl', $payload, true);
250 }
251 }