WordPress.org

Make WordPress Core

Ticket #10853: 10853_page_hier.diff

File 10853_page_hier.diff, 1.9 KB (added by hailin, 9 years ago)

patch

  • C:/xampp/htdocs/wordpress_trunk/wp-includes/post.php

     
    21552155/**
    21562156 * Order the pages with children under parents in a flat list.
    21572157 *
     2158 * It uses auxiliary structure to hold parent-children relationships and
     2159 * runs in O(N) complexity
     2160 *
    21582161 * @since 2.0.0
    21592162 *
    21602163 * @param array $posts Posts array.
    21612164 * @param int $parent Parent page ID.
    21622165 * @return array A list arranged by hierarchy. Children immediately follow their parents.
    21632166 */
    2164 function get_page_hierarchy($posts, $parent = 0) {
    2165         $result = array ( );
    2166         if ($posts) { foreach ( (array) $posts as $post) {
    2167                 if ($post->post_parent == $parent) {
    2168                         $result[$post->ID] = $post->post_name;
    2169                         $children = get_page_hierarchy($posts, $post->ID);
    2170                         $result += $children; //append $children to $result
    2171                 }
    2172         } }
     2167function &get_page_hierarchy( &$pages, $page_id = 0 ) {
     2168       
     2169        if ( empty( $pages ) )
     2170                return null;
     2171               
     2172        $children = array();
     2173        foreach ( (array) $pages as $p ) {
     2174               
     2175                $parent_id = intval( $p->post_parent );
     2176                $children[ $parent_id ][] = $p;
     2177         }
     2178         
     2179         $result = array();
     2180         _page_traverse_name( $page_id, $children, $result );
     2181         
    21732182        return $result;
    21742183}
    21752184
    21762185/**
     2186 * function to traverse and return all the nested children post names of a root page.
     2187 * $children contains parent-chilren relations
     2188 *
     2189 */
     2190function _page_traverse_name( $page_id, &$children, &$result ){
     2191       
     2192        if ( isset( $children[ $page_id ] ) ){
     2193               
     2194                foreach( (array)$children[ $page_id ] as $child ) {
     2195                       
     2196                        $result[ $child->ID ] = $child->post_name;
     2197                        _page_traverse_name( $child->ID, $children, $result );
     2198                }
     2199        }
     2200}
     2201
     2202/**
    21772203 * Builds URI for a page.
    21782204 *
    21792205 * Sub pages will be in the "directory" under the parent page post name.