Ticket #10852: 10852_get_page_children.diff

File 10852_get_page_children.diff, 2.0 KB (added by hailin, 4 years ago)

patch

Line 
1Index: C:/xampp/htdocs/wordpress_trunk/wp-includes/post.php
2===================================================================
3--- C:/xampp/htdocs/wordpress_trunk/wp-includes/post.php        (revision 11972)
4+++ C:/xampp/htdocs/wordpress_trunk/wp-includes/post.php        (working copy)
5@@ -2129,27 +2129,49 @@
6 
7 /**
8  * Retrieve child pages from list of pages matching page ID.
9+ * It returns the entire list of nested descendant pages rooted from page_id
10  *
11- * Matches against the pages parameter against the page ID. Also matches all
12- * children for the same to retrieve all children of a page. Does not make any
13- * SQL queries to get the children.
14- *
15  * @since 1.5.1
16  *
17  * @param int $page_id Page ID.
18  * @param array $pages List of pages' objects.
19  * @return array
20  */
21-function &get_page_children($page_id, $pages) {
22-       $page_list = array();
23-       foreach ( (array) $pages as $page ) {
24-               if ( $page->post_parent == $page_id ) {
25-                       $page_list[] = $page;
26-                       if ( $children = get_page_children($page->ID, $pages) )
27-                               $page_list = array_merge($page_list, $children);
28+function &get_page_children( $page_id, &$pages ) {
29+       
30+       /*
31+        * use auxiliary array structure to store the parent-children relations,
32+        * which allow O(1) per lookup, and gives O(N) overall complexity
33+        * ex: children[10][] contains all the children pages whose parent is 10
34+        */
35+        $children = array();
36+        foreach ( (array) $pages as $p ) {
37+               
38+               $parent_id = intval( $p->post_parent );
39+               $children[ $parent_id ][] = $p;
40+        }
41+       
42+        $page_list = array();
43+        _page_traverse( $page_id, $children, $page_list );
44+       
45+       return $page_list;
46+}
47+
48+/**
49+ * function to traverse and return all the nested children of a root page.
50+ * $children contains parent-chilren relations
51+ *
52+ */
53+function _page_traverse( $page_id, &$children, &$page_list ){
54+       
55+       if ( isset( $children[ $page_id ] ) ){
56+               
57+               foreach( (array)$children[ $page_id ] as $child ) {
58+                       
59+                       $page_list[] = $child;
60+                       _page_traverse( $child->ID, $children, $page_list );
61                }
62        }
63-       return $page_list;
64 }
65 
66 /**