scaffold-command/templates/block-php.mustache

78 lines
1.8 KiB
Text
Raw Normal View History

2017-11-23 11:30:57 +01:00
<?php
/**
* Functions to register client-side assets (scripts and stylesheets) for the
* Gutenberg block.
*
* @package {{namespace}}
*/
2017-11-23 11:30:57 +01:00
/**
* Registers all block assets so that they can be enqueued through Gutenberg in
* the corresponding context.
*
2019-01-29 20:15:48 +05:30
* @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/
*/
function {{machine_name}}_block_init() {
// Skip block registration if Gutenberg is not enabled/merged.
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
2018-04-10 22:49:49 +02:00
{{#plugin}}
$dir = dirname( __FILE__ );
2018-04-10 22:49:49 +02:00
{{/plugin}}
{{#theme}}
$dir = get_stylesheet_directory() . '/blocks';
2018-04-10 22:49:49 +02:00
{{/theme}}
$index_js = '{{slug}}/index.js';
wp_register_script(
'{{slug}}-block-editor',
2018-04-10 22:49:49 +02:00
{{#plugin}}
plugins_url( $index_js, __FILE__ ),
2018-04-10 22:49:49 +02:00
{{/plugin}}
{{#theme}}
2021-03-25 11:03:37 +00:00
get_stylesheet_directory_uri() . "/blocks/{$index_js}",
2018-04-10 22:49:49 +02:00
{{/theme}}
2021-03-25 11:03:37 +00:00
[
'wp-blocks',
'wp-i18n',
'wp-element',
2021-03-25 11:03:37 +00:00
],
filemtime( "{$dir}/{$index_js}" )
);
$editor_css = '{{slug}}/editor.css';
wp_register_style(
'{{slug}}-block-editor',
2018-04-10 22:49:49 +02:00
{{#plugin}}
plugins_url( $editor_css, __FILE__ ),
2018-04-10 22:49:49 +02:00
{{/plugin}}
{{#theme}}
2021-03-25 11:03:37 +00:00
get_stylesheet_directory_uri() . "/blocks/{$editor_css}",
2018-04-10 22:49:49 +02:00
{{/theme}}
2021-03-25 11:03:37 +00:00
[],
filemtime( "{$dir}/{$editor_css}" )
);
$style_css = '{{slug}}/style.css';
wp_register_style(
'{{slug}}-block',
2018-04-10 22:49:49 +02:00
{{#plugin}}
plugins_url( $style_css, __FILE__ ),
2018-04-10 22:49:49 +02:00
{{/plugin}}
{{#theme}}
2021-03-25 11:03:37 +00:00
get_stylesheet_directory_uri() . "/blocks/{$style_css}",
2018-04-10 22:49:49 +02:00
{{/theme}}
2021-03-25 11:03:37 +00:00
[],
filemtime( "{$dir}/{$style_css}" )
);
2021-03-25 11:03:37 +00:00
register_block_type( '{{namespace}}/{{slug}}', [
'editor_script' => '{{slug}}-block-editor',
'editor_style' => '{{slug}}-block-editor',
'style' => '{{slug}}-block',
2021-03-25 11:03:37 +00:00
] );
}
2021-03-25 11:03:37 +00:00
add_action( 'init', '{{machine_name}}_block_init' );