-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathFont_Face_Command.php
More file actions
124 lines (109 loc) · 3.07 KB
/
Font_Face_Command.php
File metadata and controls
124 lines (109 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
use WP_CLI\Utils;
/**
* Manages font faces.
*
* To list, get, create, update or delete font faces, use `wp post` with
* `--post_type=wp_font_face`.
*
* ## EXAMPLES
*
* # Install a font face for an existing family
* $ wp font face install 42 --src="https://example.com/font.woff2" --font-weight=700
* Success: Created font face 43.
*
* # List installed font faces
* $ wp post list --post_type=wp_font_face
*
* @package wp-cli
*/
class Font_Face_Command extends WP_CLI_Command {
/**
* Installs a font face.
*
* Creates a new font face post with the specified settings.
*
* ## OPTIONS
*
* <family-id>
* : Font family ID.
*
* --src=<src>
* : Font face source URL or file path.
*
* [--font-family=<family>]
* : CSS font-family value.
*
* [--font-style=<style>]
* : CSS font-style value (e.g., normal, italic).
* ---
* default: normal
* ---
*
* [--font-weight=<weight>]
* : CSS font-weight value (e.g., 400, 700).
* ---
* default: 400
* ---
*
* [--font-display=<display>]
* : CSS font-display value.
*
* [--porcelain]
* : Output just the new font face ID.
*
* ## EXAMPLES
*
* # Install a font face
* $ wp font face install 42 --src="https://example.com/font.woff2" --font-weight=700 --font-style=normal
* Success: Created font face 43.
*
* # Install a font face with porcelain output
* $ wp font face install 42 --src="font.woff2" --porcelain
* 44
*/
public function install( $args, $assoc_args ) {
$family_id = $args[0];
// Verify parent font family exists.
$parent_post = get_post( $family_id );
if ( ! $parent_post || 'wp_font_family' !== $parent_post->post_type ) {
WP_CLI::error( "Font family {$family_id} doesn't exist." );
}
if ( ! isset( $assoc_args['src'] ) ) {
WP_CLI::error( 'missing --src parameter' );
}
// Prepare font face settings.
$face_settings = [
'src' => $assoc_args['src'],
];
if ( isset( $assoc_args['font-family'] ) ) {
$face_settings['fontFamily'] = $assoc_args['font-family'];
}
$font_style = Utils\get_flag_value( $assoc_args, 'font-style', 'normal' );
$face_settings['fontStyle'] = $font_style;
$font_weight = Utils\get_flag_value( $assoc_args, 'font-weight', '400' );
$face_settings['fontWeight'] = $font_weight;
if ( isset( $assoc_args['font-display'] ) ) {
$face_settings['fontDisplay'] = $assoc_args['font-display'];
}
// Generate title.
$title_parts = [ $font_weight, $font_style ];
$title = implode( ' ', $title_parts );
$post_data = [
'post_type' => 'wp_font_face',
'post_parent' => $family_id,
'post_title' => $title,
'post_status' => 'publish',
'post_content' => wp_json_encode( $face_settings ) ?: '{}',
];
$font_face_id = wp_insert_post( $post_data, true );
if ( is_wp_error( $font_face_id ) ) {
WP_CLI::error( $font_face_id );
}
if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( (string) $font_face_id );
} else {
WP_CLI::success( "Created font face {$font_face_id}." );
}
}
}