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

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

310 lines 9.9 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 $rows = [];
22
23 /**
24 * Constructor
25 *
26 * @return void
27 */
28 public function __construct()
29 {
30 add_action('init', array($this, 'block_registration'));
31 }
32
33 /**
34 * Get block styles.
35 *
36 * @param array $attributes - block attributes.
37 * @return string Generated CSS styles.
38 */
39 public static function get_styles($attributes)
40 {
41 $even_row_bg = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
42 $odd_row_bg = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
43 $header_bg = Utils::get_background_color($attributes, 'headerBackgroundColor', 'headerBackgroundGradient');
44 $footer_bg = Utils::get_background_color($attributes, 'footerBackgroundColor', 'footerBackgroundGradient');
45
46
47 $cellSpacing = $attributes['cellSpacing'] ?? [];
48 $table_spacing = Utils::get_spacing_css($cellSpacing);
49
50
51 $inner_border_variables = [];
52 if ($attributes['enableInnerBorder']) {
53 $inner_border_variables = Utils::get_border_variables_css($attributes['innerBorder'], 'inner');
54 }
55 if (!isset($table_spacing['top']) || $table_spacing['top'] == '0') {
56 $inner_border_variables["--tableberg-inner-border-top"] = "none";
57 $inner_border_variables["--tableberg-inner-border-top-first"] = $inner_border_variables["--tableberg-inner-border-bottom"] ?? '';
58 }
59
60 if (!isset($table_spacing['left']) || $table_spacing['left'] == '0') {
61 $inner_border_variables["--tableberg-inner-border-left"] = "none";
62 $inner_border_variables["--tableberg-inner-border-left-first"] = $inner_border_variables["--tableberg-inner-border-right"] ?? '';
63 }
64
65
66 $cell_radius = Utils::get_border_radius_var($attributes['cellBorderRadius'], '--tableberg-cell');
67
68 $styles = [
69 'width' => $attributes['tableWidth'],
70 'max-width' => $attributes['tableWidth'],
71 'color' => $attributes['fontColor'] ?? '',
72 'font-size' => $attributes['fontSize'] ?? '',
73 '--tableberg-global-link-color' => $attributes['linkColor'] ?? '',
74 '--tableberg-even-bg' => $even_row_bg,
75 '--tableberg-odd-bg' => $odd_row_bg,
76 '--tableberg-header-bg' => $header_bg,
77 '--tableberg-footer-bg' => $footer_bg,
78 '--tableberg-block-spacing' => Utils::get_spacing_css_single($attributes['blockSpacing'] ?? ''),
79 'border-spacing' => ($table_spacing['left'] ?? 0) . ' ' . ($table_spacing['top'] ?? 0),
80 ]
81 + Utils::get_spacing_style($attributes['cellPadding'], '--tableberg-cell-padding')
82 + $inner_border_variables
83 + $cell_radius;
84
85 return Utils::generate_css_string($styles);
86 }
87
88
89 private static function get_root_styles($attributes)
90 {
91 $styles = Utils::get_border_style($attributes['tableBorder'] ?? []);
92 $styles += Utils::get_border_radius_style($attributes['tableBorderRadius'] ?? []);
93 return Utils::generate_css_string($styles);
94 }
95
96 /**
97 * Class if styling is applied.
98 *
99 * @param array $attributes The block attributes.
100 * @return array CSS classes based on attributes.
101 */
102 public function get_style_class($attributes)
103 {
104 $table_width = $attributes['tableWidth'];
105
106 $enable_inner_border = $attributes['enableInnerBorder'];
107 $classes = array();
108 if ($enable_inner_border) {
109 $classes[] = 'has-inner-border';
110 }
111 $is_value_empty = function ($value) {
112 return (
113 is_null($value) ||
114 false === $value ||
115 trim($value) === '' ||
116 trim($value) === 'undefined undefined undefined' ||
117 empty($value)
118 );
119 };
120
121 if (!$is_value_empty($table_width)) {
122 $classes[] = 'has-table-width';
123 }
124
125
126 return $classes;
127 }
128
129
130
131 private static function get_responsiveness_metadata($attributes, $device)
132 {
133 if (!isset($attributes["responsive"])) {
134 return '';
135 }
136 $responsive = $attributes["responsive"]["breakpoints"];
137 $str = " ";
138 if (isset($responsive[$device]) && $responsive[$device]["enabled"]) {
139 $deviceOpts = $responsive[$device];
140 $str .= 'data-tableberg-' . $device . '-width="' . $deviceOpts["maxWidth"] . '" ';
141 $str .= 'data-tableberg-' . $device . '-mode="' . $deviceOpts["mode"] . '" ';
142 $str .= 'data-tableberg-' . $device . '-direction="' . $deviceOpts["direction"] . '" ';
143 $str .= 'data-tableberg-' . $device . '-count="' . $deviceOpts["stackCount"] . '" ';
144
145 if ($deviceOpts["headerAsCol"] && $attributes['enableTableHeader']) {
146 $str .= 'data-tableberg-' . $device . '-header="' . $deviceOpts["headerAsCol"] . '" ';
147 }
148 }
149 return $str;
150 }
151
152 /**
153 * Renders the custom table block on the server.
154 *
155 * @param array $attributes The block attributes.
156 * @param string $content The block content.
157 * @param WP_Block $block The block object.
158 * @return string Returns the HTML content for the custom table block.
159 */
160 public function render_tableberg_table_block($attributes, $content, $block)
161 {
162 $table_class_names = $this->get_style_class($attributes);
163 $table_style = $this->get_styles($attributes);
164
165 $table_attrs = 'class = "' . esc_attr(trim(join(' ', $table_class_names))) . '" style="' . $table_style . '"';
166
167 $responsive = trim(self::get_responsiveness_metadata($attributes, 'mobile') . self::get_responsiveness_metadata($attributes, 'tablet'));
168
169 if ($responsive) {
170
171 $str = 'data-tableberg-header="' . $attributes['enableTableHeader'] . '" ';
172 $str .= 'data-tableberg-footer="' . $attributes['enableTableFooter'] . '" ';
173 $responsive = 'data-tableberg-responsive ' . $str . ' data-tableberg-rows="' . $attributes['rows'] . '" data-tableberg-cols="' . $attributes['cols'] . '" ' . $responsive;
174 $table_attrs .= ' ' . $responsive;
175 }
176
177 $wrapper_classes = ['wp-block-tableberg-wrapper'];
178 $table_alignment = $attributes['tableAlignment'];
179 if ($table_alignment && $table_alignment !== "center") {
180 $wrapper_classes[] = 'justify-table-' . $table_alignment;
181 }
182
183 if ($attributes['stickyTopRow']) {
184 $wrapper_classes[] = 'tableberg-sticky-top-row';
185 }
186 if ($attributes['stickyFirstCol']) {
187 $wrapper_classes[] = 'tableberg-sticky-first-col';
188 }
189 if ($attributes['innerBorderType'] === 'col') {
190 $wrapper_classes[] = 'tableberg-border-col-only';
191 } elseif ($attributes['innerBorderType'] === 'row') {
192 $wrapper_classes[] = 'tableberg-border-row-only';
193 }
194
195 if ($attributes['hideCellOutsideBorders'] ?? true) {
196 $wrapper_classes[] = 'tableberg-cell-no-outside-border';
197 }
198
199 $wrapper_attributes = get_block_wrapper_attributes([
200 'class' => trim(join(' ', $wrapper_classes)),
201 'style' => self::get_root_styles($attributes)
202 ]);
203
204 $colBorders = [];
205 $colGroup = '<colgroup>';
206
207 if ($attributes['fixedColWidth']) {
208 $fwidth = 100 / (int) $attributes['cols'] . '%';
209 }
210
211 for ($i = 0; $i < $attributes['cols']; $i++) {
212 $colStyle = $attributes['colStyles'][$i] ?? [];
213 $width = $fwidth ?? Utils::get_spacing_css_single($colStyle['width'] ?? '');
214
215 $colCss = Utils::generate_css_string([
216 'width' => $width,
217 'min-width' => $width,
218 'background' => Utils::get_background_color($colStyle, 'background', 'bgGradient'),
219 ]);
220
221 $colGroup .= '<col style="' . $colCss . '"/>';
222 $colBorders[$i] = Utils::generate_css_string(
223 Utils::get_border_variables_css($colStyle['border'] ?? [], 'col')
224 + Utils::get_border_radius_var($colStyle['borderRadius'] ?? [], '--tableberg-col')
225 );
226 }
227
228 $colGroup .= '</colgroup>';
229
230 $content = '<tbody>';
231
232 for ($i = 0; $i < $attributes['rows']; $i++) {
233 $rowStyle = $attributes['rowStyles'][$i] ?? [];
234 $rowCss = Utils::generate_css_string([
235 'height' => Utils::get_spacing_css_single($rowStyle['height'] ?? ''),
236 ] + Utils::get_border_radius_var($rowStyle['borderRadius'] ?? [], '--tableberg-row'));
237 $tagName = 'td';
238 $trClasses = '';
239 $isEven = $i % 2 === 1;
240 $isHeader = $i === 0 && $attributes['enableTableHeader'];
241 $isFooter = $attributes['enableTableFooter'] && $i === $attributes['rows'] - 1;
242
243 if ($attributes['enableTableHeader']) {
244 $isEven = !$isEven;
245 }
246
247 if ($isHeader) {
248 $tagName = 'th';
249 $trClasses = 'tableberg-header';
250 } elseif ($isFooter) {
251 $tagName = 'th';
252 $trClasses = 'tableberg-footer';
253 } elseif ($isEven) {
254 $trClasses = 'tableberg-even-row';
255 } else {
256 $trClasses = 'tableberg-odd-row';
257 }
258
259 $rowStyleCss = Utils::generate_css_string(Utils::get_border_variables_css($rowStyle['border'] ?? [], 'row'));
260
261 $rowBg = Utils::get_background_color($rowStyle, 'background', 'bgGradient');
262 if ($rowBg) {
263 $rowStyleCss .= 'background:' . esc_attr($rowBg) . ';';
264 } else {
265 $rowStyleCss .= '';
266 }
267
268 $content .= '<tr class="' . $trClasses . '" style="' . $rowStyleCss . '">';
269 for ($j = 0; $j < $attributes['cols']; $j++) {
270 $cell = self::$rows[$i][$j] ?? '';
271 $cell = HtmlUtils::append_attr_value($cell, $tagName, $rowCss . $colBorders[$j], 'style');
272 $content .= $cell;
273 }
274 $content .= '</tr>';
275 }
276
277 $content .= '</tbody>';
278
279 $table = '<table ' . $table_attrs . ' >' . $colGroup . $content . '</table>';
280
281 self::$rows = [];
282
283 return '<div ' . $wrapper_attributes . ' >' . $table . '</div>';
284 }
285
286
287 /**
288 * Register the block.
289 */
290 public function block_registration()
291 {
292 $tableberg_assets = new \Tableberg\Assets();
293 $tableberg_assets->register_blocks_assets();
294 if (!is_admin()) {
295 $tableberg_assets->register_frontend_assets();
296 }
297 $jsonPath = TABLEBERG_DIR_PATH . 'build/block.json';
298 $attrs = json_decode(file_get_contents($jsonPath), true)['attributes'];
299
300 register_block_type_from_metadata(
301 $jsonPath,
302 [
303 'attributes' => $attrs,
304 'render_callback' => array($this, 'render_tableberg_table_block'),
305 ]
306 );
307
308 }
309 }
310