Skip to content

Commit b241a6a

Browse files
authored
Merge branch 'main' into copilot/allow-delete-site-with-url
2 parents 47d921e + c764923 commit b241a6a

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,79 @@ wp menu item delete <db-id>...
11301130

11311131

11321132

1133+
### wp menu item get
1134+
1135+
Gets details about a menu item.
1136+
1137+
~~~
1138+
wp menu item get <db-id> [--field=<field>] [--fields=<fields>] [--format=<format>]
1139+
~~~
1140+
1141+
**OPTIONS**
1142+
1143+
<db-id>
1144+
Database ID for the menu item.
1145+
1146+
[--field=<field>]
1147+
Instead of returning the whole menu item, returns the value of a single field.
1148+
1149+
[--fields=<fields>]
1150+
Limit the output to specific fields. Defaults to db_id, type, title, link, position.
1151+
1152+
[--format=<format>]
1153+
Render output in a particular format.
1154+
---
1155+
default: table
1156+
options:
1157+
- table
1158+
- csv
1159+
- json
1160+
- yaml
1161+
---
1162+
1163+
**AVAILABLE FIELDS**
1164+
1165+
These fields are available:
1166+
1167+
* db_id
1168+
* type
1169+
* title
1170+
* link
1171+
* position
1172+
* menu_item_parent
1173+
* object_id
1174+
* object
1175+
* type_label
1176+
* target
1177+
* attr_title
1178+
* description
1179+
* classes
1180+
* xfn
1181+
1182+
**EXAMPLES**
1183+
1184+
# Get details about a menu item with ID 45
1185+
$ wp menu item get 45
1186+
+-------------+----------------------------------+
1187+
| Field | Value |
1188+
+-------------+----------------------------------+
1189+
| db_id | 45 |
1190+
| type | custom |
1191+
| title | WordPress |
1192+
| link | https://wordpress.org |
1193+
| position | 1 |
1194+
+-------------+----------------------------------+
1195+
1196+
# Get a specific field from a menu item
1197+
$ wp menu item get 45 --field=title
1198+
WordPress
1199+
1200+
# Get menu item data in JSON format
1201+
$ wp menu item get 45 --format=json
1202+
{"db_id":45,"type":"custom","title":"WordPress","link":"https://wordpress.org","position":1}
1203+
1204+
1205+
11331206
### wp menu item list
11341207

11351208
Gets a list of items associated with a menu.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"menu item add-post",
7272
"menu item add-term",
7373
"menu item delete",
74+
"menu item get",
7475
"menu item list",
7576
"menu item update",
7677
"menu list",

features/menu-item.feature

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,61 @@ Feature: Manage WordPress menu items
194194
| type | title | position | link |
195195
| custom | First | 1 | https://first.com |
196196
| custom | Third | 2 | https://third.com |
197+
198+
Scenario: Get menu item details
199+
When I run `wp menu create "Sidebar Menu"`
200+
Then STDOUT should not be empty
201+
202+
When I run `wp menu item add-custom sidebar-menu Apple https://apple.com --porcelain`
203+
Then save STDOUT as {ITEM_ID}
204+
205+
When I run `wp menu item get {ITEM_ID}`
206+
Then STDOUT should be a table containing rows:
207+
| Field | Value |
208+
| db_id | {ITEM_ID} |
209+
| type | custom |
210+
| title | Apple |
211+
| link | https://apple.com |
212+
| position | 1 |
213+
214+
When I run `wp menu item get {ITEM_ID} --format=json`
215+
Then STDOUT should be JSON containing:
216+
"""
217+
{
218+
"db_id": {ITEM_ID},
219+
"type": "custom",
220+
"title": "Apple",
221+
"link": "https://apple.com"
222+
}
223+
"""
224+
225+
When I run `wp menu item get {ITEM_ID} --field=title`
226+
Then STDOUT should be:
227+
"""
228+
Apple
229+
"""
230+
231+
When I run `wp menu item get {ITEM_ID} --fields=db_id,title,type --format=csv`
232+
Then STDOUT should be CSV containing:
233+
| Field | Value |
234+
| db_id | {ITEM_ID} |
235+
| title | Apple |
236+
| type | custom |
237+
238+
When I try `wp menu item get 99999999`
239+
Then STDERR should be:
240+
"""
241+
Error: Invalid menu item.
242+
"""
243+
And the return code should be 1
244+
245+
When I run `wp post create --post_title='Test Post' --porcelain`
246+
Then save STDOUT as {POST_ID}
247+
248+
When I try `wp menu item get {POST_ID}`
249+
Then STDERR should be:
250+
"""
251+
Error: Invalid menu item.
252+
"""
253+
And the return code should be 1
254+

