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()isfalseat 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
fontsdirectory 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 theuploadsdirectory 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.
fontsdirectory exist?fontsdirectory does not exist, try to create it and check whether the directory was created successfullyfontsdirectory writable?I reflected all these conditions in the logic.
Testing Instructions
Pattern 1
/path/to/wp-content/fontsdirectory exists, please delete it.fontsdirectory will be created and the font installation should be successful.Pattern 2
/path/to/wp-content/fontsdirectory./path-to/wp-contentdirectory non-writable with the following command:chmod a-w /path/to/wp-contentcannot_create_fonts_folder).Pattern 3
/path/to/wp-contentdirectory writable again with the following command:chmod 755 /path/to/wp-contentpath/to/wp-content/fontsdorectory/path-to/wp-content/fontsdirectory non-writable with the following command:chmod a-w /path/to/wp-content/fontscannot_write_fonts_folder).