Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace GutenbergBlockManager;
- class Blocks
- {
- public $url;
- public $dir;
- public $namespace;
- /**
- * Set up block editor basics on instantiation
- */
- public function __construct($namespace, $url, $dir)
- {
- $this->url = $url;
- $this->namespace = $namespace;
- $this->dir = $dir;
- $this->registerEditorBlocks();
- $this->registerFrontendBlocks();
- $this->registerEditorScripts();
- }
- /**
- * Glob editor blocks
- */
- public function registerEditorBlocks()
- {
- foreach (glob($this->dir .'/resources/assets/scripts/blocks/*.js') as $blockname) {
- $this->registerEditorBlock(basename($blockname, '.js'));
- }
- }
- /**
- * Glob frontend blocks
- */
- public function registerFrontendBlocks()
- {
- foreach (glob($this->dir .'/resources/assets/scripts/frontend/*.js') as $blockname) {
- $this->registerFrontendBlock(basename($blockname, '.js'));
- }
- }
- /**
- * Register block
- */
- public function registerEditorBlock($name)
- {
- add_action('init', function () use ($name) {
- register_block_type($this->namespace .'/'. $name, array(
- 'editor_script' => $this->namespace .'-editor-js',
- 'editor_style' => $this->namespace .'-editor-css'
- ));
- });
- }
- /**
- * Enqueue block editor scripts
- */
- public function registerEditorScripts()
- {
- add_action('init', function () {
- wp_register_style(
- $this->namespace .'-editor-css',
- $this->url .'/dist/styles/main.css'
- );
- wp_register_script(
- $this->namespace .'-editor-js',
- $this->url .'/dist/scripts/main.js',
- array(
- 'wp-blocks',
- 'wp-components',
- 'wp-compose',
- 'wp-data',
- 'wp-date',
- 'wp-editor',
- 'wp-element',
- 'wp-hooks',
- 'wp-i18n',
- 'wp-plugins',
- )
- );
- });
- }
- /**
- * Enqueue public assets
- */
- public function registerFrontendBlock($name)
- {
- add_action('wp_enqueue_scripts', function () use ($name) {
- wp_enqueue_script(
- 'wp-block-'. $name .'-public-js',
- $this->url .'/dist/scripts/frontend/'. $name .'.js',
- array(
- 'wp-blocks',
- 'wp-element'
- ),
- null,
- true
- );
- wp_enqueue_style(
- 'wp-block-'. $name .'-public-css',
- $this->url .'/dist/styles/frontend/'. $name .'.css',
- ['wp-block'. $name .'-public-js']
- );
- });
- }
- }
Add Comment
Please, Sign In to add comment