PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.4.1
Tableberg – Simple Gutenberg Table Block v0.4.1
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.4.1, at includes/Blocks/Button.php

203 lines 6.1 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 use Tableberg\Utils\Utils;
11
12 /**
13 * Handle the block registration on server side and rendering.
14 */
15 class Button {
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 border CSS styles from border style array
28 *
29 * @param array $border_style - border style array.
30 * @return string border CSS inline style string
31 */
32 public function get_border_style( $border_style ) {
33 if ( ! is_array( $border_style ) ) {
34 return "";
35 }
36
37 $radius = $border_style['radius'];
38 if ( ! is_array( $radius ) ) {
39 return "border-radius: {$radius};";
40 }
41
42 if ( is_array( $radius ) && count( $border_style['radius'] ) === 4 ) {
43 return "border-top-left-radius: {$border_style['radius']['topLeft']};" .
44 "border-bottom-left-radius: {$border_style['radius']['bottomLeft']};" .
45 "border-bottom-right-radius: {$border_style['radius']['bottomRight']};" .
46 "border-top-right-radius: {$border_style['radius']['topRight']};";
47 }
48 return "";
49 }
50 /**
51 * Get block styles.
52 *
53 * @param array $attributes - block attributes.
54 * @return string Generated CSS styles.
55 */
56 public static function get_styles( $attributes ) {
57 if (!isset($attributes['textHoverColor'])) {
58 $attributes['textHoverColor'] = "";
59 }
60 if (!isset($attributes['textColor'])) {
61 $attributes['textColor'] = "";
62 }
63
64 $background_color = Utils::get_background_color( $attributes, 'backgroundColor', 'backgroundGradient' );
65 $background_hover_color = Utils::get_background_color( $attributes, 'backgroundHoverColor', 'backgroundHoverGradient' );
66
67
68 $styles = array(
69 '--tableberg-button-background-color' => $background_color,
70 '--tableberg-button-hover-background-color' => $background_hover_color,
71 '--tableberg-button-text-hover-color' => $attributes['textHoverColor'],
72 '--tableberg-button-text-color' => $attributes['textColor']
73 );
74
75 if (isset($attributes['padding'])) {
76 $p = Utils::get_spacing_css($attributes['padding']);
77 $styles += [
78 'padding-top' => $p['top']??'',
79 'padding-right' => $p['right']??'',
80 'padding-bottom' => $p['bottom']??'',
81 'padding-left' => $p['left']??'',
82 ];
83 }
84
85 return Utils::generate_css_string( $styles );
86 }
87 /**
88 * Get block classes.
89 *
90 * @param array $attributes - block attributes.
91 * @return string Generated block classess.
92 */
93 public static function get_classes( $attributes ) {
94 if (!isset($attributes['backgroundColor'])) {
95 $attributes['backgroundColor'] = "";
96 }
97 if (!isset($attributes['backgroundHoverColor'])) {
98 $attributes['backgroundHoverColor'] = "";
99 }
100 if (!isset($attributes['textHoverColor'])) {
101 $attributes['textHoverColor'] = "";
102 }
103 if (!isset($attributes['textColor'])) {
104 $attributes['textColor'] = "";
105 }
106 if (!isset($attributes['backgroundGradient'])) {
107 $attributes['backgroundGradient'] = "";
108 }
109 if (!isset($attributes['backgroundHoverGradient'])) {
110 $attributes['backgroundHoverGradient'] = "";
111 }
112
113 $classes = join(
114 ' ',
115 array(
116 ! Utils::is_value_empty( $attributes['backgroundColor'] ) || ! Utils::is_value_empty( $attributes['backgroundGradient'] ) ? 'has-background-color' : '',
117 ! Utils::is_value_empty( $attributes['backgroundHoverColor'] ) || ! Utils::is_value_empty( $attributes['backgroundHoverGradient'] ) ? 'has-hover-background-color' : '',
118 ! Utils::is_value_empty( $attributes['textHoverColor'] ) ? 'has-hover-text-color' : '',
119 ! Utils::is_value_empty( $attributes['textColor'] ) ? 'has-text-color' : '',
120 )
121 );
122 return $classes;
123 }
124 /**
125 * Renders the custom button block on the server.
126 *
127 * @param array $attributes The block attributes.
128 * @param string $content The block content.
129 * @param WP_Block $block The block object.
130 * @return string Returns the HTML content for the custom button block.
131 */
132 public function render_tableberg_button_block( $attributes, $content, $block ) {
133 $text = isset( $attributes['text'] ) ? $attributes['text'] : '';
134 $align = isset( $attributes['align'] ) ? $attributes['align'] : '';
135 $width = isset( $attributes['width'] ) ? $attributes['width'] : '';
136 $text_align = isset( $attributes['textAlign'] ) ? $attributes['textAlign'] : '';
137 $id = isset( $attributes['id'] ) ? $attributes['id'] : '';
138 $url = isset( $attributes['url'] ) ? $attributes['url'] : '';
139 $link_target = isset( $attributes['linkTarget'] ) ? $attributes['linkTarget'] : '';
140 $rel = isset( $attributes['rel'] ) ? $attributes['rel'] : '';
141 $style = isset( $attributes['style'] ) ? $attributes['style'] : '';
142 $font_size = isset( $attributes['fontSize'] ) ? $attributes['fontSize'] : '';
143 $border = isset( $style['border'] ) ? $style['border'] : '';
144
145 $classes = trim(
146 join(
147 ' ',
148 array(
149 'wp-block-tableberg-button',
150 $align ? "block-align-{$align}" : '',
151 $width ? "has-custom-width wp-block-button__width-{$width}" : '',
152 $font_size ? 'has-custom-font-size' : '',
153 $this->get_classes( $attributes ),
154 )
155 )
156 );
157
158 $button_classes = trim(
159 join(
160 ' ',
161 array(
162 'wp-block-button__link',
163 'wp-element-button',
164 $text_align ? "has-text-align-{$text_align}" : '',
165 )
166 )
167 );
168 $button_styles = join(
169 '',
170 array(
171 $this->get_styles( $attributes ),
172 $this->get_border_style( $border ),
173 )
174 );
175
176 $button_attrs = sprintf( 'class="%1$s" style="%2$s"', esc_attr( $button_classes ), esc_attr( $button_styles ) );
177
178 $button = $url ? sprintf(
179 '<a href="%1$s" %2$s rel="%3$s" target="%4$s">%5$s</a>',
180 $url,
181 $button_attrs,
182 esc_attr( $rel ),
183 esc_attr( $link_target ),
184 esc_html( $text )
185 ) :
186 sprintf( '<div %1$s>%2$s</div>', $button_attrs, esc_html( $text ) );
187
188 return sprintf( '<div class="%1$s" id="%2$s">%3$s</div>', $classes, esc_attr( $id ), $button );
189 }
190
191 /**
192 * Register the block.
193 */
194 public function block_registration() {
195 register_block_type(
196 TABLEBERG_DIR_PATH . 'build/button/block.json',
197 array(
198 'render_callback' => array( $this, 'render_tableberg_button_block' ),
199 )
200 );
201 }
202 }
203