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);
// when you define your Transformer class, you must implement the BlockTransformerInterface and define
// a static $type property, which indicates the block type that the transformer should be applied to:
class MyCustomACFBlockTransformer implements BlockTransformerInterface
{
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->registerBlockTransformer(MyCustomACFBlockTransformer::class);
class MyCustomBlockParser extends BlockParser
{
protected function determineBlockType(WP_Block $block): string
{
if ($block->blockName === 'my-custom-block-name') {
return 'custom';
}
return parent::determineBlockType($block);
}
}
class MyCustomBlockTransformer implements BlockTransformerInterface
{
protected static string $type = 'custom';
public function transform(WP_Block $block, int|null $postId = null): array
{
// ..
}
}
$postId = 123;
$blockParser = new MyCustomBlockParser();
$blockParser->registerBlockTransformer(MyCustomBlockTransformer::class)
// now any blocks with name 'my-custom-block-name' will be transformed by MyCustomBlockTransformer's transform() method
$blockData = $blockParser->parseBlocksFromPost($postId);