diff --git a/blocks/journal-stream/render.php b/blocks/journal-stream/render.php index a08e275..73201a9 100644 --- a/blocks/journal-stream/render.php +++ b/blocks/journal-stream/render.php @@ -7,7 +7,7 @@ // Read-only filters do not change server state; every value is validated by the query service. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -$chrnx_request = is_array( $_GET ) ? $_GET : array(); +$chrnx_request = chrnx_get_journal_request_context( is_array( $_GET ) ? $_GET : array() ); $chrnx_args = chrnx_get_journal_query_args( $chrnx_request ); $chrnx_query = chrnx_run_eligible_query( $chrnx_args ); $chrnx_heading = chrnx_get_heading_level( $attributes['headingLevel'] ?? 2, 2 ); diff --git a/inc/queries.php b/inc/queries.php index 0dd127c..32ddc79 100644 --- a/inc/queries.php +++ b/inc/queries.php @@ -306,6 +306,39 @@ function chrnx_sanitize_journal_search( $value ) { return mb_substr( trim( sanitize_text_field( wp_unslash( (string) $value ) ) ), 0, 100, 'UTF-8' ); } +/** + * Normalize public Journal filters and force allowlisted taxonomy archive context. + * + * @param array $request Request-shaped filter values. + * @param mixed $queried_object Optional queried object; defaults to the current request. + * @return array Safe Journal request context. + */ +function chrnx_get_journal_request_context( array $request, $queried_object = null ): array { + $context = array( + 'paged' => chrnx_parse_journal_page( $request['paged'] ?? null ), + ); + foreach ( array( 'chrnx_topic', 'chrnx_language' ) as $taxonomy ) { + $slug = chrnx_validate_editorial_term_slug( $request[ $taxonomy ] ?? '', $taxonomy ); + if ( '' !== $slug ) { + $context[ $taxonomy ] = $slug; + } + } + $search = chrnx_sanitize_journal_search( $request['s'] ?? '' ); + if ( '' !== $search ) { + $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; + } + } + + return $context; +} + /** * Return the current Journal base URL while retaining required WP routing. * @@ -317,6 +350,10 @@ function chrnx_get_journal_base_url() { $url = get_permalink( $queried ); return is_string( $url ) ? $url : ''; } + if ( $queried instanceof WP_Term && in_array( $queried->taxonomy, array( 'chrnx_topic', 'chrnx_language' ), true ) ) { + $url = get_term_link( $queried ); + return is_wp_error( $url ) ? '' : $url; + } $url = get_pagenum_link( 1, false ); $base_url = strtok( $url, '?' ); @@ -346,7 +383,16 @@ function chrnx_get_full_journal_url() { } } - return home_url( '/journal/' ); + $posts_page_id = absint( get_option( 'page_for_posts' ) ); + $posts_page = $posts_page_id > 0 ? get_post( $posts_page_id ) : null; + if ( $posts_page instanceof WP_Post && 'page' === $posts_page->post_type && 'publish' === $posts_page->post_status ) { + $url = get_permalink( $posts_page ); + if ( is_string( $url ) && '' !== $url ) { + return $url; + } + } + + return home_url( '/' ); } /** diff --git a/patterns/about.php b/patterns/about.php index c6ac744..f90d646 100644 --- a/patterns/about.php +++ b/patterns/about.php @@ -12,11 +12,11 @@ $chrnx_research_url = esc_url( 'https://arxiv.org/abs/2603.05352' ); ?>
-

+

.

-

+

