3
3
Plugin Name: Force HTTPS
4
4
Plugin URI: https://www.littlebizzy.com/plugins/force-https
5
5
Description: HTTPS enforcement for WordPress
6
- Version: 3.0 .0
6
+ Version: 2.1 .0
7
7
Author: LittleBizzy
8
8
Author URI: https://www.littlebizzy.com
9
9
Requires PHP: 7.0
@@ -123,40 +123,57 @@ function force_https_filter_output( $content ) {
123
123
add_filter ( 'the_content ' , 'force_https_process_content ' , 20 );
124
124
function force_https_process_content ( $ content ) {
125
125
return preg_replace_callback (
126
- '#(<(?:a|area|audio|blockquote|button|canvas|del|embed|form| iframe|img|input|ins| link|meta|object|picture|q|script| source|style|svg|track|video )[^>]+\s(?:action|background|cite|classid|codebase|content|data-[^\s=]+|formaction| href|longdesc|manifest|ping|poster| src|srcdoc|srcset|style|usemap|xlink:href )=[" \'])(http://|//)([^" \']+)#i ' ,
126
+ '#(<(?:a|img|script| iframe|link|source|form )[^>]+\s(?:href|src)=[" \'])(http://|//)([^" \']+)#i ' ,
127
127
function ( $ matches ) {
128
128
return $ matches [1 ] . 'https:// ' . $ matches [3 ];
129
129
},
130
130
$ content
131
131
);
132
132
}
133
133
134
- // enforce https for wp resource hints
134
+ // force https inside inline script and style content
135
+ add_filter ( 'the_content ' , 'force_https_fix_scripts_styles ' , 20 );
136
+ function force_https_fix_scripts_styles ( $ content ) {
137
+ return preg_replace_callback (
138
+ '#(<script.*?>|<style.*?>)(.*?)</(script|style)>#is ' ,
139
+ function ( $ matches ) {
140
+ return $ matches [1 ] . str_replace ('http:// ' , 'https:// ' , $ matches [2 ]) . '</ ' . $ matches [3 ] . '> ' ;
141
+ },
142
+ $ content
143
+ );
144
+ }
145
+
146
+ // enforce https on wp resource hints to prevent mixed content issues
135
147
add_filter ( 'wp_resource_hints ' , 'force_https_fix_resource_hints ' , 20 );
136
148
function force_https_fix_resource_hints ( $ urls ) {
149
+
150
+ // return unchanged if not an array
137
151
if ( ! is_array ( $ urls ) ) {
138
152
return $ urls ;
139
153
}
154
+
155
+ // enforce https on each resource hint url
140
156
foreach ( $ urls as &$ url ) {
141
157
if ( is_string ( $ url ) ) {
142
158
$ url = set_url_scheme ( $ url , 'https ' );
143
159
} elseif ( is_array ( $ url ) && isset ( $ url ['href ' ] ) ) {
144
160
$ url ['href ' ] = set_url_scheme ( $ url ['href ' ], 'https ' );
145
161
}
146
162
}
163
+
147
164
return $ urls ;
148
165
}
149
166
150
- // enforce https on image srcsets
167
+ // enforce https on image srcsets to prevent mixed content issues
151
168
add_filter ( 'wp_calculate_image_srcset ' , 'force_https_fix_image_srcsets ' , 999 );
152
169
function force_https_fix_image_srcsets ( $ sources ) {
153
170
154
- // exit if sources is not an array
171
+ // return unchanged if sources is not an array
155
172
if ( ! is_array ( $ sources ) ) {
156
173
return $ sources ;
157
174
}
158
175
159
- // loop through each image source and enforce https
176
+ // loop through each source and enforce https on urls
160
177
foreach ( $ sources as &$ source ) {
161
178
if ( isset ( $ source ['url ' ] ) ) {
162
179
$ source ['url ' ] = set_url_scheme ( $ source ['url ' ], 'https ' );
@@ -166,15 +183,20 @@ function force_https_fix_image_srcsets( $sources ) {
166
183
return $ sources ;
167
184
}
168
185
169
- // ensure all urls in the upload directory use https
186
+ // enforce https on urls in the upload directory to avoid insecure media links
170
187
add_filter ( 'upload_dir ' , 'force_https_fix_upload_dir ' , 999 );
171
188
function force_https_fix_upload_dir ( $ uploads ) {
189
+
190
+ // enforce https on the main upload url
172
191
if ( isset ( $ uploads ['url ' ] ) ) {
173
192
$ uploads ['url ' ] = set_url_scheme ( $ uploads ['url ' ], 'https ' );
174
193
}
194
+
195
+ // enforce https on the base upload url
175
196
if ( isset ( $ uploads ['baseurl ' ] ) ) {
176
197
$ uploads ['baseurl ' ] = set_url_scheme ( $ uploads ['baseurl ' ], 'https ' );
177
198
}
199
+
178
200
return $ uploads ;
179
201
}
180
202
0 commit comments