PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.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 / TableBlockMigratorV2ToV3.php

TableBlockMigratorV2ToV3.php in Tableberg – Simple Gutenberg Table Block 1.1.5, at renderer/Migrations/TableBlockMigratorV2ToV3.php

122 lines 3.1 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 TableBlockMigratorV2ToV3 {
6 public function migrate($attrs, $block = null) {
7 if (!is_array($attrs)) {
8 return $attrs;
9 }
10
11 $attrs = $this->downgrade_font_sizes($attrs);
12
13 $attrs['rows'] = $this->indexed_map_to_array(
14 isset($attrs['rows']) ? $attrs['rows'] : array()
15 );
16 $attrs['columns'] = $this->indexed_map_to_array(
17 isset($attrs['columns']) ? $attrs['columns'] : array()
18 );
19 $attrs['version'] = 3;
20
21 return $attrs;
22 }
23
24 private function indexed_map_to_array($value) {
25 if (!is_array($value)) {
26 return array();
27 }
28
29 if (array_values($value) === $value) {
30 return $value;
31 }
32
33 $result = array();
34
35 foreach ($value as $key => $entry) {
36 if (!is_numeric($key)) {
37 continue;
38 }
39
40 $index = (int) $key;
41
42 while (count($result) < $index) {
43 $result[] = null;
44 }
45
46 $result[$index] = $entry;
47 }
48
49 return $result;
50 }
51
52 private function downgrade_font_sizes($value) {
53 if (!is_array($value)) {
54 return $value;
55 }
56
57 foreach ($value as $key => $entry) {
58 if (is_array($entry)) {
59 $value[$key] = $this->downgrade_font_sizes($entry);
60 continue;
61 }
62
63 if ($this->is_font_size_key($key)) {
64 $value[$key] = $this->downgrade_font_size($entry);
65 }
66 }
67
68 return $this->downgrade_implicit_element_font_size($value);
69 }
70
71 private function downgrade_implicit_element_font_size($value) {
72 if (
73 !isset($value['name']) ||
74 !is_string($value['name']) ||
75 !array_key_exists('attributes', $value)
76 ) {
77 return $value;
78 }
79
80 $name = $value['name'];
81 if (!in_array($name, array('text', 'button', 'list', 'ribbon'), true)) {
82 return $value;
83 }
84
85 if (!is_array($value['attributes'])) {
86 $value['attributes'] = array();
87 }
88
89 $font_size_key = $name === 'ribbon' ? 'style' : 'styles';
90
91 if (!isset($value['attributes'][$font_size_key]) || !is_array($value['attributes'][$font_size_key])) {
92 $value['attributes'][$font_size_key] = array();
93 }
94
95 if (!array_key_exists('fontSize', $value['attributes'][$font_size_key])) {
96 $value['attributes'][$font_size_key]['fontSize'] = '1rem';
97 }
98
99 return $value;
100 }
101
102 private function is_font_size_key($key) {
103 return is_string($key) && strtolower(substr($key, -8)) === 'fontsize';
104 }
105
106 private function downgrade_font_size($value) {
107 if (!is_string($value)) {
108 return $value;
109 }
110
111 $font_sizes = array(
112 '0.875rem' => '0.75rem',
113 '1rem' => '0.875rem',
114 '1.38rem' => '1rem',
115 '1.75rem' => '1.38rem',
116 '2.15rem' => '1.75rem',
117 );
118
119 return isset($font_sizes[$value]) ? $font_sizes[$value] : $value;
120 }
121 }
122