Download the PHP package cloakwp/block-parser without Composer
On this page you can find all versions of the php package cloakwp/block-parser. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cloakwp/block-parser
More information about cloakwp/block-parser
Files in cloakwp/block-parser
Package block-parser
Short Description Parse Gutenberg + ACF Blocks into structured objects.
License LGPL-3.0-only
Informations about the package block-parser
CloakWP Block Parser
CloakWP Block Parser is a PHP library designed to parse and transform Gutenberg and ACF blocks into structured objects/JSON. This package is part of the CloakWP ecosystem, but can be used independently or as a dependency of your own plugins/themes/packages.
Features
- Parse Gutenberg (core) blocks into objects/JSON
- Parse Advanced Custom Fields (ACF) blocks into objects/JSON
- Extensible architecture for custom block transformers
- Filters for modifying parsed block data
Motivation
WordPress block content is stored as HTML strings in the database rather than as structured data (e.g. JSON). This is particularly a problem for decoupled/headless projects where you may wish to render things your own way; to do so, you need the blocks in JSON/structured object form -- turns out this is surprisingly difficult to achieve. CloakWP Block Parser simplifies this process by providing a clean and organized way to parse and transform blocks content into structured data.
Installation
You can install this package via Composer:
Example Output
Structured data
Usage
Basic Usage
Extending
Custom Transformers
The BlockParser uses the built-in core function, `parse_blocks()`, to initially parse the blocks, but unfortunately this function doesn't do the full job. So, we extend the basic built-in parsing with block "transformers". By default, the BlockParser uses the following transformers: - CoreBlockTransformer (for Gutenberg core blocks) - ACFBlockTransformer (for ACF blocks) You can extend the BlockParser by registering your own custom block transformers for certain block types, or to override the default transformers: If in the above example you want to add a transformer for some custom block type, you just specify a custom value for the static `$type` property, and then extend the `BlockParser` class and override the `determineBlockType()` method to add your logic for determining when a block is of your custom type; for example:Filter Hooks
Besides creating custom transformers, you can also modify parsed block data using filters. These filters are applied after the block has been transformed by the appropriate transformer, but before the block is returned: The `cloakwp/block` filter accepts two modifiers, `name` and `type`, for more granular targeting: You can also filter ACF field values within ACF blocks using the `cloakwp/block/field` filter: The `cloakwp/block/field` filter accepts three modifiers, `name` (i.e. ACF field name), `type` (i.e. ACF field type), and `blockName` (i.e. ACF block name), for more granular targeting:All versions of block-parser with dependencies
cloakwp/hook-modifiers Version *
composer/installers Version ^2.2
franzl/studio Version *
tburry/pquery Version ^1.1