* from content saved by users without the unfiltered_html capability. Saved * posts hold only the block comment and its attributes. */ final class Blocks { const BLOCKS = array( 'stand-button', 'stand-card', 'stand-chatbox' ); const EDITOR_HANDLE = 'stand-chat-editor'; const STYLE_HANDLE = 'stand-chat-blocks'; /** * Front-end printer, told when a page needs the widget. * * @var Frontend */ private $frontend; /** * Constructor. * * @param Frontend $frontend Front-end printer. */ public function __construct( Frontend $frontend ) { $this->frontend = $frontend; } /** * Hooks. */ public function register(): void { add_action( 'init', array( $this, 'register_blocks' ) ); add_filter( 'block_categories_all', array( $this, 'category' ) ); } /** * Registers the editor script, the block styles and the blocks. */ public function register_blocks(): void { $asset = Frontend::asset( 'editor' ); wp_register_script( self::EDITOR_HANDLE, STAND_CHAT_URL . 'build/editor.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( self::EDITOR_HANDLE, 'stand-chat', STAND_CHAT_DIR . 'languages' ); // Block previews (loaded into the editor iframe through block.json) and the panel. wp_register_style( self::EDITOR_HANDLE, STAND_CHAT_URL . 'build/editor.css', array(), $asset['version'] ); $style = Frontend::asset( 'blocks' ); wp_register_style( self::STYLE_HANDLE, STAND_CHAT_URL . 'build/blocks.css', array(), $style['version'] ); foreach ( self::BLOCKS as $block ) { register_block_type( STAND_CHAT_DIR . 'blocks/' . $block ); } } /** * Adds the "Stand Chat" block category. * * @param array> $categories Block categories. * @return array> */ public function category( $categories ) { $categories = is_array( $categories ) ? $categories : array(); $categories[] = array( 'slug' => 'stand-chat', 'title' => __( 'Stand Chat', 'stand-chat' ), 'icon' => null, ); return $categories; } /** * Called from each block's render.php: the page now needs the widget. */ public static function rendered(): void { $blocks = Plugin::get( self::class ); if ( $blocks instanceof self ) { $blocks->frontend->content_needs_widget(); } } /** * Wrapper attributes for a block, as an array. * * @param array $extra Extra wrapper attributes. * @return array */ public static function wrapper( array $extra = array() ): array { return Markup::parse_wrapper( get_block_wrapper_attributes( $extra ) ); } }