From 58e71e4d6d1e960076e6ab8285241e23d8c4e8d7 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Thu, 8 Feb 2024 11:25:08 -0300 Subject: [PATCH 1/6] fix infinite loop when calling wp_get_upload_dir in a function thats used to filter font_dir --- .../fonts/class-wp-rest-font-faces-controller.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 8a4040e3397e0..2d244f0b94d8c 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -858,7 +858,11 @@ 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' ); + $wp_get_font_dir = wp_get_font_dir(); + + add_filter( 'upload_dir', function ( $defaults ) use ( $wp_get_font_dir ) { + return $wp_get_font_dir; + } ); $overrides = array( 'upload_error_handler' => array( $this, 'handle_font_file_upload_error' ), @@ -876,7 +880,6 @@ 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_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); return $uploaded_file; From efac622b998bcb370542bffb6948710187573021 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 28 Feb 2024 13:29:15 -0300 Subject: [PATCH 2/6] remove not needed parameter --- lib/compat/wordpress-6.5/fonts/fonts.php | 27 ++++++++---------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/fonts.php b/lib/compat/wordpress-6.5/fonts/fonts.php index 3911f61ceaec4..55dc2d3429af9 100644 --- a/lib/compat/wordpress-6.5/fonts/fonts.php +++ b/lib/compat/wordpress-6.5/fonts/fonts.php @@ -201,16 +201,6 @@ function gutenberg_register_font_collections() { * * @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. * @@ -222,19 +212,20 @@ function gutenberg_register_font_collections() { * @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. From 22ceeba6bc4f1f77b76e71d6e4fe4cce4bf4fc36 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 28 Feb 2024 13:29:43 -0300 Subject: [PATCH 3/6] remove filter after use --- .../fonts/class-wp-rest-font-faces-controller.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 2d244f0b94d8c..3f66c05ed1724 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -858,11 +858,13 @@ 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' ) ); - $wp_get_font_dir = wp_get_font_dir(); - add_filter( 'upload_dir', function ( $defaults ) use ( $wp_get_font_dir ) { - return $wp_get_font_dir; - } ); + // Set the upload directory to the fonts directory. + $font_dir = wp_get_font_dir(); + $set_upload_dir = function ( $defaults ) 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' ), @@ -879,7 +881,7 @@ protected function handle_font_file_upload( $file ) { ); $uploaded_file = wp_handle_upload( $file, $overrides ); - + remove_filter( 'upload_dir', $set_upload_dir ); remove_filter( 'upload_mimes', array( 'WP_Font_Utils', 'get_allowed_font_mime_types' ) ); return $uploaded_file; From 1f4d96b2e70123339022016ffdae8a54f510106d Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 28 Feb 2024 13:34:13 -0300 Subject: [PATCH 4/6] removing not needed parameter --- .../wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 3f66c05ed1724..3d7084aac1cf8 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -861,7 +861,7 @@ protected function handle_font_file_upload( $file ) { // Set the upload directory to the fonts directory. $font_dir = wp_get_font_dir(); - $set_upload_dir = function ( $defaults ) use ( $font_dir ) { + $set_upload_dir = function () use ( $font_dir ) { return $font_dir; }; add_filter( 'upload_dir', $set_upload_dir ); From 0641ba19fae6fc336dc08096d70be3fc74c7037e Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 28 Feb 2024 14:17:01 -0300 Subject: [PATCH 5/6] Add a comment explaining the reasons behind the implementation Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- .../fonts/class-wp-rest-font-faces-controller.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index 3d7084aac1cf8..fe28219a452fd 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -859,7 +859,15 @@ 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' ) ); - // Set the upload directory to the fonts directory. + /* + * 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; From 700010f394d383e6e35da0c84ae3a2d745738108 Mon Sep 17 00:00:00 2001 From: Matias Benedetto Date: Wed, 28 Feb 2024 14:18:27 -0300 Subject: [PATCH 6/6] fix spaces Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- .../wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php index fe28219a452fd..1d65e0f63aab9 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-faces-controller.php @@ -867,7 +867,7 @@ protected function handle_font_file_upload( $file ) { * * 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;