-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fonts Library: change upload directory to wp-content/fonts #54076
Conversation
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-font-family.php ❔ lib/experimental/fonts/font-library/class-wp-font-library.php ❔ phpunit/tests/fonts/font-library/wpFontFamily/base.php ❔ phpunit/tests/fonts/font-library/wpFontFamily/install.php ❔ phpunit/tests/fonts/font-library/wpFontLibrary/getFontsDir.php ❔ phpunit/tests/fonts/font-library/wpFontLibrary/setUploadDir.php ❔ phpunit/tests/fonts/font-library/wpRestFontLibraryController/installFonts.php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@matiasbenedetto what is the best way to test this, or is the tests passing sufficient?
Yep, it should be enough because we are testing if the files exist in the right location.
gutenberg/phpunit/tests/fonts/font-library/wpFontFamily/install.php
Lines 96 to 99 in 22d24f3
foreach ( $expected as $font_file ) { | |
$font_file = static::$fonts_dir . $font_file; | |
$this->assertFileExists( $font_file, "Font file [{$font_file}] should exists in the uploads/fonts/ directory after installing" ); | |
} |
Please update the tests to look for the files in the right place. I think we would need to update here, for example:
gutenberg/phpunit/tests/fonts/font-library/wpFontFamily/base.php
Lines 28 to 34 in 22d24f3
public static function set_up_before_class() { | |
parent::set_up_before_class(); | |
$uploads_dir = wp_upload_dir(); | |
static::$fonts_dir = $uploads_dir['basedir'] . '/fonts/'; | |
} | |
There could be an enhancement opportunity there ☝️ by simplifying and avoiding the need to define the $upload_dir
in the set_up method.
Flaky tests detected in 119f15b. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6029896336
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the tests work as expected, making a manual test round would be nice, too. For that, you can checkout the frontend branch: #53884 build, and after that, checkout this branch and use the UI to upload a few fonts.
975112e
to
fe107a0
Compare
There are two related tests erroring or failing, which are passing for me locally. Any idea why these would fail in the CI but not locally? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two related tests erroring or failing, which are passing for me locally. Any idea why these would fail in the CI but not locally?
I suspect that the destination folder is not being created on the CI machine. I'll take a look at that.
Apart from that, @jffng, could you please add your findings about why we could not use wp_handle_upload()
function to move the uploaded files?
The current implementation uses the core function If we want to store the fonts in As far as I can tell, there is no option to override this: https://github.com/WordPress/wordpress-develop/blob/6.3/src/wp-admin/includes/file.php#L776-L789 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, there is no option to override this: https://github.com/WordPress/wordpress-develop/blob/6.3/src/wp-admin/includes/file.php#L776-L789
This function is for that:
gutenberg/lib/experimental/fonts/font-library/class-wp-font-library.php
Lines 112 to 115 in 2c0aebd
public static function set_upload_dir( $defaults ) { | |
$defaults['subdir'] = '/fonts'; | |
$defaults['path'] = $defaults['basedir'] . $defaults['subdir']; | |
$defaults['url'] = $defaults['baseurl'] . $defaults['subdir']; |
It is called by the install()
method before writing the files and it's removed just after to avoid changing the normal work of the rest of the codebase:
gutenberg/lib/experimental/fonts/font-library/class-wp-font-family.php
Lines 544 to 547 in 2c0aebd
add_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) ); | |
$were_assets_written = $this->download_or_move_font_faces( $files ); | |
remove_filter( 'upload_dir', array( 'WP_Font_Library', 'set_upload_dir' ) ); | |
I just tried something like this, changing these WP_Font_Library
methods and it seems to work as expected :
public static function get_fonts_dir() {
return path_join( WP_CONTENT_DIR, 'fonts' );
}
public static function set_upload_dir( $defaults ) {
$defaults['basedir'] = WP_CONTENT_DIR;
$defaults['baseurl'] = content_url();
$defaults['path'] = self::get_fonts_dir();
$defaults['url'] = $defaults['baseurl'].'/fonts';
$defaults['subdir'] = 'fonts';
return $defaults;
}
I think it would be nice to continue using wp_handle_upload() due to their fine tunning around security and server edge cases.
Another way to test manually is running this in your console. It's a POST request to install one font family.
|
@matiasbenedetto good suggestion, thank you! Closing this in favor of #54122. |
What
This PR closes #53965.
How
We can't use
wp_handle_upload
because this function only works to move files into theuploads
directory (or subdirectory). This PR reworks the implementation torename
the temporary file to its new location in thewp-content/fonts
directory.To test
wpFontFamily
tests pass