| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.22 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 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 |
if (!isset($box->ID)) |
| 57 |
{ |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
$js = ''; |
| 62 |
|
| 63 |
// get actions |
| 64 |
$this->getActions($js); |
| 65 |
|
| 66 |
// output JS |
| 67 |
$this->outputActions($js, $box->ID); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Append the box actions |
| 72 |
* |
| 73 |
* @param string $js |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
private function getActions(&$js) |
| 78 |
{ |
| 79 |
if (!$this->actions) |
| 80 |
{ |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
foreach ($this->actions as $action_item) |
| 85 |
{ |
| 86 |
if (!$actions = $action_item->get_actions()) |
| 87 |
{ |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
foreach ($actions as $action) |
| 92 |
{ |
| 93 |
$this->item = $action; |
| 94 |
|
| 95 |
$action = new Registry($action); |
| 96 |
|
| 97 |
// Make sure the action is enabled |
| 98 |
if (!$action->get('enabled', true)) |
| 99 |
{ |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
// Validate we have a valid event type |
| 104 |
if (!$action->get('when')) |
| 105 |
{ |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
// prepare delay |
| 110 |
if (isset($this->item['delay'])) |
| 111 |
{ |
| 112 |
if (!$this->item['delay']) |
| 113 |
{ |
| 114 |
$this->item['delay'] = 0; |
| 115 |
} |
| 116 |
|
| 117 |
$this->item['delay'] *= 1000; |
| 118 |
} |
| 119 |
|
| 120 |
$data = [ |
| 121 |
'when' => $action->get('when'), |
| 122 |
'action' => $action->get('action', $this->get_default_action()) |
| 123 |
]; |
| 124 |
|
| 125 |
$js .= $this->prepare_action_output($data); |
| 126 |
} |
| 127 |
|
| 128 |
// clear action item |
| 129 |
$action_item->clear(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Tries to generate the action given the current action payload |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
protected function get_default_action() |
| 139 |
{ |
| 140 |
if (!isset($this->item['do'])) |
| 141 |
{ |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
// Validate action does exist |
| 146 |
$actionMethod = '_' . $this->item['do']; |
| 147 |
if (!method_exists($this, $actionMethod)) |
| 148 |
{ |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
return $this->$actionMethod(); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Prepares the action output |
| 157 |
* |
| 158 |
* @param array $action |
| 159 |
* |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
private function prepare_action_output($action) |
| 163 |
{ |
| 164 |
$action = new Registry($action); |
| 165 |
|
| 166 |
$when = $action->get('when'); |
| 167 |
$action_script = $action->get('action'); |
| 168 |
$wrap_result = $action->get('wrap_result', true); |
| 169 |
|
| 170 |
// Wrap the code with the event listener block |
| 171 |
$action_script = $wrap_result ? 'me.on("' . esc_html($when) . '", function() { ' . $action_script . ' });' : $action_script; |
| 172 |
|
| 173 |
// Anonymise code block |
| 174 |
return $this->anonymise($action_script); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Outputs the final box actions javascript |
| 179 |
* |
| 180 |
* @param string $js |
| 181 |
* @param integer $box_id |
| 182 |
* |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
private function outputActions($js, $box_id) |
| 186 |
{ |
| 187 |
if (empty($js)) |
| 188 |
{ |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$js = ' |
| 193 |
<!-- FireBox #' . esc_html($box_id) . ' Actions Start --> |
| 194 |
' . $this->anonymise(' |
| 195 |
if (!FireBox) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
FireBox.onReady(function() { |
| 200 |
var me = FireBox.getInstance(' . esc_html($box_id) . '); |
| 201 |
|
| 202 |
if (!me) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
' . $js . ' |
| 207 |
}); |
| 208 |
') . ' |
| 209 |
<!-- FireBox #' . esc_html($box_id) . ' Actions End --> |
| 210 |
'; |
| 211 |
|
| 212 |
add_action('wp_enqueue_scripts', function() use ($js) { |
| 213 |
wp_register_script('firebox-actions', false, ['firebox']); |
| 214 |
wp_enqueue_script('firebox-actions'); |
| 215 |
wp_add_inline_script('firebox-actions', $js); |
| 216 |
}); |
| 217 |
} |
| 218 |
} |