| 1 |
<?php |
| 2 |
/** |
| 3 |
* Server-side render for the b-blocks/timeline block. |
| 4 |
* |
| 5 |
* The block is hydrated on the front-end by view.js, which reads the |
| 6 |
* sanitized attributes from the data-attributes JSON payload and renders |
| 7 |
* the timeline. We output a single wrapping element here, escape all |
| 8 |
* attribute output, and avoid emitting any raw user-supplied HTML at |
| 9 |
* the PHP layer. |
| 10 |
* |
| 11 |
* @package bBlocks |
| 12 |
* |
| 13 |
* @var array $attributes Block attributes. |
| 14 |
* @var string $content Block inner HTML (unused). |
| 15 |
*/ |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
$bblocks_timeline_id = wp_unique_id( 'bBlocksTimeline-' ); |
| 22 |
|
| 23 |
// Allow-list the attributes we forward to the front-end so unexpected |
| 24 |
// keys are dropped before the JSON payload is rendered. |
| 25 |
$bblocks_timeline_allowed_layouts = array( 'left', 'right', 'alternating' ); |
| 26 |
$bblocks_timeline_allowed_variants = array( 'modern', 'classic', 'minimal' ); |
| 27 |
|
| 28 |
$bblocks_timeline_layout = isset( $attributes['layout'] ) ? sanitize_key( $attributes['layout'] ) : 'alternating'; |
| 29 |
if ( ! in_array( $bblocks_timeline_layout, $bblocks_timeline_allowed_layouts, true ) ) { |
| 30 |
$bblocks_timeline_layout = 'alternating'; |
| 31 |
} |
| 32 |
|
| 33 |
$bblocks_timeline_variant = isset( $attributes['variant'] ) ? sanitize_key( $attributes['variant'] ) : 'modern'; |
| 34 |
if ( ! in_array( $bblocks_timeline_variant, $bblocks_timeline_allowed_variants, true ) ) { |
| 35 |
$bblocks_timeline_variant = 'modern'; |
| 36 |
} |
| 37 |
|
| 38 |
$bblocks_timeline_line_color = isset( $attributes['lineColor'] ) ? sanitize_hex_color( $attributes['lineColor'] ) : ''; |
| 39 |
if ( empty( $bblocks_timeline_line_color ) ) { |
| 40 |
$bblocks_timeline_line_color = '#dee2e6'; |
| 41 |
} |
| 42 |
|
| 43 |
$bblocks_timeline_show_icons = ! empty( $attributes['showIcons'] ); |
| 44 |
|
| 45 |
$bblocks_timeline_items = array(); |
| 46 |
if ( isset( $attributes['items'] ) && is_array( $attributes['items'] ) ) { |
| 47 |
foreach ( $attributes['items'] as $bblocks_timeline_item ) { |
| 48 |
if ( ! is_array( $bblocks_timeline_item ) ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
$bblocks_timeline_accent = isset( $bblocks_timeline_item['accentColor'] ) |
| 53 |
? sanitize_hex_color( $bblocks_timeline_item['accentColor'] ) |
| 54 |
: ''; |
| 55 |
|
| 56 |
$bblocks_timeline_items[] = array( |
| 57 |
'date' => isset( $bblocks_timeline_item['date'] ) ? sanitize_text_field( $bblocks_timeline_item['date'] ) : '', |
| 58 |
'title' => isset( $bblocks_timeline_item['title'] ) ? wp_kses_post( $bblocks_timeline_item['title'] ) : '', |
| 59 |
'description' => isset( $bblocks_timeline_item['description'] ) ? wp_kses_post( $bblocks_timeline_item['description'] ) : '', |
| 60 |
'icon' => isset( $bblocks_timeline_item['icon'] ) ? sanitize_text_field( $bblocks_timeline_item['icon'] ) : '', |
| 61 |
'accentColor' => $bblocks_timeline_accent ? $bblocks_timeline_accent : '#0d6efd', |
| 62 |
); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$bblocks_timeline_payload = array( |
| 67 |
'layout' => $bblocks_timeline_layout, |
| 68 |
'variant' => $bblocks_timeline_variant, |
| 69 |
'lineColor' => $bblocks_timeline_line_color, |
| 70 |
'showIcons' => (bool) $bblocks_timeline_show_icons, |
| 71 |
'items' => $bblocks_timeline_items, |
| 72 |
); |
| 73 |
|
| 74 |
$bblocks_timeline_plan_class = ( class_exists( '\\BBlocks\\Inc\\Utils' ) && \BBlocks\Inc\Utils::isPro() ) ? 'pro' : 'free'; |
| 75 |
|
| 76 |
$bblocks_timeline_wrapper_classes = sprintf( |
| 77 |
'%s timeline-block timeline-variant-%s timeline-layout-%s', |
| 78 |
$bblocks_timeline_plan_class, |
| 79 |
$bblocks_timeline_variant, |
| 80 |
$bblocks_timeline_layout |
| 81 |
); |
| 82 |
?> |
| 83 |
<div |
| 84 |
<?php |
| 85 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() is properly escaped. |
| 86 |
echo get_block_wrapper_attributes( array( 'class' => $bblocks_timeline_wrapper_classes ) ); |
| 87 |
?> |
| 88 |
id="<?php echo esc_attr( $bblocks_timeline_id ); ?>" |
| 89 |
role="list" |
| 90 |
aria-label="<?php echo esc_attr__( 'Timeline', 'b-blocks' ); ?>" |
| 91 |
data-attributes='<?php echo esc_attr( wp_json_encode( $bblocks_timeline_payload ) ); ?>' |
| 92 |
></div> |
| 93 |
|