# tableberg/0.5.2/includes/Blocks/Table.php

Tableberg – Simple Gutenberg Table Block, version 0.5.2. 307 lines.

- Page: https://pluginprobe.com/plugins/tableberg/0.5.2/code/includes/Blocks/Table.php
- Raw: https://pluginprobe.com/plugins/tableberg/0.5.2/raw/includes/Blocks/Table.php
- Modified: 2024-05-30T17:28:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/tableberg/0.5.2/code/includes/Blocks/Table.php#L10-L20`.

```php
<?php
/**
 * Table Block
 *
 * @package Tableberg
 */

namespace Tableberg\Blocks;

use Tableberg;
use Tableberg\Utils\Utils;
use Tableberg\Utils\HtmlUtils;
use WP_Block;

/**
 * Handle the block registration on server side and rendering.
 */
class Table
{

	public static $lastRow = null;
	public static $lastRows = 0;

	/**
	 * Constructor
	 *
	 * @return void
	 */
	public function __construct()
	{
		add_action('init', array($this, 'block_registration'));
	}

	/**
	 * Get block styles.
	 *
	 * @param array $attributes - block attributes.
	 * @return string Generated CSS styles.
	 */
	public static function get_styles($attributes)
	{
		$even_row_bg = Utils::get_background_color($attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient');
		$odd_row_bg = Utils::get_background_color($attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient');
		$header_bg = Utils::get_background_color($attributes, 'headerBackgroundColor', 'headerBackgroundGradient');
		$footer_bg = Utils::get_background_color($attributes, 'footerBackgroundColor', 'footerBackgroundGradient');


		$cellSpacing = $attributes['cellSpacing'] ?? [];
		$table_spacing = Utils::get_spacing_css($cellSpacing);


		$table_border_css = Utils::get_border_style($attributes['tableBorder']);
		$inner_border_variables = $attributes['enableInnerBorder'] ? Utils::get_border_variables_css($attributes['innerBorder'], 'inner') : [];
		$cell_radius = Utils::get_border_radius_var($attributes['cellBorderRadius'], '--tableberg-cell', $separateBorder);

		$styles = [
			'width' => $attributes['tableWidth'],
			'max-width' => $attributes['tableWidth'],
			'color' => $attributes['fontColor'] ?? '',
			'font-size' => $attributes['fontSize'] ?? '',
			'--tableberg-global-link-color' => $attributes['linkColor'] ?? '',
			'--tableberg-even-bg' => $even_row_bg,
			'--tableberg-odd-bg' => $odd_row_bg,
			'--tableberg-header-bg' => $header_bg,
			'--tableberg-footer-bg' => $footer_bg,
			'border-spacing' => ($table_spacing['top'] ?? 0) . ' ' . ($table_spacing['left'] ?? 0),
		]
			+ Utils::get_spacing_style($attributes['cellPadding'], '--tableberg-cell-padding')
			+ $inner_border_variables
			+ $cell_radius;

		foreach (['top', 'left'] as $k) {
			if (isset($cellSpacing[$k]) && $cellSpacing[$k] !== '0') {
				$separateBorder = true;
			}
		}

		if ($separateBorder) {
			$styles['border-collapse'] = 'separate';
		} else {
			$styles += $table_border_css;
			$styles['border-collapse'] = 'collapse';
		}

		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, $attributes)
	{
		$heights = $attributes['rowHeights'];
		$widths = $attributes['colWidths'];
		$lastIdx = 0;
		foreach ($heights as $height) {
			$idx = strpos($content, '<tr', $lastIdx);
			if ($height) {
				$content = HtmlUtils::append_attr_value($content, 'tr', "height:{$height} !important;", 'style', $idx);
			}
			$lastIdx = $idx + 1;
		}
		$colgroup = '<colgroup>';
		if ($attributes['fixedColWidth']) {
			$cols = sizeof($widths);
			$w = 100 / $cols.'%';
			for ($i = 0; $i < $cols; $i++) {
				$colgroup .= "<col width=\"$w\" style=\"min-width:$w;\"/>";
			}
		} else {
			foreach ($widths as $w) {
				$colgroup .= "<col width=\"$w\" style=\"min-width:$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;
		}



		$cursor = 0;
		$i = 0;
		$end = $attributes['rows'] - 1;
		if ($attributes['enableTableHeader']) {
			$i = 1;
			$content = HtmlUtils::append_attr_value($content, 'tr', '', 'class', 0, $cursor);
		}
		if ($attributes['enableTableFooter']) {
			$end -= 1;
		}


		for (; $i <= $end; $i++) {
			$content = HtmlUtils::append_attr_value($content, 'tr', $i % 2 ? 'tableberg-even-row' : 'tableberg-odd-row', 'class', $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 = "' . esc_attr(trim(join(' ', $table_class_names))) . '" style="' . $table_style . '"';

		$content = HtmlUtils::insert_inside_tag($content, 'table', '<tbody>');
		$content = HtmlUtils::replace_attrs_of_tag($content, 'table', $table_attrs);
		$content = HtmlUtils::replace_closing_tag($content, 'table', '</tr></tbody></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) {
				$bg_color = esc_attr($bg_color);
				$content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style');
			}

		}
		if ($attributes['enableTableFooter']) {
			$footer_idx = strrpos($content, '<tr');
			$content = HtmlUtils::append_attr_value($content, 'tr', ' tableberg-footer', 'class', $footer_idx);
			$bg_color = Utils::get_background_color($attributes, 'footerBackgroundColor', 'footerBackgroundGradient');
			if ($bg_color) {
				$bg_color = esc_attr($bg_color);
				$content = HtmlUtils::append_attr_value($content, 'tr', "background: {$bg_color} !important;", 'style', $footer_idx);
			}
		}


		$content = self::setRowColSizes($content, $attributes);
		$content = self::even_odd_rows($attributes, $content);

		$responsive = trim(self::get_responsiveness_metadata($attributes, 'mobile') . self::get_responsiveness_metadata($attributes, 'tablet'));

		if ($responsive) {

			$str = 'data-tableberg-header="' . $attributes['enableTableHeader'] . '" ';
			$str .= 'data-tableberg-footer="' . $attributes['enableTableFooter'] . '" ';
			$responsive = 'data-tableberg-responsive ' . $str . ' data-tableberg-rows="' . $attributes['rows'] . '" data-tableberg-cols="' . $attributes['cols'] . '" ' . $responsive;
			$content = HtmlUtils::add_attrs_to_tag($content, 'table', $responsive);
		}

		self::$lastRow = null;

		$wrapper_classes = ['wp-block-tableberg-wrapper'];
		$table_alignment = $attributes['tableAlignment'];
		if ($table_alignment && $table_alignment !== "center") {
			$wrapper_classes[] = 'justify-table-' . $table_alignment;
		}

		if ($attributes['stickyTopRow']) {
			$wrapper_classes[] = 'tableberg-sticky-top-row';
		}
		if ($attributes['stickyFirstCol']) {
			$wrapper_classes[] = 'tableberg-sticky-first-col';
		}
		if ($attributes['innerBorderType'] === 'col') {
			$wrapper_classes[] = 'tableberg-border-col-only';
		} elseif ($attributes['innerBorderType'] === 'row') {
			$wrapper_classes[] = 'tableberg-border-row-only';
		}

		$wrapper_attributes = get_block_wrapper_attributes([
			'class' => trim(join(' ', $wrapper_classes)),
		]);

		return '<div ' . $wrapper_attributes . ' >' . $content . '</div>';
	}


	/**
	 * Register the block.
	 */
	public function block_registration()
	{
		$tableberg_assets = new \Tableberg\Assets();
		$tableberg_assets->register_blocks_assets();
		if (!is_admin()) {
			$tableberg_assets->register_frontend_assets();
		}
		$jsonPath = TABLEBERG_DIR_PATH . 'build/block.json';
		$attrs = json_decode(file_get_contents($jsonPath), true)['attributes'];

		register_block_type_from_metadata(
			$jsonPath,
			[
				'attributes' => $attrs,
				'render_callback' => array($this, 'render_tableberg_table_block'),
			]
		);

	}
}

```
