... structure. Those first few calls are a waste of
time for what I'm doing, so for those I just return the content back. But
for the last call, I'll tear apart the HTML and return it with unique SPAN
tags.
*/
//debug $f = fopen("/home/erik/blog/pluglog.txt", 'a');
//debug fwrite($f, "Started with: $content\n");
/*
Content comes in looking like this:
Uncategorized (1)
So I'm going to pattern match it and break out the Count, URL, and Category Name.
Since I have to work with the whole block at once, I'm also going to stick in
a random delimiter (in this case, '][') so that I can break each section up
later.
*/
// Rearrange the important bits into a pattern that we can parse
// Use this pattern for Wordpress <= 2.0.2
//$new_content = preg_replace("/(]*>)([^<]*)(<\/a> \()([0-9]*)/", "$6 $2 $4 ][", $content);
// Use this patter for Wordpress >= svn revision 3669 (ie., newer.)
$new_content = preg_replace("/(]*>)([^<]*)(<\/a> \()([0-9]*)/", "$6 $2 $4 ][", $content);
// This block is what allows me to ignore the callbacks that only pass in
// a category title and not the whole list structure. (ie., if the original
// content didn't match the pattern above...)
if ($new_content == $content) {
//debug fwrite($f, "Doesn't match! Done.\n");
//debug fclose($f);
// Return the original content un-touched.
return $content;
}
// This is probably slow since I'm redefining the same string variable over
// and over... but oh well. This is just some extra sanity filtering to strip
// out an potentially left-over HTML tags, the newlines characters and any
// parenthesis that might have slipped through.
$new_content = strip_tags($new_content);
$new_content = preg_replace("/[\r|\n]/", '', $new_content);
$new_content = preg_replace("/[\(|\)]/", '', $new_content);
//debug fwrite($f, " Content is now: $content\n");
// Split the content into a list so we can work with each entry on it's own.
$entries = explode("][", trim($new_content));
// Initialize the 'result' variable.
$result = '';
// Now break apart each entry by whitespace
foreach ($entries as $item) {
$bits = explode(" ", trim($item));
// If there weren't enough pieces, skip this one (it might be the first
// or last entry that was actually blank -- and known to be so.)
if (count($bits) < 3) {
continue;
} // end if
// Now we can break out each individual value.
$count = (int) trim($bits[0]);
$url = trim($bits[1]);
$tag = trim($bits[2]);
// This ugly block is what actually assigns a 'style' to each category
// based on the number of posts in it. You'll want to tweak this to best
// match the number of entries you have.
if ($count > 100) {
$style = "cat_level_10";
} else if ($count > 80) {
$style = "cat_level_9";
} else if ($count > 60) {
$style = "cat_level_8";
} else if ($count > 45) {
$style = "cat_level_7";
} else if ($count > 30) {
$style = "cat_level_6";
} else if ($count > 25) {
$style = "cat_level_5";
} else if ($count > 15) {
$style = "cat_level_4";
} else if ($count > 10) {
$style = "cat_level_3";
} else if ($count > 5) {
$style = "cat_level_2";
} else {
$style = "cat_level_1";
} // end if
// Now put it back together using a SPAN tag instead of the old structure.
// NOTE: If you want to keep your categories in a list like they were
// originally, just edit this line by adding a '' to the beginning and
// a '' to the end.
$result = $result . "$tag ";
} // end foreach
//debug fclose($f);
// We're done!
return $result;
} // end function
// The following line registers the 'flickrfy_cats' function as an action plugin
// for the 'list_cats' built-in function.
add_filter('list_cats', 'flickrfy_cats');
?>