3232 */
3333class Font_Collection_Command extends WP_CLI_Command {
3434
35- private $ fields = array (
35+ private $ fields = [
3636 'slug ' ,
3737 'name ' ,
3838 'description ' ,
39- );
40-
41- /**
42- * Gets a safe value from collection data array.
43- *
44- * @param mixed $data Collection data.
45- * @param string $key Key to retrieve.
46- * @return string Value from data or empty string.
47- */
48- private function get_safe_value ( $ data , $ key ) {
49- return is_array ( $ data ) && isset ( $ data [ $ key ] ) ? $ data [ $ key ] : '' ;
50- }
39+ ];
5140
5241 /**
5342 * Lists registered font collections.
@@ -79,6 +68,7 @@ private function get_safe_value( $data, $key ) {
7968 * * slug
8069 * * name
8170 * * description
71+ * * categories
8272 *
8373 * ## EXAMPLES
8474 *
@@ -100,14 +90,36 @@ public function list_( $args, $assoc_args ) {
10090 $ font_library = WP_Font_Library::get_instance ();
10191 $ collections = $ font_library ->get_font_collections ();
10292
103- $ items = array ();
93+ $ items = [];
94+
95+ /**
96+ * @var \WP_Font_Collection $collection
97+ */
10498 foreach ( $ collections as $ collection ) {
105- $ data = $ collection ->get_data ();
106- $ items [] = array (
107- 'slug ' => $ collection ->slug ,
108- 'name ' => $ this ->get_safe_value ( $ data , 'name ' ),
109- 'description ' => $ this ->get_safe_value ( $ data , 'description ' ),
99+ $ data = $ collection ->get_data ();
100+
101+ if ( is_wp_error ( $ data ) ) {
102+ WP_CLI ::warning ( $ data );
103+ continue ;
104+ }
105+
106+ $ categories = $ data ['categories ' ] ?? [];
107+ $ categories = implode (
108+ ', ' ,
109+ array_map (
110+ static function ( $ category ) {
111+ return "{$ category ['name ' ]} ( {$ category ['slug ' ]}) " ;
112+ },
113+ $ categories
114+ )
110115 );
116+
117+ $ items [] = [
118+ 'slug ' => $ collection ->slug ,
119+ 'name ' => $ data ['name ' ] ?? '' ,
120+ 'description ' => $ data ['description ' ] ?? '' ,
121+ 'categories ' => $ categories ,
122+ ];
111123 }
112124
113125 $ formatter = $ this ->get_formatter ( $ assoc_args );
@@ -146,6 +158,8 @@ public function list_( $args, $assoc_args ) {
146158 * * slug
147159 * * name
148160 * * description
161+ * * categories
162+ * * font_families
149163 *
150164 * ## EXAMPLES
151165 *
@@ -173,12 +187,28 @@ public function get( $args, $assoc_args ) {
173187
174188 $ collection_data = $ collection ->get_data ();
175189
176- $ data = array (
177- 'slug ' => $ collection ->slug ,
178- 'name ' => $ this ->get_safe_value ( $ collection_data , 'name ' ),
179- 'description ' => $ this ->get_safe_value ( $ collection_data , 'description ' ),
190+ if ( is_wp_error ( $ collection_data ) ) {
191+ WP_CLI ::error ( $ collection_data );
192+ }
193+
194+ $ categories = $ collection_data ['categories ' ] ?? [];
195+ $ categories = implode (
196+ ', ' ,
197+ array_map (
198+ static function ( $ category ) {
199+ return "{$ category ['name ' ]} ( {$ category ['slug ' ]}) " ;
200+ },
201+ $ categories
202+ )
180203 );
181204
205+ $ data = [
206+ 'slug ' => $ collection ->slug ,
207+ 'name ' => $ collection_data ['name ' ] ?? '' ,
208+ 'description ' => $ collection_data ['name ' ] ?? '' ,
209+ 'categories ' => $ categories ,
210+ ];
211+
182212 $ formatter = $ this ->get_formatter ( $ assoc_args );
183213 $ formatter ->display_item ( $ data );
184214 }
@@ -275,44 +305,32 @@ public function list_families( $args, $assoc_args ) {
275305 WP_CLI ::error ( $ collection_data );
276306 }
277307
278- $ font_families = $ this -> get_safe_value ( $ collection_data, 'font_families ' ) ;
308+ $ font_families = $ collection_data[ 'font_families ' ] ?? [] ;
279309
280310 if ( empty ( $ font_families ) || ! is_array ( $ font_families ) ) {
281311 WP_CLI ::error ( 'No font families found in this collection. ' );
282312 }
283313
284- // Filter by category if specified.
285314 $ category = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'category ' );
286- if ( $ category ) {
287- $ filtered = array ();
288- foreach ( $ font_families as $ family ) {
289- if ( ! is_array ( $ family ) ) {
290- continue ;
291- }
292- $ categories = isset ( $ family ['category ' ] ) ? (array ) $ family ['category ' ] : array ();
293- if ( in_array ( $ category , $ categories , true ) ) {
294- $ filtered [] = $ family ;
295- }
296- }
297- $ font_families = $ filtered ;
298- }
299315
300- $ items = array () ;
316+ $ items = [] ;
301317 foreach ( $ font_families as $ family ) {
302- if ( ! is_array ( $ family ) ) {
318+ if ( $ category && ! in_array ( $ category , $ family[ ' categories ' ], true ) ) {
303319 continue ;
304320 }
305- $ category_list = isset ( $ family ['category ' ] ) ? (array ) $ family ['category ' ] : array ();
306- $ items [] = array (
307- 'slug ' => isset ( $ family ['slug ' ] ) ? $ family ['slug ' ] : '' ,
308- 'name ' => isset ( $ family ['name ' ] ) ? $ family ['name ' ] : '' ,
309- 'fontFamily ' => isset ( $ family ['fontFamily ' ] ) ? $ family ['fontFamily ' ] : '' ,
310- 'category ' => implode ( ', ' , $ category_list ),
311- 'preview ' => isset ( $ family ['preview ' ] ) ? $ family ['preview ' ] : '' ,
312- );
321+
322+ $ settings = $ family ['font_family_settings ' ];
323+
324+ $ items [] = [
325+ 'slug ' => $ settings ['slug ' ] ?? '' ,
326+ 'name ' => $ settings ['name ' ] ?? '' ,
327+ 'fontFamily ' => $ settings ['fontFamily ' ] ?? '' ,
328+ 'categories ' => implode ( ', ' , $ settings ['categories ' ] ?? [] ),
329+ 'preview ' => $ settings ['preview ' ] ?? '' ,
330+ ];
313331 }
314332
315- $ fields = array ( 'slug ' , 'name ' , 'fontFamily ' , 'category ' , 'preview ' ) ;
333+ $ fields = [ 'slug ' , 'name ' , 'fontFamily ' , 'categories ' , 'preview ' ] ;
316334 $ formatter = new Formatter ( $ assoc_args , $ fields , 'font-family ' );
317335 $ formatter ->display_items ( $ items );
318336 }
@@ -376,23 +394,15 @@ public function list_categories( $args, $assoc_args ) {
376394 WP_CLI ::error ( $ collection_data );
377395 }
378396
379- $ categories = $ this -> get_safe_value ( $ collection_data, 'categories ' ) ;
397+ $ categories = $ collection_data[ 'categories ' ] ;
380398
381399 if ( empty ( $ categories ) || ! is_array ( $ categories ) ) {
382400 WP_CLI ::error ( 'No categories found in this collection. ' );
383401 }
384402
385- $ items = array ();
386- foreach ( $ categories as $ category ) {
387- $ items [] = array (
388- 'slug ' => isset ( $ category ['slug ' ] ) ? $ category ['slug ' ] : '' ,
389- 'name ' => isset ( $ category ['name ' ] ) ? $ category ['name ' ] : '' ,
390- );
391- }
392-
393- $ fields = array ( 'slug ' , 'name ' );
403+ $ fields = [ 'slug ' , 'name ' ];
394404 $ formatter = new Formatter ( $ assoc_args , $ fields , 'category ' );
395- $ formatter ->display_items ( $ items );
405+ $ formatter ->display_items ( $ categories );
396406 }
397407
398408 private function get_formatter ( &$ assoc_args ) {
0 commit comments