fix: validate CHRNX editorial fallbacks
This commit is contained in:
8 files changed
+232
-17
No files matched your search
@@ -14,10 +14,11 @@ $chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 )
|
||||
$chrnx_topic = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_topic'] ?? '', 'chrnx_topic' );
|
||||
$chrnx_language = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_language'] ?? '', 'chrnx_language' );
|
||||
$chrnx_search = $chrnx_args['s'] ?? '';
|
||||
$chrnx_form_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
?>
|
||||
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby="chrnx-journal-title">
|
||||
<h<?php echo esc_attr( $chrnx_heading ); ?> id="chrnx-journal-title"><?php echo esc_html__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
|
||||
<form class="chrnx-journal-stream__filters" method="get" action="<?php echo esc_url( remove_query_arg( array( 'chrnx_topic', 'chrnx_language', 's', 'paged' ) ) ); ?>">
|
||||
<form class="chrnx-journal-stream__filters" method="get" action="<?php echo esc_url( $chrnx_form_url ); ?>">
|
||||
<label>
|
||||
<span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span>
|
||||
<select name="chrnx_topic">
|
||||
@@ -74,18 +75,29 @@ $chrnx_search = $chrnx_args['s'] ?? '';
|
||||
),
|
||||
'strlen'
|
||||
);
|
||||
$chrnx_links = paginate_links(
|
||||
array(
|
||||
'total' => $chrnx_query->max_num_pages,
|
||||
'current' => $chrnx_args['paged'],
|
||||
'add_args' => $chrnx_filter_args,
|
||||
'type' => 'list',
|
||||
$chrnx_pages = array_unique(
|
||||
array_filter(
|
||||
array( 1, $chrnx_args['paged'] - 1, $chrnx_args['paged'], $chrnx_args['paged'] + 1, (int) $chrnx_query->max_num_pages ),
|
||||
static function ( $chrnx_page ) use ( $chrnx_query ) {
|
||||
return $chrnx_page >= 1 && $chrnx_page <= $chrnx_query->max_num_pages;
|
||||
}
|
||||
)
|
||||
);
|
||||
sort( $chrnx_pages, SORT_NUMERIC );
|
||||
?>
|
||||
<?php if ( is_string( $chrnx_links ) ) : ?>
|
||||
<nav class="chrnx-journal-stream__pagination" aria-label="<?php echo esc_attr__( 'Journal pages', 'chrnx' ); ?>"><?php echo wp_kses_post( $chrnx_links ); ?></nav>
|
||||
<?php endif; ?>
|
||||
<nav class="chrnx-journal-stream__pagination" aria-label="<?php echo esc_attr__( 'Journal pages', 'chrnx' ); ?>">
|
||||
<ul class="page-numbers">
|
||||
<?php foreach ( $chrnx_pages as $chrnx_page ) : ?>
|
||||
<li>
|
||||
<?php if ( $chrnx_page === $chrnx_args['paged'] ) : ?>
|
||||
<span class="page-numbers current" aria-current="page"><?php echo esc_html( number_format_i18n( $chrnx_page ) ); ?></span>
|
||||
<?php else : ?>
|
||||
<a class="page-numbers" href="<?php echo esc_url( chrnx_get_journal_page_url( $chrnx_page, $chrnx_filter_args ) ); ?>"><?php echo esc_html( number_format_i18n( $chrnx_page ) ); ?></a>
|
||||
<?php endif; ?>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</nav>
|
||||
<?php endif; ?>
|
||||
<?php else : ?>
|
||||
<p class="chrnx-journal-stream__empty"><?php echo esc_html__( 'No journal entries match these filters yet.', 'chrnx' ); ?></p>
|
||||
|
||||
@@ -13,5 +13,6 @@
|
||||
"headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] }
|
||||
},
|
||||
"supports": { "html": false, "align": ["wide", "full"] },
|
||||
"style": "file:./style.css",
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
.chrnx-related-card__image,
|
||||
.chrnx-related-card__fallback {
|
||||
aspect-ratio: 3 / 2;
|
||||
display: block;
|
||||
object-fit: cover;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.chrnx-related-card__fallback {
|
||||
background-color: var(--wp--preset--color--panel);
|
||||
background-image:
|
||||
linear-gradient(30deg, transparent 35%, color-mix(in srgb, var(--wp--preset--color--green) 65%, transparent) 36% 40%, transparent 41%),
|
||||
linear-gradient(135deg, var(--wp--preset--color--violet), var(--wp--preset--color--night));
|
||||
min-height: 10rem;
|
||||
}
|
||||
@@ -12,5 +12,6 @@
|
||||
"headingLevel": { "type": "integer", "default": 2, "enum": [2, 3, 4, 5, 6] }
|
||||
},
|
||||
"supports": { "html": false, "align": ["wide", "full"] },
|
||||
"style": "file:./style.css",
|
||||
"render": "file:./render.php"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
.chrnx-topic-hero__art {
|
||||
min-width: min(100%, 16rem);
|
||||
}
|
||||
|
||||
.chrnx-topic-hero__fallback {
|
||||
aspect-ratio: 4 / 5;
|
||||
background-color: var(--wp--preset--color--panel);
|
||||
background-image:
|
||||
radial-gradient(circle at 68% 28%, var(--wp--preset--color--green) 0 12%, transparent 13%),
|
||||
linear-gradient(145deg, var(--wp--preset--color--violet), var(--wp--preset--color--night));
|
||||
display: block;
|
||||
min-height: 16rem;
|
||||
width: 100%;
|
||||
}
|
||||
+64
-7
@@ -137,6 +137,66 @@ function chrnx_validate_editorial_term_slug( $value, $taxonomy ) {
|
||||
return $term instanceof WP_Term ? $term->slug : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a canonical public Journal page number.
|
||||
*
|
||||
* @param mixed $value Request-shaped page value.
|
||||
* @return int A page from 1 through 10000, or 1 for invalid input.
|
||||
*/
|
||||
function chrnx_parse_journal_page( $value ) {
|
||||
if ( is_int( $value ) ) {
|
||||
$page = $value;
|
||||
} elseif ( is_string( $value ) && preg_match( '/^[1-9][0-9]*$/', $value ) ) {
|
||||
$page = (int) $value;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return $page >= 1 && $page <= 10000 ? $page : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize and bound public Journal search text.
|
||||
*
|
||||
* @param mixed $value Request-shaped search value.
|
||||
* @return string Safe search text or an empty string.
|
||||
*/
|
||||
function chrnx_sanitize_journal_search( $value ) {
|
||||
if ( ! is_scalar( $value ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a Journal page URL from only validated public filters.
|
||||
*
|
||||
* @param int $page Requested page number.
|
||||
* @param array $filters Request-shaped Journal filters.
|
||||
* @return string Safe Journal URL.
|
||||
*/
|
||||
function chrnx_get_journal_page_url( int $page, array $filters ) {
|
||||
$base_url = strtok( get_pagenum_link( 1, false ), '?' );
|
||||
$args = array();
|
||||
foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) {
|
||||
$slug = chrnx_validate_editorial_term_slug( $filters[ $taxonomy ] ?? '', $taxonomy );
|
||||
if ( '' !== $slug ) {
|
||||
$args[ $taxonomy ] = $slug;
|
||||
}
|
||||
}
|
||||
$search = chrnx_sanitize_journal_search( $filters['s'] ?? '' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
$page = chrnx_parse_journal_page( $page );
|
||||
if ( $page > 1 ) {
|
||||
$args['paged'] = $page;
|
||||
}
|
||||
|
||||
return add_query_arg( $args, $base_url );
|
||||
}
|
||||
|
||||
/**
|
||||
* Build safe Journal WP_Query arguments from request-shaped data.
|
||||
*
|
||||
@@ -150,7 +210,7 @@ function chrnx_get_journal_query_args( array $request ): array {
|
||||
'post_status' => 'publish',
|
||||
'post__in' => ! empty( $eligible_ids ) ? $eligible_ids : array( 0 ),
|
||||
'posts_per_page' => 10,
|
||||
'paged' => isset( $request['paged'] ) && is_scalar( $request['paged'] ) ? max( 1, intval( $request['paged'] ) ) : 1,
|
||||
'paged' => chrnx_parse_journal_page( $request['paged'] ?? null ),
|
||||
'orderby' => array(
|
||||
'date' => 'DESC',
|
||||
'ID' => 'DESC',
|
||||
@@ -174,12 +234,9 @@ function chrnx_get_journal_query_args( array $request ): array {
|
||||
$args['tax_query'] = $tax_query;
|
||||
}
|
||||
|
||||
if ( isset( $request['s'] ) && is_scalar( $request['s'] ) ) {
|
||||
$search = sanitize_text_field( wp_unslash( (string) $request['s'] ) );
|
||||
$search = mb_substr( trim( $search ), 0, 100, 'UTF-8' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
$search = chrnx_sanitize_journal_search( $request['s'] ?? '' );
|
||||
if ( '' !== $search ) {
|
||||
$args['s'] = $search;
|
||||
}
|
||||
|
||||
return $args;
|
||||
|
||||
@@ -172,6 +172,109 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
$this->assertStringNotContainsString( 'placeholder', strtolower( $output ) );
|
||||
}
|
||||
|
||||
/** Every CSS fallback is visible, deterministic, decorative, and registered with its block. */
|
||||
public function test_editorial_fallbacks_have_registered_visible_static_styles() {
|
||||
$contracts = array(
|
||||
'featured-stories' => array( 'chrnx-featured-story__fallback', 'aspect-ratio' ),
|
||||
'journal-stream' => array( 'chrnx-journal-card__fallback', 'min-height' ),
|
||||
'latest-podcast' => array( 'chrnx-podcast-card__fallback', 'aspect-ratio' ),
|
||||
'topic-hero' => array( 'chrnx-topic-hero__fallback', 'aspect-ratio' ),
|
||||
'related-posts' => array( 'chrnx-related-card__fallback', 'aspect-ratio' ),
|
||||
);
|
||||
|
||||
foreach ( $contracts as $block_name => $contract ) {
|
||||
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $block_name );
|
||||
$this->assertNotEmpty( $type->style_handles, $block_name . ' must register its stylesheet.' );
|
||||
|
||||
$path = get_theme_file_path( 'blocks/' . $block_name . '/style.css' );
|
||||
$this->assertFileExists( $path );
|
||||
// Local test fixture inspection; no remote URL is accepted here.
|
||||
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
|
||||
$css = file_get_contents( $path );
|
||||
$this->assertStringContainsString( '.' . $contract[0], $css );
|
||||
$this->assertStringContainsString( $contract[1], $css );
|
||||
$this->assertMatchesRegularExpression( '/background(?:-image)?:/', $css );
|
||||
$this->assertStringNotContainsString( 'animation:', $css );
|
||||
}
|
||||
}
|
||||
|
||||
/** Related cards expose a visibly styled decorative fallback when thumbnails are absent. */
|
||||
public function test_related_posts_render_decorative_css_fallbacks() {
|
||||
$current = self::factory()->post->create( array( 'post_title' => 'Current story' ) );
|
||||
$related = self::factory()->post->create( array( 'post_title' => 'Related story' ) );
|
||||
wp_set_object_terms( $current, 'research', 'chrnx_topic' );
|
||||
wp_set_object_terms( $related, 'research', 'chrnx_topic' );
|
||||
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current );
|
||||
$this->assertStringContainsString( 'chrnx-related-card__fallback', $output );
|
||||
$this->assertMatchesRegularExpression( '/class="[^"]*chrnx-related-card__fallback[^"]*" aria-hidden="true"/', $output );
|
||||
$this->assertStringNotContainsString( '<img', $output );
|
||||
}
|
||||
|
||||
/** Journal pagination preserves only validated filters in actual rendered links. */
|
||||
public function test_journal_pagination_links_preserve_only_valid_bounded_filters() {
|
||||
$raw_search = 'needle' . str_repeat( 'x', 150 ) . '<b>unsafe</b>';
|
||||
$bounded_search = mb_substr( trim( sanitize_text_field( $raw_search ) ), 0, 100, 'UTF-8' );
|
||||
for ( $index = 1; $index <= 12; ++$index ) {
|
||||
$post_id = self::factory()->post->create(
|
||||
array(
|
||||
'post_title' => 'Journal result ' . $index,
|
||||
'post_content' => $bounded_search,
|
||||
)
|
||||
);
|
||||
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
|
||||
wp_set_object_terms( $post_id, 'es', 'chrnx_language' );
|
||||
}
|
||||
|
||||
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only filter request.
|
||||
$previous_uri = $_SERVER['REQUEST_URI'] ?? null;
|
||||
// Synthetic read-only filter request; no state-changing action requires a nonce.
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
$_GET = array(
|
||||
'chrnx_topic' => 'code',
|
||||
'chrnx_language' => 'es',
|
||||
's' => $raw_search,
|
||||
'paged' => '3junk',
|
||||
'evil' => 'drop-me',
|
||||
'chrnx_show' => 'silicon-ranch',
|
||||
);
|
||||
$_SERVER['REQUEST_URI'] = '/journal/?chrnx_topic=code&chrnx_language=es&s=raw&paged=3junk&evil=drop-me&chrnx_show=silicon-ranch';
|
||||
|
||||
try {
|
||||
$output = render_block(
|
||||
array(
|
||||
'blockName' => 'chrnx/journal-stream',
|
||||
'attrs' => array(),
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
)
|
||||
);
|
||||
} finally {
|
||||
$_GET = $previous_get;
|
||||
if ( null === $previous_uri ) {
|
||||
unset( $_SERVER['REQUEST_URI'] );
|
||||
} else {
|
||||
$_SERVER['REQUEST_URI'] = $previous_uri;
|
||||
}
|
||||
}
|
||||
|
||||
$this->assertMatchesRegularExpression( '/<nav class="chrnx-journal-stream__pagination".*?<\/nav>/s', $output );
|
||||
preg_match( '/<nav class="chrnx-journal-stream__pagination".*?<\/nav>/s', $output, $nav_match );
|
||||
preg_match_all( '/href="([^"]+)"/', $nav_match[0], $href_matches );
|
||||
$this->assertNotEmpty( $href_matches[1] );
|
||||
foreach ( $href_matches[1] as $encoded_href ) {
|
||||
$href = html_entity_decode( $encoded_href, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
|
||||
parse_str( (string) wp_parse_url( $href, PHP_URL_QUERY ), $query );
|
||||
$this->assertSame( 'code', $query['chrnx_topic'] ?? '' );
|
||||
$this->assertSame( 'es', $query['chrnx_language'] ?? '' );
|
||||
$this->assertSame( $bounded_search, $query['s'] ?? '' );
|
||||
$this->assertArrayNotHasKey( 'evil', $query );
|
||||
$this->assertArrayNotHasKey( 'chrnx_show', $query );
|
||||
$this->assertNotSame( '3junk', $query['paged'] ?? '' );
|
||||
}
|
||||
}
|
||||
|
||||
/** Render one dynamic post-context block for a specific post. */
|
||||
private function render_for_post( $block_name, $post_id ) {
|
||||
global $post;
|
||||
|
||||
@@ -176,6 +176,18 @@ class EditorialQueriesTest extends WP_UnitTestCase {
|
||||
$this->assertStringNotContainsString( '<', $search );
|
||||
}
|
||||
|
||||
/** Journal pages accept only canonical positive integers within the public bound. */
|
||||
public function test_journal_query_rejects_noncanonical_or_out_of_range_pages() {
|
||||
foreach ( array( '3junk', '0', 0, '-2', -2, '3.0', 3.0, '+3', '03', array( '3' ), '10001', 10001 ) as $invalid ) {
|
||||
$args = chrnx_get_journal_query_args( array( 'paged' => $invalid ) );
|
||||
$this->assertSame( 1, $args['paged'], 'Invalid page values must fail closed.' );
|
||||
}
|
||||
|
||||
$this->assertSame( 3, chrnx_get_journal_query_args( array( 'paged' => '3' ) )['paged'] );
|
||||
$this->assertSame( 10000, chrnx_get_journal_query_args( array( 'paged' => '10000' ) )['paged'] );
|
||||
$this->assertSame( 10000, chrnx_get_journal_query_args( array( 'paged' => 10000 ) )['paged'] );
|
||||
}
|
||||
|
||||
/** Related stories tier by topic, then tags, then recency without duplicates. */
|
||||
public function test_related_posts_use_ordered_deduplicated_tiers_and_eligibility() {
|
||||
$current = $this->create_post( 'Current', '2026-03-05 10:00:00', 'research' );
|
||||
|
||||
Reference in new issue
Block a user