| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.6 Free | |
| 4 | + * @version 3.1.14 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <[email protected]> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 9 | 9 | * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace FireBox\Core; |
| @@ -19,36 +19,98 @@ | ||
| 19 | 19 | class Blocks |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - $this->init(); | |
| 23 | + add_action('enqueue_block_assets', [$this, 'blocks_assets']); | |
| 24 | + | |
| 25 | + // Register categories | |
| 26 | + add_filter('block_categories_all', [$this, 'register_categories'], 10, 2); | |
| 27 | + | |
| 28 | + $this->register_blocks(); | |
| 24 | 29 | } |
| 25 | 30 | |
| 31 | + /** | |
| 32 | + * Adds the `FireBox` and `FirePlugins` custom Gutenberg block categories | |
| 33 | + * to register all our blocks. | |
| 34 | + * | |
| 35 | + * @param array $categories | |
| 36 | + * @param WP_Block_Editor_Context $context | |
| 37 | + * | |
| 38 | + * @return array | |
| 39 | + */ | |
| 40 | + public function register_categories($categories, $context) | |
| 41 | + { | |
| 42 | + return array_merge( | |
| 43 | + $categories, | |
| 44 | + [ | |
| 45 | + [ | |
| 46 | + 'slug' => 'firebox', | |
| 47 | + 'title' => 'FireBox' | |
| 48 | + ] | |
| 49 | + ] | |
| 50 | + ); | |
| 51 | + } | |
| 52 | + | |
| 26 | 53 | /** |
| 27 | 54 | * Initialize all blocks |
| 28 | 55 | * |
| 29 | 56 | * @return void |
| 30 | 57 | */ |
| 31 | - public function init() | |
| 58 | + public function register_blocks() | |
| 32 | 59 | { |
| 33 | - $blocks = \scandir(dirname(__FILE__) . '/Blocks'); | |
| 60 | + $ds = DIRECTORY_SEPARATOR; | |
| 34 | 61 | |
| 35 | - foreach ($blocks as $block_file_name) | |
| 36 | - { | |
| 37 | - if (!$ext = pathinfo($block_file_name, PATHINFO_EXTENSION)) | |
| 62 | + $base_dir = implode($ds, [__DIR__, 'Blocks']); | |
| 63 | + | |
| 64 | + $cache_key = 'firebox_block_files'; | |
| 65 | + $files = wp_cache_get($cache_key, 'firebox'); | |
| 66 | + | |
| 67 | + if ($files === false) | |
| 68 | + { | |
| 69 | + $files = array_diff(scandir($base_dir), ['.', '..', '.DS_Store', 'index.php', 'Block.php']); | |
| 70 | + | |
| 71 | + wp_cache_set($cache_key, $files, 'firebox', HOUR_IN_SECONDS); | |
| 72 | + } | |
| 73 | + | |
| 74 | + $namespace = '\FireBox\Core\Blocks\\'; | |
| 75 | + | |
| 76 | + foreach ($files as $item) | |
| 77 | + { | |
| 78 | + // Check if this is a single file and load it | |
| 79 | + $file = implode($ds, [$base_dir, $item]); | |
| 80 | + | |
| 81 | + if (!is_file($file)) | |
| 38 | 82 | { |
| 39 | - continue; | |
| 40 | - } | |
| 83 | + continue; | |
| 84 | + } | |
| 41 | 85 | |
| 42 | - $class = '\FireBox\Core\Blocks\\' . basename($block_file_name, '.php'); | |
| 86 | + $class_name = preg_replace('/.php$/', '', $item); | |
| 87 | + | |
| 88 | + $class = $namespace . $class_name; | |
| 89 | + | |
| 90 | + $block = new $class(); | |
| 91 | + $block->register(); | |
| 92 | + } | |
| 93 | + } | |
| 43 | 94 | |
| 44 | - if (!class_exists($class)) | |
| 45 | - { | |
| 46 | - continue; | |
| 47 | - } | |
| 48 | - | |
| 49 | - new $class(); | |
| 50 | - } | |
| 51 | - | |
| 95 | + /** | |
| 96 | + * Blocks assets. | |
| 97 | + * | |
| 98 | + * - Extends functionality of the button/image blocks by allowing to trigger a popup. | |
| 99 | + * It also allows us to set custom CSS Classes to these blocks without triggering a popup. | |
| 100 | + */ | |
| 101 | + public function blocks_assets() | |
| 102 | + { | |
| 103 | + if (!is_admin()) | |
| 104 | + { | |
| 105 | + return; | |
| 106 | + } | |
| 107 | + | |
| 108 | + wp_enqueue_script( | |
| 109 | + 'fb-blocks-extend', | |
| 110 | + FBOX_MEDIA_ADMIN_URL . 'js/blocks/fb_blocks_extend.js', | |
| 111 | + ['wp-i18n', 'wp-blocks', 'wp-editor', 'wp-components', 'wp-api-fetch', 'lodash'], | |
| 112 | + FBOX_VERSION, | |
| 113 | + true | |
| 114 | + ); | |
| 52 | 115 | } |
| 53 | - | |
| 54 | 116 | } |