| 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 |
$styles = [ |
| 34 |
'background' => Utils::get_any($attributes, 'bgGradient', 'background'), |
| 35 |
]; |
| 36 |
return Utils::generate_css_string($styles); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Renders the custom cell block on the server. |
| 41 |
* |
| 42 |
* @param array $attributes The block attributes. |
| 43 |
* @param string $content The block content. |
| 44 |
* @param \WP_Block $block The block object. |
| 45 |
* @return string Returns the HTML content for the custom cell block. |
| 46 |
*/ |
| 47 |
public function render_tableberg_cell_block($attributes, $content, $block) |
| 48 |
{ |
| 49 |
if (isset($attributes['isTmp']) && $attributes['isTmp']) { |
| 50 |
return ''; |
| 51 |
} |
| 52 |
$pre = ''; |
| 53 |
$post = ''; |
| 54 |
if (is_null(Table::$lastRow)) { |
| 55 |
Table::$lastRow = $attributes['row']; |
| 56 |
$pre = '<tr>'; |
| 57 |
} else if (Table::$lastRow != $attributes['row']) { |
| 58 |
$pre = '</tr><tr>'; |
| 59 |
Table::$lastRow = $attributes['row']; |
| 60 |
} |
| 61 |
$colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1; |
| 62 |
$rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1; |
| 63 |
|
| 64 |
$attrs_str = 'data-tableberg-row="'.esc_attr($attributes['row']).'" data-tableberg-col="'.esc_attr($attributes['col']).'"'; |
| 65 |
$classes = 'tableberg-v-align-'.esc_attr($attributes['vAlign']); |
| 66 |
|
| 67 |
// Add colspan attribute if it's greater than 1 |
| 68 |
if ($colspan > 1) { |
| 69 |
$attrs_str .= ' colspan="' . esc_attr($colspan) . '"'; |
| 70 |
} |
| 71 |
|
| 72 |
// Add rowspan attribute if it's greater than 1 |
| 73 |
if ($rowspan > 1) { |
| 74 |
$attrs_str .= ' rowspan="' . esc_attr($rowspan) . '"'; |
| 75 |
} |
| 76 |
|
| 77 |
$tagName = isset($attributes['tagName']) ? esc_attr($attributes['tagName']) : 'td'; |
| 78 |
|
| 79 |
$content = HtmlUtils::append_attr_value($content, $tagName, self::getStyles($attributes), 'style'); |
| 80 |
|
| 81 |
$content = HtmlUtils::append_attr_value($content, $tagName, ' '.$classes, 'class'); |
| 82 |
|
| 83 |
$content = HtmlUtils::add_attrs_to_tag($content, $tagName, $attrs_str); |
| 84 |
|
| 85 |
return $pre . $content . $post; |
| 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 |
|