PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.13
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.13
3.1.14 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 All 123 releases
firebox / Inc / Framework / Inc / Base / FieldsParser.php

FieldsParser.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 1.0.13, at Inc/Framework/Inc/Base/FieldsParser.php

454 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FirePlugins Framework
4 * @version 1.0.27
5 *
6 * @author FirePlugins <[email protected]>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2022 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FPFramework\Base;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Helpers\HTML;
20 use FPFramework\Helpers\StringHelper;
21
22 class FieldsParser
23 {
24 /**
25 * The Bind Data
26 *
27 * @var array
28 */
29 protected $bind_data;
30
31 /**
32 * The fields path of plugin.
33 *
34 * @var string
35 */
36 protected $fields_path;
37
38 /**
39 * The fields name prefix.
40 * This is used to prefix our field names.
41 *
42 * @var string
43 */
44 protected $fields_name_prefix;
45
46 public function __construct($data = [])
47 {
48 $this->bind_data = isset($data['bind_data']) ? $data['bind_data'] : [];
49 $this->fields_path = isset($data['fields_path']) ? (array) $data['fields_path'] : null;
50 $this->fields_name_prefix = isset($data['fields_name_prefix']) ? $data['fields_name_prefix'] : \FPFramework\Admin\Includes\MetaboxManager::$fields_prefix;
51 }
52
53 /**
54 * Renders the sections, in left, plain or right orientation
55 *
56 * @param array $section
57 *
58 * @return void
59 */
60 public function renderContentFields($section)
61 {
62 if (!isset($section['content']) && !is_array($section['content']) && !count($section['content']))
63 {
64 return;
65 }
66
67 // loop sections
68 foreach ($section['content'] as $_section)
69 {
70 // render section fields
71 $this->renderSectionFields($_section);
72 }
73 }
74
75 /**
76 * Renders all fields within a section
77 *
78 * @param array $section
79 *
80 * @return void
81 */
82 protected function renderSectionFields($section)
83 {
84 if (!isset($section['fields']))
85 {
86 return;
87 }
88
89 // wrap section title and fields - start
90 $this->wrapFieldsSectionStart($section);
91
92 // Render title of this set of fields
93 $this->renderSectionTitle($section);
94
95 // start - wrap the fields in a parent div
96 $this->checkAndStartWrapSectionFieldsInDiv($section);
97
98 // render all fields
99 $this->renderFields($section['fields']);
100
101 // end - wrap the fields in a parent div
102 $this->checkAndEndWrapSectionFieldsInDiv($section);
103
104 // wrap section title and fields - end
105 $this->wrapFieldsSectionEnd($section);
106 }
107
108 /**
109 * Renders given fields
110 *
111 * @param string $fields
112 *
113 * @return void
114 */
115 public function renderFields($fields)
116 {
117 if (!is_array($fields) || !count($fields))
118 {
119 return;
120 }
121
122 foreach ($fields as $key => $field)
123 {
124 $this->loadField($field);
125 }
126 }
127
128 /**
129 * Loads the field
130 *
131 * @param array $field
132 *
133 * @return mixed
134 */
135 public function loadField($field)
136 {
137 if (!isset($field['type']))
138 {
139 return;
140 }
141
142 if (!$field_path = $this->getFieldPath($field))
143 {
144 return;
145 }
146
147 // check if we need this field to have a name without prepending the prefix
148 $name_clean = isset($field['name_clean']) ? (bool) $field['name_clean'] : false;
149
150 // set field name prefix
151 if (isset($field['name']) && !$name_clean)
152 {
153 $field['name_key'] = $field['name'];
154
155 // set name prefix for the field
156 if ($this->fields_name_prefix)
157 {
158 $field['name_prefix'] = $this->fields_name_prefix;
159 }
160 }
161
162 // set field path
163 $field['field_path'] = $field_path;
164
165 // full field class name
166 $field_class = $field_path . $field['type'];
167
168 // bind data
169 if ($this->bind_data && isset($field['name_key']))
170 {
171 // get binded value
172 $binded_value = $this->getBindDataFieldValue($field['name_key']);
173
174 // set field value
175 if($binded_value || $binded_value == '0')
176 {
177 $field['value'] = $binded_value;
178 }
179 }
180
181 // initialize field
182 $field_class = new $field_class($field);
183
184 $field_class->render();
185 }
186
187 /**
188 * Returns the field path. Whether this field exists in the framework or in a plugin.
189 *
190 * @param array $field
191 *
192 * @return mixed
193 */
194 private function getFieldPath($field)
195 {
196 $field_type = $field['type'];
197
198 if ($this->fields_path && is_array($this->fields_path))
199 {
200 foreach ($this->fields_path as $fields_path)
201 {
202 // ensure field exists in field path
203 if (!class_exists($fields_path . $field_type))
204 {
205 continue;
206 }
207
208 return $fields_path;
209 }
210 }
211
212 // check framework
213 $framework_fields_path = '\\FPFramework\\Base\\Fields\\';
214 if (class_exists($framework_fields_path . $field_type))
215 {
216 return $framework_fields_path;
217 }
218
219 return false;
220 }
221
222 /**
223 * Gets the value from bind data based on field name
224 *
225 * @param string $name
226 *
227 * @return mixed
228 */
229 protected function getBindDataFieldValue($name)
230 {
231 if (!$name)
232 {
233 return null;
234 }
235
236 $value = '';
237 $found = true;
238
239 // make sure bind data are set
240 if (!isset($this->bind_data))
241 {
242 return null;
243 }
244
245 $this->bind_data = (array) $this->bind_data;
246
247 // split name as format is key.key2.key3
248 $splitted_name = explode('.', $name);
249
250 // make sure we splitted the name and have data to use
251 if (!count($splitted_name))
252 {
253 // check if it exists
254 if (!isset($this->bind_data[$name]))
255 {
256 return null;
257 }
258
259 return $this->bind_data[$name];
260 }
261
262 // get first name item
263 $key = $splitted_name[0];
264
265 // if we have 1 name key only
266 if (count($splitted_name) == 1)
267 {
268 // make sure it exists
269 if (!isset($this->bind_data[$key]))
270 {
271 return null;
272 }
273
274 return $this->bind_data[$key];
275 }
276
277 // get the bind data to use and find the value
278 $value = $this->bind_data;
279
280 // loop given name keys to find value
281 foreach ($splitted_name as $n)
282 {
283 $value = is_object($value) ? (array) $value : $value;
284
285 if (!isset($value[$n]))
286 {
287 $found = null;
288 break;
289 }
290
291 $value = $value[$n];
292 }
293
294 if (!$found)
295 {
296 return null;
297 }
298
299 return $value;
300 }
301
302 /**
303 * Wraps the section title and fields.
304 *
305 * @param array $section
306 *
307 * @return void
308 */
309 private function wrapFieldsSectionStart($section)
310 {
311 if (!isset($section['fields']) && !is_array($section['fields']) && count($section['fields']) < 2 && !isset($section['wrapper']))
312 {
313 return;
314 }
315
316 $showon = isset($section['wrapper']['showon']) ? $section['wrapper']['showon'] : '';
317
318 // outer classes
319 $default_outer_classes = ['fpf-fields-wrapper'];
320 $outer_classes = (isset($section['wrapper']) && isset($section['wrapper']['outer_class'])) ? $section['wrapper']['outer_class'] : $default_outer_classes;
321
322 // inner classes
323 $default_classes = ['grid-x', 'grid-margin-x', 'grid-margin-y'];
324 $inner_classes = isset($section['wrapper']) && isset($section['wrapper']['class']) ? $section['wrapper']['class'] : $default_classes;
325
326 // render div openings
327 if ($outer_classes)
328 {
329 echo HTML::renderStartDiv([
330 'showon' => $showon,
331 'class' => $outer_classes
332 ]);
333 }
334
335 if ($inner_classes)
336 {
337 echo HTML::renderStartDiv([
338 'class' => $inner_classes
339 ]);
340 }
341 }
342
343 /**
344 * Ends the wrap of section title and fields.
345 *
346 * @param array $section
347 *
348 * @return void
349 */
350 private function wrapFieldsSectionEnd($section)
351 {
352 if (!isset($section['fields']) && !is_array($section['fields']) && count($section['fields']) < 2 && !isset($section['wrapper']))
353 {
354 return;
355 }
356
357 // outer classes
358 $default_outer_classes = ['fpf-fields-wrapper'];
359 $outer_classes = (isset($section['wrapper']) && isset($section['wrapper']['outer_class'])) ? $section['wrapper']['outer_class'] : $default_outer_classes;
360
361 // inner classes
362 $default_classes = ['grid-x', 'grid-margin-x', 'grid-margin-y'];
363 $inner_classes = isset($section['wrapper']) && isset($section['wrapper']['class']) ? $section['wrapper']['class'] : $default_classes;
364
365 // render div openings
366 if ($outer_classes)
367 {
368 echo HTML::renderEndDiv();
369 }
370
371 if ($inner_classes)
372 {
373 echo HTML::renderEndDiv();
374 }
375 }
376
377 /**
378 * Renders the title of the fields section
379 *
380 * @param array $section
381 *
382 * @return string
383 */
384 protected function renderSectionTitle($section)
385 {
386 if (!isset($section['title']))
387 {
388 return;
389 }
390
391 // set default heading type
392 if (!isset($section['title']['heading_type']))
393 {
394 $section['title']['heading_type'] = 'h4';
395 }
396
397 $section['title']['input_class'][] = 'cell large-3 fpf-section-heading';
398
399 echo HTML::renderHeading($section['title']);
400 }
401
402 /**
403 * Checks if we have a section title and wraps the fields with a div
404 *
405 * @param array $section
406 *
407 * @return string
408 */
409 protected function checkAndStartWrapSectionFieldsInDiv($section)
410 {
411 if (!isset($section['title']))
412 {
413 return;
414 }
415
416 echo HTML::renderStartDiv([
417 'class' => ['cell', 'large-auto', 'fpf-section-content']
418 ]);
419
420 echo HTML::renderStartDiv([
421 'class' => ['grid-x', 'grid-margin-x', 'grid-margin-y']
422 ]);
423 }
424
425 /**
426 * Checks if we have a section title and close the div that wrapped the fields
427 *
428 * @param array $section
429 *
430 * @return string
431 */
432 protected function checkAndEndWrapSectionFieldsInDiv($section)
433 {
434 if (!isset($section['title']))
435 {
436 return;
437 }
438
439 echo HTML::renderEndDiv();
440 echo HTML::renderEndDiv();
441 }
442
443 /**
444 * Set form bind data
445 *
446 * @param array $bind_data
447 *
448 * @return void
449 */
450 public function setBindData($bind_data)
451 {
452 $this->bind_data = $bind_data;
453 }
454 }