Pass the taxonomy around to relevant nav menu actions to avoid arbitrarily deleting all items with menu-item-type
of taxonomy
. Adds unit test for wp_get_associated_nav_menu_items()
.
Props garyc40, SergeyBiryukov. Fixes #15264. git-svn-id: https://develop.svn.wordpress.org/trunk@25163 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
a4a765c5fd
commit
9e96365b8e
@ -262,7 +262,7 @@ add_action( 'welcome_panel', 'wp_welcome_panel'
|
||||
|
||||
// Navigation menu actions
|
||||
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
||||
add_action( 'delete_term', '_wp_delete_tax_menu_item' );
|
||||
add_action( 'delete_term', '_wp_delete_tax_menu_item', 10, 3 );
|
||||
add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
|
||||
|
||||
// Post Thumbnail CSS class filtering
|
||||
|
@ -685,9 +685,10 @@ function wp_setup_nav_menu_item( $menu_item ) {
|
||||
*
|
||||
* @param int $object_id The ID of the original object.
|
||||
* @param string $object_type The type of object, such as "taxonomy" or "post_type."
|
||||
* @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
|
||||
* @return array The array of menu item IDs; empty array if none;
|
||||
*/
|
||||
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type' ) {
|
||||
function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
|
||||
$object_id = (int) $object_id;
|
||||
$menu_item_ids = array();
|
||||
|
||||
@ -703,7 +704,8 @@ function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_
|
||||
);
|
||||
foreach( (array) $menu_items as $menu_item ) {
|
||||
if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
|
||||
if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) != $object_type )
|
||||
if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) !== $object_type ||
|
||||
get_post_meta( $menu_item->ID, '_menu_item_object', true ) !== $taxonomy )
|
||||
continue;
|
||||
|
||||
$menu_item_ids[] = (int) $menu_item->ID;
|
||||
@ -741,10 +743,10 @@ function _wp_delete_post_menu_item( $object_id = 0 ) {
|
||||
* @param int $object_id The ID of the original object being trashed.
|
||||
*
|
||||
*/
|
||||
function _wp_delete_tax_menu_item( $object_id = 0 ) {
|
||||
function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
|
||||
$object_id = (int) $object_id;
|
||||
|
||||
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy' );
|
||||
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
|
||||
|
||||
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||
wp_delete_post( $menu_item_id, true );
|
||||
|
41
tests/tests/post/nav-menu.php
Normal file
41
tests/tests/post/nav-menu.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @group post
|
||||
* @group navmenus
|
||||
*/
|
||||
class Test_Nav_Menus extends WP_UnitTestCase {
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $menu_id;
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->menu_id = wp_create_nav_menu( rand_str() );
|
||||
}
|
||||
|
||||
function test_wp_get_associated_nav_menu_items() {
|
||||
$tag_id = $this->factory->tag->create();
|
||||
$cat_id = $this->factory->category->create();
|
||||
|
||||
$tag_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
|
||||
'menu-item-type' => 'taxonomy',
|
||||
'menu-item-object' => 'post_tag',
|
||||
'menu-item-object-id' => $tag_id,
|
||||
'menu-item-status' => 'publish'
|
||||
) );
|
||||
|
||||
$cat_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
|
||||
'menu-item-type' => 'taxonomy',
|
||||
'menu-item-object' => 'category',
|
||||
'menu-item-object-id' => $cat_id,
|
||||
'menu-item-status' => 'publish'
|
||||
) );
|
||||
|
||||
$tag_items = wp_get_associated_nav_menu_items( $tag_id, 'taxonomy', 'post_tag' );
|
||||
$this->assertEqualSets( array( $tag_insert ), $tag_items );
|
||||
$cat_items = wp_get_associated_nav_menu_items( $cat_id, 'taxonomy', 'category' );
|
||||
$this->assertEqualSets( array( $cat_insert ), $cat_items );
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user