我正在为 wordpress 项目构建导入器,并且必须将块映射到其他块类型。因此,我有一个抽象的基本块类,如下所示:abstract class Block{ abstract
我正在为 wordpress 项目构建导入器,并且必须将块映射到其他块类型。因此,我有一个抽象的基本块类,如下所示:
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;
}
}
小阶段块的示例实现如下所示:
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",
];
}
}
我的映射看起来像这样( migrateOldBlockName
进行一些映射并返回一个 Block
实例数组)
$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),
// ...
));
问题: 当我现在迁移 HTML(例如字符串)时 <p class="h2">EXAMPLE TEXT</p>
,它会变成 u003cp class=u0022h2u0022u003eEXAMPLE TEXTu003c/pu003e
.
似乎缺少斜线,例如 "
应该正确存储 \u0022
在数据库中,但它是 u0022
.
如果我 wp_slash($this->getBlockData())
在类中 Block
,引号会正确转换,但其余字符仍然损坏,如果我尝试保存包含单引号的字段,它会引发异常。但是,post_title 和其他核心字段在保存时会正确编码。
解析自定义字段字符串以便正确呈现它们的正确方法是什么?