<?php
/*
Plugin Name: Subtraction Style Archives
Version: 0.8
Plugin URI: http://www.lattimore.id.au/projects/wordpress/plugins/subtraction-style-archives/
Author: Alistair Lattimore
Author URI: http://www.lattimore.id.au
Description: Displays an archive of posts &amp; categories in the same style as http://www.subtraction.com/archives/.
*/

function arl_subtraction_archives_posts($expanded_months 3$post_separator "+")
{
    global 
$wpdb;
    
$output "";
    
$year "";
    
$month "";

    
$sql "SELECT DATE_FORMAT(post_date, '%Y') AS Year, ";
    
$sql .= "DATE_FORMAT(post_date, '%m') AS Month, ";
    
$sql .= "COUNT(id) AS PostCount ";
    
$sql .= "FROM $wpdb->posts ";
    
$sql .= "WHERE DATE_FORMAT(post_date, '%Y') <> '0000'";
    
$sql .= " AND post_status = 'publish'";
    
$sql .= " AND post_type = 'post' ";
    
$sql .= "GROUP BY DATE_FORMAT(post_date, '%M %Y'), DATE_FORMAT(post_date, '%Y') ";
    
$sql .= "ORDER BY DATE_FORMAT(post_date, '%Y') DESC, DATE_FORMAT(post_date, '%m') DESC;";

    
$months $wpdb->get_results($sql);

    
$sql "SELECT wp.id, ";
    
$sql .= "wp.post_title, ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%m') AS Month, ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%Y') AS Year, ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%M %Y') AS MonthYear, ";
    
$sql .= "wp.post_date ";
    
$sql .= "FROM $wpdb->posts wp ";
    
$sql .= "WHERE wp.post_status = 'publish' ";
    
$sql .= "AND wp.post_type = 'post' ";
    
$sql .= "AND DATE_FORMAT(wp.post_date, '%Y') <> '0000' ";
    
$sql .= "AND DATE_FORMAT(wp.post_date, '%Y-%m') > DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL $expanded_months MONTH), '%Y-%m') ";
    
$sql .= "UNION ";
    
$sql .= "SELECT '-1', ";
    
$sql .= "'MONTH', ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%m') AS Month, ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%Y') AS Year, ";
    
$sql .= "DATE_FORMAT(wp.post_date, '%M %Y') AS MonthYear, ";
    
$sql .= "now() ";
    
$sql .= "FROM $wpdb->posts wp ";
    
$sql .= "WHERE wp.post_status = 'publish' ";
    
$sql .= "AND wp.post_type = 'post' ";
    
$sql .= "AND DATE_FORMAT(post_date, '%Y') <> '0000' ";
    
$sql .= "AND DATE_FORMAT(wp.post_date, '%Y-%m') <= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL $expanded_months MONTH), '%Y-%m') ";
    
$sql .= "ORDER BY year DESC, month DESC, post_date DESC;";

    
$posts $wpdb->get_results($sql);

    if (!empty(
$posts))
    {
        foreach (
$posts as $post)
        {
            if ((
$year == "" && $month == "") || ($year != $post->Year) || ($month != $post->Month))
            {
                
$thisMonth array_shift($months);
            }

            if (
$year == "" && $month == ""
            {
                
$post_count_label $thisMonth->PostCount == ' post':' posts';

                
$output .= '<h3>';
                
$output .= '<a href="' get_month_link($post->Year$post->Month) . '">' $post->MonthYear '</a> ';
                
$output .= '<span>' $thisMonth->PostCount $post_count_label '</span>';
                
$output .= "</h3>\n";
            
                
$output .= "<p>";
                
$output .= '<a href="' get_permalink($post->id) . '">' $post->post_title '</a>';
            }
            else
            {
                
$post_count_label $thisMonth->PostCount == ' post':' posts';

                if (
$post->id >= 1)
                {
                    if (
$year == $post->Year && $month == $post->Month)
                    {
                        
$output .= '&nbsp;' $post_separator '&nbsp;<a href="' get_permalink($post->id) . '">' $post->post_title '</a>';
                    }
                    else
                    {
                        
$output .= "</p>\n";

                        
$output .= '<h3>';
                        
$output .= '<a href="' get_month_link($post->Year$post->Month) . '">' $post->MonthYear '</a> ';
                        
$output .= '<span>' $thisMonth->PostCount $post_count_label '</span>';
                        
$output .= "</h3>\n";
                    
                        
$output .= "<p>";
                        
$output .= '<a href="' get_permalink($post->id) . '">' $post->post_title '</a>';
                    }
                }
                else
                {
                    
$output .= '<h3>';
                    
$output .= '<a href="' get_month_link($post->Year$post->Month) . '">' $post->MonthYear '</a> ';
                    
$output .= '<span>' $thisMonth->PostCount $post_count_label '</span>';
                    
$output .= "</h3>\n";
                }
            }

            
$year $post->Year;
            
$month $post->Month;
        }
    }
    else
    {
        
$output "<p>None available</p>\n";
    }

    print 
$output;
}

function 
arl_subtraction_archives_categories()
{
    global 
$wpdb;
    
$output "";

    
$sql "SELECT cat_id, cat_name, category_description, category_count ";
    
$sql .= "FROM $wpdb->categories ";
    
$sql .= "WHERE category_count > 0 ";
    
$sql .= "ORDER BY cat_name";
    
    
$posts $wpdb->get_results($sql);

    if (!empty(
$posts))
    {
        foreach (
$posts as $post)
        {
            
$post_count_label $post->category_count == ' post':' posts';

            
$output .= '<h3>';
            
$output .= '<a href="' get_category_link($post->cat_id) . '">' $post->cat_name '</a> ';
            
$output .= '<span>' $post->category_count $post_count_label '</span>';
            
$output .= "</h3>\n";

            if (
strlen($post->category_description) > 0)
                
$output .= "<p>" $post->category_description "</p>\n";
        }
    }
    else
    {
        
$output "No categories defined";
    }

    print 
$output;
}
?>