From 9eaf91db65d185302f8ef68f52225de5289c5595 Mon Sep 17 00:00:00 2001 From: wpmuguru Date: Fri, 11 Jun 2010 15:34:32 +0000 Subject: [PATCH] hide unpublished items on frontend nav menus, props filosofo, fixes #13822 git-svn-id: http://svn.automattic.com/wordpress/trunk@15219 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/nav-menu.php | 21 +++++--- wp-admin/nav-menus.php | 4 +- wp-includes/default-filters.php | 4 +- wp-includes/nav-menu.php | 92 +++++++++++++++++++++------------ 4 files changed, 77 insertions(+), 44 deletions(-) diff --git a/wp-admin/includes/nav-menu.php b/wp-admin/includes/nav-menu.php index 5f2f2f2ba..e5f0655c8 100644 --- a/wp-admin/includes/nav-menu.php +++ b/wp-admin/includes/nav-menu.php @@ -71,8 +71,12 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu { $title = $item->title; if ( isset( $item->post_status ) && 'draft' == $item->post_status ) { - $classes[] = 'pending'; + $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 ) { + $classes[] = 'pending'; + /* translators: %s: title of menu item in pending status */ $title = sprintf( __('%s (Pending)'), $item->title ); } @@ -194,6 +198,7 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu { + @@ -947,7 +952,7 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) { * * @since 3.0.0 * - * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item. + * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a pending, orphaned menu item. * @param array $menu_data The unsanitized posted menu item data. * @return array The database IDs of the items saved */ @@ -1079,7 +1084,7 @@ function wp_get_nav_menu_to_edit( $menu_id = 0 ) { $some_pending_menu_items = false; foreach( (array) $menu_items as $menu_item ) { - if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status ) + if ( isset( $menu_item->post_status ) && 'pending' == $menu_item->post_status ) $some_pending_menu_items = true; } @@ -1117,23 +1122,23 @@ function wp_nav_menu_manage_columns() { } /** - * Deletes orphaned draft menu items + * Deletes orphaned pending menu items * * @access private * @since 3.0.0 * */ -function _wp_delete_orphaned_draft_menu_items() { +function _wp_delete_orphaned_pending_menu_items() { global $wpdb; $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS); - // 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 ) ); + // 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 ) ); foreach( (array) $menu_items_to_delete as $menu_item_id ) wp_delete_post( $menu_item_id, true ); } -add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items'); +add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_pending_menu_items'); ?> diff --git a/wp-admin/nav-menus.php b/wp-admin/nav-menus.php index 5b4bd44e0..4c573bdfa 100644 --- a/wp-admin/nav-menus.php +++ b/wp-admin/nav-menus.php @@ -327,13 +327,13 @@ switch ( $action ) { // Update menu items if ( ! is_wp_error( $_menu_object ) ) { - $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') ); + $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') ); $menu_items = array(); // Index menu items by db ID foreach( $unsorted_menu_items as $_item ) $menu_items[$_item->db_id] = $_item; - $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' ); + $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' ); wp_defer_term_counting(true); // Loop through all the menu items' POST variables if ( ! empty( $_POST['menu-item-db-id'] ) ) { diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index fe180386e..5bbcdfb44 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -235,7 +235,7 @@ 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' ); -add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 ); +add_action( 'transition_post_status', '_wp_menu_changing_status_observer', 10, 3 ); // Post Thumbnail CSS class filtering add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' ); @@ -255,4 +255,4 @@ add_filter( 'pre_option_gmt_offset','wp_timezone_override_offset' ); add_action( 'admin_init', 'register_admin_color_schemes', 1); add_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' ); -?> \ No newline at end of file +?> diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index dd683f636..c61edc6db 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -244,7 +244,7 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { * * @since 3.0.0 * - * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. + * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a pending orphan. * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item. * @param array $menu_item_data The menu item's data. * @return int The menu item's database ID or WP_Error object on failure. @@ -262,7 +262,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) ) return $menu; - $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) ); + $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) ); $count = count( $menu_items ); @@ -339,10 +339,10 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item if ( 0 != $menu_id ) $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) ); - // New menu item. Default is draft status + // New menu item. Default is pending status if ( 0 == $menu_item_db_id ) { $post['ID'] = 0; - $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft'; + $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'pending'; $menu_item_db_id = wp_insert_post( $post ); // Update existing menu item. Default is publish status @@ -670,7 +670,7 @@ function _wp_trash_menu_item( $object_id = 0 ) { foreach( (array) $menu_item_ids as $menu_item_id ) { $menu_item = get_post( $menu_item_id, ARRAY_A ); - $menu_item['post_status'] = 'draft'; + $menu_item['post_status'] = 'pending'; wp_insert_post($menu_item); } } @@ -735,7 +735,7 @@ function _wp_delete_tax_menu_item( $object_id = 0 ) { } /** - * Automatically add newly published page objects to menus with that as an option. + * Modify a navigational menu upon post object status change, if appropos. * * @since 3.0.0 * @access private @@ -745,34 +745,62 @@ function _wp_delete_tax_menu_item( $object_id = 0 ) { * @param object $post The post object being transitioned from one status to another. * @return void */ -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; +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', + ); - $args = array( - 'menu-item-object-id' => $post->ID, - 'menu-item-object' => $post->post_type, - 'menu-item-type' => 'post_type', - 'menu-item-status' => 'publish', - ); - - 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; + 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', + )); + + 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 ); + } } - wp_update_nav_menu_item( $menu_id, 0, $args ); } }