<?php
/*
Plugin Name: Smart Archives
Version: 1.9
Plugin URI: http://justinblanton.com/projects/smartarchives/
Description: A simple, clean, and future-proof way to present your archives.
Author: Justin Blanton
Author URI: http://justinblanton.com
*/

function smartArchives($format='both'$catID='') {

    global 
$wpdb$PHP_SELF;
    
setlocale(LC_ALL,WPLANG); // set localization language
    
$now gmdate("Y-m-d H:i:s",(time()+((get_settings('gmt_offset'))*3600)));  // get the current GMT date
    
$bogusDate "/01/2001"// used for the strtotime() function below    
    
    
$yearsWithPosts $wpdb->get_results("
        SELECT distinct year(post_date) AS `year`, count(ID) as posts
        FROM $wpdb->posts 
        WHERE post_type = 'post' 
        AND post_status = 'publish' 
        GROUP BY year(post_date) 
        ORDER BY post_date DESC"
);
    
    foreach (
$yearsWithPosts as $currentYear) {
        
        for (
$currentMonth 1$currentMonth <= 12$currentMonth++) {
            
            
$monthsWithPosts[$currentYear->year][$currentMonth] = $wpdb->get_results("
            SELECT ID, post_title FROM $wpdb->posts 
            WHERE post_type = 'post'
            AND post_status = 'publish' 
            AND year(post_date) = '$currentYear->year' 
            AND month(post_date) = '$currentMonth'            
            ORDER BY id desc"
);
        }
    }
    
    if ((
$format == 'both') || ($format == 'block')) { // check to see if we are supposed to display the block
        
        // get the shortened month name; strftime() should localize
        
for($currentMonth 1$currentMonth <= 12$currentMonth++) $shortMonths[$currentMonth] = ucfirst(strftime("%b"strtotime("$currentMonth"."$bogusDate")));
        
        if (
$yearsWithPosts) {

            foreach (
$yearsWithPosts as $currentYear) {
                
                echo (
'<strong><a href="'.substr(get_year_link($currentYear->year$currentYear->year), 0, -1).'">'.$currentYear->year.'</a>:</strong> ');
                
                for (
$currentMonth 1$currentMonth <= 12$currentMonth++) {
                    
                    if (
$monthsWithPosts[$currentYear->year][$currentMonth]) echo ('<a href="'.substr(get_month_link($currentYear->year$currentMonth), 0, -1).'">'.$shortMonths[$currentMonth].'</a> ');
                    else echo 
'<span class="emptymonth">'.$shortMonths[$currentMonth].'</span> ';
                    
                }                
                echo 
'<br/>';
            }            
            echo 
'<br /><br />';        
        }    
    }
    
    if ((
$format == 'both') || ($format == 'list')) { //check to see if we are supposed to display the list
        
        // get the month name; strftime() should localize
        
for($currentMonth 1$currentMonth <= 12$currentMonth++) $monthNames[$currentMonth] = ucfirst(strftime("%B"strtotime("$currentMonth"."$bogusDate")));
        
        if (
$yearsWithPosts) {
            
            if (
$catID != '') { // at least one category was specified to be excluded
                
                
$catIDs explode(" "$catID); // put the category(ies) into an array
                
foreach($yearsWithPosts as $currentYear) {
                    
                    for (
$currentMonth 12$currentMonth >= 1$currentMonth--) {
                        
                        if (
$monthsWithPosts[$currentYear->year][$currentMonth]) {
                            echo (
'<h2><a href="'.substr(get_month_link($currentYear->year$currentMonth), 0, -1).'">'.$monthNames[$currentMonth].' '.$currentYear->year.'</a></h2>');
                            
                        
                            echo 
'<ul>';
                            
                            foreach (
$monthsWithPosts[$currentYear->year][$currentMonth] as $post) { 
                                
                                if (
$post->post_date <= $now) {
                                    
$cats $wpdb->get_col("SELECT category_id FROM $wpdb->post2cat WHERE post_id = $post->ID");
                                    
$found false;
                                    
                                    foreach (
$cats as $cat) if (in_array($cat$catIDs)) $found true;
                                    
                                    if (!
$found) echo ('<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>');
                                } 
                            }   
                            echo 
'</ul>';  
                        }
                    }
                }
            }
            
            else { 
// we don't need to exclude any categories
            
                
foreach($yearsWithPosts as $currentYear) {
                    
                    for (
$currentMonth 12$currentMonth >= 1$currentMonth--) {
                        
                        if (
$monthsWithPosts[$currentYear->year][$currentMonth]) {
                            echo 
get_archives_link(get_month_link($currentYear->year$currentMonth), $monthNames[$currentMonth] . ' ' $currentYear->year'''<h2>''</h2>');
                            echo 
'<ul>';
                            
                            foreach (
$monthsWithPosts[$currentYear->year][$currentMonth] as $post) echo ('<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>');
                            echo 
'</ul>';
                        }
                    }
                }
            }                               
        }
    }
?>