From 351ebfefa79d9213b7b49dcbaf46172cff710431 Mon Sep 17 00:00:00 2001 From: Khokan Sardar Date: Mon, 29 Jun 2026 09:15:51 +0530 Subject: [PATCH] Editor: Add hook to disable the Font Library. Introduce the `wp_is_font_library_enabled()` function and the `wp_is_font_library_enabled` filter so sites can remove the Font Library from the admin interface. When the filter returns false, the "Fonts" admin menu item is not registered and the Font Library admin screen returns a 403. This is useful for sites where fonts are managed by the theme and the ability to install fonts from the interface is not needed. Add unit tests for the new function. Fixes #65550. --- src/wp-admin/font-library.php | 8 ++++ src/wp-admin/menu.php | 4 +- src/wp-includes/fonts.php | 24 ++++++++++++ .../font-library/wpIsFontLibraryEnabled.php | 39 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/fonts/font-library/wpIsFontLibraryEnabled.php diff --git a/src/wp-admin/font-library.php b/src/wp-admin/font-library.php index abc2ea4f4da70..8e2b78b9b8630 100644 --- a/src/wp-admin/font-library.php +++ b/src/wp-admin/font-library.php @@ -10,6 +10,14 @@ /** WordPress Administration Bootstrap */ require_once __DIR__ . '/admin.php'; +if ( ! wp_is_font_library_enabled() ) { + wp_die( + '

' . __( 'Font Library is not available.' ) . '

' . + '

' . __( 'The Font Library has been disabled on this site.' ) . '

', + 403 + ); +} + if ( ! current_user_can( 'edit_theme_options' ) ) { wp_die( '

' . __( 'You need a higher level of permission.' ) . '

' . diff --git a/src/wp-admin/menu.php b/src/wp-admin/menu.php index 57d94c75e26f2..b5d5ad5a56f34 100644 --- a/src/wp-admin/menu.php +++ b/src/wp-admin/menu.php @@ -237,7 +237,9 @@ } // Font Library menu item. -$submenu['themes.php'][9] = array( __( 'Fonts' ), 'edit_theme_options', 'font-library.php' ); +if ( wp_is_font_library_enabled() ) { + $submenu['themes.php'][9] = array( __( 'Fonts' ), 'edit_theme_options', 'font-library.php' ); +} $customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' ); diff --git a/src/wp-includes/fonts.php b/src/wp-includes/fonts.php index 198fc21d61c74..700a246cab723 100644 --- a/src/wp-includes/fonts.php +++ b/src/wp-includes/fonts.php @@ -109,6 +109,30 @@ function wp_unregister_font_collection( string $slug ) { return WP_Font_Library::get_instance()->unregister_font_collection( $slug ); } +/** + * Determines whether the Font Library is enabled. + * + * The Font Library lets users with the `edit_theme_options` capability install + * and manage fonts from the admin interface. Returning false from the + * {@see 'wp_is_font_library_enabled'} filter removes the Font Library admin + * menu item and screen, which is useful for sites where fonts are managed by + * the theme and the feature is not needed in the interface. + * + * @since 7.1.0 + * + * @return bool True if the Font Library is enabled, false otherwise. + */ +function wp_is_font_library_enabled() { + /** + * Filters whether the Font Library is enabled. + * + * @since 7.1.0 + * + * @param bool $enabled Whether the Font Library is enabled. Default true. + */ + return (bool) apply_filters( 'wp_is_font_library_enabled', true ); +} + /** * Retrieves font uploads directory information. * diff --git a/tests/phpunit/tests/fonts/font-library/wpIsFontLibraryEnabled.php b/tests/phpunit/tests/fonts/font-library/wpIsFontLibraryEnabled.php new file mode 100644 index 0000000000000..cd00e527eac10 --- /dev/null +++ b/tests/phpunit/tests/fonts/font-library/wpIsFontLibraryEnabled.php @@ -0,0 +1,39 @@ +assertTrue( wp_is_font_library_enabled() ); + } + + /** + * @ticket 65550 + */ + public function test_can_be_disabled_via_filter() { + add_filter( 'wp_is_font_library_enabled', '__return_false' ); + + $this->assertFalse( wp_is_font_library_enabled() ); + } + + /** + * @ticket 65550 + */ + public function test_filtered_value_is_cast_to_boolean() { + add_filter( 'wp_is_font_library_enabled', '__return_zero' ); + + $this->assertFalse( wp_is_font_library_enabled() ); + } +}