Skip to content

Commit

Permalink
register and enqueue block css
Browse files Browse the repository at this point in the history
For #3
  • Loading branch information
JiveDig committed Jun 11, 2024
1 parent c273f7c commit 73b3e49
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
2 changes: 1 addition & 1 deletion blocks/mai-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"textdomain": "mai-lists",
"style": "",
"script": "",
"editorStyle": "file:./../../assets/mai-lists.min.css",
"editorStyle": "mai-lists",
"acf": {
"mode": "preview"
},
Expand Down
35 changes: 2 additions & 33 deletions blocks/mai-list/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

/**
Expand Down Expand Up @@ -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( '<link rel="stylesheet" href="%s">', esc_url( $src ) );
$block_content = $this->str_replace_first( '<li', $html . '<li', $block_content );
}

return $block_content;
}

/**
* Replace first instance of a string.
*
Expand Down
4 changes: 4 additions & 0 deletions classes/class-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ function get() {
return;
}

// Enqueue styles.
wp_enqueue_style( 'mai-lists' );

// Build atts from args.
$atts = [
'class' => 'mai-list',
];
Expand Down
16 changes: 16 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 73b3e49

Please sign in to comment.