<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[14295] trunk: When posts/terms are deleted/trashed,
update associated menu items.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14295">14295</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-04-29 07:33:56 +0000 (Thu, 29 Apr 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://trac.wordpress.org/ticket/13174">#13174</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminadminajaxphp">trunk/wp-admin/admin-ajax.php</a></li>
<li><a href="#trunkwpadminincludesnavmenuphp">trunk/wp-admin/includes/nav-menu.php</a></li>
<li><a href="#trunkwpadminnavmenusphp">trunk/wp-admin/nav-menus.php</a></li>
<li><a href="#trunkwpincludesdefaultfiltersphp">trunk/wp-includes/default-filters.php</a></li>
<li><a href="#trunkwpincludesnavmenuphp">trunk/wp-includes/nav-menu.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminadminajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/admin-ajax.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/admin-ajax.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-admin/admin-ajax.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -827,6 +827,8 @@
</span><span class="cx">         $menu_id = (int) $_POST['menu'];
</span><span class="cx">         if ( isset( $_POST['menu-item'] ) ) {
</span><span class="cx">                 $item_ids = wp_save_nav_menu_item( $menu_id, $_POST['menu-item'] );
</span><ins>+                if ( is_wp_error( $item_ids ) )
+                        die('-1');
</ins><span class="cx">         } else {
</span><span class="cx">                 $item_ids = array();
</span><span class="cx">         }
</span></span></pre></div>
<a id="trunkwpadminincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/nav-menu.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/nav-menu.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-admin/includes/nav-menu.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -808,7 +808,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @param int $menu_id The menu ID for which to save this item.
</span><span class="cx"> * @param array $menu_data The unsanitized posted menu item data.
</span><del>- * @return array The database IDs of the items saved.
</del><ins>+ * @return array The database IDs of the items saved
</ins><span class="cx"> */
</span><span class="cx"> function wp_save_nav_menu_item( $menu_id = 0, $menu_data = array() ) {
</span><span class="cx">         $menu_id = (int) $menu_id;
</span></span></pre></div>
<a id="trunkwpadminnavmenusphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/nav-menus.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/nav-menus.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-admin/nav-menus.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -38,7 +38,7 @@
</span><span class="cx"> wp_enqueue_script( 'postbox' );
</span><span class="cx">
</span><span class="cx"> // Container for any messages displayed to the user
</span><del>-$messages_div = '';
</del><ins>+$messages = array();
</ins><span class="cx">
</span><span class="cx"> // Container that stores the name of the active menu
</span><span class="cx"> $nav_menu_selected_title = '';
</span><span class="lines">@@ -169,7 +169,7 @@
</span><span class="cx">                 if ( is_nav_menu_item( $menu_item_id ) ) {
</span><span class="cx">                         if ( wp_delete_post( $menu_item_id, true ) ) {
</span><span class="cx">                                 
</span><del>-                                $messages_div = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
</del><ins>+                                $messages[] = '<div id="message" class="updated"><p>' . __('The menu item has been successfully deleted.') . '</p></div>';
</ins><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="cx">                 break;
</span><span class="lines">@@ -180,9 +180,9 @@
</span><span class="cx">                         $delete_nav_menu = wp_delete_nav_menu( $nav_menu_selected_id );
</span><span class="cx">
</span><span class="cx">                         if ( is_wp_error($delete_nav_menu) ) {
</span><del>-                                $messages_div = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
</del><ins>+                                $messages[] = '<div id="message" class="error"><p>' . $delete_nav_menu->get_error_message() . '</p></div>';
</ins><span class="cx">                         } else {
</span><del>-                                $messages_div = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
</del><ins>+                                $messages[] = '<div id="message" class="updated"><p>' . __('The menu has been successfully deleted.') . '</p></div>';
</ins><span class="cx">                                 $nav_menu_selected_id = 0; // Reset the selected menu
</span><span class="cx">                         }
</span><span class="cx">                         unset( $delete_nav_menu );
</span><span class="lines">@@ -201,15 +201,15 @@
</span><span class="cx">                                         $_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => $new_menu_title) );
</span><span class="cx">
</span><span class="cx">                                         if ( is_wp_error( $_nav_menu_selected_id ) ) {
</span><del>-                                                $messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
</del><ins>+                                                $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
</ins><span class="cx">                                         } else {
</span><span class="cx">                                                 $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
</span><span class="cx">                                                 $nav_menu_selected_id = $_nav_menu_selected_id;
</span><span class="cx">                                                 $nav_menu_selected_title = $_menu_object->name;
</span><del>-                                                $messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
</del><ins>+                                                $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been successfully created.'), $nav_menu_selected_title ) . '</p></div>';
</ins><span class="cx">                                         }
</span><span class="cx">                                 } else {
</span><del>-                                        $messages_div = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
</del><ins>+                                        $messages[] = '<div id="message" class="error"><p>' . __('Please enter a valid menu name.') . '</p></div>';
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><span class="cx">
</span><span class="lines">@@ -222,7 +222,7 @@
</span><span class="cx">                                 $_nav_menu_selected_id = wp_update_nav_menu_object( $nav_menu_selected_id, array( 'menu-name' => $_POST['menu-name'] ) );
</span><span class="cx">                                 if ( is_wp_error( $_nav_menu_selected_id ) ) {
</span><span class="cx">                                         $_menu_object = $_nav_menu_selected_id;
</span><del>-                                        $messages_div = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
</del><ins>+                                        $messages[] = '<div id="message" class="error"><p>' . $_nav_menu_selected_id->get_error_message() . '</p></div>';
</ins><span class="cx">                                 } else {
</span><span class="cx">                                         $_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
</span><span class="cx">                                         $nav_menu_selected_title = $_menu_object->name;
</span><span class="lines">@@ -248,9 +248,10 @@
</span><span class="cx">
</span><span class="cx">                                                 $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args );
</span><span class="cx">
</span><del>-                                                if ( ! is_wp_error( $menu_item_db_id ) && isset( $menu_items[$menu_item_db_id] ) ) {
</del><ins>+                                                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] ) )
</ins><span class="cx">                                                         unset( $menu_items[$menu_item_db_id] );
</span><del>-                                                }
</del><span class="cx">                                         }
</span><span class="cx">                                 }
</span><span class="cx">
</span><span class="lines">@@ -265,7 +266,7 @@
</span><span class="cx">
</span><span class="cx">                                 do_action( 'wp_update_nav_menu', $nav_menu_selected_id );
</span><span class="cx">
</span><del>-                                $messages_div = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
</del><ins>+                                $messages[] = '<div id="message" class="updated"><p>' . sprintf( __('The <strong>%s</strong> menu has been updated.'), $nav_menu_selected_title ) . '</p></div>';
</ins><span class="cx">                                 unset( $menu_items );
</span><span class="cx">                         }
</span><span class="cx">                 }
</span><span class="lines">@@ -310,12 +311,12 @@
</span><span class="cx"> } elseif ( current_theme_supports('widgets') ) {
</span><span class="cx">         // Set up nav menu
</span><span class="cx">         wp_nav_menu_setup();
</span><del>-        $messages_div = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the &#8220;Navigation Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p></div>';
</del><ins>+        $messages[] = '<div id="message" class="error"><p>' . __('The current theme does not natively support menus, but you can use the &#8220;Navigation Menu&#8221; widget to add any menus you create here to the theme&#8217;s sidebar.') . '</p></div>';
</ins><span class="cx">
</span><span class="cx"> // The theme supports neither menus nor widgets.
</span><span class="cx"> } else {
</span><span class="cx">         remove_meta_box( 'create-menu', 'nav-menus', 'side' );
</span><del>-        $messages_div = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
</del><ins>+        $messages[] = '<div id="message" class="error"><p>' . __('The current theme does not support menus.') . '</p></div>';
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // Get the admin header
</span><span class="lines">@@ -324,7 +325,11 @@
</span><span class="cx"> <div class="wrap">
</span><span class="cx">         <?php screen_icon(); ?>
</span><span class="cx">         <h2><?php esc_html_e('Menus'); ?></h2>
</span><del>-        <?php echo $messages_div; ?>
</del><ins>+        <?php
+        foreach( $messages as $message ) :
+                echo $message . "\n";
+        endforeach;
+        ?>
</ins><span class="cx">         
</span><span class="cx">         <?php if ( current_theme_supports('nav-menus') || current_theme_supports('widgets') ) : ?>
</span><span class="cx">         <div id="nav-menus-frame">
</span></span></pre></div>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-includes/default-filters.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -224,7 +224,11 @@
</span><span class="cx"> add_action( 'save_post', '_save_post_hook', 5, 2 );
</span><span class="cx"> add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
</span><span class="cx"> add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
</span><del>-add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
</del><ins>+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' );
</ins><span class="cx">
</span><span class="cx"> // Post Thumbnail CSS class filtering
</span><span class="cx"> add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
</span></span></pre></div>
<a id="trunkwpincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/nav-menu.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/nav-menu.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-includes/nav-menu.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -268,8 +268,17 @@
</span><span class="cx">                 if ( 'taxonomy' == $args['menu-item-type'] ) {
</span><span class="cx">                         $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
</span><span class="cx">                 } elseif ( 'post_type' == $args['menu-item-type'] ) {
</span><ins>+
</ins><span class="cx">                         $original_object = get_post( $args['menu-item-object-id'] );
</span><span class="cx">                         $original_title = $original_object->post_title;
</span><ins>+
+                        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'] ) );
+                        }
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( empty( $args['menu-item-title'] ) || $args['menu-item-title'] == $original_title ) {
</span><span class="lines">@@ -528,4 +537,120 @@
</span><span class="cx">
</span><span class="cx">         return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
</span><span class="cx"> }
</span><ins>+
+/**
+ * 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 );
+        }
+}
+
</ins><span class="cx"> ?>
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (14294 => 14295)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2010-04-29 07:28:34 UTC (rev 14294)
+++ trunk/wp-includes/post.php        2010-04-29 07:33:56 UTC (rev 14295)
</span><span class="lines">@@ -2167,7 +2167,7 @@
</span><span class="cx">                 } elseif ( !empty($post_parent) ) {
</span><span class="cx">                         $parent_post = get_post($post_parent);
</span><span class="cx">                         // Check for circular dependency
</span><del>-                        if ( $parent_post->post_parent == $post_ID )
</del><ins>+                        if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
</ins><span class="cx">                                 $post_parent = 0;
</span><span class="cx">                 }
</span><span class="cx">         }
</span></span></pre>
</div>
</div>
</body>
</html>