<?php

/*
Plugin Name: No category base?
Description: Removes <em>category base</em> from category permalinks. Not well tested.
Author: Sam
Version: 0.1
*/

/*
	TODO Run only if permalink structure contains %category%
*/

register_activation_hook(basename(__FILE__), 'ncb_flush_rewrite_rules');
register_deactivation_hook(basename(__FILE__), 'ncb_flush_rewrite_rules');
// add_action('init', 'tinker_category_rewrite');
add_filter('category_rewrite_rules', 'ncb_category_rewrite_rules');
add_action('admin_notices', 'ncb_admin_notice');
add_action('wp_footer', 'ncb_notice');

function ncb_flush_rewrite_rules() {
	$GLOBALS['wp_rewrite']->flush_rules();
	$GLOBALS['ncb_admin_notice'] = 'Updated rewrite rules.';
}

function ncb_admin_notice() {
	if (isset($GLOBALS['ncb_admin_notice'])) {
		$self = basename(__FILE__);
		echo "<div id='message'><p>{$GLOBALS['ncb_admin_notice']} <cite>($self)</cite></p></div>";
	}
}

function ncb_notice() {
	global $wp, $wp_query;
	if (current_user_can('manage_options')) {
		echo '<pre style="background:white;color:darkorange;">';
		print_r(array('matched_rule' => $wp->matched_rule, 'matched_query' => $wp->matched_query, 'query_vars' => array_filter($wp_query->query_vars)));
		echo '</pre>';
	}
}

function ncb_category_rewrite_rules($rules) {
	// Replace the default rules
	global $wp_rewrite;
	
	$paths = get_all_category_paths();
	$cat_token_key_n = array_search('%category%', $wp_rewrite->rewritecode);
	$cat_rewritereplace_saved = $wp_rewrite->rewritereplace[$cat_token_key_n];
	// Substitute our category pattern
	$wp_rewrite->rewritereplace[$cat_token_key_n] = '('.implode('|', $paths).')';
	// Re-generate category rewrite rules w/ our structure
	$ncb_category_structure = str_replace($wp_rewrite->category_base, '', $wp_rewrite->get_category_permastruct());
	$rules = $wp_rewrite->generate_rewrite_rules($ncb_category_structure, EP_CATEGORIES);
	// Restore the original token %category%
	$wp_rewrite->rewritereplace[$cat_token_key_n] = $cat_rewritereplace_saved;
	return $rules;
}

function tinker_category_rewrite($wp_rewrite) {
	// Change category structure for ALL permalinks (posts too)
	// Patterns for category/postname/attachment were spoiled and mapped to wrong query!
	global $wp_rewrite;
	$wp_rewrite->category_structure = str_replace($wp_rewrite->category_base, '', $wp_rewrite->get_category_permastruct());
	$paths = get_all_category_paths();
	$cat_token_key_n = array_search('%category%', $wp_rewrite->rewritecode);
	$wp_rewrite->rewritereplace[$cat_token_key_n] = '('.implode('|', $paths).')';
}

function get_all_category_paths() {
	// Based on category-template.php#get_category_link()
	global $wp_rewrite;
	$output = array();
	$category_permastruct = $wp_rewrite->get_category_permastruct();
	
	$categories =& get_categories(array('hide_empty' => false, 'hierarchical' => false, 'orderby' => 'ID'));
	
	foreach ($categories as $category) {
		// print_r($category);
		$category_nicename = $category->category_nicename;
		if ( $parent = $category->category_parent )
			$category_nicename = get_category_parents($parent, false, '/', true) . $category_nicename;
			
		// echo "$category_nicename\n";

		$catlink = str_replace('%category%', $category_nicename, $category_permastruct);
		$catlink = user_trailingslashit($catlink, 'category');
		$output[] = $category_nicename;
		// echo "$catlink\n";
	}
	rsort($output, SORT_STRING); // so children precede parents, I hope
	return $output;
}

?>