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

Font Library: filter fonts upload directory #57697

Merged
merged 11 commits into from
Jan 10, 2024
Merged

Conversation

jffng
Copy link
Contributor

@jffng jffng commented Jan 9, 2024

What?

This PR adds a hook for developers to filter where font assets are stored.

Why?

Fixes #55063, alternative to #57044.

See related reasoning in this comment in the above PR.

How?

Adds a single hook fonts_dir which is passed an associative array with keys path and url, to be consistent with the upload_dir filter.

This PR is based on the one started by @madhusudhand.

Testing Instructions

Case 1: Default

  • Upload / install a font from the font library modal in global styles.
  • Verify the font is uploaded to /wp-content/fonts and the font url is set to site_url/wp-content/fonts/your-font.ttf

Case 2: Custom

  • Add the following snippet to an active plugin or theme:
function filter_fonts_dir( $defaults ) {
    $new_sub_dir = '/my/sub/folder';

    $defaults['path'] = rtrim( $defaults['dir'], '/' ) . $new_sub_dir;
    $defaults['url'] = rtrim( $defaults['url'], '/' ) . $new_sub_dir;

    return $defaults;
}
add_filter( 'fonts_dir', 'filter_fonts_dir' );

Case 3: Multi-site

  • In a multi-site, install a font from the font library modal in global styles
  • Verify the font is uploaded to /wp-content/fonts/your-site-id and the font url is set to site_url/wp-content/fonts/your-site-id/your-font.ttf

Screenshots or screencast

@jffng jffng added [Type] Enhancement A suggestion for improvement. [Feature] Typography Font and typography-related issues and PRs labels Jan 9, 2024
Copy link

github-actions bot commented Jan 9, 2024

This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress.

If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged.

If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack.

Thank you! ❤️

View changed files
❔ phpunit/tests/fonts/font-library/wpFontLibrary/fontsDir.php
❔ lib/experimental/fonts/font-library/class-wp-font-family.php
❔ lib/experimental/fonts/font-library/class-wp-font-library.php

@jffng jffng changed the title Font Library: filter font upload directory Font Library: filter fonts upload directory Jan 9, 2024
Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

I've tested using the testing instructions:

Case 1: Default

  • Verify the font is uploaded to /wp-content/fonts and the font url is set to site_url/wp-content/fonts/your-font.ttf ✅

Case 2: Custom

  • Changed the fonts directory by adding the following snippet to my active theme, and verified the font is uploaded to /custom-fonts-dir ✅
function filter_fonts_dir( $font_settings ) {
    $new_sub_dir = '/custom-fonts-dir';

    $font_settings['dir'] = rtrim( $font_settings['dir'], '/' ) . $new_sub_dir;
    $font_settings['url'] = rtrim( $font_settings['url'], '/' ) . $new_sub_dir;

    return $font_settings;
}
add_filter( 'fonts_dir', 'filter_fonts_dir' );
image

Case 3: Multi-site

  • Verify the font is uploaded to /wp-content/fonts/your-site-id and the font url is set to site_url/wp-content/fonts/your-site-id/your-font.ttf ✅
image

This only worked if I switched to a site that wasn't the primary site, but I think this is the correct logic.

This all looks great to me. I've left some comments for tiny suggestions but otherwise I think this is ready to bring in 🎉

Copy link
Contributor

@matiasbenedetto matiasbenedetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a few commits to make the fonts_dir filter return an array shape equal to the upload_dir filter. Why? because we are using the values of fonts_dir to update upload_dir which will be used to handle the fonts uploaded.

Apart from that, I think it simplifies the code a little.

I removed the no longer needed tests and added new ones to try the filter and its removal.

Copy link
Contributor

@matiasbenedetto matiasbenedetto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests made:
☑️ set filter works
☑️ unset filter works
☑️ upload fonts from multisite
☑️ upload fonts from regular site
☑️ tests pass

LGTM

@jffng
Copy link
Contributor Author

jffng commented Jan 10, 2024

Thanks for testing @mikachan , and nice refactor @matiasbenedetto !

As a follow up, we should add a wp_fonts_dir() function, like wp_upload_dir().

@jffng jffng merged commit 4b1beb4 into trunk Jan 10, 2024
55 checks passed
@jffng jffng deleted the try/font-lib-filter-dir branch January 10, 2024 14:58
@github-actions github-actions bot added this to the Gutenberg 17.6 milestone Jan 10, 2024
@getdave getdave added the Needs PHP backport Needs PHP backport to Core label Jan 15, 2024
@youknowriad youknowriad added Backported to WP Core Pull request that has been successfully merged into WP Core and removed Needs PHP backport Needs PHP backport to Core labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Feature] Typography Font and typography-related issues and PRs [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Font Library: Support alternate paths for fonts (WP_FONTS_DIR)
6 participants