WordPress.org

Make WordPress Core

Changeset 7219


Ignore:
Timestamp:
03/10/2008 11:23:32 PM (10 years ago)
Author:
ryan
Message:

Make titles for multiple instances of a widget distinct. Props mdawaffe. fixes #6108

Location:
trunk/wp-admin
Files:
2 edited

Legend:

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

    r7197 r7219  
    4848
    4949            ob_start();
    50                 wp_widget_control( 'no-key', $widget['id'], 'template' );
     50                $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' ) ) );
     51                call_user_func_array( 'wp_widget_control', $args );
    5152            $widget_control_template = ob_get_contents();
    5253            ob_end_clean();
     
    123124}
    124125
    125 function wp_list_widget_controls( $widgets, $edit_widget = -1 ) {
    126 ?>
    127 
    128         <ul class="widget-control-list">
    129             <li />
    130 <?php
    131     foreach ( $widgets as $key => $widget )
    132         wp_widget_control( $key, $widget, $key == $edit_widget ? 'edit' : 'display' );
    133 ?>
    134 
    135         </ul>
    136 
    137 <?php
    138 }
    139 
     126
     127
     128function wp_list_widget_controls( $sidebar ) {
     129    add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
     130?>
     131
     132    <ul class="widget-control-list">
     133
     134        <?php if ( !dynamic_sidebar( $sidebar ) ) echo "<li />"; ?>
     135
     136    </ul>
     137
     138<?php
     139}
     140
     141
     142function wp_list_widget_controls_dynamic_sidebar( $params ) {
     143    global $wp_registered_widgets;
     144    static $i = 0;
     145    $i++;
     146
     147    $widget_id = $params[0]['widget_id'];
     148
     149    $params[0]['before_widget'] = "<li id='widget-list-control-item-$i-$widget_id' class='widget-list-control-item widget-sortable'>\n";
     150    $params[0]['after_widget'] = "</li>";
     151    $params[0]['before_title'] = "%BEG_OF_TITLE%";
     152    $params[0]['after_title'] = "%END_OF_TITLE%";
     153    $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
     154    $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
     155    return $params;
     156}
    140157
    141158/*
    142  * Displays the control form for widget of type $widget at position $key.
    143  * $display
    144  *  == 'display': Normal, "closed" form.
    145  *  == 'edit': "open" form
    146  *  == 'template': generates a form template to be used by JS
     159 * Meta widget used to display the control form for a widget.  Called from dynamic_sidebar()
    147160 */
    148 function wp_widget_control( $key, $widget, $display = 'display' ) {
    149     static $i = 0;
    150     global $wp_registered_widgets, $wp_registered_widget_controls;
    151     $control = $wp_registered_widget_controls[$widget];
    152     $widget  = $wp_registered_widgets[$widget];
     161function wp_widget_control( $sidebar_args ) {
     162    global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets, $edit_widget;
     163    $widget_id = $sidebar_args['widget_id'];
     164    $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
     165
     166    $control = $wp_registered_widget_controls[$widget_id];
     167    $widget  = $wp_registered_widgets[$widget_id];
     168
     169    $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : 'no-key'; // position of widget in sidebar
     170
     171    $edit = $edit_widget > 0 && $key && $edit_widget == $key; // (bool) are we currently editing this widget
    153172
    154173    $id_format = $widget['id'];
    155     if ( 'template' == $display && isset($control['params'][0]['number']) ) {
     174    // We aren't showing a widget control, we're outputing a template for a mult-widget control
     175    if ( 'template' == $sidebar_args['_display'] && isset($control['params'][0]['number']) ) {
    156176        // number == -1 implies a template where id numbers are replaced by a generic '%i%'
    157177        $control['params'][0]['number'] = -1;
     
    160180            $id_format = $control['id_base'] . '-%i%';
    161181    }
    162 ?>
    163 
    164         <li id="widget-list-control-item-<?php echo ++$i; ?>-<?php echo $widget['id']; ?>" class="widget-list-control-item widget-sortable">
    165             <h4 class="widget-title">
    166 
    167                 <?php echo $widget['name']; // TODO: Up/Down links for noJS reordering? ?>
    168 
    169                 <?php if ( 'edit' == $display ) : ?>
    170 
    171                 <a class="widget-action widget-control-edit" href="<?php echo remove_query_arg( array( 'edit', 'key' ) ); ?>"><?php _e('Cancel'); ?></a>
    172 
    173                 <?php else : ?>
    174 
    175                 <a class="widget-action widget-control-edit" href="<?php echo clean_url( add_query_arg( array( 'edit' => $id_format, 'key' => $key ) ) ); ?>"><?php _e('Edit'); ?></a>
     182
     183    $widget_title = '';
     184    // We grab the normal widget output to find the widget's title
     185    if ( is_callable( $widget['_callback'] ) ) {
     186        ob_start();
     187        $args = func_get_args();
     188        call_user_func_array( $widget['_callback'], $args );
     189        $widget_title = ob_get_clean();
     190        $widget_title = wp_widget_control_ob_filter( $widget_title );
     191    }
     192    $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
     193    unset($wp_registered_widgets[$widget_id]['_callback']);
     194
     195    if ( $widget_title )
     196        $widget_title = sprintf( _c('%1$s: %2$s|widget_admin_title' ), $sidebar_args['widget_name'], $widget_title );
     197    else
     198        $widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
     199
     200
     201    echo $sidebar_args['before_widget'];
     202?>
     203        <h4 class="widget-title"><?php echo $widget_title ?>
     204
     205            <?php if ( $edit ) : ?>
     206
     207            <a class="widget-action widget-control-edit" href="<?php echo remove_query_arg( array( 'edit', 'key' ) ); ?>"><?php _e('Cancel'); ?></a>
     208
     209            <?php else : ?>
     210
     211            <a class="widget-action widget-control-edit" href="<?php echo clean_url( add_query_arg( array( 'edit' => $id_format, 'key' => $key ) ) ); ?>"><?php _e('Edit'); ?></a>
     212
     213            <?php endif; ?>
     214
     215        </h4>
     216
     217        <div class="widget-control"<?php if ( $edit ) echo ' style="display: block;"'; ?>>
     218
     219            <?php
     220            if ( $control )
     221                call_user_func_array( $control['callback'], $control['params'] );
     222            else
     223                echo '<p>' . __('There are no options for this widget.') . '</p>';
     224            ?>
     225
     226            <input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
     227            <input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
     228
     229            <div class="widget-control-actions">
     230
     231                <?php if ( $control ) : ?>
     232
     233                <a class="widget-action widget-control-save wp-no-js-hidden edit alignleft" href="#save:<?php echo $id_format; ?>"><?php _e('Change'); ?></a>
    176234
    177235                <?php endif; ?>
    178236
    179             </h4>
    180 
    181             <div class="widget-control"<?php if ( 'edit' == $display ) echo ' style="display: block;"'; ?>>
    182 
    183                 <?php
    184                 if ( $control )
    185                     call_user_func_array( $control['callback'], $control['params'] );
    186                 else
    187                     echo '<p>' . __('There are no options for this widget.') . '</p>';
    188                 ?>
    189 
    190                 <input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
    191                 <input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
    192 
    193                 <div class="widget-control-actions">
    194 
    195                     <?php if ( $control && 'edit' != $display ) : ?>
    196 
    197                     <a class="widget-action widget-control-save edit alignleft" href="#save:<?php echo $id_format; ?>"><?php _e('Change'); ?></a>
    198 
    199                     <?php endif; ?>
    200 
    201                     <a class="widget-action widget-control-remove delete alignright" href="<?php echo clean_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key ), wp_nonce_url( null, "remove-widget_$widget[id]" ) ) ); ?>"><?php _e('Remove'); ?></a>
    202                     <br class="clear" />
    203                 </div>
     237                <a class="widget-action widget-control-remove delete alignright" href="<?php echo clean_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key ), wp_nonce_url( null, "remove-widget_$widget[id]" ) ) ); ?>"><?php _e('Remove'); ?></a>
     238                <br class="clear" />
    204239            </div>
    205         </li>
    206 
    207 <?php
     240        </div>
     241<?php
     242    echo $sidebar_args['after_widget'];
     243}
     244
     245function wp_widget_control_ob_filter( $string ) {
     246    if ( false === $beg = strpos( $string, '%BEG_OF_TITLE%' ) )
     247        return '';
     248    if ( false === $end = strpos( $string, '%END_OF_TITLE%' ) )
     249        return '';
     250    $string = substr( $string, $beg + 14 , $end - $beg - 14);
     251    return wp_specialchars( strip_tags( $string ) );
    208252}
    209253
  • trunk/wp-admin/widgets.php

    r7208 r7219  
    246246        <div id="current-sidebar">
    247247
    248             <?php wp_list_widget_controls( $sidebars_widgets[$sidebar], $edit_widget ); // Show the control forms for each of the widgets in this sidebar ?>
     248            <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
    249249
    250250        </div>
Note: See TracChangeset for help on using the changeset viewer.