Skip to content

Commit 18282ba

Browse files
committed
Some manual fixes
1 parent 71c790c commit 18282ba

File tree

4 files changed

+199
-116
lines changed

4 files changed

+199
-116
lines changed

features/font-family.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ Feature: Manage WordPress font families
9090

9191
@require-wp-6.5
9292
Scenario: Installing a font family from a collection
93-
When I run `wp font family install google-fonts "dm sans" --porcelain`
93+
When I run `wp font family install google-fonts "roboto" --porcelain`
9494
Then STDOUT should be a number
9595
And save STDOUT as {FONT_FAMILY_ID}
9696

9797
When I run `wp font family get {FONT_FAMILY_ID} --field=post_title`
9898
Then STDOUT should contain:
9999
"""
100-
DM Sans
100+
Roboto
101101
"""
102102

103103
When I run `wp font face list --post_parent={FONT_FAMILY_ID} --format=count`

src/Font_Collection_Command.php

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,11 @@
3232
*/
3333
class 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 ) {

src/Font_Face_Command.php

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
*/
4141
class Font_Face_Command extends WP_CLI_Command {
4242

43-
private $fields = array(
43+
private $fields = [
4444
'ID',
45-
'post_title',
46-
'post_name',
47-
'post_parent',
48-
'post_status',
49-
'post_date',
50-
);
45+
'parent',
46+
'name',
47+
'fontFamily',
48+
'fontStyle',
49+
'fontWeight',
50+
'fontDisplay',
51+
];
5152

5253
/**
5354
* Lists font faces.
@@ -111,22 +112,45 @@ class Font_Face_Command extends WP_CLI_Command {
111112
public function list_( $args, $assoc_args ) {
112113
$formatter = $this->get_formatter( $assoc_args );
113114

114-
$query_args = array(
115+
$query_args = [
115116
'post_type' => 'wp_font_face',
116117
'posts_per_page' => -1,
117118
'orderby' => 'ID',
118119
'order' => 'ASC',
119-
);
120+
];
120121

121122
// Allow filtering by any WP_Query args.
122123
foreach ( $assoc_args as $key => $value ) {
123-
if ( ! in_array( $key, array( 'format', 'fields', 'field' ), true ) ) {
124+
if ( ! in_array( $key, [ 'format', 'fields', 'field' ], true ) ) {
124125
$query_args[ $key ] = $value;
125126
}
126127
}
127128

128129
$query = new WP_Query( $query_args );
129-
$items = $query->posts;
130+
131+
$items = array_map(
132+
static function ( $post ) {
133+
/**
134+
* @var \WP_Post $post
135+
*/
136+
137+
/**
138+
* @var array{fontFamily?: string, fontStyle?: string, fontWeight?: number, fontDisplay?: string} $settings_json
139+
*/
140+
$settings_json = json_decode( $post->post_content, true );
141+
142+
return [
143+
'ID' => $post->ID,
144+
'parent' => $post->post_parent,
145+
'name' => $post->post_title ?: '',
146+
'fontFamily' => $settings_json['fontFamily'] ?? '',
147+
'fontStyle' => $settings_json['fontStyle'] ?? '',
148+
'fontWeight' => $settings_json['fontWeight'] ?? '',
149+
'fontDisplay' => $settings_json['fontDisplay'] ?? 'fallback',
150+
];
151+
},
152+
$query->posts
153+
);
130154

131155
if ( 'ids' === $formatter->format ) {
132156
$items = wp_list_pluck( $items, 'ID' );
@@ -179,8 +203,23 @@ public function get( $args, $assoc_args ) {
179203
WP_CLI::error( "Font face {$font_face_id} doesn't exist." );
180204
}
181205

206+
/**
207+
* @var array{fontFamily?: string, fontStyle?: string, fontWeight?: number, fontDisplay?: string} $settings_json
208+
*/
209+
$settings_json = json_decode( $post->post_content, true );
210+
211+
$item = [
212+
'ID' => $post->ID,
213+
'parent' => $post->post_parent,
214+
'name' => $post->post_title ?: '',
215+
'fontFamily' => $settings_json['fontFamily'] ?? '',
216+
'fontStyle' => $settings_json['fontStyle'] ?? '',
217+
'fontWeight' => $settings_json['fontWeight'] ?? '',
218+
'fontDisplay' => $settings_json['fontDisplay'] ?? 'fallback',
219+
];
220+
182221
$formatter = $this->get_formatter( $assoc_args );
183-
$formatter->display_item( $post );
222+
$formatter->display_item( $item );
184223
}
185224

186225
/**
@@ -237,12 +276,12 @@ public function create( $args, $assoc_args ) {
237276
WP_CLI::error( "Font family {$assoc_args['post_parent']} doesn't exist." );
238277
}
239278

240-
$post_data = array(
279+
$post_data = [
241280
'post_type' => 'wp_font_face',
242281
'post_parent' => $assoc_args['post_parent'],
243282
'post_title' => $assoc_args['post_title'],
244283
'post_status' => isset( $assoc_args['post_status'] ) ? $assoc_args['post_status'] : 'publish',
245-
);
284+
];
246285

247286
if ( isset( $assoc_args['post_name'] ) ) {
248287
$post_data['post_name'] = $assoc_args['post_name'];
@@ -302,9 +341,9 @@ public function update( $args, $assoc_args ) {
302341
WP_CLI::error( "Font face {$font_face_id} doesn't exist." );
303342
}
304343

305-
$post_data = array(
344+
$post_data = [
306345
'ID' => $font_face_id,
307-
);
346+
];
308347

309348
// Verify parent font family if provided.
310349
if ( isset( $assoc_args['post_parent'] ) ) {
@@ -314,7 +353,7 @@ public function update( $args, $assoc_args ) {
314353
}
315354
}
316355

317-
$allowed_fields = array( 'post_parent', 'post_title', 'post_name', 'post_status', 'post_content' );
356+
$allowed_fields = [ 'post_parent', 'post_title', 'post_name', 'post_status', 'post_content' ];
318357
foreach ( $allowed_fields as $field ) {
319358
if ( isset( $assoc_args[ $field ] ) ) {
320359
$post_data[ $field ] = $assoc_args[ $field ];
@@ -439,9 +478,9 @@ public function install( $args, $assoc_args ) {
439478
}
440479

441480
// Prepare font face settings.
442-
$face_settings = array(
481+
$face_settings = [
443482
'src' => $assoc_args['src'],
444-
);
483+
];
445484

446485
if ( isset( $assoc_args['font-family'] ) ) {
447486
$face_settings['fontFamily'] = $assoc_args['font-family'];
@@ -458,16 +497,16 @@ public function install( $args, $assoc_args ) {
458497
}
459498

460499
// Generate title.
461-
$title_parts = array( $font_weight, $font_style );
500+
$title_parts = [ $font_weight, $font_style ];
462501
$title = implode( ' ', $title_parts );
463502

464-
$post_data = array(
503+
$post_data = [
465504
'post_type' => 'wp_font_face',
466505
'post_parent' => $family_id,
467506
'post_title' => $title,
468507
'post_status' => 'publish',
469508
'post_content' => wp_json_encode( $face_settings ) ?: '{}',
470-
);
509+
];
471510

472511
$font_face_id = wp_insert_post( $post_data, true );
473512

0 commit comments

Comments
 (0)