| 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']) && isset($_POST['easyfonts_nonce']) && wp_verify_nonce($_POST['easyfonts_nonce'], 'easyfonts_options_page_nonce')) { |
| 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 |
<?php wp_nonce_field('easyfonts_options_page_nonce', 'easyfonts_nonce'); ?> |
| 80 |
<div class="heading"> |
| 81 |
<h1>Easy Fonts Options</h1> </div><p class="confirm"> |
| 82 |
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> |
| 83 |
</p> |
| 84 |
<table class="form-table"> |
| 85 |
<tr> |
| 86 |
<th scope="row"> |
| 87 |
<div class="easyfonts_host_google_fonts_locally_link">Process Google fonts Stylesheet</div> |
| 88 |
</th> |
| 89 |
<td> |
| 90 |
<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> |
| 91 |
<p class="description">If enabled, the plugin will download Google Fonts from <code><strong><link></strong></code> and host them locally.</p> |
| 92 |
</td> |
| 93 |
</tr> |
| 94 |
<tr> |
| 95 |
<th scope="row"> |
| 96 |
<div class="easyfonts_host_google_fonts_locally_import">Process @import inline style </div> |
| 97 |
</th> |
| 98 |
<td> |
| 99 |
<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> |
| 100 |
<p class="description">If enabled, the plugin will process <code><strong>@import</strong></code> rules from inline <code><strong><style></strong></code> tags.</p> |
| 101 |
</td> |
| 102 |
</tr> |
| 103 |
<tr> |
| 104 |
<th scope="row"> |
| 105 |
<div class="easyfonts_remove_inline_css_fontface">Process @font-face statement</div> |
| 106 |
</th> |
| 107 |
<td> |
| 108 |
<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> |
| 109 |
<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> |
| 110 |
</td> |
| 111 |
</tr> |
| 112 |
<tr> |
| 113 |
<th scope="row"> |
| 114 |
<div class="easyfonts_remove_resource_hints">Remove Resource Hints</div> |
| 115 |
</th> |
| 116 |
<td> |
| 117 |
<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> |
| 118 |
<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> |
| 119 |
</td> |
| 120 |
</tr> |
| 121 |
|
| 122 |
<tr> |
| 123 |
<th scope="row"> |
| 124 |
<label for="easyfonts_remove_inline_script_font">Remove webfont.js fonts </label> |
| 125 |
</th> |
| 126 |
<td> |
| 127 |
<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> |
| 128 |
<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> |
| 129 |
</td> |
| 130 |
</tr> |
| 131 |
</table> |
| 132 |
<?php submit_button('Save Changes', 'primary', 'easyfonts_submit'); ?> |
| 133 |
<button type="submit" name="easyfonts_clear_font_cache" class="button remove">Remove All stored Fonts</button> |
| 134 |
<?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)) : ?> |
| 135 |
<button type="submit" name="easyfonts_preload" class="button preload">Preload Fonts</button> |
| 136 |
<?php endif; ?> |
| 137 |
|
| 138 |
</form> |
| 139 |
<div> |
| 140 |
|
| 141 |
</div> |
| 142 |
<?php if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false)) { easyfonts_list_styles();} ?> |
| 143 |
</div> |
| 144 |
<?php |
| 145 |
// Check if the 'Clear Font Cache' button has been clicked |
| 146 |
if(isset($_POST['easyfonts_clear_font_cache']) && isset($_POST['easyfonts_nonce']) && wp_verify_nonce($_POST['easyfonts_nonce'], 'easyfonts_options_page_nonce')) { |
| 147 |
easyfonts_clear_font_cache(); |
| 148 |
} |
| 149 |
if(isset($_POST['easyfonts_preload']) && isset($_POST['easyfonts_nonce']) && wp_verify_nonce($_POST['easyfonts_nonce'], 'easyfonts_options_page_nonce')) { |
| 150 |
easyfonts_preload(); |
| 151 |
} |
| 152 |
if(isset($_POST['easyfonts_submit']) && isset($_POST['easyfonts_nonce']) && wp_verify_nonce($_POST['easyfonts_nonce'], 'easyfonts_options_page_nonce')){ |
| 153 |
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)) { |
| 154 |
easyfonts_preload_save(); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
function easyfonts_clear_font_cache() { |
| 160 |
// Get the WordPress uploads directory |
| 161 |
$uploads_dir = wp_upload_dir(); |
| 162 |
|
| 163 |
// Build the path to the 'easyfonts' folder |
| 164 |
$easyfonts_dir = $uploads_dir['basedir'] . '/easyfonts/'; |
| 165 |
|
| 166 |
// Open the 'easyfonts' folder |
| 167 |
$handle = opendir($easyfonts_dir); |
| 168 |
|
| 169 |
// Iterate over the files in the 'easyfonts' folder |
| 170 |
while(($file = readdir($handle)) !== false) { |
| 171 |
// Skip the current and parent directories |
| 172 |
if($file == '.' || $file == '..') { |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
// Build the path to the file |
| 177 |
$file_path = $easyfonts_dir . $file; |
| 178 |
|
| 179 |
// Delete the file |
| 180 |
unlink($file_path); |
| 181 |
} |
| 182 |
|
| 183 |
// Close the 'easyfonts' folder |
| 184 |
closedir($handle); |
| 185 |
|
| 186 |
// Display a success message |
| 187 |
?> |
| 188 |
<div class="notice notice-success is-dismissible"> |
| 189 |
<p>The fonts have been removed.</p> |
| 190 |
</div> |
| 191 |
<?php |
| 192 |
} |
| 193 |
|
| 194 |
function easyfonts_preload_save() { |
| 195 |
// Get the current user |
| 196 |
$current_user = wp_get_current_user(); |
| 197 |
// Check if the user is an admin |
| 198 |
if (user_can($current_user, 'manage_options')) { |
| 199 |
// Get the cookies for the current user |
| 200 |
$cookies = wp_get_current_user()->get_session_tokens(); |
| 201 |
// Add the cookies and headers to the request |
| 202 |
$options = array( |
| 203 |
'cookies' => $cookies, |
| 204 |
'headers' => array( |
| 205 |
'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', |
| 206 |
), |
| 207 |
); |
| 208 |
// Append a query string to the homepage URL to visit it as an admin |
| 209 |
$home_url = home_url() . '?easyfonts_preload=1'; |
| 210 |
// Visit the homepage as admin to preload the fonts |
| 211 |
wp_remote_get($home_url, $options); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
function easyfonts_preload() { |
| 217 |
|
| 218 |
$current_user = wp_get_current_user(); |
| 219 |
// Check if the user is an admin |
| 220 |
if (user_can($current_user, 'manage_options')) { |
| 221 |
// Get the cookies for the current user |
| 222 |
$cookies = wp_get_current_user()->get_session_tokens(); |
| 223 |
// Add the cookies and headers to the request |
| 224 |
$options = array( |
| 225 |
'cookies' => $cookies, |
| 226 |
'headers' => array( |
| 227 |
'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', |
| 228 |
), |
| 229 |
); |
| 230 |
// Append a query string to the homepage URL to visit it as an admin |
| 231 |
$home_url = home_url() . '?easyfonts_preload=1'; |
| 232 |
// Visit the homepage as admin to preload the fonts |
| 233 |
wp_remote_get($home_url, $options); |
| 234 |
} |
| 235 |
|
| 236 |
// Display a success message |
| 237 |
?> |
| 238 |
<div class="notice notice-success is-dismissible"> |
| 239 |
<p>The fonts have been preloaded.</p> |
| 240 |
</div> |
| 241 |
<?php |
| 242 |
} |
| 243 |
|
| 244 |
function easyfonts_list_styles() { |
| 245 |
$easyfonts_dir = wp_upload_dir()['basedir'] . '/easyfonts/'; |
| 246 |
$css_files = array(); |
| 247 |
$style_data = array(); |
| 248 |
|
| 249 |
try { |
| 250 |
if (!is_dir($easyfonts_dir)) { |
| 251 |
throw new Exception("Please make sure to preload the font or visit the homepage."); |
| 252 |
} |
| 253 |
|
| 254 |
$dir = new DirectoryIterator($easyfonts_dir); |
| 255 |
foreach ($dir as $file) { |
| 256 |
if (!$file->isFile()) continue; |
| 257 |
if ($file->getExtension() === 'css') { |
| 258 |
$css_files[] = $file->getFilename(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
if (!empty($css_files)) { |
| 263 |
echo '<table class="styled-table">'; |
| 264 |
echo '<thead>'; |
| 265 |
echo '<tr>'; |
| 266 |
echo '<th>Hosted Fonts CSS URL</th>'; |
| 267 |
echo '<th>Font Families</th>'; |
| 268 |
echo '<th>Variants</th>'; |
| 269 |
echo '</tr>'; |
| 270 |
echo '</thead>'; |
| 271 |
echo '<tbody>'; |
| 272 |
|
| 273 |
foreach ($css_files as $file_name) { |
| 274 |
// Prevent directory traversal |
| 275 |
if (strpos($file_name, '..') !== false) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
$file_url = wp_upload_dir()['baseurl'] . '/easyfonts/' . $file_name; |
| 280 |
$file_path = $easyfonts_dir . $file_name; |
| 281 |
|
| 282 |
// Ensure the file is readable |
| 283 |
if (!is_readable($file_path)) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
$file_content = file_get_contents($file_path); |
| 288 |
$font_family = array(); |
| 289 |
$variant_italic = array(); |
| 290 |
$variant_normal = array(); |
| 291 |
|
| 292 |
preg_match_all("/@font-face\s*{[^}]+}/", $file_content, $matches); |
| 293 |
if (!empty($matches[0])) { |
| 294 |
foreach ($matches[0] as $font_face) { |
| 295 |
if (preg_match("/font-family:\s*['\"]?([^;'\"]+)['\"]?;/", $font_face, $font_family_match)) { |
| 296 |
$font_family[] = trim($font_family_match[1]); |
| 297 |
} |
| 298 |
if (preg_match("/font-style:\s*([^;]+);/", $font_face, $variant_match)) { |
| 299 |
$style = trim($variant_match[1]); |
| 300 |
if (preg_match("/font-weight:\s*([^;]+);/", $font_face, $weight_match)) { |
| 301 |
$variant = trim($weight_match[1]); |
| 302 |
if ($style === 'italic' && !in_array($variant, $variant_italic)) { |
| 303 |
$variant_italic[] = $variant; |
| 304 |
} elseif ($style === 'normal' && !in_array($variant, $variant_normal)) { |
| 305 |
$variant_normal[] = $variant; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
$font_family = array_unique($font_family); |
| 313 |
$style_data[] = array( |
| 314 |
'file_url' => esc_url($file_url), |
| 315 |
'font_families' => esc_attr(implode(',', $font_family)), |
| 316 |
'variant' => esc_html('italic ' . implode(',', $variant_italic) . ' | normal ' . implode(',', $variant_normal)) |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
foreach ($style_data as $style) { |
| 321 |
echo '<tr>'; |
| 322 |
echo '<td><a href="' . esc_url($style['file_url']) . '" target="_blank" rel="noopener">' . esc_html($style['file_url']) . '</a></td>'; |
| 323 |
echo '<td>' . esc_html($style['font_families']) . '</td>'; |
| 324 |
echo '<td>' . esc_html($style['variant']) . '</td>'; |
| 325 |
echo '</tr>'; |
| 326 |
} |
| 327 |
|
| 328 |
echo '</tbody>'; |
| 329 |
echo '</table>'; |
| 330 |
} else { |
| 331 |
if (get_option('easyfonts_host_google_fonts_locally_link', false) || get_option('easyfonts_host_google_fonts_locally_import', false)) { |
| 332 |
echo esc_html__('Fonts styles are not found. Preload the font first or visit the homepage.', 'easyfonts'); |
| 333 |
} |
| 334 |
} |
| 335 |
} catch (Exception $e) { |
| 336 |
echo '<div class="error-message">' . esc_html__('Error: ', 'easyfonts') . esc_html($e->getMessage()) . '</div>'; |
| 337 |
} |
| 338 |
} |
| 339 |
|