PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.2.0
Tableberg – Simple Gutenberg Table Block v1.2.0
1.2.0 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 All 43 releases
← All changes | renderer/Table/TableAttrs.php +160 -140 1.0.1 → 1.2.0 View file →
@@ -35,8 +35,18 @@
35 35 /** @var CellDefaults */
36 36 public $cellDefaults;
37 37
38 38 /**
39 + * Every attribute the table block carried, untouched. Pro registers
40 + * table-level attributes of its own, so its renderer reads them from
41 + * here rather than free having to know their names — mirrors
42 + * CellData->attrs.
43 + *
44 + * @var array<string, mixed>
45 + */
46 + public $attrs;
47 +
48 + /**
39 49 * @param array $data
40 50 * @return self
41 51 */
42 52 public static function from_array($data) {
@@ -43,8 +53,9 @@
43 53 $data = is_array($data) ? $data : [];
44 54 $defaults = Defaults::get_defaults();
45 55
46 56 $instance = new self();
57 + $instance->attrs = $data;
47 58 $instance->version = new NumberAttr(
48 59 getOrNull($data['version']),
49 60 $defaults['version']
50 61 );
@@ -96,24 +107,8 @@
96 107
97 108 return ['cells' => []];
98 109 }
99 110
100 - /** @return array<int, array{array{int, int}, array<int, mixed>}> */
101 - public static function get_data_defaults() {
102 - $defaults = Defaults::get_defaults();
103 -
104 - if (
105 - isset($defaults['data']) &&
106 - is_array($defaults['data']) &&
107 - isset($defaults['data']['cells']) &&
108 - is_array($defaults['data']['cells'])
109 - ) {
110 - return $defaults['data']['cells'];
111 - }
112 -
113 - return [];
114 - }
115 -
116 111 /** @return array<int, RowConfig> */
117 112 private static function parse_row_configs($data) {
118 113 if (!is_array($data)) {
119 114 return [];
@@ -136,13 +131,9 @@
136 131
137 132 $columns = [];
138 133
139 134 foreach ($data as $column => $column_config) {
140 - $columns[(int) $column] = ColumnConfig::from_array(
141 - $column_config,
142 - 'text',
143 - true
144 - );
135 + $columns[(int) $column] = ColumnConfig::from_array($column_config);
145 136 }
146 137
147 138 return $columns;
148 139 }
@@ -185,52 +176,8 @@
185 176 return $bindings;
186 177 }
187 178 }
188 179
189 -class Data {
190 - /** @var array<int, array{array{int, int}, array<int, CellElement>}> */
191 - public $cells;
192 -
193 - /** @return self */
194 - public static function from_array($data) {
195 - $data = is_array($data) ? $data : [];
196 - $default_cells = TableAttrs::get_data_defaults();
197 -
198 - $instance = new self();
199 - $instance->cells = self::parse_cells(getOrNull($data['cells']), $default_cells);
200 -
201 - return $instance;
202 - }
203 -
204 - /** @return array<int, array{array{int, int}, array<int, CellElement>}> */
205 - private static function parse_cells($data, $default) {
206 - if (!is_array($data)) {
207 - return $default;
208 - }
209 -
210 - $cells = [];
211 -
212 - foreach ($data as $cell) {
213 - if (!is_array($cell[0])) {
214 - continue;
215 - }
216 -
217 - $coords = [(int) $cell[0][0], (int) $cell[0][1]];
218 - $elements = [];
219 -
220 - if (is_array($cell[1])) {
221 - foreach ($cell[1] as $element) {
222 - $elements[] = CellElement::from_array($element);
223 - }
224 - }
225 -
226 - $cells[] = [$coords, $elements];
227 - }
228 -
229 - return $cells;
230 - }
231 -}
232 -
233 180 class PaginationConfig {
234 181 /** @var BoolAttr */
235 182 public $enabled;
236 183
@@ -260,39 +207,8 @@
260 207 return $instance;
261 208 }
262 209 }
263 210
264 -class SearchConfig {
265 - /** @var BoolAttr */
266 - public $enabled;
267 -
268 - /** @var StringAttr */
269 - public $placeholder;
270 -
271 - /** @var StringAttr */
272 - public $highlightColor;
273 -
274 - /** @var StringAttr */
275 - public $position;
276 -
277 - /** @return self */
278 - public static function from_array($data) {
279 - $data = is_array($data) ? $data : [];
280 - $table_defaults = TableAttrs::get_table_defaults();
281 - $d = isset($table_defaults['search']) && is_array($table_defaults['search'])
282 - ? $table_defaults['search']
283 - : [];
284 -
285 - $instance = new self();
286 - $instance->enabled = new BoolAttr(getOrNull($data['enabled']), $d['enabled']);
287 - $instance->placeholder = new StringAttr(getOrNull($data['placeholder']), $d['placeholder']);
288 - $instance->highlightColor = new StringAttr(getOrNull($data['highlightColor']), $d['highlightColor']);
289 - $instance->position = new StringAttr(getOrNull($data['position']), $d['position'], ['left', 'right']);
290 -
291 - return $instance;
292 - }
293 -}
294 -
295 211 class ResponsiveBreakpointConfig {
296 212 /** @var BoolAttr */
297 213 public $enabled;
298 214
@@ -444,13 +360,23 @@
444 360
445 361 /** @var array<string, mixed>|null */
446 362 public $ribbon;
447 363
364 + /**
365 + * Every attribute the cell block carried, untouched. Pro registers cell
366 + * attributes of its own, so its renderer reads them from here rather
367 + * than free having to know their names.
368 + *
369 + * @var array<string, mixed>
370 + */
371 + public $attrs;
372 +
448 373 /** @return self */
449 374 public static function from_array($data) {
450 375 $data = is_array($data) ? $data : [];
451 376
452 377 $instance = new self();
378 + $instance->attrs = $data;
453 379 $instance->span = Span::from_array(getOrNull($data['span']));
454 380 $instance->className = new StringAttr(getOrNull($data['className']), '');
455 381 $instance->elements = [];
456 382
@@ -486,11 +412,8 @@
486 412
487 413 /** @var BoolAttr */
488 414 public $footerEnabled;
489 415
490 - /** @var BoolAttr */
491 - public $stickyHeader;
492 -
493 416 /** @var StringAttr */
494 417 public $caption;
495 418
496 419 /** @var StringAttr */
@@ -504,8 +427,17 @@
504 427
505 428 /** @var Sides */
506 429 public $tableBorder;
507 430
431 + /** @var TypographyConfig */
432 + public $typography;
433 +
434 + /** @var Sides */
435 + public $margin;
436 +
437 + /** @var Sides */
438 + public $padding;
439 +
508 440 /** @var BoolAttr */
509 441 public $fixedColumnWidths;
510 442
511 443 /** @var PaginationConfig */
@@ -510,11 +442,8 @@
510 442
511 443 /** @var PaginationConfig */
512 444 public $pagination;
513 445
514 - /** @var SearchConfig */
515 - public $search;
516 -
517 446 /** @var ResponsiveConfig */
518 447 public $responsive;
519 448
520 449 /** @return self */
@@ -527,9 +456,8 @@
527 456 $instance->cols = new NumberAttr(getOrNull($data['cols']), $d['cols']);
528 457 $instance->className = new StringAttr(getOrNull($data['className']), '');
529 458 $instance->headerEnabled = new BoolAttr(getOrNull($data['headerEnabled']), $d['headerEnabled']);
530 459 $instance->footerEnabled = new BoolAttr(getOrNull($data['footerEnabled']), $d['footerEnabled']);
531 - $instance->stickyHeader = new BoolAttr(getOrNull($data['stickyHeader']), $d['stickyHeader']);
532 460 $instance->caption = new StringAttr(getOrNull($data['caption']), $d['caption']);
533 461 $instance->tableWidth = new StringAttr(
534 462 getOrNull($data['tableWidth']),
535 463 $d['tableWidth']
@@ -543,14 +471,25 @@
543 471 $instance->tableBorder = Sides::from_array(
544 472 getOrNull($data['tableBorder']),
545 473 $d['tableBorder']
546 474 );
475 + $instance->typography = TypographyConfig::from_array(
476 + getOrNull($data['typography'])
477 + );
478 + $emptySides = ['top' => '', 'right' => '', 'bottom' => '', 'left' => ''];
479 + $instance->margin = Sides::from_array(
480 + getOrNull($data['margin']),
481 + isset($d['margin']) && is_array($d['margin']) ? $d['margin'] : $emptySides
482 + );
483 + $instance->padding = Sides::from_array(
484 + getOrNull($data['padding']),
485 + isset($d['padding']) && is_array($d['padding']) ? $d['padding'] : $emptySides
486 + );
547 487 $instance->fixedColumnWidths = new BoolAttr(
548 488 getOrNull($data['fixedColumnWidths']),
549 489 $d['fixedColumnWidths']
550 490 );
551 491 $instance->pagination = PaginationConfig::from_array(getOrNull($data['pagination']));
552 - $instance->search = SearchConfig::from_array(getOrNull($data['search']));
553 492 $instance->responsive = ResponsiveConfig::from_array(getOrNull($data['responsive']));
554 493
555 494 return $instance;
556 495 }
@@ -555,27 +494,122 @@
555 494 return $instance;
556 495 }
557 496 }
558 497
498 +/**
499 + * Table-wide typography: the same options the core Table block offers, set on
500 + * the table element so the cells inherit them.
501 + */
502 +class TypographyConfig {
503 + /** @var StringAttr */
504 + public $fontFamily;
505 +
506 + /** @var StringAttr */
507 + public $fontSize;
508 +
509 + /** @var StringAttr */
510 + public $fontStyle;
511 +
512 + /** @var StringAttr */
513 + public $fontWeight;
514 +
515 + /** @var StringAttr */
516 + public $lineHeight;
517 +
518 + /** @var StringAttr */
519 + public $letterSpacing;
520 +
521 + /** @var StringAttr */
522 + public $textDecoration;
523 +
524 + /** @var StringAttr */
525 + public $textTransform;
526 +
527 + /** @return self */
528 + public static function from_array($data) {
529 + $data = is_array($data) ? $data : [];
530 + $tableDefaults = TableAttrs::get_table_defaults();
531 + $d = isset($tableDefaults['typography']) && is_array($tableDefaults['typography'])
532 + ? $tableDefaults['typography']
533 + : [];
534 +
535 + $instance = new self();
536 + foreach (
537 + [
538 + 'fontFamily',
539 + 'fontSize',
540 + 'fontStyle',
541 + 'fontWeight',
542 + 'lineHeight',
543 + 'letterSpacing',
544 + 'textDecoration',
545 + 'textTransform',
546 + ] as $property
547 + ) {
548 + $instance->{$property} = new StringAttr(
549 + getOrNull($data[$property]),
550 + isset($d[$property]) ? $d[$property] : ''
551 + );
552 + }
553 +
554 + return $instance;
555 + }
556 +
557 + /**
558 + * The typography as `property: value` declarations, skipping everything
559 + * the user has not set so the theme keeps deciding.
560 + *
561 + * @return array<int, string>
562 + */
563 + public function asStyles() {
564 + $properties = [
565 + 'font-family' => $this->fontFamily,
566 + 'font-size' => $this->fontSize,
567 + 'font-style' => $this->fontStyle,
568 + 'font-weight' => $this->fontWeight,
569 + 'line-height' => $this->lineHeight,
570 + 'letter-spacing' => $this->letterSpacing,
571 + 'text-decoration' => $this->textDecoration,
572 + 'text-transform' => $this->textTransform,
573 + ];
574 +
575 + $styles = [];
576 +
577 + foreach ($properties as $property => $attr) {
578 + if ($attr->isNotEmpty()) {
579 + $styles[] = $property . ': ' . $attr->asAttr();
580 + }
581 + }
582 +
583 + return $styles;
584 + }
585 +}
586 +
559 587 class CellStyles {
560 588 /** @var Sides */
561 589 public $padding;
562 590
563 591 /** @var StringAttr */
564 - public $orientation;
592 + public $elementGap;
565 593
566 594 /** @var StringAttr */
567 - public $elementGap;
595 + public $verticalAlign;
568 596
569 597 /** @var StringAttr */
570 - public $wrap;
598 + public $backgroundColor;
571 599
572 600 /** @var StringAttr */
573 - public $verticalAlign;
601 + public $headerBackgroundColor;
574 602
575 603 /** @var StringAttr */
576 - public $backgroundColor;
604 + public $footerBackgroundColor;
577 605
606 + /** @var StringAttr */
607 + public $evenRowBackgroundColor;
608 +
609 + /** @var StringAttr */
610 + public $oddRowBackgroundColor;
611 +
578 612 /** @var Sides */
579 613 public $border;
580 614
581 615 /** @var Corners */
@@ -590,22 +624,12 @@
590 624 $d = TableAttrs::get_cell_style_defaults();
591 625
592 626 $instance = new self();
593 627 $instance->padding = Sides::from_array(getOrNull($data['padding']), $d['padding']);
594 - $instance->orientation = new StringAttr(
595 - getOrNull($data['orientation']),
596 - $d['orientation'],
597 - ['vertical', 'horizontal']
598 - );
599 628 $instance->elementGap = new StringAttr(
600 629 getOrNull($data['elementGap']),
601 630 $d['elementGap']
602 631 );
603 - $instance->wrap = new StringAttr(
604 - getOrNull($data['wrap']),
605 - $d['wrap'],
606 - ['wrap', 'nowrap']
607 - );
608 632 $instance->verticalAlign = new StringAttr(
609 633 getOrNull($data['verticalAlign']),
610 634 $d['verticalAlign'],
611 635 ['top', 'middle', 'bottom']
@@ -610,8 +634,12 @@
610 634 $d['verticalAlign'],
611 635 ['top', 'middle', 'bottom']
612 636 );
613 637 $instance->backgroundColor = new StringAttr(getOrNull($data['backgroundColor']), $d['backgroundColor']);
638 + $instance->headerBackgroundColor = new StringAttr(getOrNull($data['headerBackgroundColor']), $d['headerBackgroundColor']);
639 + $instance->footerBackgroundColor = new StringAttr(getOrNull($data['footerBackgroundColor']), $d['footerBackgroundColor']);
640 + $instance->evenRowBackgroundColor = new StringAttr(getOrNull($data['evenRowBackgroundColor']), $d['evenRowBackgroundColor']);
641 + $instance->oddRowBackgroundColor = new StringAttr(getOrNull($data['oddRowBackgroundColor']), $d['oddRowBackgroundColor']);
614 642 $instance->border = Sides::from_array(getOrNull($data['border']), $d['border']);
615 643 $instance->borderRadius = Corners::from_array(getOrNull($data['borderRadius']), $d['borderRadius']);
616 644 $instance->cells = is_array(getOrNull($data['cells'])) ? $data['cells'] : $d['cells'];
617 645
@@ -646,36 +674,17 @@
646 674 }
647 675
648 676 class ColumnConfig {
649 677 /** @var StringAttr|null */
650 - public $sortable;
651 -
652 - /** @var StringAttr|null */
653 678 public $width;
654 679
655 - /**
656 - * @param array|null $data
657 - * @param string $defaultSortable
658 - * @param bool $allowNull
659 - * @return self
660 - */
661 - public static function from_array($data, $defaultSortable = 'text', $allowNull = false) {
680 + /** @return self */
681 + public static function from_array($data) {
662 682 $data = is_array($data) ? $data : [];
663 - $sortable = getOrNull($data['sortable']);
664 683 $width = getOrNull($data['width']);
665 684
666 685 $instance = new self();
667 686
668 - if ($allowNull && ($sortable === null || $sortable === '')) {
669 - $instance->sortable = null;
670 - } else {
671 - $instance->sortable = new StringAttr(
672 - $sortable,
673 - $defaultSortable,
674 - ['text', 'number', 'date']
675 - );
676 - }
677 -
678 687 if ($width === null || $width === '') {
679 688 $instance->width = null;
680 689 } else {
681 690 $instance->width = new StringAttr($width, '');
@@ -689,8 +698,17 @@
689 698 /** @var StringAttr|null */
690 699 public $height;
691 700
692 701 /**
702 + * Every attribute the row block carried, untouched. Pro registers row
703 + * attributes of its own (e.g. backgroundColor), so its renderer reads
704 + * them from here rather than free having to know their names.
705 + *
706 + * @var array<string, mixed>
707 + */
708 + public $attrs;
709 +
710 + /**
693 711 * @param array|null $data
694 712 * @return self
695 713 */
696 714 public static function from_array($data) {
@@ -697,8 +715,9 @@
697 715 $data = is_array($data) ? $data : [];
698 716 $height = getOrNull($data['height']);
699 717
700 718 $instance = new self();
719 + $instance->attrs = $data;
701 720
702 721 if ($height === null || $height === '') {
703 722 $instance->height = null;
704 723 } else {
@@ -736,8 +755,9 @@
736 755 'list',
737 756 'icon',
738 757 'star-rating',
739 758 'custom-html',
759 + 'styled-list',
740 760 ];
741 761 }
742 762
743 763 $instance = new self();