t = $this->options->get( 'critical_css', '' );
} else {
$critical_css_content = rocket_direct_filesystem()->get_contents( $current_page_cpcss );
}
if ( ! $critical_css_content ) {
return $buffer;
}
$critical_css_content = str_replace( '\\', '\\\\', $critical_css_content );
$buffer = preg_replace( '##iU', '', $buffer, 1 );
return $buffer;
}
/**
* Defer loading of CSS files
*
* @since 2.10
* @author Remy Perona
*
* @param string $buffer HTML code.
* @return string Updated HTML code
*/
public function async_css( $buffer ) {
if ( ( defined( 'DONOTROCKETOPTIMIZE' ) && DONOTROCKETOPTIMIZE ) || ( defined( 'DONOTASYNCCSS' ) && DONOTASYNCCSS ) ) {
return;
}
if ( ! $this->options->get( 'async_css' ) ) {
return $buffer;
}
if ( is_rocket_post_excluded_option( 'async_css' ) ) {
return $buffer;
}
if ( ! $this->critical_css->get_current_page_critical_css() ) {
return $buffer;
}
$excluded_css = array_flip( get_rocket_exclude_async_css() );
/**
* Filters the pattern used to get all stylesheets in the HTML
*
* @since 2.10
* @author Remy Perona
*
* @param string $css_pattern Regex pattern to get all stylesheets in the HTML.
*/
$css_pattern = apply_filters( 'rocket_async_css_regex_pattern', '/(?=]*\s(rel\s*=\s*[\'"]stylesheet["\']))]*\shref\s*=\s*[\'"]([^\'"]+)[\'"](.*)>/iU' );
// Get all css files with this regex.
preg_match_all( $css_pattern, $buffer, $tags_match );
if ( ! isset( $tags_match[0] ) ) {
return $buffer;
}
$noscripts = '';
foreach ( $tags_match[0] as $i => $tag ) {
// Strip query args.
$path = rocket_extract_url_component( $tags_match[2][ $i ], PHP_URL_PATH );
// Check if this file should be deferred.
if ( isset( $excluded_css[ $path ] ) ) {
continue;
}
$preload = str_replace( 'stylesheet', 'preload', $tags_match[1][ $i ] );
$onload = preg_replace( '~' . preg_quote( $tags_match[3][ $i ], '~' ) . '~iU', ' as="style" onload=""' . $tags_match[3][ $i ] . '>', $tags_match[3][ $i ] );
$tag = str_replace( $tags_match[3][ $i ] . '>', $onload, $tag );
$tag = str_replace( $tags_match[1][ $i ], $preload, $tag );
$tag = str_replace( 'onload=""', 'onload="this.onload=null;this.rel=\'stylesheet\'"', $tag );
$buffer = str_replace( $tags_match[0][ $i ], $tag, $buffer );
$noscripts .= '';
}
$buffer = str_replace( '