PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.3.3
Tableberg – Simple Gutenberg Table Block v0.3.3
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 / includes / Blocks / Table.php

Table.php in Tableberg – Simple Gutenberg Table Block 0.3.3, at includes/Blocks/Table.php

230 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Table Block
4 *
5 * @package Tableberg
6 */
7
8 namespace Tableberg\Blocks;
9
10 use Tableberg;
11 use Tableberg\Utils\Utils;
12 use Tableberg\Utils\HtmlUtils;
13 use WP_Block;
14
15 /**
16 * Handle the block registration on server side and rendering.
17 */
18 class Table
19 {
20
21 public static $lastRow = null;
22 public static $lastRows = 0;
23
24 /**
25 * Constructor
26 *
27 * @return void
28 */
29 public function __construct()
30 {
31 add_action('init', array($this, 'block_registration'));
32 }
33
34 /**
35 * Get block styles.
36 *
37 * @param array $attributes - block attributes.
38 * @return string Generated CSS styles.
39 */
40 public static function get_styles($attributes)
41 {
42 $even_row_bg_color = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
43 $odd_row_bg_color = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
44
45 $global_font_style = Utils::get_global_style_variables_css($attributes);
46
47 $cell_padding = Utils::get_spacing_css($attributes['cellPadding']);
48 $cellSpacing = $attributes['cellSpacing'] ?? [];
49 $table_spacing = Utils::get_spacing_css($cellSpacing);
50
51
52 $table_border_variables = Utils::get_border_variables_css($attributes['tableBorder'], 'table');
53 $inner_border_variables = $attributes['enableInnerBorder'] ? Utils::get_border_variables_css($attributes['innerBorder'], 'inner') : array();
54
55 $styles = array(
56 '--tableberg-even-row-bg-color' => $even_row_bg_color,
57 '--tableberg-odd-row-bg-color' => $odd_row_bg_color,
58 '--tableberg-cell-padding-top' => $cell_padding['top'] ?? '',
59 '--tableberg-cell-padding-right' => $cell_padding['right'] ?? '',
60 '--tableberg-cell-padding-bottom' => $cell_padding['bottom'] ?? '',
61 '--tableberg-cell-padding-left' => $cell_padding['left'] ?? '',
62 '--tableberg-cell-spacing-top' => $table_spacing['top'] ?? '',
63 '--tableberg-cell-spacing-left' => $table_spacing['left'] ?? '',
64 'max-width' => $attributes['tableWidth'] ?? '',
65 ) + $inner_border_variables + $global_font_style;
66
67 foreach (['top', 'left'] as $k) {
68 if (isset($cellSpacing[$k]) && $cellSpacing[$k] !== '0') {
69 $styles['--tableberg-border-collapse'] = 'separate';
70 } else {
71 $styles += $table_border_variables;
72 }
73 }
74
75 return Utils::generate_css_string($styles);
76 }
77
78 /**
79 * Class if styling is applied.
80 *
81 * @param array $attributes The block attributes.
82 * @return array CSS classes based on attributes.
83 */
84 public function get_style_class($attributes)
85 {
86 $table_width = $attributes['tableWidth'];
87 $table_alignment = $attributes['tableAlignment'];
88 $enable_inner_border = $attributes['enableInnerBorder'];
89 $classes = array();
90 if ($enable_inner_border) {
91 $classes[] = 'has-inner-border';
92 }
93 $is_value_empty = function ($value) {
94 return (
95 is_null($value) ||
96 false === $value ||
97 trim($value) === '' ||
98 trim($value) === 'undefined undefined undefined' ||
99 empty($value)
100 );
101 };
102
103 if (!$is_value_empty($table_width)) {
104 $classes[] = 'has-table-width';
105 }
106 if (!$is_value_empty($table_alignment)) {
107 $classes[] = 'justify-table-' . $table_alignment;
108 }
109
110 return $classes;
111 }
112
113
114
115 private static function setRowColSizes($content, $heights, $widths)
116 {
117 $lastIdx = 0;
118 foreach ($heights as $height) {
119 $idx = strpos($content, '<tr', $lastIdx);
120 if ($height) {
121 $content = HtmlUtils::append_attr_value($content, 'tr', "height:{$height} !important;", 'style', $idx);
122 }
123 $lastIdx = $idx + 1;
124 }
125 $colgroup = '<colgroup>';
126 foreach ($widths as $w) {
127 $colgroup .= "<col width=\"$w\"/>";
128 }
129 $colgroup .= '</colgroup>';
130 $content = HtmlUtils::insert_inside_tag($content, 'table', $colgroup);
131 return $content;
132 }
133
134 private function even_odd_rows($attributes, $content)
135 {
136 $even_color = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
137 $odd_color = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
138
139 if (!$even_color && !$odd_color) {
140 return $content;
141 }
142
143 $even_color = "background: {$even_color} !important;";
144 $odd_color = "background: {$odd_color} !important;";
145
146 $cursor = 0;
147 $i = 1;
148 $end = $attributes['rows'];
149 if ($attributes['enableTableHeader']) {
150 $i = 2;
151 $content = HtmlUtils::append_attr_value($content, 'tr', '', 'style', 0, $cursor);
152 }
153 if ($attributes['enableTableFooter']) {
154 $end -= 1;
155 }
156
157
158 for (; $i <= $end; $i++) {
159 $content = HtmlUtils::append_attr_value($content, 'tr', $i % 2 ? $odd_color : $even_color, 'style', $cursor + 1, $cursor);
160 }
161
162 return $content;
163 }
164
165 /**
166 * Renders the custom table block on the server.
167 *
168 * @param array $attributes The block attributes.
169 * @param string $content The block content.
170 * @param WP_Block $block The block object.
171 * @return string Returns the HTML content for the custom table block.
172 */
173 public function render_tableberg_table_block($attributes, $content, $block)
174 {
175 $class_names = $this->get_style_class($attributes);
176 $style = $this->get_styles($attributes);
177 $wrapper_attributes = get_block_wrapper_attributes(
178 array(
179 'class' => trim(join(' ', $class_names)),
180 'style' => $style,
181 )
182 );
183
184 $content = HtmlUtils::insert_inside_tag($content, 'table', '<tbody>');
185 $content = HtmlUtils::replace_attrs_of_tag($content, 'table', $wrapper_attributes);
186 $content = HtmlUtils::replace_closing_tag($content, 'table', '</tr></tbody></table>');
187
188 if ($attributes['enableTableHeader']) {
189 $content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-header', 'class');
190 $bg_color = Utils::get_background_color($attributes, 'headerBackgroundColor', 'headerBackgroundGradient');
191 if ($bg_color) {
192 $content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style');
193 }
194
195 }
196 if ($attributes['enableTableFooter']) {
197 $footer_idx = strrpos($content, '<tr');
198 $content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-footer', 'class', $footer_idx);
199 $bg_color = Utils::get_background_color($attributes, 'footerBackgroundColor', 'footerBackgroundGradient');
200 if ($bg_color) {
201 $content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style', $footer_idx);
202 }
203 }
204
205 $content = self::setRowColSizes($content, $attributes['rowHeights'], $attributes['colWidths']);
206 $content = self::even_odd_rows($attributes, $content);
207
208 self::$lastRow = null;
209 return $content;
210 }
211
212
213 /**
214 * Register the block.
215 */
216 public function block_registration()
217 {
218 $defaults = new \Tableberg\Defaults();
219 $tableberg_assets = new \Tableberg\Assets();
220 $tableberg_assets->register_blocks_assets();
221 register_block_type(
222 TABLEBERG_DIR_PATH . 'build/block.json',
223 array(
224 'attributes' => $defaults->get_default_attributes('tableberg/table'),
225 'render_callback' => array($this, 'render_tableberg_table_block'),
226 )
227 );
228 }
229 }
230