Skip to content

Commit b5ce8fe

Browse files
CopilotswissspidyCopilot
authored
Add wp menu item add-post-type-archive subcommand (#594)
* Initial plan * Add wp menu item add-post-type-archive command Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 1ff0b5b commit b5ce8fe

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

features/menu-item.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,27 @@ Feature: Manage WordPress menu items
252252
"""
253253
And the return code should be 1
254254

255+
Scenario: Add a post type archive as a menu item
256+
257+
When I run `wp menu create "Archive Menu"`
258+
Then STDOUT should not be empty
259+
260+
When I run `wp menu item add-post-type-archive archive-menu post --porcelain`
261+
Then STDOUT should be a number
262+
And save STDOUT as {ITEM_ID}
263+
264+
When I run `wp menu item list archive-menu --fields=db_id,type,object`
265+
Then STDOUT should be a table containing rows:
266+
| db_id | type | object |
267+
| {ITEM_ID} | post_type_archive | post |
268+
269+
When I run `wp menu item get {ITEM_ID} --field=link`
270+
Then STDOUT should not be empty
271+
272+
When I try `wp menu item add-post-type-archive archive-menu invalidposttype`
273+
Then STDERR should be:
274+
"""
275+
Error: Invalid post type.
276+
"""
277+
And the return code should be 1
278+

src/Menu_Item_Command.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,69 @@ public function add_term( $args, $assoc_args ) {
329329
$this->add_or_update_item( 'add', 'taxonomy', $args, $assoc_args );
330330
}
331331

332+
/**
333+
* Adds a post type archive as a menu item.
334+
*
335+
* ## OPTIONS
336+
*
337+
* <menu>
338+
* : The name, slug, or term ID for the menu.
339+
*
340+
* <post-type>
341+
* : Post type slug.
342+
*
343+
* [--title=<title>]
344+
* : Set a custom title for the menu item.
345+
*
346+
* [--link=<link>]
347+
* : Set a custom url for the menu item.
348+
*
349+
* [--description=<description>]
350+
* : Set a custom description for the menu item.
351+
*
352+
* [--attr-title=<attr-title>]
353+
* : Set a custom title attribute for the menu item.
354+
*
355+
* [--target=<target>]
356+
* : Set a custom link target for the menu item.
357+
*
358+
* [--classes=<classes>]
359+
* : Set a custom link classes for the menu item.
360+
*
361+
* [--position=<position>]
362+
* : Specify the position of this menu item.
363+
*
364+
* [--parent-id=<parent-id>]
365+
* : Make this menu item a child of another menu item.
366+
*
367+
* [--porcelain]
368+
* : Output just the new menu item id.
369+
*
370+
* ## EXAMPLES
371+
*
372+
* $ wp menu item add-post-type-archive sidebar-menu post
373+
* Success: Menu item added.
374+
*
375+
* @subcommand add-post-type-archive
376+
*/
377+
public function add_post_type_archive( $args, $assoc_args ) {
378+
379+
$assoc_args['object'] = $args[1];
380+
unset( $args[1] );
381+
382+
$post_type = $assoc_args['object'];
383+
384+
if ( ! get_post_type_object( $post_type ) ) {
385+
WP_CLI::error( 'Invalid post type.' );
386+
}
387+
388+
if ( false === get_post_type_archive_link( $post_type ) ) {
389+
WP_CLI::error( 'Post type does not have an archive.' );
390+
}
391+
392+
$this->add_or_update_item( 'add', 'post_type_archive', $args, $assoc_args );
393+
}
394+
332395
/**
333396
* Adds a custom menu item.
334397
*

0 commit comments

Comments
 (0)