| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.16 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2023 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Blocks |
| 20 |
{ |
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
if (!function_exists('register_block_type')) |
| 24 |
{ |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
add_action('enqueue_block_editor_assets', [$this, 'blocks_assets']); |
| 29 |
|
| 30 |
// Register categories |
| 31 |
global $wp_version; |
| 32 |
if (version_compare($wp_version, '5.8', '>=')) |
| 33 |
{ |
| 34 |
add_action('block_categories_all', [$this, 'register_categories'], 10, 2); |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
add_action('block_categories', [$this, 'register_categories'], 10, 2); |
| 39 |
} |
| 40 |
|
| 41 |
$this->register_blocks(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Adds the `FireBox` and `FirePlugins` custom Gutenberg block categories |
| 46 |
* to register all our blocks. |
| 47 |
* |
| 48 |
* @param array $categories |
| 49 |
* @param WP_Block_Editor_Context $context |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
public function register_categories($categories, $context) |
| 54 |
{ |
| 55 |
// Add FireBox category |
| 56 |
$categories = array_merge( |
| 57 |
$categories, |
| 58 |
array( |
| 59 |
array( |
| 60 |
'slug' => 'firebox', |
| 61 |
'title' => firebox()->_('FB_PLUGIN_NAME') |
| 62 |
), |
| 63 |
) |
| 64 |
); |
| 65 |
|
| 66 |
// Add FirePlugins category |
| 67 |
$categories = array_merge( |
| 68 |
$categories, |
| 69 |
array( |
| 70 |
array( |
| 71 |
'slug' => 'fireplugins', |
| 72 |
'title' => fpframework()->_('FPF_NAME') |
| 73 |
), |
| 74 |
) |
| 75 |
); |
| 76 |
|
| 77 |
return $categories; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Initialize all blocks |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function register_blocks() |
| 86 |
{ |
| 87 |
$ds = DIRECTORY_SEPARATOR; |
| 88 |
|
| 89 |
$base_dir = implode($ds, [__DIR__, 'Blocks']); |
| 90 |
|
| 91 |
$files = array_diff(scandir($base_dir), ['.', '..', '.DS_Store', 'index.php', 'Block.php']); |
| 92 |
|
| 93 |
$namespace = '\FireBox\Core\Blocks\\'; |
| 94 |
|
| 95 |
foreach ($files as $item) |
| 96 |
{ |
| 97 |
// Check if this is a single file and load it |
| 98 |
$file = implode($ds, [$base_dir, $item]); |
| 99 |
if (is_file($file)) |
| 100 |
{ |
| 101 |
$class_name = preg_replace('/.php$/', '', $item); |
| 102 |
$this->registerBlock($namespace . $class_name); |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
// If that's not a file, we assume it is a directory of files |
| 107 |
$blocks = array_diff(scandir($file), ['.', '..', 'index.php']); |
| 108 |
|
| 109 |
foreach ($blocks as $key => $block_name) |
| 110 |
{ |
| 111 |
$class_name = preg_replace('/.php$/', '', $block_name); |
| 112 |
$class = $namespace . $item . '\\' . $class_name; |
| 113 |
$this->registerBlock($class); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Registers the block. |
| 120 |
* |
| 121 |
* @param string $class |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
private function registerBlock($class) |
| 126 |
{ |
| 127 |
$block = new $class(); |
| 128 |
$block->register(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Blocks assets. |
| 133 |
* |
| 134 |
* - Extends functionality of the button/image blocks by allowing to trigger a popup. |
| 135 |
* It also allows us to set custom CSS Classes to these blocks without triggering a popup. |
| 136 |
*/ |
| 137 |
public function blocks_assets() |
| 138 |
{ |
| 139 |
wp_enqueue_script( |
| 140 |
'fb-blocks-extend', |
| 141 |
FBOX_MEDIA_ADMIN_URL . 'js/fb_blocks_extend.js', |
| 142 |
['wp-i18n', 'wp-blocks', 'wp-editor', 'wp-components', 'wp-api-fetch'], |
| 143 |
FBOX_VERSION, |
| 144 |
true |
| 145 |
); |
| 146 |
|
| 147 |
// Enqueue block editor style only in Gutenberg editor |
| 148 |
if (function_exists('get_current_screen')) |
| 149 |
{ |
| 150 |
$screen = get_current_screen(); |
| 151 |
if ($screen->is_block_editor) |
| 152 |
{ |
| 153 |
if (get_post_type() === 'firebox') |
| 154 |
{ |
| 155 |
wp_enqueue_style( |
| 156 |
'firebox-block-editor', |
| 157 |
FBOX_MEDIA_ADMIN_URL . 'css/block-editor.css', |
| 158 |
[], |
| 159 |
FBOX_VERSION |
| 160 |
); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
} |