Skip to content

Commit ef8c6fb

Browse files
Copilotswissspidy
andcommitted
Apply $_SERVER fix in install() and multisite_install() methods before do_install() call
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 023b834 commit ef8c6fb

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

features/core-install.feature

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,13 @@ Feature: Install WordPress core
225225
http://example.com/subdir
226226
"""
227227
228-
Scenario: Install with simulated root filesystem WP-CLI executable location
228+
Scenario: Install ensures correct siteurl and home regardless of PHP_SELF
229229
Given an empty directory
230230
And WP files
231231
And wp-config.php
232232
And a database
233-
And a problematic-server-vars.php file:
234-
"""
235-
<?php
236-
// Simulate WP-CLI being executed from root filesystem (e.g., /wp)
237-
// This is the problematic scenario that the fix addresses
238-
$_SERVER['PHP_SELF'] = '/wp';
239-
$_SERVER['SCRIPT_NAME'] = '/wp';
240-
$_SERVER['SCRIPT_FILENAME'] = '/wp';
241-
"""
242233
243-
When I run `wp core install --url=https://example.com --title=Test --admin_user=wpcli --admin_email=wpcli@example.org --admin_password=password --skip-email --require=problematic-server-vars.php`
234+
When I run `wp core install --url=https://example.com --title=Test --admin_user=wpcli --admin_email=wpcli@example.org --admin_password=password --skip-email`
244235
Then STDOUT should contain:
245236
"""
246237
Success: WordPress installed successfully.

src/Core_Command.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,32 @@ public function is_installed( $args, $assoc_args ) {
469469
* @param array{url: string, title: string, admin_user: string, admin_password?: string, admin_email: string, locale?: string, 'skip-email'?: bool} $assoc_args Associative arguments.
470470
*/
471471
public function install( $args, $assoc_args ) {
472+
// Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] early to prevent incorrect
473+
// URL detection by WordPress. When WP-CLI is executed from the root of the
474+
// filesystem (e.g., /wp), these variables contain the WP-CLI executable path
475+
// rather than the WordPress installation path, which causes wp_guess_url() to
476+
// construct incorrect URLs. This must be done as early as possible.
477+
if ( isset( $assoc_args['url'] ) ) {
478+
$url_parts = Utils\parse_url( $assoc_args['url'] );
479+
$path = isset( $url_parts['path'] ) ? $url_parts['path'] : '/';
480+
481+
// Ensure path represents a PHP script for proper WordPress URL detection.
482+
$path = rtrim( $path, '/' );
483+
if ( empty( $path ) ) {
484+
$path = '/index.php';
485+
} elseif ( '' === pathinfo( $path, PATHINFO_EXTENSION ) ) {
486+
$path .= '/index.php';
487+
}
488+
489+
$_SERVER['PHP_SELF'] = $path;
490+
$_SERVER['SCRIPT_NAME'] = $path;
491+
492+
// Set SCRIPT_FILENAME to the actual WordPress index.php if available.
493+
if ( file_exists( Utils\trailingslashit( ABSPATH ) . 'index.php' ) ) {
494+
$_SERVER['SCRIPT_FILENAME'] = Utils\trailingslashit( ABSPATH ) . 'index.php';
495+
}
496+
}
497+
472498
if ( $this->do_install( $assoc_args ) ) {
473499
WP_CLI::success( 'WordPress installed successfully.' );
474500
} else {
@@ -600,6 +626,32 @@ public function multisite_convert( $args, $assoc_args ) {
600626
* @param array{url?: string, base: string, subdomains?: bool, title: string, admin_user: string, admin_password?: string, admin_email: string, 'skip-email'?: bool, 'skip-config'?: bool} $assoc_args Associative arguments.
601627
*/
602628
public function multisite_install( $args, $assoc_args ) {
629+
// Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] early to prevent incorrect
630+
// URL detection by WordPress. When WP-CLI is executed from the root of the
631+
// filesystem (e.g., /wp), these variables contain the WP-CLI executable path
632+
// rather than the WordPress installation path, which causes wp_guess_url() to
633+
// construct incorrect URLs. This must be done as early as possible.
634+
if ( isset( $assoc_args['url'] ) ) {
635+
$url_parts = Utils\parse_url( $assoc_args['url'] );
636+
$path = isset( $url_parts['path'] ) ? $url_parts['path'] : '/';
637+
638+
// Ensure path represents a PHP script for proper WordPress URL detection.
639+
$path = rtrim( $path, '/' );
640+
if ( empty( $path ) ) {
641+
$path = '/index.php';
642+
} elseif ( '' === pathinfo( $path, PATHINFO_EXTENSION ) ) {
643+
$path .= '/index.php';
644+
}
645+
646+
$_SERVER['PHP_SELF'] = $path;
647+
$_SERVER['SCRIPT_NAME'] = $path;
648+
649+
// Set SCRIPT_FILENAME to the actual WordPress index.php if available.
650+
if ( file_exists( Utils\trailingslashit( ABSPATH ) . 'index.php' ) ) {
651+
$_SERVER['SCRIPT_FILENAME'] = Utils\trailingslashit( ABSPATH ) . 'index.php';
652+
}
653+
}
654+
603655
if ( $this->do_install( $assoc_args ) ) {
604656
WP_CLI::log( 'Created single site database tables.' );
605657
} else {

0 commit comments

Comments
 (0)