wp-media / wp-rocket

Performance optimization plugin for WordPress
https://wp-rocket.me
GNU General Public License v2.0
700 stars 218 forks source link

3.18 - Filter to change the output method #7065

Open piotrbak opened 2 weeks ago

piotrbak commented 2 weeks ago

User Story As a user, I’d like to be able to print the Google Fonts CSS as an internal style

Acceptance Criteria

Khadreal commented 2 weeks ago

This should be pick up after #7063 is completed.

Scope a solution:

In the rewrite_fonts created in #7063 we check if the filter is true, if true we then the css will be printed as internal in the head tag. WP_Filesystem_Direct should be added to the class constructor of the Controller class which would have been created in previous task.

Sample code

$internal_styling = wpm_apply_filters_typed( 'boolean', 'rocket_internal_fonts_styling', false );

if( $internal_styling ) {
    return $this->set_font_internal_style( $html, $css_path );
}

private function set_font_internal_style( $html, $css_path ) {
    if ( ! preg_match( '#</title\s*>#', $html, $matches ) ) {
        return $html;
    }

    $title   = $matches[0];
    $internal_style = $title;

    if ( ! $this->filesystem->exists( $css_path ) ) {
        return $html;
    }

    $css = '<style data-wpr-hosted-gf-parameters>'
    $css .= $this->filesystem->get_contents( $css_path );
    $css .= '</style>';

    return $css;
}
remyperona commented 1 week ago

LGTM

Miraeld commented 1 week ago

Looks good to me as well