fix: clarify fixed CHRNX archive filters

This commit is contained in:
chrnx committed 2026-08-11 18:32:09 -05:00
1 parent 2230731e1b
commit ff1c64ae48
4 files changed
+160 -39

No files matched your search

+46 -32
View File
@@ -16,46 +16,60 @@ $chrnx_language = chrnx_validate_editorial_term_slug( $chrnx_request['chrnx_lang
$chrnx_search = $chrnx_args['s'] ?? '';
$chrnx_form_url = chrnx_get_journal_base_url();
$chrnx_title_id = wp_unique_id( 'chrnx-journal-title-' );
$chrnx_fixed = chrnx_get_journal_archive_filter();
?>
<section <?php echo get_block_wrapper_attributes( array( 'class' => 'chrnx-journal-stream' ) ); // 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__( 'Journal', 'chrnx' ); ?></h<?php echo esc_attr( $chrnx_heading ); ?>>
<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">
<option value=""><?php echo esc_html__( 'All topics', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_topic',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_topic, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<label>
<span><?php echo esc_html__( 'Language', 'chrnx' ); ?></span>
<select name="chrnx_language">
<option value=""><?php echo esc_html__( 'All languages', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_language',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_language, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<?php if ( isset( $chrnx_fixed['taxonomy'] ) && 'chrnx_topic' === $chrnx_fixed['taxonomy'] ) : ?>
<p class="chrnx-journal-stream__fixed-filter"><span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span> <strong><?php echo esc_html( $chrnx_fixed['label'] ); ?></strong></p>
<input type="hidden" name="chrnx_topic" value="<?php echo esc_attr( $chrnx_fixed['slug'] ); ?>">
<?php else : ?>
<label>
<span><?php echo esc_html__( 'Topic', 'chrnx' ); ?></span>
<select name="chrnx_topic">
<option value=""><?php echo esc_html__( 'All topics', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_topic',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_topic, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<?php endif; ?>
<?php if ( isset( $chrnx_fixed['taxonomy'] ) && 'chrnx_language' === $chrnx_fixed['taxonomy'] ) : ?>
<p class="chrnx-journal-stream__fixed-filter"><span><?php echo esc_html__( 'Language', 'chrnx' ); ?></span> <strong><?php echo esc_html( $chrnx_fixed['label'] ); ?></strong></p>
<input type="hidden" name="chrnx_language" value="<?php echo esc_attr( $chrnx_fixed['slug'] ); ?>">
<?php else : ?>
<label>
<span><?php echo esc_html__( 'Language', 'chrnx' ); ?></span>
<select name="chrnx_language">
<option value=""><?php echo esc_html__( 'All languages', 'chrnx' ); ?></option>
<?php
foreach ( get_terms(
array(
'taxonomy' => 'chrnx_language',
'hide_empty' => false,
)
) as $chrnx_term ) :
?>
<option value="<?php echo esc_attr( $chrnx_term->slug ); ?>" <?php selected( $chrnx_language, $chrnx_term->slug ); ?>><?php echo esc_html( $chrnx_term->name ); ?></option>
<?php endforeach; ?>
</select>
</label>
<?php endif; ?>
<label>
<span><?php echo esc_html__( 'Search', 'chrnx' ); ?></span>
<input type="search" name="s" value="<?php echo esc_attr( $chrnx_search ); ?>">
</label>
<?php if ( ! empty( $chrnx_fixed ) ) : ?>
<a class="chrnx-journal-stream__clear" href="<?php echo esc_url( chrnx_get_full_journal_url() ); ?>"><?php echo esc_html__( 'View all Journal entries', 'chrnx' ); ?></a>
<?php endif; ?>
<button type="submit"><?php echo esc_html__( 'Filter', 'chrnx' ); ?></button>
</form>
<?php if ( $chrnx_query->have_posts() ) : ?>
+27 -6
View File
@@ -306,6 +306,30 @@ function chrnx_sanitize_journal_search( $value ) {
return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' );
}
/**
* Return allowlisted fixed filter context for the current editorial taxonomy archive.
*
* @param mixed $queried_object Optional queried object; defaults to the current request.
* @return array<string,string> Taxonomy, slug, and label, or an empty array.
*/
function chrnx_get_journal_archive_filter( $queried_object = null ): array {
$queried_object = null === $queried_object ? get_queried_object() : $queried_object;
if ( ! $queried_object instanceof WP_Term || ! in_array( $queried_object->taxonomy, array( 'chrnx_topic', 'chrnx_language' ), true ) ) {
return array();
}
$slug = chrnx_validate_editorial_term_slug( $queried_object->slug, $queried_object->taxonomy );
if ( '' === $slug ) {
return array();
}
return array(
'taxonomy' => $queried_object->taxonomy,
'slug' => $slug,
'label' => $queried_object->name,
);
}
/**
* Normalize public Journal filters and force allowlisted taxonomy archive context.
*
@@ -328,12 +352,9 @@ function chrnx_get_journal_request_context( array $request, $queried_object = nu
$context['s'] = $search;
}
$queried_object = null === $queried_object ? get_queried_object() : $queried_object;
if ( $queried_object instanceof WP_Term && in_array( $queried_object->taxonomy, array( 'chrnx_topic', 'chrnx_language' ), true ) ) {
$archive_slug = chrnx_validate_editorial_term_slug( $queried_object->slug, $queried_object->taxonomy );
if ( '' !== $archive_slug ) {
$context[ $queried_object->taxonomy ] = $archive_slug;
}
$archive_filter = chrnx_get_journal_archive_filter( $queried_object );
if ( ! empty( $archive_filter ) ) {
$context[ $archive_filter['taxonomy'] ] = $archive_filter['slug'];
}
return $context;
+71
View File
@@ -270,6 +270,64 @@ class BlockRenderingTest extends WP_UnitTestCase {
}
}
/** Fixed archive dimensions render as context while the other selector and clear exit remain usable. */
public function test_topic_archive_hides_inoperable_filter_and_keeps_language_and_clear_actions() {
$code_es = self::factory()->post->create( array( 'post_title' => 'Code Spanish' ) );
$code_en = self::factory()->post->create( array( 'post_title' => 'Code English' ) );
$life_es = self::factory()->post->create( array( 'post_title' => 'Life Spanish' ) );
foreach ( array( $code_es, $code_en ) as $post_id ) {
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
}
wp_set_object_terms( $life_es, 'life', 'chrnx_topic' );
wp_set_object_terms( $code_es, 'es', 'chrnx_language' );
wp_set_object_terms( $code_en, 'en', 'chrnx_language' );
wp_set_object_terms( $life_es, 'es', 'chrnx_language' );
$journal_page = self::factory()->post->create(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => 'journal',
'post_title' => 'Journal',
)
);
$this->go_to( get_term_link( get_term_by( 'slug', 'code', 'chrnx_topic' ) ) );
$portable_home = static function ( $url, $path ) {
return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' );
};
$previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only requests.
add_filter( 'home_url', $portable_home, 10, 2 );
try {
// A conflicting fixed-topic submit must remain on Code while the free language filter works.
$_GET = array( // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
'chrnx_topic' => 'life',
'chrnx_language' => 'es',
);
$filtered = $this->render_journal_stream();
$this->assertStringNotContainsString( '<select name="chrnx_topic">', $filtered );
$this->assertStringNotContainsString( '>All topics<', $filtered );
$this->assertMatchesRegularExpression( '/chrnx-journal-stream__fixed-filter[^>]*>.*Topic.*Code/s', $filtered );
$this->assertStringContainsString( '<select name="chrnx_language">', $filtered );
$this->assertMatchesRegularExpression( '/value="es"\s+selected=[\'\"]selected[\'\"]/', $filtered );
$this->assertStringContainsString( 'Code Spanish', $filtered );
$this->assertStringNotContainsString( 'Code English', $filtered );
$this->assertStringNotContainsString( 'Life Spanish', $filtered );
// Submitting All for the fixed dimension cannot clear the route context or create a dead control.
$_GET = array( 'chrnx_topic' => '' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request.
$all = $this->render_journal_stream();
$this->assertStringNotContainsString( '<select name="chrnx_topic">', $all );
$this->assertStringNotContainsString( '>All topics<', $all );
$this->assertStringContainsString( 'Code Spanish', $all );
$this->assertStringContainsString( 'Code English', $all );
$this->assertStringNotContainsString( 'Life Spanish', $all );
$this->assertStringContainsString( 'class="chrnx-journal-stream__clear"', $all );
$this->assertStringContainsString( 'href="' . esc_url( get_permalink( $journal_page ) ) . '"', $all );
} finally {
$_GET = $previous_get;
remove_filter( 'home_url', $portable_home, 10 );
}
}
/** Podcast metadata renders known text and HTTPS links, never embed HTML. */
public function test_podcast_meta_renders_only_known_escaped_fields() {
$post_id = self::factory()->post->create( array( 'post_title' => 'Episode' ) );
@@ -710,6 +768,19 @@ class BlockRenderingTest extends WP_UnitTestCase {
$this->assertNotSame( $first_label[1] ?? '', $second_label[1] ?? '' );
}
/** Render the Journal stream block for the current request. */
private function render_journal_stream() {
return render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
}
/** Render one dynamic post-context block for a specific post. */
private function render_for_post( $block_name, $post_id ) {
global $post;
+16 -1
View File
@@ -208,7 +208,9 @@ class EditorialQueriesTest extends WP_UnitTestCase {
}
$this->assertSame( 'code', $context['chrnx_topic'] );
$this->assertMatchesRegularExpression( '/value="code"\s+selected=[\'\"]selected[\'\"]/', $output );
$this->assertStringNotContainsString( '<select name="chrnx_topic">', $output );
$this->assertMatchesRegularExpression( '/chrnx-journal-stream__fixed-filter[^>]*>.*Topic.*Code/s', $output );
$this->assertStringContainsString( 'type="hidden" name="chrnx_topic" value="code"', $output );
$this->assertStringContainsString( 'Code archive story', $output );
$this->assertStringNotContainsString( 'Life archive story', $output );
$this->assertStringContainsString( 'action="' . esc_url( get_term_link( $term ) ) . '"', $output );
@@ -228,6 +230,19 @@ class EditorialQueriesTest extends WP_UnitTestCase {
$query = chrnx_run_eligible_query( $args );
$this->assertSame( 'es', $context['chrnx_language'] );
$this->assertSame( array( $spanish ), array_map( 'absint', wp_list_pluck( $query->posts, 'ID' ) ) );
$output = render_block(
array(
'blockName' => 'chrnx/journal-stream',
'attrs' => array(),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
)
);
$this->assertStringNotContainsString( '<select name="chrnx_language">', $output );
$this->assertStringNotContainsString( '>All languages<', $output );
$this->assertStringContainsString( '<select name="chrnx_topic">', $output );
$this->assertMatchesRegularExpression( '/chrnx-journal-stream__fixed-filter[^>]*>.*Language.*Español/s', $output );
$tag = self::factory()->tag->create_and_get( array( 'slug' => 'not-allowlisted' ) );
$this->go_to( get_term_link( $tag ) );