From a1d0a8e672bc69a6c51ba7fbfa90e9578d31dcd6 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 28 Feb 2006 08:00:39 +0000 Subject: [PATCH] Add get_category_by_path(). Remove old fullpath stuff. git-svn-id: https://develop.svn.wordpress.org/trunk@3576 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/cache.php | 10 ------ wp-includes/classes.php | 33 +++++++++--------- wp-includes/functions.php | 71 +++++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 62 deletions(-) diff --git a/wp-includes/cache.php b/wp-includes/cache.php index dc3b7f92d2..2f138f8443 100644 --- a/wp-includes/cache.php +++ b/wp-includes/cache.php @@ -181,16 +181,6 @@ class WP_Object_Cache { if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) { foreach ($dogs as $catt) $this->cache['category'][$catt->cat_ID] = $catt; - - foreach ($this->cache['category'] as $catt) { - $curcat = $catt->cat_ID; - $fullpath = '/'.$this->cache['category'][$catt->cat_ID]->category_nicename; - while ($this->cache['category'][$curcat]->category_parent != 0) { - $curcat = $this->cache['category'][$curcat]->category_parent; - $fullpath = '/'.$this->cache['category'][$curcat]->category_nicename.$fullpath; - } - $this->cache['category'][$catt->cat_ID]->fullpath = $fullpath; - } } } else if ('options' == $group) { diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 9e22a99e57..adb0b92ae6 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -359,6 +359,10 @@ class WP_Query { $where .= " AND post_name = '" . $q['name'] . "'"; } else if ('' != $q['pagename']) { $reqpage = get_page_by_path($q['pagename']); + if ( !empty($reqpage) ) + $reqpage = $reqpage->ID; + else + $reqpage = 0; $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename']))); $page_paths = '/' . trim($q['pagename'], '/'); $q['pagename'] = sanitize_title(basename($page_paths)); @@ -463,29 +467,28 @@ class WP_Query { global $cache_categories; if ('' != $q['category_name']) { + $reqcat = get_category_by_path($q['category_name']); + $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name']))); + $cat_paths = '/' . trim($q['category_name'], '/'); + $q['category_name'] = sanitize_title(basename($cat_paths)); + $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); $q['category_name'] = sanitize_title(basename($cat_paths)); $cat_paths = explode('/', $cat_paths); foreach($cat_paths as $pathdir) $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); - $all_cat_ids = get_all_category_ids(); - $q['cat'] = 0; $partial_match = 0; - foreach ( $all_cat_ids as $cat_id ) { - $cat = get_category($cat_id); - if ( $cat->fullpath == $cat_path ) { - $q['cat'] = $cat_id; - break; - } elseif ( $cat->category_nicename == $q['category_name'] ) { - $partial_match = $cat_id; - } - } - //if we don't match the entire hierarchy fallback on just matching the nicename - if (!$q['cat'] && $partial_match) { - $q['cat'] = $partial_match; - } + if ( empty($reqcat) ) + $reqcat = get_category_by_path($q['category_name'], false); + if ( !empty($reqcat) ) + $reqcat = $reqcat->cat_ID; + else + $reqcat = 0; + + $q['cat'] = $reqcat; + $tables = ", $wpdb->post2cat, $wpdb->categories"; $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; $whichcat = " AND (category_id = '" . $q['cat'] . "'"; diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 9948d6bba0..c7844714ca 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -650,21 +650,7 @@ function &get_children($post = 0, $output = OBJECT) { } } -function set_page_path($page) { - $page->fullpath = '/' . $page->post_name; - $path = $page->fullpath; - $curpage = $page; - while ($curpage->post_parent != 0) { - $curpage = get_page($curpage->post_parent); - $path = '/' . $curpage->post_name . $path; - } - - $page->fullpath = $path; - - return $page; -} - -function get_page_by_path($page_path) { +function get_page_by_path($page_path, $output = OBJECT) { global $wpdb; $page_path = rawurlencode(urldecode($page_path)); $page_path = str_replace('%2F', '/', $page_path); @@ -678,7 +664,7 @@ function get_page_by_path($page_path) { $pages = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = '$leaf_path' AND post_type='page'"); if ( empty($pages) ) - return 0; + return NULL; foreach ($pages as $page) { $path = '/' . $leaf_path; @@ -689,10 +675,10 @@ function get_page_by_path($page_path) { } if ( $path == $full_path ) - return $page->ID; + return get_page($page->ID, $output); } - return 0; + return NULL; } // Retrieves page data given a page ID or page object. @@ -729,11 +715,6 @@ function &get_page(&$page, $output = OBJECT) { } } - if (!isset($_page->fullpath)) { - $_page = set_page_path($_page); - wp_cache_replace($_page->ID, $_page, 'pages'); - } - if ( $output == OBJECT ) { return $_page; } elseif ( $output == ARRAY_A ) { @@ -823,18 +804,39 @@ function walk_page_tree($pages, $to_depth, $start_element_callback, $end_element return $output; } -function set_category_path($cat) { - $cat->fullpath = '/' . $cat->category_nicename; - $path = $cat->fullpath; - $curcat = $cat; - while ($curcat->category_parent != 0) { - $curcat = get_category($curcat->category_parent); - $path = '/' . $curcat->category_nicename . $path; +function get_category_by_path($category_path, $full_match = true, $output = OBJECT) { + global $wpdb; + $category_path = rawurlencode(urldecode($category_path)); + $category_path = str_replace('%2F', '/', $category_path); + $category_path = str_replace('%20', ' ', $category_path); + $category_paths = '/' . trim($category_path, '/'); + $leaf_path = sanitize_title(basename($category_paths)); + $category_paths = explode('/', $category_paths); + foreach($category_paths as $pathdir) + $full_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); + + $categories = $wpdb->get_results("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE category_nicename = '$leaf_path'"); + + if ( empty($categories) ) + return NULL; + + foreach ($categories as $category) { + $path = '/' . $leaf_path; + $curcategory = $category; + while ($curcategory->category_parent != 0) { + $curcategory = $wpdb->get_row("SELECT cat_ID, category_nicename, category_parent FROM $wpdb->categories WHERE cat_ID = '$curcategory->category_parent'"); + $path = '/' . $curcategory->category_nicename . $path; + } + + if ( $path == $full_path ) + return get_category($category->cat_ID, $output); } - $cat->fullpath = $path; + // If full matching is not required, return the first cat that matches the leaf. + if ( ! $full_match ) + return get_category($categories[0]->cat_ID, $output); - return $cat; + return NULL; } // Retrieves category data given a category ID or category object. @@ -855,11 +857,6 @@ function &get_category(&$category, $output = OBJECT) { } } - if ( !isset($_category->fullpath) ) { - $_category = set_category_path($_category); - wp_cache_replace($_category->cat_ID, $_category, 'category'); - } - if ( $output == OBJECT ) { return $_category; } elseif ( $output == ARRAY_A ) {