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 +144 -147 1.1.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,14 +412,8 @@
486 412
487 413 /** @var BoolAttr */
488 414 public $footerEnabled;
489 415
490 - /** @var BoolAttr */
491 - public $stickyHeader;
492 -
493 - /** @var BoolAttr */
494 - public $stickyFirstCol;
495 -
496 416 /** @var StringAttr */
497 417 public $caption;
498 418
499 419 /** @var StringAttr */
@@ -507,8 +427,11 @@
507 427
508 428 /** @var Sides */
509 429 public $tableBorder;
510 430
431 + /** @var TypographyConfig */
432 + public $typography;
433 +
511 434 /** @var Sides */
512 435 public $margin;
513 436
514 437 /** @var Sides */
@@ -519,11 +442,8 @@
519 442
520 443 /** @var PaginationConfig */
521 444 public $pagination;
522 445
523 - /** @var SearchConfig */
524 - public $search;
525 -
526 446 /** @var ResponsiveConfig */
527 447 public $responsive;
528 448
529 449 /** @return self */
@@ -536,13 +456,8 @@
536 456 $instance->cols = new NumberAttr(getOrNull($data['cols']), $d['cols']);
537 457 $instance->className = new StringAttr(getOrNull($data['className']), '');
538 458 $instance->headerEnabled = new BoolAttr(getOrNull($data['headerEnabled']), $d['headerEnabled']);
539 459 $instance->footerEnabled = new BoolAttr(getOrNull($data['footerEnabled']), $d['footerEnabled']);
540 - $instance->stickyHeader = new BoolAttr(getOrNull($data['stickyHeader']), $d['stickyHeader']);
541 - $instance->stickyFirstCol = new BoolAttr(
542 - getOrNull($data['stickyFirstCol']),
543 - getOrNull($d['stickyFirstCol']) ?? false
544 - );
545 460 $instance->caption = new StringAttr(getOrNull($data['caption']), $d['caption']);
546 461 $instance->tableWidth = new StringAttr(
547 462 getOrNull($data['tableWidth']),
548 463 $d['tableWidth']
@@ -556,8 +471,11 @@
556 471 $instance->tableBorder = Sides::from_array(
557 472 getOrNull($data['tableBorder']),
558 473 $d['tableBorder']
559 474 );
475 + $instance->typography = TypographyConfig::from_array(
476 + getOrNull($data['typography'])
477 + );
560 478 $emptySides = ['top' => '', 'right' => '', 'bottom' => '', 'left' => ''];
561 479 $instance->margin = Sides::from_array(
562 480 getOrNull($data['margin']),
563 481 isset($d['margin']) && is_array($d['margin']) ? $d['margin'] : $emptySides
@@ -570,9 +488,8 @@
570 488 getOrNull($data['fixedColumnWidths']),
571 489 $d['fixedColumnWidths']
572 490 );
573 491 $instance->pagination = PaginationConfig::from_array(getOrNull($data['pagination']));
574 - $instance->search = SearchConfig::from_array(getOrNull($data['search']));
575 492 $instance->responsive = ResponsiveConfig::from_array(getOrNull($data['responsive']));
576 493
577 494 return $instance;
578 495 }
@@ -577,27 +494,122 @@
577 494 return $instance;
578 495 }
579 496 }
580 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 +
581 587 class CellStyles {
582 588 /** @var Sides */
583 589 public $padding;
584 590
585 591 /** @var StringAttr */
586 - public $orientation;
592 + public $elementGap;
587 593
588 594 /** @var StringAttr */
589 - public $elementGap;
595 + public $verticalAlign;
590 596
591 597 /** @var StringAttr */
592 - public $wrap;
598 + public $backgroundColor;
593 599
594 600 /** @var StringAttr */
595 - public $verticalAlign;
601 + public $headerBackgroundColor;
596 602
597 603 /** @var StringAttr */
598 - public $backgroundColor;
604 + public $footerBackgroundColor;
599 605
606 + /** @var StringAttr */
607 + public $evenRowBackgroundColor;
608 +
609 + /** @var StringAttr */
610 + public $oddRowBackgroundColor;
611 +
600 612 /** @var Sides */
601 613 public $border;
602 614
603 615 /** @var Corners */
@@ -612,22 +624,12 @@
612 624 $d = TableAttrs::get_cell_style_defaults();
613 625
614 626 $instance = new self();
615 627 $instance->padding = Sides::from_array(getOrNull($data['padding']), $d['padding']);
616 - $instance->orientation = new StringAttr(
617 - getOrNull($data['orientation']),
618 - $d['orientation'],
619 - ['vertical', 'horizontal']
620 - );
621 628 $instance->elementGap = new StringAttr(
622 629 getOrNull($data['elementGap']),
623 630 $d['elementGap']
624 631 );
625 - $instance->wrap = new StringAttr(
626 - getOrNull($data['wrap']),
627 - $d['wrap'],
628 - ['wrap', 'nowrap']
629 - );
630 632 $instance->verticalAlign = new StringAttr(
631 633 getOrNull($data['verticalAlign']),
632 634 $d['verticalAlign'],
633 635 ['top', 'middle', 'bottom']
@@ -632,8 +634,12 @@
632 634 $d['verticalAlign'],
633 635 ['top', 'middle', 'bottom']
634 636 );
635 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']);
636 642 $instance->border = Sides::from_array(getOrNull($data['border']), $d['border']);
637 643 $instance->borderRadius = Corners::from_array(getOrNull($data['borderRadius']), $d['borderRadius']);
638 644 $instance->cells = is_array(getOrNull($data['cells'])) ? $data['cells'] : $d['cells'];
639 645
@@ -668,36 +674,17 @@
668 674 }
669 675
670 676 class ColumnConfig {
671 677 /** @var StringAttr|null */
672 - public $sortable;
673 -
674 - /** @var StringAttr|null */
675 678 public $width;
676 679
677 - /**
678 - * @param array|null $data
679 - * @param string $defaultSortable
680 - * @param bool $allowNull
681 - * @return self
682 - */
683 - public static function from_array($data, $defaultSortable = 'text', $allowNull = false) {
680 + /** @return self */
681 + public static function from_array($data) {
684 682 $data = is_array($data) ? $data : [];
685 - $sortable = getOrNull($data['sortable']);
686 683 $width = getOrNull($data['width']);
687 684
688 685 $instance = new self();
689 686
690 - if ($allowNull && ($sortable === null || $sortable === '')) {
691 - $instance->sortable = null;
692 - } else {
693 - $instance->sortable = new StringAttr(
694 - $sortable,
695 - $defaultSortable,
696 - ['text', 'number', 'date']
697 - );
698 - }
699 -
700 687 if ($width === null || $width === '') {
701 688 $instance->width = null;
702 689 } else {
703 690 $instance->width = new StringAttr($width, '');
@@ -711,8 +698,17 @@
711 698 /** @var StringAttr|null */
712 699 public $height;
713 700
714 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 + /**
715 711 * @param array|null $data
716 712 * @return self
717 713 */
718 714 public static function from_array($data) {
@@ -719,8 +715,9 @@
719 715 $data = is_array($data) ? $data : [];
720 716 $height = getOrNull($data['height']);
721 717
722 718 $instance = new self();
719 + $instance->attrs = $data;
723 720
724 721 if ($height === null || $height === '') {
725 722 $instance->height = null;
726 723 } else {