# easyfonts/1.0.0/easyfonts.php

EasyFonts – Host Google Fonts Locally, Fast &amp; Auto-Optimize, GDPR Compliant, version 1.0.0. 284 lines.

- Page: https://pluginprobe.com/plugins/easyfonts/1.0.0/code/easyfonts.php
- Raw: https://pluginprobe.com/plugins/easyfonts/1.0.0/raw/easyfonts.php
- Modified: 2023-01-25T12:53:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/easyfonts/1.0.0/code/easyfonts.php#L10-L20`.

```php
<?php
/*
Plugin Name: EasyFonts
Plugin URI: 
Description: Automatically Host existing google fonts locally on your server
Version: 1.0.0
Author: Uzair
Author URI: https://easywpstuff.com
License: GPL2
*/

// If this file is called directly, abort.
if (!defined("WPINC")){
	die;
}

require_once dirname(__FILE__) . '/lib/simple_html_dom.php';

include_once dirname(__FILE__) . '/inc/options.php';

include_once dirname(__FILE__) . '/inc/notices.php';


function easyfont_process_content_link_tag($content) {
    $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
    // Check if the 'easyfonts' directory exists and is writable
    $easyfonts_dir = wp_upload_dir() ['basedir'] . '/easyfonts';
    if (!wp_mkdir_p($easyfonts_dir)) {
        return $content; 
    }
    // Find all <link> tags with a rel attribute of 'stylesheet' and a href attribute that points to a Google Fonts stylesheet
    foreach ($html->find('link[rel=stylesheet][href*=fonts.googleapis.com]') as $link) {
        $url = $link->href;
        // Generate a local filename for the stylesheet
        $filename = hash('sha256', $url, false);
        $filename = substr($filename, 0, 10) . '.css';
        if (!file_exists($easyfonts_dir . '/' . $filename)) {
            $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',));
            if (is_wp_error($response)) {
                add_settings_error('easyfont_process_content_link_tag', 'http_error', $response->get_error_message(), 'error');
                return $content;
            }
            $css = wp_remote_retrieve_body($response);
            if (!preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) continue;

            if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) {
                foreach ($matches[1] as $match) {
                    if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) {
                        foreach ($srcs[1] as $src) {
                            if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) {
                                $font_url = trim($url_match[1], "'\"");
                                $font_filename = hash('sha256', $font_url, false);
                                $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION);
                                if (!file_exists($easyfonts_dir . '/' . $font_filename)) {
                                    $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',));
                                    if (is_wp_error($font_response)) {
                                        add_settings_error('easyfont_process_content_link_tag', 'http_error', $font_response->get_error_message(), 'error');
                                        continue;
                                    }
                                    $font_data = wp_remote_retrieve_body($font_response);
                                    // Save the font file to the 'wp-content/uploads/easyfonts' directory
                                    file_put_contents($easyfonts_dir . '/' . $font_filename, $font_data);
                                }
                                // Replace the src property with a local reference to the downloaded font file
                                $css = str_replace($src, "url('" . wp_upload_dir() ['baseurl'] . "/easyfonts/" . $font_filename . "')", $css);
                            }
                        }
                    }
                }
            }
            // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory
            file_put_contents($easyfonts_dir . '/' . $filename, $css);
        }
        // Replace the href attribute with a local reference to the downloaded stylesheet
        $link->href = wp_upload_dir() ['baseurl'] . '/easyfonts/' . $filename;
    }
    // Return the modified content
    return $html->save();
}

function easyfont_process_content_import($content) {
    
    $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
    // Check if the 'easyfonts' directory exists and is writable
    $easyfonts_dir = wp_upload_dir() ['basedir'] . '/easyfonts';
    if (!wp_mkdir_p($easyfonts_dir)) {
        return $content;     
    }
    // Find all <style> tags with @import statements that import a Google Fonts stylesheet
    foreach ($html->find('style') as $style) {
        if (preg_match_all('/@import\s+(url\()?\s*([^\);]+)\s*(\))?/', $style->innertext, $matches)) {
            foreach ($matches[2] as $match) {
                if (strpos($match, 'fonts.googleapis.com') !== false) {
                    $url = trim($match, "'\"");
                    if (strpos($url, '//') === 0) {
                        $url = 'https:' . $url;
                    }
                    $filename = hash('sha256', $url, false);
                    $filename = substr($filename, 0, 10) . '.css';
                    // Check if the stylesheet file already exists
                    if (!file_exists($easyfonts_dir . '/' . $filename)) {
                        $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',));
                        if (is_wp_error($response)) {
                            add_settings_error('easyfont_process_content_import', 'http_error', $response->get_error_message(), 'error');
                            return $content;
                        }
                        $css = wp_remote_retrieve_body($response);
                        if (preg_match_all('/@font-face\s*\{([^}]+)\}/', $css, $matches)) {
                            foreach ($matches[1] as $match) {
                                if (preg_match_all('/src\s*:\s*([^;]+);/', $match, $srcs)) {
                                    foreach ($srcs[1] as $src) {
                                        if (preg_match('/url\(([^)]+)\)/', $src, $url_match)) {
                                            $font_url = trim($url_match[1], "'\"");
                                            $font_filename = hash('sha256', $font_url, false);
                                            $font_filename = substr($font_filename, 0, 10) . '.' . pathinfo($font_url, PATHINFO_EXTENSION);
                                            // Check if the font file already exists
                                            if (!file_exists($easyfonts_dir . '/' . $font_filename)) {
                                            
                                                $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',));
                                                if (is_wp_error($font_response)) {
                                                    add_settings_error('easyfont_process_content_import', 'http_error', $font_response->get_error_message(), 'error');
                                                    continue;
                                                }
                                                $font_data = wp_remote_retrieve_body($font_response);
                                                $file = fopen($easyfonts_dir . '/' . $font_filename, 'w');
                                                fwrite($file, $font_data);
                                                fclose($file);
                                            }
                                            
                                            $css = str_replace($src, "url('" . wp_upload_dir() ['baseurl'] . "/easyfonts/" . $font_filename . "')", $css);
                                        }
                                    }
                                }
                            }
                        }
                        // Save the modified stylesheet to the 'wp-content/uploads/easyfonts' directory
                        file_put_contents($easyfonts_dir . '/' . $filename, $css);
                    }
                    // Replace the @import statement with a local reference to the downloaded stylesheet
                    $style->innertext = str_replace($match, "" . wp_upload_dir() ['baseurl'] . '/easyfonts/' . $filename . "", $style->innertext);
                }
            }
        }
    }
    // Return the modified content
    return $html->save();
}
function easyfonts_remove_resource_hints($html) {
  // Load the HTML into the Simple HTML DOM library
  $dom = hgfl_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' ');
  
  // Find all <link> elements with the preload, preconnect, or prefetch attributes
  $links = $dom->find('link[rel=preload],link[rel=preconnect],link[rel=dns-prefetch]');
  
  // Loop through the <link> elements
  foreach($links as $link) {
    // Check if the <link> element has a href attribute with the fonts.googleapis.com or fonts.gstatic.com domain
    if (preg_match('/(https:\/\/|\/\/)(fonts\.googleapis\.com|fonts\.gstatic\.com)/', $link->href)) {
      // Remove the <link> element
      $link->outertext = '';
    }
  }
  
  // Find all <style> elements
  $styles = $dom->find('style');
  
  // Loop through the <style> elements
  foreach($styles as $style) {
    // Remove comments that contain the fonts.googleapis.com or fonts.gstatic.com domain
    if (strpos($style->innertext, 'fonts.googleapis.com') !== false || strpos($style->innertext, 'fonts.gstatic.com') !== false) {
        $style->innertext = preg_replace('#/\*(?:[^*]*(?:\*(?!/))*)*\*/#','',$style->innertext);
    }
  }
  // Return the modified HTML
  return $dom->save();
}


function easyfont_remove_font_scripts($html) {
  // Load the HTML string into a Simple HTML DOM object
  $dom = hgfl_str_get_html($html, false, true, 'UTF-8', false, PHP_EOL, ' ');
  
  // Find all `script` elements
  $scripts = $dom->find('script');
  foreach($scripts as $script) {
    // Check if the `script` element contains the `WebFontConfig` or `webfont.js` strings
    if(strpos($script->innertext, 'WebFontConfig') !== false || strpos($script->innertext, 'webfont.js') !== false) {
      // Remove the `script` element
      $script->outertext = '';
    }
  }
  
  // Return the modified HTML as a string
  return $dom->save();
}

function easyfont_download_gstatic_fonts($content) {
    // Create the "easyfonts" directory if it doesn't exist
    $easyfonts_dir = wp_upload_dir()['basedir'] . '/easyfonts';
    if (!wp_mkdir_p($easyfonts_dir)) {
        return $content;
    }
	$content = preg_replace('/(url\s?\(["\']?)\/\/(fonts\.gstatic\.com[^"\']+)/', '$1https://$2', $content);
    $html = hgfl_str_get_html($content, false, true, 'UTF-8', false, PHP_EOL, ' ');
    foreach ($html->find('style') as $style) {
        if (preg_match_all('/url\(([^)]+)\)/', $style->innertext, $matches)) {
            foreach ($matches[1] as $match) {
                $url = trim($match, "'\"");
                if(strpos($url,'//') === 0){
                    $url = 'https:'.$url;
                }
                if (strpos($url, 'fonts.gstatic.com') !== false) {
                    $path_parts = pathinfo($url);
                    $extension = isset($path_parts['extension'])?$path_parts['extension']:'';
                    if (empty($extension)) {
                       if(strpos($url,'/l/font') !== false){
                         $extension = 'svg';
                    }else{
                         $extension = 'woff2';
                         }
                    }
                    $filename = hash('sha256', $url, false);
                    $filename = substr($filename, 0, 10) . '.' . $extension;
                    // Check if the font file already exists
                    if (!file_exists($easyfonts_dir . '/' . $filename)) {
                        $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',));
                        if (is_wp_error($response)) {
                            add_settings_error('download_gstatic_fonts', 'http_error', $response->get_error_message(), 'error');
                            return $content;
                        }
                        $font_data = wp_remote_retrieve_body($response);
                        $file = fopen($easyfonts_dir . '/' . $filename, 'w');
                        fwrite($file, $font_data);
                        fclose($file);
                    }
                    $local_url = wp_upload_dir()['baseurl'] . '/easyfonts/' . $filename;
                    $content = str_replace($url, $local_url, $content);
                }
            }
        }
    }
    return $content;
}


function easy_fonts_run_template_redirect() {
	
	if (get_option('easyfonts_host_google_fonts_locally_link', false)) {
	ob_start( 'easyfont_process_content_link_tag',0, PHP_OUTPUT_HANDLER_REMOVABLE );
	}
	if (get_option('easyfonts_host_google_fonts_locally_import', false)) {
	ob_start( 'easyfont_process_content_import',0, PHP_OUTPUT_HANDLER_REMOVABLE );
	}
	
	if (get_option('easyfonts_remove_inline_css_fontface', false)) {
	ob_start( 'easyfont_download_gstatic_fonts',0, PHP_OUTPUT_HANDLER_REMOVABLE );
	}
	
	if (get_option('easyfonts_remove_resource_hints', false)) {
	ob_start( 'easyfonts_remove_resource_hints',0, PHP_OUTPUT_HANDLER_REMOVABLE );
	}
	
	if (get_option('easyfonts_remove_inline_script_font', false)) {
	ob_start( 'easyfont_remove_font_scripts',0, PHP_OUTPUT_HANDLER_REMOVABLE );
	}
	//ob_start( 'download_gstatic_fonts',0, PHP_OUTPUT_HANDLER_REMOVABLE );

	
}
add_action( 'template_redirect', 'easy_fonts_run_template_redirect', 999 );

function easyfonts_enqueue_options_styles() {
    if ( 'settings_page_easyfonts' == get_current_screen()->id ) {
        wp_enqueue_style( 'easyfonts-options-styles', plugin_dir_url( __FILE__ ) . 'assets/style.css', array(), '1.0.0' );
    }
}
add_action( 'admin_enqueue_scripts', 'easyfonts_enqueue_options_styles' );

function easyfonts_settings_link($links) {
    $settings_link = '<a href="options-general.php?page=easyfonts">Settings</a>';
    array_push($links, $settings_link);
    return $links;
}
add_filter("plugin_action_links_easyfonts/easyfonts.php", "easyfonts_settings_link");
```
