Plugin Directory

Changeset 2965733


Ignore:
Timestamp:
09/12/2023 08:46:44 AM (2 years ago)
Author:
easywpstuff
Message:

update

Location:
easyfonts
Files:
10 added
2 edited

Legend:

Unmodified
Added
Removed
  • easyfonts/trunk/easyfonts.php

    r2961663 r2965733  
    44Plugin URI:
    55Description: Automatically Host existing google fonts locally on your server
    6 Version: 1.1.1
     6Version: 1.1.2
    77Author: Uzair
    88Author URI: https://easywpstuff.com
     
    2222
    2323function 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;
     24return is_ssl() ? set_url_scheme(wp_upload_dir()['baseurl'], 'https') : wp_upload_dir()['baseurl'];
     25}
     26
     27function 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
     35if (is_wp_error($response)) {
     36add_settings_error($function_name, 'http_error', $response->get_error_message(), 'error');
     37return false;
     38}
     39
     40
     41return wp_remote_retrieve_body($response);
    6642}
    6743
     
    7955  return $content;
    8056}
     57
     58function 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
    8183
    8284function easyfont_process_content_link_tag($content) {
     
    98100        $filename = substr($filename, 0, 10) . '.css';
    99101        if (!file_exists($easyfonts_dir . '/' . $filename)) {
    100             $css = wp_get_remote_css($decoded_url);
     102            $css = wp_get_remote_file($decoded_url);
    101103            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);
    124106            // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory
    125107            file_put_contents($easyfonts_dir . '/' . $filename, $css);
    126108        }
    127         $finalbasecss = get_basecss_url();
     109        $finalbasecss = get_base_url();
    128110        // Replace the href attribute with a local reference to the downloaded stylesheet
    129111        $link->href = $finalbasecss . '/easyfonts/' . $filename;
     
    155137                    // Check if the stylesheet file already exists
    156138                    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);
    181141                        // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory
    182142                        file_put_contents($easyfonts_dir . '/' . $filename, $css);
    183143                    }
    184                     $finalbasecss = get_basecss_url();
     144                    $finalbasecss = get_base_url();
    185145                    // Replace the @import statement with a local reference to the downloaded stylesheet
    186146                    $style->innertext = str_replace($match, "" . $finalbasecss . '/easyfonts/' . $filename . "", $style->innertext);
     
    249209    $content = preg_replace('/(url\s?\(["\']?)\/\/(fonts\.gstatic\.com[^"\']+)/', '$1https://$2', $content);
    250210    $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
     211    if (empty($html)) {
     212        return $content;
     213    }
    251214    foreach ($html->find('style') as $style) {
    252215        if (preg_match_all('/url\(([^)]+)\)/', $style->innertext, $matches)) {
     
    270233                    // Check if the font file already exists
    271234                    if (!file_exists($easyfonts_dir . '/' . $filename)) {
    272                         $font_data = wp_get_remote_font($url);
     235                        $font_data = wp_get_remote_file($url);
    273236                        $file = fopen($easyfonts_dir . '/' . $filename, 'w');
    274237                        fwrite($file, $font_data);
     
    318281   
    319282}
    320 add_action( 'template_redirect', 'easy_fonts_run_template_redirect', -999 );
     283add_action( 'template_redirect', 'easy_fonts_run_template_redirect', 999 );
    321284
    322285
  • easyfonts/trunk/readme.txt

    r2961663 r2965733  
    66Tested up to: 6.3
    77Requires PHP: 5.6
    8 Stable tag: 1.1.1
     8Stable tag: 1.1.2
    99License: GNU General Public License v2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    5959
    6060== Changelog ==
     61= 1.1.2 =
     62* Fixed issue with wocoomerce checkout
    6163= 1.1.1 =
    6264* Improvement
Note: See TracChangeset for help on using the changeset viewer.