WordPress.org

Make WordPress Core

Changeset 10764


Ignore:
Timestamp:
03/10/2009 07:42:22 PM (11 years ago)
Author:
azaozz
Message:

Add WP_Widget class, first run - only the Links widget is using it at the moment, see #8441

Location:
trunk/wp-includes
Files:
2 edited

Legend:

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

    r10672 r10764  
    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 */
     1600class 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 = 'wp_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        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 ) {
     1691            $this->_set(1);
     1692            $this->_register_one();
     1693        }
     1694    }
     1695
     1696    // Private Functions. Don't worry about these.
     1697
     1698    function _set($number) {
     1699        $this->number = $number;
     1700        $this->id = $this->id_base . '-' . $number;
     1701    }
     1702
     1703    function _get_display_callback() {
     1704        return array(&$this, 'display_callback');
     1705    }
     1706
     1707    function _get_control_callback() {
     1708        return array(&$this, 'control_callback');
     1709    }
     1710
     1711    /** Generate the actual widget content.
     1712     *  Just finds the instance and calls widget().
     1713     *  Do NOT over-ride this function. */
     1714    function display_callback( $args, $widget_args = 1 ) {
     1715        if ( is_numeric($widget_args) )
     1716            $widget_args = array( 'number' => $widget_args );
     1717
     1718        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
     1719        $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]);
     1726    }
     1727
     1728    /** Deal with changed settings and generate the control form.
     1729     *  Do NOT over-ride this function. */
     1730    function control_callback( $widget_args = 1 ) {
     1731        global $wp_registered_widgets;
     1732
     1733        if ( is_numeric($widget_args) )
     1734            $widget_args = array( 'number' => $widget_args );
     1735
     1736        $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();
     1743
     1744        // We need to update the data
     1745        if ( !$this->updated && !empty($_POST['sidebar']) ) {
     1746            // Tells us what sidebar to put the data in
     1747            $sidebar = (string) $_POST['sidebar'];
     1748
     1749            $sidebars_widgets = wp_get_sidebars_widgets();
     1750            if ( isset($sidebars_widgets[$sidebar]) )
     1751                $this_sidebar =& $sidebars_widgets[$sidebar];
     1752            else
     1753                $this_sidebar = array();
     1754
     1755            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
     1758                // data since widget ids aren't necessarily persistent across multiple
     1759                // updates
     1760                if ( $this->_get_display_callback() == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
     1761                    $number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
     1762                    if( !in_array( $this->id_base . '-' . $number, (array)$_POST['widget-id'] ) ) {
     1763                        // the widget has been removed.
     1764                        unset($all_instances[$number]);
     1765                    }
     1766                }
     1767            }
     1768
     1769            foreach( (array) $_POST['widget-' . $this->id_base] as $number => $new_instance ) {
     1770                $this->_set($number);
     1771                if ( isset($all_instances[$number]) )
     1772                    $instance = $this->update($new_instance, $all_instances[$number]);
     1773                else
     1774                    $instance = $this->update($new_instance, array());
     1775
     1776                if ( !empty($instance) )    {
     1777                    $instance['_multiwidget'] = $number;
     1778                    $all_instances[$number] = $instance;
     1779                }
     1780            }
     1781
     1782            update_option($this->option_name, $all_instances);
     1783            $this->updated = true; // So that we don't go through this more than once
     1784        }
     1785
     1786        // Here we echo out the form
     1787        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
     1790            $this->_set('%i%');
     1791            $instance = array();
     1792        } else {
     1793            $this->_set($widget_args['number']);
     1794            $instance = $all_instances[ $widget_args['number'] ];
     1795        }
     1796        $this->form($instance);
     1797    }
     1798
     1799    /** Helper function: Registers a single instance. */
     1800    function _register_one($number = -1) {
     1801        wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
     1802        wp_register_widget_control( $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) );
     1803    }
     1804
     1805}
     1806
    15931807?>
  • trunk/wp-includes/widgets.php

    r10742 r10764  
    740740
    741741/**
    742  * Display links widget.
    743  *
    744  * @since 2.2.0
    745  *
    746  * @param array $args Widget arguments.
    747  */
    748 function wp_widget_links($args) {
    749     extract($args, EXTR_SKIP);
    750 
    751     $link_options = get_option('widget_links');
    752     $show_description = isset($link_options['description']) ? $link_options['description'] : false;
    753     $show_name = isset($link_options['name']) ? $link_options['name'] : false;
    754     $show_rating = isset($link_options['rating']) ? $link_options['rating'] : false;
    755     $show_images = isset($link_options['images']) ? $link_options['images'] : true;
    756 
    757     $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
    758     wp_list_bookmarks(apply_filters('widget_links_args', array(
    759         'title_before' => $before_title, 'title_after' => $after_title,
    760         'category_before' => $before_widget, 'category_after' => $after_widget,
    761         'show_images' => $show_images, 'show_description' => $show_description,
    762         'show_name' => $show_name, 'show_rating' => $show_rating,
    763         'class' => 'linkcat widget'
    764     )));
    765 }
    766 
    767 /**
    768  * Display and process links widget options form.
     742 * Links widget class
    769743 *
    770744 * @since 2.8.0
    771745 */
    772 function wp_widget_links_control() {
    773     $options = $newoptions = get_option('widget_links');
    774 
    775     //Defaults
    776     if ( ! $newoptions )
    777         $newoptions = array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false);
    778 
    779     if ( isset($_POST['links-submit']) ) {
    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 ( $options != $newoptions ) {
    786         $options = $newoptions;
    787         update_option('widget_links', $options);
    788     }
    789 ?>
    790             <p>
    791                 <label for="links-images"><input class="checkbox" type="checkbox" <?php checked($options['images'], true) ?> id="links-images" name="links-images" /> <?php _e('Show Link Image'); ?></label>
    792                 <br />
    793                 <label for="links-name"><input class="checkbox" type="checkbox" <?php checked($options['name'], true) ?> id="links-name" name="links-name" /> <?php _e('Show Link Name'); ?></label>
    794                 <br />
    795                 <label for="links-description"><input class="checkbox" type="checkbox" <?php checked($options['description'], true) ?> id="links-description" name="links-description" /> <?php _e('Show Link Description'); ?></label>
    796                 <br />
    797                 <label for="links-rating"><input class="checkbox" type="checkbox" <?php checked($options['rating'], true) ?> id="links-rating" name="links-rating" /> <?php _e('Show Link Rating'); ?></label>
    798             </p>
    799             <input type="hidden" id="links-submit" name="links-submit" value="1" />
    800 <?php
    801 }
    802 
     746class WP_Widget_Links extends WP_Widgets {
     747   
     748    function widget( $args, $instance ) {
     749        extract($args, EXTR_SKIP);
     750
     751        $show_description = isset($instance['description']) ? $instance['description'] : false;
     752        $show_name = isset($instance['name']) ? $instance['name'] : false;
     753        $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
     754        $show_images = isset($instance['images']) ? $instance['images'] : true;
     755   
     756        $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
     757        wp_list_bookmarks(apply_filters('widget_links_args', array(
     758            'title_before' => $before_title, 'title_after' => $after_title,
     759            'category_before' => $before_widget, 'category_after' => $after_widget,
     760            'show_images' => $show_images, 'show_description' => $show_description,
     761            'show_name' => $show_name, 'show_rating' => $show_rating,
     762            'class' => 'linkcat widget'
     763        )));
     764    }
     765   
     766    function update( $new_instance, $old_instance ) {
     767        if( !isset($new_instance['submit']) ) // user clicked cancel?
     768            return false;
     769
     770        return $new_instance;
     771    }
     772   
     773    function form( $instance ) {
     774
     775        //Defaults
     776        $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*/
     791?>
     792        <p>
     793        <label for="<?php echo $this->get_field_id('images'); ?>">
     794        <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" /> <?php _e('Show Link Image'); ?></label><br />
     795        <label for="<?php echo $this->get_field_id('name'); ?>">
     796        <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" /> <?php _e('Show Link Name'); ?></label><br />
     797        <label for="<?php echo $this->get_field_id('description'); ?>">
     798        <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" /> <?php _e('Show Link Description'); ?></label><br />
     799        <label for="<?php echo $this->get_field_id('rating'); ?>">
     800        <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" /> <?php _e('Show Link Rating'); ?></label>
     801        <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" />
     802        </p>
     803<?php
     804    }
     805}
    803806
    804807/**
     
    19711974    wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control' );
    19721975
    1973     $widget_ops = array('classname' => 'widget_links', 'description' => __( "Your blogroll") );
    1974     wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $widget_ops);
    1975     wp_register_widget_control('links', __('Links'), 'wp_widget_links_control' );
     1976    $widget_ops = array('description' => __( "Your blogroll" ) );
     1977    $wp_widget_links = new WP_Widget_Links('links', __('Links'), $widget_ops);
     1978    $wp_widget_links->register();
    19761979
    19771980    $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.