| | 1 | <?php |
| | 2 | /** |
| | 3 | * HTTPS detection functions. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @since 5.4.0 |
| | 7 | */ |
| | 8 | |
| | 9 | /** |
| | 10 | * Checks whether the website is using HTTPS. |
| | 11 | * |
| | 12 | * This is based on whether the home and site URL are using HTTPS. |
| | 13 | * |
| | 14 | * @since 5.4.0 |
| | 15 | * |
| | 16 | * @return bool True if using HTTPS, false otherwise. |
| | 17 | */ |
| | 18 | function wp_is_using_https() { |
| | 19 | if ( 'https' !== wp_parse_url( home_url(), PHP_URL_SCHEME ) ) { |
| | 20 | return false; |
| | 21 | } |
| | 22 | |
| | 23 | if ( 'https' !== wp_parse_url( site_url(), PHP_URL_SCHEME ) ) { |
| | 24 | return false; |
| | 25 | } |
| | 26 | |
| | 27 | return true; |
| | 28 | } |
| | 29 | |
| | 30 | /** |
| | 31 | * Checks whether HTTPS is supported for the server and domain. |
| | 32 | * |
| | 33 | * @since 5.4.0 |
| | 34 | * |
| | 35 | * @return bool True if HTTPS is supported, false otherwise. |
| | 36 | */ |
| | 37 | function wp_is_https_supported() { |
| | 38 | $https_detection_errors = get_option( 'https_detection_errors' ); |
| | 39 | |
| | 40 | // If option has never been set by the Cron hook before, run it on-the-fly as fallback. |
| | 41 | if ( false === $https_detection_errors ) { |
| | 42 | wp_update_https_detection_errors(); |
| | 43 | |
| | 44 | $https_detection_errors = get_option( 'https_detection_errors' ); |
| | 45 | } |
| | 46 | |
| | 47 | // If there are no detection errors, HTTPS is supported. |
| | 48 | return empty( $https_detection_errors ); |
| | 49 | } |
| | 50 | |
| | 51 | /** |
| | 52 | * Runs a remote HTTPS request to detect whether HTTPS supported, and stores potential errors. |
| | 53 | * |
| | 54 | * This internal function is called by a regular Cron hook to ensure HTTPS support is detected and maintained. |
| | 55 | * |
| | 56 | * @since 5.4.0 |
| | 57 | * @access private |
| | 58 | */ |
| | 59 | function wp_update_https_detection_errors() { |
| | 60 | $support_errors = new WP_Error(); |
| | 61 | |
| | 62 | $response = wp_remote_request( |
| | 63 | home_url( '/', 'https' ), |
| | 64 | array( |
| | 65 | 'headers' => array( |
| | 66 | 'Cache-Control' => 'no-cache', |
| | 67 | ), |
| | 68 | 'sslverify' => true, |
| | 69 | ) |
| | 70 | ); |
| | 71 | |
| | 72 | if ( is_wp_error( $response ) ) { |
| | 73 | $unverified_response = wp_remote_request( |
| | 74 | home_url( '/', 'https' ), |
| | 75 | array( |
| | 76 | 'headers' => array( |
| | 77 | 'Cache-Control' => 'no-cache', |
| | 78 | ), |
| | 79 | 'sslverify' => false, |
| | 80 | ) |
| | 81 | ); |
| | 82 | |
| | 83 | if ( is_wp_error( $unverified_response ) ) { |
| | 84 | $support_errors->add( |
| | 85 | $unverified_response->get_error_code(), |
| | 86 | $unverified_response->get_error_message() |
| | 87 | ); |
| | 88 | } else { |
| | 89 | $support_errors->add( |
| | 90 | 'ssl_verification_failed', |
| | 91 | $response->get_error_message() |
| | 92 | ); |
| | 93 | } |
| | 94 | |
| | 95 | $response = $unverified_response; |
| | 96 | } |
| | 97 | |
| | 98 | if ( ! is_wp_error( $response ) && 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| | 99 | $support_errors->add( 'response_error', wp_remote_retrieve_response_message( $response ) ); |
| | 100 | } |
| | 101 | |
| | 102 | update_option( 'https_detection_errors', $support_errors->errors ); |
| | 103 | } |
| | 104 | |
| | 105 | /** |
| | 106 | * Schedules the Cron hook for detecting HTTPS support. |
| | 107 | * |
| | 108 | * @since 5.4.0 |
| | 109 | * @access private |
| | 110 | */ |
| | 111 | function wp_cron_schedule_https_detection() { |
| | 112 | if ( ! wp_next_scheduled( 'wp_https_detection' ) ) { |
| | 113 | wp_schedule_event( time(), 'twicedaily', 'wp_https_detection' ); |
| | 114 | } |
| | 115 | } |
| | 116 | |
| | 117 | /** |
| | 118 | * Disables SSL verification if the 'cron_request' arguments include an HTTPS URL. |
| | 119 | * |
| | 120 | * This prevents an issue if HTTPS breaks, where there would be a failed attempt to verify HTTPS. |
| | 121 | * |
| | 122 | * @since 5.4.0 |
| | 123 | * @access private |
| | 124 | * |
| | 125 | * @param array $request The Cron request arguments. |
| | 126 | * @return array $request The filtered Cron request arguments. |
| | 127 | */ |
| | 128 | function wp_cron_conditionally_prevent_sslverify( $request ) { |
| | 129 | if ( 'https' === wp_parse_url( $request['url'], PHP_URL_SCHEME ) ) { |
| | 130 | $request['args']['sslverify'] = false; |
| | 131 | } |
| | 132 | return $request; |
| | 133 | } |
| | 134 | |
| | 135 | /** |
| | 136 | * Replaces insecure HTTP URLs with HTTPS URLs in the given content. |
| | 137 | * |
| | 138 | * @since 5.4.0 |
| | 139 | * |
| | 140 | * @param string $content Content to replace insecure URLs in. |
| | 141 | * @return string Modified content. |
| | 142 | */ |
| | 143 | function wp_replace_insecure_self_links( $content ) { |
| | 144 | if ( ! wp_is_using_https() ) { |
| | 145 | return $content; |
| | 146 | } |
| | 147 | |
| | 148 | return str_replace( home_url( '', 'http' ), home_url( '', 'https' ), $content ); |
| | 149 | } |