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

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