PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.0.2
Tableberg – Simple Gutenberg Table Block v0.0.2
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 0.5.8 All 41 releases
tableberg / includes / Blocks / Button.php

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

140 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Button Block
4 *
5 * @package Tableberg
6 */
7
8 namespace Tableberg\Blocks;
9
10 /**
11 * Handle the block registration on server side and rendering.
12 */
13 class Button {
14
15 /**
16 * Constructor
17 *
18 * @return void
19 */
20 public function __construct() {
21 add_action( 'init', array( $this, 'block_registration' ) );
22 }
23
24 /**
25 * Get border CSS styles from border style array
26 *
27 * @param array $border_style - border style array.
28 * @return string border CSS inline style string
29 */
30 public function get_border_style( $border_style ) {
31 if ( ! is_array( $border_style ) ) {
32 return;
33 }
34
35 $radius = $border_style['radius'];
36 if ( ! is_array( $radius ) ) {
37 return "border-radius: {$radius};";
38 }
39
40 if ( is_array( $radius ) && count( $border_style['radius'] ) === 4 ) {
41 return "border-top-left-radius: {$border_style['radius']['topLeft']};" .
42 "border-bottom-left-radius: {$border_style['radius']['bottomLeft']};" .
43 "border-bottom-right-radius: {$border_style['radius']['bottomRight']};" .
44 "border-top-right-radius: {$border_style['radius']['topRight']};";
45 }
46 }
47
48 /**
49 * Renders the custom button block on the server.
50 *
51 * @param array $attributes The block attributes.
52 * @param string $content The block content.
53 * @param WP_Block $block The block object.
54 * @return string Returns the HTML content for the custom button block.
55 */
56 public function render_tableberg_button_block( $attributes, $content, $block ) {
57 $text = isset( $attributes['text'] ) ? $attributes['text'] : '';
58 $align = isset( $attributes['align'] ) ? $attributes['align'] : '';
59 $width = isset( $attributes['width'] ) ? $attributes['width'] : '';
60 $background_color = isset( $attributes['backgroundColor'] ) ? $attributes['backgroundColor'] : '';
61 $text_color = isset( $attributes['textColor'] ) ? $attributes['textColor'] : '';
62 $background_hover_color =
63 $attributes['backgroundHoverColor'] ??
64 $attributes['background_color'] ??
65 'var(--wp--preset--color--primary)';
66 $text_hover_color =
67 $attributes['textHoverColor'] ??
68 $attributes['text_color'] ??
69 'var(--wp--preset--color--base)';
70 $text_align = isset( $attributes['textAlign'] ) ? $attributes['textAlign'] : '';
71 $id = isset( $attributes['id'] ) ? $attributes['id'] : '';
72 $url = isset( $attributes['url'] ) ? $attributes['url'] : '';
73 $link_target = isset( $attributes['linkTarget'] ) ? $attributes['linkTarget'] : '';
74 $rel = isset( $attributes['rel'] ) ? $attributes['rel'] : '';
75 $style = isset( $attributes['style'] ) ? $attributes['style'] : '';
76 $font_size = isset( $attributes['fontSize'] ) ? $attributes['fontSize'] : '';
77 $border = isset( $style['border'] ) ? $style['border'] : '';
78
79 $classes = trim(
80 join(
81 ' ',
82 array(
83 'wp-block-tableberg-button',
84 $align ? "block-align-{$align}" : '',
85 $width ? "has-custom-width wp-block-button__width-{$width}" : '',
86 $font_size ? 'has-custom-font-size' : '',
87 )
88 )
89 );
90
91 $button_classes = trim(
92 join(
93 ' ',
94 array(
95 'wp-block-button__link',
96 'wp-element-button',
97 $text_align ? "has-text-align-{$text_align}" : '',
98 )
99 )
100 );
101
102 $button_styles = join(
103 '',
104 array(
105 $text_color ? "color: {$text_color};" : '',
106 $background_color ? "background-color: {$background_color};" : '',
107 "--text-hover-color: {$text_hover_color};",
108 "--background-hover-color: {$background_hover_color};",
109 $this->get_border_style( $border ),
110 )
111 );
112
113 $button_attrs = sprintf( 'class="%1$s" style="%2$s"', esc_attr( $button_classes ), esc_attr( $button_styles ) );
114
115 $button = $url ? sprintf(
116 '<a href="%1$s" %2$s rel="%3$s" target="%4$s">%5$s</a>',
117 $url,
118 $button_attrs,
119 esc_attr( $rel ),
120 esc_attr( $link_target ),
121 esc_html( $text )
122 ) :
123 sprintf( '<div %1$s>%2$s</div>', $button_attrs, esc_html( $text ) );
124
125 return sprintf( '<div class="%1$s" id="%2$s">%3$s</div>', $classes, esc_attr( $id ), $button );
126 }
127
128 /**
129 * Register the block.
130 */
131 public function block_registration() {
132 register_block_type(
133 TABLEBERG_DIR_PATH . 'build/button/block.json',
134 array(
135 'render_callback' => array( $this, 'render_tableberg_button_block' ),
136 )
137 );
138 }
139 }
140