Add get_category_by_path(). Remove old fullpath stuff.

git-svn-id: https://develop.svn.wordpress.org/trunk@3576 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2006-02-28 08:00:39 +00:00
parent 71232c4705
commit a1d0a8e672
3 changed files with 52 additions and 62 deletions

View File

@ -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) {

View File

@ -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'] . "'";

View File

@ -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 ) {