src/Menu_Item_Command.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,95 @@ function ( $item ) {
120120
$formatter->display_items( $items );
121121
}
122122

123+
/**
124+
* Gets details about a menu item.
125+
*
126+
* ## OPTIONS
127+
*
128+
* <db-id>
129+
* : Database ID for the menu item.
130+
*
131+
* [--field=<field>]
132+
* : Instead of returning the whole menu item, returns the value of a single field.
133+
*
134+
* [--fields=<fields>]
135+
* : Limit the output to specific fields. Defaults to db_id, type, title, link, position.
136+
*
137+
* [--format=<format>]
138+
* : Render output in a particular format.
139+
* ---
140+
* default: table
141+
* options:
142+
* - table
143+
* - csv
144+
* - json
145+
* - yaml
146+
* ---
147+
*
148+
* ## AVAILABLE FIELDS
149+
*
150+
* These fields are available:
151+
*
152+
* * db_id
153+
* * type
154+
* * title
155+
* * link
156+
* * position
157+
* * menu_item_parent
158+
* * object_id
159+
* * object
160+
* * type_label
161+
* * target
162+
* * attr_title
163+
* * description
164+
* * classes
165+
* * xfn
166+
*
167+
* ## EXAMPLES
168+
*
169+
* # Get details about a menu item with ID 45
170+
* $ wp menu item get 45
171+
* +-------------+----------------------------------+
172+
* | Field | Value |
173+
* +-------------+----------------------------------+
174+
* | db_id | 45 |
175+
* | type | custom |
176+
* | title | WordPress |
177+
* | link | https://wordpress.org |
178+
* | position | 1 |
179+
* +-------------+----------------------------------+
180+
*
181+
* # Get a specific field from a menu item
182+
* $ wp menu item get 45 --field=title
183+
* WordPress
184+
*
185+
* # Get menu item data in JSON format
186+
* $ wp menu item get 45 --format=json
187+
* {"db_id":45,"type":"custom","title":"WordPress","link":"https://wordpress.org","position":1}
188+
*/
189+
public function get( $args, $assoc_args ) {
190+
191+
$db_id = $args[0];
192+
193+
$menu_item = get_post( $db_id );
194+
195+
if ( ! $menu_item || 'nav_menu_item' !== $menu_item->post_type ) {
196+
WP_CLI::error( 'Invalid menu item.' );
197+
}
198+
199+
/**
200+
* @var object{title: string, url: string, description: string, object: string, object_id: int, menu_item_parent: int, attr_title: string, target: string, classes: string[], xfn: string, type: string, type_label: string, menu_order: int, db_id: int, post_type: string}&\stdClass $menu_item
201+
*/
202+
$menu_item = wp_setup_nav_menu_item( $menu_item );
203+
204+
// Correct position inconsistency and protected `url` param in WP-CLI
205+
$menu_item->position = ( 0 === $menu_item->menu_order ) ? 1 : $menu_item->menu_order;
206+
$menu_item->link = $menu_item->url;
207+
208+
$formatter = $this->get_formatter( $assoc_args );
209+
$formatter->display_item( $menu_item );
210+
}
211+
123212
/**
124213
* Adds a post as a menu item.
125214
*

0 commit comments

Comments
 (0)