Code cleanup from Mark Jaquith. fixes #1746

git-svn-id: http://svn.automattic.com/wordpress/trunk@2943 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-10-12 17:01:50 +00:00
parent 65f1a4df26
commit b1f86c0a51
2 changed files with 357 additions and 309 deletions

View File

@ -1,56 +1,65 @@
<?php <?php
function get_the_author($idmode = '') { function get_the_author($idmode = '') {
global $authordata; global $authordata;
return apply_filters('the_author', $authordata->display_name); return apply_filters('the_author', $authordata->display_name);
} }
function the_author($idmode = '', $echo = true) { function the_author($idmode = '', $echo = true) {
if ($echo) echo get_the_author($idmode); if ( $echo )
echo get_the_author($idmode);
return get_the_author($idmode); return get_the_author($idmode);
} }
function get_the_author_description() { function get_the_author_description() {
global $authordata; global $authordata;
return $authordata->user_description; return $authordata->user_description;
} }
function the_author_description() { function the_author_description() {
echo get_the_author_description(); echo get_the_author_description();
} }
function get_the_author_login() { function get_the_author_login() {
global $id,$authordata; return $authordata->user_login; global $authordata;
return $authordata->user_login;
} }
function the_author_login() { function the_author_login() {
echo get_the_author_login(); echo get_the_author_login();
} }
function get_the_author_firstname() { function get_the_author_firstname() {
global $id,$authordata; return $authordata->first_name; global $authordata;
return $authordata->first_name;
} }
function the_author_firstname() { function the_author_firstname() {
echo get_the_author_firstname(); echo get_the_author_firstname();
} }
function get_the_author_lastname() { function get_the_author_lastname() {
global $id,$authordata; return $authordata->last_name; global $authordata;
return $authordata->last_name;
} }
function the_author_lastname() { function the_author_lastname() {
echo get_the_author_lastname(); echo get_the_author_lastname();
} }
function get_the_author_nickname() { function get_the_author_nickname() {
global $id,$authordata; return $authordata->nickname; global $authordata;
return $authordata->nickname;
} }
function the_author_nickname() { function the_author_nickname() {
echo get_the_author_nickname(); echo get_the_author_nickname();
} }
function get_the_author_ID() { function get_the_author_ID() {
global $id,$authordata; return $authordata->ID; global $authordata;
return $authordata->ID;
} }
function the_author_ID() { function the_author_ID() {
echo get_the_author_id(); echo get_the_author_id();
} }
function get_the_author_email() { function get_the_author_email() {
@ -63,149 +72,163 @@ function the_author_email() {
} }
function get_the_author_url() { function get_the_author_url() {
global $id,$authordata; return $authordata->user_url; global $authordata;
return $authordata->user_url;
} }
function the_author_url() { function the_author_url() {
echo get_the_author_url(); echo get_the_author_url();
} }
function get_the_author_icq() { function get_the_author_icq() {
global $id,$authordata; return $authordata->icq; global $authordata;
return $authordata->icq;
} }
function the_author_icq() { function the_author_icq() {
echo get_the_author_icq(); echo get_the_author_icq();
} }
function get_the_author_aim() { function get_the_author_aim() {
global $id,$authordata; return str_replace(' ', '+', $authordata->aim); global $authordata;
return str_replace(' ', '+', $authordata->aim);
} }
function the_author_aim() { function the_author_aim() {
echo get_the_author_aim(); echo get_the_author_aim();
} }
function get_the_author_yim() { function get_the_author_yim() {
global $id,$authordata; return $authordata->yim; global $authordata;
return $authordata->yim;
} }
function the_author_yim() { function the_author_yim() {
echo get_the_author_yim(); echo get_the_author_yim();
} }
function get_the_author_msn() { function get_the_author_msn() {
global $id,$authordata; return $authordata->msn; global $authordata;
return $authordata->msn;
} }
function the_author_msn() { function the_author_msn() {
echo get_the_author_msn(); echo get_the_author_msn();
} }
function get_the_author_posts() { function get_the_author_posts() {
global $id,$post; global $post;
$posts=get_usernumposts($post->post_author); $posts = get_usernumposts($post->post_author);
return $posts; return $posts;
} }
function the_author_posts() { function the_author_posts() {
echo get_the_author_posts(); echo get_the_author_posts();
} }
/* the_author_posts_link() requires no get_, use get_author_link() */ /* the_author_posts_link() requires no get_, use get_author_link() */
function the_author_posts_link($idmode='') { function the_author_posts_link($idmode='') {
global $id, $authordata; global $authordata;
echo '<a href="' . get_author_link(0, $authordata->ID, $authordata->user_nicename) . '" title="' . sprintf(__("Posts by %s"), wp_specialchars(the_author($idmode, false))) . '">' . the_author($idmode, false) . '</a>'; echo '<a href="' . get_author_link(0, $authordata->ID, $authordata->user_nicename) . '" title="' . sprintf(__("Posts by %s"), wp_specialchars(the_author($idmode, false))) . '">' . the_author($idmode, false) . '</a>';
} }
function get_author_link($echo = false, $author_id, $author_nicename) { function get_author_link($echo = false, $author_id, $author_nicename) {
global $wpdb, $wp_rewrite, $post, $cache_userdata; global $wpdb, $wp_rewrite, $post, $cache_userdata;
$auth_ID = $author_id; $auth_ID = $author_id;
$link = $wp_rewrite->get_author_permastruct(); $link = $wp_rewrite->get_author_permastruct();
if (empty($link)) {
$file = get_settings('home') . '/';
$link = $file . '?author=' . $auth_ID;
} else {
if ('' == $author_nicename) $author_nicename = $cache_userdata[$author_id]->user_nicename;
$link = str_replace('%author%', $author_nicename, $link);
$link = get_settings('home') . trailingslashit($link);
}
$link = apply_filters('author_link', $link, $author_id, $author_nicename); if ( empty($link) ) {
if ($echo) echo $link; $file = get_settings('home') . '/';
return $link; $link = $file . '?author=' . $auth_ID;
} else {
if ( '' == $author_nicename )
$author_nicename = $cache_userdata[$author_id]->user_nicename;
$link = str_replace('%author%', $author_nicename, $link);
$link = get_settings('home') . trailingslashit($link);
}
$link = apply_filters('author_link', $link, $author_id, $author_nicename);
if ( $echo )
echo $link;
return $link;
} }
function wp_list_authors($args = '') { function wp_list_authors($args = '') {
parse_str($args, $r); parse_str($args, $r);
if (!isset($r['optioncount'])) $r['optioncount'] = false;
if (!isset($r['exclude_admin'])) $r['exclude_admin'] = true; if ( !isset($r['optioncount']) )
if (!isset($r['show_fullname'])) $r['show_fullname'] = false; $r['optioncount'] = false;
if (!isset($r['hide_empty'])) $r['hide_empty'] = true; if ( !isset($r['exclude_admin']) )
if (!isset($r['feed'])) $r['feed'] = ''; $r['exclude_admin'] = true;
if (!isset($r['feed_image'])) $r['feed_image'] = ''; if ( !isset($r['show_fullname']) )
$r['show_fullname'] = false;
if ( !isset($r['hide_empty']) )
$r['hide_empty'] = true;
if ( !isset($r['feed']) )
$r['feed'] = '';
if ( !isset($r['feed_image']) )
$r['feed_image'] = '';
list_authors($r['optioncount'], $r['exclude_admin'], $r['show_fullname'], $r['hide_empty'], $r['feed'], $r['feed_image']); list_authors($r['optioncount'], $r['exclude_admin'], $r['show_fullname'], $r['hide_empty'], $r['feed'], $r['feed_image']);
} }
function list_authors($optioncount = false, $exclude_admin = true, $show_fullname = false, $hide_empty = true, $feed = '', $feed_image = '') { function list_authors($optioncount = false, $exclude_admin = true, $show_fullname = false, $hide_empty = true, $feed = '', $feed_image = '') {
global $wpdb; global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name";
$authors = $wpdb->get_results($query);
$query = "SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name"; foreach ( $authors as $author ) {
$authors = $wpdb->get_results($query);
foreach($authors as $author) {
$author = get_userdata( $author->ID ); $author = get_userdata( $author->ID );
$posts = get_usernumposts($author->ID); $posts = get_usernumposts($author->ID);
$name = $author->nickname; $name = $author->nickname;
if ($show_fullname && ($author->first_name != '' && $author->last_name != '')) { if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
$name = "$author->first_name $author->last_name"; $name = "$author->first_name $author->last_name";
}
if ( !($posts == 0 && $hide_empty) )
if (! ($posts == 0 && $hide_empty)) echo "<li>"; echo "<li>";
if ($posts == 0) { if ( $posts == 0 ) {
if ( !$hide_empty ) if ( !$hide_empty )
$link = $name; $link = $name;
} else { } else {
$link = '<a href="' . get_author_link(0, $author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), wp_specialchars($author->display_name)) . '">' . $name . '</a>'; $link = '<a href="' . get_author_link(0, $author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), wp_specialchars($author->display_name)) . '">' . $name . '</a>';
if ( (! empty($feed_image)) || (! empty($feed)) ) { if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' ';
$link .= ' '; if (empty($feed_image))
$link .= '(';
$link .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"';
if (empty($feed_image)) { if ( !empty($feed) ) {
$link .= '('; $title = ' title="' . $feed . '"';
} $alt = ' alt="' . $feed . '"';
$name = $feed;
$link .= $title;
}
$link .= '<a href="' . get_author_rss_link(0, $author->ID, $author->user_nicename) . '"'; $link .= '>';
if (! empty($feed)) { if ( !empty($feed_image) )
$title = ' title="' . $feed . '"'; $link .= "<img src=\"$feed_image\" border=\"0\"$alt$title" . ' />';
$alt = ' alt="' . $feed . '"'; else
$name = $feed; $link .= $name;
$link .= $title;
}
$link .= '>'; $link .= '</a>';
if (! empty($feed_image)) { if ( empty($feed_image) )
$link .= "<img src=\"$feed_image\" border=\"0\"$alt$title" . ' />'; $link .= ')';
} else { }
$link .= $name;
}
$link .= '</a>';
if (empty($feed_image)) { if ( $optioncount )
$link .= ')'; $link .= ' ('. $posts . ')';
}
}
if ($optioncount) { }
$link .= ' ('. $posts . ')';
}
}
if (! ($posts == 0 && $hide_empty)) echo "$link</li>"; if ( !($posts == 0 && $hide_empty) )
} echo "$link</li>";
}
} }
?> ?>

