| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.1.146 |
| 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 FPFramework\Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
abstract class Block |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Whether the block is loaded from a file or not. |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
protected $file = false; |
| 27 |
|
| 28 |
/** |
| 29 |
* Block namespace. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
protected $namespace; |
| 34 |
|
| 35 |
/** |
| 36 |
* Block identifier. |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $name; |
| 41 |
|
| 42 |
/** |
| 43 |
* Front-end style. |
| 44 |
* |
| 45 |
* @var string|array |
| 46 |
*/ |
| 47 |
protected $style; |
| 48 |
|
| 49 |
/** |
| 50 |
* Front-end script. |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected $script; |
| 55 |
|
| 56 |
/** |
| 57 |
* Editor style. |
| 58 |
* |
| 59 |
* @var string|array |
| 60 |
*/ |
| 61 |
protected $editor_style; |
| 62 |
|
| 63 |
/** |
| 64 |
* Editor script. |
| 65 |
* |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
protected $editor_script; |
| 69 |
|
| 70 |
/** |
| 71 |
* Category. |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
protected $category; |
| 76 |
|
| 77 |
/** |
| 78 |
* Keywords that will be used in search results. |
| 79 |
* |
| 80 |
* @var array |
| 81 |
*/ |
| 82 |
protected $keywords; |
| 83 |
|
| 84 |
protected $new_load = false; |
| 85 |
|
| 86 |
public function __construct() |
| 87 |
{ |
| 88 |
// front-end scripts |
| 89 |
add_action('wp_enqueue_scripts', [$this, 'public_assets'], 0); |
| 90 |
|
| 91 |
// front-end and back-end scripts |
| 92 |
if (method_exists($this, 'enqueue_block_assets')) |
| 93 |
{ |
| 94 |
add_action('enqueue_block_assets', [$this, 'enqueue_block_assets'], 0); |
| 95 |
} |
| 96 |
|
| 97 |
// back-end scripts only |
| 98 |
add_action('enqueue_block_editor_assets', [$this, 'assets'], 10); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Register the block. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function register() |
| 107 |
{ |
| 108 |
if ($this->getNamespace() === 'firebox' || $this->new_load) |
| 109 |
{ |
| 110 |
$path = $this->getBlockSourceDir($this->getName()); |
| 111 |
|
| 112 |
$settings = []; |
| 113 |
|
| 114 |
if (!is_admin() && method_exists($this, 'render_callback')) |
| 115 |
{ |
| 116 |
$settings['render_callback'] = [$this, 'render_callback']; |
| 117 |
} |
| 118 |
|
| 119 |
register_block_type_from_metadata($path, $settings); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
register_block_type( |
| 124 |
$this->getNamespace() . '/' . $this->getName(), |
| 125 |
$this->getPayload() |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns the block payload. |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
private function getPayload() |
| 135 |
{ |
| 136 |
$payload = []; |
| 137 |
|
| 138 |
if ($this->style) |
| 139 |
{ |
| 140 |
$payload['style'] = $this->style; |
| 141 |
} |
| 142 |
|
| 143 |
if ($this->script) |
| 144 |
{ |
| 145 |
$payload['script'] = $this->script; |
| 146 |
} |
| 147 |
|
| 148 |
if ($this->editor_style) |
| 149 |
{ |
| 150 |
$payload['editor_style'] = $this->editor_style; |
| 151 |
} |
| 152 |
|
| 153 |
if ($this->editor_script) |
| 154 |
{ |
| 155 |
$payload['editor_script'] = $this->editor_script; |
| 156 |
} |
| 157 |
|
| 158 |
if (method_exists($this, 'getAttributes')) |
| 159 |
{ |
| 160 |
$payload['attributes'] = $this->getAttributes(); |
| 161 |
} |
| 162 |
|
| 163 |
if ($this->category) |
| 164 |
{ |
| 165 |
$payload['category'] = $this->category; |
| 166 |
} |
| 167 |
|
| 168 |
if ($this->keywords) |
| 169 |
{ |
| 170 |
$payload['keywords'] = $this->keywords; |
| 171 |
} |
| 172 |
|
| 173 |
if (!is_admin() && method_exists($this, 'render_callback')) |
| 174 |
{ |
| 175 |
$payload['render_callback'] = [$this, 'render_callback']; |
| 176 |
} |
| 177 |
|
| 178 |
return $payload; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns the block namespace. |
| 183 |
* |
| 184 |
* @return string |
| 185 |
*/ |
| 186 |
protected function getNamespace() |
| 187 |
{ |
| 188 |
return $this->namespace; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns the block name. |
| 193 |
* |
| 194 |
* @return string |
| 195 |
*/ |
| 196 |
protected function getName() |
| 197 |
{ |
| 198 |
return $this->name; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Overriden by child classes to register their public (front-end) assets. |
| 203 |
* |
| 204 |
* @return mixed |
| 205 |
*/ |
| 206 |
public function public_assets() |
| 207 |
{ |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Overriden by child classes to register their assets. |
| 213 |
* |
| 214 |
* @return mixed |
| 215 |
*/ |
| 216 |
public function assets() |
| 217 |
{ |
| 218 |
return true; |
| 219 |
} |
| 220 |
} |