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 / TableBlockMigratorV0ToV1.php

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

2,230 lines 76.4 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 TableBlockMigratorV0ToV1 {
6 public function migrate($attrs, $block = null) {
7 if (!is_array($attrs)) {
8 return $attrs;
9 }
10
11 $legacy_cells = $this->get_legacy_cell_blocks($block);
12
13 $rows = (int) $this->get_scalar($attrs, 'rows', 0);
14 $cols = (int) $this->get_scalar($attrs, 'cols', 0);
15
16 foreach ($legacy_cells as $legacy_cell) {
17 $legacy_cell_attrs = $this->get_block_attrs($legacy_cell);
18 $row = (int) $this->get_scalar($legacy_cell_attrs, 'row', 0);
19 $col = (int) $this->get_scalar($legacy_cell_attrs, 'col', 0);
20 $rowspan = max(1, (int) $this->get_scalar($legacy_cell_attrs, 'rowspan', 1));
21 $colspan = max(1, (int) $this->get_scalar($legacy_cell_attrs, 'colspan', 1));
22
23 $rows = max($rows, $row + $rowspan);
24 $cols = max($cols, $col + $colspan);
25 }
26
27 if (count($legacy_cells) === 0 && $rows > 0 && $cols > 0) {
28 $legacy_cells = $this->create_synthetic_cells($rows, $cols);
29 }
30
31 $structure_cells = array();
32 $data_cells = array();
33 $cell_style_cells = array();
34
35 foreach ($legacy_cells as $legacy_cell) {
36 $legacy_cell_attrs = $this->get_block_attrs($legacy_cell);
37 $row = (int) $this->get_scalar($legacy_cell_attrs, 'row', 0);
38 $col = (int) $this->get_scalar($legacy_cell_attrs, 'col', 0);
39 $rowspan = max(1, (int) $this->get_scalar($legacy_cell_attrs, 'rowspan', 1));
40 $colspan = max(1, (int) $this->get_scalar($legacy_cell_attrs, 'colspan', 1));
41 $coords = array($row, $col);
42
43 if ($rowspan !== 1 || $colspan !== 1) {
44 $structure_cells[] = array(
45 $coords,
46 array(
47 'rowSpan' => $rowspan,
48 'colSpan' => $colspan,
49 ),
50 );
51 }
52
53 $migrated_cell = $this->migrate_legacy_cell(
54 $legacy_cell,
55 $attrs,
56 $rows,
57 $cols
58 );
59
60 $data_cells[] = array($coords, $migrated_cell['elements']);
61
62 if (
63 !empty($migrated_cell['styles']) ||
64 $migrated_cell['ribbon'] !== null
65 ) {
66 $style_entry = $migrated_cell['styles'];
67
68 if ($migrated_cell['ribbon'] !== null) {
69 $style_entry['ribbon'] = $migrated_cell['ribbon'];
70 }
71
72 $cell_style_cells[] = array($coords, $style_entry);
73 }
74 }
75
76 return array(
77 'version' => 1,
78 'isExample' => isset($attrs['isExample']) ? (bool) $attrs['isExample'] : false,
79 'structure' => array(
80 'rows' => $rows,
81 'cols' => $cols,
82 'cells' => $structure_cells,
83 'columns' => $this->migrate_columns($attrs, $cols),
84 'rowConfigs' => $this->migrate_row_configs($attrs, $rows),
85 ),
86 'data' => array(
87 'cells' => $data_cells,
88 ),
89 'tableConfig' => $this->migrate_table_config($attrs, $rows, $cols),
90 'cellStyles' => array(
91 'cells' => $cell_style_cells,
92 'padding' => $this->get_legacy_cell_padding($attrs),
93 'border' => $this->get_legacy_global_cell_border($attrs),
94 'borderRadius' => $this->normalize_border_radius(
95 $this->get_scalar($attrs, 'cellBorderRadius', array())
96 ),
97 'orientation' => 'vertical',
98 'elementGap' => $this->normalize_css_value(
99 $this->get_scalar($attrs, 'blockSpacing', '0')
100 ),
101 'wrap' => 'nowrap',
102 'verticalAlign' => 'middle',
103 'backgroundColor' => '',
104 ),
105 'bindings' => array(),
106 );
107 }
108
109 private function migrate_table_config($attrs, $rows, $cols) {
110 $table_dimensions = $this->map_table_dimensions(
111 $this->get_scalar($attrs, 'tableWidth', ''),
112 $this->get_scalar($attrs, 'tableAlignment', '')
113 );
114 $inner_border_type = $this->get_scalar($attrs, 'innerBorderType', '');
115 if (!in_array($inner_border_type, array('row', 'col'), true)) {
116 $inner_border_type = '';
117 }
118 $legacy_search_position = $this->get_scalar($attrs, 'searchPosition', 'left');
119 $legacy_responsive = $this->get_array($attrs, 'responsive', array());
120 $legacy_breakpoints = $this->get_array($legacy_responsive, 'breakpoints', array());
121
122 return array(
123 'headerEnabled' => $this->is_legacy_section_enabled(
124 $this->get_scalar($attrs, 'enableTableHeader', '')
125 ),
126 'footerEnabled' => $this->is_legacy_section_enabled(
127 $this->get_scalar($attrs, 'enableTableFooter', '')
128 ),
129 'stickyHeader' => !empty($attrs['stickyTopRow']),
130 'stickyFirstCol' => !empty($attrs['stickyFirstCol']),
131 'innerBorderType' => $inner_border_type,
132 'caption' => (string) $this->get_scalar($attrs, 'caption', ''),
133 'tableWidth' => $table_dimensions['tableWidth'],
134 'tableAlignment' => $table_dimensions['tableAlignment'],
135 'cellSpacing' => array(
136 'horizontal' => $this->pick_spacing_side(
137 $this->get_scalar($attrs, 'cellSpacing', array()),
138 array('left', 'right'),
139 '0'
140 ),
141 'vertical' => $this->pick_spacing_side(
142 $this->get_scalar($attrs, 'cellSpacing', array()),
143 array('top', 'bottom'),
144 '0'
145 ),
146 ),
147 'tableBorder' => $this->normalize_border_sides(
148 $this->get_scalar($attrs, 'tableBorder', array())
149 ),
150 'fixedColumnWidths' => array_key_exists('fixedColWidth', $attrs)
151 ? (bool) $attrs['fixedColWidth']
152 : true,
153 'pagination' => array(
154 'enabled' => false,
155 'pageSize' => 10,
156 'showPageNumbers' => true,
157 'showPrevNext' => true,
158 ),
159 'search' => array(
160 'enabled' => !empty($attrs['search']),
161 'placeholder' => (string) $this->get_scalar(
162 $attrs,
163 'searchPlaceholder',
164 'Search...'
165 ),
166 'highlightColor' => '',
167 'position' => $legacy_search_position === 'right' ? 'right' : 'left',
168 ),
169 'responsive' => array(
170 'tablet' => $this->migrate_responsive_breakpoint(
171 $this->get_array($legacy_breakpoints, 'tablet', array()),
172 array(
173 'enabled' => false,
174 'maxWidth' => 1024,
175 'mode' => 'scroll',
176 'transpose' => false,
177 'stackCount' => 3,
178 'repeatFirstCol' => false,
179 )
180 ),
181 'mobile' => $this->migrate_responsive_breakpoint(
182 $this->get_array($legacy_breakpoints, 'mobile', array()),
183 array(
184 'enabled' => false,
185 'maxWidth' => 700,
186 'mode' => 'scroll',
187 'transpose' => false,
188 'stackCount' => 1,
189 'repeatFirstCol' => false,
190 )
191 ),
192 ),
193 'rows' => $rows,
194 'cols' => $cols,
195 );
196 }
197
198 private function migrate_columns($attrs, $cols) {
199 $columns = array();
200 $col_styles = $this->get_array($attrs, 'colStyles', array());
201 $legacy_col_widths = $this->get_scalar($attrs, 'colWidths', array());
202
203 for ($column = 0; $column < $cols; $column++) {
204 $legacy_column = $this->get_array($col_styles, $column, array());
205 $width = $this->normalize_css_value(
206 $this->get_scalar(
207 $legacy_column,
208 'width',
209 is_array($legacy_col_widths) && isset($legacy_col_widths[$column])
210 ? $legacy_col_widths[$column]
211 : ''
212 )
213 );
214
215 if ($width !== '') {
216 $columns[$column] = array('width' => $width);
217 }
218 }
219
220 return $columns;
221 }
222
223 private function migrate_row_configs($attrs, $rows) {
224 $row_configs = array();
225 $row_styles = $this->get_array($attrs, 'rowStyles', array());
226 $legacy_row_heights = $this->get_scalar($attrs, 'rowHeights', array());
227
228 for ($row = 0; $row < $rows; $row++) {
229 $legacy_row = $this->get_array($row_styles, $row, array());
230 $height = $this->normalize_css_value(
231 $this->get_scalar(
232 $legacy_row,
233 'height',
234 is_array($legacy_row_heights) && isset($legacy_row_heights[$row])
235 ? $legacy_row_heights[$row]
236 : ''
237 )
238 );
239
240 if ($height !== '') {
241 $row_configs[$row] = array('height' => $height);
242 }
243 }
244
245 return $row_configs;
246 }
247
248 private function migrate_responsive_breakpoint($legacy_breakpoint, $defaults) {
249 $legacy_breakpoint = is_array($legacy_breakpoint) ? $legacy_breakpoint : array();
250 $legacy_direction = $this->get_scalar($legacy_breakpoint, 'direction', null);
251
252 return array(
253 'enabled' => array_key_exists('enabled', $legacy_breakpoint)
254 ? (bool) $legacy_breakpoint['enabled']
255 : $defaults['enabled'],
256 'maxWidth' => (int) $this->get_scalar(
257 $legacy_breakpoint,
258 'maxWidth',
259 $defaults['maxWidth']
260 ),
261 'mode' => (string) $this->get_scalar(
262 $legacy_breakpoint,
263 'mode',
264 $defaults['mode']
265 ),
266 'transpose' => $legacy_direction === null
267 ? $defaults['transpose']
268 : $legacy_direction === 'row',
269 'stackCount' => max(
270 1,
271 (int) $this->get_scalar(
272 $legacy_breakpoint,
273 'stackCount',
274 $defaults['stackCount']
275 )
276 ),
277 'repeatFirstCol' => array_key_exists('headerAsCol', $legacy_breakpoint)
278 ? (bool) $legacy_breakpoint['headerAsCol']
279 : $defaults['repeatFirstCol'],
280 );
281 }
282
283 private function migrate_legacy_cell($cell_block, $table_attrs, $rows, $cols) {
284 $legacy_cell_attrs = $this->get_block_attrs($cell_block);
285 $elements = array();
286 $ribbon = null;
287
288 foreach ($this->get_block_inner_blocks($cell_block) as $inner_block) {
289 $migrated_block = $this->migrate_legacy_inner_block(
290 $inner_block,
291 $legacy_cell_attrs,
292 $table_attrs
293 );
294
295 if (!empty($migrated_block['elements'])) {
296 $elements = array_merge($elements, $migrated_block['elements']);
297 }
298
299 if ($ribbon === null && $migrated_block['ribbon'] !== null) {
300 $ribbon = $migrated_block['ribbon'];
301 }
302 }
303
304 $styles = $this->build_cell_style_overrides(
305 $legacy_cell_attrs,
306 $table_attrs,
307 $rows,
308 $cols
309 );
310
311 return array(
312 'elements' => $elements,
313 'styles' => $styles,
314 'ribbon' => $ribbon,
315 );
316 }
317
318 private function build_cell_style_overrides($cell_attrs, $table_attrs, $rows, $cols) {
319 $styles = array();
320 $background_color = $this->resolve_cell_background_color(
321 $cell_attrs,
322 $table_attrs,
323 $rows
324 );
325
326 if ($background_color !== '') {
327 $styles['backgroundColor'] = $background_color;
328 }
329
330 $border = $this->resolve_cell_border($cell_attrs, $table_attrs, $rows, $cols);
331 if ($this->has_any_non_empty_value($border)) {
332 $styles['border'] = $border;
333 }
334
335 $border_radius = $this->resolve_cell_border_radius(
336 $cell_attrs,
337 $table_attrs,
338 $rows,
339 $cols
340 );
341 if ($this->has_any_non_empty_value($border_radius)) {
342 $styles['borderRadius'] = $border_radius;
343 }
344
345 if (!empty($cell_attrs['isHorizontal'])) {
346 $styles['orientation'] = 'horizontal';
347 }
348
349 $wrap_items = array_key_exists('wrapItems', $cell_attrs)
350 ? (bool) $cell_attrs['wrapItems']
351 : true;
352 if (!empty($cell_attrs['isHorizontal']) || array_key_exists('wrapItems', $cell_attrs)) {
353 $styles['wrap'] = $wrap_items ? 'wrap' : 'nowrap';
354 }
355
356 $vertical_align = $this->map_vertical_align(
357 $this->get_scalar($cell_attrs, 'vAlign', 'center')
358 );
359 if ($vertical_align !== 'middle') {
360 $styles['verticalAlign'] = $vertical_align;
361 }
362
363 $global_gap = $this->normalize_css_value(
364 $this->get_scalar($table_attrs, 'blockSpacing', '0')
365 );
366 $cell_gap = $this->normalize_css_value(
367 $this->get_scalar($cell_attrs, 'blockSpacing', '')
368 );
369
370 if ($cell_gap !== '' && $cell_gap !== $global_gap) {
371 $styles['elementGap'] = $cell_gap;
372 }
373
374 return $styles;
375 }
376
377 private function resolve_cell_background_color($cell_attrs, $table_attrs, $rows) {
378 $background_color = $this->get_background_value(
379 $this->get_array(
380 $this->get_array($table_attrs, 'colStyles', array()),
381 (int) $this->get_scalar($cell_attrs, 'col', 0),
382 array()
383 )
384 );
385
386 $row = (int) $this->get_scalar($cell_attrs, 'row', 0);
387 if ($this->is_header_cell($cell_attrs, $table_attrs)) {
388 $header_background = $this->normalize_css_value(
389 $this->get_scalar($table_attrs, 'headerBackgroundColor', '')
390 );
391 if ($header_background !== '') {
392 $background_color = $header_background;
393 }
394 } elseif ($this->is_footer_cell($cell_attrs, $table_attrs, $rows)) {
395 $footer_background = $this->normalize_css_value(
396 $this->get_scalar($table_attrs, 'footerBackgroundColor', '')
397 );
398 if ($footer_background !== '') {
399 $background_color = $footer_background;
400 }
401 } else {
402 $row_background = $this->normalize_css_value(
403 $this->get_scalar(
404 $table_attrs,
405 $this->is_odd_data_row($row, $table_attrs)
406 ? 'oddRowBackgroundColor'
407 : 'evenRowBackgroundColor',
408 ''
409 )
410 );
411 if ($row_background !== '') {
412 $background_color = $row_background;
413 }
414 }
415
416 $row_background = $this->get_background_value(
417 $this->get_array(
418 $this->get_array($table_attrs, 'rowStyles', array()),
419 $row,
420 array()
421 )
422 );
423 if ($row_background !== '') {
424 $background_color = $row_background;
425 }
426
427 $cell_background = $this->get_background_value($cell_attrs);
428 if ($cell_background !== '') {
429 $background_color = $cell_background;
430 }
431
432 return $background_color;
433 }
434
435 private function resolve_cell_border($cell_attrs, $table_attrs, $rows, $cols) {
436 $row = (int) $this->get_scalar($cell_attrs, 'row', 0);
437 $col = (int) $this->get_scalar($cell_attrs, 'col', 0);
438 $rowspan = max(1, (int) $this->get_scalar($cell_attrs, 'rowspan', 1));
439 $colspan = max(1, (int) $this->get_scalar($cell_attrs, 'colspan', 1));
440 $touches_first_row = $row === 0;
441 $touches_last_row = $row + $rowspan >= $rows;
442 $touches_first_col = $col === 0;
443 $touches_last_col = $col + $colspan >= $cols;
444
445 $border = $this->get_inner_border_for_cell($cell_attrs, $table_attrs, $rows, $cols);
446
447 $col_style = $this->get_array(
448 $this->get_array($table_attrs, 'colStyles', array()),
449 $col,
450 array()
451 );
452 $col_border = $this->normalize_border_sides(
453 $this->get_scalar($col_style, 'border', array())
454 );
455 if ($col_border['left'] !== '') {
456 $border['left'] = $col_border['left'];
457 }
458 if ($col_border['right'] !== '') {
459 $border['right'] = $col_border['right'];
460 }
461 if ($touches_first_row && $col_border['top'] !== '') {
462 $border['top'] = $col_border['top'];
463 }
464 if ($touches_last_row && $col_border['bottom'] !== '') {
465 $border['bottom'] = $col_border['bottom'];
466 }
467
468 $row_style = $this->get_array(
469 $this->get_array($table_attrs, 'rowStyles', array()),
470 $row,
471 array()
472 );
473 $row_border = $this->normalize_border_sides(
474 $this->get_scalar($row_style, 'border', array())
475 );
476 $border['top'] = $row_border['top'] !== '' ? $row_border['top'] : $border['top'];
477 $border['bottom'] = $row_border['bottom'] !== ''
478 ? $row_border['bottom']
479 : $border['bottom'];
480 if ($touches_first_col && $row_border['left'] !== '') {
481 $border['left'] = $row_border['left'];
482 }
483 if ($touches_last_col && $row_border['right'] !== '') {
484 $border['right'] = $row_border['right'];
485 }
486
487 $cell_border = $this->normalize_border_sides(
488 $this->get_scalar($cell_attrs, 'border', array())
489 );
490
491 return array_merge($border, array_filter($cell_border, function ($value) {
492 return $value !== '';
493 }));
494 }
495
496 private function get_inner_border_for_cell($cell_attrs, $table_attrs, $rows, $cols) {
497 if (!$this->is_legacy_inner_border_enabled($table_attrs)) {
498 return $this->get_empty_border_sides();
499 }
500
501 $row = (int) $this->get_scalar($cell_attrs, 'row', 0);
502 $col = (int) $this->get_scalar($cell_attrs, 'col', 0);
503 $rowspan = max(1, (int) $this->get_scalar($cell_attrs, 'rowspan', 1));
504 $colspan = max(1, (int) $this->get_scalar($cell_attrs, 'colspan', 1));
505 $touches_first_row = $row === 0;
506 $touches_last_row = $row + $rowspan >= $rows;
507 $touches_first_col = $col === 0;
508 $touches_last_col = $col + $colspan >= $cols;
509
510 $border = $this->get_legacy_inner_border($table_attrs);
511
512 $inner_border_type = $this->get_scalar($table_attrs, 'innerBorderType', '');
513 if ($inner_border_type === 'row') {
514 if (!$touches_first_col) {
515 $border['left'] = '';
516 }
517 if (!$touches_last_col) {
518 $border['right'] = '';
519 }
520 } elseif ($inner_border_type === 'col') {
521 if (!$touches_first_row) {
522 $border['top'] = '';
523 }
524 if (!$touches_last_row) {
525 $border['bottom'] = '';
526 }
527 }
528
529 if (!empty($table_attrs['hideCellOutsideBorders'])) {
530 if ($touches_first_row) {
531 $border['top'] = '';
532 }
533 if ($touches_last_row) {
534 $border['bottom'] = '';
535 }
536 if ($touches_first_col) {
537 $border['left'] = '';
538 }
539 if ($touches_last_col) {
540 $border['right'] = '';
541 }
542 }
543
544 return $border;
545 }
546
547 private function get_legacy_global_cell_border($attrs) {
548 if (!$this->is_legacy_inner_border_enabled($attrs)) {
549 return $this->get_empty_border_sides();
550 }
551
552 return $this->get_legacy_inner_border($attrs);
553 }
554
555 private function get_legacy_cell_padding($attrs) {
556 $cell_padding = $this->normalize_spacing(
557 $this->get_scalar($attrs, 'cellPadding', array())
558 );
559
560 $default_padding = array(
561 'top' => 'var(--wp--preset--spacing--20)',
562 'right' => 'var(--wp--preset--spacing--20)',
563 'bottom' => 'var(--wp--preset--spacing--20)',
564 'left' => 'var(--wp--preset--spacing--20)',
565 );
566
567 foreach ($default_padding as $side => $default_value) {
568 if ($cell_padding[$side] === '') {
569 $cell_padding[$side] = $default_value;
570 }
571 }
572
573 return $cell_padding;
574 }
575
576 private function is_legacy_inner_border_enabled($attrs) {
577 if (!is_array($attrs) || !array_key_exists('enableInnerBorder', $attrs)) {
578 return true;
579 }
580
581 return (bool) $attrs['enableInnerBorder'];
582 }
583
584 private function get_legacy_inner_border($attrs) {
585 $legacy_inner_border = $this->get_scalar(
586 $attrs,
587 'innerBorder',
588 array(
589 'color' => '#000000',
590 'width' => '1px',
591 )
592 );
593
594 if (!is_array($legacy_inner_border) || empty($legacy_inner_border)) {
595 $legacy_inner_border = array(
596 'color' => '#000000',
597 'width' => '1px',
598 );
599 }
600
601 return $this->normalize_border_sides($legacy_inner_border);
602 }
603
604 private function resolve_cell_border_radius($cell_attrs, $table_attrs, $rows, $cols) {
605 $row = (int) $this->get_scalar($cell_attrs, 'row', 0);
606 $col = (int) $this->get_scalar($cell_attrs, 'col', 0);
607 $rowspan = max(1, (int) $this->get_scalar($cell_attrs, 'rowspan', 1));
608 $colspan = max(1, (int) $this->get_scalar($cell_attrs, 'colspan', 1));
609 $touches_first_row = $row === 0;
610 $touches_last_row = $row + $rowspan >= $rows;
611 $touches_first_col = $col === 0;
612 $touches_last_col = $col + $colspan >= $cols;
613
614 $border_radius = $this->get_empty_border_radius();
615
616 $col_style = $this->get_array(
617 $this->get_array($table_attrs, 'colStyles', array()),
618 $col,
619 array()
620 );
621 $col_radius = $this->normalize_border_radius(
622 $this->get_scalar($col_style, 'borderRadius', array())
623 );
624 if ($touches_first_row) {
625 $border_radius['topLeft'] = $col_radius['topLeft'];
626 $border_radius['topRight'] = $col_radius['topRight'];
627 }
628 if ($touches_last_row) {
629 $border_radius['bottomLeft'] = $col_radius['bottomLeft'];
630 $border_radius['bottomRight'] = $col_radius['bottomRight'];
631 }
632
633 $row_style = $this->get_array(
634 $this->get_array($table_attrs, 'rowStyles', array()),
635 $row,
636 array()
637 );
638 $row_radius = $this->normalize_border_radius(
639 $this->get_scalar($row_style, 'borderRadius', array())
640 );
641 if ($touches_first_col) {
642 if ($row_radius['topLeft'] !== '') {
643 $border_radius['topLeft'] = $row_radius['topLeft'];
644 }
645 if ($row_radius['bottomLeft'] !== '') {
646 $border_radius['bottomLeft'] = $row_radius['bottomLeft'];
647 }
648 }
649 if ($touches_last_col) {
650 if ($row_radius['topRight'] !== '') {
651 $border_radius['topRight'] = $row_radius['topRight'];
652 }
653 if ($row_radius['bottomRight'] !== '') {
654 $border_radius['bottomRight'] = $row_radius['bottomRight'];
655 }
656 }
657
658 $cell_radius = $this->normalize_border_radius(
659 $this->get_scalar($cell_attrs, 'borderRadius', array())
660 );
661
662 return array_merge($border_radius, array_filter($cell_radius, function ($value) {
663 return $value !== '';
664 }));
665 }
666
667 private function migrate_legacy_inner_block($block, $legacy_cell_attrs, $table_attrs) {
668 $block_name = $this->get_block_name($block);
669 $block_attrs = $this->get_block_attrs($block);
670
671 switch ($block_name) {
672 case 'core/paragraph':
673 return array(
674 'elements' => array($this->migrate_paragraph_element(
675 $block,
676 $block_attrs,
677 $legacy_cell_attrs,
678 $table_attrs
679 )),
680 'ribbon' => null,
681 );
682
683 case 'tableberg/button':
684 return array(
685 'elements' => array($this->migrate_button_element(
686 $block_attrs,
687 $legacy_cell_attrs,
688 $table_attrs
689 )),
690 'ribbon' => null,
691 );
692
693 case 'tableberg/image':
694 return array(
695 'elements' => array($this->migrate_image_element(
696 $block_attrs,
697 $legacy_cell_attrs
698 )),
699 'ribbon' => null,
700 );
701
702 case 'core/list':
703 return array(
704 'elements' => array($this->migrate_core_list_element(
705 $block,
706 $block_attrs,
707 $legacy_cell_attrs,
708 $table_attrs
709 )),
710 'ribbon' => null,
711 );
712
713 case 'tableberg/styled-list':
714 return array(
715 'elements' => array($this->migrate_styled_list_element(
716 $block,
717 $block_attrs,
718 $legacy_cell_attrs,
719 $table_attrs
720 )),
721 'ribbon' => null,
722 );
723
724 case 'tableberg/icon':
725 return array(
726 'elements' => array($this->migrate_icon_element(
727 $block_attrs,
728 $legacy_cell_attrs
729 )),
730 'ribbon' => null,
731 );
732
733 case 'tableberg/star-rating':
734 return array(
735 'elements' => array($this->migrate_star_rating_element(
736 $block_attrs,
737 $legacy_cell_attrs,
738 $table_attrs
739 )),
740 'ribbon' => null,
741 );
742
743 case 'tableberg/html':
744 return array(
745 'elements' => array($this->migrate_custom_html_element(
746 $block_attrs,
747 $legacy_cell_attrs
748 )),
749 'ribbon' => null,
750 );
751
752 case 'tableberg/ribbon':
753 return array(
754 'elements' => array(),
755 'ribbon' => $this->migrate_ribbon($block_attrs),
756 );
757
758 case 'tableberg/dynamic-field':
759 return $this->migrate_dynamic_field_block(
760 $block,
761 $legacy_cell_attrs,
762 $table_attrs
763 );
764 }
765
766 $inner_blocks = $this->get_block_inner_blocks($block);
767 if (!empty($inner_blocks)) {
768 $elements = array();
769 $ribbon = null;
770
771 foreach ($inner_blocks as $inner_block) {
772 $migrated_inner_block = $this->migrate_legacy_inner_block(
773 $inner_block,
774 $legacy_cell_attrs,
775 $table_attrs
776 );
777
778 if (!empty($migrated_inner_block['elements'])) {
779 $elements = array_merge($elements, $migrated_inner_block['elements']);
780 }
781
782 if ($ribbon === null && $migrated_inner_block['ribbon'] !== null) {
783 $ribbon = $migrated_inner_block['ribbon'];
784 }
785 }
786
787 return array(
788 'elements' => $elements,
789 'ribbon' => $ribbon,
790 );
791 }
792
793 $html = $this->extract_block_html($block);
794 if ($html === '') {
795 return array(
796 'elements' => array(),
797 'ribbon' => null,
798 );
799 }
800
801 return array(
802 'elements' => array(array(
803 'name' => 'custom-html',
804 'attributes' => array(
805 'content' => $html,
806 'align' => $this->resolve_element_alignment(null, $legacy_cell_attrs),
807 ),
808 )),
809 'ribbon' => null,
810 );
811 }
812
813 private function migrate_dynamic_field_block($block, $legacy_cell_attrs, $table_attrs) {
814 $elements = array();
815 $ribbon = null;
816
817 foreach ($this->get_block_inner_blocks($block) as $inner_block) {
818 $migrated_inner_block = $this->migrate_legacy_inner_block(
819 $inner_block,
820 $legacy_cell_attrs,
821 $table_attrs
822 );
823
824 if (!empty($migrated_inner_block['elements'])) {
825 $elements = array_merge($elements, $migrated_inner_block['elements']);
826 }
827
828 if ($ribbon === null && $migrated_inner_block['ribbon'] !== null) {
829 $ribbon = $migrated_inner_block['ribbon'];
830 }
831 }
832
833 return array(
834 'elements' => $elements,
835 'ribbon' => $ribbon,
836 );
837 }
838
839 private function migrate_paragraph_element($block, $attrs, $legacy_cell_attrs, $table_attrs) {
840 return array(
841 'name' => 'text',
842 'attributes' => array(
843 'content' => $this->extract_legacy_paragraph_content($block, $attrs),
844 'align' => $this->resolve_legacy_text_alignment(
845 $attrs,
846 $legacy_cell_attrs,
847 $table_attrs
848 ),
849 'styles' => array(
850 'textColor' => $this->extract_text_color(
851 $attrs,
852 $this->normalize_css_value(
853 $this->get_scalar($table_attrs, 'fontColor', '')
854 )
855 ),
856 'linkColor' => $this->extract_link_color(
857 $attrs,
858 $this->normalize_css_value(
859 $this->get_scalar($table_attrs, 'linkColor', '')
860 )
861 ),
862 'backgroundColor' => $this->extract_background_color($attrs),
863 'fontSize' => $this->extract_legacy_paragraph_font_size(
864 $block,
865 $attrs,
866 $this->normalize_css_value(
867 $this->get_scalar($table_attrs, 'fontSize', '')
868 )
869 ),
870 ),
871 ),
872 );
873 }
874
875 private function extract_legacy_paragraph_font_size($block, $attrs, $fallback) {
876 $font_size = $this->extract_font_size($attrs, '');
877 if ($font_size !== '') {
878 return $font_size;
879 }
880
881 $html = $this->extract_block_html($block);
882 if ($html === '') {
883 $html = $this->extract_block_html_from_attrs($attrs);
884 }
885
886 $html_font_size = $this->extract_first_tag_style_value($html, 'p', 'font-size');
887 if ($html_font_size !== null && $html_font_size !== '') {
888 return $html_font_size;
889 }
890
891 $html_font_size_slug = $this->extract_first_tag_class_font_size_slug($html, 'p');
892 if ($html_font_size_slug !== null && $html_font_size_slug !== '') {
893 return $this->map_legacy_font_size_slug($html_font_size_slug);
894 }
895
896 return $fallback;
897 }
898
899 private function resolve_legacy_text_alignment($attrs, $legacy_cell_attrs, $table_attrs) {
900 $align = $this->get_scalar($attrs, 'align', null);
901 if ($this->map_text_alignment($align, '') !== '') {
902 return $this->resolve_element_alignment($align, $legacy_cell_attrs);
903 }
904
905 $table_rows = max(1, (int) $this->get_scalar($table_attrs, 'rows', 0));
906 if (
907 $this->is_header_cell($legacy_cell_attrs, $table_attrs) ||
908 $this->is_footer_cell($legacy_cell_attrs, $table_attrs, $table_rows)
909 ) {
910 return 'center';
911 }
912
913 return $this->resolve_element_alignment($align, $legacy_cell_attrs);
914 }
915
916 private function extract_legacy_paragraph_content($block, $attrs) {
917 $content = $this->get_scalar($attrs, 'content', null);
918 if (is_string($content) && $content !== '') {
919 return $content;
920 }
921
922 $html = $this->extract_block_html($block);
923 if ($html === '') {
924 $html = $this->extract_block_html_from_attrs($attrs);
925 }
926 if ($html === '') {
927 return '';
928 }
929
930 $inner_html = $this->extract_first_tag_inner_html($html, 'p');
931 if ($inner_html !== null) {
932 return $inner_html;
933 }
934
935 return $html;
936 }
937
938 private function migrate_button_element($attrs, $legacy_cell_attrs, $table_attrs) {
939 return array(
940 'name' => 'button',
941 'attributes' => array(
942 'content' => (string) $this->get_scalar($attrs, 'text', ''),
943 'id' => (string) $this->get_scalar($attrs, 'id', ''),
944 'align' => $this->resolve_element_alignment(
945 $this->get_scalar($attrs, 'align', null),
946 $legacy_cell_attrs
947 ),
948 'link' => array(
949 'url' => (string) $this->get_scalar($attrs, 'url', ''),
950 'target' => $this->get_scalar($attrs, 'linkTarget', '') === '_blank'
951 ? '_blank'
952 : '_self',
953 ),
954 'styles' => array(
955 'backgroundColor' => $this->normalize_css_value(
956 $this->get_scalar($attrs, 'backgroundColor', '#000000')
957 ),
958 'textColor' => $this->normalize_css_value(
959 $this->get_scalar($attrs, 'textColor', '#ffffff')
960 ),
961 'backgroundHoverColor' => $this->normalize_css_value(
962 $this->get_scalar($attrs, 'backgroundHoverColor', '')
963 ),
964 'textHoverColor' => $this->normalize_css_value(
965 $this->get_scalar($attrs, 'textHoverColor', '')
966 ),
967 'textAlign' => $this->map_text_alignment(
968 $this->get_scalar($attrs, 'textAlign', 'center'),
969 'center'
970 ),
971 'width' => $this->map_button_width(
972 $this->get_scalar($attrs, 'width', null)
973 ),
974 'padding' => $this->normalize_spacing(
975 $this->get_scalar($attrs, 'padding', array())
976 ),
977 'borderRadius' => $this->normalize_border_radius(
978 $this->get_nested_value($attrs, array('style', 'border', 'radius'), array())
979 ),
980 'fontSize' => $this->extract_font_size(
981 $attrs,
982 $this->normalize_css_value(
983 $this->get_scalar($table_attrs, 'fontSize', '')
984 )
985 ),
986 ),
987 ),
988 );
989 }
990
991 private function migrate_image_element($attrs, $legacy_cell_attrs) {
992 $media = $this->get_array($attrs, 'media', array());
993
994 return array(
995 'name' => 'image',
996 'attributes' => array(
997 'media' => array(
998 'id' => is_numeric($this->get_scalar($media, 'id', null))
999 ? (int) $media['id']
1000 : null,
1001 'url' => (string) $this->get_scalar($media, 'url', ''),
1002 'alt' => (string) $this->get_scalar($media, 'alt', ''),
1003 'title' => (string) $this->get_scalar($media, 'title', ''),
1004 'sizes' => $this->get_array($media, 'sizes', array()),
1005 ),
1006 'height' => $this->normalize_css_value(
1007 $this->get_scalar($attrs, 'height', '')
1008 ),
1009 'width' => $this->normalize_css_value(
1010 $this->get_scalar($attrs, 'width', '')
1011 ),
1012 'alt' => (string) $this->get_scalar(
1013 $attrs,
1014 'alt',
1015 $this->get_scalar($media, 'alt', '')
1016 ),
1017 'align' => $this->resolve_element_alignment(
1018 $this->get_scalar($attrs, 'align', null),
1019 $legacy_cell_attrs
1020 ),
1021 'aspectRatio' => (string) $this->get_scalar($attrs, 'aspectRatio', ''),
1022 'scale' => (string) $this->get_scalar($attrs, 'scale', ''),
1023 'sizeSlug' => (string) $this->get_scalar($attrs, 'sizeSlug', 'large'),
1024 'caption' => (string) $this->get_scalar($attrs, 'caption', ''),
1025 'href' => (string) $this->get_scalar($attrs, 'href', ''),
1026 'linkTarget' => (string) $this->get_scalar($attrs, 'linkTarget', '_self'),
1027 'border' => $this->normalize_border_sides(
1028 $this->get_scalar($attrs, 'border', array())
1029 ),
1030 'borderRadius' => $this->normalize_border_radius(
1031 $this->get_scalar($attrs, 'borderRadius', array())
1032 ),
1033 ),
1034 );
1035 }
1036
1037 private function migrate_core_list_element($block, $attrs, $legacy_cell_attrs, $table_attrs) {
1038 $items = $this->parse_core_list_items($block, $attrs);
1039
1040 return array(
1041 'name' => 'list',
1042 'attributes' => array(
1043 'align' => $this->resolve_element_alignment(
1044 $this->get_scalar($attrs, 'align', null),
1045 $legacy_cell_attrs
1046 ),
1047 'items' => $items,
1048 'listType' => 'basic',
1049 'listStyle' => $this->resolve_core_list_style($attrs),
1050 'icon' => null,
1051 'styles' => array(
1052 'itemSpacing' => '0',
1053 'iconColor' => '',
1054 'iconSize' => '',
1055 'iconSpacing' => '',
1056 'fontSize' => $this->extract_font_size(
1057 $attrs,
1058 $this->normalize_css_value(
1059 $this->get_scalar($table_attrs, 'fontSize', '')
1060 )
1061 ),
1062 'textColor' => $this->extract_text_color(
1063 $attrs,
1064 $this->normalize_css_value(
1065 $this->get_scalar($table_attrs, 'fontColor', '')
1066 )
1067 ),
1068 'linkColor' => $this->extract_link_color(
1069 $attrs,
1070 $this->normalize_css_value(
1071 $this->get_scalar($table_attrs, 'linkColor', '')
1072 )
1073 ),
1074 'backgroundColor' => $this->extract_background_color($attrs),
1075 ),
1076 ),
1077 );
1078 }
1079
1080 private function parse_core_list_items($block, $attrs) {
1081 $items = array();
1082 $this->append_core_list_items($this->get_block_inner_blocks($block), 0, $items);
1083
1084 if (!empty($items)) {
1085 return $items;
1086 }
1087
1088 return $this->parse_list_items_from_html(
1089 (string) $this->get_scalar($attrs, 'values', $this->extract_block_html($block))
1090 );
1091 }
1092
1093 private function append_core_list_items($blocks, $indent_level, &$items) {
1094 foreach ($blocks as $block) {
1095 if (!is_array($block)) {
1096 continue;
1097 }
1098
1099 $block_name = $this->get_block_name($block);
1100
1101 if ($block_name === 'core/list') {
1102 $this->append_core_list_items(
1103 $this->get_block_inner_blocks($block),
1104 $indent_level,
1105 $items
1106 );
1107 continue;
1108 }
1109
1110 if ($block_name !== 'core/list-item') {
1111 continue;
1112 }
1113
1114 $block_attrs = $this->get_block_attrs($block);
1115 $items[] = array(
1116 'content' => $this->extract_core_list_item_content($block, $block_attrs),
1117 'indentLevel' => $indent_level,
1118 );
1119
1120 foreach ($this->get_block_inner_blocks($block) as $inner_block) {
1121 if ($this->get_block_name($inner_block) !== 'core/list') {
1122 continue;
1123 }
1124
1125 $this->append_core_list_items(
1126 $this->get_block_inner_blocks($inner_block),
1127 $indent_level + 1,
1128 $items
1129 );
1130 }
1131 }
1132 }
1133
1134 private function extract_core_list_item_content($block, $attrs) {
1135 $content = $this->get_scalar($attrs, 'content', null);
1136 if (is_string($content) && $content !== '') {
1137 return $content;
1138 }
1139
1140 $html = $this->extract_block_html($block);
1141 if ($html === '') {
1142 return '';
1143 }
1144
1145 $inner_html = $this->extract_first_tag_inner_html($html, 'li', array('ul', 'ol'));
1146 if ($inner_html !== null) {
1147 return trim($inner_html);
1148 }
1149
1150 return trim($html);
1151 }
1152
1153 private function migrate_styled_list_element($block, $attrs, $legacy_cell_attrs, $table_attrs) {
1154 $first_item_attrs = $this->get_first_styled_list_item_attrs($block);
1155 $icon = $this->migrate_legacy_icon_payload(
1156 $this->get_scalar($attrs, 'icon', null)
1157 );
1158 if ($icon === null) {
1159 $icon = $this->migrate_legacy_icon_payload(
1160 $this->get_scalar($first_item_attrs, 'icon', null)
1161 );
1162 }
1163 if ($icon === null && (!array_key_exists('icon', $attrs) || empty($attrs['icon']))) {
1164 $icon = $this->get_default_legacy_icon_payload();
1165 }
1166
1167 $parent_icon_color = $this->normalize_css_value(
1168 $this->get_scalar($attrs, 'iconColor', '')
1169 );
1170 $parent_icon_size = $this->normalize_css_value(
1171 $this->get_scalar($attrs, 'iconSize', '')
1172 );
1173 $parent_icon_spacing = $this->normalize_css_value(
1174 $this->get_scalar($attrs, 'iconSpacing', '')
1175 );
1176 $item_icon_spacing = $this->normalize_css_value(
1177 $this->get_scalar($first_item_attrs, 'iconSpacing', '')
1178 );
1179 $parent_font_size = $this->extract_font_size(
1180 $attrs,
1181 $this->normalize_css_value(
1182 $this->get_scalar($table_attrs, 'fontSize', '')
1183 )
1184 );
1185 $parent_text_color = $this->normalize_css_value(
1186 $this->get_scalar($attrs, 'textColor', '')
1187 );
1188
1189 return array(
1190 'name' => 'list',
1191 'attributes' => array(
1192 'align' => $this->resolve_element_alignment(
1193 $this->get_scalar($attrs, 'alignment', null),
1194 $legacy_cell_attrs
1195 ),
1196 'items' => $this->parse_styled_list_items($block),
1197 'listType' => 'styled',
1198 'listStyle' => 'none',
1199 'icon' => $icon,
1200 'styles' => array(
1201 'itemSpacing' => $this->normalize_css_value(
1202 $this->get_scalar($attrs, 'itemSpacing', '')
1203 ),
1204 'iconColor' => $parent_icon_color !== ''
1205 ? $parent_icon_color
1206 : $this->normalize_css_value(
1207 $this->get_scalar($first_item_attrs, 'iconColor', '')
1208 ),
1209 'iconSize' => $parent_icon_size !== ''
1210 ? $parent_icon_size
1211 : $this->normalize_css_value(
1212 $this->get_scalar($first_item_attrs, 'iconSize', '')
1213 ),
1214 'iconSpacing' => $parent_icon_spacing !== ''
1215 ? $parent_icon_spacing
1216 : ($item_icon_spacing !== ''
1217 ? $item_icon_spacing
1218 : 'var(--wp--preset--spacing--20)'),
1219 'fontSize' => $parent_font_size !== ''
1220 ? $parent_font_size
1221 : $this->extract_font_size(
1222 $first_item_attrs,
1223 $this->normalize_css_value(
1224 $this->get_scalar($table_attrs, 'fontSize', '')
1225 )
1226 ),
1227 'textColor' => $parent_text_color !== ''
1228 ? $parent_text_color
1229 : $this->normalize_css_value(
1230 $this->get_scalar($first_item_attrs, 'textColor', '')
1231 ),
1232 'linkColor' => $this->normalize_css_value(
1233 $this->get_scalar($table_attrs, 'linkColor', '')
1234 ),
1235 'backgroundColor' => $this->normalize_css_value(
1236 $this->get_scalar($attrs, 'backgroundColor', '')
1237 ),
1238 ),
1239 ),
1240 );
1241 }
1242
1243 private function get_first_styled_list_item_attrs($block) {
1244 foreach ($this->get_block_inner_blocks($block) as $inner_block) {
1245 $block_name = $this->get_block_name($inner_block);
1246
1247 if ($block_name === 'tableberg/styled-list-item') {
1248 return $this->get_block_attrs($inner_block);
1249 }
1250
1251 if ($block_name === 'tableberg/styled-list') {
1252 $nested_attrs = $this->get_first_styled_list_item_attrs($inner_block);
1253 if (!empty($nested_attrs)) {
1254 return $nested_attrs;
1255 }
1256 }
1257 }
1258
1259 return array();
1260 }
1261
1262 private function migrate_icon_element($attrs, $legacy_cell_attrs) {
1263 $icon = $this->migrate_legacy_icon_payload(
1264 $this->get_scalar($attrs, 'icon', null)
1265 );
1266 if ($icon === null) {
1267 $icon = $this->get_default_legacy_icon_payload();
1268 }
1269
1270 return array(
1271 'name' => 'icon',
1272 'attributes' => array(
1273 'icon' => $icon,
1274 'size' => $this->normalize_css_value(
1275 $this->get_scalar($attrs, 'size', '40px')
1276 ),
1277 'behavior' => $this->get_scalar($attrs, 'behavior', 'paragraph') === 'char'
1278 ? 'char'
1279 : 'paragraph',
1280 'link' => array(
1281 'url' => (string) $this->get_scalar($attrs, 'linkUrl', ''),
1282 'target' => $this->get_scalar($attrs, 'linkTarget', '') === '_blank'
1283 ? '_blank'
1284 : '_self',
1285 ),
1286 'styles' => array(
1287 'align' => $this->resolve_element_alignment(
1288 $this->get_scalar($attrs, 'justify', null),
1289 $legacy_cell_attrs
1290 ),
1291 'rotation' => (int) $this->get_scalar($attrs, 'rotation', 0),
1292 'color' => $this->normalize_css_value(
1293 $this->get_scalar($attrs, 'color', '')
1294 ),
1295 'colorHover' => $this->normalize_css_value(
1296 $this->get_scalar($attrs, 'colorHover', '')
1297 ),
1298 'background' => $this->normalize_css_value(
1299 $this->get_scalar($attrs, 'background', '')
1300 ),
1301 'backgroundHover' => $this->normalize_css_value(
1302 $this->get_scalar($attrs, 'backgroundHover', '')
1303 ),
1304 'padding' => $this->normalize_spacing(
1305 $this->get_scalar($attrs, 'padding', array())
1306 ),
1307 'border' => $this->normalize_border_sides(
1308 $this->get_scalar($attrs, 'border', array())
1309 ),
1310 'borderRadius' => $this->normalize_border_radius(
1311 $this->get_scalar($attrs, 'borderRadius', array())
1312 ),
1313 ),
1314 ),
1315 );
1316 }
1317
1318 private function migrate_star_rating_element($attrs, $legacy_cell_attrs, $table_attrs) {
1319 return array(
1320 'name' => 'star-rating',
1321 'attributes' => array(
1322 'starCount' => (int) $this->get_scalar($attrs, 'starCount', 5),
1323 'starSize' => (int) $this->get_scalar($attrs, 'starSize', 20),
1324 'starColor' => $this->normalize_css_value(
1325 $this->get_scalar($attrs, 'starColor', '#FF912C')
1326 ),
1327 'selectedStars' => (float) $this->get_scalar($attrs, 'selectedStars', 0),
1328 'enableText' => !empty($attrs['enableText']),
1329 'reviewText' => (string) $this->get_scalar($attrs, 'reviewText', ''),
1330 'reviewTextAlign' => $this->map_text_alignment(
1331 $this->get_scalar($attrs, 'reviewTextAlign', 'left'),
1332 'left'
1333 ),
1334 'reviewTextColor' => $this->normalize_css_value(
1335 $this->get_scalar($attrs, 'reviewTextColor', '')
1336 ),
1337 'reviewTextLinkColor' => $this->normalize_css_value(
1338 $this->get_scalar($table_attrs, 'linkColor', '')
1339 ),
1340 'reviewTextFontSize' => $this->extract_font_size(
1341 $attrs,
1342 $this->normalize_css_value(
1343 $this->get_scalar($table_attrs, 'fontSize', '')
1344 )
1345 ),
1346 'align' => $this->resolve_element_alignment(
1347 $this->get_scalar($attrs, 'starAlign', null),
1348 $legacy_cell_attrs
1349 ),
1350 ),
1351 );
1352 }
1353
1354 private function migrate_custom_html_element($attrs, $legacy_cell_attrs) {
1355 return array(
1356 'name' => 'custom-html',
1357 'attributes' => array(
1358 'content' => (string) $this->get_scalar($attrs, 'content', ''),
1359 'align' => $this->resolve_element_alignment(null, $legacy_cell_attrs),
1360 ),
1361 );
1362 }
1363
1364 private function migrate_ribbon($attrs) {
1365 $type = $this->get_scalar($attrs, 'type', 'side');
1366 if (!in_array($type, array('badge', 'bookmark', 'corner', 'side', 'icon'), true)) {
1367 $type = 'side';
1368 }
1369
1370 $ribbon_attrs = $this->get_default_ribbon_attrs($type);
1371 $individual = $this->get_array($attrs, 'individual', array());
1372
1373 $ribbon_attrs['text'] = (string) $this->get_scalar(
1374 $attrs,
1375 'text',
1376 $ribbon_attrs['text']
1377 );
1378 $ribbon_attrs['style']['color'] = $this->normalize_css_value(
1379 $this->get_scalar($attrs, 'color', $ribbon_attrs['style']['color'])
1380 );
1381 $ribbon_attrs['style']['background'] = $this->normalize_css_value(
1382 $this->get_scalar($attrs, 'background', $ribbon_attrs['style']['background'])
1383 );
1384 $ribbon_attrs['style']['bgGradient'] = $this->normalize_css_value(
1385 $this->get_scalar($attrs, 'bgGradient', $ribbon_attrs['style']['bgGradient'])
1386 );
1387 $ribbon_attrs['style']['fontSize'] = $this->normalize_css_value(
1388 $this->get_scalar($attrs, 'fontSize', $ribbon_attrs['style']['fontSize'])
1389 );
1390
1391 if (isset($individual['side']) && is_string($individual['side'])) {
1392 $ribbon_attrs['originX'] = $individual['side'] === 'right' ? 'right' : 'left';
1393 }
1394 if (isset($individual['originY']) && is_string($individual['originY'])) {
1395 $ribbon_attrs['originY'] = $individual['originY'];
1396 }
1397 foreach (array('x', 'y', 'height', 'width', 'iconSize', 'shape') as $key) {
1398 if (isset($individual[$key]) && is_string($individual[$key])) {
1399 $ribbon_attrs[$key] = $individual[$key];
1400 }
1401 }
1402 if (isset($individual['rotate']) && is_numeric($individual['rotate'])) {
1403 $ribbon_attrs['rotate'] = (int) $individual['rotate'];
1404 }
1405
1406 return array(
1407 'enabled' => true,
1408 'type' => $type,
1409 'attrs' => $ribbon_attrs,
1410 );
1411 }
1412
1413 private function get_default_ribbon_attrs($type) {
1414 $attrs = array(
1415 'text' => 'New',
1416 'originX' => 'left',
1417 'originY' => 'top',
1418 'x' => '-1px',
1419 'y' => '-3px',
1420 'rotate' => 0,
1421 'height' => '70px',
1422 'width' => '30px',
1423 'iconSize' => '20px',
1424 'shape' => 'up',
1425 'icon' => null,
1426 'style' => array(
1427 'color' => '#ffffff',
1428 'background' => '#671FEB',
1429 'bgGradient' => '',
1430 'fontSize' => '1.38rem',
1431 'padding' => array(
1432 'top' => 'var(--wp--preset--spacing--20)',
1433 'right' => 'var(--wp--preset--spacing--40)',
1434 'bottom' => 'var(--wp--preset--spacing--20)',
1435 'left' => 'var(--wp--preset--spacing--40)',
1436 ),
1437 'borderRadius' => array(
1438 'topLeft' => '4px',
1439 'topRight' => '4px',
1440 'bottomRight' => '4px',
1441 'bottomLeft' => '4px',
1442 ),
1443 ),
1444 );
1445
1446 switch ($type) {
1447 case 'bookmark':
1448 $attrs['x'] = '20px';
1449 $attrs['originX'] = 'right';
1450 break;
1451
1452 case 'corner':
1453 $attrs['y'] = '50px';
1454 break;
1455
1456 case 'icon':
1457 $attrs['x'] = '20px';
1458 $attrs['originX'] = 'right';
1459 $attrs['icon'] = array(
1460 'iconName' => 'star',
1461 'svg' => array(
1462 'viewBox' => '0 0 576 512',
1463 'path' => 'M259.3 17.8L194 150.2 47.9 171.5c-26.2 3.8-36.7 36.1-17.7 54.6l105.7 103-25 145.5c-4.5 26.3 23.2 46 46.4 33.7L288 439.6l130.7 68.7c23.2 12.2 50.9-7.4 46.4-33.7l-25-145.5 105.7-103c19-18.5 8.5-50.8-17.7-54.6L382 150.2 316.7 17.8c-11.7-23.6-45.6-23.9-57.4 0z',
1464 ),
1465 );
1466 break;
1467
1468 case 'side':
1469 $attrs['y'] = '10px';
1470 break;
1471 }
1472
1473 return $attrs;
1474 }
1475
1476 private function parse_styled_list_items($block) {
1477 $items = array();
1478 $this->append_styled_list_items($this->get_block_inner_blocks($block), 0, $items);
1479
1480 return $items;
1481 }
1482
1483 private function append_styled_list_items($blocks, $indent_level, &$items) {
1484 foreach ($blocks as $block) {
1485 if (!is_array($block)) {
1486 continue;
1487 }
1488
1489 $block_name = $this->get_block_name($block);
1490 if ($block_name === 'tableberg/styled-list') {
1491 $this->append_styled_list_items(
1492 $this->get_block_inner_blocks($block),
1493 $indent_level,
1494 $items
1495 );
1496 continue;
1497 }
1498
1499 if ($block_name !== 'tableberg/styled-list-item') {
1500 continue;
1501 }
1502
1503 $block_attrs = $this->get_block_attrs($block);
1504 $items[] = array(
1505 'content' => (string) $this->get_scalar($block_attrs, 'text', ''),
1506 'indentLevel' => $indent_level,
1507 );
1508
1509 foreach ($this->get_block_inner_blocks($block) as $inner_block) {
1510 if ($this->get_block_name($inner_block) !== 'tableberg/styled-list') {
1511 continue;
1512 }
1513
1514 $this->append_styled_list_items(
1515 $this->get_block_inner_blocks($inner_block),
1516 $indent_level + 1,
1517 $items
1518 );
1519 }
1520 }
1521 }
1522
1523 private function parse_list_items_from_html($html) {
1524 if (!is_string($html) || trim($html) === '') {
1525 return array();
1526 }
1527
1528 $items = array();
1529
1530 if (preg_match_all('/<li\b[^>]*>(.*?)<\/li>/is', $html, $matches)) {
1531 foreach ($matches[1] as $item_html) {
1532 $items[] = array(
1533 'content' => trim($this->strip_nested_list_markup($item_html)),
1534 'indentLevel' => 0,
1535 );
1536 }
1537 }
1538
1539 if (!empty($items)) {
1540 return $items;
1541 }
1542
1543 return array(array(
1544 'content' => $html,
1545 'indentLevel' => 0,
1546 ));
1547 }
1548
1549 private function resolve_core_list_style($attrs) {
1550 if (!empty($attrs['ordered'])) {
1551 return 'decimal';
1552 }
1553
1554 $class_name = (string) $this->get_scalar($attrs, 'className', '');
1555
1556 if (strpos($class_name, 'is-style-circle') !== false) {
1557 return 'circle';
1558 }
1559
1560 if (strpos($class_name, 'is-style-square') !== false) {
1561 return 'square';
1562 }
1563
1564 if (strpos($class_name, 'no-bullet') !== false) {
1565 return 'none';
1566 }
1567
1568 return 'disc';
1569 }
1570
1571 private function resolve_element_alignment($align, $legacy_cell_attrs) {
1572 $mapped_align = $this->map_text_alignment($align, '');
1573 if ($mapped_align !== '') {
1574 return $mapped_align;
1575 }
1576
1577 $justify_content = $this->get_scalar($legacy_cell_attrs, 'justifyContent', 'left');
1578 if ($justify_content === 'right') {
1579 return 'right';
1580 }
1581 if ($justify_content === 'center') {
1582 return 'center';
1583 }
1584
1585 return 'left';
1586 }
1587
1588 private function extract_text_color($attrs, $fallback) {
1589 $style_color = $this->get_nested_value($attrs, array('style', 'color', 'text'), null);
1590 if (is_string($style_color) && $style_color !== '') {
1591 return $this->normalize_css_value($style_color);
1592 }
1593
1594 $text_color_slug = $this->get_scalar($attrs, 'textColor', null);
1595 if (is_string($text_color_slug) && $text_color_slug !== '') {
1596 return $this->preset_slug_to_css_var('color', $text_color_slug);
1597 }
1598
1599 return $fallback;
1600 }
1601
1602 private function extract_link_color($attrs, $fallback) {
1603 $link_color = $this->get_nested_value(
1604 $attrs,
1605 array('style', 'elements', 'link', 'color', 'text'),
1606 null
1607 );
1608 if (is_string($link_color) && $link_color !== '') {
1609 return $this->normalize_css_value($link_color);
1610 }
1611
1612 return $fallback;
1613 }
1614
1615 private function extract_background_color($attrs) {
1616 $background_color = $this->get_nested_value($attrs, array('style', 'color', 'background'), null);
1617 if (is_string($background_color) && $background_color !== '') {
1618 return $this->normalize_css_value($background_color);
1619 }
1620
1621 $background_slug = $this->get_scalar($attrs, 'backgroundColor', null);
1622 if (is_string($background_slug) && $background_slug !== '') {
1623 return $this->preset_slug_to_css_var('color', $background_slug);
1624 }
1625
1626 return '';
1627 }
1628
1629 private function extract_font_size($attrs, $fallback) {
1630 $font_size = $this->get_nested_value($attrs, array('style', 'typography', 'fontSize'), null);
1631 if (is_string($font_size) && $font_size !== '') {
1632 return $this->normalize_css_value($font_size);
1633 }
1634
1635 $font_size_slug = $this->get_scalar($attrs, 'fontSize', null);
1636 if (is_string($font_size_slug) && $font_size_slug !== '') {
1637 return $this->map_legacy_font_size_slug($font_size_slug);
1638 }
1639
1640 return $fallback;
1641 }
1642
1643 private function get_background_value($attrs) {
1644 $background = $this->normalize_css_value($this->get_scalar($attrs, 'background', ''));
1645 if ($background !== '') {
1646 return $background;
1647 }
1648
1649 return $this->extract_background_color($attrs);
1650 }
1651
1652 private function map_vertical_align($align) {
1653 if ($align === 'top') {
1654 return 'top';
1655 }
1656
1657 if ($align === 'bottom') {
1658 return 'bottom';
1659 }
1660
1661 return 'middle';
1662 }
1663
1664 private function map_text_alignment($align, $default) {
1665 if (in_array($align, array('left', 'center', 'right'), true)) {
1666 return $align;
1667 }
1668
1669 return $default;
1670 }
1671
1672 private function map_button_width($width) {
1673 if ($width === null || $width === '' || $width === 0 || $width === '0') {
1674 return 'auto';
1675 }
1676
1677 if (is_numeric($width)) {
1678 $width = (string) max(1, min(100, (int) $width)) . '%';
1679 }
1680
1681 if (!is_string($width)) {
1682 return 'auto';
1683 }
1684
1685 return $width;
1686 }
1687
1688 private function map_table_dimensions($legacy_table_width, $legacy_table_alignment) {
1689 $table_width = $this->normalize_css_value(
1690 is_string($legacy_table_width) ? $legacy_table_width : ''
1691 );
1692 $table_alignment = $this->map_text_alignment($legacy_table_alignment, 'left');
1693
1694 if ($table_width === '') {
1695 $table_width = in_array($legacy_table_alignment, array('wide', 'full'), true)
1696 ? $legacy_table_alignment
1697 : 'auto';
1698 }
1699
1700 if (!in_array($table_width, array('auto', 'wide', 'full'), true) && $table_alignment === '') {
1701 $table_alignment = 'left';
1702 }
1703
1704 return array(
1705 'tableWidth' => $table_width,
1706 'tableAlignment' => $table_alignment === '' ? 'left' : $table_alignment,
1707 );
1708 }
1709
1710 private function is_legacy_section_enabled($value) {
1711 return is_string($value) && $value !== '';
1712 }
1713
1714 private function is_header_cell($cell_attrs, $table_attrs) {
1715 return $this->is_legacy_section_enabled(
1716 $this->get_scalar($table_attrs, 'enableTableHeader', '')
1717 ) && (int) $this->get_scalar($cell_attrs, 'row', 0) === 0;
1718 }
1719
1720 private function is_footer_cell($cell_attrs, $table_attrs, $rows) {
1721 return $this->is_legacy_section_enabled(
1722 $this->get_scalar($table_attrs, 'enableTableFooter', '')
1723 ) && (int) $this->get_scalar($cell_attrs, 'row', 0) + 1 === $rows;
1724 }
1725
1726 private function is_odd_data_row($row, $table_attrs) {
1727 $has_header = $this->is_legacy_section_enabled(
1728 $this->get_scalar($table_attrs, 'enableTableHeader', '')
1729 );
1730
1731 return (($row + ($has_header ? 0 : 1)) % 2) === 1;
1732 }
1733
1734 private function pick_spacing_side($spacing, $sides, $default) {
1735 if (!is_array($spacing)) {
1736 return $default;
1737 }
1738
1739 foreach ($sides as $side) {
1740 if (isset($spacing[$side]) && $spacing[$side] !== '') {
1741 return $this->normalize_css_value($spacing[$side]);
1742 }
1743 }
1744
1745 return $default;
1746 }
1747
1748 private function normalize_spacing($spacing) {
1749 $normalized = array(
1750 'top' => '',
1751 'right' => '',
1752 'bottom' => '',
1753 'left' => '',
1754 );
1755
1756 if (is_string($spacing) && $spacing !== '') {
1757 $spacing = array(
1758 'top' => $spacing,
1759 'right' => $spacing,
1760 'bottom' => $spacing,
1761 'left' => $spacing,
1762 );
1763 }
1764
1765 if (!is_array($spacing)) {
1766 return $normalized;
1767 }
1768
1769 foreach ($normalized as $side => $value) {
1770 if (isset($spacing[$side])) {
1771 $normalized[$side] = $this->normalize_css_value($spacing[$side]);
1772 }
1773 }
1774
1775 return $normalized;
1776 }
1777
1778 private function normalize_border_sides($border) {
1779 $normalized = $this->get_empty_border_sides();
1780
1781 if (is_string($border) && $border !== '') {
1782 foreach (array_keys($normalized) as $side) {
1783 $normalized[$side] = $border;
1784 }
1785 return $normalized;
1786 }
1787
1788 if (!is_array($border)) {
1789 return $normalized;
1790 }
1791
1792 $has_split_sides = false;
1793 foreach (array_keys($normalized) as $side) {
1794 if (isset($border[$side])) {
1795 $has_split_sides = true;
1796 break;
1797 }
1798 }
1799
1800 if ($has_split_sides) {
1801 foreach (array_keys($normalized) as $side) {
1802 $normalized[$side] = $this->normalize_single_border(
1803 isset($border[$side]) ? $border[$side] : null
1804 );
1805 }
1806 return $normalized;
1807 }
1808
1809 $single_border = $this->normalize_single_border($border);
1810 foreach (array_keys($normalized) as $side) {
1811 $normalized[$side] = $single_border;
1812 }
1813
1814 return $normalized;
1815 }
1816
1817 private function normalize_single_border($border) {
1818 if (is_string($border)) {
1819 return $border;
1820 }
1821
1822 if (!is_array($border)) {
1823 return '';
1824 }
1825
1826 $width = isset($border['width']) ? trim((string) $border['width']) : '';
1827 if ($width === '') {
1828 return '';
1829 }
1830
1831 $style = isset($border['style']) ? trim((string) $border['style']) : '';
1832 $color = isset($border['color']) ? trim((string) $border['color']) : '';
1833
1834 if ($style === '') {
1835 $style = 'solid';
1836 }
1837
1838 return trim($width . ' ' . $style . ' ' . $color);
1839 }
1840
1841 private function normalize_border_radius($radius) {
1842 $normalized = $this->get_empty_border_radius();
1843
1844 if (is_string($radius) && $radius !== '') {
1845 foreach (array_keys($normalized) as $corner) {
1846 $normalized[$corner] = $radius;
1847 }
1848 return $normalized;
1849 }
1850
1851 if (!is_array($radius)) {
1852 return $normalized;
1853 }
1854
1855 foreach (array_keys($normalized) as $corner) {
1856 if (isset($radius[$corner])) {
1857 $normalized[$corner] = $this->normalize_css_value($radius[$corner]);
1858 }
1859 }
1860
1861 return $normalized;
1862 }
1863
1864 private function migrate_legacy_icon_payload($icon) {
1865 if (!is_array($icon)) {
1866 return null;
1867 }
1868
1869 $migrated_icon = array(
1870 'iconName' => (string) $this->get_scalar($icon, 'iconName', ''),
1871 );
1872
1873 $url = $this->get_scalar($icon, 'url', null);
1874 if (is_string($url) && $url !== '') {
1875 $migrated_icon['url'] = $url;
1876 }
1877
1878 $svg = $this->extract_legacy_icon_svg($icon);
1879 if ($svg !== null) {
1880 $migrated_icon['svg'] = $svg;
1881 }
1882
1883 if (
1884 $migrated_icon['iconName'] === '' &&
1885 !isset($migrated_icon['svg']) &&
1886 !isset($migrated_icon['url'])
1887 ) {
1888 return null;
1889 }
1890
1891 return $migrated_icon;
1892 }
1893
1894 private function get_default_legacy_icon_payload() {
1895 return array(
1896 'iconName' => 'check',
1897 'svg' => array(
1898 'viewBox' => '0 0 512 512',
1899 'path' => 'M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z',
1900 ),
1901 );
1902 }
1903
1904 private function extract_legacy_icon_svg($icon) {
1905 $svg = $this->get_array($icon, 'svg', array());
1906 if (
1907 isset($svg['viewBox']) &&
1908 is_string($svg['viewBox']) &&
1909 isset($svg['path']) &&
1910 is_string($svg['path'])
1911 ) {
1912 return array(
1913 'viewBox' => $svg['viewBox'],
1914 'path' => $svg['path'],
1915 );
1916 }
1917
1918 $legacy_svg = $this->get_nested_value($icon, array('icon', 'props'), null);
1919 if (!is_array($legacy_svg)) {
1920 return null;
1921 }
1922
1923 $path = $this->get_nested_value($legacy_svg, array('children', 'props', 'd'), null);
1924 $view_box = $this->get_scalar($legacy_svg, 'viewBox', null);
1925
1926 if (!is_string($path) || $path === '' || !is_string($view_box) || $view_box === '') {
1927 return null;
1928 }
1929
1930 return array(
1931 'viewBox' => $view_box,
1932 'path' => $path,
1933 );
1934 }
1935
1936 private function extract_block_html($block) {
1937 if (!is_array($block)) {
1938 return '';
1939 }
1940
1941 if (isset($block['innerHTML']) && is_string($block['innerHTML'])) {
1942 return trim($block['innerHTML']);
1943 }
1944
1945 if (!isset($block['innerContent']) || !is_array($block['innerContent'])) {
1946 return '';
1947 }
1948
1949 $html = '';
1950 foreach ($block['innerContent'] as $content) {
1951 if (is_string($content)) {
1952 $html .= $content;
1953 }
1954 }
1955
1956 return trim($html);
1957 }
1958
1959 private function extract_block_html_from_attrs($attrs) {
1960 $html = $this->get_scalar($attrs, 'originalContent', null);
1961 if (is_string($html) && trim($html) !== '') {
1962 return trim($html);
1963 }
1964
1965 $html = $this->get_scalar($attrs, '__unstableOriginalContent', null);
1966 if (is_string($html) && trim($html) !== '') {
1967 return trim($html);
1968 }
1969
1970 return '';
1971 }
1972
1973 private function extract_first_tag_inner_html($html, $tag_name, $excluded_tags = array()) {
1974 if (!is_string($html) || trim($html) === '') {
1975 return null;
1976 }
1977
1978 $pattern = '/<' . preg_quote($tag_name, '/') . '\b[^>]*>(.*)<\/' . preg_quote($tag_name, '/') . '>/is';
1979 if (preg_match($pattern, $html, $matches)) {
1980 return trim($this->strip_tag_markup($matches[1], $excluded_tags));
1981 }
1982
1983 return null;
1984 }
1985
1986 private function extract_first_tag_style_value($html, $tag_name, $css_property) {
1987 if (!is_string($html) || trim($html) === '') {
1988 return null;
1989 }
1990
1991 $pattern = '/<' . preg_quote($tag_name, '/') . '\b[^>]*style=["\']([^"\']*)["\'][^>]*>/i';
1992 if (preg_match($pattern, $html, $matches)) {
1993 return $this->extract_style_declaration_value($matches[1], $css_property);
1994 }
1995
1996 return null;
1997 }
1998
1999 private function extract_style_declaration_value($style, $css_property) {
2000 if (!is_string($style) || trim($style) === '') {
2001 return null;
2002 }
2003
2004 $pattern = '/(?:^|;)\s*' . preg_quote($css_property, '/') . '\s*:\s*([^;]+)\s*(?:;|$)/i';
2005 if (!preg_match($pattern, $style, $matches)) {
2006 return null;
2007 }
2008
2009 return trim($matches[1]);
2010 }
2011
2012 private function extract_first_tag_class_font_size_slug($html, $tag_name) {
2013 if (!is_string($html) || trim($html) === '') {
2014 return null;
2015 }
2016
2017 $class_name = null;
2018 $pattern = '/<' . preg_quote($tag_name, '/') . '\b[^>]*class=["\']([^"\']*)["\'][^>]*>/i';
2019 if (preg_match($pattern, $html, $matches)) {
2020 $class_name = $matches[1];
2021 }
2022
2023 if (!is_string($class_name) || $class_name === '') {
2024 return null;
2025 }
2026
2027 if (preg_match('/(?:^|\s)has-([a-z0-9-]+)-font-size(?:\s|$)/i', $class_name, $matches)) {
2028 return strtolower($matches[1]);
2029 }
2030
2031 return null;
2032 }
2033
2034 private function strip_nested_list_markup($html) {
2035 return preg_replace('/<(ul|ol)\b[^>]*>.*?<\/\1>/is', '', $html);
2036 }
2037
2038 private function strip_tag_markup($html, $tag_names = array()) {
2039 if (!is_string($html) || empty($tag_names)) {
2040 return $html;
2041 }
2042
2043 foreach ($tag_names as $tag_name) {
2044 $html = preg_replace(
2045 '/<' . preg_quote($tag_name, '/') . '\b[^>]*>.*?<\/' . preg_quote($tag_name, '/') . '>/is',
2046 '',
2047 $html
2048 );
2049 }
2050
2051 return $html;
2052 }
2053
2054 private function normalize_css_value($value) {
2055 if (!is_string($value)) {
2056 return '';
2057 }
2058
2059 $value = trim($value);
2060 if ($value === '') {
2061 return '';
2062 }
2063
2064 if (preg_match('/^var:preset\|([^|]+)\|(.+)$/', $value, $matches)) {
2065 if ($matches[1] === 'spacing' && $matches[2] === '10') {
2066 return 'var(--wp--preset--spacing--20)';
2067 }
2068
2069 return 'var(--wp--preset--' . $matches[1] . '--' . $matches[2] . ')';
2070 }
2071
2072 return $value;
2073 }
2074
2075 private function preset_slug_to_css_var($group, $slug) {
2076 return 'var(--wp--preset--' . $group . '--' . $slug . ')';
2077 }
2078
2079 private function map_legacy_font_size_slug($slug) {
2080 $built_in_font_sizes = array(
2081 's' => '0.75rem',
2082 'small' => '0.75rem',
2083 'm' => '0.875rem',
2084 'normal' => '0.875rem',
2085 'medium' => '0.875rem',
2086 'l' => '1rem',
2087 'large' => '1rem',
2088 'xl' => '1.38rem',
2089 'x-large' => '1.38rem',
2090 'xlarge' => '1.38rem',
2091 'xxl' => '1.75rem',
2092 'xx-large' => '1.75rem',
2093 'xxlarge' => '1.75rem',
2094 'huge' => '1.75rem',
2095 );
2096
2097 $slug = strtolower((string) $slug);
2098
2099 if (isset($built_in_font_sizes[$slug])) {
2100 return $built_in_font_sizes[$slug];
2101 }
2102
2103 return $this->preset_slug_to_css_var('font-size', $slug);
2104 }
2105
2106 private function has_any_non_empty_value($values) {
2107 if (!is_array($values)) {
2108 return false;
2109 }
2110
2111 foreach ($values as $value) {
2112 if (is_string($value) && trim($value) !== '') {
2113 return true;
2114 }
2115 }
2116
2117 return false;
2118 }
2119
2120 private function create_synthetic_cells($rows, $cols) {
2121 $cells = array();
2122
2123 for ($row = 0; $row < $rows; $row++) {
2124 for ($col = 0; $col < $cols; $col++) {
2125 $cells[] = array(
2126 'blockName' => 'tableberg/cell',
2127 'attrs' => array(
2128 'row' => $row,
2129 'col' => $col,
2130 'rowspan' => 1,
2131 'colspan' => 1,
2132 ),
2133 'innerBlocks' => array(),
2134 );
2135 }
2136 }
2137
2138 return $cells;
2139 }
2140
2141 private function get_legacy_cell_blocks($block) {
2142 $cells = array();
2143
2144 foreach ($this->get_block_inner_blocks($block) as $inner_block) {
2145 if ($this->get_block_name($inner_block) !== 'tableberg/cell') {
2146 continue;
2147 }
2148
2149 $cells[] = $inner_block;
2150 }
2151
2152 return $cells;
2153 }
2154
2155 private function get_block_name($block) {
2156 if (!is_array($block) || !isset($block['blockName']) || !is_string($block['blockName'])) {
2157 return '';
2158 }
2159
2160 return $block['blockName'];
2161 }
2162
2163 private function get_block_attrs($block) {
2164 if (!is_array($block) || !isset($block['attrs']) || !is_array($block['attrs'])) {
2165 return array();
2166 }
2167
2168 return $block['attrs'];
2169 }
2170
2171 private function get_block_inner_blocks($block) {
2172 if (!is_array($block) || !isset($block['innerBlocks']) || !is_array($block['innerBlocks'])) {
2173 return array();
2174 }
2175
2176 return $block['innerBlocks'];
2177 }
2178
2179 private function get_array($array, $key, $default) {
2180 if (!is_array($array) || !isset($array[$key]) || !is_array($array[$key])) {
2181 return $default;
2182 }
2183
2184 return $array[$key];
2185 }
2186
2187 private function get_scalar($array, $key, $default) {
2188 if (!is_array($array) || !array_key_exists($key, $array)) {
2189 return $default;
2190 }
2191
2192 return $array[$key];
2193 }
2194
2195 private function get_nested_value($array, $keys, $default) {
2196 if (!is_array($array)) {
2197 return $default;
2198 }
2199
2200 $current = $array;
2201 foreach ($keys as $key) {
2202 if (!is_array($current) || !array_key_exists($key, $current)) {
2203 return $default;
2204 }
2205
2206 $current = $current[$key];
2207 }
2208
2209 return $current;
2210 }
2211
2212 private function get_empty_border_sides() {
2213 return array(
2214 'top' => '',
2215 'right' => '',
2216 'bottom' => '',
2217 'left' => '',
2218 );
2219 }
2220
2221 private function get_empty_border_radius() {
2222 return array(
2223 'topLeft' => '',
2224 'topRight' => '',
2225 'bottomRight' => '',
2226 'bottomLeft' => '',
2227 );
2228 }
2229 }
2230