Add get_category_by_path(). Remove old fullpath stuff.

git-svn-id: http://svn.automattic.com/wordpress/trunk@3576 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-02-28 08:00:39 +00:00
parent 740d8f1c1f
commit 6eadde74ac
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")) { if ($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")) {
foreach ($dogs as $catt) foreach ($dogs as $catt)
$this->cache['category'][$catt->cat_ID] = $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 } else
if ('options' == $group) { if ('options' == $group) {

View File

@ -359,6 +359,10 @@ class WP_Query {
$where .= " AND post_name = '" . $q['name'] . "'"; $where .= " AND post_name = '" . $q['name'] . "'";
} else if ('' != $q['pagename']) { } else if ('' != $q['pagename']) {
$reqpage = get_page_by_path($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']))); $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
$page_paths = '/' . trim($q['pagename'], '/'); $page_paths = '/' . trim($q['pagename'], '/');
$q['pagename'] = sanitize_title(basename($page_paths)); $q['pagename'] = sanitize_title(basename($page_paths));
@ -463,28 +467,27 @@ class WP_Query {
global $cache_categories; global $cache_categories;
if ('' != $q['category_name']) { 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']), '/'); $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
$q['category_name'] = sanitize_title(basename($cat_paths)); $q['category_name'] = sanitize_title(basename($cat_paths));
$cat_paths = explode('/', $cat_paths); $cat_paths = explode('/', $cat_paths);
foreach($cat_paths as $pathdir) foreach($cat_paths as $pathdir)
$cat_path .= ($pathdir!=''?'/':'') . sanitize_title($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 we don't match the entire hierarchy fallback on just matching the nicename
if (!$q['cat'] && $partial_match) { if ( empty($reqcat) )
$q['cat'] = $partial_match; $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"; $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) "; $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) ";

View File

@ -650,21 +650,7 @@ function &get_children($post = 0, $output = OBJECT) {
} }
} }
function set_page_path($page) { function get_page_by_path($page_path, $output = OBJECT) {
$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) {
global $wpdb; global $wpdb;
$page_path = rawurlencode(urldecode($page_path)); $page_path = rawurlencode(urldecode($page_path));
$page_path = str_replace('%2F', '/', $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'"); $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) ) if ( empty($pages) )
return 0; return NULL;
foreach ($pages as $page) { foreach ($pages as $page) {
$path = '/' . $leaf_path; $path = '/' . $leaf_path;
@ -689,10 +675,10 @@ function get_page_by_path($page_path) {
} }
if ( $path == $full_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. // 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 ) { if ( $output == OBJECT ) {
return $_page; return $_page;
} elseif ( $output == ARRAY_A ) { } elseif ( $output == ARRAY_A ) {
@ -823,18 +804,39 @@ function walk_page_tree($pages, $to_depth, $start_element_callback, $end_element
return $output; return $output;
} }
function set_category_path($cat) { function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
$cat->fullpath = '/' . $cat->category_nicename; global $wpdb;
$path = $cat->fullpath; $category_path = rawurlencode(urldecode($category_path));
$curcat = $cat; $category_path = str_replace('%2F', '/', $category_path);
while ($curcat->category_parent != 0) { $category_path = str_replace('%20', ' ', $category_path);
$curcat = get_category($curcat->category_parent); $category_paths = '/' . trim($category_path, '/');
$path = '/' . $curcat->category_nicename . $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;
} }
$cat->fullpath = $path; if ( $path == $full_path )
return get_category($category->cat_ID, $output);
}
return $cat; // 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 NULL;
} }
// Retrieves category data given a category ID or category object. // 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 ) { if ( $output == OBJECT ) {
return $_category; return $_category;
} elseif ( $output == ARRAY_A ) { } elseif ( $output == ARRAY_A ) {