WordPress.org

Make WordPress Core

Changeset 15219


Ignore:
Timestamp:
06/11/2010 03:34:32 PM (10 years ago)
Author:
wpmuguru
Message:

hide unpublished items on frontend nav menus, props filosofo, fixes #13822

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/nav-menu.php

    r15214 r15219  
    7272
    7373        if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
     74            $classes[] = 'draft';
     75            /* translators: %s: title of menu item in draft status */
     76            $title = sprintf( __('%s (Draft)'), $item->title );
     77        } elseif ( isset( $item->post_status ) && 'pending' == $item->post_status ) {
    7478            $classes[] = 'pending';
    75             /* translators: %s: title of menu item in draft status */
     79            /* translators: %s: title of menu item in pending status */
    7680            $title = sprintf( __('%s (Pending)'), $item->title );
    7781        }
     
    195199                <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 ); ?>" />
    196200                <input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
     201                <input class="menu-item-data-status" type="hidden" name="menu-item-status[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_status ); ?>" />
    197202                <input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
    198203            </div><!-- .menu-item-settings-->
     
    948953 * @since 3.0.0
    949954 *
    950  * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
     955 * @param int $menu_id The menu ID for which to save this item. $menu_id of 0 makes a pending, orphaned menu item.
    951956 * @param array $menu_data The unsanitized posted menu item data.
    952957 * @return array The database IDs of the items saved
     
    10801085        $some_pending_menu_items = false;
    10811086        foreach( (array) $menu_items as $menu_item ) {
    1082             if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status )
     1087            if ( isset( $menu_item->post_status ) && 'pending' == $menu_item->post_status )
    10831088                $some_pending_menu_items = true;
    10841089        }
     
    11181123
    11191124/**
    1120  * Deletes orphaned draft menu items
     1125 * Deletes orphaned pending menu items
    11211126 *
    11221127 * @access private
     
    11241129 *
    11251130 */
    1126 function _wp_delete_orphaned_draft_menu_items() {
     1131function _wp_delete_orphaned_pending_menu_items() {
    11271132    global $wpdb;
    11281133    $delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
    11291134
    1130     // delete orphaned draft menu items
    1131     $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 ) );
     1135    // delete orphaned pending menu items
     1136    $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 ) );
    11321137
    11331138    foreach( (array) $menu_items_to_delete as $menu_item_id )
     
    11351140}
    11361141
    1137 add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_draft_menu_items');
     1142add_action('admin_head-nav-menus.php', '_wp_delete_orphaned_pending_menu_items');
    11381143
    11391144?>
  • trunk/wp-admin/nav-menus.php

    r15214 r15219  
    328328
    329329            if ( ! is_wp_error( $_menu_object ) ) {
    330                 $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') );
     330                $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') );
    331331                $menu_items = array();
    332332                // Index menu items by db ID
     
    334334                    $menu_items[$_item->db_id] = $_item;
    335335
    336                 $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' );
     336                $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' );
    337337                wp_defer_term_counting(true);
    338338                // Loop through all the menu items' POST variables
  • trunk/wp-includes/default-filters.php

    r15078 r15219  
    236236add_action( 'delete_post',                '_wp_delete_post_menu_item'      );
    237237add_action( 'delete_term',                '_wp_delete_tax_menu_item'       );
    238 add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu',  10, 3 );
     238add_action( 'transition_post_status', '_wp_menu_changing_status_observer',  10, 3 );
    239239
    240240// Post Thumbnail CSS class filtering
  • trunk/wp-includes/nav-menu.php

    r15193 r15219  
    245245 * @since 3.0.0
    246246 *
    247  * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
     247 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a pending orphan.
    248248 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
    249249 * @param array $menu_item_data The menu item's data.
     
    263263        return $menu;
    264264
    265     $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
     265    $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) );
    266266
    267267    $count = count( $menu_items );
     
    340340        $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
    341341
    342     // New menu item. Default is draft status
     342    // New menu item. Default is pending status
    343343    if ( 0 == $menu_item_db_id ) {
    344344        $post['ID'] = 0;
    345         $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
     345        $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'pending';
    346346        $menu_item_db_id = wp_insert_post( $post );
    347347
     
    671671    foreach( (array) $menu_item_ids as $menu_item_id ) {
    672672        $menu_item = get_post( $menu_item_id, ARRAY_A );
    673         $menu_item['post_status'] = 'draft';
     673        $menu_item['post_status'] = 'pending';
    674674        wp_insert_post($menu_item);
    675675    }
     
    736736
    737737/**
    738  * Automatically add newly published page objects to menus with that as an option.
     738 * Modify a navigational menu upon post object status change, if appropos.
    739739 *
    740740 * @since 3.0.0
     
    746746 * @return void
    747747 */
    748 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
    749     if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
    750         return;
    751     if ( ! empty( $post->post_parent ) )
    752         return;
    753     $auto_add = get_option( 'nav_menu_options' );
    754     if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
    755         return;
    756     $auto_add = $auto_add['auto_add'];
    757     if ( empty( $auto_add ) || ! is_array( $auto_add ) )
    758         return;
    759 
    760     $args = array(
    761         'menu-item-object-id' => $post->ID,
    762         'menu-item-object' => $post->post_type,
    763         'menu-item-type' => 'post_type',
    764         'menu-item-status' => 'publish',
    765     );
    766 
    767     foreach ( $auto_add as $menu_id ) {
    768         $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
    769         if ( ! is_array( $items ) )
    770             continue;
    771         foreach ( $items as $item ) {
    772             if ( $post->ID == $item->object_id )
    773                 continue 2;
    774         }
    775         wp_update_nav_menu_item( $menu_id, 0, $args );
     748function _wp_menu_changing_status_observer( $new_status, $old_status, $post ) {
     749    // append new top-level page objects to a menu for which that option is selected
     750    if (
     751        'publish' == $new_status &&
     752        'publish' != $old_status &&
     753        'page' == $post->post_type &&
     754        empty( $post->post_parent )
     755    ) {
     756        $auto_add = get_option( 'nav_menu_options' );
     757        if (
     758            isset( $auto_add['auto_add'] ) &&
     759            is_array( $auto_add['auto_add'] )
     760        ) {
     761            $args = array(
     762                'menu-item-object-id' => $post->ID,
     763                'menu-item-object' => $post->post_type,
     764                'menu-item-type' => 'post_type',
     765                'menu-item-status' => 'publish',
     766            );
     767
     768            foreach ( $auto_add['auto_add'] as $menu_id ) {
     769                $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) );
     770                if ( ! is_array( $items ) )
     771                    continue;
     772                foreach ( $items as $item ) {
     773                    if ( $post->ID == $item->object_id )
     774                        continue 2;
     775                }
     776                wp_update_nav_menu_item( $menu_id, 0, $args );
     777            }
     778        }
     779    }
     780   
     781    // give menu items draft status if their associated post objects change from "publish" to "draft", or vice versa (draft item being re-published)
     782    if (
     783        ! empty( $post->ID ) &&
     784        (
     785            ( 'publish' == $old_status && 'draft' == $new_status ) ||
     786            ( 'draft' == $old_status && 'publish' == $new_status )
     787        )
     788    ) {
     789        $menu_items = get_posts(array(
     790            'meta_key' => '_menu_item_object_id',
     791            'meta_value' => $post->ID,
     792            'post_status' => 'any',
     793            'post_type' => 'nav_menu_item',
     794        ));
     795
     796        foreach( (array) $menu_items as $menu_item ) {
     797            if ( ! empty( $menu_item->ID ) ) {
     798                $properties = get_object_vars( $menu_item );
     799                $properties['post_status'] = $new_status;
     800
     801                wp_insert_post( $properties );
     802            }
     803        }
    776804    }
    777805}
Note: See TracChangeset for help on using the changeset viewer.