Skip to content

Commit 0850b36

Browse files
committed
Add is-registered
1 parent 7fe7f7a commit 0850b36

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎features/font-collection.feature‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Feature: Manage WordPress font collections
66
@require-wp-6.5
77
Scenario: Listing font collections
88
When I try `wp font collection list`
9-
Then the return code should be 0
9+
Then STDOUT should be a table containing rows:
10+
| slug | name | description |
11+
| google-fonts | Google Fonts | Install from Google Fonts. Fonts are copied to and served from your site. |
1012

1113
@require-wp-6.5
1214
Scenario: Getting a non-existent font collection
@@ -18,9 +20,11 @@ Feature: Manage WordPress font collections
1820
"""
1921

2022
@require-wp-6.5
21-
Scenario: Font collection commands require WordPress 6.5+
22-
Given a WP install
23-
When I try `wp font collection list`
23+
Scenario: Checking whether a font collection is registered
24+
When I try `wp font collection is-registered nonexistent-collection`
25+
Then the return code should be 1
26+
27+
When I run `wp font collection is-registered google-fonts`
2428
Then the return code should be 0
2529

2630
@less-than-wp-6.5

‎src/Font_Collection_Command.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,36 @@ public function get( $args, $assoc_args ) {
183183
$formatter->display_item( $data );
184184
}
185185

186+
/**
187+
* Checks if a font collection is registered.
188+
*
189+
* ## EXAMPLES
190+
*
191+
* # Bash script for checking if a font collection is registered, with fallback.
192+
*
193+
* if wp font collection is-registered google-fonts 2>/dev/null; then
194+
* # Font collection is registered. Do something.
195+
* else
196+
* # Fallback if collection is not registered.
197+
* fi
198+
*
199+
* @subcommand is-registered
200+
*
201+
* @param string[] $args Positional arguments. Unused.
202+
* @param array{network?: bool} $assoc_args Associative arguments.
203+
*/
204+
public function is_registered( $args, $assoc_args ) {
205+
$slug = $args[0];
206+
$font_library = WP_Font_Library::get_instance();
207+
$collection = $font_library->get_font_collection( $slug );
208+
209+
if ( ! $collection ) {
210+
WP_CLI::halt( 1 );
211+
}
212+
213+
WP_CLI::halt( 0 );
214+
}
215+
186216
private function get_formatter( &$assoc_args ) {
187217
return new Formatter( $assoc_args, $this->fields, 'font-collection' );
188218
}

0 commit comments

Comments
 (0)