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

Add a databse persisted fallback for the default font folder path #6252

Closed

Conversation

matiasbenedetto
Copy link

@matiasbenedetto matiasbenedetto commented Mar 11, 2024

What?

Add a fallback for the default font directory persisted in the database as a site option.
There's an alternative PR without database persistence: #6259

Why?

For installations that don’t support modification of the wp-content directory, the Font Library will use wp-content/uploads/fonts as a fallback location, ensuring we stay true to our project philosophy of designing for the majority while still making the feature available to anyone out of the box without extra steps from the user.

Decision from https://make.wordpress.org/core/2024/03/07/unblocking-wp6-5-font-library-and-synced-pattern-overrides/
Discussion: WordPress/gutenberg#59699

How?

Add the wp_default_font_dir function.
If /wp-content/fonts is writable this function should return:

array(
    'path' => '/var/www/src/wp-content/fonts',
    'url' => 'http://localhost:8889/wp-content/fonts',
    'subdir' => ''
    'basedir' => '/var/www/src/wp-content/fonts',
    'baseurl' => 'http://localhost:8889/wp-content/fonts'
    'error' => false
)

If /wp-content/fonts is not writable this function should return:

array(
    'path' => '/var/www/src/wp-content/uploads/fonts',
    'url' => 'http://localhost:8889/wp-content/uploads/fonts',
    'subdir' => ''
    'basedir' => '/var/www/src/wp-content/uploads/fonts',
    'baseurl' => 'http://localhost:8889/wp-content/uploads/fonts'
    'error' => false
)

The output of this function is persisted in the database as a site_option. To refresh the value persisted the wp_default_font_dir function should be called like this wp_default_font_dir( true ).


Trac ticket: https://core.trac.wordpress.org/ticket/60751#ticket


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

github-actions bot commented Mar 11, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props mmaattiiaass, swissspidy.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines +33 to +34
$font_dir = wp_default_font_dir( true );
chmod( $font_dir['path'], 0000 );
Copy link
Author

Choose a reason for hiding this comment

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

I haven't found a way to restrict the writing permissions in the context of unit tests. This chmod line isn't working. It is there just to show how the functionality could be tested.

Copy link
Contributor

Choose a reason for hiding this comment

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

No idea how to test this either. If the option is changed to record the fallback rather than the directory structure then you migh be able to filter it using the pre_option_{$option} hook.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

src/wp-includes/fonts.php Outdated Show resolved Hide resolved
src/wp-includes/fonts.php Outdated Show resolved Hide resolved
src/wp-includes/fonts.php Outdated Show resolved Hide resolved
*/
function wp_get_font_dir() {
function wp_default_font_dir( $refresh_cache = false ) {
$defaults = get_option( 'font_dir' );
Copy link
Author

Choose a reason for hiding this comment

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

I'm not 100% sure if there are significant benefits in making this persistent. Related: WordPress/gutenberg#59699 (comment)

Copy link
Author

Choose a reason for hiding this comment

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

An non-persisted alternative PR to this one: #6259

Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than store the entire font directory in the option, I'd suggest storing whether it falls back to the secondary location.

This will prevent future contributors from having to manage three scenarios:

  • default
  • fallback
  • option set

It's already complicated enough without allowing extenders to change the path via either an option or a filter.

@matiasbenedetto matiasbenedetto changed the title Add fallback for the default font folder path Add a databse persisted fallback for the default font folder path Mar 12, 2024
Copy link
Contributor

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Some notes for this option too.

*/
function wp_get_font_dir() {
function wp_default_font_dir( $refresh_cache = false ) {
$defaults = get_option( 'font_dir' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than store the entire font directory in the option, I'd suggest storing whether it falls back to the secondary location.

This will prevent future contributors from having to manage three scenarios:

  • default
  • fallback
  • option set

It's already complicated enough without allowing extenders to change the path via either an option or a filter.

Comment on lines +123 to +133
wp_mkdir_p( $defaults['path'] );

if ( ! wp_is_writable( $defaults['path'] ) ) {
$defaults = wp_upload_dir();
$defaults['path'] = path_join( $defaults['basedir'], 'fonts' );
$defaults['url'] = $defaults['baseurl'] . '/fonts';
$defaults['subdir'] = '';
$defaults['basedir'] = path_join( $defaults['basedir'], 'fonts' );
$defaults['baseurl'] = $defaults['baseurl'] . '/fonts';
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Notes from #6259 (review) apply here too.

Comment on lines +33 to +34
$font_dir = wp_default_font_dir( true );
chmod( $font_dir['path'], 0000 );
Copy link
Contributor

Choose a reason for hiding this comment

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

No idea how to test this either. If the option is changed to record the fallback rather than the directory structure then you migh be able to filter it using the pre_option_{$option} hook.

@peterwilsoncc
Copy link
Contributor

Closing this PR off per https://make.wordpress.org/core/2024/03/21/font-library-update-storage-of-font-files/. Thanks everyone for your work on this, I appreciate it.

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

Successfully merging this pull request may close these issues.

3 participants