PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.6
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.6
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 3.1.6, at Inc/Core/Form/Fields/Field.php

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