Skip to content

Fix: Correct font slug before calculating $to_add #46292

New issue

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

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

Already on ? Sign in to your account

Closed

Conversation

enodekciw
Copy link
Contributor

What?

Incorrect font-family slug is being used while registering theme webfonts.

Why?

Solves #46289

How?

Passing $family to WP_Webfonts::get_font_slug( $family ) returns sanitized font-family. This is OK for webfonts, but not OK when used on settings.typography.fontFamilies. Also, there's already $family['slug'] available to check against.

@github-actions-actions bot added the First-time ContributorPull request opened by a first-time contributor to Gutenberg repositorylabel Dec 4, 2022
@github-actions
Copy link

-actions bot commented Dec 4, 2022

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @enodekciw! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@enodekciwenodekciw changed the title fix: correct font slug before calculating $to_add Fix: Correct font slug before calculating $to_add Dec 4, 2022
@simisonsimison added [Feature] Webfonts Global StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.jsonlabels Dec 8, 2022
@simison
Copy link
Member

Added few reviewers to move this forward; feel free to ping others or un-assign yourself if it makes sense.

@t-hamano
Copy link
Contributor

Sorry, I am not familiar with the web font codebase and cannot determine if this change is appropriate.

@aristath
I would be happy if you will look into this PR 🙏

@matiasbenedetto
Copy link
Contributor

matiasbenedetto commented Dec 8, 2022

Thanks a lot, @enodekciw, for working on this!

I think this approach you propose breaks in certain cases, specifically when the font family doesn't have a slug defined. It doesn't solve either this related issue around duplicated fonts when using fallback values in the family name.

This is a valid theme.json snippet:

"typography": {
    "fontFamilies": [
        {
            "fontFamily": "SpaceMono, serif",
            "name": "SpaceMono",
            "fontFace": [
                {
                    "fontDisplay": "block",
                    "fontFamily": "SpaceMono",
                    "fontWeight": "400",
                    "fontStyle": "normal",
                    "fontStretch": "normal",
                    "src": [
                        "file:./assets/fonts/SpaceMono-Regular.ttf"
                    ]
                }
            ]
        }
    ]

As you can see in the theme.json schema the slug key is not required but this change makes it required because it is being accessed without checking if it's there and without providing an fallback value when is not. If you use a theme.json like that, this change produces 2 PHP warnings:

Warning:  Undefined array key "slug" in /projects/gutenberg/lib/experimental/register-webfonts-from-theme-json.php on line 121
Warning:  array_flip(): Can only flip string and integer values. Entry skipped in /projects/gutenberg/lib/experimental/register-webfonts-from-theme-json.php on line 127

Before seeing this, I proposed an alternative fix yesterday. Sorry for the partial overlap! thanks again for this valuable work! I would greatly appreciate it if you could review and test this PR.

@enodekciw
Copy link
Contributor Author

Oh, didn't know that slug is not required, thanks for pointing this out!

Yup, saw your PR yesterday. Actually, I think that is an even better way to solve this issue.

@enodekciwenodekciw deleted the fix/incorrect-font-family-slug branch August 18, 2024 18:20
Sign up for free to join this conversation on . Already have an account? Sign in to comment
Labels
First-time ContributorPull request opened by a first-time contributor to Gutenberg repositoryGlobal StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.json
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants