carhop__plugins__PROD-DEV/plugins/carhop-blocks/build/content-box/render.php
Antoine M d68507a55b
All checks were successful
continuous-integration/drone/push Build is passing
FEATURE Transforming content box to be dynamic to avoid wp kses break block with non admin
2026-06-01 10:02:49 +02:00

111 lines
4.7 KiB
PHP

<?php
/**
* Rendu serveur : évite de persister du SVG dans le post
* (filtré par KSES pour les rôles sans unfiltered_html).
*
* @var array $attributes Attributs du bloc.
* @var string $content HTML des blocs internes (InnerBlocks).
* @var WP_Block $block Instance du bloc.
*/
$background_color = $attributes['backgroundColor'] ?? '#f1fcf9';
$text_color = $attributes['textColor'] ?? 'inherit';
$has_light_background = ! empty($attributes['hasLightBackground']);
$block_variant = $attributes['blockVariant'] ?? 'backgrounded';
$shape_type = $attributes['shapeType'] ?? 'straight';
$border_color = $attributes['borderColor'] ?? null;
$align = $attributes['align'] ?? '';
if ('' === $align && ! empty($attributes['blockWidth'])) {
$block_width = $attributes['blockWidth'];
$align = 'contained' === $block_width ? '' : $block_width;
}
// Ancien save statique : le HTML complet est déjà dans $content.
$is_legacy_static = $content && (
str_contains($content, 'content-box content-box--variant-')
|| str_contains($content, 'class="content-box ')
);
if ($is_legacy_static) {
echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
return;
}
$lightness_class = $has_light_background ? 'content-box--bg-light' : 'content-box--bg-dark';
$align_class = 'aligncontained';
if ('full' === $align) {
$align_class = 'alignfull';
} elseif ('wide' === $align) {
$align_class = 'alignwide';
}
$bg_css = ('backgrounded' === $block_variant || 'framed-backgrounded' === $block_variant)
? $background_color
: 'transparent';
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => sprintf(
'content-box content-box--variant-%s content-box--%s %s %s',
esc_attr($block_variant),
esc_attr($shape_type),
esc_attr($lightness_class),
esc_attr($align_class)
),
'style' => sprintf(
'--content-box-text-color:%s;--content-box-background-color:%s',
esc_attr($text_color ? $text_color : 'inherit'),
esc_attr($bg_css)
),
),
isset($block) ? $block : null
);
$show_shapes = $background_color && in_array($shape_type, array('shapeA', 'shapeB', 'shapeC'), true);
$shape_fill = 'none';
$shape_stroke = 'none';
if ($show_shapes) {
if ('backgrounded' === $block_variant) {
$shape_fill = $background_color;
$shape_stroke = 'none';
} elseif ('framed' === $block_variant) {
$shape_fill = 'none';
$shape_stroke = $border_color ? $border_color : 'none';
} elseif ('framed-backgrounded' === $block_variant) {
$shape_fill = $background_color;
$shape_stroke = $border_color ? $border_color : 'none';
} else {
$show_shapes = false;
}
}
$shape_fill_attr = esc_attr($shape_fill);
$shape_stroke_attr = esc_attr($shape_stroke && 'none' !== $shape_stroke ? $shape_stroke : 'none');
$stroke_width = ($shape_stroke && 'none' !== $shape_stroke) ? '2px' : '0';
?>
<section <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
<?php if ($show_shapes && 'shapeA' === $shape_type) : ?>
<svg width="1440" height="744" viewBox="0 0 1440 744" fill="none" xmlns="http://www.w3.org/2000/svg" class="content-box__shape" preserveAspectRatio="none" vector-effect="non-scaling-stroke" overflow="visible" aria-hidden="true" focusable="false">
<path d="M0 0H1440V686.701L0 744V0Z" fill="<?php echo $shape_fill_attr; ?>" stroke="<?php echo $shape_stroke_attr; ?>" stroke-width="<?php echo esc_attr($stroke_width); ?>" style="stroke-linejoin:round;vector-effect:non-scaling-stroke"></path>
</svg>
<?php endif; ?>
<?php if ($show_shapes && 'shapeB' === $shape_type) : ?>
<svg class="content-box__shape" width="1302" height="654" viewBox="0 0 1302 654" preserveAspectRatio="none" vector-effect="non-scaling-stroke" overflow="visible" aria-hidden="true" focusable="false">
<path d="M1302 0L0 15.8281V654L1302 642.633L1302 0Z" fill="<?php echo $shape_fill_attr; ?>" stroke="<?php echo $shape_stroke_attr; ?>" stroke-width="<?php echo esc_attr($stroke_width); ?>" style="stroke-linejoin:round;vector-effect:non-scaling-stroke"></path>
</svg>
<?php endif; ?>
<?php if ($show_shapes && 'shapeC' === $shape_type) : ?>
<svg width="1440" height="997" viewBox="0 0 1440 997" class="content-box__shape" preserveAspectRatio="none" vector-effect="non-scaling-stroke" overflow="visible" aria-hidden="true" focusable="false">
<path d="M1440 29.8887L0 0V997L1440 979.242V29.8887Z" fill="<?php echo $shape_fill_attr; ?>" stroke="<?php echo $shape_stroke_attr; ?>" stroke-width="<?php echo esc_attr($stroke_width); ?>" style="stroke-linejoin:round;vector-effect:non-scaling-stroke"></path>
</svg>
<?php endif; ?>
<div class="content-box__innerblocks">
<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
</div>
</section>