Version Description
[2019.01.11] = * fixed inlined css code being minified, even when minification is off * compatibility and performance improvements for the CSS merging and inlining functionality
Download this release
Release Info
Developer | Alignak |
Plugin | Fast Velocity Minify |
Version | 2.5.3 |
Comparing to | |
See all releases |
Code changes from version 2.5.2 to 2.5.3
- fvm.php +343 -696
- readme.txt +10 -5
fvm.php
CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://fastvelocity.com
|
|
5 |
Description: Improve your speed score on GTmetrix, Pingdom Tools and Google PageSpeed Insights by merging and minifying CSS and JavaScript files into groups, compressing HTML and other speed optimizations.
|
6 |
Author: Raul Peixoto
|
7 |
Author URI: http://fastvelocity.com
|
8 |
-
Version: 2.5.
|
9 |
License: GPL2
|
10 |
|
11 |
------------------------------------------------------------------------
|
@@ -93,6 +93,7 @@ $fastvelocity_min_global_js_done = array();
|
|
93 |
$fvm_collect_google_fonts = array();
|
94 |
$collect_preload_css = array();
|
95 |
$collect_preload_js = array();
|
|
|
96 |
$fvm_debug = get_option('fastvelocity_fvm_debug');
|
97 |
|
98 |
###########################################
|
@@ -115,7 +116,6 @@ $disable_css_minification = get_option('fastvelocity_min_disable_css_minificatio
|
|
115 |
$remove_print_mediatypes = get_option('fastvelocity_min_remove_print_mediatypes');
|
116 |
$skip_html_minification = get_option('fastvelocity_min_skip_html_minification');
|
117 |
$strip_htmlcomments = get_option('fastvelocity_min_strip_htmlcomments');
|
118 |
-
$skip_cssorder = get_option('fastvelocity_min_skip_cssorder');
|
119 |
$skip_google_fonts = get_option('fastvelocity_min_skip_google_fonts');
|
120 |
$skip_emoji_removal = get_option('fastvelocity_min_skip_emoji_removal');
|
121 |
$fvm_clean_header_one = get_option('fastvelocity_fvm_clean_header_one');
|
@@ -196,8 +196,11 @@ if(is_admin()) {
|
|
196 |
|
197 |
# merge, if inline is not selected
|
198 |
if($force_inline_css != true) {
|
199 |
-
add_action('wp_print_styles', 'fastvelocity_min_merge_header_css', PHP_INT_MAX );
|
200 |
-
add_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', 9.999999 );
|
|
|
|
|
|
|
201 |
} else {
|
202 |
add_filter('style_loader_tag', 'fastvelocity_optimizecss', PHP_INT_MAX, 4 );
|
203 |
add_action('wp_print_styles','fastvelocity_add_google_fonts_merged', PHP_INT_MAX);
|
@@ -228,8 +231,12 @@ if(is_admin()) {
|
|
228 |
add_action('template_redirect', 'fastvelocity_min_html_compression_start', PHP_INT_MAX);
|
229 |
}
|
230 |
|
231 |
-
# add the
|
232 |
-
|
|
|
|
|
|
|
|
|
233 |
add_action('wp_head', 'fvm_add_loadasync', 0);
|
234 |
|
235 |
# remove query from static assets and process defering (if enabled)
|
@@ -248,7 +255,7 @@ global $fvm_fix_editor, $disable_js_merge, $disable_css_merge, $skip_emoji_remov
|
|
248 |
if($fvm_fix_editor == true && is_user_logged_in()) {
|
249 |
remove_action('wp_print_scripts', 'fastvelocity_min_merge_header_scripts', PHP_INT_MAX );
|
250 |
remove_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_scripts', 9.999999 );
|
251 |
-
remove_action('wp_print_styles', '
|
252 |
remove_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', 9.999999 );
|
253 |
remove_action('init', 'fastvelocity_min_disable_wp_emojicons');
|
254 |
remove_action('template_redirect', 'fastvelocity_min_html_compression_start', PHP_INT_MAX);
|
@@ -342,7 +349,6 @@ function fastvelocity_min_register_settings() {
|
|
342 |
register_setting('fvm-group', 'fastvelocity_min_remove_print_mediatypes');
|
343 |
register_setting('fvm-group', 'fastvelocity_min_skip_html_minification');
|
344 |
register_setting('fvm-group', 'fastvelocity_min_strip_htmlcomments');
|
345 |
-
register_setting('fvm-group', 'fastvelocity_min_skip_cssorder');
|
346 |
register_setting('fvm-group', 'fastvelocity_min_skip_google_fonts');
|
347 |
register_setting('fvm-group', 'fastvelocity_min_skip_fontawesome_fonts');
|
348 |
register_setting('fvm-group', 'fastvelocity_min_skip_emoji_removal');
|
@@ -713,17 +719,13 @@ Disable CSS processing<span class="note-info">[ If selected, this plugin will ig
|
|
713 |
<input name="fastvelocity_min_disable_css_minification" type="checkbox" id="fastvelocity_min_disable_css_minification" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_css_minification'), true, false); ?>>
|
714 |
Disable minification on CSS files <span class="note-info">[ If selected, CSS files will be merged but not minified ]</span></label>
|
715 |
<br />
|
716 |
-
<label for="fastvelocity_min_skip_cssorder">
|
717 |
-
<input name="fastvelocity_min_skip_cssorder" type="checkbox" id="fastvelocity_min_skip_cssorder" value="1" <?php echo checked(1 == get_option('fastvelocity_min_skip_cssorder'), true, false); ?> >
|
718 |
-
Preserve the order of CSS files <span class="note-info">[ If selected, you will have better CSS compatibility when merging but possibly more CSS files ]</span></label>
|
719 |
-
<br />
|
720 |
<label for="fastvelocity_min_remove_print_mediatypes">
|
721 |
<input name="fastvelocity_min_remove_print_mediatypes" type="checkbox" id="fastvelocity_min_remove_print_mediatypes" value="1" <?php echo checked(1 == get_option('fastvelocity_min_remove_print_mediatypes'), true, false); ?> >
|
722 |
Remove the "Print" related stylesheets <span class="note-info">[ If selected, CSS files of mediatype "print" will be removed from the site ]</span></label>
|
723 |
<br />
|
724 |
<label for="fastvelocity_min_force_inline_css_footer">
|
725 |
<input name="fastvelocity_min_force_inline_css_footer" type="checkbox" id="fastvelocity_min_force_inline_css_footer" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_css_footer'), true, false); ?>>
|
726 |
-
Inline
|
727 |
<br />
|
728 |
<label for="fastvelocity_min_force_inline_css">
|
729 |
<input name="fastvelocity_min_force_inline_css" type="checkbox" id="fastvelocity_min_force_inline_css" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_css'), true, false); ?>>
|
@@ -1641,637 +1643,327 @@ return $tag;
|
|
1641 |
|
1642 |
|
1643 |
|
1644 |
-
###########################################
|
1645 |
-
# process header css ######################
|
1646 |
-
###########################################
|
1647 |
-
function fastvelocity_min_merge_header_css() {
|
1648 |
-
global $wp_styles, $wp_domain, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_css_merge, $disable_css_minification, $skip_google_fonts, $skip_cssorder, $remove_print_mediatypes, $force_inline_googlefonts, $css_hide_googlefonts, $min_async_googlefonts, $remove_googlefonts, $fvmloadcss, $fvm_remove_css, $fvmualist, $fvm_min_excludecsslist, $fvm_debug, $fvm_min_excludecsslist, $fvm_fawesome_method;
|
1649 |
-
if(!is_object($wp_styles)) { return false; }
|
1650 |
-
$ctime = get_option('fvm-last-cache-update', '0');
|
1651 |
-
$styles = wp_clone($wp_styles);
|
1652 |
-
$styles->all_deps($styles->queue);
|
1653 |
-
$done = $styles->done;
|
1654 |
-
$header = array();
|
1655 |
-
$google_fonts = array();
|
1656 |
-
$process = array();
|
1657 |
-
$inline_css = array();
|
1658 |
-
|
1659 |
-
# dequeue all styles
|
1660 |
-
if($fvm_remove_css != false) {
|
1661 |
-
foreach( $styles->to_do as $handle ) :
|
1662 |
-
$done = array_merge($done, array($handle));
|
1663 |
-
endforeach;
|
1664 |
-
|
1665 |
-
# remove from queue
|
1666 |
-
$wp_styles->done = $done;
|
1667 |
-
return false;
|
1668 |
-
}
|
1669 |
|
1670 |
-
# add defaults to ignore list
|
1671 |
-
$ignore = fastvelocity_default_ignore($ignore);
|
1672 |
|
1673 |
-
# get list of handles to process, dequeue duplicate css urls and keep empty source handles (for dependencies)
|
1674 |
-
$uniq = array(); $gfonts = array();
|
1675 |
-
foreach( $styles->to_do as $handle):
|
1676 |
|
1677 |
-
# conditionals
|
1678 |
-
$conditional = NULL; if(isset($wp_styles->registered[$handle]->extra["conditional"])) {
|
1679 |
-
$conditional = $wp_styles->registered[$handle]->extra["conditional"]; # such as ie7, ie8, ie9, etc
|
1680 |
-
}
|
1681 |
-
|
1682 |
-
# mediatype
|
1683 |
-
$mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all';
|
1684 |
-
if ($mt == 'screen' || $mt == 'screen, print' || empty($mt) || is_null($mt) || $mt == false) { $mt = 'all'; }
|
1685 |
-
$mediatype = $mt;
|
1686 |
-
|
1687 |
-
# full url or empty
|
1688 |
-
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $wp_home);
|
1689 |
-
|
1690 |
-
# mark duplicates as done and remove from the queue
|
1691 |
-
if(!empty($hurl)) {
|
1692 |
-
$key = hash('adler32', $hurl);
|
1693 |
-
if (isset($uniq[$key])) { $done = array_merge($done, array($handle)); continue; } else { $uniq[$key] = $handle; }
|
1694 |
-
}
|
1695 |
-
|
1696 |
-
# Exclude specific CSS files from PSI?
|
1697 |
-
if($fvm_min_excludecsslist != false && is_array($fvm_min_excludecsslist) && fastvelocity_min_in_arrayi($hurl, $fvm_min_excludecsslist)) {
|
1698 |
-
$cssguid = 'fvm'.hash('adler32', $hurl);
|
1699 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1700 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$hurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$mediatype.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1701 |
-
echo '}</script>';
|
1702 |
-
$done = array_merge($done, array($handle)); continue;
|
1703 |
-
}
|
1704 |
-
|
1705 |
-
# font awesome processing, async css
|
1706 |
-
if($fvm_fawesome_method == 2 && stripos($hurl, 'font-awesome') !== false) {
|
1707 |
-
echo '<link rel="preload" href="'.$hurl.'" as="style" media="'.$mediatype.'" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
1708 |
-
echo '<noscript><link rel="stylesheet" href="'.$hurl.'" media="'.$mediatype.'" /></noscript>';
|
1709 |
-
echo '<!--[if IE]><link rel="stylesheet" href="'.$hurl.'" media="'.$mediatype.'" /><![endif]-->';
|
1710 |
-
$done = array_merge($done, array($handle)); continue;
|
1711 |
-
}
|
1712 |
-
|
1713 |
-
# font awesome processing, async and exclude from PSI
|
1714 |
-
if($fvm_fawesome_method == 3 && stripos($hurl, 'font-awesome') !== false) {
|
1715 |
-
$cssguid = 'fvm'.hash('adler32', $hurl);
|
1716 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1717 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$hurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$mediatype.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1718 |
-
echo '}</script>';
|
1719 |
-
$done = array_merge($done, array($handle)); continue;
|
1720 |
-
}
|
1721 |
-
|
1722 |
-
# array of info to save
|
1723 |
-
$arr = array('handle'=>$handle, 'url'=>$hurl, 'conditional'=>$conditional, 'mediatype'=>$mediatype);
|
1724 |
-
|
1725 |
-
# google fonts to the top (collect and skip process array)
|
1726 |
-
if (stripos($hurl, 'fonts.googleapis.com') !== false) {
|
1727 |
-
if($remove_googlefonts != false) { $done = array_merge($done, array($handle)); continue; } # mark as done if to be removed
|
1728 |
-
if($skip_google_fonts != true || $force_inline_googlefonts != false) {
|
1729 |
-
$google_fonts[$handle] = $hurl;
|
1730 |
|
1731 |
-
} else {
|
1732 |
-
wp_enqueue_style($handle); # skip google fonts optimization?
|
1733 |
-
}
|
1734 |
-
continue;
|
1735 |
-
}
|
1736 |
-
|
1737 |
-
# all else
|
1738 |
-
$process[$handle] = $arr;
|
1739 |
|
1740 |
-
endforeach;
|
1741 |
|
1742 |
|
1743 |
-
# concat google fonts, if enabled
|
1744 |
-
if(!$skip_google_fonts && count($google_fonts) > 0 || ($force_inline_googlefonts != false && count($google_fonts) > 0)) {
|
1745 |
-
foreach ($google_fonts as $h=>$a) { $done = array_merge($done, array($h)); } # mark as done
|
1746 |
-
|
1747 |
-
# merge google fonts if force inlining is enabled?
|
1748 |
-
$nfonts = array();
|
1749 |
-
if($skip_google_fonts != true) {
|
1750 |
-
$nfonts[] = fvm_get_protocol(fastvelocity_min_concatenate_google_fonts($google_fonts));
|
1751 |
-
} else {
|
1752 |
-
foreach ($google_fonts as $a) { if(!empty($a)) { $nfonts[] = $a; } }
|
1753 |
-
}
|
1754 |
-
|
1755 |
-
# foreach google font (will be one if merged is not disabled)
|
1756 |
-
if(count($nfonts) > 0) {
|
1757 |
-
foreach($nfonts as $gfurl) {
|
1758 |
-
|
1759 |
-
# hide from PSI, async, inline, or default
|
1760 |
-
if($css_hide_googlefonts == true) {
|
1761 |
-
|
1762 |
-
# make a stylesheet, hide from PSI
|
1763 |
-
$cssguid = 'fvm'.hash('adler32', $gfurl);
|
1764 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1765 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$gfurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="all"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1766 |
-
echo '}</script>';
|
1767 |
-
|
1768 |
-
# async CSS
|
1769 |
-
} elseif ($min_async_googlefonts == true) {
|
1770 |
-
echo '<link rel="preload" href="'.$gfurl.'" as="style" media="all" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
1771 |
-
echo '<noscript><link rel="stylesheet" href="'.$gfurl.'" media="all" /></noscript>';
|
1772 |
-
echo '<!--[if IE]><link rel="stylesheet" href="'.$gfurl.'" media="all" /><![endif]-->';
|
1773 |
-
|
1774 |
-
# inline css
|
1775 |
-
} elseif($force_inline_googlefonts == true) {
|
1776 |
-
|
1777 |
-
# download, minify, cache
|
1778 |
-
$tkey = 'css-'.$ctime.'-'.hash('adler32', $gfurl).'.css';
|
1779 |
-
$json = false; $json = fvm_get_transient($tkey);
|
1780 |
-
if ( $json === false) {
|
1781 |
-
$json = fvm_download_and_minify($gfurl, null, $disable_css_minification, 'css', null);
|
1782 |
-
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Uncached file processing now for $gfurl -->" . PHP_EOL; }
|
1783 |
-
fvm_set_transient($tkey, $json);
|
1784 |
-
}
|
1785 |
-
|
1786 |
-
# decode
|
1787 |
-
$res = json_decode($json, true);
|
1788 |
-
|
1789 |
-
# response has failed
|
1790 |
-
if($res['status'] != true) {
|
1791 |
-
$log.= $res['log'];
|
1792 |
-
continue;
|
1793 |
-
}
|
1794 |
-
|
1795 |
-
# inline css or fail
|
1796 |
-
if($res['code'] !== false) {
|
1797 |
-
|
1798 |
-
# add font-display
|
1799 |
-
# https://developers.google.com/web/updates/2016/02/font-display
|
1800 |
-
$res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']);
|
1801 |
-
|
1802 |
-
# inline
|
1803 |
-
echo '<style type="text/css" media="all">'.$res['code'].'</style>'.PHP_EOL;
|
1804 |
-
} else {
|
1805 |
-
echo "<!-- GOOGLE FONTS REQUEST FAILED for $gfurl -->\n";
|
1806 |
-
}
|
1807 |
-
|
1808 |
-
# fallback, enqueue google fonts
|
1809 |
-
} else {
|
1810 |
-
wp_enqueue_style('header-fvm-fonts', $gfurl, array(), null, 'all');
|
1811 |
-
}
|
1812 |
-
|
1813 |
-
}
|
1814 |
-
}
|
1815 |
-
}
|
1816 |
|
1817 |
|
1818 |
-
# get groups of handles
|
1819 |
-
foreach( $styles->to_do as $handle ) :
|
1820 |
|
1821 |
-
# skip already processed google fonts and empty dependencies
|
1822 |
-
if(isset($google_fonts[$handle])) { continue; } # skip google fonts
|
1823 |
-
if(empty($wp_styles->registered[$handle]->src)) { continue; } # skip empty src
|
1824 |
-
if (fastvelocity_min_in_arrayi($handle, $done)) { continue; } # skip if marked as done before
|
1825 |
-
if (!isset($process[$handle])) { continue; } # skip if not on our unique process list
|
1826 |
|
1827 |
-
# get full url
|
1828 |
-
$hurl = $process[$handle]['url'];
|
1829 |
-
$conditional = $process[$handle]['conditional'];
|
1830 |
-
$mediatype = $process[$handle]['mediatype'];
|
1831 |
|
1832 |
-
# IE only files don't increment things
|
1833 |
-
$ieonly = fastvelocity_ie_blacklist($hurl);
|
1834 |
-
if($ieonly == true) { continue; }
|
1835 |
-
|
1836 |
-
# skip ignore list, conditional css, external css, font-awesome merge
|
1837 |
-
if ( (!fastvelocity_min_in_arrayi($hurl, $ignore) && !isset($conditional) && fvm_internal_url($hurl, $wp_home))
|
1838 |
-
|| empty($hurl)
|
1839 |
-
|| ($fvm_fawesome_method == 1 && stripos($hurl, 'font-awesome') !== false)) {
|
1840 |
-
|
1841 |
-
# colect inline css for this handle
|
1842 |
-
if(isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) {
|
1843 |
-
$inline_css[$handle] = fastvelocity_min_minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); # save
|
1844 |
-
$wp_styles->registered[$handle]->extra['after'] = null; # dequeue
|
1845 |
-
}
|
1846 |
-
|
1847 |
-
# process
|
1848 |
-
if(isset($header[count($header)-1]['handle']) || count($header) == 0 || $header[count($header)-1]['media'] != $mediatype) {
|
1849 |
-
array_push($header, array('handles'=>array(), 'media'=>$mediatype));
|
1850 |
-
}
|
1851 |
-
|
1852 |
-
# push it to the array
|
1853 |
-
array_push($header[count($header)-1]['handles'], $handle);
|
1854 |
|
1855 |
-
# external and ignored css
|
1856 |
-
} else {
|
1857 |
-
|
1858 |
-
# normal enqueuing
|
1859 |
-
array_push($header, array('handle'=>$handle));
|
1860 |
-
}
|
1861 |
-
endforeach;
|
1862 |
|
1863 |
-
# reorder CSS by mediatypes
|
1864 |
-
if(!$skip_cssorder) {
|
1865 |
-
if(count($header) > 0) {
|
1866 |
|
1867 |
-
|
1868 |
-
|
1869 |
-
|
1870 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1871 |
}
|
1872 |
|
1873 |
-
#
|
1874 |
-
$
|
1875 |
-
|
1876 |
-
foreach($header as $array) {
|
1877 |
-
if (isset($array['media']) && $array['media'] === $md) {
|
1878 |
-
foreach($array['handles'] as $h) { $grouphandles[$md][] = $h; }
|
1879 |
-
}
|
1880 |
-
}
|
1881 |
}
|
1882 |
-
|
1883 |
-
# reset and reorder header by mediatypes
|
1884 |
-
$newheader = array();
|
1885 |
-
foreach ($allmedia as $md=>$var) { $newheader[] = array('handles' => $grouphandles[$md], 'media'=>$md); }
|
1886 |
-
if(count($newheader) > 0) { $header = $newheader; }
|
1887 |
-
}
|
1888 |
-
}
|
1889 |
-
|
1890 |
-
# loop through header css and merge
|
1891 |
-
for($i=0,$l=count($header);$i<$l;$i++) {
|
1892 |
-
if(!isset($header[$i]['handle'])) {
|
1893 |
|
1894 |
-
#
|
1895 |
-
$
|
1896 |
-
|
1897 |
-
|
1898 |
|
1899 |
-
#
|
1900 |
-
$
|
1901 |
-
$
|
1902 |
-
|
1903 |
-
|
1904 |
-
$file = $cachedir.'/'.$hash.'-'.$ctime.'.min.css';
|
1905 |
-
$file_url = fvm_get_protocol($cachedirurl.'/'.$hash.'-'.$ctime.'.min.css');
|
1906 |
|
1907 |
-
#
|
1908 |
-
|
1909 |
-
if
|
1910 |
-
|
1911 |
-
|
1912 |
-
$log = '';
|
1913 |
-
$code = '';
|
1914 |
|
1915 |
-
|
1916 |
-
|
1917 |
-
|
1918 |
-
|
1919 |
-
|
1920 |
-
|
1921 |
-
|
1922 |
-
|
1923 |
-
|
1924 |
-
|
1925 |
-
|
1926 |
-
|
1927 |
-
|
1928 |
-
|
1929 |
-
|
1930 |
-
|
1931 |
-
|
1932 |
-
|
1933 |
-
|
1934 |
-
|
1935 |
-
|
1936 |
-
|
1937 |
-
$log.= $res['log'];
|
1938 |
-
continue;
|
1939 |
-
}
|
1940 |
-
|
1941 |
-
# append code to merged file
|
1942 |
-
$code.= $res['code'];
|
1943 |
-
$log.= $res['log'];
|
1944 |
-
|
1945 |
-
# append inlined styles
|
1946 |
-
if(isset($inline_css[$handle]) && !empty($inline_css[$handle])) {
|
1947 |
-
$code.= $inline_css[$handle];
|
1948 |
-
}
|
1949 |
-
|
1950 |
-
# consider dependencies on handles with an empty src
|
1951 |
-
} else {
|
1952 |
-
wp_dequeue_script($handle);
|
1953 |
-
wp_enqueue_script($handle);
|
1954 |
-
}
|
1955 |
-
endforeach;
|
1956 |
|
1957 |
-
#
|
1958 |
-
$
|
|
|
|
|
|
|
|
|
|
|
|
|
1959 |
|
1960 |
-
#
|
1961 |
-
if(
|
1962 |
-
|
1963 |
-
|
1964 |
-
|
1965 |
-
|
1966 |
-
# permissions
|
1967 |
-
fastvelocity_fix_permission_bits($file.'.txt');
|
1968 |
-
fastvelocity_fix_permission_bits($file);
|
1969 |
-
fastvelocity_fix_permission_bits($file.'.gz');
|
1970 |
-
|
1971 |
-
# brotli static support
|
1972 |
-
if(function_exists('brotli_compress')) {
|
1973 |
-
file_put_contents($file.'.br', brotli_compress(file_get_contents($file), 9));
|
1974 |
-
fastvelocity_fix_permission_bits($file.'.br');
|
1975 |
-
}
|
1976 |
}
|
1977 |
}
|
1978 |
|
1979 |
-
#
|
1980 |
-
if
|
1981 |
-
|
1982 |
-
|
1983 |
-
|
1984 |
-
|
1985 |
-
|
1986 |
-
|
1987 |
-
|
1988 |
-
|
1989 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1990 |
|
1991 |
-
#
|
1992 |
-
|
1993 |
-
|
1994 |
-
|
1995 |
-
|
1996 |
-
|
1997 |
-
|
1998 |
-
|
1999 |
-
|
2000 |
-
|
2001 |
-
|
2002 |
-
|
2003 |
-
|
2004 |
-
|
2005 |
-
|
2006 |
-
|
2007 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2008 |
}
|
2009 |
}
|
2010 |
-
|
2011 |
-
|
2012 |
-
|
2013 |
-
wp_dequeue_style($header[$i]['handle']);
|
2014 |
-
wp_enqueue_style($header[$i]['handle']);
|
2015 |
-
}
|
2016 |
-
}
|
2017 |
-
|
2018 |
-
# remove from queue
|
2019 |
-
$wp_styles->done = $done;
|
2020 |
-
|
2021 |
-
}
|
2022 |
-
###########################################
|
2023 |
-
|
2024 |
-
|
2025 |
-
###########################################
|
2026 |
-
# process css in the footer ###############
|
2027 |
-
###########################################
|
2028 |
-
function fastvelocity_min_merge_footer_css() {
|
2029 |
-
global $wp_styles, $wp_domain, $wp_home, $wp_home_path, $cachedir, $cachedirurl, $ignore, $disable_css_merge, $disable_css_minification, $skip_google_fonts, $skip_cssorder, $remove_print_mediatypes, $force_inline_googlefonts, $css_hide_googlefonts, $min_async_googlefonts, $remove_googlefonts, $fvmloadcss, $fvm_remove_css, $fvmualist, $fvm_debug, $fvm_fawesome_method, $fvm_min_excludecsslist, $force_inline_css_footer;
|
2030 |
-
|
2031 |
-
if(!is_object($wp_styles)) { return false; }
|
2032 |
-
$ctime = get_option('fvm-last-cache-update', '0');
|
2033 |
-
$styles = wp_clone($wp_styles);
|
2034 |
-
$styles->all_deps($styles->queue);
|
2035 |
-
$done = $styles->done;
|
2036 |
-
$footer = array();
|
2037 |
-
$google_fonts = array();
|
2038 |
-
$inline_css = array();
|
2039 |
-
|
2040 |
-
# dequeue all styles
|
2041 |
-
if($fvm_remove_css != false) {
|
2042 |
-
foreach( $styles->to_do as $handle ) :
|
2043 |
-
$done = array_merge($done, array($handle));
|
2044 |
-
endforeach;
|
2045 |
-
|
2046 |
-
# remove from queue
|
2047 |
-
$wp_styles->done = $done;
|
2048 |
-
return false;
|
2049 |
-
}
|
2050 |
-
|
2051 |
-
|
2052 |
-
# add defaults to ignore list
|
2053 |
-
$ignore = fastvelocity_default_ignore($ignore);
|
2054 |
-
|
2055 |
-
# google fonts to the top
|
2056 |
-
foreach( $styles->to_do as $handle ) :
|
2057 |
-
|
2058 |
-
# dequeue and get a list of google fonts, or requeue external
|
2059 |
-
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $wp_home);
|
2060 |
-
|
2061 |
-
if (stripos($hurl, 'fonts.googleapis.com') !== false) {
|
2062 |
-
wp_dequeue_style($handle);
|
2063 |
-
if($remove_googlefonts != false) { $done = array_merge($done, array($handle)); continue; } # mark as done if to be removed
|
2064 |
-
if($skip_google_fonts != true || $force_inline_googlefonts != false) {
|
2065 |
-
$google_fonts[$handle] = $hurl;
|
2066 |
-
} else {
|
2067 |
-
wp_enqueue_style($handle); # skip google fonts optimization?
|
2068 |
-
}
|
2069 |
-
} else {
|
2070 |
-
wp_dequeue_style($handle); wp_enqueue_style($handle); # failsafe
|
2071 |
-
}
|
2072 |
-
|
2073 |
-
endforeach;
|
2074 |
-
|
2075 |
-
|
2076 |
-
# concat google fonts, if enabled
|
2077 |
-
if(!$skip_google_fonts && count($google_fonts) > 0 || ($force_inline_googlefonts != false && count($google_fonts) > 0)) {
|
2078 |
-
foreach ($google_fonts as $h=>$a) { $done = array_merge($done, array($h)); } # mark as done
|
2079 |
-
|
2080 |
-
# merge google fonts if force inlining is enabled?
|
2081 |
-
$nfonts = array();
|
2082 |
-
if($skip_google_fonts != true) {
|
2083 |
-
$nfonts[] = fvm_get_protocol(fastvelocity_min_concatenate_google_fonts($google_fonts));
|
2084 |
-
} else {
|
2085 |
-
foreach ($google_fonts as $a) { if(!empty($a)) { $nfonts[] = $a; } }
|
2086 |
-
}
|
2087 |
-
|
2088 |
-
# foreach google font (will be one if merged is not disabled)
|
2089 |
-
if(count($nfonts) > 0) {
|
2090 |
-
foreach($nfonts as $gfurl) {
|
2091 |
-
|
2092 |
-
# hide from PSI, async, inline, or default
|
2093 |
-
if($css_hide_googlefonts == true) {
|
2094 |
-
|
2095 |
-
# make a stylesheet, hide from PSI
|
2096 |
-
$cssguid = 'fvm'.hash('adler32', $gfurl);
|
2097 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
2098 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$gfurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="all"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
2099 |
-
echo '}</script>';
|
2100 |
-
|
2101 |
-
# async CSS
|
2102 |
-
} elseif ($min_async_googlefonts == true) {
|
2103 |
-
echo '<link rel="preload" href="'.$gfurl.'" as="style" media="all" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
2104 |
-
echo '<noscript><link rel="stylesheet" href="'.$gfurl.'" media="all" /></noscript>';
|
2105 |
-
echo '<!--[if IE]><link rel="stylesheet" href="'.$gfurl.'" media="all" /><![endif]-->';
|
2106 |
|
2107 |
-
#
|
2108 |
-
|
2109 |
-
|
2110 |
-
|
2111 |
-
$
|
2112 |
-
$
|
2113 |
-
|
2114 |
-
|
2115 |
-
|
2116 |
-
|
2117 |
-
|
2118 |
-
|
2119 |
-
|
2120 |
-
|
2121 |
-
|
2122 |
-
|
2123 |
-
|
2124 |
-
|
2125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2126 |
}
|
2127 |
|
2128 |
-
#
|
2129 |
-
$
|
2130 |
-
|
2131 |
-
|
2132 |
-
|
2133 |
-
|
2134 |
-
|
2135 |
-
|
2136 |
-
|
|
|
|
|
2137 |
}
|
2138 |
-
|
2139 |
-
# fallback, enqueue google fonts
|
2140 |
-
} else {
|
2141 |
-
wp_enqueue_style('footer-fvm-fonts', $gfurl, array(), null, 'all');
|
2142 |
}
|
2143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
2144 |
}
|
2145 |
-
|
|
|
|
|
|
|
2146 |
}
|
2147 |
|
2148 |
|
2149 |
-
#
|
2150 |
-
|
2151 |
-
|
2152 |
-
|
2153 |
-
|
2154 |
-
if(isset($google_fonts[$handle])) { continue; }
|
2155 |
|
2156 |
-
#
|
2157 |
-
$
|
2158 |
-
|
2159 |
-
}
|
2160 |
-
|
2161 |
-
# mediatype
|
2162 |
-
$mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all';
|
2163 |
-
if ($mt == 'screen' || $mt == 'screen, print' || empty($mt) || is_null($mt) || $mt == false) { $mt = 'all'; }
|
2164 |
-
$mediatype = $mt;
|
2165 |
-
|
2166 |
-
# get full url
|
2167 |
-
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $wp_home);
|
2168 |
-
|
2169 |
-
# mark duplicates as done and remove from the queue
|
2170 |
-
if(!empty($hurl)) {
|
2171 |
-
$key = hash('adler32', $hurl);
|
2172 |
-
if (isset($uniq[$key])) { $done = array_merge($done, array($handle)); continue; } else { $uniq[$key] = $handle; }
|
2173 |
-
}
|
2174 |
-
|
2175 |
-
# IE only files don't increment things
|
2176 |
-
$ieonly = fastvelocity_ie_blacklist($hurl);
|
2177 |
-
if($ieonly == true) { continue; }
|
2178 |
-
|
2179 |
-
|
2180 |
-
# Exclude specific CSS files from PSI?
|
2181 |
-
if($fvm_min_excludecsslist != false && is_array($fvm_min_excludecsslist) && fastvelocity_min_in_arrayi($hurl, $fvm_min_excludecsslist)) {
|
2182 |
-
$cssguid = 'fvm'.hash('adler32', $hurl);
|
2183 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
2184 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$hurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$mediatype.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
2185 |
-
echo '}</script>';
|
2186 |
-
$done = array_merge($done, array($handle)); continue;
|
2187 |
}
|
2188 |
|
2189 |
-
#
|
2190 |
-
|
2191 |
-
echo '<link rel="preload" href="'.$hurl.'" as="style" media="'.$mediatype.'" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
2192 |
-
echo '<noscript><link rel="stylesheet" href="'.$hurl.'" media="'.$mediatype.'" /></noscript>';
|
2193 |
-
echo '<!--[if IE]><link rel="stylesheet" href="'.$hurl.'" media="'.$mediatype.'" /><![endif]-->';
|
2194 |
-
$done = array_merge($done, array($handle)); continue;
|
2195 |
-
}
|
2196 |
|
2197 |
-
#
|
2198 |
-
|
2199 |
-
$cssguid = 'fvm'.hash('adler32', $hurl);
|
2200 |
-
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
2201 |
-
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$hurl.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$mediatype.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
2202 |
-
echo '}</script>';
|
2203 |
-
$done = array_merge($done, array($handle)); continue;
|
2204 |
-
}
|
2205 |
|
2206 |
-
#
|
2207 |
-
|
2208 |
-
|
2209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
2210 |
|
2211 |
-
|
2212 |
-
|
2213 |
-
|
2214 |
-
|
2215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2216 |
|
2217 |
-
# process
|
2218 |
-
if(isset($footer[count($footer)-1]['handle']) || count($footer) == 0 || $footer[count($footer)-1]['media'] != $wp_styles->registered[$handle]->args) {
|
2219 |
-
array_push($footer, array('handles'=>array(),'media'=>$mediatype));
|
2220 |
}
|
2221 |
-
|
2222 |
-
# push it to the array get latest modified time
|
2223 |
-
array_push($footer[count($footer)-1]['handles'], $handle);
|
2224 |
|
2225 |
-
|
2226 |
-
|
2227 |
-
|
2228 |
-
|
2229 |
-
|
2230 |
-
|
2231 |
-
|
2232 |
-
|
2233 |
-
|
2234 |
-
# reorder CSS by mediatypes
|
2235 |
-
if(!$skip_cssorder) {
|
2236 |
-
if(count($footer) > 0) {
|
2237 |
-
|
2238 |
-
# get unique mediatypes
|
2239 |
-
$allmedia = array();
|
2240 |
-
foreach($footer as $key=>$array) {
|
2241 |
-
if(isset($array['media'])) { $allmedia[$array['media']] = ''; }
|
2242 |
-
}
|
2243 |
-
|
2244 |
-
# extract handles by mediatype
|
2245 |
-
$grouphandles = array();
|
2246 |
-
foreach ($allmedia as $md=>$var) {
|
2247 |
-
foreach($footer as $array) {
|
2248 |
-
if (isset($array['media']) && $array['media'] === $md) {
|
2249 |
-
foreach($array['handles'] as $h) { $grouphandles[$md][] = $h; }
|
2250 |
-
}
|
2251 |
-
}
|
2252 |
}
|
2253 |
-
|
2254 |
-
# reset and reorder footer by mediatypes
|
2255 |
-
$newfooter = array();
|
2256 |
-
foreach ($allmedia as $md=>$var) { $newfooter[] = array('handles' => $grouphandles[$md], 'media'=>$md); }
|
2257 |
-
if(count($newfooter) > 0) { $footer = $newfooter; }
|
2258 |
-
}
|
2259 |
-
}
|
2260 |
-
|
2261 |
-
# loop through footer css and merge
|
2262 |
-
for($i=0,$l=count($footer);$i<$l;$i++) {
|
2263 |
-
if(!isset($footer[$i]['handle'])) {
|
2264 |
|
2265 |
-
# get has for the inline css in this group
|
2266 |
-
$inline_css_group = array();
|
2267 |
-
foreach($footer[$i]['handles'] as $h) { if(isset($inline_css[$h]) && !empty($inline_css[$h])) { $inline_css_group[] = $inline_css[$h]; } }
|
2268 |
-
$inline_css_hash = md5(implode('',$inline_css_group));
|
2269 |
-
|
2270 |
-
# static cache file info + done
|
2271 |
-
$done = array_merge($done, $footer[$i]['handles']);
|
2272 |
-
$hash = 'footer-'.hash('adler32',implode('',$footer[$i]['handles']).$inline_css_hash);
|
2273 |
-
|
2274 |
# create cache files and urls
|
|
|
2275 |
$file = $cachedir.'/'.$hash.'-'.$ctime.'.min.css';
|
2276 |
$file_url = fvm_get_protocol($cachedirurl.'/'.$hash.'-'.$ctime.'.min.css');
|
2277 |
|
@@ -2279,59 +1971,13 @@ for($i=0,$l=count($footer);$i<$l;$i++) {
|
|
2279 |
clearstatcache();
|
2280 |
if (!file_exists($file)) {
|
2281 |
|
2282 |
-
# code and log initialization
|
2283 |
-
$log = '';
|
2284 |
-
$code = '';
|
2285 |
-
|
2286 |
-
# minify and write to file
|
2287 |
-
foreach($footer[$i]['handles'] as $handle) :
|
2288 |
-
if(!empty($wp_styles->registered[$handle]->src)) {
|
2289 |
-
|
2290 |
-
# get hurl per handle
|
2291 |
-
$hurl = fastvelocity_min_get_hurl($wp_styles->registered[$handle]->src, $wp_domain, $wp_home);
|
2292 |
-
$printurl = str_ireplace(array(site_url(), home_url(), 'http:', 'https:'), '', $hurl);
|
2293 |
-
|
2294 |
-
# download, minify, cache
|
2295 |
-
$tkey = 'css-'.$ctime.'-'.hash('adler32', $handle.$hurl).'.css';
|
2296 |
-
$json = false; $json = fvm_get_transient($tkey);
|
2297 |
-
if ( $json === false) {
|
2298 |
-
$json = fvm_download_and_minify($hurl, null, $disable_css_minification, 'css', $handle);
|
2299 |
-
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Uncached file processing now for $handle / $hurl -->" . PHP_EOL; }
|
2300 |
-
fvm_set_transient($tkey, $json);
|
2301 |
-
}
|
2302 |
-
|
2303 |
-
# decode
|
2304 |
-
$res = json_decode($json, true);
|
2305 |
-
|
2306 |
-
# response has failed
|
2307 |
-
if($res['status'] != true) {
|
2308 |
-
$log.= $res['log'];
|
2309 |
-
continue;
|
2310 |
-
}
|
2311 |
-
|
2312 |
-
# append code to merged file
|
2313 |
-
$code.= $res['code'];
|
2314 |
-
$log.= $res['log'];
|
2315 |
-
|
2316 |
-
# append inlined styles
|
2317 |
-
if(isset($inline_css[$handle]) && !empty($inline_css[$handle])) {
|
2318 |
-
$code.= $inline_css[$handle];
|
2319 |
-
}
|
2320 |
-
|
2321 |
-
# consider dependencies on handles with an empty src
|
2322 |
-
} else {
|
2323 |
-
wp_dequeue_script($handle);
|
2324 |
-
wp_enqueue_script($handle);
|
2325 |
-
}
|
2326 |
-
endforeach;
|
2327 |
-
|
2328 |
# prepare log
|
2329 |
-
$log = "PROCESSED on ".date('r').PHP_EOL
|
2330 |
|
2331 |
-
# generate cache,
|
2332 |
if(!empty($code)) {
|
2333 |
file_put_contents($file.'.txt', $log);
|
2334 |
-
file_put_contents($file, $code);
|
2335 |
file_put_contents($file.'.gz', gzencode(file_get_contents($file), 9));
|
2336 |
|
2337 |
# permissions
|
@@ -2346,53 +1992,64 @@ for($i=0,$l=count($footer);$i<$l;$i++) {
|
|
2346 |
}
|
2347 |
}
|
2348 |
}
|
2349 |
-
|
2350 |
-
#
|
2351 |
-
|
2352 |
-
|
2353 |
-
|
2354 |
-
|
2355 |
-
|
2356 |
-
|
2357 |
-
|
2358 |
-
|
2359 |
-
|
2360 |
-
|
2361 |
-
|
2362 |
-
|
2363 |
-
|
2364 |
-
if(file_exists($file) && filesize($file) > 0) {
|
2365 |
-
|
2366 |
-
# inline if the file is smaller than 20KB or option has been enabled
|
2367 |
-
if(filesize($file) < 20000 || $force_inline_css_footer != false) {
|
2368 |
-
echo '<style id="fvm-footer-'.$i.'" media="'.$footer[$i]['media'].'">'.file_get_contents($file).'</style>';
|
2369 |
-
} else {
|
2370 |
-
# enqueue it
|
2371 |
-
wp_enqueue_style("fvm-footer-$i", $file_url, array(), null, $footer[$i]['media']);
|
2372 |
-
}
|
2373 |
} else {
|
2374 |
-
|
2375 |
-
|
2376 |
-
echo "
|
2377 |
-
|
|
|
|
|
|
|
|
|
2378 |
}
|
|
|
|
|
|
|
|
|
|
|
2379 |
}
|
2380 |
}
|
2381 |
-
|
2382 |
-
|
2383 |
-
|
2384 |
-
wp_dequeue_style($footer[$i]['handle']);
|
2385 |
-
wp_enqueue_style($footer[$i]['handle']);
|
2386 |
}
|
|
|
|
|
|
|
|
|
|
|
2387 |
}
|
2388 |
|
2389 |
-
|
2390 |
-
|
2391 |
-
}
|
2392 |
###########################################
|
2393 |
|
2394 |
|
2395 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2396 |
###########################################
|
2397 |
# defer CSS globally from the header (order matters)
|
2398 |
# dev: https://www.filamentgroup.com/lab/async-css.html
|
@@ -2543,6 +2200,11 @@ return $tag;
|
|
2543 |
}
|
2544 |
|
2545 |
|
|
|
|
|
|
|
|
|
|
|
2546 |
# inline css in place, instead of inlining the large file
|
2547 |
function fastvelocity_optimizecss($html, $handle, $href, $media){
|
2548 |
global $fvm_debug, $wp_domain, $wp_home, $force_inline_css, $fvmualist, $fvm_collect_google_fonts, $force_inline_googlefonts, $min_async_googlefonts, $remove_googlefonts, $skip_google_fonts, $css_hide_googlefonts, $remove_print_mediatypes, $ignore, $blacklist, $ignorelist, $wp_home, $fvmloadcss, $fvm_remove_css, $fvm_cdn_url, $disable_minification, $fvm_min_excludecsslist, $disable_css_minification, $fvm_fix_editor, $fvm_fawesome_method;
|
@@ -2729,6 +2391,7 @@ function fastvelocity_optimizecss($html, $handle, $href, $media){
|
|
2729 |
# get inline_styles for this handle, minify and print
|
2730 |
$inline_styles = array();
|
2731 |
$inline_styles = wp_styles()->get_data( $handle, 'after' );
|
|
|
2732 |
if($inline_styles != false) {
|
2733 |
|
2734 |
# string type
|
@@ -2813,23 +2476,7 @@ function fastvelocity_add_google_fonts_merged() {
|
|
2813 |
|
2814 |
|
2815 |
|
2816 |
-
# collect all fvm
|
2817 |
-
add_filter('style_loader_tag', 'fastvelocity_collect_css_preload_headers', PHP_INT_MAX, 4 );
|
2818 |
-
function fastvelocity_collect_css_preload_headers($html, $handle, $href, $media){
|
2819 |
-
global $collect_preload_css, $fvm_enabled_css_preload, $fvm_enabled_js_preload;
|
2820 |
-
|
2821 |
-
# return if disabled
|
2822 |
-
if ($fvm_enabled_css_preload != true) {
|
2823 |
-
return $html;
|
2824 |
-
}
|
2825 |
-
|
2826 |
-
# collect
|
2827 |
-
if (stripos($href, '/fvm/out/') !== false) {
|
2828 |
-
$collect_preload_css[] = $href;
|
2829 |
-
}
|
2830 |
-
return $html;
|
2831 |
-
}
|
2832 |
-
|
2833 |
add_filter('script_loader_tag', 'fastvelocity_collect_js_preload_headers', PHP_INT_MAX, 3 );
|
2834 |
function fastvelocity_collect_js_preload_headers($html, $handle, $src){
|
2835 |
global $collect_preload_js, $fvm_enabled_css_preload, $fvm_enabled_js_preload;
|
@@ -2846,10 +2493,10 @@ function fastvelocity_collect_js_preload_headers($html, $handle, $src){
|
|
2846 |
return $html;
|
2847 |
}
|
2848 |
|
2849 |
-
# generate
|
2850 |
add_action('wp_footer', 'fastvelocity_generate_preload_headers', PHP_INT_MAX);
|
2851 |
function fastvelocity_generate_preload_headers(){
|
2852 |
-
global $collect_preload_css, $collect_preload_js, $fvm_enabled_css_preload, $fvm_enabled_js_preload
|
2853 |
|
2854 |
# return if disabled
|
2855 |
if ($fvm_enabled_css_preload != true && $fvm_enabled_js_preload != true) {
|
5 |
Description: Improve your speed score on GTmetrix, Pingdom Tools and Google PageSpeed Insights by merging and minifying CSS and JavaScript files into groups, compressing HTML and other speed optimizations.
|
6 |
Author: Raul Peixoto
|
7 |
Author URI: http://fastvelocity.com
|
8 |
+
Version: 2.5.3
|
9 |
License: GPL2
|
10 |
|
11 |
------------------------------------------------------------------------
|
93 |
$fvm_collect_google_fonts = array();
|
94 |
$collect_preload_css = array();
|
95 |
$collect_preload_js = array();
|
96 |
+
$csscollect = array();
|
97 |
$fvm_debug = get_option('fastvelocity_fvm_debug');
|
98 |
|
99 |
###########################################
|
116 |
$remove_print_mediatypes = get_option('fastvelocity_min_remove_print_mediatypes');
|
117 |
$skip_html_minification = get_option('fastvelocity_min_skip_html_minification');
|
118 |
$strip_htmlcomments = get_option('fastvelocity_min_strip_htmlcomments');
|
|
|
119 |
$skip_google_fonts = get_option('fastvelocity_min_skip_google_fonts');
|
120 |
$skip_emoji_removal = get_option('fastvelocity_min_skip_emoji_removal');
|
121 |
$fvm_clean_header_one = get_option('fastvelocity_fvm_clean_header_one');
|
196 |
|
197 |
# merge, if inline is not selected
|
198 |
if($force_inline_css != true) {
|
199 |
+
#add_action('wp_print_styles', 'fastvelocity_min_merge_header_css', PHP_INT_MAX );
|
200 |
+
#add_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', 9.999999 );
|
201 |
+
add_filter('style_loader_tag', 'fastvelocity_min_merge_css', PHP_INT_MAX, 4 );
|
202 |
+
add_action('wp_print_styles','fastvelocity_add_google_fonts_merged', PHP_INT_MAX);
|
203 |
+
add_action('wp_print_footer_scripts','fastvelocity_add_google_fonts_merged', PHP_INT_MAX );
|
204 |
} else {
|
205 |
add_filter('style_loader_tag', 'fastvelocity_optimizecss', PHP_INT_MAX, 4 );
|
206 |
add_action('wp_print_styles','fastvelocity_add_google_fonts_merged', PHP_INT_MAX);
|
231 |
add_action('template_redirect', 'fastvelocity_min_html_compression_start', PHP_INT_MAX);
|
232 |
}
|
233 |
|
234 |
+
# add the LoadCSS polyfil
|
235 |
+
if($fvmloadcss || $fvm_fawesome_method == 2 || $fvm_gfonts_method == 2) {
|
236 |
+
add_action('wp_footer', 'fvm_add_loadcss', PHP_INT_MAX);
|
237 |
+
}
|
238 |
+
|
239 |
+
# add the LoadAsync JavaScript function
|
240 |
add_action('wp_head', 'fvm_add_loadasync', 0);
|
241 |
|
242 |
# remove query from static assets and process defering (if enabled)
|
255 |
if($fvm_fix_editor == true && is_user_logged_in()) {
|
256 |
remove_action('wp_print_scripts', 'fastvelocity_min_merge_header_scripts', PHP_INT_MAX );
|
257 |
remove_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_scripts', 9.999999 );
|
258 |
+
remove_action('wp_print_styles', 'fastvelocity_min_merge_css', PHP_INT_MAX );
|
259 |
remove_action('wp_print_footer_scripts', 'fastvelocity_min_merge_footer_css', 9.999999 );
|
260 |
remove_action('init', 'fastvelocity_min_disable_wp_emojicons');
|
261 |
remove_action('template_redirect', 'fastvelocity_min_html_compression_start', PHP_INT_MAX);
|
349 |
register_setting('fvm-group', 'fastvelocity_min_remove_print_mediatypes');
|
350 |
register_setting('fvm-group', 'fastvelocity_min_skip_html_minification');
|
351 |
register_setting('fvm-group', 'fastvelocity_min_strip_htmlcomments');
|
|
|
352 |
register_setting('fvm-group', 'fastvelocity_min_skip_google_fonts');
|
353 |
register_setting('fvm-group', 'fastvelocity_min_skip_fontawesome_fonts');
|
354 |
register_setting('fvm-group', 'fastvelocity_min_skip_emoji_removal');
|
719 |
<input name="fastvelocity_min_disable_css_minification" type="checkbox" id="fastvelocity_min_disable_css_minification" value="1" <?php echo checked(1 == get_option('fastvelocity_min_disable_css_minification'), true, false); ?>>
|
720 |
Disable minification on CSS files <span class="note-info">[ If selected, CSS files will be merged but not minified ]</span></label>
|
721 |
<br />
|
|
|
|
|
|
|
|
|
722 |
<label for="fastvelocity_min_remove_print_mediatypes">
|
723 |
<input name="fastvelocity_min_remove_print_mediatypes" type="checkbox" id="fastvelocity_min_remove_print_mediatypes" value="1" <?php echo checked(1 == get_option('fastvelocity_min_remove_print_mediatypes'), true, false); ?> >
|
724 |
Remove the "Print" related stylesheets <span class="note-info">[ If selected, CSS files of mediatype "print" will be removed from the site ]</span></label>
|
725 |
<br />
|
726 |
<label for="fastvelocity_min_force_inline_css_footer">
|
727 |
<input name="fastvelocity_min_force_inline_css_footer" type="checkbox" id="fastvelocity_min_force_inline_css_footer" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_css_footer'), true, false); ?>>
|
728 |
+
Inline CSS in the footer <span class="note-info">[ If selected, any FVM generated CSS files in the footer, will be inlined ]</span></label>
|
729 |
<br />
|
730 |
<label for="fastvelocity_min_force_inline_css">
|
731 |
<input name="fastvelocity_min_force_inline_css" type="checkbox" id="fastvelocity_min_force_inline_css" value="1" <?php echo checked(1 == get_option('fastvelocity_min_force_inline_css'), true, false); ?>>
|
1643 |
|
1644 |
|
1645 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1646 |
|
|
|
|
|
1647 |
|
|
|
|
|
|
|
1648 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1649 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1650 |
|
|
|
1651 |
|
1652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1653 |
|
1654 |
|
|
|
|
|
1655 |
|
|
|
|
|
|
|
|
|
|
|
1656 |
|
|
|
|
|
|
|
|
|
1657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1658 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1659 |
|
|
|
|
|
|
|
1660 |
|
1661 |
+
|
1662 |
+
|
1663 |
+
|
1664 |
+
|
1665 |
+
|
1666 |
+
|
1667 |
+
###########################################
|
1668 |
+
# process css #############################
|
1669 |
+
###########################################
|
1670 |
+
# collect all css files as well as inline css code, to be printed later
|
1671 |
+
function fastvelocity_min_merge_css($html, $handle, $href, $media){
|
1672 |
+
global $fvm_debug, $wp_domain, $wp_home, $force_inline_css, $fvmualist, $fvm_collect_google_fonts, $force_inline_googlefonts, $min_async_googlefonts, $remove_googlefonts, $skip_google_fonts, $css_hide_googlefonts, $remove_print_mediatypes, $ignore, $blacklist, $ignorelist, $wp_home, $fvmloadcss, $fvm_remove_css, $fvm_cdn_url, $disable_minification, $fvm_min_excludecsslist, $disable_css_minification, $fvm_fix_editor, $fvm_fawesome_method, $csscollect;
|
1673 |
+
|
1674 |
+
# current timestamp
|
1675 |
+
$ctime = get_option('fvm-last-cache-update', '0');
|
1676 |
+
|
1677 |
+
# make sure href is complete
|
1678 |
+
$href = fastvelocity_min_get_hurl($href, $wp_domain, $wp_home);
|
1679 |
+
|
1680 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Merging CSS processing start $handle / $href -->" . PHP_EOL; }
|
1681 |
+
|
1682 |
+
# prevent optimization for these locations
|
1683 |
+
if (is_admin() || is_preview() || is_customize_preview() || ($fvm_fix_editor == true && is_user_logged_in())) {
|
1684 |
+
return $html;
|
1685 |
}
|
1686 |
|
1687 |
+
# skip all this, if the async css option is enabled
|
1688 |
+
if($fvmloadcss != false) {
|
1689 |
+
return $html;
|
|
|
|
|
|
|
|
|
|
|
1690 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1691 |
|
1692 |
+
# remove all css?
|
1693 |
+
if($fvm_remove_css != false) {
|
1694 |
+
return false;
|
1695 |
+
}
|
1696 |
|
1697 |
+
# leave conditionals alone
|
1698 |
+
$conditional = wp_styles()->get_data($handle, 'conditional');
|
1699 |
+
if($conditional != false) {
|
1700 |
+
return $html;
|
1701 |
+
}
|
|
|
|
|
1702 |
|
1703 |
+
# mediatype fix for some plugins + remove print mediatypes
|
1704 |
+
if ($media == 'screen' || $media == 'screen, print' || empty($media) || is_null($media) || $media == false) { $media = 'all'; }
|
1705 |
+
if($remove_print_mediatypes != false && $media == 'print') {
|
1706 |
+
return false;
|
1707 |
+
}
|
|
|
|
|
1708 |
|
1709 |
+
# Exclude specific CSS files from PSI?
|
1710 |
+
if($fvm_min_excludecsslist != false && is_array($fvm_min_excludecsslist) && fastvelocity_min_in_arrayi($href, $fvm_min_excludecsslist)) {
|
1711 |
+
$cssguid = 'fvm'.hash('adler32', $href);
|
1712 |
+
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1713 |
+
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$href.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$media.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1714 |
+
echo '}</script>';
|
1715 |
+
return false;
|
1716 |
+
}
|
1717 |
+
|
1718 |
+
# return if in any ignore or black list
|
1719 |
+
$nignore = array(); if(is_array($ignore)) { foreach ($ignore as $i) { if($i != '/fvm/cache/') { $nignore[] = $i; } } }
|
1720 |
+
if (count($nignore) > 0 && fastvelocity_min_in_arrayi($href, $nignore) || count($blacklist) > 0 && fastvelocity_min_in_arrayi($href, $blacklist) || count($ignorelist) > 0 && fastvelocity_min_in_arrayi($href, $ignorelist)) {
|
1721 |
+
return $html;
|
1722 |
+
}
|
1723 |
+
|
1724 |
+
# remove google fonts completely?
|
1725 |
+
if($remove_googlefonts != false && stripos($href, 'fonts.googleapis.com') !== false) {
|
1726 |
+
return false;
|
1727 |
+
}
|
1728 |
+
|
1729 |
+
# handle google fonts here, when merging is disabled
|
1730 |
+
if(stripos($href, 'fonts.googleapis.com') !== false && $skip_google_fonts != false) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1731 |
|
1732 |
+
# hide google fonts from PSI
|
1733 |
+
if($css_hide_googlefonts == true) {
|
1734 |
+
$cssguid = 'fvm'.hash('adler32', $href);
|
1735 |
+
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1736 |
+
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$href.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="all"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1737 |
+
echo '}</script>';
|
1738 |
+
return false;
|
1739 |
+
}
|
1740 |
|
1741 |
+
# load google fonts async
|
1742 |
+
if($min_async_googlefonts != false) {
|
1743 |
+
echo '<link rel="preload" href="'.$href.'" as="style" media="all" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
1744 |
+
echo '<noscript><link rel="stylesheet" href="'.$href.'" media="all" /></noscript>';
|
1745 |
+
echo '<!--[if IE]><link rel="stylesheet" href="'.$href.'" media="all" /><![endif]-->';
|
1746 |
+
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1747 |
}
|
1748 |
}
|
1749 |
|
1750 |
+
# font awesome processing, async css
|
1751 |
+
if($fvm_fawesome_method == 2 && stripos($href, 'font-awesome') !== false) {
|
1752 |
+
echo '<link rel="preload" href="'.$href.'" as="style" media="'.$media.'" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
1753 |
+
echo '<noscript><link rel="stylesheet" href="'.$href.'" media="'.$media.'" /></noscript>';
|
1754 |
+
echo '<!--[if IE]><link rel="stylesheet" href="'.$href.'" media="'.$media.'" /><![endif]-->';
|
1755 |
+
return false;
|
1756 |
+
}
|
1757 |
+
|
1758 |
+
# font awesome processing, async and exclude from PSI
|
1759 |
+
if($fvm_fawesome_method == 3 && stripos($href, 'font-awesome') !== false) {
|
1760 |
+
$cssguid = 'fvm'.hash('adler32', $href);
|
1761 |
+
echo '<script type="text/javascript">if(!navigator.userAgent.match(/'.implode('|', $fvmualist).'/i)){';
|
1762 |
+
echo 'var '.$cssguid.'=document.createElement("link");'.$cssguid.'.rel="stylesheet",'.$cssguid.'.type="text/css",'.$cssguid.'.media="async",'.$cssguid.'.href="'.$href.'",'.$cssguid.'.onload=function(){'.$cssguid.'.media="'.$media.'"},document.getElementsByTagName("head")[0].appendChild('.$cssguid.');';
|
1763 |
+
echo '}</script>';
|
1764 |
+
return false;
|
1765 |
+
}
|
1766 |
+
|
1767 |
+
# font awesome processing, inline
|
1768 |
+
if($fvm_fawesome_method == 1 && stripos($href, 'font-awesome') !== false) {
|
1769 |
|
1770 |
+
# download, minify, cache
|
1771 |
+
$tkey = 'css-'.$ctime.'-'.hash('adler32', $handle.$href).'.css';
|
1772 |
+
$json = false; $json = fvm_get_transient($tkey);
|
1773 |
+
if ( $json === false) {
|
1774 |
+
$json = fvm_download_and_minify($href, null, $disable_css_minification, 'css', $handle);
|
1775 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Uncached file processing now for $handle / $href -->" . PHP_EOL; }
|
1776 |
+
fvm_set_transient($tkey, $json);
|
1777 |
+
}
|
1778 |
+
|
1779 |
+
# decode
|
1780 |
+
$res = json_decode($json, true);
|
1781 |
+
|
1782 |
+
# add font-display
|
1783 |
+
# https://developers.google.com/web/updates/2016/02/font-display
|
1784 |
+
$res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']);
|
1785 |
+
|
1786 |
+
# inline css or fail
|
1787 |
+
if($res['status'] != false) {
|
1788 |
+
echo '<style type="text/css" media="all">'.$res['code'].'</style>'.PHP_EOL;
|
1789 |
+
return false;
|
1790 |
+
} else {
|
1791 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Font Awesome request failed for $href -->" . PHP_EOL; }
|
1792 |
+
return $html;
|
1793 |
}
|
1794 |
}
|
1795 |
+
|
1796 |
+
# inline google fonts, do not collect
|
1797 |
+
if(stripos($href, 'fonts.googleapis.com') !== false && $force_inline_googlefonts != false && $css_hide_googlefonts != true && $min_async_googlefonts != true) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1798 |
|
1799 |
+
# download, minify, cache
|
1800 |
+
$tkey = 'css-'.$ctime.'-'.hash('adler32', $handle.$href).'.css';
|
1801 |
+
$json = false; $json = fvm_get_transient($tkey);
|
1802 |
+
if ( $json === false) {
|
1803 |
+
$json = fvm_download_and_minify($href, null, $disable_css_minification, 'css', $handle);
|
1804 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Uncached file processing now for $handle / $href -->" . PHP_EOL; }
|
1805 |
+
fvm_set_transient($tkey, $json);
|
1806 |
+
}
|
1807 |
+
|
1808 |
+
# decode
|
1809 |
+
$res = json_decode($json, true);
|
1810 |
+
|
1811 |
+
# add font-display
|
1812 |
+
# https://developers.google.com/web/updates/2016/02/font-display
|
1813 |
+
$res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']);
|
1814 |
+
|
1815 |
+
# inline css or fail
|
1816 |
+
if($res['status'] != false) {
|
1817 |
+
echo '<style type="text/css" media="all">'.$res['code'].'</style>'.PHP_EOL;
|
1818 |
+
return false;
|
1819 |
+
} else {
|
1820 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Google fonts request failed for $href -->" . PHP_EOL; }
|
1821 |
+
return $html;
|
1822 |
+
}
|
1823 |
+
}
|
1824 |
+
|
1825 |
+
# collect and remove google fonts for merging
|
1826 |
+
if(stripos($href, 'fonts.googleapis.com') !== false){
|
1827 |
+
$fvm_collect_google_fonts[$handle] = $href;
|
1828 |
+
return false;
|
1829 |
+
}
|
1830 |
+
|
1831 |
+
# skip external scripts that are not specifically allowed
|
1832 |
+
if (fvm_internal_url($href, $wp_home) === false || empty($href)) {
|
1833 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Skipped the next external enqueued CSS -->" . PHP_EOL; }
|
1834 |
+
return $html;
|
1835 |
+
}
|
1836 |
+
|
1837 |
+
# download, minify, cache
|
1838 |
+
$tkey = 'css-'.$ctime.'-'.hash('adler32', $handle.$href).'.css';
|
1839 |
+
$json = false; $json = fvm_get_transient($tkey);
|
1840 |
+
if ( $json === false) {
|
1841 |
+
$json = fvm_download_and_minify($href, null, $disable_css_minification, 'css', $handle);
|
1842 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: Uncached file processing now for $handle / $href -->" . PHP_EOL; }
|
1843 |
+
fvm_set_transient($tkey, $json);
|
1844 |
+
}
|
1845 |
+
|
1846 |
+
# decode
|
1847 |
+
$res = json_decode($json, true);
|
1848 |
+
|
1849 |
+
# colect it, together with other inline children styles
|
1850 |
+
if($res['status'] != false) {
|
1851 |
+
|
1852 |
+
# collect main css code from file
|
1853 |
+
$csscollect[$media][] = array('handle' => $handle, 'log' => $res['log'], 'type'=> 'link', 'code' => $res['code']);
|
1854 |
+
|
1855 |
+
# get inline_styles for this handle
|
1856 |
+
$inline_styles = array();
|
1857 |
+
$inline_styles = wp_styles()->get_data( $handle, 'after' );
|
1858 |
+
wp_deregister_style($handle); # remove any inline styles
|
1859 |
+
if($inline_styles != false) {
|
1860 |
+
|
1861 |
+
# string type
|
1862 |
+
if(is_string($inline_styles)) {
|
1863 |
+
$code = fastvelocity_min_get_css($href, $inline_styles, $disable_css_minification);
|
1864 |
+
if(!empty($code) && $code != false) {
|
1865 |
+
$hash = md5($code);
|
1866 |
+
$codesize = fastvelocity_format_filesize(strlen($code));
|
1867 |
+
$nlog = "--- Merged $codesize of inlined CSS code for [$handle] with md5 hash: $hash ---" . PHP_EOL;
|
1868 |
+
$csscollect[$media][] = array('handle' => $handle, 'log' => $nlog, 'type'=> 'inline', 'code' => $code, 'hash'=>$hash);
|
1869 |
+
}
|
1870 |
}
|
1871 |
|
1872 |
+
# array type
|
1873 |
+
if(is_array($inline_styles)) {
|
1874 |
+
foreach ($inline_styles as $st) {
|
1875 |
+
$code = fastvelocity_min_get_css($href, $st, $disable_css_minification);
|
1876 |
+
if(!empty($code) && $code != false) {
|
1877 |
+
$hash = md5($code);
|
1878 |
+
$codesize = fastvelocity_format_filesize(strlen($code));
|
1879 |
+
$nlog = "--- Merged $codesize of inlined CSS code for [$handle] with md5 hash: $hash ---" . PHP_EOL;
|
1880 |
+
$csscollect[$media][] = array('handle' => $handle, 'log' => $nlog, 'type'=> 'inline', 'code' => $code, 'hash'=>$hash);
|
1881 |
+
}
|
1882 |
+
}
|
1883 |
}
|
|
|
|
|
|
|
|
|
1884 |
}
|
1885 |
|
1886 |
+
# prevent default
|
1887 |
+
return false;
|
1888 |
+
|
1889 |
+
} else {
|
1890 |
+
if($fvm_debug == true) { echo "<!-- FVM DEBUG: $handle / $href returned empty from minification -->" . PHP_EOL; }
|
1891 |
+
return $html;
|
1892 |
}
|
1893 |
+
|
1894 |
+
# fallback, for whatever reason
|
1895 |
+
echo "<!-- ERROR: FVM couldn't catch the CSS file below. Please report this on https://wordpress.org/support/plugin/fast-velocity-minify/ -->";
|
1896 |
+
return $html;
|
1897 |
}
|
1898 |
|
1899 |
|
1900 |
+
# generate merged css files and enqueue them
|
1901 |
+
add_action('wp_head', 'fastvelocity_add_merged_css', 8);
|
1902 |
+
add_action('wp_print_footer_scripts','fastvelocity_add_merged_css', PHP_INT_MAX );
|
1903 |
+
function fastvelocity_add_merged_css(){
|
1904 |
+
global $csscollect, $cachedir, $cachedirurl, $fvmloadcss, $fvm_remove_css, $fvm_enabled_css_preload, $collect_preload_css;
|
|
|
1905 |
|
1906 |
+
# return if disabled
|
1907 |
+
if ($csscollect != true) {
|
1908 |
+
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1909 |
}
|
1910 |
|
1911 |
+
# are we on the header or footer?
|
1912 |
+
$current_action = current_action();
|
|
|
|
|
|
|
|
|
|
|
1913 |
|
1914 |
+
# current timestamp
|
1915 |
+
$ctime = get_option('fvm-last-cache-update', '0');
|
|
|
|
|
|
|
|
|
|
|
|
|
1916 |
|
1917 |
+
# foreach meadiatype, generate css file name, merge, save and print it
|
1918 |
+
$i = 0; foreach ($csscollect as $mediatype=>$arr) {
|
1919 |
+
|
1920 |
+
# initialization
|
1921 |
+
$uid = '';
|
1922 |
+
$log = '';
|
1923 |
+
$code = '';
|
1924 |
+
|
1925 |
+
# filename for this mediatype
|
1926 |
+
foreach ($arr as $k=>$narr) {
|
1927 |
|
1928 |
+
# unset as we go
|
1929 |
+
if(isset($csscollect[$mediatype][$k])) {
|
1930 |
+
unset($csscollect[$mediatype][$k]);
|
1931 |
+
}
|
1932 |
+
|
1933 |
+
# uid needs handle name
|
1934 |
+
if(isset($narr['handle'])) {
|
1935 |
+
$uid.= '_'.$narr['handle'];
|
1936 |
+
}
|
1937 |
+
|
1938 |
+
# uid needs type
|
1939 |
+
if(isset($narr['type'])) {
|
1940 |
+
$uid.= '_'.$narr['type'];
|
1941 |
+
}
|
1942 |
+
|
1943 |
+
# uid needs hash if its an inline type
|
1944 |
+
if(isset($narr['type']) && $narr['type'] == 'inline' && isset($narr['hash'])) {
|
1945 |
+
$uid.= '_'.$narr['hash'];
|
1946 |
+
}
|
1947 |
+
|
1948 |
+
# merge code for this mediatype
|
1949 |
+
if(isset($narr['code']) && isset($narr['log'])) {
|
1950 |
+
$code.=$narr['code'];
|
1951 |
+
$log.=$narr['log'];
|
1952 |
+
}
|
1953 |
|
|
|
|
|
|
|
1954 |
}
|
|
|
|
|
|
|
1955 |
|
1956 |
+
# define file and id prefix
|
1957 |
+
if ($current_action == 'wp_head') {
|
1958 |
+
$cssid = 'header-'.$i;
|
1959 |
+
$fprefix = 'header-';
|
1960 |
+
} else {
|
1961 |
+
$cssid = 'footer-'.$i;
|
1962 |
+
$fprefix = 'footer-';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1963 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1964 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1965 |
# create cache files and urls
|
1966 |
+
$hash = $fprefix.hash('adler32', $mediatype.'_'.$uid);
|
1967 |
$file = $cachedir.'/'.$hash.'-'.$ctime.'.min.css';
|
1968 |
$file_url = fvm_get_protocol($cachedirurl.'/'.$hash.'-'.$ctime.'.min.css');
|
1969 |
|
1971 |
clearstatcache();
|
1972 |
if (!file_exists($file)) {
|
1973 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1974 |
# prepare log
|
1975 |
+
$log = "PROCESSED on " . date('r') . PHP_EOL . $log . "PROCESSED from " . home_url(add_query_arg(NULL, NULL)) . PHP_EOL;
|
1976 |
|
1977 |
+
# generate cache, write log
|
1978 |
if(!empty($code)) {
|
1979 |
file_put_contents($file.'.txt', $log);
|
1980 |
+
file_put_contents($file, $code);
|
1981 |
file_put_contents($file.'.gz', gzencode(file_get_contents($file), 9));
|
1982 |
|
1983 |
# permissions
|
1992 |
}
|
1993 |
}
|
1994 |
}
|
1995 |
+
|
1996 |
+
# note: the developers tab, takes precedence
|
1997 |
+
# Async CSS with loadCSS ?
|
1998 |
+
if($fvmloadcss != false && $fvm_remove_css != true) {
|
1999 |
+
echo '<link id="'.$cssid.'" rel="preload" href="'.$file_url.'" as="style" media="'.$mediatype.'" onload="this.onload=null;this.rel=\'stylesheet\'" />';
|
2000 |
+
echo '<noscript><link rel="stylesheet" href="'.$file_url.'" media="'.$mediatype.'" /></noscript>';
|
2001 |
+
echo '<!--[if IE]><link rel="stylesheet" href="'.$file_url.'" media="'.$mediatype.'" /><![endif]-->';
|
2002 |
+
|
2003 |
+
# else enqueue file, if not empty
|
2004 |
+
} else {
|
2005 |
+
if(file_exists($file) && filesize($file) > 0) {
|
2006 |
+
|
2007 |
+
# inline CSS if mediatype is not of type "all" or if the file is smaller than 20KB
|
2008 |
+
if(filesize($file) < 20000 && $mediatype != 'all') {
|
2009 |
+
echo '<style id="'.$cssid.'" media="'.$mediatype.'">'.file_get_contents($file).'</style>';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010 |
} else {
|
2011 |
+
|
2012 |
+
# add css to the html
|
2013 |
+
echo '<link id="'.$cssid.'" rel="stylesheet" href="'.$file_url.'" media="'.$mediatype.'" />';
|
2014 |
+
|
2015 |
+
# collect it for preload (if enabled + header only)
|
2016 |
+
if ($fvm_enabled_css_preload == true && $current_action == 'wp_head') {
|
2017 |
+
$collect_preload_css[] = $file_url;
|
2018 |
+
}
|
2019 |
}
|
2020 |
+
} else {
|
2021 |
+
# file could not be generated, output something meaningful
|
2022 |
+
echo "<!-- ERROR: FVM was not allowed to save it's cache on - $file -->";
|
2023 |
+
echo "<!-- Please check if the path above is correct and ensure your server has writting permission there! -->";
|
2024 |
+
echo "<!-- If you found a bug, please report this on https://wordpress.org/support/plugin/fast-velocity-minify/ -->";
|
2025 |
}
|
2026 |
}
|
2027 |
+
|
2028 |
+
# increment by mediatype
|
2029 |
+
$i++;
|
|
|
|
|
2030 |
}
|
2031 |
+
|
2032 |
+
|
2033 |
+
|
2034 |
+
# fallback
|
2035 |
+
return false;
|
2036 |
}
|
2037 |
|
2038 |
+
|
2039 |
+
|
|
|
2040 |
###########################################
|
2041 |
|
2042 |
|
2043 |
|
2044 |
+
|
2045 |
+
|
2046 |
+
|
2047 |
+
|
2048 |
+
|
2049 |
+
|
2050 |
+
|
2051 |
+
|
2052 |
+
|
2053 |
###########################################
|
2054 |
# defer CSS globally from the header (order matters)
|
2055 |
# dev: https://www.filamentgroup.com/lab/async-css.html
|
2200 |
}
|
2201 |
|
2202 |
|
2203 |
+
|
2204 |
+
|
2205 |
+
|
2206 |
+
|
2207 |
+
|
2208 |
# inline css in place, instead of inlining the large file
|
2209 |
function fastvelocity_optimizecss($html, $handle, $href, $media){
|
2210 |
global $fvm_debug, $wp_domain, $wp_home, $force_inline_css, $fvmualist, $fvm_collect_google_fonts, $force_inline_googlefonts, $min_async_googlefonts, $remove_googlefonts, $skip_google_fonts, $css_hide_googlefonts, $remove_print_mediatypes, $ignore, $blacklist, $ignorelist, $wp_home, $fvmloadcss, $fvm_remove_css, $fvm_cdn_url, $disable_minification, $fvm_min_excludecsslist, $disable_css_minification, $fvm_fix_editor, $fvm_fawesome_method;
|
2391 |
# get inline_styles for this handle, minify and print
|
2392 |
$inline_styles = array();
|
2393 |
$inline_styles = wp_styles()->get_data( $handle, 'after' );
|
2394 |
+
wp_deregister_style($handle); # remove any inline styles
|
2395 |
if($inline_styles != false) {
|
2396 |
|
2397 |
# string type
|
2476 |
|
2477 |
|
2478 |
|
2479 |
+
# collect all fvm JS files and save them to an headers file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2480 |
add_filter('script_loader_tag', 'fastvelocity_collect_js_preload_headers', PHP_INT_MAX, 3 );
|
2481 |
function fastvelocity_collect_js_preload_headers($html, $handle, $src){
|
2482 |
global $collect_preload_js, $fvm_enabled_css_preload, $fvm_enabled_js_preload;
|
2493 |
return $html;
|
2494 |
}
|
2495 |
|
2496 |
+
# generate preload headers file
|
2497 |
add_action('wp_footer', 'fastvelocity_generate_preload_headers', PHP_INT_MAX);
|
2498 |
function fastvelocity_generate_preload_headers(){
|
2499 |
+
global $collect_preload_css, $collect_preload_js, $fvm_enabled_css_preload, $fvm_enabled_js_preload;
|
2500 |
|
2501 |
# return if disabled
|
2502 |
if ($fvm_enabled_css_preload != true && $fvm_enabled_js_preload != true) {
|
readme.txt
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
Contributors: Alignak
|
3 |
Tags: PHP Minify, Lighthouse, GTmetrix, Pingdom, Pagespeed, CSS Merging, JS Merging, CSS Minification, JS Minification, Speed Optimization, HTML Minification, Performance, Optimization, Speed, Fast
|
4 |
Requires at least: 4.5
|
5 |
-
Stable tag: 2.5.
|
6 |
Tested up to: 5.0.3
|
7 |
License: GPLv3 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
@@ -201,12 +201,17 @@ If you would like to donate any amount to the plugin author (thank you in advanc
|
|
201 |
|
202 |
== Changelog ==
|
203 |
|
|
|
|
|
|
|
|
|
204 |
= 2.5.2 [2019.01.11] =
|
|
|
205 |
* fixed a query monitor notice about mkdir
|
206 |
-
* removed some legacy code
|
207 |
-
* improvement for the
|
208 |
-
*
|
209 |
-
* improvements for merging
|
210 |
|
211 |
= 2.5.1 [2018.12.17] =
|
212 |
* minor bug fix related to the font awesome option
|
2 |
Contributors: Alignak
|
3 |
Tags: PHP Minify, Lighthouse, GTmetrix, Pingdom, Pagespeed, CSS Merging, JS Merging, CSS Minification, JS Minification, Speed Optimization, HTML Minification, Performance, Optimization, Speed, Fast
|
4 |
Requires at least: 4.5
|
5 |
+
Stable tag: 2.5.3
|
6 |
Tested up to: 5.0.3
|
7 |
License: GPLv3 or later
|
8 |
License URI: http://www.gnu.org/licenses/gpl-3.0.html
|
201 |
|
202 |
== Changelog ==
|
203 |
|
204 |
+
= 2.5.3 [2019.01.11] =
|
205 |
+
* fixed inlined css code being minified, even when minification is off
|
206 |
+
* compatibility and performance improvements for the CSS merging and inlining functionality
|
207 |
+
|
208 |
= 2.5.2 [2019.01.11] =
|
209 |
+
* removed CURL as a fallback method (CURL is already a fallback on the WP HTTP API) as per WP recommendation
|
210 |
* fixed a query monitor notice about mkdir
|
211 |
+
* removed some legacy code + some minor performance improvements on the code
|
212 |
+
* improvement for the defer for pagespeed option and ignore list
|
213 |
+
* improvement for the loadCSS functionality
|
214 |
+
* improvements for merging the google fonts option
|
215 |
|
216 |
= 2.5.1 [2018.12.17] =
|
217 |
* minor bug fix related to the font awesome option
|