View File

@ -1,17 +1,17 @@
<?php <?php
function get_the_category($id = false) { function get_the_category($id = false) {
global $post, $category_cache; global $post, $category_cache;
if ( !$id ) if ( !$id )
$id = $post->ID; $id = $post->ID;
if ( ! isset($category_cache[$id]) ) if ( !isset($category_cache[$id]) )
update_post_category_cache($id); update_post_category_cache($id);
$categories = $category_cache[$id]; $categories = $category_cache[$id];
if (!empty($categories)) if ( !empty($categories) )
sort($categories); sort($categories);
else else
$categories = array(); $categories = array();
@ -30,7 +30,7 @@ function get_category_link($category_id) {
$category = &get_category($category_id); $category = &get_category($category_id);
$category_nicename = $category->category_nicename; $category_nicename = $category->category_nicename;
if ($parent = $category->category_parent) if ( $parent = $category->category_parent )
$category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/'; $category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename . '/';
$catlink = str_replace('%category%', $category_nicename, $catlink); $catlink = str_replace('%category%', $category_nicename, $catlink);
@ -40,60 +40,60 @@ function get_category_link($category_id) {
} }
function get_the_category_list($separator = '', $parents='') { function get_the_category_list($separator = '', $parents='') {
$categories = get_the_category(); $categories = get_the_category();
if (empty($categories)) { if (empty($categories))
return apply_filters('the_category', __('Uncategorized'), $separator, $parents); return apply_filters('the_category', __('Uncategorized'), $separator, $parents);
}
$thelist = ''; $thelist = '';
if ('' == $separator) { if ( '' == $separator ) {
$thelist .= '<ul class="post-categories">'; $thelist .= '<ul class="post-categories">';
foreach ($categories as $category) { foreach ( $categories as $category ) {
$category->cat_name = $category->cat_name; $category->cat_name = $category->cat_name;
$thelist .= "\n\t<li>"; $thelist .= "\n\t<li>";
switch(strtolower($parents)) { switch ( strtolower($parents) ) {
case 'multiple': case 'multiple':
if ($category->category_parent) { if ($category->category_parent)
$thelist .= get_category_parents($category->category_parent, TRUE); $thelist .= get_category_parents($category->category_parent, TRUE);
} $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>';
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>'; break;
break; case 'single':
case 'single': $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">';
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . ' rel="category tag">'; if ($category->category_parent)
if ($category->category_parent) { $thelist .= get_category_parents($category->category_parent, FALSE);
$thelist .= get_category_parents($category->category_parent, FALSE); $thelist .= $category->cat_name.'</a></li>';
} break;
$thelist .= $category->cat_name.'</a></li>'; case '':
break; default:
case '': $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>';
default: }
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a></li>'; }
} $thelist .= '</ul>';
} } else {
$thelist .= '</ul>'; $i = 0;
} else { foreach ( $categories as $category ) {
$i = 0; $category->cat_name = $category->cat_name;
foreach ($categories as $category) { if ( 0 < $i )
$category->cat_name = $category->cat_name; $thelist .= $separator . ' ';
if (0 < $i) $thelist .= $separator . ' '; switch ( strtolower($parents) ) {
switch(strtolower($parents)) { case 'multiple':
case 'multiple': if ( $category->category_parent )
if ($category->category_parent) $thelist .= get_category_parents($category->category_parent, TRUE); $thelist .= get_category_parents($category->category_parent, TRUE);
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>'; $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>';
break; break;
case 'single': case 'single':
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'; $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">';
if ($category->category_parent) $thelist .= get_category_parents($category->category_parent, FALSE); if ( $category->category_parent )
$thelist .= "$category->cat_name</a>"; $thelist .= get_category_parents($category->category_parent, FALSE);
break; $thelist .= "$category->cat_name</a>";
case '': break;
default: case '':
$thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>'; default:
} $thelist .= '<a href="' . get_category_link($category->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $category->cat_name) . '" rel="category tag">'.$category->cat_name.'</a>';
++$i; }
} ++$i;
} }
return apply_filters('the_category', $thelist, $separator, $parents); }
return apply_filters('the_category', $thelist, $separator, $parents);
} }
function the_category($separator = '', $parents='') { function the_category($separator = '', $parents='') {
@ -107,32 +107,34 @@ function get_the_category_by_ID($cat_ID) {
} }
function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
$chain = ''; $chain = '';
$parent = &get_category($id); $parent = &get_category($id);
if ($nicename) {
$name = $parent->category_nicename; if ( $nicename )
} else { $name = $parent->category_nicename;
$name = $parent->cat_name; else
} $name = $parent->cat_name;
if ($parent->category_parent) $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
if ($link) { if ( $parent->category_parent )
$chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; $chain .= get_category_parents($parent->category_parent, $link, $separator, $nicename);
} else {
$chain .= $name.$separator; if ( $link )
} $chain .= '<a href="' . get_category_link($parent->cat_ID) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;
return $chain; else
$chain .= $name.$separator;
return $chain;
} }
function get_category_children($id, $before = '/', $after = '') { function get_category_children($id, $before = '/', $after = '') {
global $cache_categories; global $cache_categories;
if ( ! isset($cache_categories)) if ( !isset($cache_categories) )
update_category_cache(); update_category_cache();
$c_cache = $cache_categories; // Can't do recursive foreach on a global, have to make a copy $c_cache = $cache_categories; // Can't do recursive foreach on a global, have to make a copy
$chain = ''; $chain = '';
foreach ($c_cache as $category){ foreach ( $c_cache as $category ) {
if ($category->category_parent == $id){ if ( $category->category_parent == $id ) {
$chain .= $before.$category->cat_ID.$after; $chain .= $before.$category->cat_ID.$after;
$chain .= get_category_children($category->cat_ID, $before, $after); $chain .= get_category_children($category->cat_ID, $before, $after);
} }
@ -142,105 +144,131 @@ function get_category_children($id, $before = '/', $after = '') {
// Deprecated. // Deprecated.
function the_category_ID($echo = true) { function the_category_ID($echo = true) {
// Grab the first cat in the list. // Grab the first cat in the list.
$categories = get_the_category(); $categories = get_the_category();
$cat = $categories[0]->cat_ID; $cat = $categories[0]->cat_ID;
if ($echo) echo $cat;
return $cat; if ( $echo )
echo $cat;
return $cat;
} }
// Deprecated. // Deprecated.
function the_category_head($before='', $after='') { function the_category_head($before='', $after='') {
global $currentcat, $previouscat; global $currentcat, $previouscat;
// Grab the first cat in the list. // Grab the first cat in the list.
$categories = get_the_category(); $categories = get_the_category();
$currentcat = $categories[0]->category_id; $currentcat = $categories[0]->category_id;
if ($currentcat != $previouscat) { if ( $currentcat != $previouscat ) {
echo $before; echo $before;
echo get_the_category_by_ID($currentcat); echo get_the_category_by_ID($currentcat);
echo $after; echo $after;
$previouscat = $currentcat; $previouscat = $currentcat;
} }
} }
function category_description($category = 0) { function category_description($category = 0) {
global $cat; global $cat;
if (!$category) $category = $cat; if ( !$category )
$category = & get_category($category); $category = $cat;
return apply_filters('category_description', $category->category_description, $category->cat_ID); $category = & get_category($category);
return apply_filters('category_description', $category->category_description, $category->cat_ID);
} }
// out of the WordPress loop // out of the WordPress loop
function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc', function dropdown_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_order = 'asc',
$optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE, $optiondates = 0, $optioncount = 0, $hide_empty = 1, $optionnone=FALSE,
$selected=0, $hide=0) { $selected=0, $hide=0) {
global $wpdb; global $wpdb;
if (($file == 'blah') || ($file == '')) $file = get_settings('home') . '/'; if ( ($file == 'blah') || ($file == '') )
if (!$selected) $selected=$cat; $file = get_settings('home') . '/';
$sort_column = 'cat_'.$sort_column; if ( !$selected )
$selected=$cat;
$sort_column = 'cat_'.$sort_column;
$query = " $query = "
SELECT cat_ID, cat_name, category_nicename,category_parent, SELECT cat_ID, cat_name, category_nicename,category_parent,
COUNT($wpdb->post2cat.post_id) AS cat_count, COUNT($wpdb->post2cat.post_id) AS cat_count,
DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth DAYOFMONTH(MAX(post_date)) AS lastday, MONTH(MAX(post_date)) AS lastmonth
FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id) FROM $wpdb->categories LEFT JOIN $wpdb->post2cat ON (cat_ID = category_id)
LEFT JOIN $wpdb->posts ON (ID = post_id) LEFT JOIN $wpdb->posts ON (ID = post_id)
WHERE cat_ID > 0 WHERE cat_ID > 0
"; ";
if ($hide) { if ( $hide ) {
$query .= " AND cat_ID != $hide"; $query .= " AND cat_ID != $hide";
$query .= get_category_children($hide, " AND cat_ID != "); $query .= get_category_children($hide, " AND cat_ID != ");
} }
$query .=" GROUP BY cat_ID"; $query .=" GROUP BY cat_ID";
if (intval($hide_empty) == 1) $query .= " HAVING cat_count > 0"; if ( intval($hide_empty) == 1 )
$query .= " ORDER BY $sort_column $sort_order, post_date DESC"; $query .= " HAVING cat_count > 0";
$query .= " ORDER BY $sort_column $sort_order, post_date DESC";
$categories = $wpdb->get_results($query); $categories = $wpdb->get_results($query);
echo "<select name='cat' class='postform'>\n"; echo "<select name='cat' class='postform'>\n";
if (intval($optionall) == 1) { if ( intval($optionall) == 1 ) {
$all = apply_filters('list_cats', $all); $all = apply_filters('list_cats', $all);
echo "\t<option value='0'>$all</option>\n"; echo "\t<option value='0'>$all</option>\n";
} }
if (intval($optionnone) == 1) echo "\t<option value='-1'>".__('None')."</option>\n"; if ( intval($optionnone) == 1 )
if ($categories) { echo "\t<option value='-1'>".__('None')."</option>\n";
foreach ($categories as $category) { if ( $categories ) {
$cat_name = apply_filters('list_cats', $category->cat_name, $category); foreach ( $categories as $category ) {
echo "\t<option value=\"".$category->cat_ID."\""; $cat_name = apply_filters('list_cats', $category->cat_name, $category);
if ($category->cat_ID == $selected) echo "\t<option value=\"".$category->cat_ID."\"";
echo ' selected="selected"'; if ( $category->cat_ID == $selected )
echo '>'; echo ' selected="selected"';
echo $cat_name; echo '>';
if (intval($optioncount) == 1) echo '&nbsp;&nbsp;('.$category->cat_count.')'; echo $cat_name;
if (intval($optiondates) == 1) echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth; if ( intval($optioncount) == 1 )
echo "</option>\n"; echo '&nbsp;&nbsp;('.$category->cat_count.')';
} if ( intval($optiondates) == 1 )
} echo '&nbsp;&nbsp;'.$category->lastday.'/'.$category->lastmonth;
echo "</select>\n"; echo "</option>\n";
}
}
echo "</select>\n";
} }
// out of the WordPress loop // out of the WordPress loop
function wp_list_cats($args = '') { function wp_list_cats($args = '') {
parse_str($args, $r); parse_str($args, $r);
if (!isset($r['optionall'])) $r['optionall'] = 0; if ( !isset($r['optionall']))
if (!isset($r['all'])) $r['all'] = 'All'; $r['optionall'] = 0;
if (!isset($r['sort_column'])) $r['sort_column'] = 'ID'; if ( !isset($r['all']))
if (!isset($r['sort_order'])) $r['sort_order'] = 'asc'; $r['all'] = 'All';
if (!isset($r['file'])) $r['file'] = ''; if ( !isset($r['sort_column']) )
if (!isset($r['list'])) $r['list'] = true; $r['sort_column'] = 'ID';
if (!isset($r['optiondates'])) $r['optiondates'] = 0; if ( !isset($r['sort_order']) )
if (!isset($r['optioncount'])) $r['optioncount'] = 0; $r['sort_order'] = 'asc';
if (!isset($r['hide_empty'])) $r['hide_empty'] = 1; if ( !isset($r['file']) )
if (!isset($r['use_desc_for_title'])) $r['use_desc_for_title'] = 1; $r['file'] = '';
if (!isset($r['children'])) $r['children'] = true; if ( !isset($r['list']) )
if (!isset($r['child_of'])) $r['child_of'] = 0; $r['list'] = true;
if (!isset($r['categories'])) $r['categories'] = 0; if ( !isset($r['optiondates']) )
if (!isset($r['recurse'])) $r['recurse'] = 0; $r['optiondates'] = 0;
if (!isset($r['feed'])) $r['feed'] = ''; if ( !isset($r['optioncount']) )
if (!isset($r['feed_image'])) $r['feed_image'] = ''; $r['optioncount'] = 0;
if (!isset($r['exclude'])) $r['exclude'] = ''; if ( !isset($r['hide_empty']) )
if (!isset($r['hierarchical'])) $r['hierarchical'] = true; $r['hide_empty'] = 1;
if ( !isset($r['use_desc_for_title']) )
$r['use_desc_for_title'] = 1;
if ( !isset($r['children']) )
$r['children'] = true;
if ( !isset($r['child_of']) )
$r['child_of'] = 0;
if ( !isset($r['categories']) )
$r['categories'] = 0;
if ( !isset($r['recurse']) )
$r['recurse'] = 0;
if ( !isset($r['feed']) )
$r['feed'] = '';
if ( !isset($r['feed_image']) )
$r['feed_image'] = '';
if ( !isset($r['exclude']) )
$r['exclude'] = '';
if ( !isset($r['hierarchical']) )
$r['hierarchical'] = true;
return list_cats($r['optionall'], $r['all'], $r['sort_column'], $r['sort_order'], $r['file'], $r['list'], $r['optiondates'], $r['optioncount'], $r['hide_empty'], $r['use_desc_for_title'], $r['children'], $r['child_of'], $r['categories'], $r['recurse'], $r['feed'], $r['feed_image'], $r['exclude'], $r['hierarchical']); return list_cats($r['optionall'], $r['all'], $r['sort_column'], $r['sort_order'], $r['file'], $r['list'], $r['optiondates'], $r['optioncount'], $r['hide_empty'], $r['use_desc_for_title'], $r['children'], $r['child_of'], $r['categories'], $r['recurse'], $r['feed'], $r['feed_image'], $r['exclude'], $r['hierarchical']);
} }
@ -248,15 +276,14 @@ function wp_list_cats($args = '') {
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 = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) { 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 = 1, $children=FALSE, $child_of=0, $categories=0, $recurse=0, $feed = '', $feed_image = '', $exclude = '', $hierarchical=FALSE) {
global $wpdb, $category_posts; global $wpdb, $category_posts;
// Optiondates now works // Optiondates now works
if ('' == $file) { if ( '' == $file )
$file = get_settings('home') . '/'; $file = get_settings('home') . '/';
}
$exclusions = ''; $exclusions = '';
if (!empty($exclude)) { if ( !empty($exclude) ) {
$excats = preg_split('/[\s,]+/',$exclude); $excats = preg_split('/[\s,]+/',$exclude);
if (count($excats)) { if ( count($excats) ) {
foreach ($excats as $excat) { foreach ( $excats as $excat ) {
$exclusions .= ' AND cat_ID <> ' . intval($excat) . ' '; $exclusions .= ' AND cat_ID <> ' . intval($excat) . ' ';
} }
} }
@ -264,10 +291,10 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
$exclusions = apply_filters('list_cats_exclusions', $exclusions ); $exclusions = apply_filters('list_cats_exclusions', $exclusions );
if (intval($categories)==0){ if ( intval($categories) == 0 ) {
$sort_column = 'cat_'.$sort_column; $sort_column = 'cat_'.$sort_column;
$query = " $query = "
SELECT cat_ID, cat_name, category_nicename, category_description, category_parent SELECT cat_ID, cat_name, category_nicename, category_description, category_parent
FROM $wpdb->categories FROM $wpdb->categories
WHERE cat_ID > 0 $exclusions WHERE cat_ID > 0 $exclusions
@ -275,63 +302,60 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
$categories = $wpdb->get_results($query); $categories = $wpdb->get_results($query);
} }
if (!count($category_posts)) { if ( !count($category_posts) ) {
$now = current_time('mysql', 1); $now = current_time('mysql', 1);
$cat_counts = $wpdb->get_results(" SELECT cat_ID, $cat_counts = $wpdb->get_results(" SELECT cat_ID,
COUNT($wpdb->post2cat.post_id) AS cat_count COUNT($wpdb->post2cat.post_id) AS cat_count
FROM $wpdb->categories FROM $wpdb->categories
INNER JOIN $wpdb->post2cat ON (cat_ID = category_id) INNER JOIN $wpdb->post2cat ON (cat_ID = category_id)
INNER JOIN $wpdb->posts ON (ID = post_id) INNER JOIN $wpdb->posts ON (ID = post_id)
WHERE post_status = 'publish' WHERE post_status = 'publish'
AND post_date_gmt < '$now' $exclusions AND post_date_gmt < '$now' $exclusions
GROUP BY category_id"); GROUP BY category_id");
if (! empty($cat_counts)) { if ( !empty($cat_counts) ) {
foreach ($cat_counts as $cat_count) { foreach ( $cat_counts as $cat_count ) {
if (1 != intval($hide_empty) || $cat_count > 0) { if ( 1 != intval($hide_empty) || $cat_count > 0 )
$category_posts["$cat_count->cat_ID"] = $cat_count->cat_count; $category_posts["$cat_count->cat_ID"] = $cat_count->cat_count;
} }
} }
}
} }
if ( $optiondates ) { if ( $optiondates ) {
$cat_dates = $wpdb->get_results(" SELECT category_id, $cat_dates = $wpdb->get_results(" SELECT category_id,
UNIX_TIMESTAMP( MAX(post_date) ) AS ts UNIX_TIMESTAMP( MAX(post_date) ) AS ts
FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories FROM $wpdb->posts, $wpdb->post2cat, $wpdb->categories
WHERE post_status = 'publish' AND post_id = ID $exclusions WHERE post_status = 'publish' AND post_id = ID $exclusions
GROUP BY category_id"); GROUP BY category_id");
foreach ($cat_dates as $cat_date) { foreach ( $cat_dates as $cat_date ) {
$category_timestamp["$cat_date->category_id"] = $cat_date->ts; $category_timestamp["$cat_date->category_id"] = $cat_date->ts;
} }
} }
$num_found=0; $num_found=0;
$thelist = ""; $thelist = "";
foreach ($categories as $category) { foreach ( $categories as $category ) {
if ((intval($hide_empty) == 0 || isset($category_posts["$category->cat_ID"])) && (!$hierarchical || $category->category_parent == $child_of) ) { if ( (intval($hide_empty) == 0 || isset($category_posts["$category->cat_ID"])) && (!$hierarchical || $category->category_parent == $child_of) ) {
$num_found++; $num_found++;
$link = '<a href="'.get_category_link($category->cat_ID).'" '; $link = '<a href="'.get_category_link($category->cat_ID).'" ';
if ($use_desc_for_title == 0 || empty($category->category_description)) { if ( $use_desc_for_title == 0 || empty($category->category_description) )
$link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"'; $link .= 'title="'. sprintf(__("View all posts filed under %s"), wp_specialchars($category->cat_name)) . '"';
} else { else
$link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"'; $link .= 'title="' . wp_specialchars(apply_filters('category_description',$category->category_description,$category)) . '"';
}
$link .= '>'; $link .= '>';
$link .= apply_filters('list_cats', $category->cat_name, $category).'</a>'; $link .= apply_filters('list_cats', $category->cat_name, $category).'</a>';
if ( (! empty($feed_image)) || (! empty($feed)) ) { if ( (! empty($feed_image)) || (! empty($feed)) ) {
$link .= ' '; $link .= ' ';
if (empty($feed_image)) { if ( empty($feed_image) )
$link .= '('; $link .= '(';
}
$link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"'; $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '"';
if ( !empty($feed) ) { if ( !empty($feed) ) {
$title = ' title="' . $feed . '"'; $title = ' title="' . $feed . '"';
$alt = ' alt="' . $feed . '"'; $alt = ' alt="' . $feed . '"';
$name = $feed; $name = $feed;
$link .= $title; $link .= $title;
@ -339,66 +363,67 @@ function list_cats($optionall = 1, $all = 'All', $sort_column = 'ID', $sort_orde
$link .= '>'; $link .= '>';
if (! empty($feed_image)) { if ( !empty($feed_image) )
$link .= "<img src='$feed_image' $alt$title" . ' />'; $link .= "<img src='$feed_image' $alt$title" . ' />';
} else { else
$link .= $name; $link .= $name;
}
$link .= '</a>'; $link .= '</a>';
if (empty($feed_image)) { if (empty($feed_image))
$link .= ')'; $link .= ')';
}
} }
if (intval($optioncount) == 1) { if ( intval($optioncount) == 1 )
$link .= ' ('.intval($category_posts["$category->cat_ID"]).')'; $link .= ' ('.intval($category_posts["$category->cat_ID"]).')';
}
if ( $optiondates ) { if ( $optiondates ) {
if ( $optiondates == 1 ) $optiondates = 'Y-m-d'; if ( $optiondates == 1 )
$optiondates = 'Y-m-d';
$link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]); $link .= ' ' . gmdate($optiondates, $category_timestamp["$category->cat_ID"]);
} }
if ($list) {
if ( $list )
$thelist .= "\t<li>$link\n"; $thelist .= "\t<li>$link\n";
} else { else
$thelist .= "\t$link<br />\n"; $thelist .= "\t$link<br />\n";
}
if ($hierarchical && $children) $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical); if ($hierarchical && $children)
if ($list) $thelist .= "</li>\n"; $thelist .= list_cats($optionall, $all, $sort_column, $sort_order, $file, $list, $optiondates, $optioncount, $hide_empty, $use_desc_for_title, $hierarchical, $category->cat_ID, $categories, 1, $feed, $feed_image, $exclude, $hierarchical);
} if ($list)
$thelist .= "</li>\n";
}
} }
if (!$num_found && !$child_of){ if ( !$num_found && !$child_of ) {
if ($list) { if ( $list ) {
$before = '<li>'; $before = '<li>';
$after = '</li>'; $after = '</li>';
} }
echo $before . __("No categories") . $after . "\n"; echo $before . __("No categories") . $after . "\n";
return; return;
} }
if ($list && $child_of && $num_found && $recurse) { if ( $list && $child_of && $num_found && $recurse ) {
$pre = "\t\t<ul class='children'>"; $pre = "\t\t<ul class='children'>";
$post = "\t\t</ul>\n"; $post = "\t\t</ul>\n";
} else { } else {
$pre = $post = ''; $pre = $post = '';
} }
$thelist = $pre . $thelist . $post; $thelist = $pre . $thelist . $post;
if ($recurse) { if ( $recurse )
return $thelist; return $thelist;
}
echo apply_filters('list_cats', $thelist); echo apply_filters('list_cats', $thelist);
} }
function in_category($category) { // Check if the current post is in the given category function in_category($category) { // Check if the current post is in the given category
global $post, $category_cache; global $post, $category_cache;
$cats = ''; $cats = '';
foreach ($category_cache[$post->ID] as $cat) : foreach ( $category_cache[$post->ID] as $cat ) {
$cats[] = $cat->cat_ID; $cats[] = $cat->cat_ID;
endforeach; }
if ( in_array($category, $cats) ) if ( in_array($category, $cats) )
return true; return true;
else else
return false; return false;
} }
?> ?>