Skip to content

Commit

Permalink
Use slug instead of id for Font Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
pbking committed Jan 10, 2024
1 parent 176dd50 commit 0094034
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function __construct( $config ) {
throw new Exception( 'Font Collection config options is required as a non-empty array.' );
}

if ( empty( $config['id'] ) || ! is_string( $config['id'] ) ) {
throw new Exception( 'Font Collection config ID is required as a non-empty string.' );
if ( empty( $config['slug'] ) || ! is_string( $config['slug'] ) ) {
throw new Exception( 'Font Collection config slug is required as a non-empty string.' );
}

if ( empty( $config['name'] ) || ! is_string( $config['name'] ) ) {
Expand Down
36 changes: 18 additions & 18 deletions lib/experimental/fonts/font-library/class-wp-font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public static function get_expected_font_mime_types_per_php_version( $php_versio
*/
public static function register_font_collection( $config ) {
$new_collection = new WP_Font_Collection( $config );
if ( self::is_collection_registered( $config['id'] ) ) {
if ( self::is_collection_registered( $config['slug'] ) ) {
$error_message = sprintf(
/* translators: %s: Font collection id. */
__( 'Font collection with id: "%s" is already registered.', 'default' ),
$config['id']
/* translators: %s: Font collection slug. */
__( 'Font collection with slug: "%s" is already registered.', 'default' ),
$config['slug']
);
_doing_it_wrong(
__METHOD__,
Expand All @@ -76,7 +76,7 @@ public static function register_font_collection( $config ) {
);
return new WP_Error( 'font_collection_registration_error', $error_message );
}
self::$collections[ $config['id'] ] = $new_collection;
self::$collections[ $config['slug'] ] = $new_collection;
return $new_collection;
}

Expand All @@ -85,20 +85,20 @@ public static function register_font_collection( $config ) {
*
* @since 6.5.0
*
* @param string $collection_id Font collection ID.
* @param string $collection_slug Font collection slug.
* @return bool True if the font collection was unregistered successfully and false otherwise.
*/
public static function unregister_font_collection( $collection_id ) {
if ( ! self::is_collection_registered( $collection_id ) ) {
public static function unregister_font_collection( $slug ) {
if ( ! self::is_collection_registered( $slug ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Font collection id. */
sprintf( __( 'Font collection "%s" not found.', 'default' ), $collection_id ),
/* translators: %s: Font collection slug. */
sprintf( __( 'Font collection "%s" not found.', 'default' ), $slug ),
'6.5.0'
);
return false;
}
unset( self::$collections[ $collection_id ] );
unset( self::$collections[ $slug ] );
return true;
}

Expand All @@ -107,11 +107,11 @@ public static function unregister_font_collection( $collection_id ) {
*
* @since 6.5.0
*
* @param string $collection_id Font collection ID.
* @param string $slug Font collection slug.
* @return bool True if the font collection is registered and false otherwise.
*/
private static function is_collection_registered( $collection_id ) {
return array_key_exists( $collection_id, self::$collections );
private static function is_collection_registered( $slug ) {
return array_key_exists( $slug, self::$collections );
}

/**
Expand All @@ -130,12 +130,12 @@ public static function get_font_collections() {
*
* @since 6.5.0
*
* @param string $id Font collection id.
* @param string $slug Font collection slug.
* @return array List of font collections.
*/
public static function get_font_collection( $id ) {
if ( array_key_exists( $id, self::$collections ) ) {
return self::$collections[ $id ];
public static function get_font_collection( $slug ) {
if ( array_key_exists( $slug, self::$collections ) ) {
return self::$collections[ $slug ];
}
return new WP_Error( 'font_collection_not_found', 'Font collection not found.' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function register_routes() {

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
'/' . $this->rest_base . '/(?P<slug>[\/\w-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
Expand All @@ -70,8 +70,8 @@ public function register_routes() {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_font_collection( $request ) {
$id = $request->get_param( 'id' );
$collection = WP_Font_Library::get_font_collection( $id );
$slug = $request->get_param( 'slug' );

Check warning on line 73 in lib/experimental/fonts/font-library/class-wp-rest-font-collections-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 7 spaces but found 9 spaces
$collection = WP_Font_Library::get_font_collection( $slug );
// If the collection doesn't exist returns a 404.
if ( is_wp_error( $collection ) ) {
$collection->add_data( array( 'status' => 404 ) );
Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/fonts/font-library/font-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function wp_unregister_font_collection( $collection_id ) {
}

$default_font_collection = array(
'id' => 'default-font-collection',
'slug' => 'default-font-collection',
'name' => 'Google Fonts',
'description' => __( 'Add from Google Fonts. Fonts are copied to and served from your site.', 'gutenberg' ),
'src' => 'https://s.w.org/images/fonts/16.7/collections/google-fonts-with-preview.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function test_should_initialize_data() {
$property->setAccessible( true );

$config = array(
'id' => 'my-collection',
'slug' => 'my-collection',

Check warning on line 20 in phpunit/tests/fonts/font-library/wpFontCollection/__construct.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection',
'description' => 'My collection description',
'src' => 'my-collection-data.json',
Expand Down Expand Up @@ -55,7 +55,7 @@ public function data_should_throw_exception() {
'description' => 'My collection description',
'src' => 'my-collection-data.json',
),
'Font Collection config ID is required as a non-empty string.',
'Font Collection config slug is required as a non-empty string.',
),

'no config' => array(
Expand All @@ -80,7 +80,7 @@ public function data_should_throw_exception() {

'missing src' => array(
array(
'id' => 'my-collection',
'slug' => 'my-collection',

Check warning on line 83 in phpunit/tests/fonts/font-library/wpFontCollection/__construct.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection',
'description' => 'My collection description',
),
Expand Down
12 changes: 6 additions & 6 deletions phpunit/tests/fonts/font-library/wpFontCollection/getData.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,27 @@ public function data_should_get_data() {
return array(
'with a file' => array(
'config' => array(
'id' => 'my-collection',
'slug' => 'my-collection',
'name' => 'My Collection',
'description' => 'My collection description',
'src' => $mock_file,
),
'expected_data' => array(
'id' => 'my-collection',
'slug' => 'my-collection',

Check warning on line 78 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection',
'description' => 'My collection description',
'data' => array( 'this is mock data' => true ),
),
),
'with a url' => array(
'config' => array(
'id' => 'my-collection-with-url',
'slug' => 'my-collection-with-url',

Check warning on line 86 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection with URL',
'description' => 'My collection description',
'src' => 'https://localhost/fonts/mock-font-collection.json',
),
'expected_data' => array(
'id' => 'my-collection-with-url',
'slug' => 'my-collection-with-url',

Check warning on line 92 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection with URL',
'description' => 'My collection description',
'data' => array(
Expand All @@ -100,13 +100,13 @@ public function data_should_get_data() {
),
'with data' => array(

Check warning on line 101 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 3 space(s) between "'with data'" and double arrow, but found 1.
'config' => array(
'id' => 'my-collection',
'slug' => 'my-collection',

Check warning on line 103 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection',
'description' => 'My collection description',
'data' => array( 'this is mock data' => true ),
),
'expected_data' => array(
'id' => 'my-collection',
'slug' => 'my-collection',

Check warning on line 109 in phpunit/tests/fonts/font-library/wpFontCollection/getData.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Collection',
'description' => 'My collection description',
'data' => array( 'this is mock data' => true ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Tests_Fonts_WpFontLibrary_GetFontCollection extends WP_Font_Library_UnitTe

public function test_should_get_font_collection() {
$my_font_collection_config = array(
'id' => 'my-font-collection',
'slug' => 'my-font-collection',
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => path_join( __DIR__, 'my-font-collection-data.json' ),
Expand All @@ -24,7 +24,7 @@ public function test_should_get_font_collection() {
$this->assertInstanceOf( 'WP_Font_Collection', $font_collection );
}

public function test_should_get_no_font_collection_if_the_id_is_not_registered() {
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' );
$this->assertWPError( $font_collection );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function test_should_get_an_empty_list() {

public function test_should_get_mock_font_collection() {
$my_font_collection_config = array(
'id' => 'my-font-collection',
'slug' => 'my-font-collection',
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => path_join( __DIR__, 'my-font-collection-data.json' ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Tests_Fonts_WpFontLibrary_RegisterFontCollection extends WP_Font_Library_U

public function test_should_register_font_collection() {
$config = array(
'id' => 'my-collection',
'slug' => 'my-collection',
'name' => 'My Collection',
'description' => 'My Collection Description',
'src' => 'my-collection-data.json',
Expand All @@ -23,20 +23,20 @@ public function test_should_register_font_collection() {
$this->assertInstanceOf( 'WP_Font_Collection', $collection );
}

public function test_should_return_error_if_id_is_missing() {
public function test_should_return_error_if_slug_is_missing() {
$config = array(
'name' => 'My Collection',
'description' => 'My Collection Description',
'src' => 'my-collection-data.json',
);
$this->expectException( 'Exception' );
$this->expectExceptionMessage( 'Font Collection config ID is required as a non-empty string.' );
$this->expectExceptionMessage( 'Font Collection config slug is required as a non-empty string.' );
WP_Font_Library::register_font_collection( $config );
}

public function test_should_return_error_if_name_is_missing() {
$config = array(
'id' => 'my-collection',
'slug' => 'my-collection',
'description' => 'My Collection Description',
'src' => 'my-collection-data.json',
);
Expand All @@ -52,15 +52,15 @@ public function test_should_return_error_if_config_is_empty() {
WP_Font_Library::register_font_collection( $config );
}

public function test_should_return_error_if_id_is_repeated() {
public function test_should_return_error_if_slug_is_repeated() {
$config1 = array(
'id' => 'my-collection-1',
'slug' => 'my-collection-1',
'name' => 'My Collection 1',
'description' => 'My Collection 1 Description',
'src' => 'my-collection-1-data.json',
);
$config2 = array(
'id' => 'my-collection-1',
'slug' => 'my-collection-1',
'name' => 'My Collection 2',
'description' => 'My Collection 2 Description',
'src' => 'my-collection-2-data.json',
Expand All @@ -72,7 +72,7 @@ public function test_should_return_error_if_id_is_repeated() {

// Expects a _doing_it_wrong notice.
$this->setExpectedIncorrectUsage( 'WP_Font_Library::register_font_collection' );
// Try to register a second collection with same id.
// Try to register a second collection with same slug.
$collection2 = WP_Font_Library::register_font_collection( $config2 );
$this->assertWPError( $collection2, 'A WP_Error should be returned.' );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Tests_Fonts_WpFontLibrary_UnregisterFontCollection extends WP_Font_Library
public function test_should_unregister_font_collection() {
// Registers two mock font collections.
$config = array(
'id' => 'mock-font-collection-1',
'slug' => 'mock-font-collection-1',
'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' => 'mock-font-collection-2',
'slug' => 'mock-font-collection-2',
'name' => 'Mock Collection',
'description' => 'A mock font collection.',
'src' => 'my-mock-data.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function set_up() {
add_filter( 'pre_http_request', array( $this, 'mock_request' ), 10, 3 );

$config_with_file = array(
'id' => 'one-collection',
'slug' => 'one-collection',
'name' => 'One Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => $mock_file,
);
wp_register_font_collection( $config_with_file );

$config_with_url = array(
'id' => 'collection-with-url',
'slug' => 'collection-with-url',
'name' => 'Another Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => 'https://wordpress.org/fonts/mock-font-collection.json',
Expand All @@ -42,7 +42,7 @@ public function set_up() {
wp_register_font_collection( $config_with_url );

$config_with_non_existing_file = array(
'id' => 'collection-with-non-existing-file',
'slug' => 'collection-with-non-existing-file',
'name' => 'Another Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => '/home/non-existing-file.json',
Expand All @@ -51,7 +51,7 @@ public function set_up() {
wp_register_font_collection( $config_with_non_existing_file );

$config_with_non_existing_url = array(
'id' => 'collection-with-non-existing-url',
'slug' => 'collection-with-non-existing-url',
'name' => 'Another Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => 'https://non-existing-url-1234x.com.ar/fake-path/missing-file.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function test_get_font_collections() {

// Add a font collection.
$config = array(
'id' => 'my-font-collection',
'slug' => 'my-font-collection',

Check warning on line 30 in phpunit/tests/fonts/font-library/wpRestFontCollectionsController/getFontCollections.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 10.
'name' => 'My Font Collection',
'description' => 'Demo about how to a font collection to your WordPress Font Library.',
'src' => $mock_file,
Expand All @@ -39,7 +39,7 @@ public function test_get_font_collections() {
$data = $response->get_data();
$this->assertSame( 200, $response->get_status(), 'The response status is not 200.' );
$this->assertCount( 1, $data, 'The response data is not an array with one element.' );
$this->assertArrayHasKey( 'id', $data[0], 'The response data does not have the key with the collection ID.' );
$this->assertArrayHasKey( 'slug', $data[0], 'The response data does not have the key with the collection slug.' );
$this->assertArrayHasKey( 'name', $data[0], 'The response data does not have the key with the collection name.' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Tests_Fonts_WPRESTFontCollectionsController_RegisterRoutes extends WP_Unit
public function test_register_routes() {
$routes = rest_get_server()->get_routes();
$this->assertCount( 1, $routes['/wp/v2/font-collections'], 'Rest server has not the collections path initialized.' );
$this->assertCount( 1, $routes['/wp/v2/font-collections/(?P<id>[\/\w-]+)'], 'Rest server has not the collection path initialized.' );
$this->assertCount( 1, $routes['/wp/v2/font-collections/(?P<slug>[\/\w-]+)'], 'Rest server has not the collection path initialized.' );

$this->assertArrayHasKey( 'GET', $routes['/wp/v2/font-collections'][0]['methods'], 'Rest server has not the GET method for collections intialized.' );
$this->assertArrayHasKey( 'GET', $routes['/wp/v2/font-collections/(?P<id>[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' );
$this->assertArrayHasKey( 'GET', $routes['/wp/v2/font-collections/(?P<slug>[\/\w-]+)'][0]['methods'], 'Rest server has not the GET method for collection intialized.' );
}
}

0 comments on commit 0094034

Please sign in to comment.