+ /**
+ * Retrieve all page and attachments for pages URIs.
+ *
+ * The attachments are for those that have pages as parents and will be
+ * retrieved.
+ *
+ * @since 2.5.0
+ * @access public
+ *
+ * @return array Array of page URIs as first element and attachment URIs as second element.
+ */
+ function page_uri_index() {
+ global $wpdb;
+
+ //get pages in order of hierarchy, i.e. children after parents
+ $posts = get_page_hierarchy($wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"));
+ //now reverse it, because we need parents after children for rewrite rules to work properly
+ $posts = array_reverse($posts, true);
+
+ $page_uris = array();
+ $page_attachment_uris = array();
+
+ if ( !$posts )
+ return array( array(), array() );
+
+ foreach ($posts as $id => $post) {
+ // URL => page name
+ $uri = get_page_uri($id);
+ $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
+ if ( $attachments ) {
+ foreach ( $attachments as $attachment ) {
+ $attach_uri = get_page_uri($attachment->ID);
+ $page_attachment_uris[$attach_uri] = $attachment->ID;
+ }
+ }
+
+ $page_uris[$uri] = $id;
+ }
+
+ return array( $page_uris, $page_attachment_uris );
+ }