Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions features/server.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Feature: Serve WordPress locally
Just another WordPress site
"""

When I run `curl -sS localhost:8181/license.txt > /tmp/license.txt`
And I run `cmp /tmp/license.txt license.txt`
Then STDOUT should be empty
When I run `curl -sSL http://localhost:8181/license.txt`
Then STDOUT should contain:
"""
WordPress - Web publishing software
"""

Scenario: Passthrough arguments to PHP binary
Given a WP install
Expand Down Expand Up @@ -43,8 +45,8 @@ Feature: Serve WordPress locally
Scenario: Pretty permalinks
Given a WP install
And I launch in the background `wp server --host=localhost --port=8183`
And I run `wp option update permalink_structure '/%postname%/'`
And I run `wp rewrite flush`
# No leading slash for Windows compatibility on CI.
And I run `wp rewrite structure "%postname%/"`

When I run `curl -sSL http://localhost:8183/hello-world/`
Then STDOUT should contain:
Expand Down
19 changes: 12 additions & 7 deletions router.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,34 @@ static function ( $buffer ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
$wpcli_server_path = '/' . ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ) )['path'], '/' );

if ( file_exists( $wpcli_server_root . $wpcli_server_path ) ) {
if ( is_dir( $wpcli_server_root . $wpcli_server_path ) && substr( $wpcli_server_path, -1 ) !== '/' ) {
$wpcli_server_file = $wpcli_server_root . $wpcli_server_path;
// Normalize slashes for file operations
$wpcli_server_file = str_replace( array( '/', '\\' ), DIRECTORY_SEPARATOR, $wpcli_server_file );

if ( file_exists( $wpcli_server_file ) ) {
if ( is_dir( $wpcli_server_file ) && substr( $wpcli_server_path, -1 ) !== '/' ) {
header( "Location: $wpcli_server_path/" );
exit;
}

// Check if this is a PHP file by examining the extension
if ( pathinfo( $wpcli_server_path, PATHINFO_EXTENSION ) === 'php' ) {
if ( pathinfo( $wpcli_server_file, PATHINFO_EXTENSION ) === 'php' ) {
// Set $_SERVER variables to mimic direct access to the PHP file
$_SERVER['SCRIPT_NAME'] = $wpcli_server_path;
$_SERVER['PHP_SELF'] = $wpcli_server_path;
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_root . $wpcli_server_path;
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_file;

chdir( dirname( $wpcli_server_root . $wpcli_server_path ) );
require_once $wpcli_server_root . $wpcli_server_path;
chdir( dirname( $wpcli_server_file ) );
require_once $wpcli_server_file;
} else {
return false;
}
} else {
// File doesn't exist - route to index.php for pretty permalinks
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['PHP_SELF'] = '/index.php';
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_root . '/index.php';
$_SERVER['SCRIPT_FILENAME'] = $wpcli_server_root . DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['PATH_INFO'] = $wpcli_server_path; // Help WordPress parse request

chdir( $wpcli_server_root );
require_once 'index.php';
Expand Down