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

228 lines 6.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_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 = array(
56 '--tableber-table-width' => $attributes['tableWidth'],
57 '--tableberg-even-row-bg-color' => $even_row_bg_color,
58 '--tableberg-odd-row-bg-color' => $odd_row_bg_color,
59 '--tableberg-cell-padding-top' => $cell_padding['top'] ?? '',
60 '--tableberg-cell-padding-right' => $cell_padding['right'] ?? '',
61 '--tableberg-cell-padding-bottom' => $cell_padding['bottom'] ?? '',
62 '--tableberg-cell-padding-left' => $cell_padding['left'] ?? '',
63 '--tableberg-cell-spacing-top' => $table_spacing['top'] ?? '',
64 '--tableberg-cell-spacing-left' => $table_spacing['left'] ?? '',
65 ) + $table_border_variables + $inner_border_variables + $global_font_style;
66
67 foreach (['top', 'left'] as $k) {
68 if (isset($cellSpacing[$k]) && $cellSpacing[$k] !== '0') {
69 $styles['--tableberg-border-collapse'] = 'separate';
70 }
71 }
72
73 return Utils::generate_css_string($styles);
74 }
75
76 /**
77 * Class if styling is applied.
78 *
79 * @param array $attributes The block attributes.
80 * @return array CSS classes based on attributes.
81 */
82 public function get_style_class($attributes)
83 {
84 $table_width = $attributes['tableWidth'];
85 $table_alignment = $attributes['tableAlignment'];
86 $enable_inner_border = $attributes['enableInnerBorder'];
87 $classes = array();
88 if ($enable_inner_border) {
89 $classes[] = 'has-inner-border';
90 }
91 $is_value_empty = function ($value) {
92 return (
93 is_null($value) ||
94 false === $value ||
95 trim($value) === '' ||
96 trim($value) === 'undefined undefined undefined' ||
97 empty($value)
98 );
99 };
100
101 if (!$is_value_empty($table_width)) {
102 $classes[] = 'has-table-width';
103 }
104 if (!$is_value_empty($table_alignment)) {
105 $classes[] = 'justify-table-' . $table_alignment;
106 }
107
108 return $classes;
109 }
110
111
112
113 private static function setRowColSizes($content, $heights, $widths)
114 {
115 $lastIdx = 0;
116 foreach ($heights as $height) {
117 $idx = strpos($content, '<tr', $lastIdx);
118 if ($height) {
119 $content = HtmlUtils::append_attr_value($content, 'tr', "height:{$height} !important;", 'style', $idx);
120 }
121 $lastIdx = $idx + 1;
122 }
123 $colgroup = '<colgroup>';
124 foreach ($widths as $w) {
125 $colgroup .= "<col width=\"$w\"/>";
126 }
127 $colgroup .= '</colgroup>';
128 $content = HtmlUtils::insert_inside_tag($content, 'table', $colgroup);
129 return $content;
130 }
131
132 private function even_odd_rows($attributes, $content)
133 {
134 $even_color = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
135 $odd_color = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
136
137 if (!$even_color && !$odd_color) {
138 return $content;
139 }
140
141 $even_color = "background: {$even_color} !important;";
142 $odd_color = "background: {$odd_color} !important;";
143
144 $cursor = 0;
145 $i = 1;
146 $end = $attributes['rows'];
147 if ($attributes['enableTableHeader']) {
148 $i = 2;
149 $content = HtmlUtils::append_attr_value($content, 'tr', '', 'style', 0, $cursor);
150 }
151 if ($attributes['enableTableFooter']) {
152 $end -= 1;
153 }
154
155
156 for (; $i <= $end; $i++) {
157 $content = HtmlUtils::append_attr_value($content, 'tr', $i % 2 ? $odd_color : $even_color, 'style', $cursor + 1, $cursor);
158 }
159
160 return $content;
161 }
162
163 /**
164 * Renders the custom table block on the server.
165 *
166 * @param array $attributes The block attributes.
167 * @param string $content The block content.
168 * @param WP_Block $block The block object.
169 * @return string Returns the HTML content for the custom table block.
170 */
171 public function render_tableberg_table_block($attributes, $content, $block)
172 {
173 $class_names = $this->get_style_class($attributes);
174 $style = $this->get_styles($attributes);
175 $wrapper_attributes = get_block_wrapper_attributes(
176 array(
177 'class' => trim(join(' ', $class_names)),
178 'style' => $style,
179 )
180 );
181
182 $content = HtmlUtils::insert_inside_tag($content, 'table', '<tbody>');
183 $content = HtmlUtils::replace_attrs_of_tag($content, 'table', $wrapper_attributes);
184 $content = HtmlUtils::replace_closing_tag($content, 'table', '</tr></tbody></table>');
185
186 if ($attributes['enableTableHeader']) {
187 $content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-header', 'class');
188 $bg_color = Utils::get_background_color($attributes, 'headerBackgroundColor', 'headerBackgroundGradient');
189 if ($bg_color) {
190 $content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style');
191 }
192
193 }
194 if ($attributes['enableTableFooter']) {
195 $footer_idx = strrpos($content, '<tr');
196 $content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-footer', 'class', $footer_idx);
197 $bg_color = Utils::get_background_color($attributes, 'footerBackgroundColor', 'footerBackgroundGradient');
198 if ($bg_color) {
199 $content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style', $footer_idx);
200 }
201 }
202
203 $content = self::setRowColSizes($content, $attributes['rowHeights'], $attributes['colWidths']);
204 $content = self::even_odd_rows($attributes, $content);
205
206 self::$lastRow = null;
207 return $content;
208 }
209
210
211 /**
212 * Register the block.
213 */
214 public function block_registration()
215 {
216 $defaults = new \Tableberg\Defaults();
217 $tableberg_assets = new \Tableberg\Assets();
218 $tableberg_assets->register_blocks_assets();
219 register_block_type(
220 TABLEBERG_DIR_PATH . 'build/block.json',
221 array(
222 'attributes' => $defaults->get_default_attributes('tableberg/table'),
223 'render_callback' => array($this, 'render_tableberg_table_block'),
224 )
225 );
226 }
227 }
228