Make WordPress Core

Changeset 10782


Ignore:
Timestamp:
03/13/2009 11:18:57 PM (17 years ago)
Author:
ryan
Message:

s/WP_Widgets/WP_Widget/ and move class to widgets.php

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r10781 r10782  
    15911591}
    15921592
    1593 /**
    1594  * This class must be extended for each widget and WP_Widgets::widget(), WP_Widgets::update()
    1595  * and WP_Widgets::form() need to be over-ridden.
    1596  *
    1597  * @package WordPress
    1598  * @since 2.8
    1599  */
    1600 class WP_Widgets {
    1601 
    1602         var $id_base;           // Root id for all widgets of this type.
    1603         var $name;                              // Name for this widget type.
    1604         var $widget_options;    // Option array passed to wp_register_sidebar_widget()
    1605         var $control_options;   // Option array passed to wp_register_widget_control()
    1606 
    1607         var $number = false;    // Unique ID number of the current instance.
    1608         var $id = false;                // Unique ID string of the current instance (id_base-number)
    1609         var $updated = false;   // Set true when we update the data after a POST submit - makes sure we don't do it twice.
    1610 
    1611         // Member functions that you must over-ride.
    1612 
    1613         /** Echo the actual widget content. Subclasses should over-ride this function
    1614          *      to generate their widget code. */
    1615         function widget($args, $instance) {
    1616                 die('function WP_Widgets::widget() must be over-ridden in a sub-class.');
    1617         }
    1618 
    1619         /** Update a particular instance.
    1620          *      This function should check that $new_instance is set correctly.
    1621          *      The newly calculated value of $instance should be returned. */
    1622         function update($new_instance, $old_instance) {
    1623                 die('function WP_Widgets::update() must be over-ridden in a sub-class.');
    1624         }
    1625 
    1626         /** Echo a control form for the current instance. */
    1627         function form($instance) {
    1628                 die('function WP_Widgets::form() must be over-ridden in a sub-class.');
    1629         }
    1630 
    1631         // Functions you'll need to call.
    1632 
    1633         /**
    1634          * PHP4 constructor
    1635          */
    1636         function WP_Widgets( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    1637                 $this->__construct( $id_base, $name, $widget_options, $control_options );
    1638         }
    1639 
    1640         /**
    1641          * PHP5 constructor
    1642          *       widget_options: passed to wp_register_sidebar_widget()
    1643          *       - description
    1644          *       - classname
    1645          *       control_options: passed to wp_register_widget_control()
    1646          *       - width
    1647          *       - height
    1648          */
    1649         function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    1650                 $this->id_base = $id_base;
    1651                 $this->name = $name;
    1652                 $this->option_name = 'widget_' . $id_base;
    1653                 $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
    1654                 $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
    1655 
    1656                 add_action( 'widgets_init', array( &$this, 'register' ) );
    1657         }
    1658 
    1659         /** Helper function to be called by form().
    1660          *      Returns an HTML name for the field. */
    1661         function get_field_name($field_name) {
    1662                 return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
    1663         }
    1664 
    1665         /** Helper function to be called by form().
    1666          *      Returns an HTML id for the field. */
    1667         function get_field_id($field_name) {
    1668                 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
    1669         }
    1670 
    1671         /** Registers this widget-type.
    1672          *      Called during the 'widgets_init' action. */
    1673         function register() {
    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
    1679                         $this->_set(1);
    1680                         $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                         }
    1688                 }
    1689         }
    1690 
    1691         // Private Functions. Don't worry about these.
    1692 
    1693         function _set($number) {
    1694                 $this->number = $number;
    1695                 $this->id = $this->id_base . '-' . $number;
    1696         }
    1697 
    1698         function _get_display_callback() {
    1699                 return array(&$this, 'display_callback');
    1700         }
    1701 
    1702         function _get_control_callback() {
    1703                 return array(&$this, 'control_callback');
    1704         }
    1705 
    1706         /** Generate the actual widget content.
    1707          *      Just finds the instance and calls widget().
    1708          *      Do NOT over-ride this function. */
    1709         function display_callback( $args, $widget_args = 1 ) {
    1710                 if ( is_numeric($widget_args) )
    1711                         $widget_args = array( 'number' => $widget_args );
    1712 
    1713                 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    1714                 $this->_set( $widget_args['number'] );
    1715                 $settings = $this->get_settings();
    1716 
    1717                 if ( array_key_exists( $this->number, $settings ) )
    1718                         $this->widget($args, $settings[$this->number]);
    1719         }
    1720 
    1721         /** Deal with changed settings and generate the control form.
    1722          *      Do NOT over-ride this function. */
    1723         function control_callback( $widget_args = 1 ) {
    1724                 global $wp_registered_widgets;
    1725 
    1726                 if ( is_numeric($widget_args) )
    1727                         $widget_args = array( 'number' => $widget_args );
    1728 
    1729                 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    1730                 $all_instances = $this->get_settings();
    1731 
    1732                 // We need to update the data
    1733                 if ( !$this->updated && !empty($_POST['sidebar']) ) {
    1734 
    1735                         // Tells us what sidebar to put the data in
    1736                         $sidebar = (string) $_POST['sidebar'];
    1737 
    1738                         $sidebars_widgets = wp_get_sidebars_widgets();
    1739                         if ( isset($sidebars_widgets[$sidebar]) )
    1740                                 $this_sidebar =& $sidebars_widgets[$sidebar];
    1741                         else
    1742                                 $this_sidebar = array();
    1743 
    1744                         foreach ( $this_sidebar as $_widget_id )        {
    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
    1747                                 // data since widget ids aren't necessarily persistent across multiple
    1748                                 // updates
    1749                                 if ( $this->_get_display_callback() == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
    1750                                         $number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
    1751                                         if( !in_array( $this->id_base . '-' . $number, (array)$_POST['widget-id'] ) ) {
    1752                                                 // the widget has been removed.
    1753                                                 unset($all_instances[$number]);
    1754                                         }
    1755                                 }
    1756                         }
    1757 
    1758                         foreach( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
    1759                                 $this->_set($number);
    1760                                 if ( isset($all_instances[$number]) )
    1761                                         $instance = $this->update($new_instance, $all_instances[$number]);
    1762                                 else
    1763                                         $instance = $this->update($new_instance, array());
    1764 
    1765                                 if ( !empty($instance) )
    1766                                         $all_instances[$number] = $instance;
    1767                         }
    1768 
    1769                         $this->save_settings($all_instances);
    1770                         $this->updated = true;
    1771                 }
    1772 
    1773                 // Here we echo out the form
    1774                 if ( -1 == $widget_args['number'] ) {
    1775                         // We echo out a form where 'number' can be set later via JS
    1776                         $this->_set('%i%');
    1777                         $instance = array();
    1778                 } else {
    1779                         $this->_set($widget_args['number']);
    1780                         $instance = $all_instances[ $widget_args['number'] ];
    1781                 }
    1782 
    1783                 $this->form($instance);
    1784         }
    1785 
    1786         /** Helper function: Registers a single instance. */
    1787         function _register_one($number = -1) {
    1788                 wp_register_sidebar_widget(     $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
    1789                 wp_register_widget_control(     $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) );
    1790         }
    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         }
    1811 }
    1812 
    18131593?>
  • trunk/wp-includes/widgets.php

    r10781 r10782  
    4242 */
    4343$wp_registered_widget_controls = array();
     44
     45/**
     46 * This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
     47 * and WP_Widget::form() need to be over-ridden.
     48 *
     49 * @package WordPress
     50 * @subpackage Widgets
     51 * @since 2.8
     52 */
     53class WP_Widget {
     54
     55        var $id_base;           // Root id for all widgets of this type.
     56        var $name;                              // Name for this widget type.
     57        var $widget_options;    // Option array passed to wp_register_sidebar_widget()
     58        var $control_options;   // Option array passed to wp_register_widget_control()
     59
     60        var $number = false;    // Unique ID number of the current instance.
     61        var $id = false;                // Unique ID string of the current instance (id_base-number)
     62        var $updated = false;   // Set true when we update the data after a POST submit - makes sure we don't do it twice.
     63
     64        // Member functions that you must over-ride.
     65
     66        /** Echo the actual widget content. Subclasses should over-ride this function
     67         *      to generate their widget code. */
     68        function widget($args, $instance) {
     69                die('function WP_Widget::widget() must be over-ridden in a sub-class.');
     70        }
     71
     72        /** Update a particular instance.
     73         *      This function should check that $new_instance is set correctly.
     74         *      The newly calculated value of $instance should be returned. */
     75        function update($new_instance, $old_instance) {
     76                die('function WP_Widget::update() must be over-ridden in a sub-class.');
     77        }
     78
     79        /** Echo a control form for the current instance. */
     80        function form($instance) {
     81                die('function WP_Widget::form() must be over-ridden in a sub-class.');
     82        }
     83
     84        // Functions you'll need to call.
     85
     86        /**
     87         * PHP4 constructor
     88         */
     89        function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) {
     90                $this->__construct( $id_base, $name, $widget_options, $control_options );
     91        }
     92
     93        /**
     94         * PHP5 constructor
     95         *       widget_options: passed to wp_register_sidebar_widget()
     96         *       - description
     97         *       - classname
     98         *       control_options: passed to wp_register_widget_control()
     99         *       - width
     100         *       - height
     101         */
     102        function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
     103                $this->id_base = $id_base;
     104                $this->name = $name;
     105                $this->option_name = 'widget_' . $id_base;
     106                $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
     107                $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
     108
     109                add_action( 'widgets_init', array( &$this, 'register' ) );
     110        }
     111
     112        /** Helper function to be called by form().
     113         *      Returns an HTML name for the field. */
     114        function get_field_name($field_name) {
     115                return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
     116        }
     117
     118        /** Helper function to be called by form().
     119         *      Returns an HTML id for the field. */
     120        function get_field_id($field_name) {
     121                return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
     122        }
     123
     124        /** Registers this widget-type.
     125         *      Called during the 'widgets_init' action. */
     126        function register() {
     127                $settings = $this->get_settings();
     128
     129                if ( empty($settings) ) {
     130                        // If there are none, we register the widget's existance with a
     131                        // generic template
     132                        $this->_set(1);
     133                        $this->_register_one();
     134                } elseif ( is_array($settings) ) {
     135                        foreach ( array_keys($settings) as $number ) {
     136                                if ( is_numeric($number) ) {
     137                                        $this->_set($number);
     138                                        $this->_register_one($number);
     139                                }
     140                        }
     141                }
     142        }
     143
     144        // Private Functions. Don't worry about these.
     145
     146        function _set($number) {
     147                $this->number = $number;
     148                $this->id = $this->id_base . '-' . $number;
     149        }
     150
     151        function _get_display_callback() {
     152                return array(&$this, 'display_callback');
     153        }
     154
     155        function _get_control_callback() {
     156                return array(&$this, 'control_callback');
     157        }
     158
     159        /** Generate the actual widget content.
     160         *      Just finds the instance and calls widget().
     161         *      Do NOT over-ride this function. */
     162        function display_callback( $args, $widget_args = 1 ) {
     163                if ( is_numeric($widget_args) )
     164                        $widget_args = array( 'number' => $widget_args );
     165
     166                $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
     167                $this->_set( $widget_args['number'] );
     168                $settings = $this->get_settings();
     169
     170                if ( array_key_exists( $this->number, $settings ) )
     171                        $this->widget($args, $settings[$this->number]);
     172        }
     173
     174        /** Deal with changed settings and generate the control form.
     175         *      Do NOT over-ride this function. */
     176        function control_callback( $widget_args = 1 ) {
     177                global $wp_registered_widgets;
     178
     179                if ( is_numeric($widget_args) )
     180                        $widget_args = array( 'number' => $widget_args );
     181
     182                $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
     183                $all_instances = $this->get_settings();
     184
     185                // We need to update the data
     186                if ( !$this->updated && !empty($_POST['sidebar']) ) {
     187
     188                        // Tells us what sidebar to put the data in
     189                        $sidebar = (string) $_POST['sidebar'];
     190
     191                        $sidebars_widgets = wp_get_sidebars_widgets();
     192                        if ( isset($sidebars_widgets[$sidebar]) )
     193                                $this_sidebar =& $sidebars_widgets[$sidebar];
     194                        else
     195                                $this_sidebar = array();
     196
     197                        foreach ( $this_sidebar as $_widget_id )        {
     198                                // Remove all widgets of this type from the sidebar. We'll add the
     199                                // new data in a second. This makes sure we don't get any duplicate
     200                                // data since widget ids aren't necessarily persistent across multiple
     201                                // updates
     202                                if ( $this->_get_display_callback() == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
     203                                        $number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
     204                                        if( !in_array( $this->id_base . '-' . $number, (array)$_POST['widget-id'] ) ) {
     205                                                // the widget has been removed.
     206                                                unset($all_instances[$number]);
     207                                        }
     208                                }
     209                        }
     210
     211                        foreach( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
     212                                $this->_set($number);
     213                                if ( isset($all_instances[$number]) )
     214                                        $instance = $this->update($new_instance, $all_instances[$number]);
     215                                else
     216                                        $instance = $this->update($new_instance, array());
     217
     218                                if ( !empty($instance) )
     219                                        $all_instances[$number] = $instance;
     220                        }
     221
     222                        $this->save_settings($all_instances);
     223                        $this->updated = true;
     224                }
     225
     226                // Here we echo out the form
     227                if ( -1 == $widget_args['number'] ) {
     228                        // We echo out a form where 'number' can be set later via JS
     229                        $this->_set('%i%');
     230                        $instance = array();
     231                } else {
     232                        $this->_set($widget_args['number']);
     233                        $instance = $all_instances[ $widget_args['number'] ];
     234                }
     235
     236                $this->form($instance);
     237        }
     238
     239        /** Helper function: Registers a single instance. */
     240        function _register_one($number = -1) {
     241                wp_register_sidebar_widget(     $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
     242                wp_register_widget_control(     $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) );
     243        }
     244       
     245        function save_settings($settings) {
     246                $settings['_multiwidget'] = 1;
     247                update_option( $this->option_name, $settings );
     248        }
     249
     250        function get_settings() {
     251                $settings = get_option($this->option_name);     
     252
     253                if ( !is_array($settings) )
     254                        return array();
     255
     256                if ( !array_key_exists('_multiwidget', $settings) ) {
     257                        // old format, conver if single widget
     258                        $settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
     259                }
     260
     261                unset($settings['_multiwidget']);
     262                return $settings;
     263        }
     264}
    44265
    45266/* Template tags & API functions */
     
    7851006 * @since 2.8.0
    7861007 */
    787 class WP_Widget_Links extends WP_Widgets {
     1008class WP_Widget_Links extends WP_Widget {
    7881009       
    7891010        function widget( $args, $instance ) {
Note: See TracChangeset for help on using the changeset viewer.