| 1 |
<?php |
| 2 |
/** |
| 3 |
* Table Block |
| 4 |
* |
| 5 |
* @package Tableberg |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Tableberg\Blocks; |
| 9 |
|
| 10 |
use WP_Block; |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle the block registration on server side and rendering. |
| 14 |
*/ |
| 15 |
class Table { |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
add_action( 'init', array( $this, 'block_registration' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get block styles. |
| 28 |
* |
| 29 |
* @param array $attributes - block attributes. |
| 30 |
* @return string Generated CSS styles. |
| 31 |
*/ |
| 32 |
public static function get_styles( $attributes ) { |
| 33 |
$header_bg_color = \Tableberg\Utils::get_background_color( $attributes, 'headerBackgroundColor', 'headerBackgroundGradient' ); |
| 34 |
$even_row_bg_color = \Tableberg\Utils::get_background_color( $attributes, 'evenRowBackgroundColor', 'evenRowBackgroundGradient' ); |
| 35 |
$odd_row_bg_color = \Tableberg\Utils::get_background_color( $attributes, 'oddRowBackgroundColor', 'oddRowBackgroundGradient' ); |
| 36 |
$footer_bg_color = \Tableberg\Utils::get_background_color( $attributes, 'footerBackgroundColor', 'footerBackgroundGradient' ); |
| 37 |
|
| 38 |
$cell_padding = \Tableberg\Utils::get_spacing_css( $attributes['cellPadding'] ); |
| 39 |
|
| 40 |
$table_border_variables = \Tableberg\Utils::get_border_variables_css( $attributes['tableBorder'], 'table' ); |
| 41 |
$inner_border_variables = $attributes['enableInnerBorder'] ? \Tableberg\Utils::get_border_variables_css( $attributes['innerBorder'], 'inner' ) : array(); |
| 42 |
|
| 43 |
$styles = array( |
| 44 |
'--tableber-table-width' => $attributes['tableWidth'], |
| 45 |
'--tableberg-header-bg-color' => $header_bg_color, |
| 46 |
'--tableberg-even-row-bg-color' => $even_row_bg_color, |
| 47 |
'--tableberg-odd-row-bg-color' => $odd_row_bg_color, |
| 48 |
'--tableberg-footer-bg-color' => $footer_bg_color, |
| 49 |
'--tableberg-cell-padding-top' => $cell_padding['top'] ?? '', |
| 50 |
'--tableberg-cell-padding-right' => $cell_padding['right'] ?? '', |
| 51 |
'--tableberg-cell-padding-bottom' => $cell_padding['bottom'] ?? '', |
| 52 |
'--tableberg-cell-padding-left' => $cell_padding['left'] ?? '', |
| 53 |
) + $table_border_variables + $inner_border_variables; |
| 54 |
|
| 55 |
return \Tableberg\Utils::generate_css_string( $styles ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Class if styling is applied. |
| 60 |
* |
| 61 |
* @param array $attributes The block attributes. |
| 62 |
* @return array CSS classes based on attributes. |
| 63 |
*/ |
| 64 |
public function get_style_class( $attributes ) { |
| 65 |
$table_width = $attributes['tableWidth']; |
| 66 |
$table_alignment = $attributes['tableAlignment']; |
| 67 |
$enable_inner_border = $attributes['enableInnerBorder']; |
| 68 |
$classes = array(); |
| 69 |
if ( $enable_inner_border ) { |
| 70 |
$classes[] = 'has-inner-border'; |
| 71 |
} |
| 72 |
$is_value_empty = function( $value ) { |
| 73 |
return ( |
| 74 |
is_null( $value ) || |
| 75 |
false === $value || |
| 76 |
trim( $value ) === '' || |
| 77 |
trim( $value ) === 'undefined undefined undefined' || |
| 78 |
empty( $value ) |
| 79 |
); |
| 80 |
}; |
| 81 |
|
| 82 |
if ( ! $is_value_empty( $table_width ) ) { |
| 83 |
$classes[] = 'has-table-width'; |
| 84 |
} |
| 85 |
if ( ! $is_value_empty( $table_alignment ) ) { |
| 86 |
$classes[] = 'justify-table-' . $table_alignment; |
| 87 |
} |
| 88 |
|
| 89 |
return $classes; |
| 90 |
} |
| 91 |
/** |
| 92 |
* Renders the custom table block on the server. |
| 93 |
* |
| 94 |
* @param array $attributes The block attributes. |
| 95 |
* @param string $content The block content. |
| 96 |
* @param WP_Block $block The block object. |
| 97 |
* @return string Returns the HTML content for the custom table block. |
| 98 |
*/ |
| 99 |
public function render_tableberg_table_block( $attributes, $content, $block ) { |
| 100 |
$class_names = $this->get_style_class( $attributes ); |
| 101 |
$style = $this->get_styles( $attributes ); |
| 102 |
$wrapper_attributes = get_block_wrapper_attributes( |
| 103 |
array( |
| 104 |
'class' => trim( join( ' ', $class_names ) ), |
| 105 |
'style' => $style, |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
$pattern = '/<table[^>]*>/s'; |
| 110 |
if ( preg_match( $pattern, $content, $matches ) ) { |
| 111 |
$table_element = $matches[0]; |
| 112 |
|
| 113 |
$content = str_replace( $table_element, '<table ' . $wrapper_attributes . ' >', $content ); |
| 114 |
} |
| 115 |
return $content; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Register the block. |
| 120 |
*/ |
| 121 |
public function block_registration() { |
| 122 |
$defaults = new \Tableberg\Defaults(); |
| 123 |
register_block_type( |
| 124 |
TABLEBERG_DIR_PATH . 'build/block.json', |
| 125 |
array( |
| 126 |
'attributes' => $defaults->get_default_attributes( 'tableberg/table' ), |
| 127 |
'render_callback' => array( $this, 'render_tableberg_table_block' ), |
| 128 |
) |
| 129 |
); |
| 130 |
} |
| 131 |
} |
| 132 |
|