Prefix */ // Stop Direct Access if ( ! defined( 'ABSPATH' ) ) { exit; } // include admin page require_once plugin_dir_path( __FILE__ ) . 'admin/admin.php'; /** * Blocks Final Class */ final class ATBS_BLOCKS_CLASS { public function __construct() { // define constants $this->atbs_define_constants(); // block initialization add_action( 'init', [ $this, 'atbs_blocks_init' ] ); // admin page add_action( 'activated_plugin', [ $this, 'atbs_user_admin_page' ] ); // blocks category if( version_compare( $GLOBALS['wp_version'], '5.7', '<' ) ) { add_filter( 'block_categories', [ $this, 'atbs_register_block_category' ], 10, 2 ); } else { add_filter( 'block_categories_all', [ $this, 'atbs_register_block_category' ], 10, 2 ); } // enqueue block assets add_action( 'enqueue_block_assets', [ $this, 'atbs_external_libraries' ] ); // Enqueue Inline style on render block add_filter( 'render_block', [ $this, 'generate_inline_style_on_render_block' ], 10, 2 ); } /** * Initialize the plugin */ public static function init(){ static $instance = false; if( ! $instance ) { $instance = new self(); } return $instance; } /** * Define the plugin constants */ private function atbs_define_constants() { define( 'ATBS_VERSION', '1.1.4' ); define( 'ATBS_URL', plugin_dir_url( __FILE__ ) ); define( 'ATBS_LIB_URL', ATBS_URL . 'lib/' ); } /** * Blocks Registration */ public function atbs_register_block( $name, $options = array() ) { register_block_type( __DIR__ . '/build/blocks/' . $name, $options ); } // render inline css public function atbs_render_inline_css( $handle, $css ) { wp_register_style( $handle, false ); wp_enqueue_style( $handle ); wp_add_inline_style( $handle, $css ); } /** * Blocks Initialization */ public function atbs_blocks_init() { // register single block $this->atbs_register_block( 'tabs' ); } /** * Register Inline Style */ function generate_inline_style_on_render_block($block_content, $block ) { if (isset($block['blockName']) && str_contains($block['blockName'], 'atbs/')) { if (isset($block['attrs']['blockStyle'])) { $style = $block['attrs']['blockStyle']; $handle = isset( $block['attrs']['uniqueId'] ) ? $block['attrs']['uniqueId'] : 'advanced-tabs-block'; // convert style array to string if ( is_array($style) ) { $style = implode(' ', $style); } // minify style to remove extra space $style = preg_replace( '/\s+/', ' ', $style ); wp_register_style( $handle, false ); wp_enqueue_style( $handle ); wp_add_inline_style( $handle, $style ); } } return $block_content; } /** * Register Block Category */ public function atbs_register_block_category( $categories, $post ) { return array_merge( array( array( 'slug' => 'atbs-block', 'title' => __( 'Tabs Block', 'advanced-tabs-block' ), ), ), $categories, ); } // Admin page public function atbs_user_admin_page( $plugin ) { if( plugin_basename(__FILE__) == $plugin ){ wp_redirect( admin_url( 'tools.php?page=atbs-block' ) ); die(); } } /** * Enqueue Block Assets */ public function atbs_external_libraries() { // enqueue JS if( ! is_admin() && has_block('atbs/tabs')){ wp_enqueue_script( 'atbs-tabs', ATBS_LIB_URL . 'js/tabs.js', array(), ATBS_VERSION, true ); } } } /** * Kickoff */ ATBS_BLOCKS_CLASS::init();