$attributes['tableWidth'],
'max-width' => $attributes['tableWidth'],
'--tableberg-even-row-bg-color' => $even_row_bg_color,
'--tableberg-odd-row-bg-color' => $odd_row_bg_color,
'--tableberg-cell-padding-top' => $cell_padding['top'] ?? '',
'--tableberg-cell-padding-right' => $cell_padding['right'] ?? '',
'--tableberg-cell-padding-bottom' => $cell_padding['bottom'] ?? '',
'--tableberg-cell-padding-left' => $cell_padding['left'] ?? '',
'--tableberg-cell-spacing-top' => $table_spacing['top'] ?? '',
'--tableberg-cell-spacing-left' => $table_spacing['left'] ?? '',
] + $table_border_variables + $inner_border_variables + $global_font_style;
foreach (['top', 'left'] as $k) {
if (isset($cellSpacing[$k]) && $cellSpacing[$k] !== '0') {
$styles['--tableberg-border-collapse'] = 'separate';
} else {
$styles += $table_border_variables;
}
}
return Utils::generate_css_string($styles);
}
/**
* Class if styling is applied.
*
* @param array $attributes The block attributes.
* @return array CSS classes based on attributes.
*/
public function get_style_class($attributes)
{
$table_width = $attributes['tableWidth'];
$enable_inner_border = $attributes['enableInnerBorder'];
$classes = array();
if ($enable_inner_border) {
$classes[] = 'has-inner-border';
}
$is_value_empty = function ($value) {
return (
is_null($value) ||
false === $value ||
trim($value) === '' ||
trim($value) === 'undefined undefined undefined' ||
empty ($value)
);
};
if (!$is_value_empty($table_width)) {
$classes[] = 'has-table-width';
}
return $classes;
}
private static function setRowColSizes($content, $heights, $widths)
{
$lastIdx = 0;
foreach ($heights as $height) {
$idx = strpos($content, '
';
foreach ($widths as $w) {
$colgroup .= "";
}
$colgroup .= '';
$content = HtmlUtils::insert_inside_tag($content, 'table', $colgroup);
return $content;
}
private function even_odd_rows($attributes, $content)
{
$even_color = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
$odd_color = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
if (!$even_color && !$odd_color) {
return $content;
}
$even_color = "background: {$even_color} !important;";
$odd_color = "background: {$odd_color} !important;";
$cursor = 0;
$i = 1;
$end = $attributes['rows'];
if ($attributes['enableTableHeader']) {
$i = 2;
$content = HtmlUtils::append_attr_value($content, 'tr', '', 'style', 0, $cursor);
}
if ($attributes['enableTableFooter']) {
$end -= 1;
}
for (; $i <= $end; $i++) {
$content = HtmlUtils::append_attr_value($content, 'tr', $i % 2 ? $odd_color : $even_color, 'style', $cursor + 1, $cursor);
}
return $content;
}
private static function get_responsiveness_metadata($attributes, $device)
{
if (!isset($attributes["responsive"])) {
return '';
}
$responsive = $attributes["responsive"]["breakpoints"];
$str = " ";
if (isset($responsive[$device]) && $responsive[$device]["enabled"]) {
$deviceOpts = $responsive[$device];
$str .= 'data-tableberg-' . $device . '-width="' . $deviceOpts["maxWidth"] . '" ';
$str .= 'data-tableberg-' . $device . '-mode="' . $deviceOpts["mode"] . '" ';
$str .= 'data-tableberg-' . $device . '-direction="' . $deviceOpts["direction"] . '" ';
$str .= 'data-tableberg-' . $device . '-count="' . $deviceOpts["stackCount"] . '" ';
if ($deviceOpts["headerAsCol"] && $attributes['enableTableHeader']) {
$str .= 'data-tableberg-' . $device . '-header="' . $deviceOpts["headerAsCol"] . '" ';
}
}
return $str;
}
/**
* Renders the custom table block on the server.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block The block object.
* @return string Returns the HTML content for the custom table block.
*/
public function render_tableberg_table_block($attributes, $content, $block)
{
$table_class_names = $this->get_style_class($attributes);
$table_style = $this->get_styles($attributes);
$table_attrs = 'class = "' . trim(join(' ', $table_class_names)) . '" style="' . $table_style . '"';
$content = HtmlUtils::insert_inside_tag($content, 'table', '
');
$content = HtmlUtils::replace_attrs_of_tag($content, 'table', $table_attrs);
$content = HtmlUtils::replace_closing_tag($content, 'table', '');
if ($attributes['enableTableHeader']) {
$content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-header', 'class');
$bg_color = Utils::get_background_color($attributes, 'headerBackgroundColor', 'headerBackgroundGradient');
if ($bg_color) {
$content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style');
}
}
if ($attributes['enableTableFooter']) {
$footer_idx = strrpos($content, ' trim(join(' ', $wrapper_classes)),
]);
return '' . $content . '
';
}
/**
* Register the block.
*/
public function block_registration()
{
$defaults = new \Tableberg\Defaults();
$tableberg_assets = new \Tableberg\Assets();
$tableberg_assets->register_blocks_assets();
if (!is_admin()) {
$tableberg_assets->register_frontend_assets();
}
register_block_type(
TABLEBERG_DIR_PATH . 'build/block.json',
array(
'attributes' => $defaults->get_default_attributes('tableberg/table'),
'render_callback' => array($this, 'render_tableberg_table_block'),
)
);
}
}