* @link https://www.fireplugins.com * @copyright Copyright © 2021 FirePlugins All Rights Reserved * @license GNU GPLv3 or later */ namespace FireBox\Core\Blocks; if (!defined('ABSPATH')) { exit; // Exit if accessed directly. } class FireBox { public function __construct() { if (!$this->canRun()) { return; } // set custom gutenberg block category add_filter('block_categories', [$this, 'set_custom_block_category'], 10, 2); // load gutenberg block js add_action('admin_enqueue_scripts', [$this, 'registerMedia']); // register gutenberg block type register_block_type('fireplugins/firebox', array( 'editor_script' => 'fb-gutenberg-block', )); } /** * Whether we can run * * @return boolean */ private function canRun() { if (!current_user_can('manage_options') || !current_user_can('edit_posts') || !current_user_can('edit_pages')) { return false; } global $pagenow; if (!in_array($pagenow, ['post.php', 'post-new.php', 'index.php'])) { return false; } return true; } /** * Adds custom gutenberg block category * * @param array $categories * @param object $post * * @return array */ public function set_custom_block_category($categories, $post) { // check fireplugins does not exist foreach ($categories as $cat) { if ($cat['slug'] == 'fireplugins') { return $categories; } } return array_merge( $categories, array( array( 'slug' => 'fireplugins', 'title' => fpframework()->_('FPF_NAME') ), ) ); } /** * Adds block media * * @return void */ public function registerMedia() { // load gutenberg block js wp_register_script( 'fb-gutenberg-block', FBOX_MEDIA_ADMIN_URL . 'js/fb_gutenberg_block.js', ['wp-i18n', 'wp-blocks', 'wp-editor', 'wp-components', 'wp-api-fetch'], FBOX_VERSION, false ); wp_enqueue_script( 'fb-gutenberg-block' ); // load gutenberg block button extend js wp_register_script( 'fb-admin-block-button-extend', FBOX_MEDIA_ADMIN_URL . 'js/fb_block_button_extend.js', [], FBOX_VERSION, false ); wp_enqueue_script( 'fb-admin-block-button-extend' ); } }