From 73b3e49f11ea0f8665ef583035e3a5adbdd732c9 Mon Sep 17 00:00:00 2001 From: JiveDig Date: Tue, 11 Jun 2024 16:30:46 -0400 Subject: [PATCH] register and enqueue block css For https://github.com/maithemewp/mai-lists/issues/3 --- blocks/mai-list/block.json | 2 +- blocks/mai-list/block.php | 35 ++--------------------------------- classes/class-list.php | 4 ++++ includes/functions.php | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/blocks/mai-list/block.json b/blocks/mai-list/block.json index a95238b..acf31e6 100644 --- a/blocks/mai-list/block.json +++ b/blocks/mai-list/block.json @@ -7,7 +7,7 @@ "textdomain": "mai-lists", "style": "", "script": "", - "editorStyle": "file:./../../assets/mai-lists.min.css", + "editorStyle": "mai-lists", "acf": { "mode": "preview" }, diff --git a/blocks/mai-list/block.php b/blocks/mai-list/block.php index 964a997..d505b60 100644 --- a/blocks/mai-list/block.php +++ b/blocks/mai-list/block.php @@ -19,9 +19,8 @@ function __construct() { * @return void */ function hooks() { - add_action( 'acf/init', [ $this, 'register_block' ] ); - add_action( 'acf/init', [ $this, 'register_field_group' ], 12 ); // 12 to make sure clone fields are registered. - add_filter( 'render_block_acf/mai-list', [ $this, 'add_css' ], 50, 2 ); + add_action( 'acf/init', [ $this, 'register_block' ] ); + add_action( 'acf/init', [ $this, 'register_field_group' ], 12 ); // 12 to make sure clone fields are registered. } /** @@ -115,36 +114,6 @@ function render_block( $attributes, $content, $is_preview, $post_id, $block ) { echo $list->get(); } - /** - * Adds CSS on demand. - * This runs after the standard `render_block` filters - * to fix instances where `render_block` hides/removes the block. - * - * @since TBD - * - * @param string $block_content The block content. - * @param array $block The block data. - * - * @return string - */ - function add_css( $block_content, $block ) { - if ( is_admin() || ! $block_content || ! did_action( 'wp_print_styles' ) ) { - return $block_content; - } - - static $loaded = false; - - if ( ! $loaded ) { - $loaded = true; - $suffix = mai_lists_get_suffix(); - $src = MAI_LISTS_PLUGIN_URL . sprintf( 'assets/mai-lists%s.css', $suffix ) . '?ver=' . MAI_LISTS_VERSION . '.' . date( 'njYHi', filemtime( MAI_LISTS_PLUGIN_DIR . sprintf( 'assets/mai-lists%s.css', $suffix ) ) ); - $html = sprintf( '', esc_url( $src ) ); - $block_content = $this->str_replace_first( ' 'mai-list', ]; diff --git a/includes/functions.php b/includes/functions.php index 2b513f0..b672b0a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -3,6 +3,22 @@ // Exit if accessed directly. if ( ! defined( 'ABSPATH' ) ) exit; +add_action( 'init', 'mai_lists_register_styles' ); +/** + * Registers the plugin scripts and styles. + * + * @access private + * + * @since TBD + * + * @return void + */ +function mai_lists_register_styles() { + $suffix = mai_lists_get_suffix(); + + wp_register_style( 'mai-lists', MAI_LISTS_PLUGIN_URL . sprintf( 'assets/mai-lists%s.css', $suffix ), [], MAI_LISTS_VERSION . '.' . date( 'njYHi', filemtime( MAI_LISTS_PLUGIN_DIR . sprintf( 'assets/mai-lists%s.css', $suffix ) ) ) ); +} + /** * Gets an list. *