diff --git a/blocks/journal-stream/render.php b/blocks/journal-stream/render.php index 73201a9..2537b81 100644 --- a/blocks/journal-stream/render.php +++ b/blocks/journal-stream/render.php @@ -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(); ?>
'chrnx-journal-stream' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> aria-labelledby=""> id="">>
- - + +

+ + + + + +

+ + + + + + +
have_posts() ) : ?> diff --git a/inc/queries.php b/inc/queries.php index 32ddc79..e339eca 100644 --- a/inc/queries.php +++ b/inc/queries.php @@ -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 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; diff --git a/tests/php/BlockRenderingTest.php b/tests/php/BlockRenderingTest.php index bf796a5..ae44b8d 100644 --- a/tests/php/BlockRenderingTest.php +++ b/tests/php/BlockRenderingTest.php @@ -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( '', $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( '', $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( '', $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 ) );