PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 1.0.7
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v1.0.7
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 / FB / Box.php

Box.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 1.0.7, at Inc/Core/FB/Box.php

906 lines 22.1 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 1.0.7 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2021 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\FB;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FireBox\Core\Helpers\BoxHelper;
20 use FPFramework\Libs\Registry;
21 use FPFramework\Helpers\Fields\DimensionsHelper;
22
23 class Box
24 {
25 /**
26 * Send useful JS snippet once in first box
27 *
28 * @var boolean
29 */
30 static $loadedLocalizedScript = false;
31
32 /**
33 * The box settings
34 */
35 private $boxSettings;
36
37 /**
38 * The factory
39 *
40 * @var Factory
41 */
42 protected $factory;
43
44 public function __construct($boxSettings = null, $factory = null)
45 {
46 $this->init($boxSettings, $factory);
47 }
48
49 public function init($boxSettings = null, $factory = null)
50 {
51 if (!$factory)
52 {
53 $factory = new \FPFramework\Base\Factory();
54 }
55
56 $this->boxSettings = $boxSettings;
57 $this->factory = $factory;
58 }
59
60 /**
61 * Gets published box.
62 *
63 * @param integer $id
64 * @param string $status
65 *
66 * @return array
67 */
68 public function get($id, $status = null)
69 {
70 $payload = [
71 'where' => [
72 'ID' => ' = ' . esc_sql($id),
73 'post_type' => " = 'firebox'"
74 ]
75 ];
76
77 // apply status if given
78 if ($status)
79 {
80 $payload['where']['post_status'] = ' = \'' . esc_sql($status) . '\'';
81 }
82
83 if (!$box = firebox()->tables->box->getResults($payload))
84 {
85 return [];
86 }
87
88 if (!isset($box[0]))
89 {
90 return [];
91 }
92
93 $box = $box[0];
94
95 // get meta options for box
96 $meta = self::getMeta($id);
97 $box->params = new Registry($meta);
98
99 return $box;
100 }
101
102 /**
103 * Get box meta
104 *
105 * @param int $id
106 *
107 * @return array
108 */
109 public static function getMeta($id)
110 {
111 return get_post_meta($id, 'fpframework_meta_settings', true);
112 }
113
114 /**
115 * Render box
116 *
117 * @return string
118 */
119 public function render($box = null)
120 {
121 // we do not have a passed box or box settings from constructor, abort
122 // as we cannot render a box without any box parameters
123 if (!$box && !$this->boxSettings)
124 {
125 return;
126 }
127
128 // if a box was given to the function, use these settings instead
129 if ($box)
130 {
131 $this->boxSettings = $box;
132 }
133
134 // if we have box settings, then set up our box with these settings
135 if ($this->boxSettings)
136 {
137 $box = $this->boxSettings;
138 }
139
140 // Check Publishing Assignments
141 if (!$this->pass($box))
142 {
143 return;
144 }
145
146 // get settings params
147 $params = BoxHelper::getParams();
148
149 if (!did_action('wp_enqueue_scripts'))
150 {
151 /**
152 * Loads all media files.
153 */
154 add_action('wp_enqueue_scripts', function() use ($box, $params) {
155 // load box media
156 $this->loadBoxMedia($box, $params);
157 });
158 }
159 else
160 {
161 // load box media
162 $this->loadBoxMedia($box, $params);
163 }
164
165 self::prepare($box);
166
167 if (!did_action('wp_head'))
168 {
169 /**
170 * Custom CSS
171 */
172 add_action('wp_head', function() use ($box) {
173 if ($css = $this->getCustomCSS($box))
174 {
175 echo '<style type="text/css" class="fb-' . esc_attr($box->ID) . '-style fb-responsive-style">' . $css . '</style>';
176 }
177 });
178 }
179 else
180 {
181 if ($css = $this->getCustomCSS($box))
182 {
183 echo '<style type="text/css" class="fb-' . esc_attr($box->ID) . '-style fb-responsive-style">' . $css . '</style>';
184 }
185 }
186
187 /**
188 * Runs before rendering the box.
189 */
190 do_action('firebox/box/before_render', $box);
191
192 // after we prepare it, update box settings
193 $this->boxSettings = $box;
194
195 // payload
196 $payload = [
197 'box' => $box,
198 'params' => $params,
199 ];
200
201 // return box template
202 add_action('wp_footer', function() use ($box, $payload) {
203 echo firebox()->renderer->public->render('box', $payload, true);
204 });
205 }
206
207 /**
208 * Gets the Custom CSS of the popup.
209 *
210 * @param object $box
211 *
212 * @return string
213 */
214 private function getCustomCSS($box)
215 {
216 $css = is_string($box->params->get('customcss')) ? $box->params->get('customcss') : '';
217
218 // add responsive CSS
219 if ($responsive_css = (array) $box->params->get('responsive_css', []))
220 {
221 if (isset($responsive_css['tablet']) && !empty($responsive_css['tablet']))
222 {
223 $css .= '
224 @media only screen and (max-width: 768px) {
225 .fb-' . esc_attr($box->ID) . ' .fb-dialog { ' . esc_html($responsive_css['tablet']) . ' }
226 }';
227 }
228 if (isset($responsive_css['mobile']) && !empty($responsive_css['mobile']))
229 {
230 $css .= '
231 @media only screen and (max-width: 468px) {
232 .fb-' . esc_attr($box->ID) . ' .fb-dialog { ' . esc_html($responsive_css['mobile']) . ' }
233 }';
234 }
235 }
236
237 return $css;
238 }
239
240 /**
241 * Send a helpful object to JavaScript files
242 *
243 * @return void
244 */
245 public static function setJSObject()
246 {
247 if (self::$loadedLocalizedScript)
248 {
249 return;
250 }
251 self::$loadedLocalizedScript = true;
252
253 $data = array(
254 'ajax_url' => admin_url('admin-ajax.php'),
255 'nonce' => wp_create_nonce('fbox_js_nonce'),
256 'site_url' => site_url('/'),
257 'referrer' => wp_get_referer()
258 );
259
260 wp_localize_script('firebox', 'fbox_js_object', $data);
261 }
262
263 /**
264 * Load Box Media
265 *
266 * @param array $box
267 * @param array $params
268 *
269 * @return void
270 */
271 public function loadBoxMedia($box, $params)
272 {
273 $box = new Registry($box);
274 $params = new Registry($params);
275
276 // Add polyfills for Internet Explorer
277 $browser = $this->factory->getBrowser();
278 if ($browser && is_array($browser) && array_key_exists('name', $browser) && $browser['name'] == 'ie')
279 {
280 wp_enqueue_script(
281 'firebox-ie11-polyfill',
282 'https://polyfill.io/v3/polyfill.min.js?features=NodeList.prototype.forEach%2CElement.prototype.closest%2CArray.prototype.forEach%2CArray.prototype.find%2CIntersectionObserver%2CIntersectionObserverEntry',
283 [],
284 FBOX_VERSION,
285 false
286 );
287 }
288
289 /**
290 * Velocity
291 */
292 if ($params->get('loadVelocity', true))
293 {
294 wp_enqueue_script(
295 'firebox-velocity',
296 FBOX_MEDIA_PUBLIC_URL . 'js/vendor/velocity.js',
297 [],
298 FBOX_VERSION,
299 false
300 );
301 wp_enqueue_script(
302 'firebox-velocity-ui',
303 FBOX_MEDIA_PUBLIC_URL . 'js/vendor/velocity.ui.js',
304 [],
305 FBOX_VERSION,
306 false
307 );
308
309 /**
310 * Animations
311 */
312 if (strpos($box->get('params.data.animationin'), 'firebox') !== false || strpos($box->get('params.data.animationout'), 'firebox') !== false)
313 {
314 wp_enqueue_script(
315 'firebox-animations',
316 FBOX_MEDIA_PUBLIC_URL . 'js/animations.js',
317 [],
318 FBOX_VERSION,
319 false
320 );
321 }
322 }
323
324 /**
325 * FireBox JS
326 */
327 wp_enqueue_script(
328 'firebox',
329 FBOX_MEDIA_PUBLIC_URL . 'js/firebox.js',
330 [],
331 FBOX_VERSION,
332 false
333 );
334
335 // run above the main JS script to run only once
336 self::setJSObject();
337
338 /**
339 * FireBox CSS
340 */
341 if ($params->get('loadCSS', true))
342 {
343 wp_enqueue_style(
344 'firebox',
345 FBOX_MEDIA_PUBLIC_URL . 'css/firebox.css',
346 [],
347 FBOX_VERSION,
348 false
349 );
350 }
351
352 /**
353 * Page Slide mode JS
354 */
355 if ($box->get('params.data.mode') == 'pageslide')
356 {
357 wp_enqueue_script(
358 'firebox-pageslide-mode',
359 FBOX_MEDIA_PUBLIC_URL . 'js/pageslide_mode.js',
360 [],
361 FBOX_VERSION,
362 false
363 );
364 }
365
366
367
368 $this->loadThemeCSSOverrides();
369 }
370
371 /**
372 * Some themes require overrides to preserve as much as we can the styling of the popups.
373 *
374 * @return void
375 */
376 private function loadThemeCSSOverrides()
377 {
378 $active_theme = wp_get_theme();
379 $theme = $active_theme->template;
380
381 /**
382 * This is the listed of the themes that we have created overrides
383 */
384 $themes = [
385 'twentytwentyone'
386 ];
387
388 if (!in_array($theme, $themes))
389 {
390 return;
391 }
392
393 wp_enqueue_style(
394 'firebox-theme-' . $theme . '-override',
395 FBOX_MEDIA_PUBLIC_URL . 'css/themes/' . $theme . '.css',
396 [],
397 FBOX_VERSION,
398 false
399 );
400 }
401
402 /**
403 * Prepares the box before rendering
404 *
405 * @param array $box
406 *
407 * @return void
408 */
409 public static function prepare(&$box)
410 {
411 $cParam = BoxHelper::getParams();
412 $cParam = new Registry($cParam);
413
414 $box->post_content = apply_filters('the_content', $box->post_content);
415
416 $css_class_prefix = 'fb-';
417
418 $position = $box->params->get('position', '');
419 $position = !is_string($position) ? '' : $position;
420
421 /* Classes */
422 $css_class = [
423 $box->ID,
424 $position
425 ];
426
427 $rtl = $box->params->get('rtl', '0');
428 if ($rtl == '1')
429 {
430 $css_class[] = 'rtl';
431 }
432
433 self::prefixCSSClasses($css_class);
434
435 // class suffix
436 $classSuffix = $box->params->get('classsuffix', '');
437 $classSuffix = is_string($classSuffix) ? $classSuffix : '';
438
439 $css_class[] = $classSuffix;
440
441 $box->classes = $css_class;
442
443 // box shadow
444 $boxshadow = (is_string($box->params->get('boxshadow', '1')) || is_int($box->params->get('boxshadow', '1'))) ? $box->params->get('boxshadow', '1') : '0';
445
446 $dialog_css_classes = [
447 $boxshadow != '0' ? 'shd' . $boxshadow : null
448 ];
449
450 // Align Content
451 $aligncontent = is_string($box->params->get('aligncontent')) ? explode(' ', $box->params->get('aligncontent')) : [];
452 $dialog_css_classes = array_merge($dialog_css_classes, $aligncontent);
453
454 self::prefixCSSClasses($dialog_css_classes);
455 $box->dialog_classes = $dialog_css_classes;
456
457 /* CSS */
458 // border
459 $border = is_string($box->params->get('bordertype', 'none')) ? $box->params->get('bordertype', 'none') : 'none';
460 $border_width = $box->params->get('borderwidth.value', 0) ? $box->params->get('borderwidth.value', 0) : 0;
461 $border_unit = is_string($box->params->get('borderwidth.unit', 'px')) ? $box->params->get('borderwidth.unit', 'px') : 0;
462 $border_color = is_string($box->params->get('bordercolor')) ? $box->params->get('bordercolor') : '';
463
464 $style = [
465 'background' => $box->params->get('backgroundcolor'),
466 'color' => $box->params->get('textcolor'),
467 'border' => $border == 'none' ? null : $border . ' ' . $border_width . $border_unit . ' ' . $border_color,
468 ];
469
470 // add shared properties
471 $shared_properties = [
472 'width' => [
473 'prefix_only' => true
474 ],
475 'height' => [
476 'prefix_only' => true
477 ],
478 'padding' => [],
479 'margin' => []
480 ];
481 foreach ($shared_properties as $prop => $prop_data)
482 {
483 // add dekstop property
484 $value = (array) $box->params->get($prop . '_control.' . $prop, []);
485 if (isset($value['desktop']))
486 {
487 $prefix_only = isset($prop_data['prefix_only']) ? (bool) $prop_data['prefix_only'] : false;
488
489 $desktop_value = DimensionsHelper::parseDimensionsData($value['desktop'], $prop, false, $prefix_only);
490 $style = array_merge($style, $desktop_value);
491 }
492 }
493
494 // add dekstop border_radius
495 $border_radius = (array) $box->params->get('borderradius_control.borderradius', []);
496 if (isset($border_radius['desktop']))
497 {
498 $desktop_border_radius = DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['desktop']);
499 $style = array_merge($style, $desktop_border_radius);
500 }
501
502 // Background Image
503 if ($box->params->get('bgimage', false))
504 {
505 $bgrepeat = is_string($box->params->get('bgrepeat')) ? $box->params->get('bgrepeat') : '';
506 $bgsize = is_string($box->params->get('bgsize')) ? $box->params->get('bgsize') : '';
507 $bgposition = is_string($box->params->get('bgposition')) ? $box->params->get('bgposition') : '';
508
509 $style['background-image'] = 'url(\'' . esc_url($box->params->get('bgimagefile')) . '\')';
510 $style['background-repeat'] = strtolower($bgrepeat);
511 $style['background-size'] = strtolower($bgsize);
512 $style['background-position'] = strtolower($bgposition);
513 }
514
515 $box->style = BoxHelper::arrayToCSSS($style);
516
517 $trigger_point_methods = [
518 'pageheight' => 'onScrollDepth',
519 'element' => 'onElementVisibility',
520 'pageready' => 'onPageReady',
521 'pageload' => 'onPageLoad',
522 'userleave' => 'onExit',
523 'onclick' => 'onClick',
524 'elementHover' => 'onHover',
525 'ondemand' => 'onDemand'
526 ];
527
528 // z index
529 $zindex = $box->params->get('zindex', 99999) != 99999 ? $box->params->get('zindex') : null;
530
531 $box->styles_container = BoxHelper::arrayToCSSS([
532 'z-index' => $zindex
533 ]);
534
535 /* Other Settings */
536 $scroll_depth = $box->params->get('scroll_depth', 'percentage');
537 $scroll_depth = !is_array($scroll_depth) ? $scroll_depth : '';
538
539 $animation_duration = $box->params->get('duration') ? (float) $box->params->get('duration') : 0;
540
541 // Use Namespaced classes for each trigger point and let them manipulate the settings dynamicaly.
542 $box->settings = [
543 'trigger' => (is_string($box->params->get('triggermethod'))) && array_key_exists($box->params->get('triggermethod'), $trigger_point_methods) ? $trigger_point_methods[$box->params->get('triggermethod')] : $box->params->get('triggermethod'),
544 'trigger_selector' => $box->params->get('triggerelement'),
545 'delay' => (int) $box->params->get('triggerdelay') * 1000,
546 'scroll_depth' => $scroll_depth,
547 'scroll_depth_value' => $scroll_depth == 'percentage' ? (int) $box->params->get('triggerpercentage') : (int) $box->params->get('scroll_pixel'),
548 'firing_frequency' => (int) $box->params->get('firing_frequency', 1),
549 'reverse_scroll_close' => (bool) $box->params->get('autohide'),
550 'threshold' => (float) $box->params->get('threshold', 0) / 100,
551 'close_out_viewport' => (bool) $box->params->get('close_out_viewport', false),
552 'exit_timer' => (int) $box->params->get('exittimer') * 1000,
553 'idle_time' => (int) $box->params->get('idle_time') * 1000,
554 'animation_open' => $box->params->get('animationin'),
555 'animation_close' => $box->params->get('animationout'),
556 'animation_duration' => (float) $animation_duration * 1000,
557 'prevent_default' => (bool) $box->params->get('preventdefault', true),
558 'backdrop' => (bool) $box->params->get('overlay'),
559 'backdrop_color' => $box->params->get('overlay_color'),
560 'backdrop_click' => (bool) $box->params->get('overlayclick'),
561 'disable_page_scroll' => (bool) $box->params->get('preventpagescroll'),
562 'test_mode' => (bool) $box->params->get('testmode'),
563 'debug' => (bool) $cParam->get('debug', false),
564 'ga_tracking' => (bool) $cParam->get('gaTrack', 0),
565 'ga_tracking_id' => $cParam->get('gaID', 0),
566 'ga_tracking_label' => $cParam->get('gaCategory'),
567 'auto_focus' => (bool) $box->params->get('autofocus', false)
568 ];
569
570 // set padding and margin for other devices as well
571 self::setResponsiveCSS($box);
572
573 self::replaceBoxSmartTags($box);
574 }
575
576 /**
577 * Sets all responsive CSS to use in box view
578 *
579 * @param object $box
580 *
581 * @return void
582 */
583 private static function setResponsiveCSS(&$box)
584 {
585 $tablet = '';
586 $mobile = '';
587
588 /**
589 * Handle width, height, padding, margin the same way.
590 */
591 $shared_properties = [
592 'width' => [
593 'prefix_only' => true
594 ],
595 'height' => [
596 'prefix_only' => true
597 ],
598 'padding' => [],
599 'margin' => []
600 ];
601
602 foreach ($shared_properties as $prop => $prop_settings)
603 {
604 $prop_data = (array) $box->params->get($prop . '_control.' . $prop, []);
605
606 $prefix_only = isset($prop_settings['prefix_only']) ? $prop_settings['prefix_only'] : false;
607
608 // assign each device CSS
609 if (isset($prop_data['tablet']))
610 {
611 $tablet .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsData($prop_data['tablet'], $prop, true, $prefix_only));
612 }
613 if (isset($prop_data['mobile']))
614 {
615 $mobile .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsData($prop_data['mobile'], $prop, true, $prefix_only));
616 }
617 }
618
619 // border radius requires a special method
620 $border_radius = (array) $box->params->get('borderradius_control.borderradius', []);
621 if (isset($border_radius['tablet']))
622 {
623 $tablet .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['tablet'], true));
624 }
625 if (isset($border_radius['mobile']))
626 {
627 $mobile .= BoxHelper::arrayToCSSS(DimensionsHelper::parseDimensionsBorderRadiusData($border_radius['mobile'], true));
628 }
629
630 $responsive_css = [
631 'tablet' => $tablet,
632 'mobile' => $mobile
633 ];
634 $box->params->set('responsive_css', $responsive_css);
635 }
636
637 /**
638 * Replaces all box smart tags
639 *
640 * @param object $box
641 *
642 * @return object
643 */
644 public static function replaceBoxSmartTags(&$box)
645 {
646 $tags = new \FPFramework\Base\SmartTags\SmartTags();
647
648 // register FB Smart Tags
649 $tags->register('\FireBox\Core\SmartTags', FBOX_BASE_FOLDER . '/Inc/Core/SmartTags', $box);
650
651 $box = $tags->replace($box);
652 }
653
654 /**
655 * Checks if a box passes assignments
656 *
657 * @param object $box
658 *
659 * @return boolean
660 */
661 public function pass($box)
662 {
663 if (!$box || !is_object($box))
664 {
665 return false;
666 }
667
668 // set box settings if empty
669 if (empty($this->boxSettings))
670 {
671 $this->boxSettings = $box;
672 }
673
674 // Prepare boxes that mirror other boxes assignments
675 if ($box->params->get('mirror', false) && $mirror_box_id = $box->params->get('mirror_box'))
676 {
677 $box->params->merge(self::getAssignmentsForMirroring($mirror_box_id));
678 }
679
680 // Check first local assignments
681 if (!$this->passLocalAssignments($box))
682 {
683 return false;
684 }
685
686 // If testmode is enabled disable the User Groups assignment
687 if ($box->params->get('testmode'))
688 {
689 $box->params->set('assignments.assign_grouplevel.selection', '0');
690 }
691
692 $globalAssignments = $this->passGlobalAssignments($box);
693
694 // Check framework based assignments
695 return $globalAssignments;
696 }
697
698 /**
699 * Passes framework based global assignments
700 *
701 * @param object $box
702 *
703 * @return boolean
704 */
705 private function passGlobalAssignments($box)
706 {
707 $assignments = new \FPFramework\Base\Assignments($this->factory);
708 $pass = $assignments->passAll($box, $box->params->get('assignmentMatchingMethod', 'and'));
709 return $pass;
710 }
711
712 /**
713 * Check if a box passes local assignments
714 *
715 * @param object $box
716 *
717 * @return boolean
718 */
719 private function passLocalAssignments($box)
720 {
721 $localAssignments = new \FireBox\Core\FB\Assignments($this, $this->factory);
722 return $localAssignments->passAll();
723 }
724
725 /**
726 * Gets assignments of mirrored box
727 *
728 * @param int $box_id
729 *
730 * @return object
731 */
732 private static function getAssignmentsForMirroring($box_id)
733 {
734 $payload = [
735 'where' => [
736 'ID' => ' = ' . intval($box_id),
737 'post_status' => " = 'publish'",
738 'post_type' => " = 'firebox'"
739 ]
740 ];
741
742 // Load box
743 if (!$box = firebox()->tables->box->getResults($payload))
744 {
745 return;
746 }
747
748 $box = $box[0];
749
750 // get meta options for box
751 $meta = get_post_meta($box_id, 'fpframework_meta_settings', true);
752 $box->params = $meta;
753
754 // To prevent user frustration, we ignore the following assignments because they are not displayed in the Publishing Assignments.
755 $params_to_ignore = [
756 'assign_impressions'
757 ];
758
759 $assignments = [];
760
761 // Gather params to merge
762 foreach ($box->params as $param_key => $param_value)
763 {
764 if (strpos($param_key, 'assign') === false || in_array($param_key, $params_to_ignore))
765 {
766 continue;
767 }
768
769 $assignments[$param_key] = $param_value;
770 }
771
772 return new Registry($assignments);
773 }
774
775 /**
776 * Prefixes the CSS classes
777 *
778 * @param array $classes
779 * @param string $prefix
780 *
781 * @return void
782 */
783 private static function prefixCSSClasses(&$classes, $prefix = 'fb-')
784 {
785 $classes = array_filter($classes);
786
787 if (empty($classes))
788 {
789 return;
790 }
791
792 foreach ($classes as &$class)
793 {
794 $class = $prefix . $class;
795 }
796 }
797
798 /**
799 * Track box open
800 *
801 * @param integer $box_id
802 * @param string $page
803 * @param string $referrer
804 *
805 * @return void
806 */
807 public function logOpenEvent($box_id, $page = null, $referrer = null)
808 {
809 $box = $this->get($box_id);
810
811 // Do not track if statistics option is disabled
812 $track_open_event = (bool) (is_null($box->params->get('stats', null)) ? BoxHelper::getParams()->get('stats', 1) : $box->params->get('stats'));
813 if (!$track_open_event)
814 {
815 return;
816 }
817
818 return firebox()->log->track($box_id, 1, null, $page, $referrer);
819 }
820
821 /**
822 * Track box close
823 *
824 * @param integer $box_id
825 * @param integer $box_log_id
826 *
827 * @return void
828 */
829 public function logCloseEvent($box_id, $box_log_id)
830 {
831 $box = $this->get($box_id);
832
833 // Do not track if statistics option is disabled
834 $track_open_event = (bool) (is_null($box->params->get('stats', null)) ? BoxHelper::getParams()->get('stats', 1) : $box->params->get('stats'));
835 if (!$track_open_event)
836 {
837 return null;
838 }
839
840 firebox()->log->track($box_id, 2, $box_log_id);
841 }
842
843 /**
844 * Get box impressions
845 *
846 * @param array $payload
847 *
848 * @return array
849 */
850 public function getImpressions($payload)
851 {
852 return firebox()->tables->boxlog->getResults($payload);
853 }
854
855 /**
856 * Get total box impressions
857 *
858 * @param array $payload
859 *
860 * @return array
861 */
862 public function getTotalImpressions($payload)
863 {
864 return count($this->getImpressions($payload));
865 }
866
867 /**
868 * Return the box settings
869 *
870 * @return object
871 */
872 public function getBoxSettings()
873 {
874 return $this->boxSettings;
875 }
876
877 /**
878 * Sets the box settings
879 *
880 * @param object $settings
881 *
882 * @return object
883 */
884 public function setBoxSettings($settings)
885 {
886 $this->boxSettings = $settings;
887 }
888
889 /**
890 * Returns the box cookie
891 *
892 * @param string $cookie
893 *
894 * @return mixed
895 */
896 public function getCookie($cookie = null)
897 {
898 $cookie = $cookie ? $cookie : (isset($this->boxSettings->ID) ? $this->boxSettings->ID : null);
899 if (!isset($cookie))
900 {
901 return;
902 }
903
904 return new Cookie($cookie);
905 }
906 }