mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Switching a WordPress site from HTTP to HTTPS has historically been a tedious task. While on the surface the Site Address and WordPress Address have to be updated, existing content still remains using HTTP URLs where hard-coded in the database. Furthermore, updating _two_ URLs to migrate to HTTPS is still a fairly unintuitive step which is not clearly explained.
This changeset simplifies migration from HTTP to HTTPS and, where possible, makes it a one-click interaction.
* Automatically replace insecure versions of the Site Address (`home_url()`) with its HTTPS counterpart on the fly if the site has been migrated from HTTP to HTTPS. This is accomplished by introducing a `https_migration_required` option and enabling it when the `home_url()` is accordingly changed.
* A new `wp_replace_insecure_home_url()` function is hooked into various pieces of content to replace URLs accordingly.
* The migration only kicks in when the Site Address (`home_url()`) and WordPress Address (`site_url()`) match, which is the widely common case. Configurations where these differ are often maintained by more advanced users, where this migration routine would be less essential - something to potentially iterate on in the future though.
* The migration does not actually update content in the database. More savvy users that prefer to do that can prevent the migration logic from running by either deleting the `https_migration_required` option or using the new `wp_should_replace_insecure_home_url` filter.
* For fresh sites that do not have any content yet at the point of changing the URLs to HTTPS, the migration will also be skipped since it would not be relevant.
* Expose a primary action in the Site Health recommendation, if HTTPS is already supported by the environment, built on top of the HTTPS detection mechanism from [49904]. When clicked, the default behavior is to update `home_url()` and `site_url()` in one go to their HTTPS counterpart.
* A new `wp_update_urls_to_https()` function takes care of the update routine.
* A new `update_https` meta capability is introduced to control access.
* If the site's URLs are controlled by constants, this update is not automatically possible, so in these scenarios the user is informed about that in the HTTPS status check in Site Health.
* Allow hosting providers to modify the URLs linked to in the HTTPS status check in Site Health, similar to how that is possible for the URLs around updating the PHP version.
* A `WP_UPDATE_HTTPS_URL` environment variable or `wp_update_https_url` filter can be used to provide a custom URL with guidance about updating the site to use HTTPS.
* A `WP_DIRECT_UPDATE_HTTPS_URL` environment variable or `wp_direct_update_https_url` filter can be used to provide a custom URL for the primary CTA to update the site to use HTTPS.
Props flixos90, timothyblynjacobs.
Fixes #51437.
git-svn-id: https://develop.svn.wordpress.org/trunk@50131 602fd350-edb4-49c9-b593-d223f7449a82
181 lines
6.5 KiB
PHP
181 lines
6.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group https-migration
|
|
*/
|
|
class Tests_HTTPS_Migration extends WP_UnitTestCase {
|
|
|
|
/**
|
|
* @ticket 51437
|
|
*/
|
|
public function test_wp_should_replace_insecure_home_url() {
|
|
// Should return false because site is not using HTTPS.
|
|
$this->force_wp_is_using_https( false );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should still return false because HTTPS migration flag is not set.
|
|
$this->force_wp_is_using_https( true );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should return false because HTTPS migration flag is marked as not required.
|
|
update_option( 'https_migration_required', '0' );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should return true because HTTPS migration flag is marked as required.
|
|
update_option( 'https_migration_required', '1' );
|
|
$this->assertTrue( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should be overridable via filter.
|
|
add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
}
|
|
|
|
/**
|
|
* @ticket 51437
|
|
*/
|
|
public function test_wp_replace_insecure_home_url() {
|
|
$http_url = home_url( '', 'http' );
|
|
$https_url = home_url( '', 'https' );
|
|
|
|
$http_block_data = array(
|
|
'id' => 3,
|
|
'url' => $http_url . '/wp-content/uploads/2021/01/image.jpg',
|
|
);
|
|
$https_block_data = array(
|
|
'id' => 3,
|
|
'url' => $https_url . '/wp-content/uploads/2021/01/image.jpg',
|
|
);
|
|
|
|
$content = '
|
|
<!-- wp:paragraph -->
|
|
<p><a href="%1$s">This is a link.</a></p>
|
|
<!-- /wp:paragraph -->
|
|
|
|
<!-- wp:custom-media %2$s -->
|
|
<img src="%3$s" alt="">
|
|
<!-- /wp:custom-media -->
|
|
';
|
|
|
|
$http_content = sprintf( $content, $http_url, wp_json_encode( $http_block_data ), $http_block_data['url'] );
|
|
$https_content = sprintf( $content, $https_url, wp_json_encode( $https_block_data ), $https_block_data['url'] );
|
|
|
|
// Replaces URLs, including its encoded variant.
|
|
add_filter( 'wp_should_replace_insecure_home_url', '__return_true' );
|
|
$this->assertEquals( $https_content, wp_replace_insecure_home_url( $http_content ) );
|
|
|
|
// Does not replace anything if determined as unnecessary.
|
|
add_filter( 'wp_should_replace_insecure_home_url', '__return_false' );
|
|
$this->assertEquals( $http_content, wp_replace_insecure_home_url( $http_content ) );
|
|
}
|
|
|
|
/**
|
|
* @ticket 51437
|
|
*/
|
|
public function test_wp_update_urls_to_https() {
|
|
remove_all_filters( 'option_home' );
|
|
remove_all_filters( 'option_siteurl' );
|
|
remove_all_filters( 'home_url' );
|
|
remove_all_filters( 'site_url' );
|
|
|
|
$http_url = 'http://example.org';
|
|
$https_url = 'https://example.org';
|
|
|
|
// Set up options to use HTTP URLs.
|
|
update_option( 'home', $http_url );
|
|
update_option( 'siteurl', $http_url );
|
|
|
|
// Update URLs to HTTPS (successfully).
|
|
$this->assertTrue( wp_update_urls_to_https() );
|
|
$this->assertEquals( $https_url, get_option( 'home' ) );
|
|
$this->assertEquals( $https_url, get_option( 'siteurl' ) );
|
|
|
|
// Switch options back to use HTTP URLs, but now add filter to
|
|
// force option value which will make the update irrelevant.
|
|
update_option( 'home', $http_url );
|
|
update_option( 'siteurl', $http_url );
|
|
$this->force_option( 'home', $http_url );
|
|
|
|
// Update URLs to HTTPS. While the update technically succeeds, it does not take effect due to the enforced
|
|
// option. Therefore the change is expected to be reverted.
|
|
$this->assertFalse( wp_update_urls_to_https() );
|
|
$this->assertEquals( $http_url, get_option( 'home' ) );
|
|
$this->assertEquals( $http_url, get_option( 'siteurl' ) );
|
|
}
|
|
|
|
/**
|
|
* @ticket 51437
|
|
*/
|
|
public function test_wp_update_https_migration_required() {
|
|
// Changing HTTP to HTTPS on a site with content should result in flag being set, requiring migration.
|
|
update_option( 'fresh_site', '0' );
|
|
wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
|
|
$this->assertEquals( '1', get_option( 'https_migration_required' ) );
|
|
|
|
// Changing another part than the scheme should delete/reset the flag because changing those parts (e.g. the
|
|
// domain) can have further implications.
|
|
wp_update_https_migration_required( 'http://example.org', 'https://another-example.org' );
|
|
$this->assertFalse( get_option( 'https_migration_required' ) );
|
|
|
|
// Changing HTTP to HTTPS on a site without content should result in flag being set, but not requiring migration.
|
|
update_option( 'fresh_site', '1' );
|
|
wp_update_https_migration_required( 'http://example.org', 'https://example.org' );
|
|
$this->assertEquals( '', get_option( 'https_migration_required' ) );
|
|
|
|
// Changing (back) from HTTPS to HTTP should delete/reset the flag.
|
|
wp_update_https_migration_required( 'https://example.org', 'http://example.org' );
|
|
$this->assertFalse( get_option( 'https_migration_required' ) );
|
|
}
|
|
|
|
/**
|
|
* @ticket 51437
|
|
*/
|
|
public function test_wp_should_replace_insecure_home_url_integration() {
|
|
// Setup (a site on HTTP, with existing content).
|
|
remove_all_filters( 'option_home' );
|
|
remove_all_filters( 'option_siteurl' );
|
|
remove_all_filters( 'home_url' );
|
|
remove_all_filters( 'site_url' );
|
|
$http_url = 'http://example.org';
|
|
$https_url = 'https://example.org';
|
|
update_option( 'home', $http_url );
|
|
update_option( 'siteurl', $http_url );
|
|
update_option( 'fresh_site', '0' );
|
|
|
|
// Should return false when URLs are HTTP.
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should still return false because only one of the two URLs was updated to its HTTPS counterpart.
|
|
update_option( 'home', $https_url );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should return true because now both URLs are updated to their HTTPS counterpart.
|
|
update_option( 'siteurl', $https_url );
|
|
$this->assertTrue( wp_should_replace_insecure_home_url() );
|
|
|
|
// Should return false because the domains of 'home' and 'siteurl' do not match, and we shouldn't make any
|
|
// assumptions about such special cases.
|
|
update_option( 'siteurl', 'https://wp.example.org' );
|
|
$this->assertFalse( wp_should_replace_insecure_home_url() );
|
|
}
|
|
|
|
private function force_wp_is_using_https( $enabled ) {
|
|
$scheme = $enabled ? 'https' : 'http';
|
|
|
|
$replace_scheme = function( $url ) use ( $scheme ) {
|
|
return str_replace( array( 'http://', 'https://' ), $scheme . '://', $url );
|
|
};
|
|
|
|
add_filter( 'home_url', $replace_scheme, 99 );
|
|
add_filter( 'site_url', $replace_scheme, 99 );
|
|
}
|
|
|
|
private function force_option( $option, $value ) {
|
|
add_filter(
|
|
"option_$option",
|
|
function() use ( $value ) {
|
|
return $value;
|
|
}
|
|
);
|
|
}
|
|
}
|