PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.11
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.11
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 / Core / FB / Actions / ActionsBase.php

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

258 lines 5.8 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 3.1.11 Free
5 *
6 * @author FirePlugins <[email protected]>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2026 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 /**
40 * The campaign whose actions are currently being rendered. The "Run Javascript"
41 * action needs it to check the author-based custom code gate.
42 *
43 * @var int
44 */
45 protected $box_id = 0;
46
47 public function __construct($classes = null)
48 {
49 $this->actions = $classes;
50
51 // renders the Actions per box
52 add_filter('firebox/box/before_render', [$this, 'onFireBoxBeforeRender']);
53 }
54
55 /**
56 * The BeforeRender event fires before the box's layout is ready.
57 *
58 * @param object $box The box's settings object
59 *
60 * @return object
61 */
62 public function onFireBoxBeforeRender($box)
63 {
64 if (!isset($box->ID))
65 {
66 return $box;
67 }
68
69 $this->box_id = (int) $box->ID;
70
71 $js = '';
72
73 // get actions
74 $this->getActions($js);
75
76 // replace Smart Tags
77 $this->replaceSmartTags($js, $box);
78
79 // output JS
80 $this->outputActions($js, $box->ID);
81
82 return $box;
83 }
84
85 /**
86 * Replace Smart Tags in JS code.
87 *
88 * @param string $js
89 * @param object $box
90 *
91 * @return void
92 */
93 private function replaceSmartTags(&$js = '', $box = '')
94 {
95 $tags = new \FPFramework\Base\SmartTags\SmartTags();
96
97 // register FB Smart Tags
98 $tags->register('\FireBox\Core\SmartTags', FBOX_BASE_FOLDER . '/Inc/Core/SmartTags', $box);
99
100 $js = $tags->replace($js);
101 }
102
103 /**
104 * Append the box actions
105 *
106 * @param string $js
107 *
108 * @return void
109 */
110 private function getActions(&$js)
111 {
112 if (!$this->actions)
113 {
114 return;
115 }
116
117 foreach ($this->actions as $action_item)
118 {
119 if (!$actions = $action_item->get_actions())
120 {
121 continue;
122 }
123
124 foreach ($actions as $action)
125 {
126 $this->item = $action;
127
128 $action = new Registry($action);
129
130 // Make sure the action is enabled
131 if (!$action->get('enabled', true))
132 {
133 continue;
134 }
135
136 // Validate we have a valid event type
137 if (!$action->get('when'))
138 {
139 continue;
140 }
141
142 // prepare delay
143 if (isset($this->item['delay']))
144 {
145 if (!$this->item['delay'])
146 {
147 $this->item['delay'] = 0;
148 }
149
150 $this->item['delay'] *= 1000;
151 }
152
153 $data = [
154 'when' => $action->get('when'),
155 'action' => $action->get('action', $this->get_default_action())
156 ];
157
158 $js .= $this->prepare_action_output($data);
159 }
160
161 // clear action item
162 $action_item->clear();
163 }
164 }
165
166 /**
167 * Tries to generate the action given the current action payload
168 *
169 * @return string
170 */
171 protected function get_default_action()
172 {
173 if (!isset($this->item['do']))
174 {
175 return;
176 }
177
178 // Validate action does exist
179 $actionMethod = '_' . $this->item['do'];
180 if (!method_exists($this, $actionMethod))
181 {
182 return;
183 }
184
185 return $this->$actionMethod();
186 }
187
188 /**
189 * Prepares the action output
190 *
191 * @param array $action
192 *
193 * @return string
194 */
195 private function prepare_action_output($action)
196 {
197 $action = new Registry($action);
198
199 $when = $action->get('when');
200 $action_script = $action->get('action');
201 $wrap_result = $action->get('wrap_result', true);
202
203 // Wrap the code with the event listener block
204 $action_script = $wrap_result ? 'me.on("' . esc_html($when) . '", function() { ' . $action_script . ' });' : $action_script;
205
206 // Anonymise code block
207 return $this->anonymise($action_script);
208 }
209
210 /**
211 * Outputs the final box actions javascript
212 *
213 * @param string $js
214 * @param integer $box_id
215 *
216 * @return void
217 */
218 private function outputActions($js, $box_id)
219 {
220 if (empty($js))
221 {
222 return;
223 }
224
225 $js = $this->anonymise('
226 if (!FireBox) {
227 return;
228 }
229
230 FireBox.onReady(function() {
231 var me = FireBox.getInstance(' . esc_html($box_id) . ');
232
233 if (!me) {
234 return;
235 }
236
237 ' . $js . '
238 });
239 ');
240
241 if (did_action('wp_enqueue_scripts'))
242 {
243 $this->addScript($js);
244 }
245 else
246 {
247 add_action('wp_enqueue_scripts', function() use ($js) {
248 $this->addScript($js);
249 });
250 }
251 }
252
253 private function addScript($js)
254 {
255 \FireBox\Core\Helpers\BoxHelper::addInlineScript($js);
256 }
257 }
258