Skip to content

Commit d58d4fe

Browse files
swissspidyclaude
andauthored
Add wp icon and wp icon collection commands (#648)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent acd3beb commit d58d4fe

8 files changed

Lines changed: 1007 additions & 0 deletions

‎composer.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@
7474
"font face install",
7575
"font family",
7676
"font family install",
77+
"icon",
78+
"icon collection",
79+
"icon collection get",
80+
"icon collection is-registered",
81+
"icon collection list",
82+
"icon get",
83+
"icon is-registered",
84+
"icon list",
85+
"icon render",
7786
"menu",
7887
"menu create",
7988
"menu delete",

‎entity-command.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@
140140
WP_CLI::add_command( 'font collection', 'Font_Collection_Command', $wpcli_entity_font_version_check );
141141
WP_CLI::add_command( 'font family', 'Font_Family_Command', $wpcli_entity_font_version_check );
142142
WP_CLI::add_command( 'font face', 'Font_Face_Command', $wpcli_entity_font_version_check );
143+
144+
$wpcli_entity_icon_version_check = array(
145+
'before_invoke' => function () {
146+
if ( Utils\wp_version_compare( '7.1', '<' ) ) {
147+
WP_CLI::error( 'Requires WordPress 7.1 or greater.' );
148+
}
149+
},
150+
);
151+
152+
WP_CLI::add_command( 'icon', 'Icon_Command', $wpcli_entity_icon_version_check );
153+
WP_CLI::add_command( 'icon collection', 'Icon_Collection_Command', $wpcli_entity_icon_version_check );

‎features/icon-collection.feature‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
Feature: Manage WordPress icon collections
2+
3+
Background:
4+
Given a WP install
5+
6+
@require-wp-7.1
7+
Scenario: Listing icon collections
8+
When I run `wp icon collection list --fields=slug,label,description`
9+
Then STDOUT should be a table containing rows:
10+
| slug | label | description |
11+
| core | WordPress | Default icon collection. |
12+
13+
@require-wp-7.1
14+
Scenario: Listing icon collections in other formats
15+
When I run `wp icon collection list --format=ids`
16+
Then STDOUT should contain:
17+
"""
18+
core
19+
"""
20+
21+
When I run `wp icon collection list --fields=slug,label --format=json`
22+
Then STDOUT should be JSON containing:
23+
"""
24+
[{"slug":"core","label":"WordPress"}]
25+
"""
26+
27+
When I run `wp icon collection list --format=count`
28+
Then STDOUT should be a number
29+
30+
@require-wp-7.1
31+
Scenario: Getting an icon collection
32+
When I run `wp icon collection get core --fields=slug,label,description`
33+
Then STDOUT should be a table containing rows:
34+
| Field | Value |
35+
| slug | core |
36+
| label | WordPress |
37+
| description | Default icon collection. |
38+
39+
When I run `wp icon collection get core --field=label`
40+
Then STDOUT should be:
41+
"""
42+
WordPress
43+
"""
44+
45+
@require-wp-7.1
46+
Scenario: Counting the icons of a collection
47+
When I run `wp icon collection get core --field=count`
48+
Then STDOUT should be a number
49+
50+
@require-wp-7.1
51+
Scenario: Getting a non-existent icon collection
52+
When I try `wp icon collection get nonexistent-collection`
53+
Then the return code should be 1
54+
And STDERR should contain:
55+
"""
56+
doesn't exist
57+
"""
58+
59+
@require-wp-7.1
60+
Scenario: Checking whether an icon collection is registered
61+
When I try `wp icon collection is-registered nonexistent-collection`
62+
Then the return code should be 1
63+
64+
When I run `wp icon collection is-registered core`
65+
Then the return code should be 0
66+
67+
@require-wp-7.1
68+
Scenario: Listing a collection registered by a plugin
69+
Given a wp-content/mu-plugins/test-icons.php file:
70+
"""
71+
<?php
72+
// Plugin Name: Test Icons
73+
add_action(
74+
'init',
75+
function () {
76+
wp_register_icon_collection(
77+
'test-icons',
78+
array(
79+
'label' => 'Test Icons',
80+
'description' => 'A collection for testing.',
81+
)
82+
);
83+
84+
wp_register_icon(
85+
'test-icons/square',
86+
array(
87+
'label' => 'Square',
88+
'content' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 4h16v16H4z" /></svg>',
89+
)
90+
);
91+
}
92+
);
93+
"""
94+
95+
When I run `wp icon collection list --fields=slug,label,description,count`
96+
Then STDOUT should be a table containing rows:
97+
| slug | label | description | count |
98+
| test-icons | Test Icons | A collection for testing. | 1 |
99+
100+
When I run `wp icon collection is-registered test-icons`
101+
Then the return code should be 0
102+
103+
@less-than-wp-7.1
104+
Scenario: Icon collection commands fail on WordPress < 7.1
105+
When I try `wp icon collection list`
106+
Then the return code should be 1
107+
And STDERR should contain:
108+
"""
109+
Requires WordPress 7.1 or greater
110+
"""

‎features/icon.feature‎

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
Feature: Manage WordPress SVG icons
2+
3+
Background:
4+
Given a WP install
5+
6+
@require-wp-7.1
7+
Scenario: Listing icons
8+
When I run `wp icon list --collection=core --format=count`
9+
Then STDOUT should be a number
10+
11+
When I run `wp icon list --search=arrow-down --field=name`
12+
Then STDOUT should contain:
13+
"""
14+
core/arrow-down
15+
"""
16+
17+
When I run `wp icon list --format=ids`
18+
Then STDOUT should contain:
19+
"""
20+
core/plus
21+
"""
22+
23+
@require-wp-7.1
24+
Scenario: Listing icons matches names and labels
25+
When I run `wp icon list --search="Arrow Down" --fields=name,label,collection`
26+
Then STDOUT should be a table containing rows:
27+
| name | label | collection |
28+
| core/arrow-down | Arrow Down | core |
29+
30+
@require-wp-7.1
31+
Scenario: Listing icons of a non-existent collection
32+
When I try `wp icon list --collection=nonexistent-collection`
33+
Then the return code should be 1
34+
And STDERR should contain:
35+
"""
36+
doesn't exist
37+
"""
38+
39+
@require-wp-7.1
40+
Scenario: Getting an icon
41+
When I run `wp icon get core/plus --fields=name,label,collection`
42+
Then STDOUT should be a table containing rows:
43+
| Field | Value |
44+
| name | core/plus |
45+
| label | Plus |
46+
| collection | core |
47+
48+
When I run `wp icon get core/plus --field=content`
49+
Then STDOUT should contain:
50+
"""
51+
<svg
52+
"""
53+
54+
When I run `wp icon get core/plus --field=file_path`
55+
Then STDOUT should contain:
56+
"""
57+
wp-includes/images/icon-library/plus.svg
58+
"""
59+
60+
@require-wp-7.1
61+
Scenario: Getting a non-existent icon
62+
When I try `wp icon get core/nonexistent-icon`
63+
Then the return code should be 1
64+
And STDERR should contain:
65+
"""
66+
doesn't exist
67+
"""
68+
69+
@require-wp-7.1
70+
Scenario: Checking whether an icon is registered
71+
When I try `wp icon is-registered core/nonexistent-icon`
72+
Then the return code should be 1
73+
74+
When I run `wp icon is-registered core/plus`
75+
Then the return code should be 0
76+
77+
@require-wp-7.1
78+
Scenario: Rendering an icon
79+
When I run `wp icon render core/plus`
80+
Then STDOUT should contain:
81+
"""
82+
width="24"
83+
"""
84+
And STDOUT should contain:
85+
"""
86+
height="24"
87+
"""
88+
And STDOUT should contain:
89+
"""
90+
aria-hidden="true"
91+
"""
92+
93+
@require-wp-7.1
94+
Scenario: Rendering an icon with a size, class and label
95+
When I run `wp icon render core/plus --size=48 --class=my-icon --label="Add item"`
96+
Then STDOUT should contain:
97+
"""
98+
width="48"
99+
"""
100+
And STDOUT should contain:
101+
"""
102+
class="my-icon"
103+
"""
104+
And STDOUT should contain:
105+
"""
106+
role="img"
107+
"""
108+
And STDOUT should contain:
109+
"""
110+
aria-label="Add item"
111+
"""
112+
And STDOUT should not contain:
113+
"""
114+
aria-hidden
115+
"""
116+
117+
@require-wp-7.1
118+
Scenario: Rendering an icon at its intrinsic size
119+
When I run `wp icon render core/plus --size=none`
120+
Then STDOUT should contain:
121+
"""
122+
<svg
123+
"""
124+
And STDOUT should not contain:
125+
"""
126+
width=
127+
"""
128+
129+
@require-wp-7.1
130+
Scenario: Rendering an icon with an invalid size
131+
When I try `wp icon render core/plus --size=huge`
132+
Then the return code should be 1
133+
And STDERR should contain:
134+
"""
135+
The --size argument must be a positive integer or "none".
136+
"""
137+
138+
@require-wp-7.1
139+
Scenario: Rendering a non-existent icon
140+
When I try `wp icon render core/nonexistent-icon`
141+
Then the return code should be 1
142+
And STDERR should contain:
143+
"""
144+
doesn't exist
145+
"""
146+
147+
@require-wp-7.1
148+
Scenario: Managing icons registered by a plugin
149+
Given a wp-content/mu-plugins/test-icons.php file:
150+
"""
151+
<?php
152+
// Plugin Name: Test Icons
153+
add_action(
154+
'init',
155+
function () {
156+
wp_register_icon_collection(
157+
'test-icons',
158+
array(
159+
'label' => 'Test Icons',
160+
'description' => 'A collection for testing.',
161+
)
162+
);
163+
164+
wp_register_icon(
165+
'test-icons/square',
166+
array(
167+
'label' => 'Square',
168+
'content' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 4h16v16H4z" /></svg>',
169+
)
170+
);
171+
}
172+
);
173+
"""
174+
175+
When I run `wp icon list --collection=test-icons --fields=name,label,collection`
176+
Then STDOUT should be a table containing rows:
177+
| name | label | collection |
178+
| test-icons/square | Square | test-icons |
179+
180+
When I run `wp icon get test-icons/square --field=content`
181+
Then STDOUT should contain:
182+
"""
183+
M4 4h16v16H4z
184+
"""
185+
186+
When I run `wp icon get test-icons/square --field=file_path`
187+
Then STDOUT should not contain:
188+
"""
189+
.svg
190+
"""
191+
192+
When I run `wp icon render test-icons/square --size=16`
193+
Then STDOUT should contain:
194+
"""
195+
width="16"
196+
"""
197+
198+
@less-than-wp-7.1
199+
Scenario: Icon commands fail on WordPress < 7.1
200+
When I try `wp icon list`
201+
Then the return code should be 1
202+
And STDERR should contain:
203+
"""
204+
Requires WordPress 7.1 or greater
205+
"""

‎phpcs.xml.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<exclude-pattern>*/src/Comment(_Meta)?_Command\.php$</exclude-pattern>
6464
<exclude-pattern>*/src/Font(_Collection|_Face|_Family)?_Command\.php$</exclude-pattern>
6565
<exclude-pattern>*/src/Font_Namespace\.php$</exclude-pattern>
66+
<exclude-pattern>*/src/Icon(_Collection)?_Command\.php$</exclude-pattern>
6667
<exclude-pattern>*/src/Menu(_Item|_Location)?_Command\.php$</exclude-pattern>
6768
<exclude-pattern>*/src/Network_Meta_Command\.php$</exclude-pattern>
6869
<exclude-pattern>*/src/Network_Namespace\.php$</exclude-pattern>

0 commit comments

Comments
 (0)