diff --git a/features/menu-item.feature b/features/menu-item.feature index 494e0dfd0..c5a040568 100644 --- a/features/menu-item.feature +++ b/features/menu-item.feature @@ -252,3 +252,27 @@ Feature: Manage WordPress menu items """ And the return code should be 1 + Scenario: Add a post type archive as a menu item + + When I run `wp menu create "Archive Menu"` + Then STDOUT should not be empty + + When I run `wp menu item add-post-type-archive archive-menu post --porcelain` + Then STDOUT should be a number + And save STDOUT as {ITEM_ID} + + When I run `wp menu item list archive-menu --fields=db_id,type,object` + Then STDOUT should be a table containing rows: + | db_id | type | object | + | {ITEM_ID} | post_type_archive | post | + + When I run `wp menu item get {ITEM_ID} --field=link` + Then STDOUT should not be empty + + When I try `wp menu item add-post-type-archive archive-menu invalidposttype` + Then STDERR should be: + """ + Error: Invalid post type. + """ + And the return code should be 1 + diff --git a/src/Menu_Item_Command.php b/src/Menu_Item_Command.php index 40bf6610b..94a1ea3f3 100644 --- a/src/Menu_Item_Command.php +++ b/src/Menu_Item_Command.php @@ -329,6 +329,69 @@ public function add_term( $args, $assoc_args ) { $this->add_or_update_item( 'add', 'taxonomy', $args, $assoc_args ); } + /** + * Adds a post type archive as a menu item. + * + * ## OPTIONS + * + * + * : The name, slug, or term ID for the menu. + * + * + * : Post type slug. + * + * [--title=] + * : Set a custom title for the menu item. + * + * [--link=<link>] + * : Set a custom url for the menu item. + * + * [--description=<description>] + * : Set a custom description for the menu item. + * + * [--attr-title=<attr-title>] + * : Set a custom title attribute for the menu item. + * + * [--target=<target>] + * : Set a custom link target for the menu item. + * + * [--classes=<classes>] + * : Set a custom link classes for the menu item. + * + * [--position=<position>] + * : Specify the position of this menu item. + * + * [--parent-id=<parent-id>] + * : Make this menu item a child of another menu item. + * + * [--porcelain] + * : Output just the new menu item id. + * + * ## EXAMPLES + * + * $ wp menu item add-post-type-archive sidebar-menu post + * Success: Menu item added. + * + * @subcommand add-post-type-archive + */ + public function add_post_type_archive( $args, $assoc_args ) { + + $assoc_args['object'] = $args[1]; + unset( $args[1] ); + + $post_type = $assoc_args['object']; + + if ( ! get_post_type_object( $post_type ) ) { + WP_CLI::error( 'Invalid post type.' ); + } + + if ( false === get_post_type_archive_link( $post_type ) ) { + WP_CLI::error( 'Post type does not have an archive.' ); + } + + $this->add_or_update_item( 'add', 'post_type_archive', $args, $assoc_args ); + } + /** * Adds a custom menu item. *