<?php
/*
Plugin Name: Stewart's Filters Manager
Plugin URI: http://www.ugelow.com/2005/05/24/stewart-filters/
Version: 1.1
Description: Stewart's Filters Manager (formerly WP Filters Manager) allows you to turn on and off filter calls made by WordPress plugins on an individual basis without editing plugin or source code. Activating this plugin will add a "Filters" option to your Options menu. Requires WP 1.5.1+
Author: Stewart Ugelow
Author URI: http://www.ugelow.com
*/ 

// ---------------------------------------------------------------
// RYAN BOREN PLUGIN BOOTSTRAP HEADER
// ---------------------------------------------------------------

if (! function_exists('wpfm_run_wp_filters')) {
    function 
wpfm_run_wp_filters() {
        
add_options_page(__("Stewart's Filters Manager"), __("Filters"), 10__FILE__);
    }
}

if (
is_plugin_page()) {

// ---------------------------------------------------------------
// FUNCTIONS
// ---------------------------------------------------------------

function wpfm_create_database_table($name) {
global 
$wpdb;
$wpdb->query("CREATE TABLE IF NOT EXISTS $name  (
id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
diagnostic_type VARCHAR( 128 ) NOT NULL,
hook VARCHAR( 128 ) NOT NULL ,
filter VARCHAR( 128 ) NOT NULL ,
priority INT, 
status INT(1),
updated TIMESTAMP NOT NULL,
PRIMARY KEY ( `id` )
)"
);
//wpfm_create_database_table

function wpfm_layout_table_head() {
    global 
$thispage;
    print(
'<div class="wrap">
    <h2>Filters</h2>
    <table width="100%" cellpadding="3" cellspacing="3" style="text-align: left; vertical-align: top;">
    <tr>
        <th>Hook</th>
        <th>Filter</th>
        <th>Priority</th>
        <th>Action</th>
    </tr>'
);
    
    } 
//wpfm_layout_table_head()

function wpfm_wp_list_filters() {
    global 
$wp_filter
    global 
$wpdb;
    global 
$filters_manager;

    
ksort ($wp_filter);
    
$filters $wp_filter;
    foreach (
$filters as $hook => $value) {
    
        
krsort($value);
        
        foreach (
$value as $priority=>$combination) {
    
            if (
$combination) {
                foreach (
$combination as $prior=>$filter_action) {
                    
$listed_filters_array[] = array(hook=>$hookaction=>$filter_action['function'], priority=>$priority);
                } 
//foreach $combination as
            
//if $combination
    
        
//foreach $value
        
    
}    // foreach $filters as $hook
    
    
return $listed_filters_array;
}

function 
wpfm_layout_filter_rows($id,$filter_action,$priority,$filter_status) {
global 
$thispage;
    
$row_text '';
        if (
$filter_status == '0') {
            
$row_text .= "<td bgcolor='white'>&nbsp;</td><td>$filter_action</td><td>$priority</td>";
            
$row_text .= "<td><a href=\"$thispage&amp;action=activate&amp;id=$id\" class='edit'>Activate</a>";
        } 
//if filter_status = 0
        
        
if ($filter_status == '1') {    
            
$row_text .= "<td bgcolor='white'>&nbsp;</td><td><strong>$filter_action</strong></td><td>$priority</td>";
            
$row_text .= "<td><a href=\"$thispage&amp;action=deactivate&amp;id=$id\" class='delete'>Deactivate</a>";
        } 
//if filter_status = 1

    
$row_text .= "</td>";
    
$row_text .= "</tr>";
    return 
$row_text;


function 
wpfm_make_changes($action$id) {
    global 
$wpdb;
    global 
$filters_manager;
    global 
$filters_pre;

    if ((
$action == 'deactivate') && ($id)) {
    
$wpdb->query("update $filters_manager set status = 0 where id = $id");
    
$result $wpdb->get_results("select hook, filter from $filters_manager where id = $id");
    print(
'<div class="updated"><p>'.$result[0]->hook.', '.$result[0]->filter.' <strong>deactivated</strong></p></div>');
    }
    
    if ((
$action == 'activate') && ($id)) {
    
$wpdb->query("update $filters_manager set status = 1 where id = $id");
    
$result $wpdb->get_results("select hook, filter from $filters_manager where id = $id");
    print(
'<div class="updated"><p>'.$result[0]->hook.', '.$result[0]->filter.' <strong>activated</strong></p></div>');

    }
    
    if (
$action == 'reset') {
            
$wpdb->query("update $filters_manager set status = 1");
    print(
'<div class="updated"><p>All filters are <strong>reset</strong> to active.</p></div>');

    } 
//if resset
    
//wpfm_make_changes

function wpfm_execute_known_filters()  {
global 
$wpdb;
global 
$filters_manager;

$known_filters $wpdb->get_results("select id,hook,filter,priority,status from $filters_manager where diagnostic_type = 'filter'");

    if (
$known_filters) {
    
           foreach (
$known_filters as $known_filter) {
                   
            if (
$known_filter->status == 0) {
                
remove_filter($known_filter->hook$known_filter->filter$known_filter->priority);
                } 
//if stats = 0
            
           
//foreach known_filter_array
       
    
//if($known_filters)

//wpfm_execute_known_filters()

// ---------------------------------------------------------------
// DEFINE VARIABLES
// ---------------------------------------------------------------

global $table_prefix;
$filters_manager $table_prefix."filters_manager";
$thispage $_SERVER['PHP_SELF'].'?page=stewart-filters.php';
$wp_reference_source 'http://codex.wordpress.org/Special:Search?search=';
$known_filter_array = array();


// ---------------------------------------------------------------
// FILTER MANAGER MAIN
// ---------------------------------------------------------------

wpfm_create_database_table($filters_manager);
wpfm_layout_table_head();
wpfm_make_changes($_GET[action],$_GET[id]);

    
$filters_pre wpfm_wp_list_filters();
    
    
$database_filters $wpdb->get_results("select id,hook,filter,priority,status from $filters_manager where diagnostic_type = 'filter'");
        
    if (
$database_filters) {
    
        foreach (
$database_filters as $database_filter) {
            
$known_filter_array[$database_filter->id] = array(hook=>$database_filter->hookaction=>$database_filter->filterpriority=>$database_filter->priority);
        } 
//foreach
        
        
foreach ($filters_pre as $filtered) {
            
$filter_diff[] = $filtered[hook].'     '.$filtered[action].'     '.$filtered[priority];
        } 
//foreach filters_pre
        
        
foreach ($known_filter_array as $filtered) {
            
$known_diff[] = $filtered[hook].'     '.$filtered[action].'     '.$filtered[priority];
        } 
//foreach known_filter_array
    
        
$known_diff array_unique($known_diff);

        
//THIS PART LOOKS FOR FILTERS THAT ARE REGISTERED BUT NOT IN THE DATABASE
        //AND INSERTS THEM
    
        
$new_filters array_diff($filter_diff$known_diff);

        foreach (
$new_filters as $diff) {
        
$filtered explode('     ',$diff);
        
$wpdb->query("insert into $filters_manager (diagnostic_type, hook, filter, priority, status) values ('filter','$filtered[0]','$filtered[1]','$filtered[2]',1)");
        }
        
        
//THIS PART LOOKS FOR FILTERS THAT ARE UNREGISTERED BUT ARE STILL IN THE DATABASE
        //AND REMOVES THEM


        
$unregistered_filters array_diff($known_diff$filter_diff);
        foreach (
$unregistered_filters as $diff) {
            
$filtered explode('     ',$diff);
            
$wpdb->query("delete from $filters_manager where hook = '$filtered[0]' and filter = '$filtered[1]' and priority ='$filtered[2]'");
        } 
//foreach
        

        
    
//if database_filters
    
else {
            foreach (
$filters_pre as $filtered) {
            
$wpdb->query("insert into $filters_manager (diagnostic_type, hook, filter, priority, status) values ('filter','$filtered[hook]','$filtered[action]','$filtered[priority]',1)");
        } 
//foreach
    
//else    
    
    
    
    
wpfm_execute_known_filters();
    
$filters_post wpfm_wp_list_filters();
    
    
    
    
//LAYOUT FILTER ROWS
    
    
$layout_filters $wpdb->get_results("select id,hook,filter,priority,status from $filters_manager where diagnostic_type = 'filter' and status < 2 ORDER BY hook ASC, filter ASC, priority DESC");
    
    if (
$layout_filters) {
    
        foreach (
$layout_filters as $layout_filter) {
            
$filter_rows[$layout_filter->hook][] = &wpfm_layout_filter_rows($layout_filter->id,$layout_filter->filter,$layout_filter->priority,$layout_filter->status);
        } 
//foreach $layout_filters as $layout_filter
        
        
            
foreach ($filter_rows as $key => $value) {
                print(
"<tr><td colspan='4' align='left'><h4><a href=\"$wp_reference_source$key\" target=\"_blank\">$key</a></h4></td></tr>");
                
$rowcount 0;
                        
                        
ksort ($value);
                            
                            foreach (
$value as $line) {
                                
$rowcount $rowcount 1;
                                if (
is_integer($rowcount 2)) { 
                                    print(
"<tr>");
                                } 
//if is_integer
                                
else {
                                    print(
"<tr class='alternate'>");
                                } 
//else
                            
print($line);
                            
                            } 
//foreach $value as $line
                    
                
//foreach $filter_rows as $key => $value

        
print("</table></div>");
                
    } 
//if layout_filters

        
// ---------------------------------------------------------------
// RYAN BOREN BOOTSTRAP LOOP FOOTER
// ---------------------------------------------------------------

//if (is_plugin_page())
else {
    
add_action('admin_menu''wpfm_run_wp_filters');
//else

?>