wp-gutenberg-block-template/index.php

48 lines
1.2 KiB
PHP

<?php
$block_slug = basename( dirname( __FILE__ ) );
// TODO calculate this correctly whether this file is within a theme or plugin
$block_namespace = get_option( 'stylesheet' );
add_action( 'init', function() {
global $block_slug, $block_namespace;
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
$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(
"{$block_slug}-block-editor",
"$uri/$index_js",
$asset_file['dependencies'],
$asset_file['version']
);
$editor_css = 'build/index.css';
wp_register_style(
"{$block_slug}-block-editor",
"$uri/$editor_css",
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'build/style-index.css';
wp_register_style(
"{$block_slug}-block",
"$uri/$style_css",
array(),
filemtime( "$dir/$style_css" )
);
register_block_type( "$block_namespace/$block_slug", array(
'editor_script' => "{$block_slug}-block-editor",
'editor_style' => "{$block_slug}-block-editor",
'style' => "{$block_slug}-block",
) );
} );