Changeset 2965733
- Timestamp:
- 09/12/2023 08:46:44 AM (2 years ago)
- Location:
- easyfonts
- Files:
-
- 10 added
- 2 edited
-
tags/1.1.2 (added)
-
tags/1.1.2/assets (added)
-
tags/1.1.2/assets/style.css (added)
-
tags/1.1.2/easyfonts.php (added)
-
tags/1.1.2/inc (added)
-
tags/1.1.2/inc/notices.php (added)
-
tags/1.1.2/inc/options.php (added)
-
tags/1.1.2/lib (added)
-
tags/1.1.2/lib/simple_html_dom.php (added)
-
tags/1.1.2/readme.txt (added)
-
trunk/easyfonts.php (modified) (8 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
easyfonts/trunk/easyfonts.php
r2961663 r2965733 4 4 Plugin URI: 5 5 Description: Automatically Host existing google fonts locally on your server 6 Version: 1.1. 16 Version: 1.1.2 7 7 Author: Uzair 8 8 Author URI: https://easywpstuff.com … … 22 22 23 23 function get_base_url() { 24 if (is_ssl()) { 25 $base_url = set_url_scheme(wp_upload_dir()['baseurl'], 'https'); 26 } else { 27 $base_url = wp_upload_dir()['baseurl']; 28 } 29 return $base_url; 30 } 31 32 function get_basecss_url() { 33 if (is_ssl()) { 34 $basecss_url = set_url_scheme(wp_upload_dir()['baseurl'], 'https'); 35 } else { 36 $basecss_url = wp_upload_dir()['baseurl']; 37 } 38 return $basecss_url; 39 } 40 41 function wp_get_remote_css($decoded_url) { 42 $backtrace = debug_backtrace(); 43 $function_name = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : ''; 44 45 $response = wp_remote_get($decoded_url, array('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',)); 46 47 if (is_wp_error($response)) { 48 add_settings_error($function_name, 'http_error', $response->get_error_message(), 'error'); 49 return false; 50 } 51 52 $getcss = wp_remote_retrieve_body($response); 53 return $getcss; 54 } 55 56 function wp_get_remote_font($font_url) { 57 $backtrace = debug_backtrace(); 58 $function_name = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : ''; 59 $font_response = wp_remote_get($font_url, array('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',)); 60 if (is_wp_error($font_response)) { 61 add_settings_error($function_name, 'http_error', $font_response->get_error_message(), 'error'); 62 return false; 63 } 64 $getfont_data = wp_remote_retrieve_body($font_response); 65 return $getfont_data; 24 return is_ssl() ? set_url_scheme(wp_upload_dir()['baseurl'], 'https') : wp_upload_dir()['baseurl']; 25 } 26 27 function wp_get_remote_file($url) { 28 $backtrace = debug_backtrace(); 29 $function_name = isset($backtrace[1]['function']) ? $backtrace[1]['function'] : ''; 30 31 32 $response = wp_remote_get($url, array('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',)); 33 34 35 if (is_wp_error($response)) { 36 add_settings_error($function_name, 'http_error', $response->get_error_message(), 'error'); 37 return false; 38 } 39 40 41 return wp_remote_retrieve_body($response); 66 42 } 67 43 … … 79 55 return $content; 80 56 } 57 58 function easyfonts_process_font_face_declarations($css, $easyfonts_dir) { 59 if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) { 60 foreach ($matches[1] as $match) { 61 if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) { 62 foreach ($srcs[1] as $src) { 63 if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) { 64 $font_url = trim($url_match[1], "'\""); 65 $font_filename = hash('sha256', $font_url, false); 66 $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION); 67 if (!file_exists($easyfonts_dir . '/' . $font_filename)) { 68 $font_data = wp_get_remote_file($font_url); 69 // Save the font file to the 'wp-content/uploads/easyfonts' directory 70 file_put_contents($easyfonts_dir . '/' . $font_filename, $font_data); 71 } 72 // Replace the src property with a local reference to the downloaded font file 73 $finalbase = get_base_url(); 74 $css = str_replace($src, "url('" . $finalbase . "/easyfonts/" . $font_filename . "')", $css); 75 } 76 } 77 } 78 } 79 } 80 return $css; 81 } 82 81 83 82 84 function easyfont_process_content_link_tag($content) { … … 98 100 $filename = substr($filename, 0, 10) . '.css'; 99 101 if (!file_exists($easyfonts_dir . '/' . $filename)) { 100 $css = wp_get_remote_ css($decoded_url);102 $css = wp_get_remote_file($decoded_url); 101 103 if (!preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) continue; 102 103 if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) { 104 foreach ($matches[1] as $match) { 105 if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) { 106 foreach ($srcs[1] as $src) { 107 if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) { 108 $font_url = trim($url_match[1], "'\""); 109 $font_filename = hash('sha256', $font_url, false); 110 $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION); 111 if (!file_exists($easyfonts_dir . '/' . $font_filename)) { 112 $font_data = wp_get_remote_font($font_url); 113 // Save the font file to the 'wp-content/uploads/easyfonts' directory 114 file_put_contents($easyfonts_dir . '/' . $font_filename, $font_data); 115 } 116 // Replace the src property with a local reference to the downloaded font file 117 $finalbase = get_base_url(); 118 $css = str_replace($src, "url('" . $finalbase . "/easyfonts/" . $font_filename . "')", $css); 119 } 120 } 121 } 122 } 123 } 104 105 $css = easyfonts_process_font_face_declarations($css, $easyfonts_dir); 124 106 // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory 125 107 file_put_contents($easyfonts_dir . '/' . $filename, $css); 126 108 } 127 $finalbasecss = get_base css_url();109 $finalbasecss = get_base_url(); 128 110 // Replace the href attribute with a local reference to the downloaded stylesheet 129 111 $link->href = $finalbasecss . '/easyfonts/' . $filename; … … 155 137 // Check if the stylesheet file already exists 156 138 if (!file_exists($easyfonts_dir . '/' . $filename)) { 157 $css = wp_get_remote_css($decoded_url); 158 if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) { 159 foreach ($matches[1] as $match) { 160 if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) { 161 foreach ($srcs[1] as $src) { 162 if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) { 163 $font_url = trim($url_match[1], "'\""); 164 $font_filename = hash('sha256', $font_url, false); 165 $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION); 166 // Check if the font file already exists 167 if (!file_exists($easyfonts_dir . '/' . $font_filename)) { 168 169 $font_data = wp_get_remote_font($font_url); 170 $file = fopen($easyfonts_dir . '/' . $font_filename, 'w'); 171 fwrite($file, $font_data); 172 fclose($file); 173 } 174 $finalbase = get_base_url(); 175 $css = str_replace($src, "url('" . $finalbase . "/easyfonts/" . $font_filename . "')", $css); 176 } 177 } 178 } 179 } 180 } 139 $css = wp_get_remote_file($decoded_url); 140 $css = easyfonts_process_font_face_declarations($css, $easyfonts_dir); 181 141 // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory 182 142 file_put_contents($easyfonts_dir . '/' . $filename, $css); 183 143 } 184 $finalbasecss = get_base css_url();144 $finalbasecss = get_base_url(); 185 145 // Replace the @import statement with a local reference to the downloaded stylesheet 186 146 $style->innertext = str_replace($match, "" . $finalbasecss . '/easyfonts/' . $filename . "", $style->innertext); … … 249 209 $content = preg_replace('/(url\s?\(["\']?)\/\/(fonts\.gstatic\.com[^"\']+)/', '$1https://$2', $content); 250 210 $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' '); 211 if (empty($html)) { 212 return $content; 213 } 251 214 foreach ($html->find('style') as $style) { 252 215 if (preg_match_all('/url\(([^)]+)\)/', $style->innertext, $matches)) { … … 270 233 // Check if the font file already exists 271 234 if (!file_exists($easyfonts_dir . '/' . $filename)) { 272 $font_data = wp_get_remote_f ont($url);235 $font_data = wp_get_remote_file($url); 273 236 $file = fopen($easyfonts_dir . '/' . $filename, 'w'); 274 237 fwrite($file, $font_data); … … 318 281 319 282 } 320 add_action( 'template_redirect', 'easy_fonts_run_template_redirect', -999 );283 add_action( 'template_redirect', 'easy_fonts_run_template_redirect', 999 ); 321 284 322 285 -
easyfonts/trunk/readme.txt
r2961663 r2965733 6 6 Tested up to: 6.3 7 7 Requires PHP: 5.6 8 Stable tag: 1.1. 18 Stable tag: 1.1.2 9 9 License: GNU General Public License v2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 59 59 60 60 == Changelog == 61 = 1.1.2 = 62 * Fixed issue with wocoomerce checkout 61 63 = 1.1.1 = 62 64 * Improvement
Note: See TracChangeset
for help on using the changeset viewer.