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.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 / Actions / ActionsBase.php

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

213 lines 4.9 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.13 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
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 FireBox\Core\FB\Actions;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 use FPFramework\Libs\Registry;
20
21 class ActionsBase
22 {
23 use ActionsTrait;
24
25 /**
26 * All extra actions that will run on the page.
27 *
28 * @var array
29 */
30 protected $actions;
31
32 /**
33 * Currently manipulating action item
34 *
35 * @var object
36 */
37 protected $item;
38
39 public function __construct($classes = null)
40 {
41 $this->actions = $classes;
42
43 // renders the Actions per box
44 add_action('firebox/box/before_render', [$this, 'onFireBoxBeforeRender']);
45 }
46
47 /**
48 * The BeforeRender event fires before the box's layout is ready.
49 *
50 * @param object $box The box's settings object
51 *
52 * @return void
53 */
54 public function onFireBoxBeforeRender($box)
55 {
56 $js = '';
57
58 // get actions
59 $this->getActions($js);
60
61 // output JS
62 $this->outputActions($js, $box->ID);
63 }
64
65 /**
66 * Append the box actions
67 *
68 * @param string $js
69 *
70 * @return void
71 */
72 private function getActions(&$js)
73 {
74 if (!$this->actions)
75 {
76 return;
77 }
78
79 foreach ($this->actions as $action_item)
80 {
81 if (!$actions = $action_item->get_actions())
82 {
83 continue;
84 }
85
86 foreach ($actions as $action)
87 {
88 $this->item = $action;
89
90 $action = new Registry($action);
91
92 // Make sure the action is enabled
93 if (!$action->get('enabled', true))
94 {
95 continue;
96 }
97
98 // Validate we have a valid event type
99 if (!$action->get('when'))
100 {
101 continue;
102 }
103
104 // prepare delay
105 if (isset($this->item['delay']))
106 {
107 $this->item['delay'] *= 1000;
108 }
109
110 $data = [
111 'when' => $action->get('when'),
112 'action' => $action->get('action', $this->get_default_action())
113 ];
114
115 $js .= $this->prepare_action_output($data);
116 }
117
118 // clear action item
119 $action_item->clear();
120 }
121 }
122
123 /**
124 * Tries to generate the action given the current action payload
125 *
126 * @return string
127 */
128 protected function get_default_action()
129 {
130 if (!isset($this->item['do']))
131 {
132 return;
133 }
134
135 // Validate action does exist
136 $actionMethod = '_' . $this->item['do'];
137 if (!method_exists($this, $actionMethod))
138 {
139 return;
140 }
141
142 return $this->$actionMethod();
143 }
144
145 /**
146 * Prepares the action output
147 *
148 * @param array $action
149 *
150 * @return string
151 */
152 private function prepare_action_output($action)
153 {
154 $action = new Registry($action);
155
156 $when = $action->get('when');
157 $action_script = $action->get('action');
158 $wrap_result = $action->get('wrap_result', true);
159
160 // Wrap the code with the event listener block
161 $action_script = $wrap_result ? 'me.on("' . esc_html($when) . '", function() { ' . $action_script . ' });' : $action_script;
162
163 // Anonymise code block
164 return $this->anonymise($action_script);
165 }
166
167 /**
168 * Outputs the final box actions javascript
169 *
170 * @param string $js
171 * @param integer $box_id
172 *
173 * @return void
174 */
175 private function outputActions($js, $box_id)
176 {
177 if (empty($js))
178 {
179 return;
180 }
181
182 $js = '
183 <!-- FireBox #' . esc_html($box_id) . ' Actions Start -->
184 ' . $this->anonymise('
185 if (!FireBox) {
186 return;
187 }
188
189 FireBox.onReady(function() {
190 var me = FireBox.getInstance(' . esc_html($box_id) . ');
191
192 if (!me) {
193 return;
194 }
195
196 ' . $js . '
197 });
198 ') . '
199 <!-- FireBox #' . esc_html($box_id) . ' Actions End -->
200 ';
201
202 if (!did_action('wp_head'))
203 {
204 add_action('wp_head', function() use ($js) {
205 echo '<script type="text/javascript">' . $js . '</script>';
206 });
207 }
208 else
209 {
210 echo '<script type="text/javascript">' . $js . '</script>';
211 }
212 }
213 }