Skip to content

refactor: simplify RewriteHandler validation and output logic#13

Closed
jdevalk wants to merge 2 commits intomainfrom
optimize/rewrite-handler
Closed

refactor: simplify RewriteHandler validation and output logic#13
jdevalk wants to merge 2 commits intomainfrom
optimize/rewrite-handler

Conversation

@jdevalk
Copy link
Member

@jdevalk jdevalk commented Feb 16, 2026

Summary

Eliminates code duplication in RewriteHandler by extracting shared validation and output logic into reusable private methods.

Changes

  • Extract validate_post_for_markdown() to consolidate post type and status checks
  • Extract serve_markdown_response() to consolidate password check, rendering, and output
  • Cache ContentRenderer instance to avoid creating new objects on every request
  • Simplify lowercase .md enforcement logic for better readability
  • Simplify format parameter validation

Impact

  • Reduces code from ~405 lines to ~350 lines
  • Eliminates duplication between handle_format_parameter() and handle_markdown_request()
  • Improves maintainability — validation logic now lives in one place
  • No functional changes — all behavior preserved

Testing

  • .md URLs serve markdown
  • ?format=markdown parameter works
  • Accept header negotiation redirects correctly
  • Password protection returns 403
  • Unsupported post types ignored
  • Unpublished posts ignored
  • Case-sensitive extension enforcement works

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the RewriteHandler class to eliminate code duplication by extracting common validation and response logic into reusable private methods. It also introduces caching for the ContentRenderer instance and simplifies several conditional checks.

Changes:

  • Extracted validate_post_for_markdown() to consolidate post type and status validation
  • Extracted serve_markdown_response() to consolidate password checks, rendering, and output
  • Added ContentRenderer instance caching via get_renderer() method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// Enforce lowercase .md extension - let WP 404 if wrong case
if (!preg_match('/\.md$/', $request_uri) && preg_match('/\.md$/i', $request_uri)) {
if (str_ends_with($request_uri, '.MD') || preg_match('/\.[Mm][Dd]$/', $request_uri)) {
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern /\.[Mm][Dd]$/ incorrectly matches lowercase .md extensions. The character class [Mm] matches both uppercase M and lowercase m, and [Dd] matches both uppercase D and lowercase d. This means the condition will incorrectly reject valid .md URLs.

The original logic !preg_match('/\.md$/', $request_uri) && preg_match('/\.md$/i', $request_uri) correctly identified non-lowercase extensions by checking "if NOT lowercase AND case-insensitive match".

To fix this while keeping the simplified approach, the condition should only check for non-lowercase variations. One option: preg_match('/\.[MD]/', $request_uri) (checks if either M or D is uppercase anywhere in the extension).

Suggested change
if (str_ends_with($request_uri, '.MD') || preg_match('/\.[Mm][Dd]$/', $request_uri)) {
if (
!preg_match('/\.md$/', $request_uri) &&
preg_match('/\.md$/i', $request_uri)
) {

Copilot uses AI. Check for mistakes.
Restore original logic for non-lowercase .md extension detection.
The simplified regex '/\.[Mm][Dd]$/' incorrectly matched lowercase
.md extensions, causing valid requests to be rejected. The original
two-part check correctly identifies only non-lowercase variants.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@jdevalk
Copy link
Member Author

jdevalk commented Feb 17, 2026

Consolidation note: overlaps with #10/#12 on RewriteHandler. Recommend consolidation with those PRs into one post-fix refactor PR.

jdevalk pushed a commit that referenced this pull request Feb 17, 2026
Restore original logic for non-lowercase .md extension detection.
The simplified regex '/\.[Mm][Dd]$/' incorrectly matched lowercase
.md extensions, causing valid requests to be rejected. The original
two-part check correctly identifies only non-lowercase variants.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
(cherry picked from commit 69ff1f7)
@jdevalk
Copy link
Member Author

jdevalk commented Feb 17, 2026

Superseded by consolidated PR #15 to reduce overlap/conflicts. Closing this one in favor of the combined integration path.

@jdevalk jdevalk closed this Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments