From 91421a7124bb31894787d62833e8968f8b37773f Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 10 Feb 2006 00:54:16 +0000 Subject: [PATCH] get_page_by_path. Some page uri cleanups. git-svn-id: https://develop.svn.wordpress.org/trunk@3511 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/classes.php | 16 ++-------------- wp-includes/functions-post.php | 16 ++++++++-------- wp-includes/functions.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 7567486eb4..deffcc787f 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -367,23 +367,11 @@ class WP_Query { $q['name'] = sanitize_title($q['name']); $where .= " AND post_name = '" . $q['name'] . "'"; } else if ('' != $q['pagename']) { + $reqpage = get_page_by_path($q['pagename']); $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); $page_paths = '/' . trim($q['pagename'], '/'); $q['pagename'] = sanitize_title(basename($page_paths)); $q['name'] = $q['pagename']; - $page_paths = explode('/', $page_paths); - foreach($page_paths as $pathdir) - $page_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); - - $all_page_ids = get_all_page_ids(); - $reqpage = 0; - if (is_array($all_page_ids)) { foreach ( $all_page_ids as $page_id ) { - $page = get_page($page_id); - if ( $page->fullpath == $page_path ) { - $reqpage = $page_id; - break; - } - } } $where .= " AND (ID = '$reqpage')"; } elseif ('' != $q['attachment']) { @@ -1412,7 +1400,7 @@ class WP_Rewrite { } function flush_rules() { - generate_page_rewrite_rules(); + generate_page_uri_index(); delete_option('rewrite_rules'); $this->wp_rewrite_rules(); if ( function_exists('save_mod_rewrite_rules') ) diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index 14fefe71d6..bee7d0e342 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -792,7 +792,7 @@ function get_page_hierarchy($posts, $parent = 0) { return $result; } -function generate_page_rewrite_rules() { +function generate_page_uri_index() { global $wpdb; //get pages in order of hierarchy, i.e. children after parents @@ -800,8 +800,8 @@ function generate_page_rewrite_rules() { //now reverse it, because we need parents after children for rewrite rules to work properly $posts = array_reverse($posts, true); - $page_rewrite_rules = array(); - $page_attachment_rewrite_rules = array(); + $page_uris = array(); + $page_attachment_uris = array(); if ($posts) { @@ -813,17 +813,17 @@ function generate_page_rewrite_rules() { if ( $attachments ) { foreach ( $attachments as $attachment ) { $attach_uri = get_page_uri($attachment->ID); - $page_attachment_rewrite_rules[$attach_uri] = $attachment->post_name; + $page_attachment_uris[$attach_uri] = $attachment->ID; } } - $page_rewrite_rules[$uri] = $post; + $page_uris[$uri] = $id; } - update_option('page_uris', $page_rewrite_rules); + update_option('page_uris', $page_uris); - if ( $page_attachment_rewrite_rules ) - update_option('page_attachment_uris', $page_attachment_rewrite_rules); + if ( $page_attachment_uris ) + update_option('page_attachment_uris', $page_attachment_uris); } } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index e065e1ea14..7b6c30afe4 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -665,6 +665,36 @@ function set_page_path($page) { return $page; } +function get_page_by_path($page_path) { + global $wpdb; + + $page_path = str_replace('%2F', '/', urlencode(urldecode($page_path))); + $page_paths = '/' . trim($page_path, '/'); + $leaf_path = sanitize_title(basename($page_paths)); + $page_paths = explode('/', $page_paths); + foreach($page_paths as $pathdir) + $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); + + $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path'"); + + if ( empty($pages) ) + return 0; + + foreach ($pages as $page) { + $path = '/' . $leaf_path; + $curpage = $page; + while ($curpage->post_parent != 0) { + $curpage = $wpdb->get_row("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = '$curpage->post_parent'"); + $path = '/' . $curpage->post_name . $path; + } + + if ( $path == $full_path ) + return $page->ID; + } + + return 0; +} + // Retrieves page data given a page ID or page object. // Handles page caching. function &get_page(&$page, $output = OBJECT) {