@@ -39,8 +39,13 @@ function force_https_filter_home( $value ) {
39
39
// enforce https by redirecting non-ssl requests on frontend, admin, and login pages
40
40
function force_https_redirect () {
41
41
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 ' ) {
44
49
return ;
45
50
}
46
51
@@ -125,9 +130,9 @@ function force_https_filter_output( $content ) {
125
130
add_filter ( 'the_content ' , 'force_https_process_content ' , 20 );
126
131
function force_https_process_content ( $ content ) {
127
132
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 ' ,
129
134
function ( $ matches ) {
130
- return $ matches [1 ] . 'https:// ' . $ matches [3 ];
135
+ return $ matches [1 ] . 'https:// ' . $ matches [2 ];
131
136
},
132
137
$ content
133
138
);
@@ -148,18 +153,17 @@ function( $matches ) {
148
153
// enforce https on wp resource hints to prevent mixed content issues
149
154
add_filter ( 'wp_resource_hints ' , 'force_https_fix_resource_hints ' , 20 );
150
155
function force_https_fix_resource_hints ( $ urls ) {
151
-
152
156
// return unchanged if not an array
153
157
if ( ! is_array ( $ urls ) ) {
154
158
return $ urls ;
155
159
}
156
160
157
161
// enforce https on each resource hint url
158
- foreach ( $ urls as & $ url ) {
162
+ foreach ( $ urls as $ key => $ url ) {
159
163
if ( is_string ( $ url ) ) {
160
- $ url = set_url_scheme ( $ url , 'https ' );
164
+ $ urls [ $ key ] = set_url_scheme ( $ url , 'https ' );
161
165
} 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 ' );
163
167
}
164
168
}
165
169
@@ -169,16 +173,16 @@ function force_https_fix_resource_hints( $urls ) {
169
173
// enforce https on image srcsets to prevent mixed content issues
170
174
add_filter ( 'wp_calculate_image_srcset ' , 'force_https_fix_image_srcsets ' , 999 );
171
175
function force_https_fix_image_srcsets ( $ sources ) {
172
-
173
176
// return unchanged if sources is not an array
174
177
if ( ! is_array ( $ sources ) ) {
175
178
return $ sources ;
176
179
}
177
180
178
181
// 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
180
184
if ( isset ( $ source ['url ' ] ) ) {
181
- $ source ['url ' ] = set_url_scheme ( $ source ['url ' ], 'https ' );
185
+ $ sources [ $ key ] ['url ' ] = set_url_scheme ( $ source ['url ' ], 'https ' );
182
186
}
183
187
}
184
188
0 commit comments