| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\Renderer\Migrations; |
| 4 |
|
| 5 |
class BlockContentMigrator { |
| 6 |
public function migrate_parsed_block($parsed_block) { |
| 7 |
if (!is_array($parsed_block)) { |
| 8 |
return $parsed_block; |
| 9 |
} |
| 10 |
|
| 11 |
if ( |
| 12 |
!isset($parsed_block['blockName']) || |
| 13 |
$parsed_block['blockName'] !== 'tableberg/table' || |
| 14 |
!isset($parsed_block['attrs']) || |
| 15 |
!is_array($parsed_block['attrs']) |
| 16 |
) { |
| 17 |
return $parsed_block; |
| 18 |
} |
| 19 |
|
| 20 |
$migrated_attrs = $this->migrate_table_block_attrs( |
| 21 |
$parsed_block['attrs'], |
| 22 |
$parsed_block |
| 23 |
); |
| 24 |
|
| 25 |
if ($migrated_attrs === $parsed_block['attrs']) { |
| 26 |
return $parsed_block; |
| 27 |
} |
| 28 |
|
| 29 |
$parsed_block['attrs'] = $migrated_attrs; |
| 30 |
|
| 31 |
return $parsed_block; |
| 32 |
} |
| 33 |
|
| 34 |
public function register_rest_hooks() { |
| 35 |
$post_types = get_post_types(['show_in_rest' => true], 'names'); |
| 36 |
|
| 37 |
foreach ($post_types as $post_type) { |
| 38 |
add_filter( |
| 39 |
'rest_prepare_' . $post_type, |
| 40 |
[$this, 'migrate_rest_response'], |
| 41 |
10, |
| 42 |
3 |
| 43 |
); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public function migrate_rest_response($response, $post, $request) { |
| 48 |
if (!($response instanceof \WP_REST_Response)) { |
| 49 |
return $response; |
| 50 |
} |
| 51 |
|
| 52 |
if (!($request instanceof \WP_REST_Request)) { |
| 53 |
return $response; |
| 54 |
} |
| 55 |
|
| 56 |
if ('edit' !== $request->get_param('context')) { |
| 57 |
return $response; |
| 58 |
} |
| 59 |
|
| 60 |
$data = $response->get_data(); |
| 61 |
|
| 62 |
if (!is_array($data)) { |
| 63 |
return $response; |
| 64 |
} |
| 65 |
|
| 66 |
if ( |
| 67 |
!isset($data['content']) || |
| 68 |
!is_array($data['content']) || |
| 69 |
!isset($data['content']['raw']) || |
| 70 |
!is_string($data['content']['raw']) |
| 71 |
) { |
| 72 |
return $response; |
| 73 |
} |
| 74 |
|
| 75 |
$migrated_content = $this->migrate_content($data['content']['raw']); |
| 76 |
|
| 77 |
if ($migrated_content === $data['content']['raw']) { |
| 78 |
return $response; |
| 79 |
} |
| 80 |
|
| 81 |
$data['content']['raw'] = $migrated_content; |
| 82 |
$response->set_data($data); |
| 83 |
|
| 84 |
return $response; |
| 85 |
} |
| 86 |
|
| 87 |
private function migrate_content($content) { |
| 88 |
if (!is_string($content) || strpos($content, 'wp:tableberg/table') === false) { |
| 89 |
return $content; |
| 90 |
} |
| 91 |
|
| 92 |
list($migrated_blocks, $did_migrate) = $this->migrate_blocks( |
| 93 |
parse_blocks($content) |
| 94 |
); |
| 95 |
|
| 96 |
if (!$did_migrate) { |
| 97 |
return $content; |
| 98 |
} |
| 99 |
|
| 100 |
return serialize_blocks($migrated_blocks); |
| 101 |
} |
| 102 |
|
| 103 |
private function migrate_blocks($blocks) { |
| 104 |
if (!is_array($blocks)) { |
| 105 |
return [$blocks, false]; |
| 106 |
} |
| 107 |
|
| 108 |
$did_migrate = false; |
| 109 |
|
| 110 |
foreach ($blocks as $index => $block) { |
| 111 |
if (!is_array($block)) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$is_table_block = |
| 116 |
isset($block['blockName']) && |
| 117 |
$block['blockName'] === 'tableberg/table' && |
| 118 |
isset($block['attrs']) && |
| 119 |
is_array($block['attrs']); |
| 120 |
|
| 121 |
if ($is_table_block) { |
| 122 |
$migrated_attrs = $this->migrate_table_block_attrs( |
| 123 |
$block['attrs'], |
| 124 |
$block |
| 125 |
); |
| 126 |
|
| 127 |
if ($migrated_attrs !== $block['attrs']) { |
| 128 |
$blocks[$index]['attrs'] = $migrated_attrs; |
| 129 |
$did_migrate = true; |
| 130 |
} |
| 131 |
|
| 132 |
// Editor path only: materialize the v4 native-blocks tree |
| 133 |
// from v3 attrs so the editor opens real blocks. Replaces |
| 134 |
// the whole block, so skip the generic inner-blocks |
| 135 |
// recursion below. (The frontend render_block_data path |
| 136 |
// stays attrs-only — non-resaved posts keep rendering.) |
| 137 |
$v4_block = $this->migrate_table_block_to_native( |
| 138 |
$blocks[$index] |
| 139 |
); |
| 140 |
if ($v4_block !== $blocks[$index]) { |
| 141 |
$blocks[$index] = $v4_block; |
| 142 |
$did_migrate = true; |
| 143 |
continue; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if (isset($block['innerBlocks']) && is_array($block['innerBlocks'])) { |
| 148 |
list($inner_blocks, $inner_migrated) = $this->migrate_blocks( |
| 149 |
$block['innerBlocks'] |
| 150 |
); |
| 151 |
$blocks[$index]['innerBlocks'] = $inner_blocks; |
| 152 |
$did_migrate = $did_migrate || $inner_migrated; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return [$blocks, $did_migrate]; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* @param array $block Parsed tableberg/table block (attrs already at v3). |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
private function migrate_table_block_to_native($block) { |
| 164 |
$version = isset($block['attrs']['version']) && is_numeric($block['attrs']['version']) |
| 165 |
? (int) $block['attrs']['version'] |
| 166 |
: 0; |
| 167 |
|
| 168 |
if ($version !== 3) { |
| 169 |
return $block; |
| 170 |
} |
| 171 |
|
| 172 |
$migrator = new TableBlockMigratorV3ToV4(); |
| 173 |
|
| 174 |
return $migrator->migrate_block($block); |
| 175 |
} |
| 176 |
|
| 177 |
private function migrate_table_block_attrs($attrs, $block) { |
| 178 |
$table_block_migrator = new TableBlockMigrator(); |
| 179 |
|
| 180 |
return $table_block_migrator->migrate($attrs, $block); |
| 181 |
} |
| 182 |
} |
| 183 |
|