diff --git a/lib/experimental/fonts/font-library/class-wp-font-library.php b/lib/experimental/fonts/font-library/class-wp-font-library.php index aab7bcee79a7e1..346cf10014541b 100644 --- a/lib/experimental/fonts/font-library/class-wp-font-library.php +++ b/lib/experimental/fonts/font-library/class-wp-font-library.php @@ -52,6 +52,15 @@ public static function get_expected_font_mime_types_per_php_version( $php_versio */ private static $collections = array(); + /** + * Unregistered font collections ids. + * + * @since 6.4.0 + * + * @var array + */ + private static $unregistered_collection_ids = array(); + /** * Register a new font collection. * @@ -81,11 +90,7 @@ public static function register_font_collection( $config ) { * @return bool True if the font collection was unregistered successfully and false otherwise. */ public static function unregister_font_collection( $collection_id ) { - if ( ! isset( self::$collections[ $collection_id ] ) ) { - return false; - } - unset( self::$collections[ $collection_id ] ); - return true; + self::$unregistered_collection_ids[] = $collection_id; } /** @@ -96,7 +101,14 @@ public static function unregister_font_collection( $collection_id ) { * @return array List of font collections. */ public static function get_font_collections() { - return self::$collections; + // filters collections to remove unregistered collections. + $registered_collections = array(); + foreach ( self::$collections as $key => $value ) { + if ( ! in_array( $key, self::$unregistered_collection_ids, true ) ) { + $registered_collections[ $key ] = $value; + } + } + return $registered_collections; } /** diff --git a/lib/experimental/fonts/font-library/font-library.php b/lib/experimental/fonts/font-library/font-library.php index ea6f9c8394b513..bffc9d0a550409 100644 --- a/lib/experimental/fonts/font-library/font-library.php +++ b/lib/experimental/fonts/font-library/font-library.php @@ -66,13 +66,9 @@ function wp_register_font_collection( $config ) { * @param string $collection_id The font collection ID. */ function wp_unregister_font_collection( $collection_id ) { - add_action( - 'init', - function () use ( $collection_id ) { - WP_Font_Library::unregister_font_collection( $collection_id ); - } - ); + WP_Font_Library::unregister_font_collection( $collection_id ); } + } $default_font_collection = array( diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php index 425a7e717cee37..927bc70443895d 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php @@ -13,36 +13,28 @@ class Tests_Fonts_WpFontLibrary_UnregisterFontCollection extends WP_UnitTestCase { public function set_up() { - $config = array( - 'id' => 'mock-col-to-unregister', - 'name' => 'Mock Collection to be unregistered', - 'description' => 'A mock font collection to be unregistered.', - 'data_json_file' => 'my-collection-data.json', + $config = array( + 'id' => 'mock-col-to-unregister', + 'name' => 'Mock Collection to be unregistered', + 'description' => 'A mock font collection to be unregistered.', + 'src' => 'my-collection-data.json', + ); + WP_Font_Library::register_font_collection( $config ); + + $config = array( + 'id' => 'another-collecction', + 'name' => 'Mock Collection', + 'description' => 'A mock font collection.', + 'src' => 'my-mock-data.json', ); WP_Font_Library::register_font_collection( $config ); } public function test_should_unregister_font_collection() { // Unregister mock font collection. - $result1 = WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' ); - $this->assertTrue( $result1, 'Should return true if it was unregistered succesfully.' ); - // Try to unregister mock font collection again. - $result2 = WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' ); - $this->assertFalse( $result2, 'Should return false because it was already unregistered.' ); - } - - public function test_should_unregister_default_font_collection() { - // Unregister default font collection. - $result1 = WP_Font_Library::unregister_font_collection( 'default-font-collection' ); - $this->assertTrue( $result1, 'Should return true if it was unregistered succesfully.' ); - // Try to unregister default font collection again. - $result2 = WP_Font_Library::unregister_font_collection( 'default-font-collection' ); - $this->assertFalse( $result2, 'Should return false because it was already unregistered.' ); - } - - public function test_should_unregister_non_existing_font_collection() { - // Unregister default font collection. - $result = WP_Font_Library::unregister_font_collection( 'fake-collection-id-x1234' ); - $this->assertFalse( $result ); + WP_Font_Library::unregister_font_collection( 'mock-col-to-unregister' ); + $collections = WP_Font_Library::get_font_collections(); + $this->assertArrayNotHasKey( 'mock-col-to-unregister', $collections, 'Font collection was not unregistered.' ); + $this->assertArrayHasKey( 'default-font-collection', $collections, 'Font collection was unregistered by mistake.' ); } }