1. Go to this page and download the library: Download cloakwp/block-parser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cloakwp / block-parser example snippets
use CloakWP\BlockParser\BlockParser;
$postId = 123;
$blockParser = new BlockParser();
$blockData = $blockParser->parseBlocksFromPost($postId);
use CloakWP\BlockParser\BlockParser;
use CloakWP\BlockParser\Html\NativeHtmlAdapter;
use CloakWP\BlockParser\Html\PQueryHtmlAdapter;
$parser = new BlockParser(new NativeHtmlAdapter());
$legacyParser = new BlockParser(new PQueryHtmlAdapter());
add_filter('cloakwp/block_parser/html_adapter', fn() => new PQueryHtmlAdapter());
use CloakWP\BlockParser\Transformers\AbstractBlockTransformer;
// Extend the abstract base to inherit getType() and the parser constructor.
// The static $type property selects the block type this transformer handles:
class MyCustomACFBlockTransformer extends AbstractBlockTransformer
{
protected static string $type = 'acf'; // this will override the default ACFBlockTransformer
public function transform(WP_Block $block, int|null $postId = null): array
{
// your custom data transformation code here -- whatever you return here will be the final block data
}
}
// now register the transformer with your BlockParser instance:
$blockParser = new BlockParser();
$blockParser->registerTransformer(MyCustomACFBlockTransformer::class);
class MyCustomBlockParser extends BlockParser
{
protected function determineBlockType(WP_Block $block): string
{
if ($block->name === 'my-plugin/custom-block') {
return 'custom';
}
return parent::determineBlockType($block);
}
}
class MyCustomBlockTransformer extends AbstractBlockTransformer
{
protected static string $type = 'custom';
public function transform(WP_Block $block, int|null $postId = null): array
{
// ..
}
}
$postId = 123;
$blockParser = new MyCustomBlockParser();
$blockParser->registerTransformer(MyCustomBlockTransformer::class);
// Blocks named 'my-plugin/custom-block' now use MyCustomBlockTransformer.
$blockData = $blockParser->parseBlocksFromPost($postId);
add_filter('cloakwp/block/data', function (array $parsedBlock, array $fieldDefinitions, WP_Block $wpBlock, ?int $postId) {
// $fieldDefinitions is the ACF field-object tree for the block (nested `sub_fields`