@@ -474,30 +474,10 @@ public function is_installed( $args, $assoc_args ) {
474474 * @param array{url: string, title: string, admin_user: string, admin_password?: string, admin_email: string, locale?: string, 'skip-email'?: bool} $assoc_args Associative arguments.
475475 */
476476 public function install ( $ args , $ assoc_args ) {
477- // Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] early to prevent incorrect
478- // URL detection by WordPress. When WP-CLI is executed from the root of the
479- // filesystem (e.g., /wp), these variables contain the WP-CLI executable path
480- // rather than the WordPress installation path, which causes wp_guess_url() to
481- // construct incorrect URLs. This must be done as early as possible.
477+ // Fix $_SERVER variables early to prevent incorrect URL detection.
478+ // See set_server_url_vars() for details.
482479 if ( isset ( $ assoc_args ['url ' ] ) ) {
483- $ url_parts = Utils \parse_url ( $ assoc_args ['url ' ] );
484- $ path = isset ( $ url_parts ['path ' ] ) ? $ url_parts ['path ' ] : '/ ' ;
485-
486- // Ensure path represents a PHP script for proper WordPress URL detection.
487- $ path = rtrim ( $ path , '/ ' );
488- if ( empty ( $ path ) ) {
489- $ path = '/index.php ' ;
490- } elseif ( '' === pathinfo ( $ path , PATHINFO_EXTENSION ) ) {
491- $ path .= '/index.php ' ;
492- }
493-
494- $ _SERVER ['PHP_SELF ' ] = $ path ;
495- $ _SERVER ['SCRIPT_NAME ' ] = $ path ;
496-
497- // Set SCRIPT_FILENAME to the actual WordPress index.php if available.
498- if ( file_exists ( Utils \trailingslashit ( ABSPATH ) . 'index.php ' ) ) {
499- $ _SERVER ['SCRIPT_FILENAME ' ] = Utils \trailingslashit ( ABSPATH ) . 'index.php ' ;
500- }
480+ $ this ->set_server_url_vars ( $ assoc_args ['url ' ] );
501481 }
502482
503483 if ( $ this ->do_install ( $ assoc_args ) ) {
@@ -632,30 +612,10 @@ public function multisite_convert( $args, $assoc_args ) {
632612 * @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.
633613 */
634614 public function multisite_install ( $ args , $ assoc_args ) {
635- // Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] early to prevent incorrect
636- // URL detection by WordPress. When WP-CLI is executed from the root of the
637- // filesystem (e.g., /wp), these variables contain the WP-CLI executable path
638- // rather than the WordPress installation path, which causes wp_guess_url() to
639- // construct incorrect URLs. This must be done as early as possible.
615+ // Fix $_SERVER variables early to prevent incorrect URL detection.
616+ // See set_server_url_vars() for details.
640617 if ( isset ( $ assoc_args ['url ' ] ) ) {
641- $ url_parts = Utils \parse_url ( $ assoc_args ['url ' ] );
642- $ path = isset ( $ url_parts ['path ' ] ) ? $ url_parts ['path ' ] : '/ ' ;
643-
644- // Ensure path represents a PHP script for proper WordPress URL detection.
645- $ path = rtrim ( $ path , '/ ' );
646- if ( empty ( $ path ) ) {
647- $ path = '/index.php ' ;
648- } elseif ( '' === pathinfo ( $ path , PATHINFO_EXTENSION ) ) {
649- $ path .= '/index.php ' ;
650- }
651-
652- $ _SERVER ['PHP_SELF ' ] = $ path ;
653- $ _SERVER ['SCRIPT_NAME ' ] = $ path ;
654-
655- // Set SCRIPT_FILENAME to the actual WordPress index.php if available.
656- if ( file_exists ( Utils \trailingslashit ( ABSPATH ) . 'index.php ' ) ) {
657- $ _SERVER ['SCRIPT_FILENAME ' ] = Utils \trailingslashit ( ABSPATH ) . 'index.php ' ;
658- }
618+ $ this ->set_server_url_vars ( $ assoc_args ['url ' ] );
659619 }
660620
661621 if ( $ this ->do_install ( $ assoc_args ) ) {
@@ -716,6 +676,38 @@ private static function set_multisite_defaults( $assoc_args ) {
716676 return array_merge ( $ defaults , $ assoc_args );
717677 }
718678
679+ /**
680+ * Fix $_SERVER variables to prevent incorrect URL detection during installation.
681+ *
682+ * When WP-CLI is executed from the root of the filesystem (e.g., /wp), PHP_SELF
683+ * and SCRIPT_NAME contain the WP-CLI executable path rather than the WordPress
684+ * installation path. This causes WordPress's wp_guess_url() to construct incorrect
685+ * URLs. This method overrides these variables based on the provided URL to ensure
686+ * correct home/siteurl values during installation.
687+ *
688+ * @param string $url The URL to use for setting server variables.
689+ */
690+ private function set_server_url_vars ( $ url ) {
691+ $ url_parts = Utils \parse_url ( $ url );
692+ $ path = isset ( $ url_parts ['path ' ] ) ? $ url_parts ['path ' ] : '/ ' ;
693+
694+ // Ensure path represents a PHP script for proper WordPress URL detection.
695+ $ path = rtrim ( $ path , '/ ' );
696+ if ( empty ( $ path ) ) {
697+ $ path = '/index.php ' ;
698+ } elseif ( '' === pathinfo ( $ path , PATHINFO_EXTENSION ) ) {
699+ $ path .= '/index.php ' ;
700+ }
701+
702+ $ _SERVER ['PHP_SELF ' ] = $ path ;
703+ $ _SERVER ['SCRIPT_NAME ' ] = $ path ;
704+
705+ // Set SCRIPT_FILENAME to the actual WordPress index.php if available.
706+ if ( file_exists ( Utils \trailingslashit ( ABSPATH ) . 'index.php ' ) ) {
707+ $ _SERVER ['SCRIPT_FILENAME ' ] = Utils \trailingslashit ( ABSPATH ) . 'index.php ' ;
708+ }
709+ }
710+
719711 private function do_install ( $ assoc_args ) {
720712 /**
721713 * @var \wpdb $wpdb
@@ -725,38 +717,11 @@ private function do_install( $assoc_args ) {
725717 return false ;
726718 }
727719
728- // Fix $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME'] early to prevent incorrect
729- // URL detection by WordPress during installation. When WP-CLI is executed from
730- // the root of the filesystem (e.g., /wp), these variables contain the WP-CLI
731- // executable path rather than the WordPress installation path, which causes
732- // wp_guess_url() to construct incorrect URLs. This must be done before loading
733- // any WordPress files that might use these values.
720+ // Fix $_SERVER variables early to prevent incorrect URL detection.
721+ // This must be done before loading any WordPress files. See set_server_url_vars().
734722 if ( isset ( $ assoc_args ['url ' ] ) ) {
735723 WP_CLI ::set_url ( $ assoc_args ['url ' ] );
736-
737- $ url_parts = Utils \parse_url ( $ assoc_args ['url ' ] );
738- $ path = isset ( $ url_parts ['path ' ] ) ? $ url_parts ['path ' ] : '/ ' ;
739-
740- // Ensure path represents a PHP script for proper WordPress URL detection.
741- // If the path doesn't already end with a file (no extension in basename),
742- // append '/index.php' to represent the WordPress entry point.
743- $ path = rtrim ( $ path , '/ ' );
744- if ( empty ( $ path ) ) {
745- $ path = '/index.php ' ;
746- } elseif ( '' === pathinfo ( $ path , PATHINFO_EXTENSION ) ) {
747- // Path doesn't end with a file, append /index.php
748- $ path .= '/index.php ' ;
749- }
750-
751- $ _SERVER ['PHP_SELF ' ] = $ path ;
752- $ _SERVER ['SCRIPT_NAME ' ] = $ path ;
753-
754- // Set SCRIPT_FILENAME to the actual WordPress index.php if available.
755- // This is optional and only set when ABSPATH is defined and index.php exists.
756- // If not set, WordPress can still function using PHP_SELF and SCRIPT_NAME.
757- if ( file_exists ( Utils \trailingslashit ( ABSPATH ) . 'index.php ' ) ) {
758- $ _SERVER ['SCRIPT_FILENAME ' ] = Utils \trailingslashit ( ABSPATH ) . 'index.php ' ;
759- }
724+ $ this ->set_server_url_vars ( $ assoc_args ['url ' ] );
760725 }
761726
762727 if ( true === Utils \get_flag_value ( $ assoc_args , 'skip-email ' ) ) {
0 commit comments