mirror of
https://github.com/WordPress/WordPress.git
synced 2024-12-22 17:18:32 +01:00
When posts/terms are deleted/trashed, update associated menu items. Deleted post = deleted menu item, trashed post = menu item becomes a draft. props filosofo, see #13174
git-svn-id: http://svn.automattic.com/wordpress/trunk@14295 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
7846ae85f6
commit
d1a37c1f31
@ -827,6 +827,8 @@ case 'add-menu-item' :
|
|||||||
$menu_id = (int) $_POST['menu'];
|
$menu_id = (int) $_POST['menu'];
|
||||||
if ( isset( $_POST['menu-item'] ) ) {
|
if ( isset( $_POST['menu-item'] ) ) {
|
||||||
$item_ids = wp_save_nav_menu_item( $menu_id, $_POST['menu-item'] );
|
$item_ids = wp_save_nav_menu_item( $menu_id, $_POST['menu-item'] );
|
||||||
|
if ( is_wp_error( $item_ids ) )
|
||||||
|
die('-1');
|
||||||
} else {
|
} else {
|
||||||
$item_ids = array();
|
$item_ids = array();
|
||||||
}
|
}
|
||||||
|
@ -808,7 +808,7 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
|
|||||||
*
|
*
|
||||||
* @param int $menu_id The menu ID for which to save this item.
|
* @param int $menu_id The menu ID for which to save this item.
|
||||||
* @param array $menu_data The unsanitized posted menu item data.
|
* @param array $menu_data The unsanitized posted menu item data.
|
||||||
* @return array The database IDs of the items saved.
|
* @return array The database IDs of the items saved
|
||||||
*/
|
*/
|
||||||
function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) {
|
function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) {
|
||||||
$menu_id = (int) $menu_id;
|
$menu_id = (int) $menu_id;
|
||||||
|
@ -38,7 +38,7 @@ wp_enqueue_script( 'wp-lists' );
|
|||||||
wp_enqueue_script( 'postbox' );
|
wp_enqueue_script( 'postbox' );
|
||||||
|
|
||||||
// Container for any messages displayed to the user
|
// Container for any messages displayed to the user
|
||||||
$messages_div = '';
|
$messages = array();
|
||||||
|
|
||||||
// Container that stores the name of the active menu
|
// Container that stores the name of the active menu
|
||||||
$nav_menu_selected_title = '';
|
$nav_menu_selected_title = '';
|
||||||
@ -169,7 +169,7 @@ switch ( $action ) {
|
|||||||
if ( is_nav_menu_item( $menu_item_id ) ) {
|
if ( is_nav_menu_item( $menu_item_id ) ) {
|
||||||
if ( wp_delete_post( $menu_item_id, true ) ) {
|
if ( wp_delete_post( $menu_item_id, true ) ) {
|
||||||
|
|
||||||
$messages_div = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
|
$messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -180,9 +180,9 @@ switch ( $action ) {
|
|||||||
$delete_nav_menu = wp_delete_nav_menu( $nav_menu_selected_id );
|
$delete_nav_menu = wp_delete_nav_menu( $nav_menu_selected_id );
|
||||||
|
|
||||||
if ( is_wp_error($delete_nav_menu) ) {
|
if ( is_wp_error($delete_nav_menu) ) {
|
||||||
$messages_div = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
|
||||||
} else {
|
} else {
|
||||||
$messages_div = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
|
$messages[] = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
|
||||||
$nav_menu_selected_id = 0; // Reset the selected menu
|
$nav_menu_selected_id = 0; // Reset the selected menu
|
||||||
}
|
}
|
||||||
unset( $delete_nav_menu );
|
unset( $delete_nav_menu );
|
||||||
@ -201,15 +201,15 @@ switch ( $action ) {
|
|||||||
$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
|
$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
|
||||||
|
|
||||||
if ( is_wp_error( $_nav_menu_selected_id ) ) {
|
if ( is_wp_error( $_nav_menu_selected_id ) ) {
|
||||||
$messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
|
||||||
} else {
|
} else {
|
||||||
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
|
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
|
||||||
$nav_menu_selected_id = $_nav_menu_selected_id;
|
$nav_menu_selected_id = $_nav_menu_selected_id;
|
||||||
$nav_menu_selected_title = $_menu_object->name;
|
$nav_menu_selected_title = $_menu_object->name;
|
||||||
$messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
|
$messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$messages_div = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,7 +222,7 @@ switch ( $action ) {
|
|||||||
$_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $_POST['menu-name'] ) );
|
$_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $_POST['menu-name'] ) );
|
||||||
if ( is_wp_error( $_nav_menu_selected_id ) ) {
|
if ( is_wp_error( $_nav_menu_selected_id ) ) {
|
||||||
$_menu_object = $_nav_menu_selected_id;
|
$_menu_object = $_nav_menu_selected_id;
|
||||||
$messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
|
||||||
} else {
|
} else {
|
||||||
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
|
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
|
||||||
$nav_menu_selected_title = $_menu_object->name;
|
$nav_menu_selected_title = $_menu_object->name;
|
||||||
@ -248,9 +248,10 @@ switch ( $action ) {
|
|||||||
|
|
||||||
$menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
|
$menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
|
||||||
|
|
||||||
if ( ! is_wp_error( $menu_item_db_id ) && isset( $menu_items[$menu_item_db_id] ) ) {
|
if ( is_wp_error( $menu_item_db_id ) )
|
||||||
|
$messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>';
|
||||||
|
elseif ( isset( $menu_items[$menu_item_db_id] ) )
|
||||||
unset( $menu_items[$menu_item_db_id] );
|
unset( $menu_items[$menu_item_db_id] );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,7 +266,7 @@ switch ( $action ) {
|
|||||||
|
|
||||||
do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
|
do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
|
||||||
|
|
||||||
$messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
|
$messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
|
||||||
unset( $menu_items );
|
unset( $menu_items );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -310,12 +311,12 @@ if ( current_theme_supports('nav-menus') ) {
|
|||||||
} elseif ( current_theme_supports('widgets') ) {
|
} elseif ( current_theme_supports('widgets') ) {
|
||||||
// Set up nav menu
|
// Set up nav menu
|
||||||
wp_nav_menu_setup();
|
wp_nav_menu_setup();
|
||||||
$messages_div = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the “Navigation Menu” widget to add any menus you create here to the theme’s sidebar.') . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the “Navigation Menu” widget to add any menus you create here to the theme’s sidebar.') . '</p></div>';
|
||||||
|
|
||||||
// The theme supports neither menus nor widgets.
|
// The theme supports neither menus nor widgets.
|
||||||
} else {
|
} else {
|
||||||
remove_meta_box( 'create-menu', 'nav-menus', 'side' );
|
remove_meta_box( 'create-menu', 'nav-menus', 'side' );
|
||||||
$messages_div = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
|
$messages[] = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the admin header
|
// Get the admin header
|
||||||
@ -324,7 +325,11 @@ require_once( 'admin-header.php' );
|
|||||||
<div class="wrap">
|
<div class="wrap">
|
||||||
<?php screen_icon(); ?>
|
<?php screen_icon(); ?>
|
||||||
<h2><?php esc_html_e('Menus'); ?></h2>
|
<h2><?php esc_html_e('Menus'); ?></h2>
|
||||||
<?php echo $messages_div; ?>
|
<?php
|
||||||
|
foreach( $messages as $message ) :
|
||||||
|
echo $message . "\n";
|
||||||
|
endforeach;
|
||||||
|
?>
|
||||||
|
|
||||||
<?php if ( current_theme_supports('nav-menus') || current_theme_supports('widgets') ) : ?>
|
<?php if ( current_theme_supports('nav-menus') || current_theme_supports('widgets') ) : ?>
|
||||||
<div id="nav-menus-frame">
|
<div id="nav-menus-frame">
|
||||||
|
@ -224,7 +224,11 @@ add_action( 'publish_post', '_publish_post_hook', 5, 1 );
|
|||||||
add_action( 'save_post', '_save_post_hook', 5, 2 );
|
add_action( 'save_post', '_save_post_hook', 5, 2 );
|
||||||
add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
|
add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
|
||||||
add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
|
add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
|
||||||
add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
|
add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
|
||||||
|
add_action( 'trash_post', '_wp_trash_menu_item' );
|
||||||
|
add_action( 'untrash_post', '_wp_untrash_menu_item' );
|
||||||
|
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
||||||
|
add_action( 'delete_term', '_wp_delete_tax_menu_item' );
|
||||||
|
|
||||||
// Post Thumbnail CSS class filtering
|
// Post Thumbnail CSS class filtering
|
||||||
add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
|
add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
|
||||||
|
@ -268,8 +268,17 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
|
|||||||
if ( 'taxonomy' == $args['menu-item-type'] ) {
|
if ( 'taxonomy' == $args['menu-item-type'] ) {
|
||||||
$original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
|
$original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
|
||||||
} elseif ( 'post_type' == $args['menu-item-type'] ) {
|
} elseif ( 'post_type' == $args['menu-item-type'] ) {
|
||||||
|
|
||||||
$original_object = get_post( $args['menu-item-object-id'] );
|
$original_object = get_post( $args['menu-item-object-id'] );
|
||||||
$original_title = $original_object->post_title;
|
$original_title = $original_object->post_title;
|
||||||
|
|
||||||
|
if ( 'trash' == get_post_status( $args['menu-item-object-id'] ) ) {
|
||||||
|
$post_type_object = get_post_type_object( $args['menu-item-object'] );
|
||||||
|
if ( isset( $post_type_object->singular_label ) )
|
||||||
|
return new WP_Error('update_nav_menu_item_failed', sprintf(__('The menu item "%1$s" belongs to a %2$s that is in the trash, so it cannot be updated.'), $args['menu-item-title'], $post_type_object->singular_label ) );
|
||||||
|
else
|
||||||
|
return new WP_Error('update_nav_menu_item_failed', sprintf(__('The menu item "%1$s" belongs to something that is in the trash, so it cannot be updated.'), $args['menu-item-title'] ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( empty( $args['menu-item-title'] ) || $args['menu-item-title'] == $original_title ) {
|
if ( empty( $args['menu-item-title'] ) || $args['menu-item-title'] == $original_title ) {
|
||||||
@ -528,4 +537,120 @@ function wp_setup_nav_menu_item( $menu_item ) {
|
|||||||
|
|
||||||
return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
|
return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the menu items associated with a particular object.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
*
|
||||||
|
* @param int $object_id The ID of the original object.
|
||||||
|
* @param string $object_type The type of object, such as "taxonomy" or "post_type."
|
||||||
|
* @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' ) {
|
||||||
|
$object_id = (int) $object_id;
|
||||||
|
$menu_item_ids = array();
|
||||||
|
|
||||||
|
$query = new WP_Query;
|
||||||
|
$menu_items = $query->query(
|
||||||
|
array(
|
||||||
|
'meta_key' => '_menu_item_object_id',
|
||||||
|
'meta_value' => $object_id,
|
||||||
|
'post_status' => 'any',
|
||||||
|
'post_type' => 'nav_menu_item',
|
||||||
|
'showposts' => -1,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
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 )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
$menu_item_ids[] = (int) $menu_item->ID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_unique( $menu_item_ids );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for handling a menu item when its original object is trashed.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param int $object_id The ID of the original object being trashed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function _wp_trash_menu_item( $object_id = 0 ) {
|
||||||
|
$object_id = (int) $object_id;
|
||||||
|
|
||||||
|
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id );
|
||||||
|
|
||||||
|
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||||
|
$menu_item = get_post( $menu_item_id, ARRAY_A );
|
||||||
|
$menu_item['post_status'] = 'draft';
|
||||||
|
wp_insert_post($menu_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for handling a menu item when its original object is un-trashed.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param int $object_id The ID of the original object being untrashed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function _wp_untrash_menu_item( $object_id = 0 ) {
|
||||||
|
$object_id = (int) $object_id;
|
||||||
|
|
||||||
|
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id );
|
||||||
|
|
||||||
|
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||||
|
$menu_item = get_post( $menu_item_id, ARRAY_A );
|
||||||
|
$menu_item['post_status'] = 'publish';
|
||||||
|
wp_insert_post($menu_item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for handling a menu item when its original object is deleted.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param int $object_id The ID of the original object being trashed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function _wp_delete_post_menu_item( $object_id = 0 ) {
|
||||||
|
$object_id = (int) $object_id;
|
||||||
|
|
||||||
|
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
|
||||||
|
|
||||||
|
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||||
|
wp_delete_post( $menu_item_id, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback for handling a menu item when its original object is deleted.
|
||||||
|
*
|
||||||
|
* @since 3.0.0
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param int $object_id The ID of the original object being trashed.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function _wp_delete_tax_menu_item( $object_id = 0 ) {
|
||||||
|
$object_id = (int) $object_id;
|
||||||
|
|
||||||
|
$menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy' );
|
||||||
|
|
||||||
|
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||||
|
wp_delete_post( $menu_item_id, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@ -2167,7 +2167,7 @@ function wp_insert_post($postarr = array(), $wp_error = false) {
|
|||||||
} elseif ( !empty($post_parent) ) {
|
} elseif ( !empty($post_parent) ) {
|
||||||
$parent_post = get_post($post_parent);
|
$parent_post = get_post($post_parent);
|
||||||
// Check for circular dependency
|
// Check for circular dependency
|
||||||
if ( $parent_post->post_parent == $post_ID )
|
if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
|
||||||
$post_parent = 0;
|
$post_parent = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user