diff --git a/README.md b/README.md index 54f3edc..1f7955d 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,9 @@ the first 100 posts are listed, followed by a link to the HTML archive for the r The `?format=markdown` fallback, the Accept redirect, and the alternate link work on these archives as they do on posts. +Author and date archives remain HTML, including when a client sends +`Accept: text/markdown`; they have no Markdown renderer. + Frontmatter links to a term point to its `.md` URL only when its taxonomy is served; other terms link to their HTML archive. Serve another taxonomy with `markdown_alternate_supported_taxonomies`: diff --git a/readme.txt b/readme.txt index 4074e75..dfd6170 100644 --- a/readme.txt +++ b/readme.txt @@ -103,6 +103,7 @@ Yes, as an opt-in. With `add_filter( 'markdown_alternate_skip_noindexed', '__ret == Changelog == = Unreleased = +* Kept author and date archives in HTML instead of redirecting Markdown requests to missing pages * Fixed cover block images running into the first inner heading * Omitted aria-hidden and data-URI images from Markdown * Preserved iframe embeds as links instead of dropping their content diff --git a/src/Router/RewriteHandler.php b/src/Router/RewriteHandler.php index f2291ba..3a2270a 100644 --- a/src/Router/RewriteHandler.php +++ b/src/Router/RewriteHandler.php @@ -612,14 +612,13 @@ public function handle_accept_negotiation(): void { } - /** - * Get canonical URL for current content. - * - * Supports singular posts/pages, category/tag archives, author archives, - * and date archives. - * - * @return string|null The canonical URL or null if not determinable. - */ + /** + * Get the canonical URL for content with a supported Markdown route. + * + * Author and date archives have no Markdown renderer, so they retain HTML. + * + * @return string|null The canonical URL, or null for unsupported content. + */ private function get_current_canonical_url(): ?string { // Singular posts and pages if (is_singular()) { @@ -637,24 +636,6 @@ private function get_current_canonical_url(): ?string { return is_wp_error($link) ? null : $link; } - // Author archives - if (is_author()) { - return get_author_posts_url(get_queried_object_id()); - } - - // Date archives - if (is_date()) { - if (is_year()) { - return get_year_link(get_query_var('year')); - } - if (is_month()) { - return get_month_link(get_query_var('year'), get_query_var('monthnum')); - } - if (is_day()) { - return get_day_link(get_query_var('year'), get_query_var('monthnum'), get_query_var('day')); - } - } - return null; } diff --git a/tests/router-request.php b/tests/router-request.php index 9c48f75..03eb3a7 100644 --- a/tests/router-request.php +++ b/tests/router-request.php @@ -40,7 +40,37 @@ function get_queried_object() { return $GLOBALS['md_alt_router_post']; } function is_singular() { - return true; + return ! in_array( $GLOBALS['md_alt_mode'], array( 'author', 'year', 'month', 'day' ), true ); + } + function is_author() { + return 'author' === $GLOBALS['md_alt_mode']; + } + function is_date() { + return in_array( $GLOBALS['md_alt_mode'], array( 'year', 'month', 'day' ), true ); + } + function is_year() { + return 'year' === $GLOBALS['md_alt_mode']; + } + function is_month() { + return 'month' === $GLOBALS['md_alt_mode']; + } + function is_day() { + return 'day' === $GLOBALS['md_alt_mode']; + } + function get_queried_object_id() { + return 5; + } + function get_author_posts_url( $author_id ) { + return 'https://example.com/author/test/'; + } + function get_year_link( $year ) { + return 'https://example.com/' . $year . '/'; + } + function get_month_link( $year, $month ) { + return sprintf( 'https://example.com/%04d/%02d/', $year, $month ); + } + function get_day_link( $year, $month, $day ) { + return sprintf( 'https://example.com/%04d/%02d/%02d/', $year, $month, $day ); } function is_category() { return false; @@ -83,10 +113,17 @@ function wp_safe_redirect( $url, $status = 302 ) { $GLOBALS['md_alt_response_headers']['Location'] = $url; } - $md_alt_router_post = md_alt_test_fixture( 'ordinary' ); - $md_alt_route = $argv[1]; - $md_alt_mode = $argv[2]; - $md_alt_query_vars = array(); + $md_alt_router_post = md_alt_test_fixture( 'ordinary' ); + $md_alt_route = $argv[1]; + $md_alt_mode = $argv[2]; + $md_alt_query_vars = array(); + if ( is_date() ) { + $md_alt_query_vars = array( + 'year' => 2001, + 'monthnum' => 2, + 'day' => 3, + ); + } $md_alt_response_headers = array(); $md_alt_response_status = 200; $_SERVER['REQUEST_URI'] = 'md' === $md_alt_route ? '/post.md' : '/post/'; diff --git a/tests/security.php b/tests/security.php index ea4ec6b..f3a18aa 100644 --- a/tests/security.php +++ b/tests/security.php @@ -203,3 +203,13 @@ function md_alt_router_request( $route, $mode ) { md_alt_assert_same( 'HTML template', $response['body'] ); md_alt_assert_same( 0, $response['renders'] ); }; + +$md_alt_tests['Author and date archives retain HTML when Markdown is requested'] = static function () { + foreach ( array( 'author', 'year', 'month', 'day' ) as $archive ) { + $response = md_alt_router_request( 'accept', $archive ); + md_alt_assert_same( 200, $response['status'], $archive ); + md_alt_assert_same( 'HTML template', $response['body'], $archive ); + md_alt_assert_same( false, isset( $response['headers']['Location'] ), $archive ); + md_alt_assert_same( 0, $response['renders'], $archive ); + } +}; diff --git a/tests/term-integration.php b/tests/term-integration.php index cf41b6b..56a03cf 100644 --- a/tests/term-integration.php +++ b/tests/term-integration.php @@ -99,6 +99,29 @@ function md_alt_term_assert( $condition, $message ) { ) ); md_alt_term_assert( ! empty( $md_alt_admin_ids ), 'HTTP cache tests have an administrator fixture' ); + wp_update_post( + array( + 'ID' => $md_alt_term_post_id, + 'post_author' => (int) $md_alt_admin_ids[0], + 'post_date' => '2001-02-03 12:00:00', + 'post_date_gmt' => '2001-02-03 12:00:00', + ) + ); + $md_alt_accept_args = array( + 'headers' => array( 'Accept' => 'text/markdown' ), + 'redirection' => 0, + ); + foreach ( array( get_author_posts_url( (int) $md_alt_admin_ids[0] ), get_year_link( 2001 ), get_month_link( 2001, 2 ), get_day_link( 2001, 2, 3 ) ) as $md_alt_archive_url ) { + $md_alt_response = wp_remote_get( $md_alt_archive_url, $md_alt_accept_args ); + md_alt_term_assert( 200 === wp_remote_retrieve_response_code( $md_alt_response ), 'Unsupported archive stays on its HTML URL: ' . $md_alt_archive_url ); + md_alt_term_assert( '' === wp_remote_retrieve_header( $md_alt_response, 'location' ), 'Unsupported archive does not redirect to missing Markdown' ); + md_alt_term_assert( 0 === strpos( wp_remote_retrieve_header( $md_alt_response, 'content-type' ), 'text/html' ), 'Unsupported archive response remains HTML' ); + } + $md_alt_response = wp_remote_get( get_term_link( $md_alt_term_child ), $md_alt_accept_args ); + $md_alt_term_url = UrlConverter::convert_to_markdown_url( get_term_link( $md_alt_term_child ) ); + md_alt_term_assert( 303 === wp_remote_retrieve_response_code( $md_alt_response ) && wp_remote_retrieve_header( $md_alt_response, 'location' ) === $md_alt_term_url, 'Supported category still redirects to Markdown' ); + $md_alt_response = wp_remote_get( $md_alt_term_url ); + md_alt_term_assert( 200 === wp_remote_retrieve_response_code( $md_alt_response ) && 0 === strpos( wp_remote_retrieve_header( $md_alt_response, 'content-type' ), 'text/markdown' ), 'Advertised category Markdown URL resolves successfully' ); $md_alt_auth_cookie = wp_generate_auth_cookie( (int) $md_alt_admin_ids[0], time() + 600, 'logged_in' ); foreach ( array( array( LOGGED_IN_COOKIE => $md_alt_auth_cookie ), array( 'wp_woocommerce_session_fixture' => 'test-session' ) ) as $md_alt_cookies ) { $md_alt_response = wp_remote_get(