<?php
/**
 * #9701 Plugin
 *  *  
 *  
 * Plugin Name: #9701 Test Plugin
 * Plugin URI: http://wordpress.org/
 * Description: Test Plugin to provide a Widget and a Callback to output that Widget with a new "the_widget" template tag.
 * Author: hakre
 * Version: 0.1
 * Author URI: http://codex.wordpress.org/User:Hakre
*/

/**
 * Test Plugin Class 
 */
class plugin9701test {

	/**
	 * constructor
	 * 
	 * @return
	 */
	public function plugin9701test() {		
		
		// register widget
		add_action('widgets_init', create_function('', 'return register_widget(\'widget9701\');'));

		// footer output
		add_action('wp_footer', array($this, 'wp_footer'));
	} 
	
	function wp_footer() {		
	?>
		<ul>
		<?php the_widget('widget9701', array('title' => 'Demo Widget w/o Sidebar')); // Demo?>
		<?php the_widget('WP_Widget_Recent_Posts'); // Widget Classname Example ?>
		<?php the_widget('Recent Comments'); // Widget Name Example ?>
		</ul>
	<?php 
	}
}

$plugin9701test = new plugin9701test();

/**
 * Test Widget Class
 * 
 */
class widget9701 Extends WP_Widget {
	
	/** Echo the widget content.
	 *
	 * Subclasses should over-ride this function to generate their widget code.
	 *
	 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
	 * @param array $instance The settings for the particular instance of the widget
	 */
	function widget($args, $instance) {
		
		extract( $args );		
		extract( $instance );
		
		if (!strlen($title)) $title = '#9701 Test (Default Title)';
		
			echo $before_widget;
			echo $before_title . $title . $after_title;		
		?>
		
		<div style="font-size:1.5em; overflow:scroll;">
		This widget displays it's innerst values:<br />	
		<?php var_dump($args, $instance); ?>
		</div>

		<?php
			echo $after_widget;
	}

	/** Update a particular instance.
	 *
	 * This function should check that $new_instance is set correctly.
	 * The newly calculated value of $instance should be returned.
	 * If "false" is returned, the instance won't be saved/updated.
	 *
	 * @param array $new_instance New settings for this instance as input by the user via form()
	 * @param array $old_instance Old settings for this instance
	 * @return array Settings to save or bool false to cancel saving
	 */
	function update($new_instance, $old_instance) {				
		return $new_instance;
	}
	
	/** Echo the settings update form
	 *
	 * @param array $instance Current settings
	 */	
	function form($instance) {				
		$title = attr($instance['title']);
		?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
		<?php 
	}
	
}

/** widget template tag
 * 
 * get a configured widgets output without the need of a sidebar and backend administration. 
 * 
 * NOTE: this is development code
 * 
 * @global WP_Widget_Factory $wp_widget_factory 
 * @global array $wp_registered_sidebars
 * 
 * @param string $widget   widget identifier
 * @param array  $settings optional widget settings
 * @param array  $args     optional sidebar options  
 * @return void
 */
function the_widget($widget, $settings = array(), $args = array()) {
	
	/* sanitize input */
	$widget = (string) $widget;
	if (!strlen($widget))
		return;
	if (!is_array($settings))
		return;
	if (!is_array($args))
		return;

	/* globals */	
	global $wp_widget_factory, $wp_registered_sidebars;	
	
	/* pull default (def) sidebar options (args) */	
	$sidebar_id = register_sidebar( array('name' => uniqid('temp-')) );	
	$def  = $wp_registered_sidebars[$sidebar_id];
	$args = array_merge($def, $args);
							
	/* get widget ($instance) */
 	$instance = null;
	if (isset($wp_widget_factory->widgets[$widget])) {
		$instance = $wp_widget_factory->widgets[$widget];
	} else {
		foreach($wp_widget_factory->widgets as $value)
			if (isset($value->name))
				if ($value->name == $widget) {
					$instance = $value;
					break;
				}
	}
	
	/*  display widget */
	if (is_a($instance, 'WP_Widget')) {
		$settings = apply_filters('widget_display_callback', $settings, $instance);   
		if ( false !== $settings )
			$instance->widget($args, $settings);
	}	
	
	unregister_sidebar($sidebar_id);
}