42 lines
2.2 KiB
PHP
42 lines
2.2 KiB
PHP
<?php
|
|
/**
|
|
* Related Posts block render.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
$chrnx_post_id = isset( $block->context['postId'] ) ? absint( $block->context['postId'] ) : get_the_ID();
|
|
$chrnx_limit = isset( $attributes['limit'] ) && is_int( $attributes['limit'] ) && 5 === $attributes['limit'] ? 5 : 3;
|
|
$chrnx_layout = isset( $attributes['layout'] ) && is_string( $attributes['layout'] ) && in_array( $attributes['layout'], array( 'adaptive', 'grid', 'carousel' ), true )
|
|
? $attributes['layout']
|
|
: 'adaptive';
|
|
$chrnx_ids = chrnx_get_related_post_ids( $chrnx_post_id, $chrnx_limit );
|
|
if ( empty( $chrnx_ids ) ) {
|
|
return;
|
|
}
|
|
$chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 );
|
|
$chrnx_title_id = wp_unique_id( 'chrnx-related-posts-title-' );
|
|
$chrnx_carousel_capable = 'grid' !== $chrnx_layout;
|
|
$chrnx_section_class_name = 'chrnx-related-posts is-layout-' . $chrnx_layout;
|
|
?>
|
|
<section <?php echo get_block_wrapper_attributes( array( 'class' => $chrnx_section_class_name ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>">
|
|
<h<?php echo esc_attr( $chrnx_heading ); ?> id="<?php echo esc_attr( $chrnx_title_id ); ?>"><?php echo esc_html__( 'Related stories', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
|
<div class="chrnx-related-posts__items" data-chrnx-related-track
|
|
<?php if ( $chrnx_carousel_capable ) : ?>
|
|
role="region" aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>" tabindex="0"
|
|
<?php endif; ?>
|
|
>
|
|
<?php
|
|
foreach ( $chrnx_ids as $chrnx_related_id ) {
|
|
echo chrnx_get_editorial_card_markup( $chrnx_related_id, min( 6, $chrnx_heading + 1 ), 'chrnx-related-card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
?>
|
|
</div>
|
|
<?php if ( $chrnx_carousel_capable ) : ?>
|
|
<div class="chrnx-related-posts__controls" role="group" aria-label="<?php echo esc_attr__( 'Related stories navigation', 'chrnx' ); ?>" hidden>
|
|
<button type="button" data-chrnx-related-previous><?php echo esc_html__( 'Previous stories', 'chrnx' ); ?></button>
|
|
<button type="button" data-chrnx-related-next><?php echo esc_html__( 'Next stories', 'chrnx' ); ?></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
</section>
|