<!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>[15219] trunk: hide unpublished items on frontend nav menus, props filosofo
, fixes #13822</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/15219">15219</a></dd>
<dt>Author</dt> <dd>wpmuguru</dd>
<dt>Date</dt> <dd>2010-06-11 15:34:32 +0000 (Fri, 11 Jun 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>hide unpublished items on frontend nav menus, props filosofo, fixes <a href="http://trac.wordpress.org/ticket/13822">#13822</a></pre>
<h3>Modified Paths</h3>
<ul>
<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>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/nav-menu.php (15218 => 15219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/nav-menu.php        2010-06-11 15:30:12 UTC (rev 15218)
+++ trunk/wp-admin/includes/nav-menu.php        2010-06-11 15:34:32 UTC (rev 15219)
</span><span class="lines">@@ -71,8 +71,12 @@
</span><span class="cx">                 $title = $item->title;
</span><span class="cx">
</span><span class="cx">                 if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
</span><ins>+                        $classes[] = 'draft';
+                        /* translators: %s: title of menu item in draft status */
+                        $title = sprintf( __('%s (Draft)'), $item->title );
+                } elseif ( isset( $item->post_status ) && 'pending' == $item->post_status ) {
</ins><span class="cx">                         $classes[] = 'pending';
</span><del>-                        /* translators: %s: title of menu item in draft status */
</del><ins>+                        /* translators: %s: title of menu item in pending status */
</ins><span class="cx">                         $title = sprintf( __('%s (Pending)'), $item->title );
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -194,6 +198,7 @@
</span><span class="cx">                                 <input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
</span><span class="cx">                                 <input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
</span><span class="cx">                                 <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
</span><ins>+                                <input class="menu-item-data-status" type="hidden" name="menu-item-status[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_status ); ?>" />
</ins><span class="cx">                                 <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
</span><span class="cx">                         </div><!-- .menu-item-settings-->
</span><span class="cx">                         <ul class="menu-item-transport"></ul>
</span><span class="lines">@@ -947,7 +952,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 3.0.0
</span><span class="cx"> *
</span><del>- * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
</del><ins>+ * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a pending, orphaned menu item.
</ins><span class="cx"> * @param array $menu_data The unsanitized posted menu item data.
</span><span class="cx"> * @return array The database IDs of the items saved
</span><span class="cx"> */
</span><span class="lines">@@ -1079,7 +1084,7 @@
</span><span class="cx">
</span><span class="cx">                 $some_pending_menu_items = false;
</span><span class="cx">                 foreach( (array) $menu_items as $menu_item ) {
</span><del>-                        if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
</del><ins>+                        if ( isset( $menu_item->post_status ) && 'pending' == $menu_item->post_status )
</ins><span class="cx">                                 $some_pending_menu_items = true;
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -1117,23 +1122,23 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Deletes orphaned draft menu items
</del><ins>+ * Deletes orphaned pending menu items
</ins><span class="cx"> *
</span><span class="cx"> * @access private
</span><span class="cx"> * @since 3.0.0
</span><span class="cx"> *
</span><span class="cx"> */
</span><del>-function _wp_delete_orphaned_draft_menu_items() {
</del><ins>+function _wp_delete_orphaned_pending_menu_items() {
</ins><span class="cx">         global $wpdb;
</span><span class="cx">         $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
</span><span class="cx">
</span><del>-        // delete orphaned draft menu items
-        $menu_items_to_delete = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) );
</del><ins>+        // delete orphaned pending menu items
+        $menu_items_to_delete = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS m ON p.ID = m.post_id WHERE post_type = 'nav_menu_item' AND post_status = 'pending' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) );
</ins><span class="cx">
</span><span class="cx">         foreach( (array) $menu_items_to_delete as $menu_item_id )
</span><span class="cx">                 wp_delete_post( $menu_item_id, true );
</span><span class="cx"> }
</span><span class="cx">
</span><del>-add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items');
</del><ins>+add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_pending_menu_items');
</ins><span class="cx">
</span><span class="cx"> ?>
</span></span></pre></div>
<a id="trunkwpadminnavmenusphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/nav-menus.php (15218 => 15219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/nav-menus.php        2010-06-11 15:30:12 UTC (rev 15218)
+++ trunk/wp-admin/nav-menus.php        2010-06-11 15:34:32 UTC (rev 15219)
</span><span class="lines">@@ -327,13 +327,13 @@
</span><span class="cx">                         // Update menu items
</span><span class="cx">
</span><span class="cx">                         if ( ! is_wp_error( $_menu_object ) ) {
</span><del>-                                $unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,publish') );
</del><ins>+                                $unsorted_menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array('orderby' => 'ID', 'output' => ARRAY_A, 'output_key' => 'ID', 'post_status' => 'draft,pending,publish') );
</ins><span class="cx">                                 $menu_items = array();
</span><span class="cx">                                 // Index menu items by db ID
</span><span class="cx">                                 foreach( $unsorted_menu_items as $_item )
</span><span class="cx">                                         $menu_items[$_item->db_id] = $_item;
</span><span class="cx">
</span><del>-                                $post_fields = array( 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object', 'menu-item-parent-id', 'menu-item-position', 'menu-item-type', 'menu-item-title', 'menu-item-url', 'menu-item-description', 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn' );
</del><ins>+                                $post_fields = array( 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object', 'menu-item-parent-id', 'menu-item-position', 'menu-item-type', 'menu-item-title', 'menu-item-url', 'menu-item-description', 'menu-item-attr-title', 'menu-item-status', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn' );
</ins><span class="cx">                                 wp_defer_term_counting(true);
</span><span class="cx">                                 // Loop through all the menu items' POST variables
</span><span class="cx">                                 if ( ! empty( $_POST['menu-item-db-id'] ) ) {
</span></span></pre></div>
<a id="trunkwpincludesdefaultfiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/default-filters.php (15218 => 15219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/default-filters.php        2010-06-11 15:30:12 UTC (rev 15218)
+++ trunk/wp-includes/default-filters.php        2010-06-11 15:34:32 UTC (rev 15219)
</span><span class="lines">@@ -235,7 +235,7 @@
</span><span class="cx"> add_action( 'untrash_post', '_wp_untrash_menu_item' );
</span><span class="cx"> add_action( 'delete_post', '_wp_delete_post_menu_item' );
</span><span class="cx"> add_action( 'delete_term', '_wp_delete_tax_menu_item' );
</span><del>-add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
</del><ins>+add_action( 'transition_post_status', '_wp_menu_changing_status_observer', 10, 3 );
</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 class="lines">@@ -255,4 +255,4 @@
</span><span class="cx"> add_action( 'admin_init', 'register_admin_color_schemes', 1);
</span><span class="cx"> add_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
</span><span class="cx">
</span><del>-?>
</del><span class="cx">\ No newline at end of file
</span><ins>+?>
</ins></span></pre></div>
<a id="trunkwpincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/nav-menu.php (15218 => 15219)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/nav-menu.php        2010-06-11 15:30:12 UTC (rev 15218)
+++ trunk/wp-includes/nav-menu.php        2010-06-11 15:34:32 UTC (rev 15219)
</span><span class="lines">@@ -244,7 +244,7 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 3.0.0
</span><span class="cx"> *
</span><del>- * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
</del><ins>+ * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a pending orphan.
</ins><span class="cx"> * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
</span><span class="cx"> * @param array $menu_item_data The menu item's data.
</span><span class="cx"> * @return int The menu item's database ID or WP_Error object on failure.
</span><span class="lines">@@ -262,7 +262,7 @@
</span><span class="cx">         if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
</span><span class="cx">                 return $menu;
</span><span class="cx">
</span><del>-        $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
</del><ins>+        $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) );
</ins><span class="cx">
</span><span class="cx">         $count = count( $menu_items );
</span><span class="cx">
</span><span class="lines">@@ -339,10 +339,10 @@
</span><span class="cx">         if ( 0 != $menu_id )
</span><span class="cx">                 $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
</span><span class="cx">
</span><del>-        // New menu item. Default is draft status
</del><ins>+        // New menu item. Default is pending status
</ins><span class="cx">         if ( 0 == $menu_item_db_id ) {
</span><span class="cx">                 $post['ID'] = 0;
</span><del>-                $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
</del><ins>+                $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'pending';
</ins><span class="cx">                 $menu_item_db_id = wp_insert_post( $post );
</span><span class="cx">
</span><span class="cx">         // Update existing menu item. Default is publish status
</span><span class="lines">@@ -670,7 +670,7 @@
</span><span class="cx">
</span><span class="cx">         foreach( (array) $menu_item_ids as $menu_item_id ) {
</span><span class="cx">                 $menu_item = get_post( $menu_item_id, ARRAY_A );
</span><del>-                $menu_item['post_status'] = 'draft';
</del><ins>+                $menu_item['post_status'] = 'pending';
</ins><span class="cx">                 wp_insert_post($menu_item);
</span><span class="cx">         }
</span><span class="cx"> }
</span><span class="lines">@@ -735,7 +735,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Automatically add newly published page objects to menus with that as an option.
</del><ins>+ * Modify a navigational menu upon post object status change, if appropos.
</ins><span class="cx"> *
</span><span class="cx"> * @since 3.0.0
</span><span class="cx"> * @access private
</span><span class="lines">@@ -745,34 +745,62 @@
</span><span class="cx"> * @param object $post The post object being transitioned from one status to another.
</span><span class="cx"> * @return void
</span><span class="cx"> */
</span><del>-function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
-        if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
-                return;
-        if ( ! empty( $post->post_parent ) )
-                return;
-        $auto_add = get_option( 'nav_menu_options' );
-        if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
-                return;
-        $auto_add = $auto_add['auto_add'];
-        if ( empty( $auto_add ) || ! is_array( $auto_add ) )
-                return;
</del><ins>+function _wp_menu_changing_status_observer( $new_status, $old_status, $post ) {
+        // append new top-level page objects to a menu for which that option is selected
+        if (
+                'publish' == $new_status &&
+                'publish' != $old_status &&
+                'page' == $post->post_type &&
+                empty( $post->post_parent )
+        ) {
+                $auto_add = get_option( 'nav_menu_options' );
+                if (
+                        isset( $auto_add['auto_add'] ) &&
+                        is_array( $auto_add['auto_add'] )
+                ) {
+                        $args = array(
+                                'menu-item-object-id' => $post->ID,
+                                'menu-item-object' => $post->post_type,
+                                'menu-item-type' => 'post_type',
+                                'menu-item-status' => 'publish',
+                        );
</ins><span class="cx">
</span><del>-        $args = array(
-                'menu-item-object-id' => $post->ID,
-                'menu-item-object' => $post->post_type,
-                'menu-item-type' => 'post_type',
-                'menu-item-status' => 'publish',
-        );
</del><ins>+                        foreach ( $auto_add['auto_add'] as $menu_id ) {
+                                $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) );
+                                if ( ! is_array( $items ) )
+                                        continue;
+                                foreach ( $items as $item ) {
+                                        if ( $post->ID == $item->object_id )
+                                                continue 2;
+                                }
+                                wp_update_nav_menu_item( $menu_id, 0, $args );
+                        }
+                }
+        }
+        
+        // give menu items draft status if their associated post objects change from "publish" to "draft", or vice versa (draft item being re-published)
+        if (
+                ! empty( $post->ID ) &&
+                (
+                        ( 'publish' == $old_status && 'draft' == $new_status ) ||
+                        ( 'draft' == $old_status && 'publish' == $new_status )
+                )
+        ) {
+                $menu_items = get_posts(array(
+                        'meta_key' => '_menu_item_object_id',
+                        'meta_value' => $post->ID,
+                        'post_status' => 'any',
+                        'post_type' => 'nav_menu_item',
+                ));
</ins><span class="cx">
</span><del>-        foreach ( $auto_add as $menu_id ) {
-                $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
-                if ( ! is_array( $items ) )
-                        continue;
-                foreach ( $items as $item ) {
-                        if ( $post->ID == $item->object_id )
-                                continue 2;
</del><ins>+                foreach( (array) $menu_items as $menu_item ) {
+                        if ( ! empty( $menu_item->ID ) ) {
+                                $properties = get_object_vars( $menu_item );
+                                $properties['post_status'] = $new_status;
+
+                                wp_insert_post( $properties );
+                        }
</ins><span class="cx">                 }
</span><del>-                wp_update_nav_menu_item( $menu_id, 0, $args );
</del><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>