Make WordPress Core

Changeset 10781


Ignore:
Timestamp:
03/13/2009 10:27:38 PM (16 years ago)
Author:
azaozz
Message:

WP_Widgets: make save_settings() and get_settings() separate methods, convert defined single widgets settings to multi-widget format, small fixes for the Links widget, see #8441

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r10764 r10781  
    16501650        $this->id_base = $id_base;
    16511651        $this->name = $name;
    1652         $this->option_name = 'wp_widget_' . $id_base;
     1652        $this->option_name = 'widget_' . $id_base;
    16531653        $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
    16541654        $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
    16551655
    1656         //add_action( 'widgets_init', array( &$this, 'register' ) );
     1656        add_action( 'widgets_init', array( &$this, 'register' ) );
    16571657    }
    16581658
     
    16721672     *  Called during the 'widgets_init' action. */
    16731673    function register() {
    1674         if ( ! $all_instances = get_option($this->option_name) )
    1675             $all_instances = array();
    1676 
    1677         $registered = false;
    1678         foreach( array_keys($all_instances) as $number ) {
    1679             // Old widgets can have null values for some reason
    1680             if ( !isset($all_instances[$number]['_multiwidget']) )
    1681                 continue;
    1682 
    1683             $this->_set($number);
    1684             $registered = true;
    1685             $this->_register_one($number);
    1686         }
    1687 
    1688         // If there are none, we register the widget's existance with a
    1689         // generic template
    1690         if ( !$registered ) {
     1674        $settings = $this->get_settings();
     1675
     1676        if ( empty($settings) ) {
     1677            // If there are none, we register the widget's existance with a
     1678            // generic template
    16911679            $this->_set(1);
    16921680            $this->_register_one();
     1681        } elseif ( is_array($settings) ) {
     1682            foreach ( array_keys($settings) as $number ) {
     1683                if ( is_numeric($number) ) {
     1684                    $this->_set($number);
     1685                    $this->_register_one($number);
     1686                }
     1687            }
    16931688        }
    16941689    }
     
    17181713        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    17191714        $this->_set( $widget_args['number'] );
    1720 
    1721         // Data is stored as array:
    1722         //  array( number => data for that instance of the widget, ... )
    1723         $all_instances = get_option($this->option_name);
    1724         if ( isset($all_instances[$this->number]) )
    1725             $this->widget($args, $all_instances[$this->number]);
     1715        $settings = $this->get_settings();
     1716
     1717        if ( array_key_exists( $this->number, $settings ) )
     1718            $this->widget($args, $settings[$this->number]);
    17261719    }
    17271720
     
    17351728
    17361729        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    1737 
    1738         // Data is stored as array:
    1739         //  array( number => data for that instance of the widget, ... )
    1740         $all_instances = get_option($this->option_name);
    1741         if ( !is_array($all_instances) )
    1742             $all_instances = array();
     1730        $all_instances = $this->get_settings();
    17431731
    17441732        // We need to update the data
    17451733        if ( !$this->updated && !empty($_POST['sidebar']) ) {
     1734
    17461735            // Tells us what sidebar to put the data in
    17471736            $sidebar = (string) $_POST['sidebar'];
     
    17541743
    17551744            foreach ( $this_sidebar as $_widget_id )    {
    1756                 // Remove all widgets of this type from the sidebar.    We'll add the
    1757                 // new data in a second.    This makes sure we don't get any duplicate
     1745                // Remove all widgets of this type from the sidebar. We'll add the
     1746                // new data in a second. This makes sure we don't get any duplicate
    17581747                // data since widget ids aren't necessarily persistent across multiple
    17591748                // updates
     
    17741763                    $instance = $this->update($new_instance, array());
    17751764
    1776                 if ( !empty($instance) )    {
    1777                     $instance['_multiwidget'] = $number;
     1765                if ( !empty($instance) )
    17781766                    $all_instances[$number] = $instance;
    1779                 }
    17801767            }
    17811768
    1782             update_option($this->option_name, $all_instances);
    1783             $this->updated = true; // So that we don't go through this more than once
     1769            $this->save_settings($all_instances);
     1770            $this->updated = true;
    17841771        }
    17851772
    17861773        // Here we echo out the form
    17871774        if ( -1 == $widget_args['number'] ) {
    1788             // We echo out a template for a form which can be converted to a
    1789             // specific form later via JS
     1775            // We echo out a form where 'number' can be set later via JS
    17901776            $this->_set('%i%');
    17911777            $instance = array();
     
    17941780            $instance = $all_instances[ $widget_args['number'] ];
    17951781        }
     1782
    17961783        $this->form($instance);
    17971784    }
     
    18021789        wp_register_widget_control( $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) );
    18031790    }
    1804 
     1791   
     1792    function save_settings($settings) {
     1793        $settings['_multiwidget'] = 1;
     1794        update_option( $this->option_name, $settings );
     1795    }
     1796
     1797    function get_settings() {
     1798        $settings = get_option($this->option_name);
     1799
     1800        if ( !is_array($settings) )
     1801            return array();
     1802
     1803        if ( !array_key_exists('_multiwidget', $settings) ) {
     1804            // old format, conver if single widget
     1805            $settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
     1806        }
     1807
     1808        unset($settings['_multiwidget']);
     1809        return $settings;
     1810    }
    18051811}
    18061812
  • trunk/wp-includes/widgets.php

    r10774 r10781  
    656656
    657657    return $defaults;
     658}
     659
     660/**
     661 * Convert the widget settings from single to multi-widget format.
     662 *
     663 * @since 2.8.0
     664 *
     665 * @return array
     666 */
     667function wp_convert_widget_settings($base_name, $option_name, $settings) {
     668    // This test may need expanding.
     669    $single = false;
     670    foreach ( array_keys($settings) as $number ) {
     671        if ( !is_numeric($number) ) {
     672            $single = true;
     673            break;
     674        }
     675    }
     676
     677    if ( $single ) {
     678        $settings = array( 2 => $settings );
     679       
     680        $sidebars_widgets = get_option('sidebars_widgets');
     681        foreach ( (array) $sidebars_widgets as $index => $sidebar ) {
     682            if ( is_array($sidebar) ) {
     683                foreach ( $sidebar as $i => $name ) {
     684                    if ( $base_name == $name ) {
     685                        $sidebars_widgets[$index][$i] = "$name-2";
     686                        break 2;
     687                    }
     688                }
     689            }
     690        }
     691
     692        update_option('sidebars_widgets', $sidebars_widgets);
     693    }
     694
     695    $settings['_multiwidget'] = 1;
     696    update_option( $option_name, $settings );
     697   
     698    return $settings;
    658699}
    659700
     
    768809            return false;
    769810
    770         return $new_instance;
     811        $new_instance = (array) $new_instance;
     812        $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0);
     813        foreach ( $instance as $field => $val ) {
     814            if ( isset($new_instance[$field]) )
     815                $instance[$field] = 1;
     816        }
     817   
     818        return $instance;
    771819    }
    772820   
     
    775823        //Defaults
    776824        $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false) );
    777 /*
    778         if ( isset($_POST['links-submit']) ) {
    779             $newoptions = array();
    780             $newoptions['description'] = isset($_POST['links-description']);
    781             $newoptions['name'] = isset($_POST['links-name']);
    782             $newoptions['rating'] = isset($_POST['links-rating']);
    783             $newoptions['images'] = isset($_POST['links-images']);
    784 
    785             if ( $instance != $newoptions ) {
    786                 $instance = $newoptions;
    787                 update_option('widget_links', $instance);
    788             }
    789         }
    790 */
    791825?>
    792826        <p>
     
    19762010    $widget_ops = array('description' => __( "Your blogroll" ) );
    19772011    $wp_widget_links = new WP_Widget_Links('links', __('Links'), $widget_ops);
    1978     $wp_widget_links->register();
     2012    //$wp_widget_links->register();
    19792013
    19802014    $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
Note: See TracChangeset for help on using the changeset viewer.