| 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', [$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 |
private static function getInnerStyles($attributes){ |
| 45 |
|
| 46 |
$styles = [ |
| 47 |
'display' => $attributes['isHorizontal'] ? 'flex': 'block', |
| 48 |
'justify-content' => $attributes['justifyContent'], |
| 49 |
'flex-wrap' => $attributes['wrapItems'] ? 'wrap': 'nowrap', |
| 50 |
]; |
| 51 |
|
| 52 |
return Utils::generate_css_string($styles); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
public function render_tableberg_cell_block($attributes, $content, $block) |
| 57 |
{ |
| 58 |
if ($attributes['isTmp']) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
|
| 62 |
$colspan = $attributes['colspan'] ?? 1; |
| 63 |
$rowspan = $attributes['rowspan'] ?? 1; |
| 64 |
|
| 65 |
$attrs_str = 'data-tableberg-row="' . esc_attr($attributes['row']) . '" data-tableberg-col="' . esc_attr($attributes['col']) . '"'; |
| 66 |
$classes = 'tableberg-v-align-' . esc_attr($attributes['vAlign']); |
| 67 |
|
| 68 |
// Add colspan attribute if it's greater than 1 |
| 69 |
if ($colspan > 1) { |
| 70 |
$attrs_str .= ' colspan="' . esc_attr($colspan) . '"'; |
| 71 |
} |
| 72 |
|
| 73 |
// Add rowspan attribute if it's greater than 1 |
| 74 |
if ($rowspan > 1) { |
| 75 |
$attrs_str .= ' rowspan="' . esc_attr($rowspan) . '"'; |
| 76 |
} |
| 77 |
|
| 78 |
$tagName = isset($attributes['tagName']) ? esc_attr($attributes['tagName']) : 'td'; |
| 79 |
|
| 80 |
$content = HtmlUtils::append_attr_value($content, $tagName, self::getStyles($attributes), 'style'); |
| 81 |
|
| 82 |
$content = HtmlUtils::append_attr_value($content, $tagName, ' ' . $classes, 'class'); |
| 83 |
|
| 84 |
$content = HtmlUtils::add_attrs_to_tag($content, $tagName, $attrs_str); |
| 85 |
|
| 86 |
$innerClass = 'tableberg-cell-inner'; |
| 87 |
if ($attributes['isHorizontal']) { |
| 88 |
$innerClass .= ' tableberg-cell-horizontal'; |
| 89 |
} |
| 90 |
|
| 91 |
$innerDiv = '<div class="'.$innerClass.'" style="' . self::getInnerStyles($attributes) . '">'; |
| 92 |
|
| 93 |
$content = HtmlUtils::insert_inside_tag($content, $tagName, $innerDiv); |
| 94 |
$content = HtmlUtils::replace_closing_tag($content, $tagName, '</div></'.$tagName.'>'); |
| 95 |
|
| 96 |
|
| 97 |
Table::$rows[$attributes['row']][$attributes['col']] = $content; |
| 98 |
|
| 99 |
return ''; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Register the block. |
| 104 |
*/ |
| 105 |
public function block_registration() |
| 106 |
{ |
| 107 |
$json = TABLEBERG_DIR_PATH . 'build/cell/block.json'; |
| 108 |
register_block_type( |
| 109 |
$json, |
| 110 |
[ |
| 111 |
'attributes' => json_decode(file_get_contents($json), true)['attributes'], |
| 112 |
'render_callback' => [$this, 'render_tableberg_cell_block'], |
| 113 |
] |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|