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