= $this->remove_google_font_preconnect( $html );
if ( ! empty( $used_css->id ) ) {
$this->used_css_query->update_last_accessed( (int) $used_css->id );
}
return $this->add_meta_comment( 'remove_unused_css', $html );
}
/**
* Delete used css based on URL.
*
* @param string $url The page URL.
*
* @return boolean
*/
public function delete_used_css( string $url ): bool {
$used_css_arr = $this->used_css_query->get_rows_by_url( $url );
if ( empty( $used_css_arr ) ) {
return false;
}
$deleted = true;
foreach ( $used_css_arr as $used_css ) {
if ( ! is_object( $used_css ) || empty( $used_css->id ) ) {
continue;
}
$deleted = $deleted && $this->used_css_query->delete_item( $used_css->id );
if ( ! empty( $used_css->hash ) ) {
$count = $this->used_css_query->count_rows_by_hash( $used_css->hash );
if ( 0 === $count ) {
$this->filesystem->delete_used_css( $used_css->hash );
}
}
}
return $deleted;
}
/**
* Deletes all the used CSS files
*
* @since 3.11.4
*
* @return void
*/
public function delete_all_used_css() {
$this->filesystem->delete_all_used_css();
}
/**
* Alter HTML and remove all CSS which was processed from HTML page.
*
* @param string $clean_html Cleaned HTML after removing comments, noscripts and scripts.
* @param string $html HTML content.
*
* @return string HTML content.
*/
private function remove_used_css_from_html( string $clean_html, string $html ): string {
$this->set_inline_exclusions_lists();
$html = $this->remove_external_styles_from_html( $clean_html, $html );
return $this->remove_internal_styles_from_html( $clean_html, $html );
}
/**
* Remove external styles from the page's HTML.
*
* @param string $clean_html Cleaned HTML after removing comments, noscripts and scripts.
* @param string $html Actual page's HTML.
*
* @return string
*/
private function remove_external_styles_from_html( string $clean_html, string $html ) {
$link_styles = $this->find(
']+[\s"\'])?href\s*=\s*[\'"]\s*?(?[^\'"]+(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>',
$clean_html,
'Uis'
);
$preserve_google_font = apply_filters( 'rocket_rucss_preserve_google_font', false );
$external_exclusions = $this->validate_array_and_quote(
/**
* Filters the array of external exclusions.
*
* @since 3.11.4
*
* @param array $external_exclusions Array of patterns used to match against the external style tag.
*/
(array) apply_filters( 'rocket_rucss_external_exclusions', $this->external_exclusions )
);
foreach ( $link_styles as $style ) {
if (
(
! (bool) preg_match( '/rel=[\'"]?stylesheet[\'"]?/is', $style[0] )
&&
! ( (bool) preg_match( '/rel=[\'"]?preload[\'"]?/is', $style[0] ) && (bool) preg_match( '/as=[\'"]?style[\'"]?/is', $style[0] ) )
)
||
( $preserve_google_font && strstr( $style['url'], '//fonts.googleapis.com/css' ) )
) {
continue;
}
if ( ! empty( $external_exclusions ) && $this->find( implode( '|', $external_exclusions ), $style[0] ) ) {
continue;
}
$html = str_replace( $style[0], '', $html );
}
return (string) $html;
}
/**
* Remove internal styles from the page's HTML.
*
* @param string $clean_html Cleaned HTML after removing comments, noscripts and scripts.
* @param string $html Actual page's HTML.
*
* @return string
*/
private function remove_internal_styles_from_html( string $clean_html, string $html ) {
$inline_styles = $this->find(
'