GithubHelp home page GithubHelp logo

micropackage / block-loader Goto Github PK

View Code? Open in Web Editor NEW
24.0 4.0 5.0 63 KB

Automatic WordPress Gutenberg block loader based on template files

License: GNU General Public License v3.0

PHP 100.00%
acf meta-box micropackage bracketspace gutenberg composer-library

block-loader's Introduction

Block Loader

BracketSpace Micropackage Latest Stable Version PHP from Packagist Total Downloads License

Micropackage logo

Requires WordPress >=5.8.0.

🧬 About Block Loader

Block Loader loads the Gutenberg Block configuration directly out of the block template file. It parses the file header comment figuring out hwo to register the Block.

Basically instead doing this:

acf_register_block_type( [
	'name'              => 'sample-block',
	'title'             => __('Sample Block'),
	'render_template'   => 'blocks/sample-block.php',
] );

You can do initialize the loader once:

Micropackage\BlockLoader\BlockLoader::init( [
	'dir' => 'blocks',
] );

And define the block config directly in the template (blocks/sample-block.php):

<?php
/**
 * Block Name: Sample Block
 */

...

Supported plugins:

  • Advanced Custom Fields
  • Meta Box

This micropackage is compatible with ACF Block Creator which can create the block templates for you while defining the Field group for your new block.

💾 Installation

composer require micropackage/block-loader

🕹 Usage

Before you can start creating blocks you need to initiate the block loader passing optional config array:

Micropackage\BlockLoader\BlockLoader::init( [
	'dir'        => 'blocks',
	'categories' => [],
	'wrap'       => '<div id="%3$s" class="%2$s">%1$s</div>', // ACF only
] );

Blocks are based on the template files located by default in blocks folder in your theme (you can change that).

There are two steps to create a block:

  1. Create block template file in the blocks folder
  2. Define custom fields for your block using ACF or Meta Box.

Block template file needs to have a comment header containing block parameters.

<?php
/**
 * Block Name: (required)
 * Description:
 * Category:
 * Icon:
 * Keywords: (comma-separated)
 * Post Types: 	(comma-separated, ACF only)
 * Mode: (ACF only)
 * Align: (ACF only)
 * Enqueue Style:
 * Enqueue Script:
 * Enqueue Assets:
 * Supports Align: (comma-separated)
 * Supports Anchor: (true|false)
 * Supports Custom Class Name: (true|false)
 * Supports Mode: (true|false, ACF only)
 * Supports Multiple: (true|false)
 * Supports Reusable: (true|false)
 */

ACF

Creating template files is enough for ACF to register blocks. After that you only need to create new fields group and set it's location to your custom block.

Meta Box

In Meta Box you need "MB Blocks" extension to work with blocks. With this plugin custom fields are defined in code. You need to use rwmb_meta_boxes filter to create metabox for your block.

Let's say you have a template called blocks/some-block.php. You need to add the fields definition like this:

add_filter( 'rwmb_meta_boxes', function( $meta_boxes ) {
	$meta_boxes[] = [
		'id'     => 'some-block',
		'type'   => 'block',
		'fields' => [
			// ...fields configuration
		],
	];

	return $meta_boxes;
} );

All block parameters will be fetched from template header comment and merged with your fields configuration.

⚙️ Configuration

All parameters are optional.

Parameter Type Description
dir (string) This is a directory within your theme where block templates are located. Relative path.
Default: 'blocks'
categories (array) Array of custom block categories passed directly to block_categories_all filter.
If only one category will be configured, it will be used as default category for all custom blocks.
Default: [] (empty array)
wrap (false|string) Wrapper to each block. If set to false, the block content will be just the template file content.
Works only for ACF due to the differences in block rendering mechanisms.
Default: '<div id="%3$s" class="%2$s">%1$s</div>'
...$block_creator_params - Additional parameters passed to ACF Block Creator

Categories definition

This is how to define the categories array.

	...
	'categories' => [
		[
			'slug'  => 'custom-cat',
			'title' => __( 'Custom Category', 'textdomain' ),
			'icon'  => 'book-alt',
		],
		...
	],
	...

