-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Font Library: Font Collection backend (#54098)
Add extensibility capabilities to the Font Library. Provides a way to provide collections of typographic fonts by code. --------- Co-authored-by: Tonya Mork <[email protected]>
- Loading branch information
1 parent
8cd5bff
commit 607ef90
Showing
17 changed files
with
806 additions
and
76 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
lib/experimental/fonts/font-library/class-wp-font-collection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Font Collection class. | ||
* | ||
* This file contains the Font Collection class definition. | ||
* | ||
* @package WordPress | ||
* @subpackage Font Library | ||
* @since 6.4.0 | ||
*/ | ||
|
||
if ( class_exists( 'WP_Font_Collection' ) ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Font Collection class. | ||
* | ||
* @since 6.4.0 | ||
*/ | ||
class WP_Font_Collection { | ||
|
||
/** | ||
* Font collection configuration. | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* WP_Font_Collection constructor. | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @param array $config Font collection config options. | ||
* See {@see wp_register_font_collection()} for the supported fields. | ||
* @throws Exception If the required parameters are missing. | ||
*/ | ||
public function __construct( $config ) { | ||
if ( empty( $config ) || ! is_array( $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['name'] ) || ! is_string( $config['name'] ) ) { | ||
throw new Exception( 'Font Collection config name is required as a non-empty string.' ); | ||
} | ||
|
||
if ( empty( $config['data_json_file'] ) || ! is_string( $config['data_json_file'] ) ) { | ||
throw new Exception( 'Font Collection config "data_json_file" option is required as a non-empty string.' ); | ||
} | ||
|
||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Gets the font collection config. | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @return array An array containing the font collection config. | ||
*/ | ||
public function get_config() { | ||
return $this->config; | ||
} | ||
|
||
/** | ||
* Gets the font collection data. | ||
* | ||
* @since 6.4.0 | ||
* | ||
* @return array|WP_Error An array containing the list of font families in theme.json format on success, | ||
* else an instance of WP_Error on failure. | ||
*/ | ||
public function get_data() { | ||
if ( ! file_exists( $this->config['data_json_file'] ) ) { | ||
return new WP_Error( 'font_collection_file_error', __( 'Font Collection data JSON file does not exist.', 'gutenberg' ) ); | ||
} | ||
|
||
$data = file_get_contents( $this->config['data_json_file'] ); | ||
if ( empty( $data ) ) { | ||
return new WP_Error( 'font_collection_read_error', __( 'Error reading the Font Collection data JSON file contents.', 'gutenberg' ) ); | ||
} | ||
|
||
$collection_data = $this->get_config(); | ||
$collection_data['data'] = $data; | ||
unset( $collection_data['data_json_file'] ); | ||
return $collection_data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
phpunit/tests/fonts/font-library/wpFontCollection/__construct.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Test WP_Font_Collection::__construct(). | ||
* | ||
* @package WordPress | ||
* @subpackage Font Library | ||
* | ||
* @group fonts | ||
* @group font-library | ||
* | ||
* @covers WP_Font_Collection::__construct | ||
*/ | ||
class Tests_Fonts_WpFontCollection_Construct extends WP_UnitTestCase { | ||
|
||
public function test_should_initialize_data() { | ||
$property = new ReflectionProperty( WP_Font_Collection::class, 'config' ); | ||
$property->setAccessible( true ); | ||
|
||
$config = array( | ||
'id' => 'my-collection', | ||
'name' => 'My Collection', | ||
'description' => 'My collection description', | ||
'data_json_file' => 'my-collection-data.json', | ||
); | ||
$font_collection = new WP_Font_Collection( $config ); | ||
|
||
$actual = $property->getValue( $font_collection ); | ||
$property->setAccessible( false ); | ||
|
||
$this->assertSame( $config, $actual ); | ||
} | ||
|
||
/** | ||
* @dataProvider data_should_throw_exception | ||
* | ||
* @param mixed $config Config of the font collection. | ||
* @param string $expected_exception_message Expected exception message. | ||
*/ | ||
public function test_should_throw_exception( $config, $expected_exception_message ) { | ||
$this->expectException( 'Exception' ); | ||
$this->expectExceptionMessage( $expected_exception_message ); | ||
new WP_Font_Collection( $config ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array | ||
*/ | ||
public function data_should_throw_exception() { | ||
return array( | ||
'no id' => array( | ||
array( | ||
'name' => 'My Collection', | ||
'description' => 'My collection description', | ||
'data_json_file' => 'my-collection-data.json', | ||
), | ||
'Font Collection config ID is required as a non-empty string.', | ||
), | ||
|
||
'no config' => array( | ||
'', | ||
'Font Collection config options is required as a non-empty array.', | ||
), | ||
|
||
'empty array' => array( | ||
array(), | ||
'Font Collection config options is required as a non-empty array.', | ||
), | ||
|
||
'boolean instead of config array' => array( | ||
false, | ||
'Font Collection config options is required as a non-empty array.', | ||
), | ||
|
||
'null instead of config array' => array( | ||
null, | ||
'Font Collection config options is required as a non-empty array.', | ||
), | ||
|
||
'missing data_json_file' => array( | ||
array( | ||
'id' => 'my-collection', | ||
'name' => 'My Collection', | ||
'description' => 'My collection description', | ||
), | ||
'Font Collection config "data_json_file" option is required as a non-empty string.', | ||
), | ||
|
||
); | ||
} | ||
} |
Oops, something went wrong.