承接 micropackage/block-loader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

micropackage/block-loader

Composer 安装命令:

composer require micropackage/block-loader

包简介

Block Loader - automatic Gutenberg blocks from template files.

README 文档

README

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)
 * Supports Spacing: (true)
 */

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.

micropackage/block-loader 适用场景与选型建议

micropackage/block-loader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2020 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 micropackage/block-loader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 micropackage/block-loader 我们能提供哪些服务?
定制开发 / 二次开发

基于 micropackage/block-loader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 23
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 23
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2020-01-30