Skip to content

Commit c5a82a2

Browse files
authored
Merge pull request #570 from wp-cli/copilot/add-wp-cli-menu-item-get-command
Add `wp menu item get` command
2 parents e982b14 + 325a1c1 commit c5a82a2

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

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)