| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\Renderer\Migrations; |
| 4 |
|
| 5 |
use Tableberg\Renderer\Table\Defaults; |
| 6 |
|
| 7 |
class TableBlockMigratorV1ToV2 { |
| 8 |
public function migrate($attrs, $block = null) { |
| 9 |
if (!is_array($attrs)) { |
| 10 |
return $attrs; |
| 11 |
} |
| 12 |
|
| 13 |
$defaults = Defaults::get_defaults(); |
| 14 |
$v1_defaults = $this->get_v1_defaults(); |
| 15 |
|
| 16 |
$structure = $this->get_array($attrs, 'structure', $v1_defaults['structure']); |
| 17 |
$data = $this->get_array($attrs, 'data', $v1_defaults['data']); |
| 18 |
$table_config = $this->get_array($attrs, 'tableConfig', $v1_defaults['tableConfig']); |
| 19 |
$cell_styles = $this->get_array($attrs, 'cellStyles', $v1_defaults['cellStyles']); |
| 20 |
$bindings = $this->get_array($attrs, 'bindings', array()); |
| 21 |
$binding_registry = array(); |
| 22 |
$binding_ids_by_signature = array(); |
| 23 |
$next_binding_index = 1; |
| 24 |
|
| 25 |
foreach ($bindings as $binding_id => $binding_definition) { |
| 26 |
if (!is_string($binding_id) || !is_array($binding_definition)) { |
| 27 |
continue; |
| 28 |
} |
| 29 |
|
| 30 |
$normalized_binding = $this->normalize_binding_definition($binding_definition); |
| 31 |
$binding_registry[$binding_id] = $normalized_binding; |
| 32 |
$binding_ids_by_signature[$this->get_binding_signature($normalized_binding)] = $binding_id; |
| 33 |
|
| 34 |
if (preg_match('/_(\d+)$/', $binding_id, $matches)) { |
| 35 |
$next_binding_index = max($next_binding_index, ((int) $matches[1]) + 1); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
$table = array_merge($defaults['table'], $table_config); |
| 40 |
$table['cellSpacing'] = array_merge( |
| 41 |
$defaults['table']['cellSpacing'], |
| 42 |
$this->get_array($table_config, 'cellSpacing', array()) |
| 43 |
); |
| 44 |
$table['tableBorder'] = array_merge( |
| 45 |
$defaults['table']['tableBorder'], |
| 46 |
$this->get_array($table_config, 'tableBorder', array()) |
| 47 |
); |
| 48 |
$table['pagination'] = array_merge( |
| 49 |
$defaults['table']['pagination'], |
| 50 |
$this->get_array($table_config, 'pagination', array()) |
| 51 |
); |
| 52 |
$table['search'] = array_merge( |
| 53 |
$defaults['table']['search'], |
| 54 |
$this->get_array($table_config, 'search', array()) |
| 55 |
); |
| 56 |
$table['responsive'] = array( |
| 57 |
'tablet' => array_merge( |
| 58 |
$defaults['table']['responsive']['tablet'], |
| 59 |
$this->get_array( |
| 60 |
$this->get_array($table_config, 'responsive', array()), |
| 61 |
'tablet', |
| 62 |
array() |
| 63 |
) |
| 64 |
), |
| 65 |
'mobile' => array_merge( |
| 66 |
$defaults['table']['responsive']['mobile'], |
| 67 |
$this->get_array( |
| 68 |
$this->get_array($table_config, 'responsive', array()), |
| 69 |
'mobile', |
| 70 |
array() |
| 71 |
) |
| 72 |
), |
| 73 |
); |
| 74 |
$table['rows'] = (int) $this->get_scalar($structure, 'rows', $defaults['table']['rows']); |
| 75 |
$table['cols'] = (int) $this->get_scalar($structure, 'cols', $defaults['table']['cols']); |
| 76 |
|
| 77 |
$default_cell_styles = $defaults['cellDefaults']['styles']; |
| 78 |
$cell_style_defaults = $cell_styles; |
| 79 |
unset($cell_style_defaults['cells']); |
| 80 |
|
| 81 |
$migrated_cell_defaults = array_merge($default_cell_styles, $cell_style_defaults); |
| 82 |
$migrated_cell_defaults['padding'] = array_merge( |
| 83 |
$default_cell_styles['padding'], |
| 84 |
$this->get_array($cell_styles, 'padding', array()) |
| 85 |
); |
| 86 |
$migrated_cell_defaults['border'] = array_merge( |
| 87 |
$default_cell_styles['border'], |
| 88 |
$this->get_array($cell_styles, 'border', array()) |
| 89 |
); |
| 90 |
$migrated_cell_defaults['borderRadius'] = array_merge( |
| 91 |
$default_cell_styles['borderRadius'], |
| 92 |
$this->get_array($cell_styles, 'borderRadius', array()) |
| 93 |
); |
| 94 |
|
| 95 |
$cells = array(); |
| 96 |
|
| 97 |
foreach ($this->get_array($structure, 'cells', array()) as $cell_entry) { |
| 98 |
if (!is_array($cell_entry) || count($cell_entry) < 2 || !is_array($cell_entry[0])) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
$key = $this->coords_to_key($cell_entry[0]); |
| 103 |
$cells[$key] = isset($cells[$key]) ? $cells[$key] : array(); |
| 104 |
$cells[$key]['span'] = is_array($cell_entry[1]) ? $cell_entry[1] : array(); |
| 105 |
} |
| 106 |
|
| 107 |
foreach ($this->get_array($data, 'cells', array()) as $cell_entry) { |
| 108 |
if (!is_array($cell_entry) || count($cell_entry) < 2 || !is_array($cell_entry[0])) { |
| 109 |
continue; |
| 110 |
} |
| 111 |
|
| 112 |
$key = $this->coords_to_key($cell_entry[0]); |
| 113 |
$cells[$key] = isset($cells[$key]) ? $cells[$key] : array(); |
| 114 |
$cells[$key]['elements'] = $this->migrate_elements( |
| 115 |
is_array($cell_entry[1]) ? $cell_entry[1] : array(), |
| 116 |
$binding_registry, |
| 117 |
$binding_ids_by_signature, |
| 118 |
$next_binding_index |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
foreach ($this->get_array($cell_styles, 'cells', array()) as $cell_entry) { |
| 123 |
if (!is_array($cell_entry) || count($cell_entry) < 2 || !is_array($cell_entry[0])) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$key = $this->coords_to_key($cell_entry[0]); |
| 128 |
$cells[$key] = isset($cells[$key]) ? $cells[$key] : array(); |
| 129 |
|
| 130 |
$style_overrides = is_array($cell_entry[1]) ? $cell_entry[1] : array(); |
| 131 |
|
| 132 |
if (array_key_exists('ribbon', $style_overrides)) { |
| 133 |
$cells[$key]['ribbon'] = $style_overrides['ribbon']; |
| 134 |
unset($style_overrides['ribbon']); |
| 135 |
} |
| 136 |
|
| 137 |
if (!empty($style_overrides)) { |
| 138 |
$cells[$key]['styles'] = $style_overrides; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return array( |
| 143 |
'version' => 2, |
| 144 |
'isExample' => isset($attrs['isExample']) ? (bool) $attrs['isExample'] : $defaults['isExample'], |
| 145 |
'table' => $table, |
| 146 |
'rows' => $this->get_array($structure, 'rowConfigs', array()), |
| 147 |
'columns' => $this->get_array($structure, 'columns', array()), |
| 148 |
'cells' => $cells, |
| 149 |
'bindings' => $binding_registry, |
| 150 |
'cellDefaults' => array( |
| 151 |
'styles' => $migrated_cell_defaults, |
| 152 |
), |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
private function migrate_elements( |
| 157 |
$elements, |
| 158 |
&$binding_registry, |
| 159 |
&$binding_ids_by_signature, |
| 160 |
&$next_binding_index |
| 161 |
) { |
| 162 |
if (!is_array($elements)) { |
| 163 |
return array(); |
| 164 |
} |
| 165 |
|
| 166 |
$migrated_elements = array(); |
| 167 |
|
| 168 |
foreach ($elements as $element) { |
| 169 |
if (!is_array($element)) { |
| 170 |
$migrated_elements[] = $element; |
| 171 |
continue; |
| 172 |
} |
| 173 |
|
| 174 |
$element_bindings = isset($element['bindings']) && is_array($element['bindings']) |
| 175 |
? $element['bindings'] |
| 176 |
: null; |
| 177 |
|
| 178 |
if ($element_bindings === null) { |
| 179 |
$migrated_elements[] = $element; |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
$migrated_bindings = array(); |
| 184 |
|
| 185 |
foreach ($element_bindings as $path => $binding) { |
| 186 |
if (!is_string($path)) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
|
| 190 |
if (is_string($binding)) { |
| 191 |
$migrated_bindings[$path] = $binding; |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
if (!is_array($binding)) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
$normalized_binding = $this->normalize_binding_definition($binding); |
| 200 |
$signature = $this->get_binding_signature($normalized_binding); |
| 201 |
|
| 202 |
if (isset($binding_ids_by_signature[$signature])) { |
| 203 |
$binding_id = $binding_ids_by_signature[$signature]; |
| 204 |
} else { |
| 205 |
$binding_id = 'binding_' . $next_binding_index; |
| 206 |
$next_binding_index++; |
| 207 |
|
| 208 |
$binding_registry[$binding_id] = $normalized_binding; |
| 209 |
$binding_ids_by_signature[$signature] = $binding_id; |
| 210 |
} |
| 211 |
|
| 212 |
$migrated_bindings[$path] = $binding_id; |
| 213 |
} |
| 214 |
|
| 215 |
if (count($migrated_bindings) > 0) { |
| 216 |
$element['bindings'] = $migrated_bindings; |
| 217 |
} else { |
| 218 |
unset($element['bindings']); |
| 219 |
} |
| 220 |
|
| 221 |
$migrated_elements[] = $element; |
| 222 |
} |
| 223 |
|
| 224 |
return $migrated_elements; |
| 225 |
} |
| 226 |
|
| 227 |
private function normalize_binding_definition($binding_definition) { |
| 228 |
$normalized = array( |
| 229 |
'key' => isset($binding_definition['key']) && is_string($binding_definition['key']) |
| 230 |
? $binding_definition['key'] |
| 231 |
: '', |
| 232 |
); |
| 233 |
|
| 234 |
if (array_key_exists('postId', $binding_definition) && is_numeric($binding_definition['postId'])) { |
| 235 |
$normalized['postId'] = (int) $binding_definition['postId']; |
| 236 |
} |
| 237 |
|
| 238 |
if (array_key_exists('fallback', $binding_definition) && is_string($binding_definition['fallback'])) { |
| 239 |
$normalized['fallback'] = $binding_definition['fallback']; |
| 240 |
} |
| 241 |
|
| 242 |
return $normalized; |
| 243 |
} |
| 244 |
|
| 245 |
private function get_binding_signature($binding_definition) { |
| 246 |
return wp_json_encode($binding_definition); |
| 247 |
} |
| 248 |
|
| 249 |
private function coords_to_key($coords) { |
| 250 |
$row = isset($coords[0]) ? (int) $coords[0] : 0; |
| 251 |
$col = isset($coords[1]) ? (int) $coords[1] : 0; |
| 252 |
|
| 253 |
return $row . ',' . $col; |
| 254 |
} |
| 255 |
|
| 256 |
private function get_array($array, $key, $default) { |
| 257 |
if (!is_array($array) || !isset($array[$key]) || !is_array($array[$key])) { |
| 258 |
return $default; |
| 259 |
} |
| 260 |
|
| 261 |
return $array[$key]; |
| 262 |
} |
| 263 |
|
| 264 |
private function get_scalar($array, $key, $default) { |
| 265 |
if (!is_array($array) || !array_key_exists($key, $array)) { |
| 266 |
return $default; |
| 267 |
} |
| 268 |
|
| 269 |
return $array[$key]; |
| 270 |
} |
| 271 |
|
| 272 |
private function get_v1_defaults() { |
| 273 |
return array( |
| 274 |
'structure' => array( |
| 275 |
'rows' => 0, |
| 276 |
'cols' => 0, |
| 277 |
'cells' => array(), |
| 278 |
'columns' => array(), |
| 279 |
'rowConfigs' => array(), |
| 280 |
), |
| 281 |
'data' => array( |
| 282 |
'cells' => array(), |
| 283 |
), |
| 284 |
'tableConfig' => array( |
| 285 |
'headerEnabled' => false, |
| 286 |
'footerEnabled' => false, |
| 287 |
'stickyHeader' => false, |
| 288 |
'caption' => '', |
| 289 |
'tableWidth' => 'auto', |
| 290 |
'tableAlignment' => 'left', |
| 291 |
'cellSpacing' => array( |
| 292 |
'horizontal' => '0', |
| 293 |
'vertical' => '0', |
| 294 |
), |
| 295 |
'tableBorder' => array( |
| 296 |
'top' => '', |
| 297 |
'right' => '', |
| 298 |
'bottom' => '', |
| 299 |
'left' => '', |
| 300 |
), |
| 301 |
'fixedColumnWidths' => true, |
| 302 |
'pagination' => array( |
| 303 |
'enabled' => false, |
| 304 |
'pageSize' => 10, |
| 305 |
'showPageNumbers' => true, |
| 306 |
'showPrevNext' => true, |
| 307 |
), |
| 308 |
'search' => array( |
| 309 |
'enabled' => false, |
| 310 |
'placeholder' => 'Search...', |
| 311 |
'highlightColor' => '', |
| 312 |
'position' => 'left', |
| 313 |
), |
| 314 |
'responsive' => array( |
| 315 |
'tablet' => array( |
| 316 |
'enabled' => false, |
| 317 |
'maxWidth' => 1024, |
| 318 |
'mode' => 'scroll', |
| 319 |
'transpose' => false, |
| 320 |
'stackCount' => 3, |
| 321 |
'repeatFirstCol' => false, |
| 322 |
), |
| 323 |
'mobile' => array( |
| 324 |
'enabled' => false, |
| 325 |
'maxWidth' => 700, |
| 326 |
'mode' => 'scroll', |
| 327 |
'transpose' => false, |
| 328 |
'stackCount' => 1, |
| 329 |
'repeatFirstCol' => false, |
| 330 |
), |
| 331 |
), |
| 332 |
), |
| 333 |
'cellStyles' => array( |
| 334 |
'cells' => array(), |
| 335 |
'padding' => array( |
| 336 |
'top' => 'var(--wp--preset--spacing--20)', |
| 337 |
'right' => 'var(--wp--preset--spacing--20)', |
| 338 |
'bottom' => 'var(--wp--preset--spacing--20)', |
| 339 |
'left' => 'var(--wp--preset--spacing--20)', |
| 340 |
), |
| 341 |
'border' => array( |
| 342 |
'top' => '1px solid black', |
| 343 |
'right' => '1px solid black', |
| 344 |
'bottom' => '1px solid black', |
| 345 |
'left' => '1px solid black', |
| 346 |
), |
| 347 |
'borderRadius' => array( |
| 348 |
'topLeft' => '0px', |
| 349 |
'topRight' => '0px', |
| 350 |
'bottomRight' => '0px', |
| 351 |
'bottomLeft' => '0px', |
| 352 |
), |
| 353 |
'orientation' => 'vertical', |
| 354 |
'elementGap' => 'var(--wp--preset--spacing--20)', |
| 355 |
'wrap' => 'nowrap', |
| 356 |
'verticalAlign' => 'middle', |
| 357 |
'backgroundColor' => '', |
| 358 |
), |
| 359 |
); |
| 360 |
} |
| 361 |
} |
| 362 |
|