| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if (!defined("WPINC")){ |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
function easyfonts_options_page() { |
| 9 |
// Add an options page to the Settings menu |
| 10 |
add_options_page( |
| 11 |
'Easy Fonts Options', |
| 12 |
'Easy Fonts', |
| 13 |
'manage_options', |
| 14 |
'easyfonts', |
| 15 |
'easyfonts_options_page_html' |
| 16 |
); |
| 17 |
} |
| 18 |
add_action('admin_menu', 'easyfonts_options_page'); |
| 19 |
|
| 20 |
function easyfonts_options_page_html() { |
| 21 |
// Check if the user has permission to access this page |
| 22 |
if(!current_user_can('manage_options')) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Check if the form has been submitted |
| 27 |
if(isset($_POST['easyfonts_submit'])) { |
| 28 |
// Sanitize the submitted form data |
| 29 |
$host_google_fonts_locally_link = isset($_POST['easyfonts_host_google_fonts_locally_link']) ? sanitize_text_field($_POST['easyfonts_host_google_fonts_locally_link']) : false; |
| 30 |
$host_google_fonts_locally_import = isset($_POST['easyfonts_host_google_fonts_locally_import']) ? sanitize_text_field($_POST['easyfonts_host_google_fonts_locally_import']) : false; |
| 31 |
$remove_resource_hints = isset($_POST['easyfonts_remove_resource_hints']) ? sanitize_text_field($_POST['easyfonts_remove_resource_hints']) : false; |
| 32 |
$remove_inline_css_fontface = isset($_POST['easyfonts_remove_inline_css_fontface']) ? sanitize_text_field($_POST['easyfonts_remove_inline_css_fontface']) : false; |
| 33 |
$remove_inline_script_font = isset($_POST['easyfonts_remove_inline_script_font']) ? sanitize_text_field($_POST['easyfonts_remove_inline_script_font']) : false; |
| 34 |
|
| 35 |
// Update the 'host_google_fonts_locally link' option |
| 36 |
update_option( |
| 37 |
'easyfonts_host_google_fonts_locally_link', |
| 38 |
$host_google_fonts_locally_link ? true : false |
| 39 |
); |
| 40 |
// Update the 'host_google_fonts_locally @import' option |
| 41 |
update_option( |
| 42 |
'easyfonts_host_google_fonts_locally_import', |
| 43 |
$host_google_fonts_locally_import ? true : false |
| 44 |
); |
| 45 |
|
| 46 |
// Update the 'remove_resource_hints' option |
| 47 |
update_option( |
| 48 |
'easyfonts_remove_resource_hints', |
| 49 |
$remove_resource_hints ? true : false |
| 50 |
); |
| 51 |
update_option( |
| 52 |
'easyfonts_remove_inline_css_fontface', |
| 53 |
$remove_inline_css_fontface ? true : false |
| 54 |
); |
| 55 |
update_option( |
| 56 |
'easyfonts_remove_inline_script_font', |
| 57 |
$remove_inline_script_font ? true : false |
| 58 |
); |
| 59 |
|
| 60 |
// Display a success message |
| 61 |
?> |
| 62 |
<div class="notice notice-success is-dismissible"> |
| 63 |
<p>Your changes have been saved.</p> |
| 64 |
</div> |
| 65 |
<?php |
| 66 |
} |
| 67 |
|
| 68 |
// Get the current values of the options |
| 69 |
$host_google_fonts_locally_link = get_option('easyfonts_host_google_fonts_locally_link', false); |
| 70 |
$host_google_fonts_locally_import = get_option('easyfonts_host_google_fonts_locally_import', false); |
| 71 |
$remove_resource_hints = get_option('easyfonts_remove_resource_hints', false); |
| 72 |
$remove_inline_css_fontface = get_option('easyfonts_remove_inline_css_fontface', false); |
| 73 |
$remove_inline_script_font = get_option('easyfonts_remove_inline_script_font', false); |
| 74 |
|
| 75 |
?> |
| 76 |
<div class="easyfontwrap"> |
| 77 |
|
| 78 |
<form method="post"> |
| 79 |
<div class="heading"> |
| 80 |
<h1>Easy Fonts Options</h1> </div><p class="confirm"> |
| 81 |
To confirm and obtain the list of fonts that are loading on your website <a href="https://easywpstuff.com/google-fonts-checker/" target="_blank">click here</a> |
| 82 |
</p> |
| 83 |
<table class="form-table"> |
| 84 |
<tr> |
| 85 |
<th scope="row"> |
| 86 |
<div class="easyfonts_host_google_fonts_locally_link">Process Google fonts Stylesheet</div> |
| 87 |
</th> |
| 88 |
<td> |
| 89 |
<div class="checkbox-wrapper-2"><label for="easyfonts_host_google_fonts_locally_link"><input class="sc-gJwTLC ikxBAC" type="checkbox" name="easyfonts_host_google_fonts_locally_link" id="easyfonts_host_google_fonts_locally_link" value="1" <?php echo $host_google_fonts_locally_link ? 'checked' : ''; ?>><span class="slider"></span></label></div> |
| 90 |
<p class="description">If enabled, the plugin will download Google Fonts from <code><strong><link></strong></code> and host them locally.</p> |
| 91 |
</td> |
| 92 |
</tr> |
| 93 |
<tr> |
| 94 |
<th scope="row"> |
| 95 |
<div class="easyfonts_host_google_fonts_locally_import">Process @import inline style </div> |
| 96 |
</th> |
| 97 |
<td> |
| 98 |
<div class="checkbox-wrapper-2"><label for="easyfonts_host_google_fonts_locally_import"><input class="sc-gJwTLC ikxBAC" type="checkbox" name="easyfonts_host_google_fonts_locally_import" id="easyfonts_host_google_fonts_locally_import" value="1" <?php echo $host_google_fonts_locally_import ? 'checked' : ''; ?>><span class="slider"></span></label></div> |
| 99 |
<p class="description">If enabled, the plugin will process <code><strong>@import</strong></code> rules from inline <code><strong><style></strong></code> tags.</p> |
| 100 |
</td> |
| 101 |
</tr> |
| 102 |
<tr> |
| 103 |
<th scope="row"> |
| 104 |
<div class="easyfonts_remove_inline_css_fontface">Process @font-face statement</div> |
| 105 |
</th> |
| 106 |
<td> |
| 107 |
<div class="checkbox-wrapper-2"><label for="easyfonts_remove_inline_css_fontface"><input type="checkbox" class="sc-gJwTLC ikxBAC" name="easyfonts_remove_inline_css_fontface" id="easyfonts_remove_inline_css_fontface" value="1" <?php echo $remove_inline_css_fontface ? 'checked' : ''; ?>><span class="slider"></span></label></div> |
| 108 |
<p class="description">If enabled, the plugin will process <code><strong>@font-face</strong></code> statement from inline <code><strong><style></strong></code> tags.</p> |
| 109 |
</td> |
| 110 |
</tr> |
| 111 |
<tr> |
| 112 |
<th scope="row"> |
| 113 |
<div class="easyfonts_remove_resource_hints">Remove Resource Hints</div> |
| 114 |
</th> |
| 115 |
<td> |
| 116 |
<div class="checkbox-wrapper-2"><label for="easyfonts_remove_resource_hints" class="switch"><input class="sc-gJwTLC ikxBAC" type="checkbox" name="easyfonts_remove_resource_hints" id="easyfonts_remove_resource_hints" value="1" <?php echo $remove_resource_hints ? 'checked' : ''; ?>><span class="slider"></span></label></div> |
| 117 |
<p class="description">If enabled, the plugin will remove resource hints (such as <code><strong>preconnect</strong></code>, <code><strong>prefetch</strong></code>, etc) from the website.</p> |
| 118 |
</td> |
| 119 |
</tr> |
| 120 |
|
| 121 |
<tr> |
| 122 |
<th scope="row"> |
| 123 |
<label for="easyfonts_remove_inline_script_font">Remove webfont.js fonts </label> |
| 124 |
</th> |
| 125 |
<td> |
| 126 |
<div class="checkbox-wrapper-2"><label for="easyfonts_remove_inline_script_font"><input type="checkbox" class="sc-gJwTLC ikxBAC" name="easyfonts_remove_inline_script_font" id="easyfonts_remove_inline_script_font" value="1" <?php echo $remove_inline_script_font ? 'checked' : ''; ?>><span class="slider"></span></label></div> |
| 127 |
<p class="description">If enabled, the plugin will remove google fonts loading from <code><strong>webfont.js</strong></code> loader inline <code><strong>script</strong></code> tags.</p> |
| 128 |
</td> |
| 129 |
</tr> |
| 130 |
</table> |
| 131 |
<?php submit_button('Save Changes', 'primary', 'easyfonts_submit'); ?> |
| 132 |
<button type="submit" name="easyfonts_clear_font_cache" class="button remove">Remove All stored Fonts</button> |
| 133 |
<?php if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false) || get_option('easyfonts_remove_inline_css_fontface', false)) : ?> |
| 134 |
<button type="submit" name="easyfonts_preload" class="button preload">Preload Fonts</button> |
| 135 |
<?php endif; ?> |
| 136 |
|
| 137 |
</form> |
| 138 |
<div> |
| 139 |
|
| 140 |
</div> |
| 141 |
<?php if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false)) { easyfonts_list_styles();} ?> |
| 142 |
</div> |
| 143 |
<?php |
| 144 |
// Check if the 'Clear Font Cache' button has been clicked |
| 145 |
if(isset($_POST['easyfonts_clear_font_cache'])) { |
| 146 |
easyfonts_clear_font_cache(); |
| 147 |
} |
| 148 |
if(isset($_POST['easyfonts_preload'])) { |
| 149 |
easyfonts_preload(); |
| 150 |
} |
| 151 |
if(isset($_POST['easyfonts_submit'])){ |
| 152 |
if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false) || get_option('easyfonts_remove_inline_css_fontface', false)) { |
| 153 |
easyfonts_preload_save(); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
function easyfonts_clear_font_cache() { |
| 159 |
// Get the WordPress uploads directory |
| 160 |
$uploads_dir = wp_upload_dir(); |
| 161 |
|
| 162 |
// Build the path to the 'easyfonts' folder |
| 163 |
$easyfonts_dir = $uploads_dir['basedir'] . '/easyfonts/'; |
| 164 |
|
| 165 |
// Open the 'easyfonts' folder |
| 166 |
$handle = opendir($easyfonts_dir); |
| 167 |
|
| 168 |
// Iterate over the files in the 'easyfonts' folder |
| 169 |
while(($file = readdir($handle)) !== false) { |
| 170 |
// Skip the current and parent directories |
| 171 |
if($file == '.' || $file == '..') { |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
// Build the path to the file |
| 176 |
$file_path = $easyfonts_dir . $file; |
| 177 |
|
| 178 |
// Delete the file |
| 179 |
unlink($file_path); |
| 180 |
} |
| 181 |
|
| 182 |
// Close the 'easyfonts' folder |
| 183 |
closedir($handle); |
| 184 |
|
| 185 |
// Display a success message |
| 186 |
?> |
| 187 |
<div class="notice notice-success is-dismissible"> |
| 188 |
<p>The fonts have been removed.</p> |
| 189 |
</div> |
| 190 |
<?php |
| 191 |
} |
| 192 |
|
| 193 |
function easyfonts_preload_save() { |
| 194 |
// Get the current user |
| 195 |
$current_user = wp_get_current_user(); |
| 196 |
// Check if the user is an admin |
| 197 |
if (user_can($current_user, 'manage_options')) { |
| 198 |
// Get the cookies for the current user |
| 199 |
$cookies = wp_get_current_user()->get_session_tokens(); |
| 200 |
// Add the cookies and headers to the request |
| 201 |
$options = array( |
| 202 |
'cookies' => $cookies, |
| 203 |
'headers' => array( |
| 204 |
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', |
| 205 |
), |
| 206 |
); |
| 207 |
// Append a query string to the homepage URL to visit it as an admin |
| 208 |
$home_url = home_url() . '?easyfonts_preload=1'; |
| 209 |
// Visit the homepage as admin to preload the fonts |
| 210 |
wp_remote_get($home_url, $options); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
function easyfonts_preload() { |
| 216 |
|
| 217 |
$current_user = wp_get_current_user(); |
| 218 |
// Check if the user is an admin |
| 219 |
if (user_can($current_user, 'manage_options')) { |
| 220 |
// Get the cookies for the current user |
| 221 |
$cookies = wp_get_current_user()->get_session_tokens(); |
| 222 |
// Add the cookies and headers to the request |
| 223 |
$options = array( |
| 224 |
'cookies' => $cookies, |
| 225 |
'headers' => array( |
| 226 |
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', |
| 227 |
), |
| 228 |
); |
| 229 |
// Append a query string to the homepage URL to visit it as an admin |
| 230 |
$home_url = home_url() . '?easyfonts_preload=1'; |
| 231 |
// Visit the homepage as admin to preload the fonts |
| 232 |
wp_remote_get($home_url, $options); |
| 233 |
} |
| 234 |
|
| 235 |
// Display a success message |
| 236 |
?> |
| 237 |
<div class="notice notice-success is-dismissible"> |
| 238 |
<p>The fonts have been preloaded.</p> |
| 239 |
</div> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
function easyfonts_list_styles() { |
| 244 |
$easyfonts_dir = wp_upload_dir()['basedir'].'/easyfonts/'; |
| 245 |
$css_files = array(); |
| 246 |
$style_data = array(); |
| 247 |
try { |
| 248 |
if(!is_dir($easyfonts_dir)){ |
| 249 |
throw new Exception("Please make sure to preload the font or visit the homepage"); |
| 250 |
} |
| 251 |
$dir = new DirectoryIterator($easyfonts_dir); |
| 252 |
// iterate over the files in the directory |
| 253 |
foreach ($dir as $file) { |
| 254 |
if (!$file->isFile()) continue; |
| 255 |
if($file->getExtension() == 'css'){ |
| 256 |
$css_files[] = $file->getFilename(); |
| 257 |
} |
| 258 |
} |
| 259 |
if(!empty($css_files)){ |
| 260 |
echo '<table class="styled-table">'; |
| 261 |
echo '<thead>'; |
| 262 |
echo '<tr>'; |
| 263 |
echo '<th>Hosted Fonts CSS URL</th>'; |
| 264 |
echo '<th>Font Families</th>'; |
| 265 |
echo '<th>Variants</th>'; |
| 266 |
echo '</tr>'; |
| 267 |
echo '</thead>'; |
| 268 |
echo '<tbody>'; |
| 269 |
foreach($css_files as $file_name) { |
| 270 |
$file_url = wp_upload_dir()['baseurl'].'/easyfonts/'.$file_name; |
| 271 |
$file_path = $easyfonts_dir.$file_name; |
| 272 |
$file_content = file_get_contents($file_path); |
| 273 |
$font_family = array(); |
| 274 |
$variant = array(); |
| 275 |
$variant_italic = array(); |
| 276 |
$variant_normal = array(); |
| 277 |
preg_match_all("/@font-face\s*{[^}]+}/", $file_content, $matches); |
| 278 |
if (count($matches[0]) > 0) { |
| 279 |
foreach ($matches[0] as $font_face) { |
| 280 |
if (preg_match("/font-family:([^;]+);/", $font_face, $font_family_match)) { |
| 281 |
$font_family[] = trim($font_family_match[1]); |
| 282 |
} |
| 283 |
if (preg_match("/font-style:([^;]+);/", $font_face, $variant_match)) { |
| 284 |
$style = trim($variant_match[1]); |
| 285 |
if($style == 'italic'){ |
| 286 |
if (preg_match("/font-weight:([^;]+);/", $font_face, $variant_match)) { |
| 287 |
$variant = trim($variant_match[1]); |
| 288 |
if (!in_array($variant, $variant_italic)) { |
| 289 |
$variant_italic[] = $variant; |
| 290 |
} |
| 291 |
} |
| 292 |
}elseif($style == 'normal'){ |
| 293 |
if (preg_match("/font-weight:([^;]+);/", $font_face, $variant_match)) { |
| 294 |
$variant = trim($variant_match[1]); |
| 295 |
if (!in_array($variant, $variant_normal)) { |
| 296 |
$variant_normal[] = $variant; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
} |
| 304 |
$font_family = array_unique($font_family); |
| 305 |
$style_data[] = array( |
| 306 |
'file_url' => $file_url, |
| 307 |
'font_families' => $font_family, |
| 308 |
'variant' => 'italic '.implode(',', $variant_italic) . ' | normal '.implode(',', $variant_normal) |
| 309 |
); |
| 310 |
} |
| 311 |
if(!empty($style_data)){ |
| 312 |
foreach($style_data as $style){ |
| 313 |
echo '<tr>'; |
| 314 |
echo '<td>'. esc_url($style['file_url']) .'</td>'; |
| 315 |
echo '<td>'. esc_attr(implode(',', $style['font_families'])) .'</td>'; |
| 316 |
echo '<td>'. esc_html($style['variant']) .'</td>'; |
| 317 |
echo '</tr>'; |
| 318 |
} |
| 319 |
} |
| 320 |
echo '</tbody>'; |
| 321 |
echo '</table>'; |
| 322 |
}else{ |
| 323 |
if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false)) { |
| 324 |
echo 'fonts styles are not found. Preload Font first or visit the homepage.'; |
| 325 |
} |
| 326 |
} |
| 327 |
} catch (Exception $e) { |
| 328 |
echo 'Error: ' . $e->getMessage(); |
| 329 |
} |
| 330 |
} |
| 331 |
|