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

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