From f67cb7370e32660c55b1ebadde6dab3a5feec7dc Mon Sep 17 00:00:00 2001 From: youknowriad Date: Thu, 29 Feb 2024 10:17:07 +0000 Subject: [PATCH] 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 --- wp-includes/fonts.php | 27 +++++-------- .../class-wp-rest-font-faces-controller.php | 38 +++++++++++++------ wp-includes/version.php | 2 +- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/wp-includes/fonts.php b/wp-includes/fonts.php index 9538981363..690aaa5ffc 100644 --- a/wp-includes/fonts.php +++ b/wp-includes/fonts.php @@ -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. * @@ -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. diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php index 0a870b8c70..3b3a338b83 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-font-faces-controller.php @@ -856,7 +856,21 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller { */ 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' ), @@ -874,7 +888,7 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller { $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; @@ -902,16 +916,16 @@ class WP_REST_Font_Faces_Controller extends WP_REST_Posts_Controller { } /** - * 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; diff --git a/wp-includes/version.php b/wp-includes/version.php index f81e9d985c..137afb5c67 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.