Skip to content

Commit 4af0645

Browse files
committed
Ensure Description is respected in post type archive menu items.
Tested scenarios include: using the default (which is the post type description), Setting a custom description for that individual menu item, and setting a custom description that is blank. Introduced in r35382. Props Toro_Unit, mayukojpn, extendwings, jorbin. Fixes #35324. See #16075. git-svn-id: https://develop.svn.wordpress.org/trunk@36859 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e555666 commit 4af0645

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

src/wp-includes/nav-menu.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ function wp_setup_nav_menu_item( $menu_item ) {
767767
}
768768

769769
$menu_item->type_label = __( 'Post Type Archive' );
770-
$menu_item->description = '';
770+
$post_content = wp_trim_words( $menu_item->post_content, 200 );
771+
$post_type_description = '' == $post_content ? $object->description : $post_content;
771772
$menu_item->url = get_post_type_archive_link( $menu_item->object );
772773
} elseif ( 'taxonomy' == $menu_item->type ) {
773774
$object = get_taxonomy( $menu_item->object );

tests/phpunit/tests/post/nav-menu.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,82 @@ function test_orderby_name_by_default() {
180180

181181
$this->assertEquals( $nav_menus_names, $expected_nav_menus_names );
182182
}
183+
184+
/**
185+
* @ticket 35324
186+
*/
187+
function test_wp_setup_nav_menu_item_for_post_type_archive() {
188+
189+
$post_type_slug = rand_str( 12 );
190+
$post_type_description = rand_str();
191+
register_post_type( $post_type_slug ,array(
192+
'public' => true,
193+
'has_archive' => true,
194+
'description' => $post_type_description,
195+
'label' => $post_type_slug
196+
));
197+
198+
$post_type_archive_item_id = wp_update_nav_menu_item( $this->menu_id, 0, array(
199+
'menu-item-type' => 'post_type_archive',
200+
'menu-item-object' => $post_type_slug,
201+
'menu-item-description' => $post_type_description,
202+
'menu-item-status' => 'publish'
203+
) );
204+
$post_type_archive_item = wp_setup_nav_menu_item( get_post( $post_type_archive_item_id ) );
205+
206+
$this->assertEquals( $post_type_slug , $post_type_archive_item->title );
207+
$this->assertEquals( $post_type_description , $post_type_archive_item->description );
208+
}
209+
210+
/**
211+
* @ticket 35324
212+
*/
213+
function test_wp_setup_nav_menu_item_for_post_type_archive_no_description() {
214+
215+
$post_type_slug = rand_str( 12 );
216+
$post_type_description = '';
217+
register_post_type( $post_type_slug ,array(
218+
'public' => true,
219+
'has_archive' => true,
220+
'label' => $post_type_slug
221+
));
222+
223+
$post_type_archive_item_id = wp_update_nav_menu_item( $this->menu_id, 0, array(
224+
'menu-item-type' => 'post_type_archive',
225+
'menu-item-object' => $post_type_slug,
226+
'menu-item-status' => 'publish'
227+
) );
228+
$post_type_archive_item = wp_setup_nav_menu_item( get_post( $post_type_archive_item_id ) );
229+
230+
$this->assertEquals( $post_type_slug , $post_type_archive_item->title );
231+
$this->assertEquals( $post_type_description , $post_type_archive_item->description ); //fail!!!
232+
}
233+
234+
/**
235+
* @ticket 35324
236+
*/
237+
function test_wp_setup_nav_menu_item_for_post_type_archive_custom_description() {
238+
239+
$post_type_slug = rand_str( 12 );
240+
$post_type_description = rand_str();
241+
register_post_type( $post_type_slug ,array(
242+
'public' => true,
243+
'has_archive' => true,
244+
'description' => $post_type_description,
245+
'label' => $post_type_slug
246+
));
247+
248+
$menu_item_description = rand_str();
249+
250+
$post_type_archive_item_id = wp_update_nav_menu_item( $this->menu_id, 0, array(
251+
'menu-item-type' => 'post_type_archive',
252+
'menu-item-object' => $post_type_slug,
253+
'menu-item-description' => $menu_item_description,
254+
'menu-item-status' => 'publish'
255+
) );
256+
$post_type_archive_item = wp_setup_nav_menu_item( get_post( $post_type_archive_item_id ) );
257+
258+
$this->assertEquals( $post_type_slug , $post_type_archive_item->title );
259+
$this->assertEquals( $menu_item_description , $post_type_archive_item->description );
260+
}
183261
}

0 commit comments

Comments
 (0)