I am building an importer for a wordpress project and have to map blocks to other block types. Therefore, I have an abstract base Block-Class that looks like this:
abstract class Block
{
abstract public function getBlockName(): string;
abstract public function getBlockData(): array;
public function getBlockAnchor(): ?string
{
return null;
}
public function getBlockMode(): string
{
return 'edit';
}
public function toArray(): array
{
$data = [
'blockName' => $this->getBlockName(),
'innerBlocks' => [],
'innerHTML' => '',
'innerContent' => [],
'attrs' => [
'id' => $this->getBlockAnchor(),
'name' => $this->getBlockName(),
'align' => '',
'className' => '',
'mode' => $this->getBlockMode(),
'data' => $this->getBlockData()
],
];
$data['attrs'] = array_filter($data['attrs'], fn($el) => !is_null($el));
return $data;
}
}
An example implementation of a small stage block looks like this:
class StageSmall extends Block {
public function __construct(
private string $headline = "",
private string $copy = "",
) {}
public function getBlockName() : string {
return 'acf/stage-small';
}
public function getBlockData() : array {
return [
"stage_small_title_headline" => $this->headline,
"_stage_small_title_headline" => "field_69bk817sdgsee3r2ll-Headline",
"stage_small_copy" => $this->copy,
"_stage_small_copy" => "field_667bd92e8feb6",
];
}
}
And my mapping looks something like this (migrateOldBlockName
does some mapping and returns an array of Block
instances)
$migratedBlocks = [];
foreach($blocks as $block) {
switch ($block['blockName']) {
// ...
case 'acf/old-block-name':
$migratedBlocks = [...$migratedBlocks, ...$this->migrateOldBlockName($block)];
break;
// ...
}
}
$migratedBlocks = array_map(fn($block) => $block->toArray(), $migratedBlocks);
wp_insert_post(array(
// ...
'post_content' => serialize_blocks($migratedBlocks),
// ...
));
When I now migrate HTML, for example the string <p class="h2">EXAMPLE TEXT</p>
, it turns into u003cp class=u0022h2u0022u003eEXAMPLE TEXTu003c/pu003e
. It seems like there are missing slashes, for example "
should correctly be stored as u0022
in the DB, but it is u0022
. If I use wp_slash($this->getBlockData())
in the Block
class, the quotes are correctly transformed, but the remaining characters are still broken and it throws an exception if I try save fields that contain single quotes.
What is the correct way to parse the block data strings so that they get rendered in the correct format?