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

347 lines 11.5 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_wrapper_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 $table_attrs .= 'data-tableberg-header="' . $attributes['enableTableHeader'] . '" ';
168 $table_attrs .= 'data-tableberg-footer="' . $attributes['enableTableFooter'] . '" ';
169
170 $responsive = trim(self::get_responsiveness_metadata($attributes, 'mobile') . self::get_responsiveness_metadata($attributes, 'tablet'));
171 if ($responsive) {
172 $table_attrs .= 'data-tableberg-responsive data-tableberg-rows="' . $attributes['rows'] . '" data-tableberg-cols="' . $attributes['cols'] . '" ' . $responsive;
173 }
174
175 $wrapper_classes = ['wp-block-tableberg-wrapper'];
176 $table_alignment = $attributes['tableAlignment'];
177 if ($table_alignment && $table_alignment !== "center") {
178 $wrapper_classes[] = 'justify-table-' . $table_alignment;
179 }
180
181 if ($attributes['stickyTopRow']) {
182 $wrapper_classes[] = 'tableberg-sticky-top-row';
183 }
184 if ($attributes['stickyFirstCol']) {
185 $wrapper_classes[] = 'tableberg-sticky-first-col';
186 }
187 if ($attributes['innerBorderType'] === 'col') {
188 $wrapper_classes[] = 'tableberg-border-col-only';
189 } elseif ($attributes['innerBorderType'] === 'row') {
190 $wrapper_classes[] = 'tableberg-border-row-only';
191 }
192
193 if ($attributes['hideCellOutsideBorders'] ?? true) {
194 $wrapper_classes[] = 'tableberg-cell-no-outside-border';
195 }
196
197 if ($attributes['disableThemeStyle']) {
198 $wrapper_classes[] = 'tableberg-theme-disabled';
199 }
200
201 $wrapper_attributes = get_block_wrapper_attributes([
202 'class' => trim(join(' ', $wrapper_classes)),
203 ]);
204
205 $colBorders = [];
206 $colGroup = '<colgroup>';
207
208 if ($attributes['fixedColWidth']) {
209 $fwidth = 100 / (int) $attributes['cols'] . '%';
210 }
211
212 for ($i = 0; $i < $attributes['cols']; $i++) {
213 $colStyle = $attributes['colStyles'][$i] ?? [];
214 $width = $fwidth ?? Utils::get_spacing_css_single($colStyle['width'] ?? '');
215
216 $colCss = Utils::generate_css_string([
217 'width' => $width,
218 'min-width' => $width,
219 'background' => Utils::get_background_color($colStyle, 'background', 'bgGradient'),
220 ]);
221
222 $colGroup .= '<col style="' . $colCss . '"/>';
223 $colBorders[$i] = Utils::generate_css_string(
224 Utils::get_border_variables_css($colStyle['border'] ?? [], 'col')
225 + Utils::get_border_radius_var($colStyle['borderRadius'] ?? [], '--tableberg-col')
226 );
227 }
228
229 $colGroup .= '</colgroup>';
230
231 $content = '<tbody>';
232
233 $isHSort = isset($attributes['sort']['horizontal']['enabled']) && $attributes['sort']['horizontal']['enabled'];
234 $isVSort = isset($attributes['sort']['vertical']['enabled']) && $attributes['sort']['vertical']['enabled'];
235
236 for ($i = 0; $i < $attributes['rows']; $i++) {
237 $rowStyle = $attributes['rowStyles'][$i] ?? [];
238 $rowCss = Utils::generate_css_string([
239 'height' => Utils::get_spacing_css_single($rowStyle['height'] ?? ''),
240 ] + Utils::get_border_radius_var($rowStyle['borderRadius'] ?? [], '--tableberg-row'));
241 $tagName = 'td';
242 $trClasses = '';
243 $isEven = $i % 2 === 1;
244 $isHeader = $i === 0 && $attributes['enableTableHeader'];
245 $isFooter = $attributes['enableTableFooter'] && $i === $attributes['rows'] - 1;
246
247 if ($attributes['enableTableHeader']) {
248 $isEven = !$isEven;
249 }
250
251 if ($isHeader) {
252 $tagName = 'th';
253 $trClasses = 'tableberg-header';
254 } elseif ($isFooter) {
255 $tagName = 'th';
256 $trClasses = 'tableberg-footer';
257 } elseif ($isEven) {
258 $trClasses = 'tableberg-even-row';
259 } else {
260 $trClasses = 'tableberg-odd-row';
261 }
262
263 $rowStyleCss = Utils::generate_css_string(Utils::get_border_variables_css($rowStyle['border'] ?? [], 'row'));
264
265 $rowBg = Utils::get_background_color($rowStyle, 'background', 'bgGradient');
266 if ($rowBg) {
267 $rowStyleCss .= 'background:' . esc_attr($rowBg) . ';';
268 } else {
269 $rowStyleCss .= '';
270 }
271
272
273
274
275 $sortingBtnV = '';
276
277 if ($isVSort && $i == 0) {
278 $sortingBtnV = '<button type="button" class="tableberg-v-sorter"></button>';
279 }
280
281 $content .= '<tr class="' . $trClasses . '" style="' . $rowStyleCss . '">';
282 for ($j = 0; $j < $attributes['cols']; $j++) {
283 $sortingBtns = $sortingBtnV;
284 if ($isHSort && $j == 0) {
285 $sortingBtns .= '<button type="button" class="tableberg-h-sorter"></button>';
286 }
287 $cell = self::$rows[$i][$j] ?? ' ';
288 $cell = HtmlUtils::append_attr_value($cell, $tagName, $rowCss . $colBorders[$j], 'style');
289 $cell = HtmlUtils::replace_closing_tag($cell, $tagName, $sortingBtns . '</' . $tagName . '>');
290 $content .= $cell;
291 }
292 $content .= '</tr>';
293 }
294
295 $content .= '</tbody>';
296
297 self::$rows = [];
298
299 $table = '';
300
301 if ($attributes['search']) {
302 $table = '
303 <div class="tableberg-search tableberg-search-' . $attributes['searchPosition'] . '">
304 <input type="text" placeholder="' . esc_attr( isset($attributes['searchPlaceholder']) && $attributes['searchPlaceholder'] !== 'Search...' ? $attributes['searchPlaceholder']: __('Search...', 'tableberg')). '" data-tableberg-search />
305 <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="magnifying-glass" class="svg-inline--fa fa-magnifying-glass " role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
306 <path fill="currentColor" d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352a144 144 0 1 0 0-288 144 144 0 1 0 0 288z"></path>
307 </svg>
308 </div>
309 ';
310 }
311
312 $table .= '
313 <div class="tableberg-table-wrapper" style="' . self::get_wrapper_styles($attributes) . '">
314 <table ' . $table_attrs . ' >' . $colGroup . $content . '</table>
315 </div>
316 ';
317
318
319
320 return '<div ' . $wrapper_attributes . ' >' . $table . '</div>';
321 }
322
323
324 /**
325 * Register the block.
326 */
327 public function block_registration()
328 {
329 $tableberg_assets = new \Tableberg\Assets();
330 $tableberg_assets->register_blocks_assets();
331 if (!is_admin()) {
332 $tableberg_assets->register_frontend_assets();
333 }
334 $jsonPath = TABLEBERG_DIR_PATH . 'build/block.json';
335 $attrs = json_decode(file_get_contents($jsonPath), true)['attributes'];
336
337 register_block_type_from_metadata(
338 $jsonPath,
339 [
340 'attributes' => $attrs,
341 'render_callback' => array($this, 'render_tableberg_table_block'),
342 ]
343 );
344
345 }
346 }
347