feat: configure Related Stories rendering

This commit is contained in:
chrnx committed 2026-08-12 19:00:01 -05:00
1 parent 367d4db916
commit 3bc62358d7
4 files changed
+279 -8

No files matched your search

+2 -1
View File
@@ -9,7 +9,8 @@
"textdomain": "chrnx",
"usesContext": ["postId", "postType"],
"attributes": {
"limit": { "type": "integer", "default": 3, "minimum": 1, "maximum": 12 },
"limit": { "type": "integer", "default": 3, "enum": [3, 5] },
"layout": { "type": "string", "default": "adaptive", "enum": ["adaptive", "grid", "carousel"] },
"headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] }
},
"supports": { "html": false, "align": ["wide", "full"] },
+20 -5
View File
@@ -6,21 +6,36 @@
*/
$chrnx_post_id = isset( $block->context['postId'] ) ? absint( $block->context['postId'] ) : get_the_ID();
$chrnx_limit = isset( $attributes['limit'] ) ? min( 12, max( 1, absint( $attributes['limit'] ) ) ) : 3;
$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_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-related-posts' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="<?php echo esc_attr( $chrnx_title_id ); ?>">
<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__grid">
<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>
+239 -2
View File
@@ -801,6 +801,200 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertStringNotContainsString( '<img', $output );
}
/** Provide strict Related Stories limit inputs and their expected card counts. */
public function related_limit_attributes_provider(): array {
return array(
'missing' => array( array(), 3 ),
'one' => array( array( 'limit' => 1 ), 3 ),
'twelve' => array( array( 'limit' => 12 ), 3 ),
'string five' => array( array( 'limit' => '5' ), 3 ),
'negative' => array( array( 'limit' => -5 ), 3 ),
'array' => array( array( 'limit' => array( 5 ) ), 3 ),
'float' => array( array( 'limit' => 5.0 ), 3 ),
'integer five' => array( array( 'limit' => 5 ), 5 ),
);
}
/**
* Related Stories renders five only for the exact integer and defaults every other limit to three.
*
* @dataProvider related_limit_attributes_provider
*/
public function test_related_posts_strictly_allowlists_limit( array $attributes, int $expected_count ): void {
$current = $this->create_related_posts_fixture( 5 );
$output = $this->render_for_post( 'chrnx/related-posts', $current, $attributes );
$this->assertSame( $expected_count, $this->count_html_tags( $output, 'ARTICLE' ) );
}
/** Provide malformed Related Stories layouts that must fail closed. */
public function related_invalid_layout_attributes_provider(): array {
return array(
'missing' => array( array() ),
'empty' => array( array( 'layout' => '' ) ),
'unknown' => array( array( 'layout' => 'masonry' ) ),
'injected class' => array( array( 'layout' => 'carousel evil' ) ),
'integer' => array( array( 'layout' => 1 ) ),
'array' => array( array( 'layout' => array( 'grid' ) ) ),
);
}
/**
* Missing and malformed layouts render only the adaptive layout class.
*
* @dataProvider related_invalid_layout_attributes_provider
*/
public function test_related_posts_defaults_invalid_layouts_to_adaptive( array $attributes ): void {
$current = $this->create_related_posts_fixture( 1 );
$output = $this->render_for_post( 'chrnx/related-posts', $current, $attributes );
$this->assertSame( array( 'is-layout-adaptive' ), $this->get_related_layout_classes( $output ) );
}
/** Provide every approved Related Stories layout and its rail semantics. */
public function related_layout_provider(): array {
return array(
'adaptive' => array( 'adaptive', true ),
'grid' => array( 'grid', false ),
'carousel' => array( 'carousel', true ),
);
}
/**
* Approved layouts emit one exact class and only carousel-capable semantics and controls.
*
* @dataProvider related_layout_provider
*/
public function test_related_posts_render_layout_semantics( string $layout, bool $is_carousel_capable ): void {
$current = $this->create_related_posts_fixture( 1 );
$output = $this->render_for_post(
'chrnx/related-posts',
$current,
array( 'layout' => $layout )
);
$this->assertSame( array( 'is-layout-' . $layout ), $this->get_related_layout_classes( $output ) );
$processor = new WP_HTML_Tag_Processor( $output );
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'H2' ) ) );
$heading_id = $processor->get_attribute( 'id' );
$this->assertNotEmpty( $heading_id );
$this->assertTrue(
$processor->next_tag(
array(
'tag_name' => 'DIV',
'class_name' => 'chrnx-related-posts__items',
)
)
);
$this->assertTrue( $processor->get_attribute( 'data-chrnx-related-track' ) );
if ( $is_carousel_capable ) {
$this->assertSame( 'region', $processor->get_attribute( 'role' ) );
$this->assertSame( $heading_id, $processor->get_attribute( 'aria-labelledby' ) );
$this->assertSame( '0', $processor->get_attribute( 'tabindex' ) );
$this->assertTrue(
$processor->next_tag(
array(
'tag_name' => 'DIV',
'class_name' => 'chrnx-related-posts__controls',
)
)
);
$this->assertSame( 'group', $processor->get_attribute( 'role' ) );
$this->assertTrue( $processor->get_attribute( 'hidden' ) );
$this->assertSame( 2, $this->count_html_tags( $output, 'BUTTON' ) );
} else {
$this->assertNull( $processor->get_attribute( 'role' ) );
$this->assertNull( $processor->get_attribute( 'aria-labelledby' ) );
$this->assertNull( $processor->get_attribute( 'tabindex' ) );
$this->assertFalse(
$processor->next_tag(
array(
'tag_name' => 'DIV',
'class_name' => 'chrnx-related-posts__controls',
)
)
);
$this->assertSame( 0, $this->count_html_tags( $output, 'BUTTON' ) );
}
}
/** Provide every non-empty partial Related Stories availability count. */
public function related_partial_count_provider(): array {
return array(
'one available' => array( 1 ),
'two available' => array( 2 ),
'four available' => array( 4 ),
);
}
/**
* Related Stories renders only the eligible cards that actually exist.
*
* @dataProvider related_partial_count_provider
*/
public function test_related_posts_render_partial_availability( int $available ): void {
$current = $this->create_related_posts_fixture( $available );
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'limit' => 5 ) );
$this->assertSame( $available, $this->count_html_tags( $output, 'ARTICLE' ) );
}
/** Related Stories returns no section when there are no eligible destinations. */
public function test_related_posts_render_nothing_without_results(): void {
$current = $this->create_related_posts_fixture( 0 );
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'limit' => 5 ) );
$this->assertSame( 0, $this->count_html_tags( $output, 'SECTION' ) );
$this->assertSame( '', $output );
}
/** Related Stories navigation labels pass through the chrnx translation domain. */
public function test_related_posts_localize_navigation_labels(): void {
$calls = array();
$translate = static function ( $translated, $text, $domain ) use ( &$calls ) {
$labels = array(
'Related stories navigation' => 'Navegación de historias',
'Previous stories' => 'Historias anteriores',
'Next stories' => 'Historias siguientes',
);
if ( isset( $labels[ $text ] ) ) {
$calls[] = array( $text, $domain );
return $labels[ $text ];
}
return $translated;
};
add_filter( 'gettext', $translate, 10, 3 );
try {
$current = $this->create_related_posts_fixture( 1 );
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'layout' => 'carousel' ) );
} finally {
remove_filter( 'gettext', $translate, 10 );
}
$this->assertSame(
array(
array( 'Related stories navigation', 'chrnx' ),
array( 'Previous stories', 'chrnx' ),
array( 'Next stories', 'chrnx' ),
),
$calls
);
$processor = new WP_HTML_Tag_Processor( $output );
$this->assertTrue(
$processor->next_tag(
array(
'tag_name' => 'DIV',
'class_name' => 'chrnx-related-posts__controls',
)
)
);
$this->assertSame( 'Navegación de historias', $processor->get_attribute( 'aria-label' ) );
$this->assertStringContainsString( '>Historias anteriores</button>', $output );
$this->assertStringContainsString( '>Historias siguientes</button>', $output );
}
/** Topic heroes are the section surface and prioritize their approved portrait. */
public function test_topic_hero_portrait_is_eager_and_high_priority() {
$output = render_block(
@@ -1085,7 +1279,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
}
/** Render one dynamic post-context block for a specific post. */
private function render_for_post( $block_name, $post_id ) {
private function render_for_post( $block_name, $post_id, $attributes = array() ) {
global $post;
$previous = $post;
$post = get_post( $post_id );
@@ -1094,7 +1288,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
return render_block(
array(
'blockName' => $block_name,
'attrs' => array(),
'attrs' => $attributes,
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
@@ -1105,4 +1299,47 @@ class BlockRenderingTest extends WP_UnitTestCase {
$post = $previous;
}
}
/** Create one current story and an exact number of eligible related stories. */
private function create_related_posts_fixture( $available ) {
$current = self::factory()->post->create( array( 'post_title' => 'Current related-story fixture' ) );
wp_set_object_terms( $current, 'research', 'chrnx_topic' );
for ( $index = 1; $index <= $available; ++$index ) {
$related = self::factory()->post->create( array( 'post_title' => 'Related fixture ' . $index ) );
wp_set_object_terms( $related, 'research', 'chrnx_topic' );
}
return $current;
}
/** Count real parsed HTML tags instead of matching markup substrings. */
private function count_html_tags( $html, $tag_name ) {
$processor = new WP_HTML_Tag_Processor( $html );
$count = 0;
while ( $processor->next_tag( array( 'tag_name' => $tag_name ) ) ) {
++$count;
}
return $count;
}
/** Return only parsed Related Stories layout classes from the section wrapper. */
private function get_related_layout_classes( $html ) {
$processor = new WP_HTML_Tag_Processor( $html );
$this->assertTrue(
$processor->next_tag(
array(
'tag_name' => 'SECTION',
'class_name' => 'chrnx-related-posts',
)
)
);
$classes = preg_split( '/\s+/', trim( (string) $processor->get_attribute( 'class' ) ) );
return array_values(
array_filter(
$classes,
static function ( $class_name ) {
return 0 === strpos( $class_name, 'is-layout-' );
}
)
);
}
}
@@ -0,0 +1,18 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';
test( 'Related Stories metadata exposes only the approved server attributes', async () => {
const metadata = JSON.parse( await readFile( 'blocks/related-posts/block.json', 'utf8' ) );
assert.deepEqual( metadata.attributes.limit, {
type: 'integer',
default: 3,
enum: [ 3, 5 ],
} );
assert.deepEqual( metadata.attributes.layout, {
type: 'string',
default: 'adaptive',
enum: [ 'adaptive', 'grid', 'carousel' ],
} );
} );