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