<?php
/*
Plugin Name: irrepressible.info widget
Description: Adds a sidebar widget to undermine censorship
Author: Paul "TBBle" Hampson
Version: 1.0
Author URI: http://blog.tbble.net/

Based on gsearch.php (Google Search widget) by Automattic, Inc. (http://automattic.com)
*/

// Put functions into one big function we'll call at the plugins_loaded
// action. This ensures that all required plugin functions are defined.
function widget_irrepressible_init() {

    
// Check for the required plugin functions. This will prevent fatal
    // errors occurring when you deactivate the dynamic-sidebar plugin.
    
if ( !function_exists('register_sidebar_widget') )
        return;

    
// This is the function that outputs our little Google search form.
    
function widget_irrepressible($args) {
        
        
// $args is an array of strings that help widgets to conform to
        // the active theme: before_widget, before_title, after_widget,
        // and after_title are the array keys. Default tags: li and h2.
        
extract($args);

        
// Each widget can store its own options. We keep strings here.
        
$options get_option('widget_irrepressible');
        
$title $options['title'];
        
$size $options['size'];

        
// These lines generate our output. Widgets can be very complex
        // but as you can see here, they can also be very, very simple.
        // (This is even simpler than the gsearch one...)
        
echo $before_widget $before_title $title $after_title;
        
// I don't know for sure that the <p> tags are neccessary... Ocadia needed 'em.
        
echo '<p><script src="http://fragments.irrepressible.info/js/fragment-'.$size.'.js" type="text/javascript"></script></p>';
        echo 
$after_widget;
    }

    
// This is the function that outputs the form to let the users edit
    // the widget's title. It's an optional feature that users cry for.
    
function widget_irrepressible_control() {

        
// Get our options and see if we're handling a form submission.
        
$options get_option('widget_irrepressible');
        if ( !
is_array($options) )
            
$options = array('title'=>"Somebody doesn't want people to read this"'size'=>'180');
        if ( 
$_POST['irrepressible-submit'] ) {

            
// Remember to sanitize and format use input appropriately.
            
$options['title'] = strip_tags(stripslashes($_POST['irrepressible-title']));
            
$options['size'] = strip_tags(stripslashes($_POST['irrepressible-size']));
            
update_option('widget_irrepressible'$options);
        }

        
// Be sure you format your options to be valid HTML attributes.
        
$title htmlspecialchars($options['title'], ENT_QUOTES);
        
$size htmlspecialchars($options['size'], ENT_QUOTES);
        
        
// Here is our little form segment. Notice that we don't need a
        // complete form. This will be embedded into the existing form.
        
echo '<p style="text-align:right;"><label for="irrepressible-title">Title: <input style="width: 200px;" id="irrepressible-title" name="irrepressible-title" type="text" value="'.$title.'" /></label></p>';
        echo 
'<p style="text-align:right;"><label for="irrepressible-size">Box size: ';
        echo 
'<select id="irrepressible-size" name="irrepressible-size">';
        foreach (array(
468 => 'wide (468x60)',234 => 'half wide (234x60)',180 => 'rectangle (180x150)',125 => 'small square (125x125)') as $key => $value) {
            echo 
'<option value="'.$key.'" ';
            if (
$key == $size)
                echo 
'selected="selected" ';
            echo 
'>'.$value.'</option>';
        }
        echo 
'</select>';

//            <input style="width: 200px;" id="irrepressible-size" name="irrepressible-size" type="text" value="'.$size.'" /></label></p>';

        
echo '</label></p>';
        echo 
'<input type="hidden" id="irrepressible-submit" name="irrepressible-submit" value="1" />';
    }
    
    
// This registers our widget so it appears with the other available
    // widgets and can be dragged and dropped into any active sidebars.
    
register_sidebar_widget('irrepressible.info random content''widget_irrepressible');

    
// This registers our optional widget control form. Because of this
    // our widget will have a button that reveals a 300x100 pixel form.
    
register_widget_control('irrepressible.info random content''widget_irrepressible_control'300100);
}

// Run our code later in case this loads prior to any required plugins.
add_action('plugins_loaded''widget_irrepressible_init');

?>