Make WordPress Core


Ignore:
Timestamp:
03/13/2009 10:27:38 PM (17 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

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.