From 733e7592c66a97efb7fe7fbe0ba97bad517a7a11 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 17 Mar 2009 00:59:42 +0000 Subject: [PATCH] Separate widget form and update callbacks. git-svn-id: http://svn.automattic.com/wordpress/trunk@10798 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/widgets.php | 2 +- wp-includes/default-widgets.php | 1 + wp-includes/widgets.php | 90 ++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/wp-admin/widgets.php b/wp-admin/widgets.php index fabdb5a82d..60d53a235f 100644 --- a/wp-admin/widgets.php +++ b/wp-admin/widgets.php @@ -73,7 +73,7 @@ if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) { */ $already_done = array(); - foreach ( $wp_registered_widget_controls as $name => $control ) { + foreach ( $wp_registered_widget_updates as $name => $control ) { if ( in_array( $control['callback'], $already_done ) ) continue; diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php index e259cf2466..da260bbbcb 100644 --- a/wp-includes/default-widgets.php +++ b/wp-includes/default-widgets.php @@ -216,6 +216,7 @@ class WP_Widget_Archives extends WP_Widget { return false; $instance = $old_instance; + $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') ); $instance['title'] = strip_tags($new_instance['title']); $instance['count'] = $new_instance['count'] ? 1 : 0; $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0; diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index 5feeae2256..efe96a660e 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -16,7 +16,7 @@ /* Global Variables */ /** @ignore */ -global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls; +global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; /** * Stores the sidebars, since many themes can have more than one. @@ -41,6 +41,7 @@ $wp_registered_widgets = array(); * @since 2.2.0 */ $wp_registered_widget_controls = array(); +$wp_registered_widget_updates = array(); /** * This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update() @@ -152,8 +153,12 @@ class WP_Widget { return array(&$this, 'display_callback'); } - function _get_control_callback() { - return array(&$this, 'control_callback'); + function _get_update_callback() { + return array(&$this, 'update_callback'); + } + + function _get_form_callback() { + return array(&$this, 'form_callback'); } /** Generate the actual widget content. @@ -171,9 +176,9 @@ class WP_Widget { $this->widget($args, $settings[$this->number]); } - /** Deal with changed settings and generate the control form. + /** Deal with changed settings. * Do NOT over-ride this function. */ - function control_callback( $widget_args = 1 ) { + function update_callback( $widget_args = 1 ) { global $wp_registered_widgets; if ( is_numeric($widget_args) ) @@ -223,8 +228,17 @@ class WP_Widget { $this->save_settings($all_instances); $this->updated = true; } + } + + /** Generate the control form. + * Do NOT over-ride this function. */ + function form_callback( $widget_args = 1 ) { + if ( is_numeric($widget_args) ) + $widget_args = array( 'number' => $widget_args ); + + $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); + $all_instances = $this->get_settings(); - // Here we echo out the form if ( -1 == $widget_args['number'] ) { // We echo out a form where 'number' can be set later via JS $this->_set('%i%'); @@ -240,7 +254,8 @@ class WP_Widget { /** Helper function: Registers a single instance. */ function _register_one($number = -1) { wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) ); - wp_register_widget_control( $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) ); + _register_widget_update_callback( $this->id, $this->name, $this->_get_update_callback(), $this->control_options, array( 'number' => $number ) ); + _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) ); } function save_settings($settings) { @@ -489,12 +504,13 @@ function wp_unregister_sidebar_widget($id) { * @param mixed $params,... Optional. Additional parameters to add to widget. */ function wp_register_widget_control($id, $name, $control_callback, $options = array()) { - global $wp_registered_widget_controls; + global $wp_registered_widget_controls, $wp_registered_widget_updates; $id = strtolower($id); if ( empty($control_callback) ) { unset($wp_registered_widget_controls[$id]); + unset($wp_registered_widget_updates[$id]); return; } @@ -514,6 +530,64 @@ function wp_register_widget_control($id, $name, $control_callback, $options = ar ); $widget = array_merge($widget, $options); + $wp_registered_widget_controls[$id] = $wp_registered_widget_updates[$id] = $widget; +} + +function _register_widget_update_callback($id, $name, $update_callback, $options = array()) { + global $wp_registered_widget_updates; + + $id = strtolower($id); + + if ( empty($update_callback) ) { + unset($wp_registered_widget_updates[$id]); + return; + } + + if ( isset($wp_registered_widget_updates[$id]) && !did_action( 'widgets_init' ) ) + return; + + $defaults = array('width' => 250, 'height' => 200 ); // height is never used + $options = wp_parse_args($options, $defaults); + $options['width'] = (int) $options['width']; + $options['height'] = (int) $options['height']; + + $widget = array( + 'name' => $name, + 'id' => $id, + 'callback' => $update_callback, + 'params' => array_slice(func_get_args(), 4) + ); + $widget = array_merge($widget, $options); + + $wp_registered_widget_updates[$id] = $widget; +} + +function _register_widget_form_callback($id, $name, $form_callback, $options = array()) { + global $wp_registered_widget_controls; + + $id = strtolower($id); + + if ( empty($form_callback) ) { + unset($wp_registered_widget_controls[$id]); + return; + } + + if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) ) + return; + + $defaults = array('width' => 250, 'height' => 200 ); // height is never used + $options = wp_parse_args($options, $defaults); + $options['width'] = (int) $options['width']; + $options['height'] = (int) $options['height']; + + $widget = array( + 'name' => $name, + 'id' => $id, + 'callback' => $form_callback, + 'params' => array_slice(func_get_args(), 4) + ); + $widget = array_merge($widget, $options); + $wp_registered_widget_controls[$id] = $widget; }