PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.2
Tableberg – Simple Gutenberg Table Block v0.5.2
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.2, at includes/Blocks/Table.php

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