Skip to content

Commit 9a2bfbb

Browse files
Copilotswissspidy
andcommitted
Changes before error encountered
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 0850b36 commit 9a2bfbb

4 files changed

Lines changed: 422 additions & 0 deletions

File tree

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,22 @@
6666
"font",
6767
"font collection",
6868
"font collection get",
69+
"font collection is-registered",
6970
"font collection list",
71+
"font collection list-categories",
72+
"font collection list-families",
7073
"font face",
7174
"font face create",
7275
"font face delete",
7376
"font face get",
77+
"font face install",
7478
"font face list",
7579
"font face update",
7680
"font family",
7781
"font family create",
7882
"font family delete",
7983
"font family get",
84+
"font family install",
8085
"font family list",
8186
"font family update",
8287
"menu",

src/Font_Collection_Command.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,181 @@ public function is_registered( $args, $assoc_args ) {
213213
WP_CLI::halt( 0 );
214214
}
215215

216+
/**
217+
* Lists font families in a collection.
218+
*
219+
* ## OPTIONS
220+
*
221+
* <slug>
222+
* : Font collection slug.
223+
*
224+
* [--category=<slug>]
225+
* : Filter by category slug.
226+
*
227+
* [--field=<field>]
228+
* : Prints the value of a single field for each family.
229+
*
230+
* [--fields=<fields>]
231+
* : Limit the output to specific family fields.
232+
*
233+
* [--format=<format>]
234+
* : Render output in a particular format.
235+
* ---
236+
* default: table
237+
* options:
238+
* - table
239+
* - csv
240+
* - json
241+
* - count
242+
* - yaml
243+
* ---
244+
*
245+
* ## AVAILABLE FIELDS
246+
*
247+
* * slug
248+
* * name
249+
* * fontFamily
250+
* * category
251+
* * preview
252+
*
253+
* ## EXAMPLES
254+
*
255+
* # List all font families in a collection
256+
* $ wp font collection list-families google-fonts
257+
*
258+
* # List font families in a specific category
259+
* $ wp font collection list-families google-fonts --category=sans-serif
260+
*
261+
* @subcommand list-families
262+
*/
263+
public function list_families( $args, $assoc_args ) {
264+
$slug = $args[0];
265+
$font_library = WP_Font_Library::get_instance();
266+
$collection = $font_library->get_font_collection( $slug );
267+
268+
if ( ! $collection ) {
269+
WP_CLI::error( "Font collection {$slug} doesn't exist." );
270+
}
271+
272+
$collection_data = $collection->get_data();
273+
274+
if ( is_wp_error( $collection_data ) ) {
275+
WP_CLI::error( $collection_data );
276+
}
277+
278+
$font_families = $this->get_safe_value( $collection_data, 'font_families' );
279+
280+
if ( empty( $font_families ) || ! is_array( $font_families ) ) {
281+
WP_CLI::error( 'No font families found in this collection.' );
282+
}
283+
284+
// Filter by category if specified.
285+
$category = \WP_CLI\Utils\get_flag_value( $assoc_args, 'category' );
286+
if ( $category ) {
287+
$font_families = array_filter(
288+
$font_families,
289+
function ( $family ) use ( $category ) {
290+
$categories = isset( $family['category'] ) ? (array) $family['category'] : array();
291+
return in_array( $category, $categories, true );
292+
}
293+
);
294+
}
295+
296+
$items = array();
297+
foreach ( $font_families as $family ) {
298+
$category_list = isset( $family['category'] ) ? (array) $family['category'] : array();
299+
$items[] = array(
300+
'slug' => isset( $family['slug'] ) ? $family['slug'] : '',
301+
'name' => isset( $family['name'] ) ? $family['name'] : '',
302+
'fontFamily' => isset( $family['fontFamily'] ) ? $family['fontFamily'] : '',
303+
'category' => implode( ', ', $category_list ),
304+
'preview' => isset( $family['preview'] ) ? $family['preview'] : '',
305+
);
306+
}
307+
308+
$fields = array( 'slug', 'name', 'fontFamily', 'category', 'preview' );
309+
$formatter = new Formatter( $assoc_args, $fields, 'font-family' );
310+
$formatter->display_items( $items );
311+
}
312+
313+
/**
314+
* Lists categories in a collection.
315+
*
316+
* ## OPTIONS
317+
*
318+
* <slug>
319+
* : Font collection slug.
320+
*
321+
* [--field=<field>]
322+
* : Prints the value of a single field for each category.
323+
*
324+
* [--fields=<fields>]
325+
* : Limit the output to specific category fields.
326+
*
327+
* [--format=<format>]
328+
* : Render output in a particular format.
329+
* ---
330+
* default: table
331+
* options:
332+
* - table
333+
* - csv
334+
* - json
335+
* - count
336+
* - yaml
337+
* ---
338+
*
339+
* ## AVAILABLE FIELDS
340+
*
341+
* * slug
342+
* * name
343+
*
344+
* ## EXAMPLES
345+
*
346+
* # List all categories in a collection
347+
* $ wp font collection list-categories google-fonts
348+
* +-------------+--------------+
349+
* | slug | name |
350+
* +-------------+--------------+
351+
* | sans-serif | Sans Serif |
352+
* | display | Display |
353+
* +-------------+--------------+
354+
*
355+
* @subcommand list-categories
356+
*/
357+
public function list_categories( $args, $assoc_args ) {
358+
$slug = $args[0];
359+
$font_library = WP_Font_Library::get_instance();
360+
$collection = $font_library->get_font_collection( $slug );
361+
362+
if ( ! $collection ) {
363+
WP_CLI::error( "Font collection {$slug} doesn't exist." );
364+
}
365+
366+
$collection_data = $collection->get_data();
367+
368+
if ( is_wp_error( $collection_data ) ) {
369+
WP_CLI::error( $collection_data );
370+
}
371+
372+
$categories = $this->get_safe_value( $collection_data, 'categories' );
373+
374+
if ( empty( $categories ) || ! is_array( $categories ) ) {
375+
WP_CLI::error( 'No categories found in this collection.' );
376+
}
377+
378+
$items = array();
379+
foreach ( $categories as $category ) {
380+
$items[] = array(
381+
'slug' => isset( $category['slug'] ) ? $category['slug'] : '',
382+
'name' => isset( $category['name'] ) ? $category['name'] : '',
383+
);
384+
}
385+
386+
$fields = array( 'slug', 'name' );
387+
$formatter = new Formatter( $assoc_args, $fields, 'category' );
388+
$formatter->display_items( $items );
389+
}
390+
216391
private function get_formatter( &$assoc_args ) {
217392
return new Formatter( $assoc_args, $this->fields, 'font-collection' );
218393
}

