dynamically use theme and block slugs

This commit is contained in:
Ray Elliott 2020-08-12 19:29:39 +01:00
parent 91ebae4cf5
commit 5494287999
3 changed files with 20 additions and 17 deletions

View File

@ -4,6 +4,6 @@
* Replace them with your own styles or remove the file completely.
*/
.wp-block-room-vw-svg {
.wp-block-<theme-slug>-<block-slug> { // FIXME use correct class name
border: 1px dotted #f00;
}

View File

@ -4,7 +4,7 @@
* Replace them with your own styles or remove the file completely.
*/
.wp-block-room-vw-svg {
.wp-block-<theme-slug>-<block-slug> { // FIXME use correct class name
background-color: #666;
color: #fff;
padding: 2px;

33
svg.php
View File

@ -1,18 +1,22 @@
<?php
function svg_block_init() {
$block_slug = basename( __FILE__, '.php' );
$theme_slug = get_option( 'stylesheet' );
add_action( 'init', function() {
global $block_slug, $theme_slug;
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
// remember - for a block defined in a plugin, we will not be using
// get_template_directory(), etc, it will be the equivalent for plugins.
$dir = get_template_directory() . '/blocks/svg';
$uri = get_template_directory_uri() . "/blocks/svg";
$asset_file = include( get_template_directory() . '/blocks/svg/build/index.asset.php');
$dir = get_template_directory() . "/blocks/$block_slug";
$uri = get_template_directory_uri() . "/blocks/$block_slug";
$asset_file = include( get_template_directory() . "/blocks/$block_slug/build/index.asset.php");
$index_js = 'build/index.js';
wp_register_script(
'svg-block-editor',
"{$block_slug}-block-editor",
"$uri/$index_js",
$asset_file['dependencies'],
$asset_file['version']
@ -20,7 +24,7 @@ function svg_block_init() {
$editor_css = 'build/index.css';
wp_register_style(
'svg-block-editor',
"{$block_slug}-block-editor",
"$uri/$editor_css",
array(),
filemtime( "$dir/$editor_css" )
@ -28,16 +32,15 @@ function svg_block_init() {
$style_css = 'build/style-index.css';
wp_register_style(
'svg-block',
"{$block_slug}-block",
"$uri/$style_css",
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( 'room-vw/svg', array(
'editor_script' => 'svg-block-editor',
'editor_style' => 'svg-block-editor',
'style' => 'svg-block',
register_block_type( "$theme_slug/$block_slug", array(
'editor_script' => "{$block_slug}-block-editor",
'editor_style' => "{$block_slug}-block-editor",
'style' => "{$block_slug}-block",
) );
}
add_action( 'init', 'svg_block_init' );
} );