PluginProbe
Tableberg – Simple Gutenberg Table Block / 0.3.3
Tableberg – Simple Gutenberg Table Block v0.3.3
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 / Button.php

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

192 lines 5.8 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 $styles = array(
68 '--tableberg-button-background-color' => $background_color,
69 '--tableberg-button-hover-background-color' => $background_hover_color,
70 '--tableberg-button-text-hover-color' => $attributes['textHoverColor'],
71 '--tableberg-button-text-color' => $attributes['textColor'],
72 );
73
74 return Utils::generate_css_string( $styles );
75 }
76 /**
77 * Get block classes.
78 *
79 * @param array $attributes - block attributes.
80 * @return string Generated block classess.
81 */
82 public static function get_classes( $attributes ) {
83 if (!isset($attributes['backgroundColor'])) {
84 $attributes['backgroundColor'] = "";
85 }
86 if (!isset($attributes['backgroundHoverColor'])) {
87 $attributes['backgroundHoverColor'] = "";
88 }
89 if (!isset($attributes['textHoverColor'])) {
90 $attributes['textHoverColor'] = "";
91 }
92 if (!isset($attributes['textColor'])) {
93 $attributes['textColor'] = "";
94 }
95 if (!isset($attributes['backgroundGradient'])) {
96 $attributes['backgroundGradient'] = "";
97 }
98 if (!isset($attributes['backgroundHoverGradient'])) {
99 $attributes['backgroundHoverGradient'] = "";
100 }
101
102 $classes = join(
103 ' ',
104 array(
105 ! Utils::is_value_empty( $attributes['backgroundColor'] ) || ! Utils::is_value_empty( $attributes['backgroundGradient'] ) ? 'has-background-color' : '',
106 ! Utils::is_value_empty( $attributes['backgroundHoverColor'] ) || ! Utils::is_value_empty( $attributes['backgroundHoverGradient'] ) ? 'has-hover-background-color' : '',
107 ! Utils::is_value_empty( $attributes['textHoverColor'] ) ? 'has-hover-text-color' : '',
108 ! Utils::is_value_empty( $attributes['textColor'] ) ? 'has-text-color' : '',
109 )
110 );
111 return $classes;
112 }
113 /**
114 * Renders the custom button block on the server.
115 *
116 * @param array $attributes The block attributes.
117 * @param string $content The block content.
118 * @param WP_Block $block The block object.
119 * @return string Returns the HTML content for the custom button block.
120 */
121 public function render_tableberg_button_block( $attributes, $content, $block ) {
122 $text = isset( $attributes['text'] ) ? $attributes['text'] : '';
123 $align = isset( $attributes['align'] ) ? $attributes['align'] : '';
124 $width = isset( $attributes['width'] ) ? $attributes['width'] : '';
125 $text_align = isset( $attributes['textAlign'] ) ? $attributes['textAlign'] : '';
126 $id = isset( $attributes['id'] ) ? $attributes['id'] : '';
127 $url = isset( $attributes['url'] ) ? $attributes['url'] : '';
128 $link_target = isset( $attributes['linkTarget'] ) ? $attributes['linkTarget'] : '';
129 $rel = isset( $attributes['rel'] ) ? $attributes['rel'] : '';
130 $style = isset( $attributes['style'] ) ? $attributes['style'] : '';
131 $font_size = isset( $attributes['fontSize'] ) ? $attributes['fontSize'] : '';
132 $border = isset( $style['border'] ) ? $style['border'] : '';
133
134 $classes = trim(
135 join(
136 ' ',
137 array(
138 'wp-block-tableberg-button',
139 $align ? "block-align-{$align}" : '',
140 $width ? "has-custom-width wp-block-button__width-{$width}" : '',
141 $font_size ? 'has-custom-font-size' : '',
142 $this->get_classes( $attributes ),
143 )
144 )
145 );
146
147 $button_classes = trim(
148 join(
149 ' ',
150 array(
151 'wp-block-button__link',
152 'wp-element-button',
153 $text_align ? "has-text-align-{$text_align}" : '',
154 )
155 )
156 );
157 $button_styles = join(
158 '',
159 array(
160 $this->get_styles( $attributes ),
161 $this->get_border_style( $border ),
162 )
163 );
164
165 $button_attrs = sprintf( 'class="%1$s" style="%2$s"', esc_attr( $button_classes ), esc_attr( $button_styles ) );
166
167 $button = $url ? sprintf(
168 '<a href="%1$s" %2$s rel="%3$s" target="%4$s">%5$s</a>',
169 $url,
170 $button_attrs,
171 esc_attr( $rel ),
172 esc_attr( $link_target ),
173 esc_html( $text )
174 ) :
175 sprintf( '<div %1$s>%2$s</div>', $button_attrs, esc_html( $text ) );
176
177 return sprintf( '<div class="%1$s" id="%2$s">%3$s</div>', $classes, esc_attr( $id ), $button );
178 }
179
180 /**
181 * Register the block.
182 */
183 public function block_registration() {
184 register_block_type(
185 TABLEBERG_DIR_PATH . 'build/button/block.json',
186 array(
187 'render_callback' => array( $this, 'render_tableberg_button_block' ),
188 )
189 );
190 }
191 }
192