<?php
/*
Plugin Name: Extended Daily Post Fixer
Plugin URI: http://satunnainenbjorklund.net/arkisto/paivittaiset-delicious-linkit-siistimmin
Description: If you set up the del.icio.us "daily blog posting" feature, this plugin will let you set the post title, and add the rel="tag" microformat to the tag links and lets you customize the HTML in the tag list.
Author: Aki Björklund
Based on the work of Dougal Campbell in his del.icio.us daily blog post fixer plugin
    http://dougal.gunters.org/blog/2006/04/07/delicious-daily-blog-post-fixer-plugin

Reuses some admin interface code from the Ultimate Google Analytics plugin
    at http://www.oratransplant.nl/uga/

Version: 1.0
Author URI: http://satunnainenbjorklund.net/
License: GPL
*/

// If we see a title of the form "links for 2006-03-14", rewrite it:
function dafix_title_change($text '') {
    
$mytitle  dafix_get_option('title');

    
$text preg_replace('/links for (\d\d\d\d)-(0?([1-9]\d|\d))-(0?([1-9]\d|\d))/'$mytitle$text);

    return 
$text;
}

// Add 'rel="tag"' to any del.icio.us tag links
function dafix_del_link_rel_tag($text '') {
    
/* 
     * Add rel="tag" to del.icio.us links.
     * 
     * Once I realized that backslashes need to be escaped inside
     * of single-quoted strings, things got happy.
     */

    
$regex '%<a (href=(?:\\\\)?"http://del\.icio\.us/[^/"]+/[^/"]+(?:\\\\)?")>([^<]+)</a>%';

    
$text preg_replace($regex"<a $1 rel=\"tag\">$2</a>"$text);

    return 
$text;
}

// customize the markup for tag lists (probably to mark them up as lists)
function dafix_remarkup_tags($text '') {
    if (!
strrpos($text'<div class=\"delicious-tags\">')) return $text;
    
$before_taglist  dafix_get_option('before_taglist');
    
$between_taglist dafix_get_option('between_taglist');
    
$after_taglist   dafix_get_option('after_taglist');
    
    
$text str_replace('<div class=\"delicious-tags\">(tags: <a',
        
'<div class="delicious-tags">' $before_taglist '<a'$text);
    
$text str_replace('</a> <a ',
        
'</a>' $between_taglist '<a '$text);
    
$text str_replace('</a>)</div>',
        
'</a>' $after_taglist '</div>'$text);

    return 
$text;
}

// get an Daily Post FiXer option from the WordPress options database table
// if the option does not exist (yet), the default value is returned
function dafix_get_option($option_name) {
  
// get options from the database
  
$dafix_options get_option('dafix_options'); 
  
  if (!
$dafix_options || !array_key_exists($option_name$dafix_options)) {
    
// no options in database yet, or not this specific option 
    // create default options array
    
$dafix_default_options=array();
    
$dafix_default_options['before_taglist']             = '(tags: <ul><li>';
    
$dafix_default_options['between_taglist']            = '</li><li>';
    
$dafix_default_options['after_taglist']              = '</li></ul>)';
    
$dafix_default_options['title']                      = 'Links for $1-$2-$4';
    
// add default options to the database (if options already exist, 
    // add_option does nothing
    
add_option('dafix_options'$dafix_default_options
               
'Settings for Daily Post Fixer plugin');

    
// return default option if option is not in the array in the database
    // this can happen if a new option was added to the array in an upgrade
    // and the options haven't been changed/saved to the database yet
    
$result $dafix_default_options[$option_name];

  } else {
    
// option found in database
    
$result $dafix_options[$option_name];
  }
  
  return 
$result;
}


// function that is added as an Action to ADMIN_MENU
// it adds an option subpage to the options menu in WordPress administration
function dafix_admin() {
  if (
function_exists('add_options_page')) {
    
add_options_page('Extended Daily Post Fixer' /* page title */
                     
'del.icio.us fix' /* menu title */
                     
/* min. user level */
                     
basename(__FILE__/* php file */ 
                     
'dafix_options' /* function for subpanel */);
  }
}

// displays options subpage to set options for Ultimate GA and save any
// changes to these options back to the database
function dafix_options() {
  if (isset(
$_POST['info_update'])) {
    
?><div class="updated"><p><strong><?php 
    
// process submitted form
    
$dafix_options get_option('dafix_options');
    
$dafix_options['title']               = $_POST['title'];
    
$dafix_options['before_taglist']      = $_POST['before_taglist'];
    
$dafix_options['between_taglist']     = $_POST['between_taglist'];
    
$dafix_options['after_taglist']       = $_POST['after_taglist'];

    
update_option('dafix_options'$dafix_options);
    
    
_e('Options saved''dafix')
    
?></strong></p></div><?php
    

    
    
// show options form with current values
    
?>
<div class=wrap>
  <form method="post">
    <h2>Daily Post Fixer</h2>
    <fieldset class="options" name="general">
      <legend><?php _e('General settings''dafix'?></legend>
      <table width="100%" cellspacing="2" cellpadding="5" class="editform">
        <tr>
          <th nowrap valign="top" width="33%"><?php _e('Title pattern''dafix'?></th>
          <td><input name="title" type="text" id="title" value="<?php echo dafix_get_option('title'); ?>" size="50" />
            <br />Pattern for the daily post title. $1 for the year, $2 for month with leading zero, $3 without the zero, $4 for the day
            with leading zero, $5 without it.
          </td>
        </tr>
        
        <tr>
          <th nowrap valign="top" width="33%"><?php _e('Before taglist''dafix'?></th>
          <td><input name="before_taglist" type="text" id="before_taglist" value="<?php echo htmlspecialchars(dafix_get_option('before_taglist')); ?>" size="50" />
            <br />Custom markup before the taglist.
          </td>
        </tr>
        
        <tr>
          <th nowrap valign="top" width="33%"><?php _e('Between tags''dafix'?></th>
          <td><input name="between_taglist" type="text" id="between_taglist" value="<?php echo htmlspecialchars(dafix_get_option('between_taglist')); ?>" size="50" />
            <br />Custom markup between the tags.
          </td>
        </tr>
        
        <tr>
          <th nowrap valign="top" width="33%"><?php _e('After taglist''dafix'?></th>
          <td><input name="after_taglist" type="text" id="after_taglist" value="<?php echo htmlspecialchars(dafix_get_option('after_taglist')); ?>" size="50" />
            <br />Custom markup after the taglist.
          </td>
        </tr>

      </table>
    </fieldset>

    
    <div class="submit">
      <input type="submit" name="info_update" value="<?php _e('Update options''dafix'?>" />
      </div>
  </form>
</div><?php
}


add_action('admin_menu''dafix_admin');
// Tell WP to do some magic to the post title and content before
// saving it into the db.
add_filter('title_save_pre','dafix_title_change');
add_filter('content_save_pre','dafix_del_link_rel_tag');
add_filter('content_save_pre','dafix_remarkup_tags');

?>