@@ -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 }
0 commit comments