<?php
/**
 * Plugin Name: Mark Parent Pages
 * Plugin URI: http://xavisys.com/wordpress-mark-parent-pages-plugin/
 * Description: This plugin simply adds a class (current_page_ancestor) to the li tag of each page that is an ancestor of the current one.  With a little of your own CSS, you can have nice collapsing menus
 * Version: 0.0.1
 * Author: Aaron D. Campbell
 * Author URI: http://xavisys.com/
 */

/**
 * Changelog:
 * 01/08/2008: 0.0.1
 *         - Original Version
 */

/*  Copyright 2006  Aaron D. Campbell  (email : wp_plugins@xavisys.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
class wpMarkParentPages
{
    public static function 
wp_list_pages($pageList) {
        
$domDoc = new DOMDocument();
        
$domDoc->loadXML("<wpMarkParentPages>{$pageList}</wpMarkParentPages>");

        
$xpath = new DOMXPath($domDoc);
        
$ancestors $xpath->query('//*[contains(@class, "current_page_item")]/ancestor::li');

        foreach (
$ancestors as $ancestorNode) {
            
$ancestorNode->setAttribute('class'$ancestorNode->getAttribute('class').' current_page_ancestor');
        }

        
$parentNode $domDoc->getElementsByTagName('wpMarkParentPages')->item(0);
        return 
str_replace(array('<wpMarkParentPages>''</wpMarkParentPages>'), ''$domDoc->saveXML($parentNode));
    }
}

add_filter('wp_list_pages', array('wpMarkParentPages''wp_list_pages'));