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

308 lines 9.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 $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 $content = HtmlUtils::add_attrs_to_tag($content, 'table', $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 for ($i = 0; $i < $attributes['cols']; $i++) {
208 $colStyle = $attributes['colStyles'][$i] ?? [];
209 $width = Utils::get_spacing_css_single($colStyle['width'] ?? '');
210
211 $colCss = Utils::generate_css_string([
212 'width' => $width,
213 'min-width' => $width,
214 'background' => Utils::get_background_color($colStyle, 'background', 'bgGradient'),
215 ]);
216
217 $colGroup .= '<col style="' . $colCss . '"/>';
218 $colBorders[$i] = Utils::generate_css_string(
219 Utils::get_border_variables_css($colStyle['border'] ?? [], 'col')
220 + Utils::get_border_radius_var($colStyle['borderRadius'] ?? [], '--tableberg-col')
221 );
222 }
223
224 $colGroup .= '</colgroup>';
225
226 $content = '<tbody>';
227
228 for ($i = 0; $i < $attributes['rows']; $i++) {
229 $rowStyle = $attributes['rowStyles'][$i] ?? [];
230 $rowCss = Utils::generate_css_string([
231 'height' => Utils::get_spacing_css_single($rowStyle['height'] ?? ''),
232 ] + Utils::get_border_radius_var($rowStyle['borderRadius'] ?? [], '--tableberg-row'));
233 $tagName = 'td';
234 $trClasses = '';
235 $isEven = $i % 2 === 0;
236 $isHeader = $i === 0 && $attributes['enableTableHeader'];
237 $isFooter = $attributes['enableTableFooter'] && $i === $attributes['rows'] - 1;
238
239 if ($attributes['enableTableHeader']) {
240 $isEven = !$isEven;
241 }
242
243 if ($isHeader) {
244 $tagName = 'th';
245 $trClasses = 'tableberg-header';
246 } elseif ($isFooter) {
247 $tagName = 'th';
248 $trClasses = 'tableberg-footer';
249 } elseif ($isEven) {
250 $trClasses = 'tableberg-even-row';
251 } else {
252 $trClasses = 'tableberg-odd-row';
253 }
254
255 $rowStyleCss = Utils::generate_css_string(Utils::get_border_variables_css($rowStyle['border'] ?? [], 'row'));
256
257 $rowBg = Utils::get_background_color($rowStyle, 'background', 'bgGradient');
258 if ($rowBg) {
259 $rowStyleCss .= 'background:' . esc_attr($rowBg) . ';';
260 } else {
261 $rowStyleCss .= '';
262 }
263
264 $content .= '<tr class="' . $trClasses . '" style="' . $rowStyleCss . '">';
265 for ($j = 0; $j < $attributes['cols']; $j++) {
266 $cell = self::$rows[$i][$j] ?? '';
267 $cell = HtmlUtils::append_attr_value($cell, $tagName, $rowCss . $colBorders[$j], 'style');
268 $content .= $cell;
269 }
270 $content .= '</tr>';
271 }
272
273 $content .= '</tbody>';
274
275 $table = '<table ' . $table_attrs . ' >' . $colGroup . $content . '</table>';
276
277
278 self::$rows = [];
279 // exit();
280
281 return '<div ' . $wrapper_attributes . ' >' . $table . '</div>';
282 }
283
284
285 /**
286 * Register the block.
287 */
288 public function block_registration()
289 {
290 $tableberg_assets = new \Tableberg\Assets();
291 $tableberg_assets->register_blocks_assets();
292 if (!is_admin()) {
293 $tableberg_assets->register_frontend_assets();
294 }
295 $jsonPath = TABLEBERG_DIR_PATH . 'build/block.json';
296 $attrs = json_decode(file_get_contents($jsonPath), true)['attributes'];
297
298 register_block_type_from_metadata(
299 $jsonPath,
300 [
301 'attributes' => $attrs,
302 'render_callback' => array($this, 'render_tableberg_table_block'),
303 ]
304 );
305
306 }
307 }
308