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 / Button.php

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

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