2017-11-23 11:30:57 +01:00
|
|
|
<?php
|
2017-12-20 14:20:02 +01:00
|
|
|
/**
|
2018-01-10 18:10:37 +01:00
|
|
|
* Functions to register client-side assets (scripts and stylesheets) for the
|
|
|
|
* Gutenberg block.
|
2017-12-20 14:20:02 +01:00
|
|
|
*
|
|
|
|
* @package {{namespace}}
|
|
|
|
*/
|
2017-11-23 11:30:57 +01:00
|
|
|
|
2017-12-20 14:20:02 +01:00
|
|
|
/**
|
2018-01-10 18:10:37 +01:00
|
|
|
* Registers all block assets so that they can be enqueued through Gutenberg in
|
|
|
|
* the corresponding context.
|
2018-01-10 12:45:08 +01:00
|
|
|
*
|
2019-01-29 20:15:48 +05:30
|
|
|
* @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/
|
2017-12-20 14:20:02 +01:00
|
|
|
*/
|
2018-01-10 12:45:08 +01:00
|
|
|
function {{machine_name}}_block_init() {
|
2018-04-21 23:53:37 -04:00
|
|
|
// 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}}
|
2017-11-28 10:54:02 +01:00
|
|
|
$dir = dirname( __FILE__ );
|
2018-04-10 22:49:49 +02:00
|
|
|
{{/plugin}}
|
|
|
|
{{#theme}}
|
2019-11-13 20:16:23 +01:00
|
|
|
$dir = get_stylesheet_directory() . '/blocks';
|
2018-04-10 22:49:49 +02:00
|
|
|
{{/theme}}
|
2017-12-20 14:20:02 +01:00
|
|
|
|
2018-04-17 06:10:07 -04:00
|
|
|
$index_js = '{{slug}}/index.js';
|
2018-01-10 12:45:08 +01:00
|
|
|
wp_register_script(
|
2017-12-20 14:20:02 +01:00
|
|
|
'{{slug}}-block-editor',
|
2018-04-10 22:49:49 +02:00
|
|
|
{{#plugin}}
|
2018-04-17 06:10:07 -04:00
|
|
|
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
|
|
|
[
|
2017-12-20 14:20:02 +01:00
|
|
|
'wp-blocks',
|
|
|
|
'wp-i18n',
|
|
|
|
'wp-element',
|
2021-03-25 11:03:37 +00:00
|
|
|
],
|
|
|
|
filemtime( "{$dir}/{$index_js}" )
|
2017-12-20 14:20:02 +01:00
|
|
|
);
|
|
|
|
|
2017-11-28 10:54:02 +01:00
|
|
|
$editor_css = '{{slug}}/editor.css';
|
2018-01-10 12:45:08 +01:00
|
|
|
wp_register_style(
|
2017-12-20 14:20:02 +01:00
|
|
|
'{{slug}}-block-editor',
|
2018-04-10 22:49:49 +02:00
|
|
|
{{#plugin}}
|
2017-12-20 14:20:02 +01:00
|
|
|
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}" )
|
2017-12-20 14:20:02 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$style_css = '{{slug}}/style.css';
|
2018-01-10 12:45:08 +01:00
|
|
|
wp_register_style(
|
|
|
|
'{{slug}}-block',
|
2018-04-10 22:49:49 +02:00
|
|
|
{{#plugin}}
|
2017-12-20 14:20:02 +01:00
|
|
|
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}" )
|
2017-12-20 14:20:02 +01:00
|
|
|
);
|
2018-01-10 12:45:08 +01:00
|
|
|
|
2021-03-25 11:03:37 +00:00
|
|
|
register_block_type( '{{namespace}}/{{slug}}', [
|
2018-01-10 12:45:08 +01:00
|
|
|
'editor_script' => '{{slug}}-block-editor',
|
|
|
|
'editor_style' => '{{slug}}-block-editor',
|
|
|
|
'style' => '{{slug}}-block',
|
2021-03-25 11:03:37 +00:00
|
|
|
] );
|
2017-12-20 14:20:02 +01:00
|
|
|
}
|
2021-03-25 11:03:37 +00:00
|
|
|
|
2018-01-10 12:45:08 +01:00
|
|
|
add_action( 'init', '{{machine_name}}_block_init' );
|