Make WordPress Core


Ignore:
Timestamp:
04/11/2009 02:37:24 PM (16 years ago)
Author:
azaozz
Message:

Widgets page, first run, see #9511

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/widgets.php

    r10822 r10912  
    5050     * This function should check that $new_instance is set correctly.
    5151     * The newly calculated value of $instance should be returned.
     52     * If "false" is returned, the instance won't be saved/updated.
    5253     *
    5354     * @param array $new_instance New settings for this instance as input by the user via form()
    5455     * @param array $old_instance Old settings for this instance
    55      * @return array Settings to save
     56     * @return array Settings to save or bool false to cancel saving
    5657     */
    5758    function update($new_instance, $old_instance) {
     
    192193                $this_sidebar = array();
    193194
    194             foreach ( $this_sidebar as $_widget_id )    {
    195                 // Remove all widgets of this type from the sidebar. We'll add the
    196                 // new data in a second. This makes sure we don't get any duplicate
    197                 // data since widget ids aren't necessarily persistent across multiple
    198                 // updates
    199                 if ( $this->_get_display_callback() == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    200                     $number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    201                     if( !in_array( $this->id_base . '-' . $number, (array)$_POST['widget-id'] ) ) {
    202                         // the widget has been removed.
     195            if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
     196                // Delete the settings for this instance of the widget
     197                if ( isset($_POST['widget-id']) )
     198                    $del_id = $_POST['widget-id'];
     199                else
     200                    return;
     201
     202                if ( $this->_get_display_callback() == $wp_registered_widgets[$del_id]['callback'] && isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
     203                    $number = $wp_registered_widgets[$del_id]['params'][0]['number'];
     204
     205                    if ( $this->id_base . '-' . $number == $del_id ) {
    203206                        unset($all_instances[$number]);
    204207                    }
    205208                }
    206             }
    207 
    208             foreach ( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
    209                 $new_instance = stripslashes_deep($new_instance);
    210                 $this->_set($number);
    211 
    212                 if ( !isset($new_instance['submit']) )
    213                     continue;
    214 
    215                 if ( isset($all_instances[$number]) )
    216                     $instance = $this->update($new_instance, $all_instances[$number]);
    217                 else
    218                     $instance = $this->update($new_instance, array());
    219 
    220                 if ( !empty($instance) )
    221                     $all_instances[$number] = $instance;
     209            } else {
     210                foreach ( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
     211                    $new_instance = stripslashes_deep($new_instance);
     212                    $this->_set($number);
     213
     214                    if ( isset($all_instances[$number]) )
     215                        $instance = $this->update($new_instance, $all_instances[$number]);
     216                    else
     217                        $instance = $this->update($new_instance, array());
     218
     219                    if ( false !== $instance )
     220                        $all_instances[$number] = $instance;
     221                }
    222222            }
    223223
     
    237237
    238238        if ( -1 == $widget_args['number'] ) {
    239             // We echo out a form where 'number' can be set later via JS
    240             $this->_set('%i%');
     239            // We echo out a form where 'number' can be set later
     240            $this->_set('__i__');
    241241            $instance = array();
    242242        } else {
     
    246246
    247247        $this->form($instance);
    248 ?>
    249         <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
    250 <?php
    251248    }
    252249
     
    254251    function _register_one($number = -1) {
    255252        wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
    256         _register_widget_update_callback(   $this->id, $this->name, $this->_get_update_callback(), $this->control_options, array( 'number' => $number ) );
     253        _register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
    257254        _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
    258255    }
     
    580577
    581578    $id = strtolower($id);
     579    $update_id = preg_replace( '/-[0-9]+$/', '', $id );
    582580
    583581    if ( empty($control_callback) ) {
    584582        unset($wp_registered_widget_controls[$id]);
    585         unset($wp_registered_widget_updates[$id]);
     583        unset($wp_registered_widget_updates[$update_id]);
    586584        return;
    587585    }
     
    603601    $widget = array_merge($widget, $options);
    604602
    605     $wp_registered_widget_controls[$id] = $wp_registered_widget_updates[$id] = $widget;
    606 }
    607 
    608 function _register_widget_update_callback($id, $name, $update_callback, $options = array()) {
     603    $wp_registered_widget_controls[$id] = $widget;
     604
     605    if ( isset($wp_registered_widget_updates[$update_id]) )
     606        return;
     607
     608    if ( isset($widget['params'][0]['number']) )
     609        $widget['params'][0]['number'] = -1;
     610
     611    unset($widget['width'], $widget['height'], $widget['name'], $widget['id']);
     612    $wp_registered_widget_updates[$update_id] = $widget;
     613}
     614
     615function _register_widget_update_callback($id_base, $update_callback, $options = array()) {
    609616    global $wp_registered_widget_updates;
    610617
    611     $id = strtolower($id);
    612 
    613     if ( empty($update_callback) ) {
    614         unset($wp_registered_widget_updates[$id]);
     618    if ( isset($wp_registered_widget_updates[$id_base]) ) {
     619        if ( empty($update_callback) )
     620            unset($wp_registered_widget_updates[$id_base]);
    615621        return;
    616622    }
    617623
    618     if ( isset($wp_registered_widget_updates[$id]) && !did_action( 'widgets_init' ) )
    619         return;
    620 
    621     $defaults = array('width' => 250, 'height' => 200 ); // height is never used
    622     $options = wp_parse_args($options, $defaults);
    623     $options['width'] = (int) $options['width'];
    624     $options['height'] = (int) $options['height'];
    625 
    626624    $widget = array(
    627         'name' => $name,
    628         'id' => $id,
    629625        'callback' => $update_callback,
    630         'params' => array_slice(func_get_args(), 4)
     626        'params' => array_slice(func_get_args(), 3)
    631627    );
     628
    632629    $widget = array_merge($widget, $options);
    633 
    634     $wp_registered_widget_updates[$id] = $widget;
     630    $wp_registered_widget_updates[$id_base] = $widget;
    635631}
    636632
     
    648644        return;
    649645
    650     $defaults = array('width' => 250, 'height' => 200 ); // height is never used
     646    $defaults = array('width' => 250, 'height' => 200 );
    651647    $options = wp_parse_args($options, $defaults);
    652648    $options['width'] = (int) $options['width'];
Note: See TracChangeset for help on using the changeset viewer.