Wrap template

Parameters used in internal sprintf:

  1. block content from template file, which should be wrapped
  2. block classes string
  3. unique block id

Example: '<div id="%3$s" class="%2$s">%1$s</div>'

📦 About the Micropackage project

Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.

The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.

Micropackages are maintained by BracketSpace.

📖 Changelog

See the changelog file.

📃 License

GNU General Public License (GPL) v3.0. See the LICENSE file for more information.

© Credits

The Loader engine is based on the palmiak's Timber ACF WP Blocks.

block-loader's People

Contributors

arrubakook avatar jakubmikita avatar joshuafredrickson avatar szaleq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

block-loader's Issues

Supports JSX: true?

Love this tool, super helpful. I can't seem to get the InnerBlocks to work properly in my theme and I've tried just about everything. Since I don't see it, I'm wondering if Block Loader enables adding Supports JSX in the comment heading.

Right now the element gets added in the theme file when generating from ACF. But I can't actually add any innerblocks in the post editor. Am I missing something or has this feature not been added? In the screenshot you can see that an element gets added, but nothing can be dropped into the block or created within the block.

Screen Shot 2022-03-28 at 1 26 15 PM

!

Publish latest dependency update

Hi! Sorry to bother you, but it seems like the latest addition of Micropackage\Filesystem\Filesystem slipped into the code, but not in the composer.json file (maybe a publishing issue with composer?) so that when I installed the package, it didn't install Micropackage\Filesystem\Filesystem. To fix the issue, I had to install it manually even though it seems like you added it as a dependency lately.

Here's the composer.json file of the package that got installed on my end

{
	"name": "micropackage/block-loader",
	"version": "1.0.3",
	"description": "Block Loader - automatic Gutenberg blocks from template files.",
	"license": "GPL-3.0-or-later",
	"authors": [
		{
			"name": "Jakub Mikita",
			"email": "[email protected]"
		},
    {
      "name": "Wojtek Szałkiewicz",
      "email": "[email protected]"
    }
	],
	"scripts": {
		"phpcs": "phpcs",
		"phpcbf": "phpcbf"
	},
	"require": {
		"php": ">=5.6",
		"micropackage/singleton": "^1.1",
		"micropackage/dochooks": "^1.0"
	},
	"require-dev": {
		"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
		"phpcompatibility/php-compatibility": "^9.1",
		"wp-coding-standards/wpcs": "^2.0"
	},
	"autoload": {
		"psr-4" : {
			"Micropackage\\BlockLoader\\" : "src"
		}
	}
}

compared to what I can see in the repo right now

{
	"name": "micropackage/block-loader",
	"version": "1.0.3",
	"description": "Block Loader - automatic Gutenberg blocks from template files.",
	"license": "GPL-3.0-or-later",
	"authors": [
		{
			"name": "Jakub Mikita",
			"email": "[email protected]"
		},
    {
      "name": "Wojtek Szałkiewicz",
      "email": "[email protected]"
    }
	],
	"scripts": {
		"phpcs": "phpcs",
		"phpcbf": "phpcbf"
	},
	"require": {
		"php": ">=5.6",
		"micropackage/singleton": "^1.1",
		"micropackage/dochooks": "^1.0",
		"micropackage/filesystem": "^1.1"
	},
	"require-dev": {
		"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
		"phpcompatibility/php-compatibility": "^9.1",
		"wp-coding-standards/wpcs": "^2.0"
	},
	"autoload": {
		"psr-4" : {
			"Micropackage\\BlockLoader\\" : "src"
		}
	}
}

Relative asset URI

I'd like to avoid writing manually the full base url (/wp-content/themes/mytheme/blocks/assets/js/myblock.js). Somehow I expected the path to be prefixed automatically with get_template_directory_uri(). Is it something that's already supported but I'm just doing it wrong? If not, what do you think about adding it? 🤔

Submitted by @davidwebca in #2 (comment)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.