PluginProbe
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant / 1.0.4
EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant v1.0.4
2.0.4 2.0.3 2.0.2 2.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2 1.3 2.0.0
easyfonts / inc / options.php

options.php in EasyFonts – Host Google Fonts Locally, Fast & Auto-Optimize, GDPR Compliant 1.0.4, at inc/options.php

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