| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cell Block |
| 4 |
* |
| 5 |
* @package Tableberg |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Tableberg\Blocks; |
| 9 |
|
| 10 |
use Tableberg; |
| 11 |
use \WP_HTML_Tag_Processor; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handle the block registration on server side and rendering. |
| 15 |
*/ |
| 16 |
class Cell { |
| 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 |
* Renders the custom cell block on the server. |
| 29 |
* |
| 30 |
* @param array $attributes The block attributes. |
| 31 |
* @param string $content The block content. |
| 32 |
* @param WP_Block $block The block object. |
| 33 |
* @return string Returns the HTML content for the custom cell block. |
| 34 |
*/ |
| 35 |
public function render_tableberg_cell_block( $attributes, $content, $block ) { |
| 36 |
$vertical_align = $attributes['vAlign'] ?? ''; |
| 37 |
|
| 38 |
$td = new WP_HTML_Tag_Processor( $content ); |
| 39 |
|
| 40 |
if ( $td->next_tag( 'td' ) && $vertical_align ) { |
| 41 |
$td->add_class( "align-v-{$vertical_align}" ); |
| 42 |
} |
| 43 |
|
| 44 |
return $td; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Register the block. |
| 49 |
*/ |
| 50 |
public function block_registration() { |
| 51 |
$defaults = new \Tableberg\Defaults(); |
| 52 |
register_block_type( |
| 53 |
TABLEBERG_DIR_PATH . 'build/cell/block.json', |
| 54 |
array( |
| 55 |
'attributes' => $defaults->get_default_attributes( 'tableberg/cell' ), |
| 56 |
'render_callback' => array( $this, 'render_tableberg_cell_block' ), |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|