refactor: simplify RewriteHandler validation and output logic#13
refactor: simplify RewriteHandler validation and output logic#13
Conversation
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
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
ContentRendererinstance caching viaget_renderer()method
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Router/RewriteHandler.php
Outdated
|
|
||
| // 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)) { |
There was a problem hiding this comment.
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).
| 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) | |
| ) { |
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>
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)
|
Superseded by consolidated PR #15 to reduce overlap/conflicts. Closing this one in favor of the combined integration path. |
Summary
Eliminates code duplication in
RewriteHandlerby extracting shared validation and output logic into reusable private methods.Changes
validate_post_for_markdown()to consolidate post type and status checksserve_markdown_response()to consolidate password check, rendering, and outputContentRendererinstance to avoid creating new objects on every request.mdenforcement logic for better readabilityImpact
handle_format_parameter()andhandle_markdown_request()Testing
.mdURLs serve markdown?format=markdownparameter works🤖 Generated with Claude Code