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
This commit is contained in:
parent
a810538fc7
commit
9eaf91db65
|
@ -71,8 +71,12 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu {
|
||||||
$title = $item->title;
|
$title = $item->title;
|
||||||
|
|
||||||
if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
|
if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
|
||||||
$classes[] = 'pending';
|
$classes[] = 'draft';
|
||||||
/* translators: %s: title of menu item in draft status */
|
/* 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 );
|
$title = sprintf( __('%s (Pending)'), $item->title );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,6 +198,7 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu {
|
||||||
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
|
<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
|
||||||
<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 ); ?>" />
|
<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 ); ?>" />
|
||||||
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
|
<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
|
||||||
|
<input class="menu-item-data-status" type="hidden" name="menu-item-status[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_status ); ?>" />
|
||||||
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
|
<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
|
||||||
</div><!-- .menu-item-settings-->
|
</div><!-- .menu-item-settings-->
|
||||||
<ul class="menu-item-transport"></ul>
|
<ul class="menu-item-transport"></ul>
|
||||||
|
@ -947,7 +952,7 @@ function wp_nav_menu_item_taxonomy_meta_box( $object, $taxonomy ) {
|
||||||
*
|
*
|
||||||
* @since 3.0.0
|
* @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.
|
* @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
|
||||||
*/
|
*/
|
||||||
|
@ -1079,7 +1084,7 @@ function wp_get_nav_menu_to_edit( $menu_id = 0 ) {
|
||||||
|
|
||||||
$some_pending_menu_items = false;
|
$some_pending_menu_items = false;
|
||||||
foreach( (array) $menu_items as $menu_item ) {
|
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;
|
$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
|
* @access private
|
||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function _wp_delete_orphaned_draft_menu_items() {
|
function _wp_delete_orphaned_pending_menu_items() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
|
$delete_timestamp = time() - (60*60*24*EMPTY_TRASH_DAYS);
|
||||||
|
|
||||||
// delete orphaned draft menu items
|
// 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 = 'draft' AND meta_key = '_menu_item_orphaned' AND meta_value < '%d'", $delete_timestamp ) );
|
$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 )
|
foreach( (array) $menu_items_to_delete as $menu_item_id )
|
||||||
wp_delete_post( $menu_item_id, true );
|
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');
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -327,13 +327,13 @@ switch ( $action ) {
|
||||||
// Update menu items
|
// Update menu items
|
||||||
|
|
||||||
if ( ! is_wp_error( $_menu_object ) ) {
|
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();
|
$menu_items = array();
|
||||||
// Index menu items by db ID
|
// Index menu items by db ID
|
||||||
foreach( $unsorted_menu_items as $_item )
|
foreach( $unsorted_menu_items as $_item )
|
||||||
$menu_items[$_item->db_id] = $_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);
|
wp_defer_term_counting(true);
|
||||||
// Loop through all the menu items' POST variables
|
// Loop through all the menu items' POST variables
|
||||||
if ( ! empty( $_POST['menu-item-db-id'] ) ) {
|
if ( ! empty( $_POST['menu-item-db-id'] ) ) {
|
||||||
|
|
|
@ -235,7 +235,7 @@ add_action( 'trash_post', '_wp_trash_menu_item' );
|
||||||
add_action( 'untrash_post', '_wp_untrash_menu_item' );
|
add_action( 'untrash_post', '_wp_untrash_menu_item' );
|
||||||
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
||||||
add_action( 'delete_term', '_wp_delete_tax_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
|
// 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' );
|
||||||
|
|
|
@ -244,7 +244,7 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
|
||||||
*
|
*
|
||||||
* @since 3.0.0
|
* @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 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.
|
* @param array $menu_item_data The menu item's data.
|
||||||
* @return int The menu item's database ID or WP_Error object on failure.
|
* @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 ) )
|
if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
|
||||||
return $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 );
|
$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 )
|
if ( 0 != $menu_id )
|
||||||
$post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_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 ) {
|
if ( 0 == $menu_item_db_id ) {
|
||||||
$post['ID'] = 0;
|
$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 );
|
$menu_item_db_id = wp_insert_post( $post );
|
||||||
|
|
||||||
// Update existing menu item. Default is publish status
|
// 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 ) {
|
foreach( (array) $menu_item_ids as $menu_item_id ) {
|
||||||
$menu_item = get_post( $menu_item_id, ARRAY_A );
|
$menu_item = get_post( $menu_item_id, ARRAY_A );
|
||||||
$menu_item['post_status'] = 'draft';
|
$menu_item['post_status'] = 'pending';
|
||||||
wp_insert_post($menu_item);
|
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
|
* @since 3.0.0
|
||||||
* @access private
|
* @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.
|
* @param object $post The post object being transitioned from one status to another.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
|
function _wp_menu_changing_status_observer( $new_status, $old_status, $post ) {
|
||||||
if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
|
// append new top-level page objects to a menu for which that option is selected
|
||||||
return;
|
if (
|
||||||
if ( ! empty( $post->post_parent ) )
|
'publish' == $new_status &&
|
||||||
return;
|
'publish' != $old_status &&
|
||||||
$auto_add = get_option( 'nav_menu_options' );
|
'page' == $post->post_type &&
|
||||||
if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
|
empty( $post->post_parent )
|
||||||
return;
|
) {
|
||||||
$auto_add = $auto_add['auto_add'];
|
$auto_add = get_option( 'nav_menu_options' );
|
||||||
if ( empty( $auto_add ) || ! is_array( $auto_add ) )
|
if (
|
||||||
return;
|
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(
|
foreach ( $auto_add['auto_add'] as $menu_id ) {
|
||||||
'menu-item-object-id' => $post->ID,
|
$items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'draft,pending,publish' ) );
|
||||||
'menu-item-object' => $post->post_type,
|
if ( ! is_array( $items ) )
|
||||||
'menu-item-type' => 'post_type',
|
continue;
|
||||||
'menu-item-status' => 'publish',
|
foreach ( $items as $item ) {
|
||||||
);
|
if ( $post->ID == $item->object_id )
|
||||||
|
continue 2;
|
||||||
foreach ( $auto_add as $menu_id ) {
|
}
|
||||||
$items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
|
wp_update_nav_menu_item( $menu_id, 0, $args );
|
||||||
if ( ! is_array( $items ) )
|
}
|
||||||
continue;
|
}
|
||||||
foreach ( $items as $item ) {
|
}
|
||||||
if ( $post->ID == $item->object_id )
|
|
||||||
continue 2;
|
// 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 );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue