| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.0 Free | |
| 4 | + * @version 2.1.14 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2024 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,141 @@ | ||
| 19 | 19 | class Blocks |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - $this->init(); | |
| 23 | + if (!function_exists('register_block_type')) | |
| 24 | + { | |
| 25 | + return; | |
| 26 | + } | |
| 27 | + | |
| 28 | + add_action('enqueue_block_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(); | |
| 24 | 42 | } |
| 25 | 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 | + return $categories; | |
| 67 | + } | |
| 68 | + | |
| 26 | 69 | /** |
| 27 | 70 | * Initialize all blocks |
| 28 | 71 | * |
| 29 | 72 | * @return void |
| 30 | 73 | */ |
| 31 | - public function init() | |
| 74 | + public function register_blocks() | |
| 32 | 75 | { |
| 33 | - $blocks = \scandir(dirname(__FILE__) . '/Blocks'); | |
| 76 | + $ds = DIRECTORY_SEPARATOR; | |
| 77 | + | |
| 78 | + $base_dir = implode($ds, [__DIR__, 'Blocks']); | |
| 79 | + | |
| 80 | + $files = array_diff(scandir($base_dir), ['.', '..', '.DS_Store', 'index.php', 'FormBlock.php', 'Block.php']); | |
| 81 | + | |
| 82 | + $namespace = '\FireBox\Core\Blocks\\'; | |
| 34 | 83 | |
| 35 | - foreach ($blocks as $block_file_name) | |
| 36 | - { | |
| 37 | - if (!$ext = pathinfo($block_file_name, PATHINFO_EXTENSION)) | |
| 84 | + foreach ($files as $item) | |
| 85 | + { | |
| 86 | + // Check if this is a single file and load it | |
| 87 | + $file = implode($ds, [$base_dir, $item]); | |
| 88 | + if (is_file($file)) | |
| 38 | 89 | { |
| 90 | + $class_name = preg_replace('/.php$/', '', $item); | |
| 91 | + $this->registerBlock($namespace . $class_name); | |
| 39 | 92 | continue; |
| 40 | 93 | } |
| 94 | + | |
| 95 | + // If that's not a file, we assume it is a directory of files | |
| 96 | + $blocks = array_diff(scandir($file), ['.', '..', 'index.php']); | |
| 41 | 97 | |
| 42 | - $class = '\FireBox\Core\Blocks\\' . basename($block_file_name, '.php'); | |
| 98 | + foreach ($blocks as $key => $block_name) | |
| 99 | + { | |
| 100 | + $class_name = preg_replace('/.php$/', '', $block_name); | |
| 101 | + $class = $namespace . $item . '\\' . $class_name; | |
| 102 | + $this->registerBlock($class); | |
| 103 | + } | |
| 104 | + } | |
| 105 | + } | |
| 43 | 106 | |
| 44 | - if (!class_exists($class)) | |
| 45 | - { | |
| 46 | - continue; | |
| 47 | - } | |
| 48 | - | |
| 49 | - new $class(); | |
| 50 | - } | |
| 51 | - | |
| 107 | + /** | |
| 108 | + * Registers the block. | |
| 109 | + * | |
| 110 | + * @param string $class | |
| 111 | + * | |
| 112 | + * @return void | |
| 113 | + */ | |
| 114 | + private function registerBlock($class) | |
| 115 | + { | |
| 116 | + $block = new $class(); | |
| 117 | + $block->register(); | |
| 52 | 118 | } |
| 53 | - | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Blocks assets. | |
| 122 | + * | |
| 123 | + * - Extends functionality of the button/image blocks by allowing to trigger a popup. | |
| 124 | + * It also allows us to set custom CSS Classes to these blocks without triggering a popup. | |
| 125 | + */ | |
| 126 | + public function blocks_assets() | |
| 127 | + { | |
| 128 | + if (!is_admin()) | |
| 129 | + { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + | |
| 133 | + wp_enqueue_script( | |
| 134 | + 'fb-blocks-extend', | |
| 135 | + FBOX_MEDIA_ADMIN_URL . 'js/fb_blocks_extend.js', | |
| 136 | + ['wp-i18n', 'wp-blocks', 'wp-editor', 'wp-components', 'wp-api-fetch', 'lodash'], | |
| 137 | + FBOX_VERSION, | |
| 138 | + true | |
| 139 | + ); | |
| 140 | + | |
| 141 | + // Enqueue block editor style only in Gutenberg editor | |
| 142 | + if (function_exists('get_current_screen')) | |
| 143 | + { | |
| 144 | + $screen = get_current_screen(); | |
| 145 | + if ($screen->is_block_editor) | |
| 146 | + { | |
| 147 | + if (get_post_type() === 'firebox') | |
| 148 | + { | |
| 149 | + wp_enqueue_style( | |
| 150 | + 'firebox-block-editor', | |
| 151 | + FBOX_MEDIA_ADMIN_URL . 'css/block-editor.css', | |
| 152 | + [], | |
| 153 | + FBOX_VERSION | |
| 154 | + ); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + } | |
| 158 | + } | |
| 54 | 159 | } |