diff --git a/inc/setup.php b/inc/setup.php index 1e09e8b..e14eef0 100644 --- a/inc/setup.php +++ b/inc/setup.php @@ -32,6 +32,62 @@ function chrnx_setup() { add_action( 'after_setup_theme', 'chrnx_setup' ); add_filter( 'comments_open', '__return_false' ); +/* + * Nothing collects comments, so the head must not advertise a feed for them. + * This one filter covers both the site-wide feed printed by feed_links() and + * the per-post one from feed_links_extra(), which still fired on old posts + * carrying a non-zero comment_count. Topic, author and search feeds stay. + */ +add_filter( 'feed_links_show_comments_feed', '__return_false' ); + +/** + * Resolve the document language from the editorial language taxonomy. + * + * Roughly half the journal is written in Spanish while every route declared + * en-US, so screen readers read Spanish with English phonetics and crawlers + * indexed those posts as English. chrnx_language already records the answer. + * + * @return string BCP 47 tag, or an empty string to keep the site language. + */ +function chrnx_get_document_locale(): string { + // ponytail: explicit map, a third language is one more line here. + $locales = array( + 'en' => 'en-US', + 'es' => 'es-MX', + ); + + $slug = ''; + + if ( is_singular( 'post' ) ) { + // A post carries one language; extra terms are an editing mistake. + $terms = get_the_terms( get_queried_object_id(), 'chrnx_language' ); + $slug = ( is_array( $terms ) && isset( $terms[0]->slug ) ) ? $terms[0]->slug : ''; + } elseif ( is_tax( 'chrnx_language' ) ) { + $term = get_queried_object(); + $slug = $term instanceof WP_Term ? $term->slug : ''; + } + + return $locales[ $slug ] ?? ''; +} + +/** + * Apply the editorial language to the html element. + * + * @param string $output Attribute string built by core. + * @return string Attribute string carrying the editorial language. + */ +function chrnx_filter_language_attributes( $output ) { + $locale = chrnx_get_document_locale(); + + if ( '' === $locale ) { + return $output; + } + + // Replace only the language, so dir and any plugin attribute survive. + return preg_replace( '/lang="[^"]*"/', 'lang="' . esc_attr( $locale ) . '"', (string) $output, 1 ); +} +add_filter( 'language_attributes', 'chrnx_filter_language_attributes' ); + /** * Print Open Graph and Twitter card tags only when nothing else does. * diff --git a/tests/php/ThemeSetupTest.php b/tests/php/ThemeSetupTest.php index 5c29bf9..0c3e82b 100644 --- a/tests/php/ThemeSetupTest.php +++ b/tests/php/ThemeSetupTest.php @@ -37,6 +37,83 @@ class ThemeSetupTest extends WP_UnitTestCase { $this->assertContains( 'assets/css/components.css', $editor_styles ); } + /** + * Ensure a Spanish post declares a Spanish document language. + */ + public function test_language_attributes_follow_a_spanish_post() { + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, 'es', 'chrnx_language' ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertTrue( is_singular( 'post' ) ); + $this->assertSame( 'lang="es-MX"', chrnx_filter_language_attributes( 'lang="en-US"' ) ); + } + + /** + * Ensure an English post keeps the English document language. + */ + public function test_language_attributes_follow_an_english_post() { + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, 'en', 'chrnx_language' ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertSame( 'lang="en-US"', chrnx_filter_language_attributes( 'lang="en-US"' ) ); + } + + /** + * Ensure the direction attribute survives the language swap. + */ + public function test_language_attributes_preserve_other_attributes() { + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, 'es', 'chrnx_language' ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertSame( 'lang="es-MX" dir="ltr"', chrnx_filter_language_attributes( 'lang="en-US" dir="ltr"' ) ); + } + + /** + * Ensure a post without a language term is left untouched. + */ + public function test_language_attributes_ignore_a_post_without_a_language() { + $post_id = self::factory()->post->create(); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertSame( 'lang="en-US"', chrnx_filter_language_attributes( 'lang="en-US"' ) ); + } + + /** + * Ensure a language archive declares the language it lists. + */ + public function test_language_attributes_follow_a_language_archive() { + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, 'es', 'chrnx_language' ); + + $this->go_to( get_term_link( 'es', 'chrnx_language' ) ); + + $this->assertTrue( is_tax( 'chrnx_language' ) ); + $this->assertSame( 'lang="es-MX"', chrnx_filter_language_attributes( 'lang="en-US"' ) ); + } + + /** + * Ensure the home page keeps the site language. + */ + public function test_language_attributes_leave_the_home_page_alone() { + $this->go_to( home_url( '/' ) ); + + $this->assertSame( 'lang="en-US"', chrnx_filter_language_attributes( 'lang="en-US"' ) ); + } + + /** + * Ensure the head never advertises a comments feed the theme cannot render. + */ + public function test_comments_feed_is_not_advertised() { + $this->assertFalse( apply_filters( 'feed_links_show_comments_feed', true ) ); + } + /** * Ensure the local base stylesheet is enqueued with the theme version. */