<?php
/*
Plugin Name: maurus.net - Sane archive widget
Plugin URI: http://maurus.net/resources/plug-ins/sane-archives
Description: The default archive widget's select box is just plain inaccessible. This provides an accessible alternative. This plug-in is licensed under the GPL v2, it has been derived from the archive widget included with Wordpress 2.3.3.
Version: 0.1
Author: Jonas Maurus
Author URI: http://maurus.net/
*/

/*
    This plug-in has been derived from the original archive widget
    in Wordpress version 2.3.3 which has been licensed under the
    GPL v2. Therefor my changes fall under the GPL v2 as well.
*/

function mnet_widget_archives($args) {
    
extract($args);
    
$options get_option('mnet_widget_archives');
    
$c $options['count'] ? '1' '0';
    
$d $options['dropdown'] ? '1' '0';
    
$title = empty($options['title']) ? __('Archives') : $options['title'];

    echo 
$before_widget;
    echo 
$before_title $title $after_title;

    if(
$d) {
?>
      <form action="" method="get"><p>
        <input type="hidden" name="mn_redir" value="1" />
        <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
        <input type="submit" value="Go" /></p>
      </form>
<?php
    
} else {
?>
        <ul>
        <?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
        </ul>
<?php
    
}

    echo 
$after_widget;
}

function 
mnet_widget_archives_control() {
    
$options $newoptions get_option('mnet_widget_archives');
    if ( 
$_POST["mnet_archives-submit"] ) {
        
$newoptions['count'] = isset($_POST['mnet_archives-count']);
        
$newoptions['dropdown'] = isset($_POST['mnet_archives-dropdown']);
        
$newoptions['title'] = strip_tags(stripslashes($_POST["mnet_archives-title"]));
    }
    if ( 
$options != $newoptions ) {
        
$options $newoptions;
        
update_option('mnet_widget_archives'$options);
    }
    
$count $options['count'] ? 'checked="checked"' '';
    
$dropdown $options['dropdown'] ? 'checked="checked"' '';
    
$title attribute_escape($options['title']);
?>
            <p><label for="mnet_archives-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="mnet_archives-title" name="mnet_archives-title" type="text" value="<?php echo $title?>" /></label></p>
            <p style="text-align:right;margin-right:40px;"><label for="mnet_archives-count"><?php _e('Show post counts'); ?> <input class="checkbox" type="checkbox" <?php echo $count?> id="mnet_archives-count" name="mnet_archives-count" /></label></p>
            <p style="text-align:right;margin-right:40px;"><label for="mnet_archives-dropdown"><?php _e('Display as a drop down'); ?> <input class="checkbox" type="checkbox" <?php echo $dropdown?> id="mnet_archives-dropdown" name="mnet_archives-dropdown" /></label></p>
            <input type="hidden" id="mnet_archives-submit" name="mnet_archives-submit" value="1" />
<?php
}

function 
widget_maurusnet_archives_register() {
  
register_sidebar_widget('Sane Archives''mnet_widget_archives'null'mnet_widget');
  
register_widget_control('Sane Archives''mnet_widget_archives_control'300100'mnet_widget');
}

add_action('init''widget_maurusnet_archives_register');

function 
maurusnet_redir() {
  if (isset(
$_GET['mn_redir']) && isset($_GET['archive-dropdown'])) {
    
$url $_GET['archive-dropdown'];
    if (
preg_match('#^' get_option('siteurl'). '#'$url)) {
      
wp_redirect($url);
    }
    exit;
  }
}

add_action('template_redirect''maurusnet_redir');
?>