PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.5.3
Tableberg – Simple Gutenberg Table Block v0.5.3
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 / Cell.php

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

103 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cell Block
4 *
5 * @package Tableberg
6 */
7
8 namespace Tableberg\Blocks;
9
10 use Tableberg;
11 use Tableberg\Utils\HtmlUtils;
12 use Tableberg\Utils\Utils;
13
14 /**
15 * Handle the block registration on server side and rendering.
16 */
17 class Cell
18 {
19
20 /**
21 * Constructor
22 *
23 * @return void
24 */
25 public function __construct()
26 {
27 add_action('init', array($this, 'block_registration'));
28 }
29
30
31
32 private static function getStyles($attributes)
33 {
34 $styles = [
35 'background' => Utils::get_any($attributes, 'bgGradient', 'background'),
36 ];
37 $blockSpacing = Utils::get_spacing_css_single($attributes['blockSpacing'] ?? '');
38 if ($blockSpacing && $blockSpacing[0] != '0') {
39 $styles['--tableberg-block-spacing'] = $blockSpacing;
40 }
41 return Utils::generate_css_string($styles);
42 }
43
44 /**
45 * Renders the custom cell block on the server.
46 *
47 * @param array $attributes The block attributes.
48 * @param string $content The block content.
49 * @param \WP_Block $block The block object.
50 * @return string Returns the HTML content for the custom cell block.
51 */
52 public function render_tableberg_cell_block($attributes, $content, $block)
53 {
54 if (isset($attributes['isTmp']) && $attributes['isTmp']) {
55 return '';
56 }
57
58 $colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1;
59 $rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1;
60
61 $attrs_str = 'data-tableberg-row="' . esc_attr($attributes['row']) . '" data-tableberg-col="' . esc_attr($attributes['col']) . '"';
62 $classes = 'tableberg-v-align-' . esc_attr($attributes['vAlign']);
63
64 // Add colspan attribute if it's greater than 1
65 if ($colspan > 1) {
66 $attrs_str .= ' colspan="' . esc_attr($colspan) . '"';
67 }
68
69 // Add rowspan attribute if it's greater than 1
70 if ($rowspan > 1) {
71 $attrs_str .= ' rowspan="' . esc_attr($rowspan) . '"';
72 }
73
74 $tagName = isset($attributes['tagName']) ? esc_attr($attributes['tagName']) : 'td';
75
76 $content = HtmlUtils::append_attr_value($content, $tagName, self::getStyles($attributes), 'style');
77
78 $content = HtmlUtils::append_attr_value($content, $tagName, ' ' . $classes, 'class');
79
80 $content = HtmlUtils::add_attrs_to_tag($content, $tagName, $attrs_str);
81
82
83 Table::$rows[$attributes['row']][$attributes['col']] = $content;
84
85 return '';
86 }
87
88 /**
89 * Register the block.
90 */
91 public function block_registration()
92 {
93 $defaults = new \Tableberg\Defaults();
94 register_block_type(
95 TABLEBERG_DIR_PATH . 'build/cell/block.json',
96 array(
97 'attributes' => $defaults->get_default_attributes('tableberg/cell'),
98 'render_callback' => array($this, 'render_tableberg_cell_block'),
99 )
100 );
101 }
102 }
103