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

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