diff --git a/patterns/music-landing.php b/patterns/music-landing.php index 0735c35..82cbb46 100644 --- a/patterns/music-landing.php +++ b/patterns/music-landing.php @@ -13,7 +13,7 @@ $chrnx_music_cutout = get_theme_file_uri( 'assets/images/the-no-names-musician- ?>
-

+

@@ -23,12 +23,12 @@ $chrnx_music_cutout = get_theme_file_uri( 'assets/images/the-no-names-musician- <?php echo esc_attr__( 'The No Names musician portrait', 'chrnx' ); ?> -

+

-

-

-

+

+

+

diff --git a/patterns/podcast-episode.php b/patterns/podcast-episode.php index ed0c090..30dfe85 100644 --- a/patterns/podcast-episode.php +++ b/patterns/podcast-episode.php @@ -11,7 +11,6 @@ ?>
-
diff --git a/patterns/podcast-landing.php b/patterns/podcast-landing.php index 4d7df87..134d9d4 100644 --- a/patterns/podcast-landing.php +++ b/patterns/podcast-landing.php @@ -12,7 +12,7 @@ $chrnx_podcast_art = get_theme_file_uri( 'assets/images/alter-ego-podcasts.png' ?>
-

+

diff --git a/patterns/podcast-show.php b/patterns/podcast-show.php index d4a6595..b36529d 100644 --- a/patterns/podcast-show.php +++ b/patterns/podcast-show.php @@ -12,7 +12,6 @@ $chrnx_podcast_show_art = get_theme_file_uri( 'assets/images/alter-ego-podcasts. ?>
-
diff --git a/patterns/project-detail.php b/patterns/project-detail.php index bf3a558..08971bf 100644 --- a/patterns/project-detail.php +++ b/patterns/project-detail.php @@ -11,7 +11,6 @@ ?>
-

diff --git a/tests/php/BlockRenderingTest.php b/tests/php/BlockRenderingTest.php index 4e09f4e..bf796a5 100644 --- a/tests/php/BlockRenderingTest.php +++ b/tests/php/BlockRenderingTest.php @@ -182,12 +182,21 @@ class BlockRenderingTest extends WP_UnitTestCase { /** Empty/recovery links always point directly to a portable full Journal URL. */ public function test_journal_recovery_links_are_localized_and_portable() { + $journal_page = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_name' => 'journal', + 'post_title' => 'Journal', + ) + ); $portable_home = static function ( $url, $path ) { return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' ); }; add_filter( 'home_url', $portable_home, 10, 2 ); try { - $link = render_block( + $expected_url = esc_url( get_permalink( $journal_page ) ); + $link = render_block( array( 'blockName' => 'chrnx/journal-link', 'attrs' => array(), @@ -196,7 +205,7 @@ class BlockRenderingTest extends WP_UnitTestCase { 'innerContent' => array(), ) ); - $this->assertStringContainsString( 'href="https://example.test/subdirectory/journal/"', $link ); + $this->assertStringContainsString( 'href="' . $expected_url . '"', $link ); $this->assertStringContainsString( 'Browse the full Journal', $link ); $stream = render_block( @@ -208,13 +217,59 @@ class BlockRenderingTest extends WP_UnitTestCase { 'innerContent' => array(), ) ); - $this->assertStringContainsString( 'href="https://example.test/subdirectory/journal/"', $stream ); + $this->assertStringContainsString( 'href="' . $expected_url . '"', $stream ); $this->assertStringContainsString( 'Browse the full Journal', $stream ); } finally { remove_filter( 'home_url', $portable_home, 10 ); } } + /** Journal recovery falls back to a valid posts Page and then site home, never a guessed route. */ + public function test_full_journal_url_has_non_404_plain_pretty_and_subdirectory_fallbacks() { + global $wp_rewrite; + $previous_structure = get_option( 'permalink_structure' ); + $previous_posts_page = get_option( 'page_for_posts' ); + $portable_home = static function ( $url, $path ) { + return 'https://example.test/subdirectory/' . ltrim( (string) $path, '/' ); + }; + $existing_journal = get_page_by_path( 'journal', OBJECT, 'page' ); + if ( $existing_journal instanceof WP_Post ) { + wp_delete_post( $existing_journal->ID, true ); + } + $posts_page = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_name' => 'writing', + 'post_title' => 'Writing', + ) + ); + update_option( 'page_for_posts', $posts_page ); + + try { + $wp_rewrite->set_permalink_structure( '' ); + $this->assertSame( get_permalink( $posts_page ), chrnx_get_full_journal_url() ); + + $wp_rewrite->set_permalink_structure( '/%postname%/' ); + $this->assertSame( get_permalink( $posts_page ), chrnx_get_full_journal_url() ); + + wp_update_post( + array( + 'ID' => $posts_page, + 'post_status' => 'draft', + ) + ); + add_filter( 'home_url', $portable_home, 10, 2 ); + $this->assertSame( 'https://example.test/subdirectory/', chrnx_get_full_journal_url() ); + $this->assertStringNotContainsString( '/journal/', chrnx_get_full_journal_url() ); + remove_filter( 'home_url', $portable_home, 10 ); + } finally { + remove_filter( 'home_url', $portable_home, 10 ); + update_option( 'page_for_posts', $previous_posts_page ); + $wp_rewrite->set_permalink_structure( $previous_structure ); + } + } + /** 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' ) ); diff --git a/tests/php/EditorialQueriesTest.php b/tests/php/EditorialQueriesTest.php index 2d8e635..b71f3f2 100644 --- a/tests/php/EditorialQueriesTest.php +++ b/tests/php/EditorialQueriesTest.php @@ -181,6 +181,61 @@ class EditorialQueriesTest extends WP_UnitTestCase { $this->assertSame( 'es', $args['tax_query'][1]['terms'] ); } + /** Real topic archives override conflicting request filters and never mix terms. */ + public function test_topic_archive_context_drives_journal_query_and_selector() { + $code = self::factory()->post->create( array( 'post_title' => 'Code archive story' ) ); + $life = self::factory()->post->create( array( 'post_title' => 'Life archive story' ) ); + wp_set_object_terms( $code, 'code', 'chrnx_topic' ); + wp_set_object_terms( $life, 'life', 'chrnx_topic' ); + $term = get_term_by( 'slug', 'code', 'chrnx_topic' ); + $this->go_to( get_term_link( $term ) ); + + $previous_get = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request. + $_GET = array( 'chrnx_topic' => 'life' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request. + try { + $context = chrnx_get_journal_request_context( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Synthetic read-only request. + $output = render_block( + array( + 'blockName' => 'chrnx/journal-stream', + 'attrs' => array(), + 'innerBlocks' => array(), + 'innerHTML' => '', + 'innerContent' => array(), + ) + ); + } finally { + $_GET = $previous_get; + } + + $this->assertSame( 'code', $context['chrnx_topic'] ); + $this->assertMatchesRegularExpression( '/value="code"\s+selected=[\'\"]selected[\'\"]/', $output ); + $this->assertStringContainsString( 'Code archive story', $output ); + $this->assertStringNotContainsString( 'Life archive story', $output ); + $this->assertStringContainsString( 'action="' . esc_url( get_term_link( $term ) ) . '"', $output ); + } + + /** Language archives constrain results while unsupported taxonomy archives are ignored. */ + public function test_language_archive_context_is_allowlisted_and_other_taxonomies_are_not() { + $spanish = self::factory()->post->create( array( 'post_title' => 'Historia en español' ) ); + $english = self::factory()->post->create( array( 'post_title' => 'English story' ) ); + wp_set_object_terms( $spanish, 'es', 'chrnx_language' ); + wp_set_object_terms( $english, 'en', 'chrnx_language' ); + $spanish_term = get_term_by( 'slug', 'es', 'chrnx_language' ); + $this->go_to( get_term_link( $spanish_term ) ); + + $context = chrnx_get_journal_request_context( array( 'chrnx_language' => 'en' ) ); + $args = chrnx_get_journal_query_args( $context ); + $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' ) ) ); + + $tag = self::factory()->tag->create_and_get( array( 'slug' => 'not-allowlisted' ) ); + $this->go_to( get_term_link( $tag ) ); + $unsupported = chrnx_get_journal_request_context( array() ); + $this->assertArrayNotHasKey( 'post_tag', $unsupported ); + $this->assertArrayNotHasKey( 'tax_query', chrnx_get_journal_query_args( $unsupported ) ); + } + /** Malformed journal request values are ignored and never copied into query args. */ public function test_journal_query_ignores_malformed_filters_and_caps_search() { $args = chrnx_get_journal_query_args( diff --git a/tests/php/PatternsTest.php b/tests/php/PatternsTest.php index ec36cff..85bdb82 100644 --- a/tests/php/PatternsTest.php +++ b/tests/php/PatternsTest.php @@ -133,6 +133,51 @@ class PatternsTest extends WP_UnitTestCase { } } + /** Insertable page patterns render below the template's one H1 without duplicating post titles. */ + public function test_insertable_page_patterns_preserve_single_template_h1() { + global $post; + $patterns = array( 'about', 'music-landing', 'podcast-landing', 'project-detail', 'podcast-show', 'podcast-episode' ); + $template = file_get_contents( get_theme_file_path( 'templates/page.html' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local integration fixture. + foreach ( $patterns as $name ) { + $content = $this->render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) ); + $this->assertStringNotContainsString( 'wp:post-title', $content, $name ); + $this->assertDoesNotMatchRegularExpression( '/assertNotEmpty( parse_blocks( $content ), $name ); + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_status' => 'publish', + 'post_title' => 'Template page title', + 'post_content' => $content, + ) + ); + $previous_post = $post; + $post = get_post( $page_id ); + setup_postdata( $post ); + $rendered = ''; + try { + foreach ( parse_blocks( $template ) as $template_block ) { + $rendered .= ( new WP_Block( + $template_block, + array( + 'postId' => $page_id, + 'postType' => 'page', + ) + ) )->render(); + } + } finally { + wp_reset_postdata(); + $post = $previous_post; + } + $this->assertSame( 1, substr_count( $rendered, 'render_pattern( get_theme_file_path( 'patterns/' . $name . '.php' ) ); + $this->assertMatchesRegularExpression( '/\s*