WordPress.org

Make WordPress Core

Changeset 10798


Ignore:
Timestamp:
03/17/2009 12:59:42 AM (11 years ago)
Author:
ryan
Message:

Separate widget form and update callbacks.

Location:
trunk
Files:
3 edited

Legend:

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

    r10631 r10798  
    7474        $already_done = array();
    7575
    76         foreach ( $wp_registered_widget_controls as $name => $control ) {
     76        foreach ( $wp_registered_widget_updates as $name => $control ) {
    7777            if ( in_array( $control['callback'], $already_done ) )
    7878                continue;
  • trunk/wp-includes/default-widgets.php

    r10797 r10798  
    217217
    218218        $instance = $old_instance;
     219        $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
    219220        $instance['title'] = strip_tags($new_instance['title']);
    220221        $instance['count'] = $new_instance['count'] ? 1 : 0;
  • trunk/wp-includes/widgets.php

    r10795 r10798  
    1717
    1818/** @ignore */
    19 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
     19global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates;
    2020
    2121/**
     
    4242 */
    4343$wp_registered_widget_controls = array();
     44$wp_registered_widget_updates = array();
    4445
    4546/**
     
    153154    }
    154155
    155     function _get_control_callback() {
    156         return array(&$this, 'control_callback');
     156    function _get_update_callback() {
     157        return array(&$this, 'update_callback');
     158    }
     159
     160    function _get_form_callback() {
     161        return array(&$this, 'form_callback');
    157162    }
    158163
     
    172177    }
    173178
    174     /** Deal with changed settings and generate the control form.
     179    /** Deal with changed settings.
    175180     *  Do NOT over-ride this function. */
    176     function control_callback( $widget_args = 1 ) {
     181    function update_callback( $widget_args = 1 ) {
    177182        global $wp_registered_widgets;
    178183
     
    224229            $this->updated = true;
    225230        }
    226 
    227         // Here we echo out the form
     231    }
     232
     233    /** Generate the control form.
     234     *  Do NOT over-ride this function. */
     235    function form_callback( $widget_args = 1 ) {
     236        if ( is_numeric($widget_args) )
     237            $widget_args = array( 'number' => $widget_args );
     238
     239        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
     240        $all_instances = $this->get_settings();
     241
    228242        if ( -1 == $widget_args['number'] ) {
    229243            // We echo out a form where 'number' can be set later via JS
     
    241255    function _register_one($number = -1) {
    242256        wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
    243         wp_register_widget_control( $this->id, $this->name, $this->_get_control_callback(), $this->control_options, array( 'number' => $number ) );
     257        _register_widget_update_callback(   $this->id, $this->name, $this->_get_update_callback(), $this->control_options, array( 'number' => $number ) );
     258        _register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
    244259    }
    245260   
     
    490505 */
    491506function wp_register_widget_control($id, $name, $control_callback, $options = array()) {
    492     global $wp_registered_widget_controls;
     507    global $wp_registered_widget_controls, $wp_registered_widget_updates;
    493508
    494509    $id = strtolower($id);
     
    496511    if ( empty($control_callback) ) {
    497512        unset($wp_registered_widget_controls[$id]);
     513        unset($wp_registered_widget_updates[$id]);
    498514        return;
    499515    }
     
    511527        'id' => $id,
    512528        'callback' => $control_callback,
     529        'params' => array_slice(func_get_args(), 4)
     530    );
     531    $widget = array_merge($widget, $options);
     532
     533    $wp_registered_widget_controls[$id] = $wp_registered_widget_updates[$id] = $widget;
     534}
     535
     536function _register_widget_update_callback($id, $name, $update_callback, $options = array()) {
     537    global $wp_registered_widget_updates;
     538
     539    $id = strtolower($id);
     540
     541    if ( empty($update_callback) ) {
     542        unset($wp_registered_widget_updates[$id]);
     543        return;
     544    }
     545
     546    if ( isset($wp_registered_widget_updates[$id]) && !did_action( 'widgets_init' ) )
     547        return;
     548
     549    $defaults = array('width' => 250, 'height' => 200 ); // height is never used
     550    $options = wp_parse_args($options, $defaults);
     551    $options['width'] = (int) $options['width'];
     552    $options['height'] = (int) $options['height'];
     553
     554    $widget = array(
     555        'name' => $name,
     556        'id' => $id,
     557        'callback' => $update_callback,
     558        'params' => array_slice(func_get_args(), 4)
     559    );
     560    $widget = array_merge($widget, $options);
     561
     562    $wp_registered_widget_updates[$id] = $widget;
     563}
     564
     565function _register_widget_form_callback($id, $name, $form_callback, $options = array()) {
     566    global $wp_registered_widget_controls;
     567
     568    $id = strtolower($id);
     569
     570    if ( empty($form_callback) ) {
     571        unset($wp_registered_widget_controls[$id]);
     572        return;
     573    }
     574
     575    if ( isset($wp_registered_widget_controls[$id]) && !did_action( 'widgets_init' ) )
     576        return;
     577
     578    $defaults = array('width' => 250, 'height' => 200 ); // height is never used
     579    $options = wp_parse_args($options, $defaults);
     580    $options['width'] = (int) $options['width'];
     581    $options['height'] = (int) $options['height'];
     582
     583    $widget = array(
     584        'name' => $name,
     585        'id' => $id,
     586        'callback' => $form_callback,
    513587        'params' => array_slice(func_get_args(), 4)
    514588    );
Note: See TracChangeset for help on using the changeset viewer.