Advertisement
Viper007Bond

Untitled

Sep 3rd, 2011
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php /*
  2.  
  3. Plugin Name: Add Children As Menu Items
  4.  
  5. Work in progress! Mostly a mockup at this point. -Alex
  6.  
  7. */
  8.  
  9. //add_filter( 'wp_nav_menu_objects', 'filter_var_dump', 99 );
  10. function filter_var_dump( $var ) {
  11.     var_dump( $var );
  12.     return $var;
  13. }
  14.  
  15.  
  16. class Add_Children_As_Menu_Items {
  17.  
  18.     public $added = array();
  19.  
  20.     function __construct() {
  21.         if ( ! is_admin() ) {
  22.             add_filter( 'wp_get_nav_menu_items', array( &$this, 'add_child_pages_to_menu' ), 10, 3 );
  23.         }
  24.     }
  25.  
  26.     // Add all decendent pages to top level menu items that are pages
  27.     function add_child_pages_to_menu( $items, $menu, $args ) {
  28.         $menu_order = count( $items ) + 1000;
  29.         $filter_added = false;
  30.  
  31.         foreach ( $items as $item ) {
  32.             // Skip menu items that aren't top level or aren't post_type=page
  33.             if ( 0 != $item->menu_item_parent || 'page' != $item->object )
  34.                 continue;
  35.  
  36.             // Get all decendent pages
  37.             $children = get_pages( array(
  38.                 'child_of' => $item->object_id,
  39.                 'sort_column' => 'menu_order',
  40.             ) );
  41.  
  42.             if ( ! $children )
  43.                 continue;
  44.  
  45.             // Menu items are being added, so later fix the "current" values for highlighting
  46.             if ( ! $filter_added )
  47.                 add_filter( 'wp_nav_menu_objects',  array( &$this, 'fix_menu_current_item' ) );
  48.  
  49.             // Add each child page to the menu
  50.             foreach ( $children as $child ) {
  51.                 $this->added[$child->ID] = true; // We'll need this later
  52.  
  53.                 $child = wp_setup_nav_menu_item( $child );
  54.                 $child->db_id = $child->ID;
  55.  
  56.                 // Set the parent menu item.
  57.                 // If the current page is a child of the current menu item,
  58.                 // then use that menu item's ID otherwise use the real ID
  59.                 // because it's a menu item that this plugin added itself.
  60.                 if ( $child->post_parent == $item->object_id )
  61.                     $child->menu_item_parent = $item->ID; // Children
  62.                 else
  63.                     $child->menu_item_parent = $child->post_parent; // Grandchildren, etc.
  64.  
  65.                 // The menu_order has to be unique, so make up new ones
  66.                 // The items are already sorted anyway due to the get_posts()
  67.                 $menu_order++;
  68.                 $child->menu_order = $menu_order;
  69.  
  70.                 $items[] = $child;
  71.             }
  72.         }
  73.  
  74.         //var_dump( $items );
  75.  
  76.         return $items;
  77.     }
  78.  
  79.     // Fixes the ancestor/parent settings/classes when viewing a menu item added by this plugin
  80.     function fix_menu_current_item( $items ) {
  81.         global $wp_query;
  82.  
  83.         $queried_object = $wp_query->get_queried_object();
  84.         $queried_object_id = (int) $wp_query->queried_object_id;
  85.  
  86.         // Only need to fix items added by this plugin
  87.         if ( empty( $this->added[$queried_object_id] ) )
  88.             return $items;
  89.  
  90.         _get_post_ancestors( $queried_object );
  91.  
  92.         foreach ( $items as $item ) {
  93.             if ( ! in_array( $item->object_id, $queried_object->ancestors ) )
  94.                 continue;
  95.  
  96.             $item->current_item_ancestor = true;
  97.             $item->classes[] = 'current-menu-ancestor';
  98.             $item->classes[] = 'current_page_ancestor';
  99.  
  100.             // If menu item is direct parent of current page
  101.             if ( $item->object_id == $queried_object->post_parent ) {
  102.                 $item->current_item_parent = true;
  103.                 $item->classes[] = 'current-menu-parent';
  104.                 $item->classes[] = 'current_page_parent';
  105.             }
  106.         }
  107.  
  108.         return $items;
  109.     }
  110. }
  111.  
  112. $add_children_as_menu_items = new Add_Children_As_Menu_Items();
  113.  
  114. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement