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. * 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; border: 1px dotted #f00;
} }

View File

@ -4,7 +4,7 @@
* Replace them with your own styles or remove the file completely. * 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; background-color: #666;
color: #fff; color: #fff;
padding: 2px; padding: 2px;

33
svg.php
View File

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