From 4c5cd0ba65066fcc46030f54b0865624a4b0bcb5 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 5 Feb 2024 13:25:05 +0100 Subject: [PATCH] Font Library: Refactor as a singleton --- .../fonts/class-wp-font-library.php | 53 ++++++++++++++----- ...ss-wp-rest-font-collections-controller.php | 4 +- lib/compat/wordpress-6.5/fonts/fonts.php | 4 +- .../fonts/font-library/wpFontLibrary/base.php | 9 ++-- .../wpFontLibrary/getFontCollection.php | 4 +- .../wpFontLibrary/getFontCollections.php | 6 +-- .../wpFontLibrary/registerFontCollection.php | 6 +-- .../unregisterFontCollection.php | 16 +++--- 8 files changed, 63 insertions(+), 39 deletions(-) diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php index dec2421cb94df..9bf90ca1d9dd8 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-font-library.php @@ -25,7 +25,15 @@ class WP_Font_Library { * * @var array */ - private static $collections = array(); + private $collections = array(); + + /** + * Container for the main instance of the class. + * + * @since 6.5.0 + * @var WP_Font_Library|null + */ + private static $instance = null; /** * Register a new font collection. @@ -39,10 +47,10 @@ class WP_Font_Library { * @return WP_Font_Collection|WP_Error A font collection if it was registered successfully, * or WP_Error object on failure. */ - public static function register_font_collection( $slug, $data_or_file ) { + public function register_font_collection( $slug, $data_or_file ) { $new_collection = new WP_Font_Collection( $slug, $data_or_file ); - if ( self::is_collection_registered( $new_collection->slug ) ) { + if ( $this->is_collection_registered( $new_collection->slug ) ) { $error_message = sprintf( /* translators: %s: Font collection slug. */ __( 'Font collection with slug: "%s" is already registered.', 'gutenberg' ), @@ -55,7 +63,7 @@ public static function register_font_collection( $slug, $data_or_file ) { ); return new WP_Error( 'font_collection_registration_error', $error_message ); } - self::$collections[ $new_collection->slug ] = $new_collection; + $this->collections[ $new_collection->slug ] = $new_collection; return $new_collection; } @@ -67,8 +75,8 @@ public static function register_font_collection( $slug, $data_or_file ) { * @param string $slug Font collection slug. * @return bool True if the font collection was unregistered successfully and false otherwise. */ - public static function unregister_font_collection( $slug ) { - if ( ! self::is_collection_registered( $slug ) ) { + public function unregister_font_collection( $slug ) { + if ( ! $this->is_collection_registered( $slug ) ) { _doing_it_wrong( __METHOD__, /* translators: %s: Font collection slug. */ @@ -77,7 +85,7 @@ public static function unregister_font_collection( $slug ) { ); return false; } - unset( self::$collections[ $slug ] ); + unset( $this->collections[ $slug ] ); return true; } @@ -89,8 +97,8 @@ public static function unregister_font_collection( $slug ) { * @param string $slug Font collection slug. * @return bool True if the font collection is registered and false otherwise. */ - private static function is_collection_registered( $slug ) { - return array_key_exists( $slug, self::$collections ); + private function is_collection_registered( $slug ) { + return array_key_exists( $slug, $this->collections ); } /** @@ -100,8 +108,8 @@ private static function is_collection_registered( $slug ) { * * @return array List of font collections. */ - public static function get_font_collections() { - return self::$collections; + public function get_font_collections() { + return $this->collections; } /** @@ -113,11 +121,28 @@ public static function get_font_collections() { * @return WP_Font_Collection|WP_Error Font collection object, * or WP_Error object if the font collection doesn't exist. */ - public static function get_font_collection( $slug ) { - if ( array_key_exists( $slug, self::$collections ) ) { - return self::$collections[ $slug ]; + public function get_font_collection( $slug ) { + if ( array_key_exists( $slug, $this->collections ) ) { + return $this->collections[ $slug ]; } return new WP_Error( 'font_collection_not_found', 'Font collection not found.' ); } + + /** + * Utility method to retrieve the main instance of the class. + * + * The instance will be created if it does not exist yet. + * + * @since 6.5.0 + * + * @return WP_Font_Library The main instance. + */ + public static function get_instance() { + if ( null === self::$instance ) { + self::$instance = new self(); + } + + return self::$instance; + } } } diff --git a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php index 372f798f0d2bf..06a073d426abc 100644 --- a/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php +++ b/lib/compat/wordpress-6.5/fonts/class-wp-rest-font-collections-controller.php @@ -74,7 +74,7 @@ public function register_routes() { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_items( $request ) { - $collections_all = WP_Font_Library::get_font_collections(); + $collections_all = WP_Font_Library::get_instance()->get_font_collections(); $page = $request['page']; $per_page = $request['per_page']; @@ -142,7 +142,7 @@ public function get_items( $request ) { */ public function get_item( $request ) { $slug = $request->get_param( 'slug' ); - $collection = WP_Font_Library::get_font_collection( $slug ); + $collection = WP_Font_Library::get_instance()->get_font_collection( $slug ); // If the collection doesn't exist returns a 404. if ( is_wp_error( $collection ) ) { diff --git a/lib/compat/wordpress-6.5/fonts/fonts.php b/lib/compat/wordpress-6.5/fonts/fonts.php index 1258eaacd99c6..e6913b0d8a57e 100644 --- a/lib/compat/wordpress-6.5/fonts/fonts.php +++ b/lib/compat/wordpress-6.5/fonts/fonts.php @@ -131,7 +131,7 @@ function gutenberg_init_font_library() { * successfully, or WP_Error object on failure. */ function wp_register_font_collection( $slug, $data_or_file ) { - return WP_Font_Library::register_font_collection( $slug, $data_or_file ); + return WP_Font_Library::get_instance()->register_font_collection( $slug, $data_or_file ); } } @@ -145,7 +145,7 @@ function wp_register_font_collection( $slug, $data_or_file ) { * @return bool True if the font collection was unregistered successfully, else false. */ function wp_unregister_font_collection( $slug ) { - return WP_Font_Library::unregister_font_collection( $slug ); + return WP_Font_Library::get_instance()->unregister_font_collection( $slug ); } } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/base.php b/phpunit/tests/fonts/font-library/wpFontLibrary/base.php index e8d970f5b3d39..135329e5add73 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/base.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/base.php @@ -7,11 +7,10 @@ */ abstract class WP_Font_Library_UnitTestCase extends WP_UnitTestCase { public function reset_font_collections() { - // Resets the private static property WP_Font_Library::$collections to empty array. - $reflection = new ReflectionClass( 'WP_Font_Library' ); - $property = $reflection->getProperty( 'collections' ); - $property->setAccessible( true ); - $property->setValue( array() ); + $collections = WP_Font_Library::get_instance()->get_font_collections(); + foreach ( $collections as $slug => $collection ) { + WP_Font_Library::get_instance()->unregister_font_collection( $slug ); + } } public function set_up() { diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php index 2c246de80b8b9..675efe81aec59 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollection.php @@ -19,12 +19,12 @@ public function test_should_get_font_collection() { ); wp_register_font_collection( 'my-font-collection', $mock_collection_data ); - $font_collection = WP_Font_Library::get_font_collection( 'my-font-collection' ); + $font_collection = WP_Font_Library::get_instance()->get_font_collection( 'my-font-collection' ); $this->assertInstanceOf( 'WP_Font_Collection', $font_collection ); } public function test_should_get_no_font_collection_if_the_slug_is_not_registered() { - $font_collection = WP_Font_Library::get_font_collection( 'not-registered-font-collection' ); + $font_collection = WP_Font_Library::get_instance()->get_font_collection( 'not-registered-font-collection' ); $this->assertWPError( $font_collection ); } } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php index 394c977799a06..f5ca6389b8ff5 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getFontCollections.php @@ -12,7 +12,7 @@ */ class Tests_Fonts_WpFontLibrary_GetFontCollections extends WP_Font_Library_UnitTestCase { public function test_should_get_an_empty_list() { - $font_collections = WP_Font_Library::get_font_collections(); + $font_collections = WP_Font_Library::get_instance()->get_font_collections(); $this->assertEmpty( $font_collections, 'Should return an empty array.' ); } @@ -23,9 +23,9 @@ public function test_should_get_mock_font_collection() { 'font_families' => array( 'mock' ), ); - WP_Font_Library::register_font_collection( 'my-font-collection', $my_font_collection_config ); + WP_Font_Library::get_instance()->register_font_collection( 'my-font-collection', $my_font_collection_config ); - $font_collections = WP_Font_Library::get_font_collections(); + $font_collections = WP_Font_Library::get_instance()->get_font_collections(); $this->assertNotEmpty( $font_collections, 'Should return an array of font collections.' ); $this->assertCount( 1, $font_collections, 'Should return an array with one font collection.' ); $this->assertArrayHasKey( 'my-font-collection', $font_collections, 'The array should have the key of the registered font collection id.' ); diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php index 24529d0dd090b..d3b0f126e2e7e 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/registerFontCollection.php @@ -17,7 +17,7 @@ public function test_should_register_font_collection() { 'font_families' => array( 'mock' ), ); - $collection = WP_Font_Library::register_font_collection( 'my-collection', $config ); + $collection = WP_Font_Library::get_instance()->register_font_collection( 'my-collection', $config ); $this->assertInstanceOf( 'WP_Font_Collection', $collection ); } @@ -28,13 +28,13 @@ public function test_should_return_error_if_slug_is_repeated() { ); // Register first collection. - $collection1 = WP_Font_Library::register_font_collection( 'my-collection-1', $mock_collection_data ); + $collection1 = WP_Font_Library::get_instance()->register_font_collection( 'my-collection-1', $mock_collection_data ); $this->assertInstanceOf( 'WP_Font_Collection', $collection1, 'A collection should be registered.' ); // Expects a _doing_it_wrong notice. $this->setExpectedIncorrectUsage( 'WP_Font_Library::register_font_collection' ); // Try to register a second collection with same slug. - WP_Font_Library::register_font_collection( 'my-collection-1', $mock_collection_data ); + WP_Font_Library::get_instance()->register_font_collection( 'my-collection-1', $mock_collection_data ); } } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php b/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php index 657d3bb07aaaf..ddb0fa91c1d60 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/unregisterFontCollection.php @@ -19,18 +19,18 @@ public function test_should_unregister_font_collection() { ); // Registers two mock font collections. - WP_Font_Library::register_font_collection( 'mock-font-collection-1', $mock_collection_data ); - WP_Font_Library::register_font_collection( 'mock-font-collection-2', $mock_collection_data ); + WP_Font_Library::get_instance()->register_font_collection( 'mock-font-collection-1', $mock_collection_data ); + WP_Font_Library::get_instance()->register_font_collection( 'mock-font-collection-2', $mock_collection_data ); // Unregister mock font collection. - WP_Font_Library::unregister_font_collection( 'mock-font-collection-1' ); - $collections = WP_Font_Library::get_font_collections(); + WP_Font_Library::get_instance()->unregister_font_collection( 'mock-font-collection-1' ); + $collections = WP_Font_Library::get_instance()->get_font_collections(); $this->assertArrayNotHasKey( 'mock-font-collection-1', $collections, 'Font collection was not unregistered.' ); $this->assertArrayHasKey( 'mock-font-collection-2', $collections, 'Font collection was unregistered by mistake.' ); // Unregisters remaining mock font collection. - WP_Font_Library::unregister_font_collection( 'mock-font-collection-2' ); - $collections = WP_Font_Library::get_font_collections(); + WP_Font_Library::get_instance()->unregister_font_collection( 'mock-font-collection-2' ); + $collections = WP_Font_Library::get_instance()->get_font_collections(); $this->assertArrayNotHasKey( 'mock-font-collection-2', $collections, 'Mock font collection was not unregistered.' ); // Checks that all font collections were unregistered. @@ -39,8 +39,8 @@ public function test_should_unregister_font_collection() { public function unregister_non_existing_collection() { // Unregisters non-existing font collection. - WP_Font_Library::unregister_font_collection( 'non-existing-collection' ); - $collections = WP_Font_Library::get_font_collections(); + WP_Font_Library::get_instance()->unregister_font_collection( 'non-existing-collection' ); + $collections = WP_Font_Library::get_instance()->get_font_collections(); $this->assertEmpty( $collections, 'No collections should be registered.' ); } }