Font Library: Fix font installation failure #55893
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #55879
What?
This PR fixes an issue where local fonts or Google fonts installation fails.
Why?
When I traced the cause, I found that it was caused by #54829. The error also occurs because
has_write_permission()
isfalse
at this location.gutenberg/lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php
Line 421 in 43c5e2a
The
has_write_permission()
method checks whether the font directory (/path-to/wp-content/fonts
) is writable.However, if you have never installed any fonts, the
fonts
directory does not exist and is considered unwritable.Before the change in #54829,
wp_upload_dir()['basedir']
(/path/to/wp-content/uploads
) was checked to see if it was writable, which is not the actual directory where the fonts would be written. In most cases theuploads
directory will be writable, so thehas_write_permission()
method will return true and the font will be installed unintentionally.How?
Originally, I think we should install the font after all checks below have passed.
fonts
directory exist?fonts
directory does not exist, try to create it and check whether the directory was created successfullyfonts
directory writable?I reflected all these conditions in the logic.
Testing Instructions
Pattern 1
/path/to/wp-content/fonts
directory exists, please delete it.fonts
directory will be created and the font installation should be successful.Pattern 2
/path/to/wp-content/fonts
directory./path-to/wp-content
directory non-writable with the following command:chmod a-w /path/to/wp-content
cannot_create_fonts_folder
).Pattern 3
/path/to/wp-content
directory writable again with the following command:chmod 755 /path/to/wp-content
path/to/wp-content/fonts
dorectory/path-to/wp-content/fonts
directory non-writable with the following command:chmod a-w /path/to/wp-content/fonts
cannot_write_fonts_folder
).