Skip to content

Commit

Permalink
Editor: Prevent infinite loops when filtering the font library folder.
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://develop.svn.wordpress.org/trunk@57740 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
youknowriad committed Feb 29, 2024
1 parent a7943a9 commit 9c415ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
27 changes: 9 additions & 18 deletions src/wp-includes/fonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,6 @@ function wp_unregister_font_collection( string $slug ) {
*
* @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 {
* Array of information about the upload directory.
*
Expand All @@ -117,19 +107,20 @@ function wp_unregister_font_collection( string $slug ) {
* @type string|false $error False or error message.
* }
*/
function wp_get_font_dir( $defaults = array() ) {
function wp_get_font_dir() {
$site_path = '';
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$site_path = '/sites/' . get_current_blog_id();
}

// Sets the defaults.
$defaults['path'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
$defaults['url'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
$defaults['subdir'] = '';
$defaults['basedir'] = path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path;
$defaults['baseurl'] = untrailingslashit( content_url( 'fonts' ) ) . $site_path;
$defaults['error'] = false;
$defaults = array(
'path' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
'url' => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
'subdir' => '',
'basedir' => path_join( WP_CONTENT_DIR, 'fonts' ) . $site_path,
'baseurl' => untrailingslashit( content_url( 'fonts' ) ) . $site_path,
'error' => false,
);

/**
* Filters the fonts directory data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,21 @@ protected function sanitize_src( $value ) {
*/
protected function handle_font_file_upload( $file ) {
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(
'upload_error_handler' => array( $this, 'handle_font_file_upload_error' ),
Expand All @@ -874,7 +888,7 @@ protected function handle_font_file_upload( $file ) {

$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' ) );

return $uploaded_file;
Expand Down Expand Up @@ -902,16 +916,16 @@ public function handle_font_file_upload_error( $file, $message ) {
}

/**
* Returns relative path to an uploaded font file.
*
* The path is relative to the current fonts directory.
*
* @since 6.5.0
* @access private
*
* @param string $path Full path to the file.
* @return string Relative path on success, unchanged path on failure.
*/
* Returns relative path to an uploaded font file.
*
* The path is relative to the current fonts directory.
*
* @since 6.5.0
* @access private
*
* @param string $path Full path to the file.
* @return string Relative path on success, unchanged path on failure.
*/
protected function relative_fonts_path( $path ) {
$new_path = $path;

Expand Down

0 comments on commit 9c415ed

Please sign in to comment.