| 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 |
|
| 13 |
/** |
| 14 |
* Handle the block registration on server side and rendering. |
| 15 |
*/ |
| 16 |
class Cell |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct() |
| 25 |
{ |
| 26 |
add_action('init', array($this, 'block_registration')); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Renders the custom cell block on the server. |
| 31 |
* |
| 32 |
* @param array $attributes The block attributes. |
| 33 |
* @param string $content The block content. |
| 34 |
* @param \WP_Block $block The block object. |
| 35 |
* @return string Returns the HTML content for the custom cell block. |
| 36 |
*/ |
| 37 |
public function render_tableberg_cell_block($attributes, $content, $block) |
| 38 |
{ |
| 39 |
if (isset($attributes['isTmp']) && $attributes['isTmp']) { |
| 40 |
return ''; |
| 41 |
} |
| 42 |
$pre = ''; |
| 43 |
$post = ''; |
| 44 |
if (is_null(Table::$lastRow)) { |
| 45 |
Table::$lastRow = $attributes['row']; |
| 46 |
$pre = '<tr>'; |
| 47 |
} else if (Table::$lastRow != $attributes['row']) { |
| 48 |
$pre = '</tr><tr>'; |
| 49 |
Table::$lastRow = $attributes['row']; |
| 50 |
} |
| 51 |
$colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1; |
| 52 |
$rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1; |
| 53 |
|
| 54 |
$attrs_str = 'data-tableberg-row="'.$attributes['row'].'" data-tableberg-col="'.$attributes['col'].'"'; |
| 55 |
$classes = 'tableberg-v-align-'.$attributes['vAlign']; |
| 56 |
|
| 57 |
// Add colspan attribute if it's greater than 1 |
| 58 |
if ($colspan > 1) { |
| 59 |
$attrs_str .= ' colspan="' . esc_attr($colspan) . '"'; |
| 60 |
} |
| 61 |
|
| 62 |
// Add rowspan attribute if it's greater than 1 |
| 63 |
if ($rowspan > 1) { |
| 64 |
$attrs_str .= ' rowspan="' . esc_attr($rowspan) . '"'; |
| 65 |
} |
| 66 |
|
| 67 |
$tagName = isset($attributes['tagName']) ? $attributes['tagName'] : 'td'; |
| 68 |
|
| 69 |
$content = HtmlUtils::append_attr_value($content, $tagName, ' '.$classes, 'class'); |
| 70 |
|
| 71 |
$content = HtmlUtils::add_attrs_to_tag($content, $tagName, $attrs_str); |
| 72 |
|
| 73 |
return $pre . $content . $post; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Register the block. |
| 78 |
*/ |
| 79 |
public function block_registration() |
| 80 |
{ |
| 81 |
$defaults = new \Tableberg\Defaults(); |
| 82 |
register_block_type( |
| 83 |
TABLEBERG_DIR_PATH . 'build/cell/block.json', |
| 84 |
array( |
| 85 |
'attributes' => $defaults->get_default_attributes('tableberg/cell'), |
| 86 |
'render_callback' => array($this, 'render_tableberg_cell_block'), |
| 87 |
) |
| 88 |
); |
| 89 |
} |
| 90 |
} |
| 91 |
|