Initial sub-categories code from Jason Verber

git-svn-id: https://develop.svn.wordpress.org/trunk@798 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Matt Mullenweg 2004-01-27 06:35:07 +00:00
parent 3ca7dad5d1
commit 4b4a1405f3
5 changed files with 1723 additions and 1598 deletions

View File

@ -14,6 +14,11 @@ if (!isset($blogID)) $blog_ID=1;
if (!isset($debug)) $debug=0;
timer_start();
$dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
foreach ($dogs as $catt) {
$cache_categories[$catt->cat_ID] = $catt;
}
get_currentuserinfo();
$posts_per_page = get_settings('posts_per_page');

View File

@ -49,7 +49,7 @@ case 'addcat':
$category_nicename = sanitize_title($cat_name);
$category_description = addslashes(stripslashes(stripslashes($HTTP_POST_VARS['category_description'])));
$wpdb->query("INSERT INTO $tablecategories (cat_ID, cat_name, category_nicename, category_description) VALUES ('0', '$cat_name', '$category_nicename', '$category_description')");
$wpdb->query("INSERT INTO $tablecategories (cat_ID, cat_name, category_nicename, category_description, category_parent) VALUES ('0', '$cat_name', '$category_nicename', '$category_description', $cat)");
header('Location: categories.php');
@ -91,6 +91,8 @@ case 'edit':
<input type="hidden" name="cat_ID" value="<?php echo $HTTP_GET_VARS['cat_ID'] ?>" />
<p>Category name:<br />
<input type="text" name="cat_name" value="<?php echo $cat_name; ?>" /></p>
<p>Category parent:<br />
<?php dropdown_cats(FALSE, '', 'name', 'asc', FALSE, FALSE, FALSE, TRUE, $category->category_parent, $HTTP_GET_VARS['cat_ID']); ?></p>
<p>Description:<br />
<textarea name="category_description" rows="5" cols="50" style="width: 97%;"><?php echo htmlentities($category->category_description); ?></textarea></p>
<p><input type="submit" name="submit" value="Edit it!" class="search" /></p>
@ -114,7 +116,7 @@ case 'editedcat':
$category_nicename = sanitize_title($cat_name);
$category_description = $HTTP_POST_VARS['category_description'];
$wpdb->query("UPDATE $tablecategories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description' WHERE cat_ID = $cat_ID");
$wpdb->query("UPDATE $tablecategories SET cat_name = '$cat_name', category_nicename = '$category_nicename', category_description = '$category_description', category_parent = $cat WHERE cat_ID = $cat_ID");
header('Location: categories.php');
@ -134,6 +136,7 @@ default:
<table width="100%" cellpadding="3" cellspacing="3">
<tr>
<th scope="col">Name</th>
<th scope="col">Parent</th>
<th scope="col">Description</th>
<th scope="col"># Posts</th>
<th colspan="2">Action</th>
@ -141,9 +144,12 @@ default:
<?php
$categories = $wpdb->get_results("SELECT * FROM $tablecategories ORDER BY cat_name");
foreach ($categories as $category) {
$parent = "None";
if ($category->category_parent) $parent = $wpdb->get_var("SELECT cat_name FROM $tablecategories WHERE cat_ID = $category->category_parent");
$count = $wpdb->get_var("SELECT COUNT(post_id) FROM $tablepost2cat WHERE category_id = $category->cat_ID");
$bgcolor = ('#eee' == $bgcolor) ? 'none' : '#eee';
echo "<tr style='background-color: $bgcolor'><td>$category->cat_name</td>
<td>$parent</td>
<td>$category->category_description</td>
<td>$count</td>
<td><a href='categories.php?action=edit&amp;cat_ID=$category->cat_ID' class='edit'>Edit</a></td><td><a href='categories.php?action=Delete&amp;cat_ID=$category->cat_ID' onclick=\"return confirm('You are about to delete the category \'". addslashes($category->cat_name) ."\' and all its posts will go to the default category.\\n \'OK\' to delete, \'Cancel\' to stop.')\" class='delete'>Delete</a></td>
@ -159,6 +165,8 @@ default:
<p>Name:<br />
<input type="text" name="cat_name" value="" /></p>
<p>Category parent:<br />
<?php dropdown_cats(FALSE, '', 'name', 'asc', FALSE, FALSE, FALSE, TRUE); ?></p>
<p>Description: (optional) <br />
<textarea name="category_description" rows="5" cols="50" style="width: 97%;"></textarea></p>
<p><input type="hidden" name="action" value="addcat" /><input type="submit" name="submit" value="Add" class="search" /></p>

View File

@ -140,6 +140,11 @@ if (!empty($s)) {
}
// category stuff
$dogs = $wpdb->get_results("SELECT * FROM $tablecategories WHERE 1=1");
foreach ($dogs as $catt) {
$cache_categories[$catt->cat_ID] = $catt;
}
if ((empty($cat)) || ($cat == 'all') || ($cat == '0')) {
$whichcat='';
} else {
@ -157,8 +162,10 @@ if ((empty($cat)) || ($cat == 'all') || ($cat == '0')) {
$join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) ";
$cat_array = explode(' ',$cat);
$whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
$whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
$whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
$whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
}
$whichcat .= ')';
}
@ -166,11 +173,21 @@ if ((empty($cat)) || ($cat == 'all') || ($cat == '0')) {
// Category stuff for nice URIs
if ('' != $category_name) {
if (stristr($category_name,'/')) {
$category_name = explode('/',$category_name);
if ($category_name[count($category_name)-1]) {
$category_name = $category_name[count($category_name)-1];#no trailing slash
} else {
$category_name = $category_name[count($category_name)-2];#there was a trailling slash
}
}
$category_name = preg_replace('|[^a-z0-9-]|', '', $category_name);
$tables = ", $tablepost2cat, $tablecategories";
$join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) ";
$whichcat = " AND (category_nicename = '$category_name') ";
$whichcat = " AND (category_nicename = '$category_name'";
$cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'");
$whichcat .= get_category_children($cat, " OR category_id = ");
$whichcat .= ")";
}
// author stuff
@ -330,7 +347,7 @@ if ($posts) {
$post_id_list = implode(',', $post_id_list);
$dogs = $wpdb->get_results("SELECT DISTINCT
ID, category_id, cat_name, category_nicename, category_description
ID, category_id, cat_name, category_nicename, category_description, category_parent
FROM $tablecategories, $tablepost2cat, $tableposts
WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)");

View File

@ -1336,7 +1336,7 @@ function get_the_category() {
return $category_cache[$post->ID];
} else {
$categories = $wpdb->get_results("
SELECT category_id, cat_name, category_nicename, category_description
SELECT category_id, cat_name, category_nicename, category_description, category_parent
FROM $tablecategories, $tablepost2cat
WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = $post->ID
");
@ -1346,7 +1346,7 @@ function get_the_category() {
}
function get_category_link($echo = false, $category_id, $category_nicename) {
global $wpdb, $tablecategories, $post, $querystring_start, $querystring_equal, $siteurl, $blogfilename;
global $wpdb, $tablecategories, $post, $querystring_start, $querystring_equal, $siteurl, $blogfilename, $cache_categories;
$cat_ID = $category_id;
$permalink_structure = get_settings('permalink_structure');
@ -1354,23 +1354,39 @@ function get_category_link($echo = false, $category_id, $category_nicename) {
$file = "$siteurl/$blogfilename";
$link = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
} else {
if ('' == $category_nicename) $category_nicename = $wpdb->get_var("SELECT category_nicename FROM $tablecategories WHERE cat_ID = $category_id");
if ('' == $category_nicename) $category_nicename = $cache_categories[$category_id]->category_nicename;
// Get any static stuff from the front
$front = substr($permalink_structure, 0, strpos($permalink_structure, '%'));
$link = $siteurl . $front . 'category/' . $category_nicename . '/';
$link = $siteurl . $front . 'category/';
if ($parent=$cache_categories[$category_id]->category_parent) $link .= get_category_parents($parent, FALSE, '/', TRUE);
$link .= $category_nicename . '/';
}
if ($echo) echo $link;
return $link;
}
function the_category($seperator = '') {
function the_category($seperator = '', $parents='') {
$categories = get_the_category();
if ('' == $seperator) {
echo '<ul class="post-categories">';
foreach ($categories as $category) {
$category->cat_name = stripslashes($category->cat_name);
echo "\n\t<li><a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>$category->cat_name</a></li>";
echo "\n\t<li>";
switch(strtolower($parents)) {
case 'multiple':
if ($category->category_parent) echo get_category_parents($category->category_parent, TRUE);
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>$category->cat_name</a></li>";
break;
case 'single':
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>";
if ($category->category_parent)echo get_category_parents($category->category_parent, FALSE);
echo "$category->cat_name</a></li>";
break;
case '':
default:
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>$category->cat_name</a></li>";
}
}
echo '</ul>';
} else {
@ -1378,7 +1394,18 @@ function the_category($seperator = '') {
foreach ($categories as $category) {
$category->cat_name = stripslashes($category->cat_name);
if (0 < $i) echo $seperator . ' ';
switch(strtolower($parents)) {
case 'multiple':
if ($category->category_parent) echo get_category_parents($category->category_parent, TRUE);
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>$category->cat_name</a>";
case 'single':
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>";
if ($category->category_parent)echo get_category_parents($category->category_parent, FALSE);
echo "$category->cat_name</a>";
case '':
default:
echo "<a href='" . get_category_link(0, $category->category_id, $category->category_nicename) . "' title='View all posts in $category->cat_name'>$category->cat_name</a>";
}
++$i;
}
}
@ -1402,19 +1429,48 @@ function the_category_unicode() {
echo convert_chars($category, 'unicode');
}
function get_the_category_by_ID($cat_ID) {
global $tablecategories, $cache_categories, $use_cache, $wpdb;
if ((!$cache_categories[$cat_ID]) OR (!$use_cache)) {
$cat_name = $wpdb->get_var("SELECT cat_name FROM $tablecategories WHERE cat_ID = '$cat_ID'");
$cache_categories[$cat_ID] = $cat_name;
$cache_categories[$cat_ID]->cat_name = $cat_name;
} else {
$cat_name = $cache_categories[$cat_ID];
$cat_name = $cache_categories[$cat_ID]->cat_name;
}
return(stripslashes($cat_name));
}
function get_category_parents($id, $link=FALSE, $separator=' / ', $nicename=FALSE){
global $tablecategories, $cache_categories;
$chain = "";
$parent = $cache_categories[$id];
if ($nicename) {
$name = $parent->category_nicename;
} else {
$name = $parent->cat_name;
}
if ($parent->category_parent) $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
if ($link) {
$chain .= "<a href='" . get_category_link(0, $parent->cat_ID, $parent->category_nicename) . "' title='View all posts in $parent->cat_name'>$name</a>" . $separator;
} else {
$chain .= $name.$separator;
}
return $chain;
}
function get_category_children($id, $before=' / ', $after='') {
global $tablecategories, $cache_categories;
$c_cache=$cache_categories;#can't do recursive foreach on a global, have to make a copy
$chain = "";
foreach ($c_cache as $category){
if ($category->category_parent == $id){
$chain .= $before.$category->cat_ID.$after;
$chain .= get_category_children($category->cat_ID, $before, $after);
}
}
return $chain;
}
function the_category_ID($echo=true) {
global $post;
if ($echo)
@ -1435,28 +1491,38 @@ function the_category_head($before='', $after='') {
}
function category_description($category = 0) {
global $cat, $wpdb, $tablecategories;
global $cat, $wpdb, $tablecategories, $cache_categories;
if (!$category) $category = $cat;
$category_description = $wpdb->get_var("SELECT category_description FROM $tablecategories WHERE cat_ID = $category");
$category_description = $cache_categories[$category]->category_description;
$category_description = apply_filters('category_description', $category_description);
return $category_description;
}
// out of the WordPress loop
function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
$optiondates = 0, $optioncount = 0, $hide_empty = 1) {
global $cat, $tablecategories, $tableposts, $wpdb;
$optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
$selected=0, $hide=0) {
global $tablecategories, $tableposts, $tablepost2cat, $wpdb;
global $pagenow, $siteurl, $blogfilename;
global $querystring_start, $querystring_equal, $querystring_separator;
if (($file == 'blah') || ($file == '')) $file = "$siteurl/$blogfilename";
if (!$selected) $selected=$cat;
$sort_column = 'cat_'.$sort_column;
$query = " SELECT cat_ID, cat_name,";
$query .= " COUNT($tableposts.ID) AS cat_count,";
$query .= " DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth";
$query .= " FROM $tablecategories LEFT JOIN $tableposts ON cat_ID = post_category";
$query .= " WHERE cat_ID > 0 ";
$query .= " GROUP BY post_category ";
if (intval($hide_empty) == 1) {
$query .= " HAVING cat_count > 0";
$query = "
SELECT cat_ID, cat_name, category_nicename,category_parent,
COUNT($tablepost2cat.post_id) AS cat_count,
DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
FROM $tablecategories LEFT JOIN $tablepost2cat ON (cat_ID = category_id)
LEFT JOIN $tableposts ON (ID = post_id)
WHERE cat_ID > 0
";
if ($hide) {
$query .= " AND cat_ID != $hide";
$query .= get_category_children($hide, " AND cat_ID != ");
}
$query .=" GROUP BY cat_ID";
if (intval($hide_empty) == 1) $query .= " HAVING cat_count > 0";
$query .= " ORDER BY $sort_column $sort_order, post_date DESC";
$categories = $wpdb->get_results($query);
@ -1465,27 +1531,25 @@ function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_
$all = apply_filters('list_cats', $all);
echo "\t<option value='all'>$all</option>\n";
}
if (intval($optionnone) == 1) echo "\t<option value='0'>None</option>\n";
if ($categories) {
foreach ($categories as $category) {
$cat_name = apply_filters('list_cats', $category->cat_name);
echo "\t<option value=\"".$category->cat_ID."\"";
if ($category->cat_ID == $cat)
if ($category->cat_ID == $selected)
echo ' selected="selected"';
echo '>'.stripslashes($cat_name);
if (intval($optioncount) == 1) {
echo '&nbsp;&nbsp;('.$category->cat_count.')';
}
if (intval($optiondates) == 1) {
echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth;
}
echo '>';
echo stripslashes($cat_name);
if (intval($optioncount) == 1) echo '&nbsp;&nbsp;('.$category->cat_count.')';
if (intval($optiondates) == 1) echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth;
echo "</option>\n";
}
}
echo "</select>\n";
}
// Out of the WordPress loop
function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 0) {
// out of the WordPress loop
function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', $file = '', $list = true, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $use_desc_for_title = 0, $children=FALSE, $child_of=0, $categories=0, $recurse=0) {
global $tablecategories, $tableposts, $tablepost2cat, $wpdb;
global $pagenow, $siteurl, $blogfilename;
global $querystring_start, $querystring_equal, $querystring_separator;
@ -1493,16 +1557,17 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
if ('' == $file) {
$file = "$siteurl/$blogfilename";
}
if (intval($categories)==0){
$sort_column = 'cat_'.$sort_column;
$query = "
SELECT cat_ID, cat_name, category_nicename, category_description
SELECT cat_ID, cat_name, category_nicename, category_description, category_parent
FROM $tablecategories
WHERE cat_ID > 0
ORDER BY $sort_column $sort_order";
$categories = $wpdb->get_results($query);
}
if (intval($hide_empty) == 1 || intval($optioncount) == 1) {
$cat_counts = $wpdb->get_results(" SELECT cat_ID,
COUNT($tablepost2cat.post_id) AS cat_count
@ -1526,17 +1591,22 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
}
}
if (!$categories) {
if (intval($optionall) == 1 && !$child_of && $categories) {
$all = apply_filters('list_cats', $all);
$link = "<a href=\"".$file.$querystring_start.'cat'.$querystring_equal.'all">'.$all."</a>";
if ($list) {
$before = '<li>';
$after = '</li>';
echo "\n\t<li>$link</li>";
} else {
echo "\t$link<br />\n";
}
echo $before . "No categories" . $after . "\n";
return;
}
$num_found=0;
$thelist = "";
foreach ($categories as $category) {
if (intval($hide_empty) == 0 || $category_posts["$category->cat_ID"] > 0) {
if ((intval($hide_empty) == 0 || $category_posts["$category->cat_ID"] > 0) && (!$children || $category->category_parent == $child_of)) {
$num_found++;
$link = '<a href="'.get_category_link(0, $category->cat_ID, $category->category_nicename).'" ';
if ($use_desc_for_title == 0 || empty($category->category_description)) {
$link .= 'title="View all posts filed under ' . htmlspecialchars($category->cat_name) . '"';
@ -1552,12 +1622,31 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
$link .= ' '.$category_lastday["$category->cat_ID"].'/'.$category_lastmonth["$category->cat_ID"];
}
if ($list) {
echo "\t<li>$link</li>\n";
$thelist .= "\t<li>$link\n";
} else {
echo "\t$link<br />\n";
$thelist .= "\t$link<br />\n";
}
if ($children) $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $children, $category->cat_ID, $categories, 1);
if ($list) $thelist .= "</li>\n";
}
}
if (!$num_found && !$child_of){
if ($list) {
$before = '<li>';
$after = '</li>';
}
echo $before . "No categories" . $after . "\n";
return;
}
if ($list && $child_of && $num_found && $recurse) {
$pre = "\t\t<ul class='children'>";
$post = "\t\t</ul>\n";
}
$thelist=$pre.$thelist.$post;
if ($recurse) {
return $thelist;
}
echo $thelist;
}
/***** // Category tags *****/
@ -1656,8 +1745,9 @@ function comment_author() {
$author = apply_filters('comment_auther', $author);
$author = convert_chars($author);
if (!empty($author)) {
echo $author;
} else {
echo $comment->comment_author;
}
else {
echo "Anonymous";
}
}

View File

@ -223,6 +223,11 @@ p, li, .feedback {
border-bottom: 1px solid #809080;
}
#menu ul ul ul.children {
padding-left:4px;
font-size:142%;
}
#wp-calendar {
border: 1px solid #ddd;
empty-cells: show;