Skip to content

Commit c36fc0d

Browse files
authored
Update force-https.php
1 parent f33dbc1 commit c36fc0d

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

force-https.php

+15-11
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ function force_https_filter_home( $value ) {
3939
// enforce https by redirecting non-ssl requests on frontend, admin, and login pages
4040
function force_https_redirect() {
4141

42-
// exit if already using https, headers are sent, or running via cli or ajax, or no request uri exists
43-
if ( is_ssl() || headers_sent() || defined( 'WP_CLI' ) || ( defined('DOING_AJAX') && DOING_AJAX ) || ! isset( $_SERVER['REQUEST_URI'] ) ) {
42+
// exit if already using https, headers are sent, running via cli, cron, or ajax, or no request uri exists
43+
if ( is_ssl() || headers_sent() || defined( 'WP_CLI' ) || defined( 'DOING_CRON' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ! isset( $_SERVER['REQUEST_URI'] ) ) {
44+
return;
45+
}
46+
47+
// fallback check for https in server if is_ssl fails
48+
if ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) !== 'off' ) {
4449
return;
4550
}
4651

@@ -125,9 +130,9 @@ function force_https_filter_output( $content ) {
125130
add_filter( 'the_content', 'force_https_process_content', 20 );
126131
function force_https_process_content( $content ) {
127132
return preg_replace_callback(
128-
'#(<(?:a|img|script|iframe|link|source|form)[^>]+\s(?:href|src)=["\'])(http://)([^"\']+)#i',
133+
'#(<(?:a|img|script|iframe|link|source|form)[^>]+\s(?:href|src)=["\'])http://([^"\']+)#i',
129134
function( $matches ) {
130-
return $matches[1] . 'https://' . $matches[3];
135+
return $matches[1] . 'https://' . $matches[2];
131136
},
132137
$content
133138
);
@@ -148,18 +153,17 @@ function( $matches ) {
148153
// enforce https on wp resource hints to prevent mixed content issues
149154
add_filter( 'wp_resource_hints', 'force_https_fix_resource_hints', 20 );
150155
function force_https_fix_resource_hints( $urls ) {
151-
152156
// return unchanged if not an array
153157
if ( ! is_array( $urls ) ) {
154158
return $urls;
155159
}
156160

157161
// enforce https on each resource hint url
158-
foreach ( $urls as &$url ) {
162+
foreach ( $urls as $key => $url ) {
159163
if ( is_string( $url ) ) {
160-
$url = set_url_scheme( $url, 'https' );
164+
$urls[$key] = set_url_scheme( $url, 'https' );
161165
} elseif ( is_array( $url ) && isset( $url['href'] ) ) {
162-
$url['href'] = set_url_scheme( $url['href'], 'https' );
166+
$urls[$key]['href'] = set_url_scheme( $url['href'], 'https' );
163167
}
164168
}
165169

@@ -169,16 +173,16 @@ function force_https_fix_resource_hints( $urls ) {
169173
// enforce https on image srcsets to prevent mixed content issues
170174
add_filter( 'wp_calculate_image_srcset', 'force_https_fix_image_srcsets', 999 );
171175
function force_https_fix_image_srcsets( $sources ) {
172-
173176
// return unchanged if sources is not an array
174177
if ( ! is_array( $sources ) ) {
175178
return $sources;
176179
}
177180

178181
// loop through each source and enforce https on urls
179-
foreach ( $sources as &$source ) {
182+
foreach ( $sources as $key => $source ) {
183+
// check if url is set and enforce https
180184
if ( isset( $source['url'] ) ) {
181-
$source['url'] = set_url_scheme( $source['url'], 'https' );
185+
$sources[$key]['url'] = set_url_scheme( $source['url'], 'https' );
182186
}
183187
}
184188

0 commit comments

Comments
 (0)