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

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