Introduce singleton widget factory

git-svn-id: http://svn.automattic.com/wordpress/trunk@10808 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2009-03-17 23:44:36 +00:00
parent 1183bfa70b
commit 1eab8fccb2
3 changed files with 59 additions and 9 deletions

View File

@ -1219,19 +1219,19 @@ function wp_widgets_init() {
if ( !is_blog_installed() )
return;
new WP_Widget_Pages();
register_widget('WP_Widget_Pages');
new WP_Widget_Calendar();
register_widget('WP_Widget_Calendar');
new WP_Widget_Archives();
register_widget('WP_Widget_Archives');
new WP_Widget_Links();
register_widget('WP_Widget_Links');
new WP_Widget_Meta();
register_widget('WP_Widget_Meta');
new WP_Widget_Search();
register_widget('WP_Widget_Search');
new WP_Widget_Text();
register_widget('WP_Widget_Text');
$widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your blog") );
wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $widget_ops);

View File

@ -43,6 +43,30 @@ $wp_registered_widgets = array();
$wp_registered_widget_controls = array();
$wp_registered_widget_updates = array();
/**
* Singleton that registers and instantiates WP_Widget classes.
*
* @package WordPress
* @subpackage Widgets
* @since 2.8
*/
class WP_Widget_Factory {
var $widgets = array();
function WP_Widget_Factory() {
add_action( 'widgets_init', array( &$this, '_register_widgets' ), 100 );
}
function register($widget_class) {
$this->widgets[] = new $widget_class();
}
function _register_widgets() {
foreach ( $this->widgets as $widget )
$widget->_register();
}
}
/**
* This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
* and WP_Widget::form() need to be over-ridden.
@ -107,7 +131,7 @@ class WP_Widget {
$this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
$this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
add_action( 'widgets_init', array( &$this, 'register' ) );
//add_action( 'widgets_init', array( &$this, '_register' ) );
}
/** Helper function to be called by form().
@ -124,7 +148,7 @@ class WP_Widget {
/** Registers this widget-type.
* Called during the 'widgets_init' action. */
function register() {
function _register() {
$settings = $this->get_settings();
if ( empty($settings) ) {
@ -288,6 +312,25 @@ class WP_Widget {
/* Template tags & API functions */
/**
* Register a widget
*
* Registers a WP_Widget widget
*
* @since 2.8.0
*
* @see WP_Widget
* @see WP_Widget_Factory
* @uses WP_Widget_Factory
*
* @param string $widget_class The name of a class that extends WP_Widget
*/
function register_widget($widget_class) {
global $wp_widget_factory;
$wp_widget_factory->register($widget_class);
}
/**
* Creates multiple sidebars.
*

View File

@ -589,6 +589,13 @@ $wp_rewrite =& new WP_Rewrite();
*/
$wp =& new WP();
/**
* WordPress Widget Factory Object
* @global object $wp_widget_factory
* @since 2.8.0
*/
$wp_widget_factory =& new WP_Widget_Factory();
do_action('setup_theme');
/**