| 1 |
<?php |
| 2 |
/** |
| 3 |
* Image 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 Image { |
| 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 |
* Check is border split |
| 26 |
* |
| 27 |
* @param array $border - block border. |
| 28 |
*/ |
| 29 |
public function has_split_borders( $border = array() ) { |
| 30 |
$sides = array( 'top', 'right', 'bottom', 'left' ); |
| 31 |
foreach ( $border as $side => $value ) { |
| 32 |
if ( in_array( $side, $sides, true ) ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get the border from attributes |
| 42 |
* |
| 43 |
* @param array $object - block border. |
| 44 |
*/ |
| 45 |
public function get_splitted_border( $object ) { |
| 46 |
$css = array(); |
| 47 |
|
| 48 |
if ( ! $this->has_split_borders( $object ) ) { |
| 49 |
$css['top'] = $object; |
| 50 |
$css['right'] = $object; |
| 51 |
$css['bottom'] = $object; |
| 52 |
$css['left'] = $object; |
| 53 |
return $css; |
| 54 |
} |
| 55 |
|
| 56 |
return $object; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the single side of border |
| 61 |
* |
| 62 |
* @param array $border - border. |
| 63 |
* @param string $side - border side. |
| 64 |
*/ |
| 65 |
public function get_single_side_border_value( $border, $side ) { |
| 66 |
$width = $border[ $side ]['width'] ?? ''; |
| 67 |
$style = $border[ $side ]['style'] ?? ''; |
| 68 |
$color = $border[ $side ]['color'] ?? ''; |
| 69 |
|
| 70 |
return "{$width} " . ( $width && empty( $border[ $side ]['style'] ) ? 'solid' : $style ) . " {$color}"; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get border variables |
| 75 |
* |
| 76 |
* @param array $border_attribute - border. |
| 77 |
*/ |
| 78 |
public function get_border_variables_css( $border_attribute ) { |
| 79 |
$border_sides = array( 'top', 'right', 'bottom', 'left' ); |
| 80 |
$splitted_borders = $this->get_splitted_border( $border_attribute ); |
| 81 |
$borders = array(); |
| 82 |
|
| 83 |
foreach ( $border_sides as $side ) { |
| 84 |
$side_property = "border-$side"; |
| 85 |
$side_value = $this->get_single_side_border_value( $splitted_borders, $side ); |
| 86 |
|
| 87 |
$borders[ $side_property ] = $side_value; |
| 88 |
} |
| 89 |
|
| 90 |
return $borders; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Build the CSS style for the image element. |
| 95 |
* |
| 96 |
* @param array $borders The borders. |
| 97 |
* @param string $aspect_ratio The aspect ratio. |
| 98 |
* @param string $scale The scaling method. |
| 99 |
* @param int $width The width of the image. |
| 100 |
* @param int $height The height of the image. |
| 101 |
* @param array $border_radius The border radius of the image. |
| 102 |
* @return string The CSS style string. |
| 103 |
*/ |
| 104 |
public function build_image_style( $borders, $aspect_ratio, $scale, $width, $height, $border_radius ) { |
| 105 |
$style = ''; |
| 106 |
|
| 107 |
if ( $aspect_ratio && ! empty( $aspect_ratio ) ) { |
| 108 |
$style .= "aspect-ratio: $aspect_ratio;"; |
| 109 |
} |
| 110 |
|
| 111 |
if ( $scale && ! empty( $scale ) ) { |
| 112 |
$style .= "object-fit: $scale;"; |
| 113 |
} |
| 114 |
|
| 115 |
if ( $width && ! empty( $width ) ) { |
| 116 |
$style .= "width: {$width};"; |
| 117 |
} |
| 118 |
|
| 119 |
if ( $height && ! empty( $height ) ) { |
| 120 |
$style .= "height: {$height};"; |
| 121 |
} |
| 122 |
|
| 123 |
if ( $borders && ! empty( $borders ) ) { |
| 124 |
foreach ( $borders as $property => $value ) { |
| 125 |
$style .= "$property:$value;"; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( $border_radius && ! empty( $border_radius ) ) { |
| 130 |
if ( isset( $border_radius['topLeft'] ) ) { |
| 131 |
$style .= 'border-top-left-radius:' . $border_radius['topLeft'] . ';'; |
| 132 |
} |
| 133 |
if ( isset( $border_radius['topRight'] ) ) { |
| 134 |
$style .= 'border-top-right-radius:' . $border_radius['topRight'] . ';'; |
| 135 |
} |
| 136 |
if ( isset( $border_radius['bottomLeft'] ) ) { |
| 137 |
$style .= 'border-bottom-left-radius:' . $border_radius['bottomLeft'] . ';'; |
| 138 |
} |
| 139 |
if ( isset( $border_radius['bottomLeft'] ) ) { |
| 140 |
$style .= 'border-bottom-right-radius:' . $border_radius['bottomRight'] . ';'; |
| 141 |
} |
| 142 |
} |
| 143 |
return $style; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Renders the custom image block on the server. |
| 148 |
* |
| 149 |
* @param array $attributes The block attributes. |
| 150 |
* @param string $content The block content. |
| 151 |
* @param WP_Block $block The block object. |
| 152 |
* @return string Returns the HTML content for the custom image block. |
| 153 |
*/ |
| 154 |
public function render_tableberg_image_block( $attributes, $content, $block ) { |
| 155 |
$size_slug = $attributes['sizeSlug']; |
| 156 |
$media = $attributes['media']; |
| 157 |
$url = isset( $media['sizes'][ $size_slug ]['url'] ) ? $media['sizes'][ $size_slug ]['url'] : $media['url'] ?? ''; |
| 158 |
$alt = $attributes['alt']; |
| 159 |
$caption = $attributes['caption']; |
| 160 |
$align = $attributes['align'] ?? ''; |
| 161 |
$href = $attributes['href']; |
| 162 |
$rel = $attributes['rel']; |
| 163 |
$link_class = $attributes['linkClass']; |
| 164 |
$width = $attributes['width']; |
| 165 |
$height = $attributes['height']; |
| 166 |
$aspect_ratio = $attributes['aspectRatio']; |
| 167 |
$scale = $attributes['scale']; |
| 168 |
$id = isset( $media['id'] ); |
| 169 |
$link_target = $attributes['linkTarget']; |
| 170 |
|
| 171 |
$new_rel = ! empty( $rel ) ? ' rel = "' . esc_attr( $rel ) . '"' : ''; |
| 172 |
$border = $this->get_border_variables_css( $attributes['border'] ); |
| 173 |
$border_radius = $attributes['borderRadius']; |
| 174 |
|
| 175 |
$classes = join( |
| 176 |
' ', |
| 177 |
array( |
| 178 |
'wp - block - tableberg - image', |
| 179 |
'align' . $align, |
| 180 |
'size - ' . $size_slug, |
| 181 |
( $width || $height ) ? 'is - resized' : '', |
| 182 |
) |
| 183 |
); |
| 184 |
|
| 185 |
$image_classes = join( |
| 186 |
' ', |
| 187 |
array_filter( |
| 188 |
array( |
| 189 |
$id ? 'wp - image - ' . esc_attr( $id ) : '', |
| 190 |
) |
| 191 |
) |
| 192 |
); |
| 193 |
|
| 194 |
$image = sprintf( |
| 195 |
'<img src="%1$s" alt="%2$s" class="%3$s" style="%4$s" />', |
| 196 |
esc_url( $url ), |
| 197 |
esc_attr( $alt ), |
| 198 |
esc_attr( $image_classes ), |
| 199 |
$this->build_image_style( $border, $aspect_ratio, $scale, $width, $height, $border_radius ), |
| 200 |
); |
| 201 |
|
| 202 |
$figure = ''; |
| 203 |
|
| 204 |
if ( $href ) { |
| 205 |
$figure = sprintf( |
| 206 |
'<a href="%1$s" class="%2$s" target="%3$s"%4$s>%5$s</a>', |
| 207 |
esc_url( $href ), |
| 208 |
esc_attr( $link_class ), |
| 209 |
esc_attr( $link_target ), |
| 210 |
$new_rel, |
| 211 |
$image |
| 212 |
); |
| 213 |
} else { |
| 214 |
$figure = $image; |
| 215 |
} |
| 216 |
|
| 217 |
if ( ! empty( $caption ) ) { |
| 218 |
$figure .= sprintf( |
| 219 |
'<figcaption class="wp-element-caption">%1$s</figcaption>', |
| 220 |
esc_html( $caption ) |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
return sprintf( |
| 225 |
'<figure class="%1$s">%2$s</figure>', |
| 226 |
esc_attr( $classes ), |
| 227 |
$figure |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Register the block. |
| 233 |
*/ |
| 234 |
public function block_registration() { |
| 235 |
register_block_type( |
| 236 |
TABLEBERG_DIR_PATH . 'build/image/block.json', |
| 237 |
array( |
| 238 |
'render_callback' => array( $this, 'render_tableberg_image_block' ), |
| 239 |
) |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|