Make WordPress Core


Ignore:
Timestamp:
02/02/2021 12:08:01 AM (3 years ago)
Author:
flixos90
Message:

Security, Site Health: Make migrating a site to HTTPS a one-click interaction.

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r50129 r50131  
    75827582
    75837583/**
     7584 * Gets the URL to learn more about updating the site to use HTTPS.
     7585 *
     7586 * This URL can be overridden by specifying an environment variable `WP_UPDATE_HTTPS_URL` or by using the
     7587 * {@see 'wp_update_https_url'} filter. Providing an empty string is not allowed and will result in the
     7588 * default URL being used. Furthermore the page the URL links to should preferably be localized in the
     7589 * site language.
     7590 *
     7591 * @since 5.7.0
     7592 *
     7593 * @return string URL to learn more about updating to HTTPS.
     7594 */
     7595function wp_get_update_https_url() {
     7596    $default_url = wp_get_default_update_https_url();
     7597
     7598    $update_url = $default_url;
     7599    if ( false !== getenv( 'WP_UPDATE_HTTPS_URL' ) ) {
     7600        $update_url = getenv( 'WP_UPDATE_HTTPS_URL' );
     7601    }
     7602
     7603    /**
     7604     * Filters the URL to learn more about updating the HTTPS version the site is running on.
     7605     *
     7606     * Providing an empty string is not allowed and will result in the default URL being used. Furthermore
     7607     * the page the URL links to should preferably be localized in the site language.
     7608     *
     7609     * @since 5.7.0
     7610     *
     7611     * @param string $update_url URL to learn more about updating HTTPS.
     7612     */
     7613    $update_url = apply_filters( 'wp_update_https_url', $update_url );
     7614    if ( empty( $update_url ) ) {
     7615        $update_url = $default_url;
     7616    }
     7617
     7618    return $update_url;
     7619}
     7620
     7621/**
     7622 * Gets the default URL to learn more about updating the site to use HTTPS.
     7623 *
     7624 * Do not use this function to retrieve this URL. Instead, use {@see wp_get_update_https_url()} when relying on the URL.
     7625 * This function does not allow modifying the returned URL, and is only used to compare the actually used URL with the
     7626 * default one.
     7627 *
     7628 * @since 5.7.0
     7629 * @access private
     7630 *
     7631 * @return string Default URL to learn more about updating to HTTPS.
     7632 */
     7633function wp_get_default_update_https_url() {
     7634    /* translators: Documentation explaining HTTPS and why it should be used. */
     7635    return __( 'https://wordpress.org/support/article/why-should-i-use-https/' );
     7636}
     7637
     7638/**
     7639 * Gets the URL for directly updating the site to use HTTPS.
     7640 *
     7641 * A URL will only be returned if the `WP_DIRECT_UPDATE_HTTPS_URL` environment variable is specified or
     7642 * by using the {@see 'wp_direct_update_https_url'} filter. This allows hosts to send users directly to
     7643 * the page where they can update their site to use HTTPS.
     7644 *
     7645 * @since 5.7.0
     7646 *
     7647 * @return string URL for directly updating to HTTPS or empty string.
     7648 */
     7649function wp_get_direct_update_https_url() {
     7650    $direct_update_url = '';
     7651
     7652    if ( false !== getenv( 'WP_DIRECT_UPDATE_HTTPS_URL' ) ) {
     7653        $direct_update_url = getenv( 'WP_DIRECT_UPDATE_HTTPS_URL' );
     7654    }
     7655
     7656    /**
     7657     * Filters the URL for directly updating the PHP version the site is running on from the host.
     7658     *
     7659     * @since 5.7.0
     7660     *
     7661     * @param string $direct_update_url URL for directly updating PHP.
     7662     */
     7663    $direct_update_url = apply_filters( 'wp_direct_update_https_url', $direct_update_url );
     7664
     7665    return $direct_update_url;
     7666}
     7667
     7668/**
    75847669 * Get the size of a directory.
    75857670 *
Note: See TracChangeset for help on using the changeset viewer.