<?php
/*
Plugin Name: Slugger+
Plugin URI: http://justinblanton.com/projects/sluggerplus/
Description: Allows you to specify a post slug from within MarsEdit.
Version: 0.3
Author: Justin Blanton
Author URI: http://justinblanton.com/
*/

/************************************* 
This plugin is a rather simple modification of the original Slugger plugin
by Colin Devroe, which can be found at http://chancecube.com/products/slugger/.

The basic difference is that mine removes the slug-specifying text during the 
initial XML-RPC transaction, thereby keeping your post content clean and obviating
the need to filter the slug-specifying text each time the post is accessed. 
*************************************/


/* slugger_getslug
* Reads in the post content, finds the slug, and sets it as a global variable.
* @param STRING
* @return STRING
*/
function slugger_getslug($post_content) {
    
    
$slug slugger_findslug($post_content);
    
    if (
$slug) {
        
$GLOBALS['slug'] = $slug;
    }   
    
    
$temp '/(' slugger_regExEscape('[slug]') . '(.*?)' slugger_regExEscape('[/slug]') . ')/i';
    
$post_content = (preg_replace($temp''$post_content));
    
return 
$post_content;
}

/* slugger_editslug
* Globals in slug, strips spaces, edits post_name.
* @param STRING
* @return STRING
*/
function slugger_editslug($post_name) {
    global 
$slug;
    
    
// If we have a slug and the postname is empty.
    // A post_name from within WP overrides Slugger
    
if ($slug && $post_name == '') {
        
        
// Strip spaces and replace with dashes
        
$slug str_replace(' ''-'$slug);
        
        
// Set post_name equal to our slug
        
$post_name $slug;
    }

return 
$post_name;
}

/* slugger_findslug
* Rips through the post content, finds the slug, and returns ONLY the slug
* @param STRING
* @return STRING
*/
function slugger_findslug($text) {
    
    
$postname '/(' slugger_regExEscape('[slug]') . '(.*?)' slugger_regExEscape('[/slug]') . ')/i';
    
    
preg_match_all($postname$text$matches);
    
    if (
$matches) {
        foreach (
$matches[2] as $match) {
            if (
$match) {
                return 
$match;
            }
        }
    } else {
        return 
false;
    }
    
// WHOOPSIE
}

/* slugger_regExEscape
* Escapes for the regular expression.
* @param STRING
* @return STRING
*/
function slugger_regExEscape($str) {
    
$str str_replace('\\''\\\\'$str);
    
$str str_replace('/''\\/'$str);
    
$str str_replace('[''\\['$str);
    
$str str_replace(']''\\]'$str);

return 
$str;
}

add_filter('content_save_pre''slugger_getslug'); // Grab post name and save to a global
add_filter('name_save_pre''slugger_editslug'); // See if there is a global, and set it to post_name
?>