[wp-trac] Re: [WordPress Trac] #8441: New Widget API based on MultiWidget Class

WordPress Trac wp-trac at lists.automattic.com
Sun Apr 19 23:45:07 GMT 2009


#8441: New Widget API based on MultiWidget Class
----------------------------+-----------------------------------------------
 Reporter:  thornomad       |       Owner:  azaozz                      
     Type:  task (blessed)  |      Status:  new                         
 Priority:  high            |   Milestone:  2.8                         
Component:  Widgets         |     Version:  2.8                         
 Severity:  normal          |    Keywords:  widget, plugin, multi-widget
----------------------------+-----------------------------------------------

Comment(by ceenz):

 Hi,
 I was wondering if it was possible to add a couple of hooks to the new
 widget api to allow for filtering the display of individual widgets based
 on options or criteria from a plugin, and secondly allow individual
 widgets to have applied to them unique options or display criteria applied
 to them by plugin.

 This would be achieved by adding two filter hooks and one action hook to
 the update_callback, display_callback, and form_callback functions. I have
 included my recommendation.

 New Filters:
 display_widget_test
 default_widget_options_update

 New Action:
 default_widget_options_display


 {{{
         function update_callback( $widget_args = 1 ) {
                 global $wp_registered_widgets;

                 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();

                 // We need to update the data
                 if ( !$this->updated && !empty($_POST['sidebar']) ) {

                         // Tells us what sidebar to put the data in
                         $sidebar = (string) $_POST['sidebar'];

                         $sidebars_widgets = wp_get_sidebars_widgets();
                         if ( isset($sidebars_widgets[$sidebar]) )
                                 $this_sidebar =&
 $sidebars_widgets[$sidebar];
                         else
                                 $this_sidebar = array();

                         if ( isset($_POST['delete_widget']) &&
 $_POST['delete_widget'] ) {
                                 // Delete the settings for this instance
 of the widget
                                 if ( isset($_POST['widget-id']) )
                                         $del_id = $_POST['widget-id'];
                                 else
                                         return;

                                 if ( $this->_get_display_callback() ==
 $wp_registered_widgets[$del_id]['callback'] &&
 isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
                                         $number =
 $wp_registered_widgets[$del_id]['params'][0]['number'];

                                         if ( $this->id_base . '-' .
 $number == $del_id ) {
 unset($all_instances[$number]);
                                         }
                                 }
                         } else {
                                 foreach ( (array) $_POST['widget-' .
 $this->id_base] as $number => $new_instance ) {
                                         $new_instance =
 stripslashes_deep($new_instance);
                                         $this->_set($number);

                                         if (
 isset($all_instances[$number]) )
                                                 $instance =
 $this->update($new_instance, $all_instances[$number]);
                                         else
                                                 $instance =
 $this->update($new_instance, array());

                                         if ( false !== $instance ) {
                                           $widget_details_compare =
 array($new_instance,$instance);
             $instance =
 apply_filters('default_widget_options_update',$widget_details_compare);
 //Callback to apply default or other options to be added to list of widget
 options to store in database
                                                 $all_instances[$number] =
 $instance;

                                         }
                                 }
                         }

                         $this->save_settings($all_instances);
                         $this->updated = true;
                 }
         }
 }}}

 {{{
         function display_callback( $args, $widget_args = 1 ) {
                 if ( is_numeric($widget_args) )
                         $widget_args = array( 'number' => $widget_args );

                 $widget_args = wp_parse_args( $widget_args, array(
 'number' => -1 ) );
                 $this->_set( $widget_args['number'] );
                 $settings = $this->get_settings();

                 if ( array_key_exists( $this->number, $settings ) ) {
       $hide_widget = apply_filters('widget_display_test',
 $settings[$this->number]); //Add hook to test if widget should be
 displayed.
       if (!$hide_widget) $this->widget($args, $settings[$this->number]);
 //Test and display widget.
       $this->widget($args, $settings[$this->number]);
     }
         }

 }}}


 {{{
         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();

                 if ( -1 == $widget_args['number'] ) {
                         // We echo out a form where 'number' can be set
 later
                         $this->_set('__i__');
                         $instance = array();
                 } else {
                         $this->_set($widget_args['number']);
                         $instance = $all_instances[ $widget_args['number']
 ];
                 }

                 $this->form($instance);
         $widget_details = array($this->id_base,$this->number,$instance);
     do_action('default_widget_options_display', $widget_details); //Add
 action hook to display default widget information or options
         }
 }}}

 Please let me know what you think. I believe that these three small
 changes would eaisly allow for the adding of plugins to filter the display
 of individual widgets and add widget options to unique instances of
 multiwidgets.

 Many thanks,
 Chris

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/8441#comment:44>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list