Skip to content

Commit ac29aff

Browse files
Copilotswissspidy
andcommitted
Fix router to support pretty permalinks and multiple entry points
Co-authored-by: swissspidy <[email protected]>
1 parent 1db2efb commit ac29aff

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

features/server.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,36 @@ Feature: Serve WordPress locally
1515
When I run `curl -sS localhost:8181/license.txt > /tmp/license.txt`
1616
And I run `cmp /tmp/license.txt license.txt`
1717
Then STDOUT should be empty
18+
19+
Scenario: Access wp-login.php
20+
Given a WP install
21+
And I launch in the background `wp server --host=localhost --port=8182`
22+
23+
When I run `curl -sS localhost:8182/wp-login.php`
24+
Then STDOUT should contain:
25+
"""
26+
<form name="loginform"
27+
"""
28+
29+
Scenario: Pretty permalinks with posts
30+
Given a WP install
31+
And I launch in the background `wp server --host=localhost --port=8183`
32+
And I run `wp rewrite structure '/%postname%/' --hard`
33+
And I run `wp post create --post_title='Test Post' --post_status=publish --porcelain`
34+
And save STDOUT as {POST_ID}
35+
36+
When I run `curl -sS localhost:8183/test-post/`
37+
Then STDOUT should contain:
38+
"""
39+
Test Post
40+
"""
41+
42+
Scenario: Access wp-admin entry point
43+
Given a WP install
44+
And I launch in the background `wp server --host=localhost --port=8184`
45+
46+
When I run `curl -sS -L localhost:8184/wp-admin/`
47+
Then STDOUT should contain:
48+
"""
49+
<form name="loginform"
50+
"""

router.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,24 @@ function ( $url ) {
123123
exit;
124124
}
125125

126-
if ( strpos( $wpcli_server_path, '.php' ) !== false ) {
126+
// Check if this is a PHP file by examining the extension
127+
if ( pathinfo( $wpcli_server_path, PATHINFO_EXTENSION ) === 'php' ) {
128+
// Set $_SERVER variables to mimic direct access to the PHP file
129+
$_SERVER['SCRIPT_NAME'] = $wpcli_server_path;
130+
$_SERVER['PHP_SELF'] = $wpcli_server_path;
131+
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_root . $wpcli_server_path;
132+
127133
chdir( dirname( $wpcli_server_root . $wpcli_server_path ) );
128134
require_once $wpcli_server_root . $wpcli_server_path;
129135
} else {
130136
return false;
131137
}
132138
} else {
139+
// File doesn't exist - route to index.php for pretty permalinks
140+
$_SERVER['SCRIPT_NAME'] = '/index.php';
141+
$_SERVER['PHP_SELF'] = '/index.php';
142+
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_root . '/index.php';
143+
133144
chdir( $wpcli_server_root );
134145
require_once 'index.php';
135146
}

0 commit comments

Comments
 (0)