PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.4.1
Tableberg – Simple Gutenberg Table Block v0.4.1
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 0.5.8 All 41 releases
tableberg / includes / Blocks / Table.php

Table.php in Tableberg – Simple Gutenberg Table Block 0.4.1, at includes/Blocks/Table.php

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