diff --git a/features/server.feature b/features/server.feature index 4a47a73..3c81075 100644 --- a/features/server.feature +++ b/features/server.feature @@ -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: + """ + '; } ); + """ + 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 + """ diff --git a/router.php b/router.php index 201aa28..68bae24 100644 --- a/router.php +++ b/router.php @@ -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 + ); + } + ); +} + $_SERVER['SERVER_ADDR'] = gethostbyname( $_SERVER['SERVER_NAME'] ); $wpcli_server_root = $_SERVER['DOCUMENT_ROOT']; // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url diff --git a/src/Server_Command.php b/src/Server_Command.php index 2c2e350..ebe0e81 100644 --- a/src/Server_Command.php +++ b/src/Server_Command.php @@ -34,6 +34,10 @@ class Server_Command extends WP_CLI_Command { * [--config=] * : 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. + * * [...] * : Optional arguments to pass to the PHP binary. Any arguments after `--` * will be passed through to the `php` command. @@ -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 $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 ) { @@ -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() ) {