Editor: Prevent infinite loops when filtering the font library folder.

Changing the font library is something we expect hosts to perform.
It's important that we make this filter as seemless as possible.
This commit prevents a potential infinite loop caused by calling wp_get_upload_dir() within the font_dir filter.

Props mmaattiiaass, ironprogrammer, costdev, swissspidy.
Fixes #60652.
Built from https://develop.svn.wordpress.org/trunk@57740


git-svn-id: http://core.svn.wordpress.org/trunk@57241 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
youknowriad 2024-02-29 10:17:07 +00:00
parent 12d738c64c
commit f67cb7370e
3 changed files with 36 additions and 31 deletions

View File

@ -96,16 +96,6 @@ function wp_unregister_font_collection( string $slug ) {
* *
* @since 6.5.0 * @since 6.5.0
* *
* @param array $defaults {
* Array of information about the upload directory.
*
* @type string $path Base directory and subdirectory or full path to the fonts upload directory.
* @type string $url Base URL and subdirectory or absolute URL to the fonts upload directory.
* @type string $subdir Subdirectory
* @type string $basedir Path without subdir.
* @type string $baseurl URL path without subdir.
* @type string|false $error False or error message.
* }
* @return array $defaults { * @return array $defaults {
* Array of information about the upload directory. * Array of information about the upload directory.
* *
@ -117,19 +107,20 @@ function wp_unregister_font_collection( string $slug ) {
* @type string|false $error False or error message. * @type string|false $error False or error message.
* } * }
*/ */
function wp_get_font_dir( $defaults = array() ) { function wp_get_font_dir() {
$site_path = ''; $site_path = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) { if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$site_path = '/sites/' . get_current_blog_id(); $site_path = '/sites/' . get_current_blog_id();
} }
// Sets the defaults. $defaults = array(
$defaults['path'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path; 'path' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
$defaults['url'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path; 'url' => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
$defaults['subdir'] = ''; 'subdir' => '',
$defaults['basedir'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path; 'basedir' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
$defaults['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path; 'baseurl' => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
$defaults['error'] = false; 'error' => false,
);
/** /**
* Filters the fonts directory data. * Filters the fonts directory data.

View File

@ -856,7 +856,21 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller {
*/ */
protected function handle_font_file_upload( $file ) { protected function handle_font_file_upload( $file ) {
add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); add_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );
add_filter( 'upload_dir', 'wp_get_font_dir' );
/*
* Set the upload directory to the fonts directory.
*
* wp_get_font_dir() contains the 'font_dir' hook, whose callbacks are
* likely to call wp_get_upload_dir().
*
* To avoid an infinite loop, don't hook wp_get_font_dir() to 'upload_dir'.
* Instead, just pass its return value to the 'upload_dir' callback.
*/
$font_dir = wp_get_font_dir();
$set_upload_dir = function () use ( $font_dir ) {
return $font_dir;
};
add_filter( 'upload_dir', $set_upload_dir );
$overrides = array( $overrides = array(
'upload_error_handler' => array( $this, 'handle_font_file_upload_error' ), 'upload_error_handler' => array( $this, 'handle_font_file_upload_error' ),
@ -874,7 +888,7 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller {
$uploaded_file = wp_handle_upload( $file, $overrides ); $uploaded_file = wp_handle_upload( $file, $overrides );
remove_filter( 'upload_dir', 'wp_get_font_dir' ); remove_filter( 'upload_dir', $set_upload_dir );
remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) );
return $uploaded_file; return $uploaded_file;

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.5-beta3-57739'; $wp_version = '6.5-beta3-57740';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.