]*?href=[\'"]([^\'"]+?\.css.*?)[\'"][^>]*?\/?>#i', $html, $stylesheets, PREG_SET_ORDER);
if(!empty($stylesheets)) {
//create our css cache directory
if(!is_dir(PERFMATTERS_CACHE_DIR . 'css/')) {
@mkdir(PERFMATTERS_CACHE_DIR . 'css/', 0755, true);
}
//populate used selectors
self::get_used_selectors($html);
self::get_excluded_selectors();
$used_css_string = '';
//loop through stylesheets
foreach($stylesheets as $key => $stylesheet) {
//stylesheet check
if(!preg_match('#\srel=[\'"]stylesheet[\'"]#is', $stylesheet[0])) {
continue;
}
//ignore google fonts
if(stripos($stylesheet[1], '//fonts.googleapis.com/css') !== false || stripos($stylesheet[1], '.google-fonts.css') !== false) {
continue;
}
//exclude entire stylesheets
$stylesheet_exclusions = array(
'dashicons.min.css', //core
'/uploads/elementor/css/post-', //elementor
'animations.min.css',
'woocommerce-mobile.min.css', //woocommerce
'woocommerce-smallscreen.css',
'/uploads/oxygen/css/', //oxygen
'/uploads/bb-plugin/cache/', //beaver builder
'/uploads/generateblocks/', //generateblocks
'/et-cache/', //divi
'/widget-google-reviews/assets/css/public-main.css' //plugin for google reviews
);
if(!empty(Config::$options['assets']['rucss_excluded_stylesheets'])) {
$stylesheet_exclusions = array_merge($stylesheet_exclusions, Config::$options['assets']['rucss_excluded_stylesheets']);
}
$stylesheet_exclusions = apply_filters('perfmatters_rucss_excluded_stylesheets', $stylesheet_exclusions);
foreach($stylesheet_exclusions as $exclude) {
if(strpos($stylesheet[1], $exclude) !== false) {
unset($stylesheets[$key]);
continue 2;
}
}
//need to generate used css
if(!$used_css_exists) {
//get local stylesheet path
$url = str_replace(trailingslashit(apply_filters('perfmatters_local_stylesheet_url', (!empty(Config::$options['assets']['rucss_cdn_url']) ? Config::$options['assets']['rucss_cdn_url'] : site_url()))), '', explode('?', $stylesheet[1])[0]);
$file = str_replace('/wp-content', '/', WP_CONTENT_DIR) . $url;
//make sure local file exists
if(!file_exists($file)) {
continue;
}
//get used css from stylesheet
$used_css = self::clean_stylesheet($stylesheet[1], @file_get_contents($file));
//add used stylesheet css to total used
$used_css_string.= $used_css;
}
//delay stylesheets
if(empty(Config::$options['assets']['rucss_stylesheet_behavior'])) {
$new_link = preg_replace('#href=([\'"]).+?\1#', 'data-pmdelayedstyle="' . $stylesheet[1] . '"',$stylesheet[0]);
$html = str_replace($stylesheet[0], $new_link, $html);
}
//async stylesheets
elseif(Config::$options['assets']['rucss_stylesheet_behavior'] == 'async') {
$new_link = preg_replace(array('#media=([\'"]).+?\1#', '#onload=([\'"]).+?\1#'), '', $stylesheet[0]);
$new_link = str_replace('');
if($pos !== false) {
//print file
if(!empty(Config::$options['assets']['rucss_method']) && Config::$options['assets']['rucss_method'] == 'file') {
//grab stored timestamp for query string
$time = get_option('perfmatters_used_css_time', array());
if(!empty($time[$type])) {
$used_css_url = add_query_arg('ver', $time[$type], $used_css_url);
}
$used_css_output = "";
$used_css_output.= '';
}
//print inline
else {
$used_css_output = '';
}
$html = substr_replace($html, '' . $used_css_output, $pos, 8);
}
//delay stylesheet script
if(empty(Config::$options['assets']['rucss_stylesheet_behavior'])) {
$delay_check = !empty(apply_filters('perfmatters_delay_js', !empty(Config::$options['assets']['delay_js']))) && !Utilities::get_post_meta('perfmatters_exclude_delay_js');
if(!$delay_check || empty(Config::$options['assets']['delay_js_behavior']) || isset($_GET['perfmattersjsoff'])) {
$script = '';
$html = str_replace('