From 400e0f3cd1331a81812da1208ba91a9471d99563 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Fri, 23 Oct 2026 23:14:27 +0530 Subject: [PATCH 1/3] fix(ssl): keep the WordPress URLs on http while the site is served over HTTP WordPress is installed with https:// URLs before the certificate step. When that step fails, site-command now keeps the site on plain HTTP, but WordPress kept redirecting visitors and wp-admin to HTTPS, which isn't served. The same happens while a manual DNS-01 challenge waits for `ee site ssl-verify`. After www_ssl_wrapper(), a create whose site isn't served over HTTPS switches the URLs back to http (search-replace, with --network for multisite). `update --ssl=le` still switches them to https when it issues the certificate, but no longer while a manual DNS-01 challenge is pending; site-command's new site_scheme_changed() hook switches them once ssl-verify issues the certificate. With an older site-command, only the failed create is handled. The search-replace of update_ssl() moves into replace_url_scheme(), which now warns instead of reporting success when it fails. --- src/WordPress.php | 102 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 85 insertions(+), 17 deletions(-) diff --git a/src/WordPress.php b/src/WordPress.php index a4ea073..4016e61 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -1004,7 +1004,7 @@ private function create_site( $assoc_args ) { if ( 'custom' === $this->site_data['site_ssl'] ) { $this->custom_site_ssl(); } - $this->www_ssl_wrapper( [ 'nginx' ] ); + $this->run_ssl_step(); } catch ( \Exception $e ) { $this->catch_clean( $e ); } @@ -1051,6 +1051,21 @@ private function create_site( $assoc_args ) { $this->info( [ $this->site_data['site_url'] ], [] ); } + /** + * Runs the certificate step and switches the URLs back to http when the site isn't served over HTTPS afterwards. + */ + private function run_ssl_step() { + + // install_wp() used https:// URLs for any SSL type. + $installed_with_https = ! $this->skip_install && ! empty( $this->site_data['site_ssl'] ); + + $this->www_ssl_wrapper( [ 'nginx' ] ); + + if ( $installed_with_https && ! $this->serves_https() ) { + $this->replace_url_scheme( 'https', 'http', false ); + } + } + /** * Download WordPress core into the site, retrying transient failures. * @@ -1462,32 +1477,85 @@ private function create_site_db_entry() { protected function update_ssl( $assoc_args ) { parent::update_ssl( $assoc_args ); + + $ssl = get_flag_value( $assoc_args, 'ssl' ); + // A manual DNS-01 challenge keeps the site on HTTP; site_scheme_changed() switches the URLs once ssl-verify succeeds. + if ( 'off' !== $ssl && ! $this->serves_https() ) { + return; + } + + $this->replace_url_scheme( 'off' === $ssl ? 'https' : 'http', 'off' === $ssl ? 'http' : 'https' ); + } + + /** + * Switches the WordPress URLs when site-command changes the site's scheme outside `ee site update --ssl`. + * + * @param string $scheme The scheme the site is served over now: http or https. + */ + protected function site_scheme_changed( $scheme ) { + chdir( $this->site_data['site_fs_path'] ); + // A site created with --skip-install has no URLs to switch yet. + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec php wp core is-installed' ) ) { + return; + } - EE::log( 'Running search-replace.' ); - EE::log( 'Taking database backup before search-replace.' ); - EE::exec( sprintf( \EE_DOCKER::docker_compose_with_custom() . ' exec php wp db export --skip-ssl %s.db', $this->site_data['site_url'] ) ); + $this->replace_url_scheme( 'https' === $scheme ? 'http' : 'https', $scheme ); + } + + /** + * Whether the site is served over HTTPS. site-command keeps it on HTTP when its certificate could not be issued (it + * clears site_ssl) and while a manual DNS-01 challenge waits for `ee site ssl-verify`. + * + * @return bool + */ + private function serves_https() { - $db_file = $this->site_data['site_fs_path'] . '/app/htdocs/' . $this->site_data['site_url'] . '.db'; + if ( empty( $this->site_data['site_ssl'] ) ) { + return false; + } + + // Older site-command versions have no pending state. + return ! function_exists( '\EE\Site\Utils\is_le_ssl_pending' ) || ! \EE\Site\Utils\is_le_ssl_pending( $this->site_data['site_url'], $this->site_data['site_ssl'] ); + } + + /** + * Rewrites the site's URLs in its database from one scheme to the other. + * + * @param string $from Scheme of the URLs now. + * @param string $to Scheme to switch them to. + * @param bool $backup Whether to export the database first. + */ + private function replace_url_scheme( $from, $to, $backup = true ) { + + chdir( $this->site_data['site_fs_path'] ); + + EE::log( 'Running search-replace.' ); + $backup_success = false; $backup_location = EE_BACKUP_DIR . '/' . $this->site_data['site_url'] . '/' . $this->site_data['site_url'] . '.db'; - $this->fs->mkdir( dirname( $backup_location ) ); + if ( $backup ) { + EE::log( 'Taking database backup before search-replace.' ); + EE::exec( sprintf( \EE_DOCKER::docker_compose_with_custom() . ' exec php wp db export --skip-ssl %s.db', $this->site_data['site_url'] ) ); - $backup_success = false; - if ( $this->fs->exists( $db_file ) ) { - $this->fs->remove( $backup_location ); - $this->fs->rename( $db_file, $backup_location ); - $backup_success = true; + $db_file = $this->site_data['site_fs_path'] . '/app/htdocs/' . $this->site_data['site_url'] . '.db'; + $this->fs->mkdir( dirname( $backup_location ) ); + + if ( $this->fs->exists( $db_file ) ) { + $this->fs->remove( $backup_location ); + $this->fs->rename( $db_file, $backup_location ); + $backup_success = true; + } } $extra_flags = '--precise'; $extra_flags .= ( 'wp' === $this->site_data['app_sub_type'] ) ? '' : ' --network'; - $ssl = get_flag_value( $assoc_args, 'ssl' ); - $from = $ssl === 'off' ? 'https' : 'http'; - $to = $ssl === 'off' ? 'http' : 'https'; - - EE::exec( sprintf( \EE_DOCKER::docker_compose_with_custom() . ' exec php wp search-replace %2$s://%1$s %3$s://%1$s %4$s', $this->site_data['site_url'], $from, $to, $extra_flags ), true, true ); - EE::success( 'Successfully completed search-replace.' ); + $search_replace = sprintf( 'wp search-replace %2$s://%1$s %3$s://%1$s %4$s', $this->site_data['site_url'], $from, $to, $extra_flags ); + if ( EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' exec php ' . $search_replace, true, true ) ) { + EE::success( 'Successfully completed search-replace.' ); + } else { + EE::warning( sprintf( 'The site URLs could not be switched to %1$s. Run `ee shell %2$s --command=\'%3$s\'` to switch them.', $to, $this->site_data['site_url'], $search_replace ) ); + } if ( $backup_success ) { EE::log( "In case something is not working as intended, you can restore your DB from backup file generated before search-replace located at:\n `$backup_location`\nand proceed with search-replace according to your needs." ); From 8e27c66a5bf72dbf6defac9e424f332d8d9c8921 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 16 Nov 2026 16:23:49 +0530 Subject: [PATCH 2/3] fix(ssl): switch subdomain multisite subsites to the new URL scheme too The network-wide search-replace only matches the main site's origin, so subdomain subsites (covered by the wildcard certificate) kept their old scheme; each one is now switched with its own search-replace, while mapped domains keep theirs. --- src/WordPress.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/WordPress.php b/src/WordPress.php index 4016e61..a694ac9 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -1557,11 +1557,47 @@ private function replace_url_scheme( $from, $to, $backup = true ) { EE::warning( sprintf( 'The site URLs could not be switched to %1$s. Run `ee shell %2$s --command=\'%3$s\'` to switch them.', $to, $this->site_data['site_url'], $search_replace ) ); } + if ( 'subdom' === $this->site_data['app_sub_type'] ) { + $this->replace_subsite_url_scheme( $from, $to ); + } + if ( $backup_success ) { EE::log( "In case something is not working as intended, you can restore your DB from backup file generated before search-replace located at:\n `$backup_location`\nand proceed with search-replace according to your needs." ); } } + /** + * Switches the scheme of the subdomain subsites, whose origins the network-wide search for the site's own origin doesn't match. + * + * @param string $from Scheme of the URLs now. + * @param string $to Scheme to switch them to. + */ + private function replace_subsite_url_scheme( $from, $to ) { + + $compose = \EE_DOCKER::docker_compose_with_custom(); + $sites = EE::launch( $compose . ' exec -T php wp site list --field=url' ); + if ( $sites->return_code ) { + EE::warning( 'Could not list the subsites, so their URLs were not switched to ' . $to . '.' ); + + return; + } + + $suffix = '.' . $this->site_data['site_url']; + foreach ( preg_split( '/\R/', trim( $sites->stdout ) ) as $url ) { + $host = (string) parse_url( trim( $url ), PHP_URL_HOST ); + // Only subdomains of the site are covered by its certificate; mapped domains keep their scheme. + // The host goes into a shell command unquoted, so accept plain hostnames only. + if ( parse_url( trim( $url ), PHP_URL_SCHEME ) !== $from || substr( $host, -strlen( $suffix ) ) !== $suffix || ! preg_match( '/^[a-z0-9.-]+$/i', $host ) ) { + continue; + } + + $search_replace = sprintf( 'wp search-replace %2$s://%1$s %3$s://%1$s --precise --url=%1$s', $host, $from, $to ); + if ( ! EE::exec( $compose . ' exec php ' . $search_replace, true, true ) ) { + EE::warning( sprintf( 'The URLs of %1$s could not be switched to %2$s. Run `ee shell %3$s --command=\'%4$s\'` to switch them.', $host, $to, $this->site_data['site_url'], $search_replace ) ); + } + } + } + /** * Restarts containers associated with site. * When no service(--nginx etc.) is specified, all site containers will be restarted. From dcc3d64ccaaa11bdb9119393a6c1108f3f5c6428 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 16 Nov 2026 17:02:48 +0530 Subject: [PATCH 3/3] fix(ssl): switch subsite URLs across the whole network A subsite's origin can be referenced from the main site or other subsites, so its search-replace now runs with --network instead of only on its own tables. --- src/WordPress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WordPress.php b/src/WordPress.php index a694ac9..6b12c27 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -1591,7 +1591,7 @@ private function replace_subsite_url_scheme( $from, $to ) { continue; } - $search_replace = sprintf( 'wp search-replace %2$s://%1$s %3$s://%1$s --precise --url=%1$s', $host, $from, $to ); + $search_replace = sprintf( 'wp search-replace %2$s://%1$s %3$s://%1$s --precise --network', $host, $from, $to ); if ( ! EE::exec( $compose . ' exec php ' . $search_replace, true, true ) ) { EE::warning( sprintf( 'The URLs of %1$s could not be switched to %2$s. Run `ee shell %3$s --command=\'%4$s\'` to switch them.', $host, $to, $this->site_data['site_url'], $search_replace ) ); }