Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.19 - Preload Fonts - Exclusion filter #7283

Open
piotrbak opened this issue Feb 6, 2025 · 1 comment
Open

3.19 - Preload Fonts - Exclusion filter #7283

piotrbak opened this issue Feb 6, 2025 · 1 comment
Milestone

Comments

@piotrbak
Copy link
Contributor

piotrbak commented Feb 6, 2025

User Story

As an admin, I want a filter to exclude fonts from preload fonts

Acceptance Criteria

  • System fonts are excluded by default (details)
  • Filter is guarded against unexpected values
  • Exclusion can happen using URL of the font
  • We need backend for this part
@piotrbak piotrbak added this to the 3.19-alpha milestone Feb 6, 2025
@Miraeld
Copy link
Contributor

Miraeld commented Feb 17, 2025

Scope a solution

There are 2 options for this issue to be implemented.

Case 1: Managing exclusion through JavaScript.

This feature is kinda already implemented. So it could be modified & improved.

At the moment based on #7302 we get a filter here named rocket_preload_fonts_system_fonts which is added to custom data for the beacon to be picked up.

We could rename this filter to be rocket_preload_fonts_excluded_fonts so we could give the beacon the excluded fonts list and it will be managed by the beacon script.

Case 2: Managing exclusions through php

The beacon script detects all kind of fonts, and we manage the exclusion in PHP.
For this, we could add a method in inc/Engine/Media/PreloadFonts/AJAX/Controller.php remove_excluded_fonts($fonts),
In this newly added method,
we could apply a filter rocket_preload_fonts_excluded_fonts that would return an array of excluded fonts.
Do a loop through the $fonts array to check if the font is within the excluded font array and if this is the case remove them.
It would return the edited fonts array.

And this new method could be called within add_data of inc/Engine/Media/PreloadFonts/AJAX/Controller.php (based on https://github.com/wp-media/wp-rocket/pull/7274/files#diff-c379f2b0aa09a595742ade3633cc8e6ba0b841da82cd40c5affaa6ff01e64372) before inserting data to the database.

Prototype of remove_excluded_fonts($fonts):

/**
 * Removes excluded fonts from the list of fonts to be preloaded.
 *
 * @param array $fonts Array of font URLs to be preloaded.
 *
 * @return array Filtered array of fonts, excluding those specified in the exclusion list.
 */
public function remove_excluded_fonts( array $fonts ) {
    // Get the array of excluded fonts from the filter.
    $excluded_fonts = wpm_apply_filters_typed('array', 'rocket_preload_fonts_excluded_fonts', [] );

    // Filter out the excluded fonts.
    $filtered_fonts = array_filter( $fonts, function( $font ) use ( $excluded_fonts ) {
        return ! in_array( $font, $excluded_fonts, true );
    } );

    return $filtered_fonts;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants