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: Fix font installation failure #55893

Merged
merged 2 commits into from
Nov 7, 2023
Merged

Conversation

t-hamano
Copy link
Contributor

@t-hamano t-hamano commented Nov 6, 2023

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() is false at this location.

if ( $this->needs_write_permission( $fonts_to_install ) && ! $this->has_write_permission() ) {

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 the uploads directory will be writable, so the has_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.

  • Does the fonts directory exist?
  • If the fonts directory does not exist, try to create it and check whether the directory was created successfully
  • Is the fonts directory writable?

I reflected all these conditions in the logic.

Testing Instructions

Pattern 1

  • If the /path/to/wp-content/fonts directory exists, please delete it.
  • Try installing the font.
  • The fonts directory will be created and the font installation should be successful.

Pattern 2

  • Delete /path/to/wp-content/fonts directory.
  • Make the /path-to/wp-content directory non-writable with the following command: chmod a-w /path/to/wp-content
  • Try installing the font.
  • The installation should fail and return a 500 error (cannot_create_fonts_folder).

Pattern 3

  • Make the /path/to/wp-content directory writable again with the following command: chmod 755 /path/to/wp-content
  • Create path/to/wp-content/fonts dorectory
  • Make the /path-to/wp-content/fonts directory non-writable with the following command: chmod a-w /path/to/wp-content/fonts
  • Try installing the font.
  • The installation should fail and return a 500 error (cannot_write_fonts_folder).

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Feature] Typography Font and typography-related issues and PRs labels Nov 6, 2023
@t-hamano t-hamano self-assigned this Nov 6, 2023
Copy link

github-actions bot commented Nov 6, 2023

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
❔ lib/experimental/fonts/font-library/class-wp-rest-font-library-controller.php

Copy link

github-actions bot commented Nov 6, 2023

Flaky tests detected in d7d46a3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6771852220
📝 Reported issues:

@t-hamano t-hamano marked this pull request as ready for review November 6, 2023 14:48
Copy link
Contributor

@pbking pbking left a comment

Choose a reason for hiding this comment

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

Yes! Good catch @t-hamano and thank you for fixing.
I made only one minor change so that the has_upload_directory is private (and thus no need for unit testing) since it is not used externally.

🚢

@t-hamano
Copy link
Contributor Author

t-hamano commented Nov 7, 2023

Thanks for the review, @pbking!

I made only one minor change so that the has_upload_directory is private

Thank you for your follow-up. This certainly makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Typography Font and typography-related issues and PRs [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot import Google fonts in Gutenburg 16.9.0
2 participants