src/Font_Face_Command.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,107 @@ public function delete( $args, $assoc_args ) {
381381
Utils\report_batch_operation_results( 'font face', 'delete', count( $args ), $count, $errors );
382382
}
383383

384+
/**
385+
* Installs a font face.
386+
*
387+
* Creates a new font face post with the specified settings.
388+
*
389+
* ## OPTIONS
390+
*
391+
* <family-id>
392+
* : Font family ID.
393+
*
394+
* --src=<src>
395+
* : Font face source URL or file path.
396+
*
397+
* [--font-family=<family>]
398+
* : CSS font-family value.
399+
*
400+
* [--font-style=<style>]
401+
* : CSS font-style value (e.g., normal, italic).
402+
* ---
403+
* default: normal
404+
* ---
405+
*
406+
* [--font-weight=<weight>]
407+
* : CSS font-weight value (e.g., 400, 700).
408+
* ---
409+
* default: 400
410+
* ---
411+
*
412+
* [--font-display=<display>]
413+
* : CSS font-display value.
414+
*
415+
* [--porcelain]
416+
* : Output just the new font face ID.
417+
*
418+
* ## EXAMPLES
419+
*
420+
* # Install a font face
421+
* $ wp font face install 42 --src="https://example.com/font.woff2" --font-weight=700 --font-style=normal
422+
* Success: Created font face 43.
423+
*
424+
* # Install a font face with porcelain output
425+
* $ wp font face install 42 --src="font.woff2" --porcelain
426+
* 44
427+
*/
428+
public function install( $args, $assoc_args ) {
429+
$family_id = $args[0];
430+
431+
// Verify parent font family exists.
432+
$parent_post = get_post( $family_id );
433+
if ( ! $parent_post || 'wp_font_family' !== $parent_post->post_type ) {
434+
WP_CLI::error( "Font family {$family_id} doesn't exist." );
435+
}
436+
437+
if ( ! isset( $assoc_args['src'] ) ) {
438+
WP_CLI::error( 'The --src parameter is required.' );
439+
}
440+
441+
// Prepare font face settings.
442+
$face_settings = array(
443+
'src' => $assoc_args['src'],
444+
);
445+
446+
if ( isset( $assoc_args['font-family'] ) ) {
447+
$face_settings['fontFamily'] = $assoc_args['font-family'];
448+
}
449+
450+
$font_style = isset( $assoc_args['font-style'] ) ? $assoc_args['font-style'] : 'normal';
451+
$face_settings['fontStyle'] = $font_style;
452+
453+
$font_weight = isset( $assoc_args['font-weight'] ) ? $assoc_args['font-weight'] : '400';
454+
$face_settings['fontWeight'] = $font_weight;
455+
456+
if ( isset( $assoc_args['font-display'] ) ) {
457+
$face_settings['fontDisplay'] = $assoc_args['font-display'];
458+
}
459+
460+
// Generate title.
461+
$title_parts = array( $font_weight, $font_style );
462+
$title = implode( ' ', $title_parts );
463+
464+
$post_data = array(
465+
'post_type' => 'wp_font_face',
466+
'post_parent' => $family_id,
467+
'post_title' => $title,
468+
'post_status' => 'publish',
469+
'post_content' => wp_json_encode( $face_settings ),
470+
);
471+
472+
$font_face_id = wp_insert_post( $post_data, true );
473+
474+
if ( is_wp_error( $font_face_id ) ) {
475+
WP_CLI::error( $font_face_id );
476+
}
477+
478+
if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
479+
WP_CLI::line( (string) $font_face_id );
480+
} else {
481+
WP_CLI::success( "Created font face {$font_face_id}." );
482+
}
483+
}
484+
384485
private function get_formatter( &$assoc_args ) {
385486
return new Formatter( $assoc_args, $this->fields, 'font-face' );
386487
}

0 commit comments

Comments
 (0)