PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.0.5
Tableberg – Simple Gutenberg Table Block v1.0.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / renderer / Migrations / BlockContentMigrator.php

BlockContentMigrator.php in Tableberg – Simple Gutenberg Table Block 1.0.5, at renderer/Migrations/BlockContentMigrator.php

150 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if (
116 isset($block['blockName']) &&
117 $block['blockName'] === 'tableberg/table' &&
118 isset($block['attrs']) &&
119 is_array($block['attrs'])
120 ) {
121 $migrated_attrs = $this->migrate_table_block_attrs(
122 $block['attrs'],
123 $block
124 );
125
126 if ($migrated_attrs !== $block['attrs']) {
127 $blocks[$index]['attrs'] = $migrated_attrs;
128 $did_migrate = true;
129 }
130 }
131
132 if (isset($block['innerBlocks']) && is_array($block['innerBlocks'])) {
133 list($inner_blocks, $inner_migrated) = $this->migrate_blocks(
134 $block['innerBlocks']
135 );
136 $blocks[$index]['innerBlocks'] = $inner_blocks;
137 $did_migrate = $did_migrate || $inner_migrated;
138 }
139 }
140
141 return [$blocks, $did_migrate];
142 }
143
144 private function migrate_table_block_attrs($attrs, $block) {
145 $table_block_migrator = new TableBlockMigrator();
146
147 return $table_block_migrator->migrate($attrs, $block);
148 }
149 }
150