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
20 changes: 20 additions & 0 deletions features/server.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,23 @@ Feature: Serve WordPress locally
"""
Hello world!
"""

Scenario: Adapt HTTPS scheme to HTTP
Given a WP install
And a wp-content/mu-plugins/test-https-url.php file:
"""
<?php add_filter( 'wp_head', function() { echo '<meta name="test-https" content="https://localhost:8184/wp-content/uploads/test.jpg">'; } );
"""
And I run `wp option update home https://localhost:8184`
And I run `wp option update siteurl https://localhost:8184`
And I launch in the background `wp server --host=localhost --port=8184 --adapt-scheme`

When I run `curl -sS localhost:8184`
Then STDOUT should contain:
"""
http://localhost:8184/wp-content/uploads/test.jpg
"""
And STDOUT should not contain:
"""
https://localhost:8184
"""
16 changes: 16 additions & 0 deletions router.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ function ( $url ) {

add_filter( 'got_url_rewrite', '__return_true' );

if ( getenv( 'WPCLI_SERVER_ADAPT_SCHEME' ) ) {
ob_start(
static function ( $buffer ) {
if ( ! isset( $GLOBALS['wpcli_server_original_url'] ) ) {
return $buffer;
}
$original_host = _get_full_host( $GLOBALS['wpcli_server_original_url'] );
return str_replace(
'https://' . $original_host,
'http://' . $_SERVER['HTTP_HOST'],
$buffer
Comment on lines +125 to +129
);
Comment on lines +126 to +130
}
);
}

$_SERVER['SERVER_ADDR'] = gethostbyname( $_SERVER['SERVER_NAME'] );
$wpcli_server_root = $_SERVER['DOCUMENT_ROOT'];
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
Expand Down
17 changes: 16 additions & 1 deletion src/Server_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Server_Command extends WP_CLI_Command {
* [--config=<file>]
* : Configure the server with a specific .ini file.
*
* [--adapt-scheme]
* : Replace HTTPS URLs matching the original site URL with HTTP in server responses.
* Useful when the site is configured with HTTPS but the development server runs on HTTP.
*
* [<passthrough>...]
* : Optional arguments to pass to the PHP binary. Any arguments after `--`
* will be passed through to the `php` command.
Expand Down Expand Up @@ -68,10 +72,17 @@ class Server_Command extends WP_CLI_Command {
* Document root is /var/www/public
* Press Ctrl-C to quit.
*
* # Adapt HTTPS links when the site is configured with HTTPS
* $ wp server --adapt-scheme
* PHP 8.0.0 Development Server started at Wed Nov 10 18:00:00 2025
* Listening on http://localhost:8080
* Document root is /var/www/html
* Press Ctrl-C to quit.
*
* @when before_wp_load
*
* @param array<string> $args Positional arguments passed through to the PHP binary.
* @param array{host: string, port: string, docroot?: string, config?: string} $assoc_args Associative arguments passed to the command.
* @param array{host: string, port: string, docroot?: string, config?: string, 'adapt-scheme'?: bool} $assoc_args Associative arguments passed to the command.
* @return void
*/
public function __invoke( $args, $assoc_args ) {
Expand Down Expand Up @@ -123,6 +134,10 @@ public function __invoke( $args, $assoc_args ) {

$descriptors = array( STDIN, STDOUT, STDERR );

if ( Utils\get_flag_value( $assoc_args, 'adapt-scheme', false ) ) { // @phpstan-ignore argument.type
putenv( 'WPCLI_SERVER_ADAPT_SCHEME=1' );
}

// https://bugs.php.net/bug.php?id=60181
$options = array();
if ( Utils\is_windows() ) {
Expand Down
Loading