PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.2.1
Tableberg – Simple Gutenberg Table Block v0.2.1
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / includes / Blocks / Table.php

Table.php in Tableberg – Simple Gutenberg Table Block 0.2.1, at includes/Blocks/Table.php

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