WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/11/2009 02:37:24 PM (11 years ago)
Author:
azaozz
Message:

Widgets page, first run, see #9511

File:
1 edited

Legend:

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

    r10680 r10912  
    88
    99/**
    10  * Display list of widgets, either all or matching search.
     10 * Display list of the available widgets, either all or matching search.
    1111 *
    1212 * The search parameter are search terms separated by spaces.
     
    1919function wp_list_widgets( $show = 'all', $_search = false ) {
    2020    global $wp_registered_widgets, $sidebars_widgets, $wp_registered_widget_controls;
    21     if ( $_search ) {
     21
     22    if ( $_search ) {
    2223        // sanitize
    2324        $search = preg_replace( '/[^\w\s]/', '', $_search );
     
    3233?>
    3334
    34     <ul id='widget-list'>
    35         <?php
     35    <ul id="widget-list">
     36<?php
    3637        $no_widgets_shown = true;
    3738        $already_shown = array();
    38         foreach ( $wp_registered_widgets as $name => $widget ) :
     39        foreach ( $wp_registered_widgets as $name => $widget ) {
    3940            if ( 'all' == $show && in_array( $widget['callback'], $already_shown ) ) // We already showed this multi-widget
    4041                continue;
     
    6465            if ( ! isset( $widget['params'][0] ) )
    6566                $widget['params'][0] = array();
    66             ob_start();
    67             $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template', '_show' => $show ), 1 => $widget['params'][0] ) );
    68             $sidebar_args = call_user_func_array( 'wp_widget_control', $args );
    69             $widget_control_template = ob_get_contents();
    70             ob_end_clean();
    71 
    72             $widget_id = $widget['id']; // save this for later in case we mess with $widget['id']
    73 
    74             $is_multi = false !== strpos( $widget_control_template, '%i%' );
    75             if ( !$sidebar || $is_multi ) {
    76                 $add_query = array(
    77                     'sidebar' => $sidebar,
    78                     'key' => false,
    79                     'edit' => false
    80                 );
    81                 if ( 'all' == $show && $is_multi ) {
    82                     // it's a multi-widget.  We only need to show it in the list once.
    83                     $already_shown[] = $widget['callback'];
    84                     $num = (int) array_pop( $ids = explode( '-', $widget['id'] ) );
    85                     $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
    86                     // so that we always add a new one when clicking "add"
    87                     while ( isset($wp_registered_widgets["$id_base-$num"]) )
    88                         $num++;
    89                     $widget['id'] = "$id_base-$num";
    90                     $add_query['base'] = $id_base;
    91                     $add_query['key'] = $num;
    92                     $add_query['sidebar'] = $GLOBALS['sidebar'];
    93                 }
    94                 $add_query['add'] = $widget['id'];
    95                 $action = 'add';
    96                 $add_url = clean_url( wp_nonce_url( add_query_arg( $add_query ), "add-widget_$widget[id]" ) );
     67
     68            $already_shown[] = $widget['callback'];
     69            $no_widgets_shown = false;
     70
     71            $args = array( 'widget_id' => $widget['id'], 'widget_name' => $widget['name'], '_display' => 'template' );
     72
     73            if ( isset($wp_registered_widget_controls[$widget['id']]['id_base']) && isset($widget['params'][0]['number']) ) {
     74                $id_base = $wp_registered_widget_controls[$widget['id']]['id_base'];
     75                $args['_temp_id'] = "$id_base-__i__";
     76                $args['_multi_num'] = next_widget_id_number($id_base);
     77                $args['_add'] = 'multi';
    9778            } else {
    98                 $action = 'edit';
    99                 $edit_url = clean_url( add_query_arg( array(
    100                     'sidebar' => $sidebar,
    101                     'edit' => $widget['id'],
    102                     'key' => array_search( $widget['id'], $sidebars_widgets[$sidebar] ),
    103                 ) ) );
    104 
    105                 $widget_control_template = '<textarea rows="1" cols="1">' . htmlspecialchars( $widget_control_template ) . '</textarea>';
     79                $args['_add'] = 'single';
     80                if ( $sidebar )
     81                    $args['_hide'] = '1';
    10682            }
    10783
    108             $widget_control_template = $sidebar_args['before_widget'] . $widget_control_template . $sidebar_args['after_widget'];
    109 
    110             $no_widgets_shown = false;
    111 
    112 
    113             if ( 'all' != $show && $sidebar_args['_widget_title'] )
    114                 $widget_title = $sidebar_args['_widget_title'];
    115             else
    116                 $widget_title = $widget['name'];
    117         ?>
    118 
    119         <li id="widget-list-item-<?php echo attribute_escape( $widget['id'] ); ?>" class="widget-list-item">
    120             <h4 class="widget-title widget-draggable">
    121 
    122                 <span><?php echo $widget_title; ?></span>
    123 
    124                 <?php if ( 'add' == $action ) : ?>
    125 
    126                 <a class="widget-action widget-control-add" href="<?php echo $add_url; ?>"><?php _e( 'Add' ); ?></a>
    127 
    128                 <?php elseif ( 'edit' == $action ) :
    129                     // We echo a hidden edit link for the sake of the JS.  Edit links are shown (needlessly?) after a widget is added.
    130                 ?>
    131 
    132                 <a class="widget-action widget-control-edit" href="<?php echo $edit_url; ?>" style="display: none;"><?php _e( 'Edit' ); ?></a>
    133 
    134                 <?php endif; ?>
    135 
    136                 <br class="clear" />
    137 
    138             </h4>
    139 
    140 
    141             <ul id="widget-control-info-<?php echo $widget['id']; ?>" class="widget-control-info">
    142 
    143                 <?php echo $widget_control_template; ?>
    144 
    145             </ul>
    146 
    147             <?php if ( 'add' == $action ) : ?>
    148             <?php endif; ?>
    149 
    150             <div class="widget-description">
    151                 <?php echo ( $widget_description = wp_widget_description( $widget_id ) ) ? $widget_description : '&nbsp;'; ?>
    152             </div>
    153 
    154             <br class="clear" />
    155 
    156         </li>
    157 
    158         <?php endforeach; if ( $no_widgets_shown ) : ?>
    159 
    160         <li><?php _e( 'No matching widgets' ); ?></li>
    161 
    162         <?php endif; ?>
    163 
     84            $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
     85            call_user_func_array( 'wp_widget_control', $args );
     86        }
     87
     88        if ( $no_widgets_shown )
     89          echo '<li>' . __( 'No matching widgets' ) . "</li>\n";
     90?>
    16491    </ul>
    16592<?php
     
    173100 * @param string $sidebar
    174101 */
    175 function wp_list_widget_controls( $sidebar ) {
     102function wp_list_widget_controls( $sidebar, $hide = false ) {
    176103    add_filter( 'dynamic_sidebar_params', 'wp_list_widget_controls_dynamic_sidebar' );
    177104?>
    178105
    179     <ul class="widget-control-list">
    180 
    181         <?php if ( !dynamic_sidebar( $sidebar ) ) echo "<li />"; ?>
    182 
     106    <ul class="widgets-sortables<?php echo $hide ? ' hide-if-js' : ''; ?>">
     107    <?php dynamic_sidebar( $sidebar ); ?>
    183108    </ul>
    184109
     
    200125
    201126    $widget_id = $params[0]['widget_id'];
    202 
    203     $params[0]['before_widget'] = "<li id='widget-list-control-item-$i-$widget_id' class='widget-list-control-item widget-sortable'>\n";
     127    $id = isset($params[0]['_temp_id']) ? $params[0]['_temp_id'] : $widget_id;
     128    $hidden = isset($params[0]['_hide']) ? ' style="display:none;"' : '';
     129
     130    $params[0]['before_widget'] = "<li id='widget-${i}_$id' class='widget'$hidden>";
    204131    $params[0]['after_widget'] = "</li>";
    205     $params[0]['before_title'] = "%BEG_OF_TITLE%";
    206     $params[0]['after_title'] = "%END_OF_TITLE%";
     132    $params[0]['before_title'] = "%BEG_OF_TITLE%"; // deprecated
     133    $params[0]['after_title'] = "%END_OF_TITLE%"; // deprecated
    207134    if ( is_callable( $wp_registered_widgets[$widget_id]['callback'] ) ) {
    208         $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
     135        $wp_registered_widgets[$widget_id]['_callback'] = $wp_registered_widgets[$widget_id]['callback'];
    209136        $wp_registered_widgets[$widget_id]['callback'] = 'wp_widget_control';
    210137    }
     138
    211139    return $params;
     140}
     141
     142function next_widget_id_number($id_base) {
     143    global $wp_registered_widgets;
     144    $number = 2;
     145
     146    while ( isset($wp_registered_widgets["$id_base-$number"]) )
     147        $number++;
     148
     149    return $number;
    212150}
    213151
     
    223161 */
    224162function wp_widget_control( $sidebar_args ) {
    225     global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets, $edit_widget;
     163    global $wp_registered_widgets, $wp_registered_widget_controls, $sidebars_widgets;
     164
    226165    $widget_id = $sidebar_args['widget_id'];
    227166    $sidebar_id = isset($sidebar_args['id']) ? $sidebar_args['id'] : false;
    228167
    229     $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : 0;
     168    $control = isset($wp_registered_widget_controls[$widget_id]) ? $wp_registered_widget_controls[$widget_id] : array();
    230169    $widget  = $wp_registered_widgets[$widget_id];
    231170
    232171    $key = $sidebar_id ? array_search( $widget_id, $sidebars_widgets[$sidebar_id] ) : 'no-key'; // position of widget in sidebar
    233172
    234     $edit = -1 <  $edit_widget && is_numeric($key) && $edit_widget === $key; // (bool) are we currently editing this widget
     173    $edit = isset($_GET['editwidget']); // editing without js
     174    $nonce = $edit ? wp_create_nonce("edit-widget_$widget_id") : '';
    235175
    236176    $id_format = $widget['id'];
    237177
    238     if ( ! isset( $sidebar_args['_show'] ) )
    239         $sidebar_args['_show'] = '';
    240 
    241     if ( ! isset( $sidebar_args['_display'] ) )
    242         $sidebar_args['_display'] = '';
     178    $widget_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
     179    $add_new = $sidebar_args['_add'];
     180    $id_base = isset($control['id_base']) ? $control['id_base'] : $widget_id;
    243181
    244182    // We aren't showing a widget control, we're outputing a template for a mult-widget control
    245     if ( 'all' == $sidebar_args['_show'] && 'template' == $sidebar_args['_display'] && isset($control['params'][0]['number']) ) {
    246         // number == -1 implies a template where id numbers are replaced by a generic '%i%'
     183    if ( isset($sidebar_args['_display']) && 'template' == $sidebar_args['_display'] && $widget_number ) {
     184        // number == -1 implies a template where id numbers are replaced by a generic '__i__'
    247185        $control['params'][0]['number'] = -1;
    248186        // if given, id_base means widget id's should be constructed like {$id_base}-{$id_number}
    249187        if ( isset($control['id_base']) )
    250             $id_format = $control['id_base'] . '-%i%';
     188            $id_format = $control['id_base'] . '-__i__';
     189
     190        $multi_number = $sidebar_args['_multi_num'];
    251191    }
    252192
    253     $widget_title = '';
    254     // We grab the normal widget output to find the widget's title
    255     if ( ( 'all' != $sidebar_args['_show'] || 'template' != $sidebar_args['_display'] ) && is_callable( $widget['_callback'] ) ) {
    256         ob_start();
    257         $args = func_get_args();
    258         call_user_func_array( $widget['_callback'], $args );
    259         $widget_title = ob_get_clean();
    260         $widget_title = wp_widget_control_ob_filter( $widget_title );
    261     }
    262193    $wp_registered_widgets[$widget_id]['callback'] = $wp_registered_widgets[$widget_id]['_callback'];
    263194    unset($wp_registered_widgets[$widget_id]['_callback']);
    264195
    265     if ( $widget_title && $widget_title != $sidebar_args['widget_name'] )
    266         /* translators: 1: widget name, 2: widget title */
    267         $widget_title = sprintf( _x('%1$s: %2$s', 'widget' ), $sidebar_args['widget_name'], $widget_title );
    268     else
    269         $widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
    270 
    271     $sidebar_args['_widget_title'] = $widget_title;
    272 
    273     if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
    274         echo $sidebar_args['before_widget'];
    275 ?>
     196    $widget_title = wp_specialchars( strip_tags( $sidebar_args['widget_name'] ) );
     197
     198    echo $sidebar_args['before_widget']; ?>
    276199        <div class="widget-top">
    277         <h4 class="widget-title"><span><?php echo $widget_title ?></span>
    278 
    279             <?php if ( $edit ) : ?>
    280 
    281             <a class="widget-action widget-control-edit" href="<?php echo clean_url( remove_query_arg( array( 'edit', 'key' ) ) ); ?>"><?php _e('Cancel'); ?></a>
    282 
    283             <?php else : ?>
    284 
    285             <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>
    286 
    287             <?php endif; ?>
    288 
    289             <br class="clear" />
    290 
    291         </h4></div>
    292 
    293         <div class="widget-control"<?php if ( $edit ) echo ' style="display: block;"'; ?>>
    294 
    295             <?php
    296             if ( $control )
     200        <div class="widget-title-action">
     201<?php       if ( !$edit ) { ?>
     202            <a class="widget-action widget-control-edit" href="<?php echo clean_url( add_query_arg( array( 'edit' => $id_format, 'key' => $key ) ) ); ?>"></a>
     203<?php       } ?>
     204        </div>
     205        <div class="widget-title"><h4><?php echo $widget_title ?></h4></div>
     206        </div>
     207
     208        <div class="widget-inside">
     209        <form action="" method="post">
     210        <div class="widget-control">
     211<?php
     212            if ( isset($control['callback']) )
    297213                call_user_func_array( $control['callback'], $control['params'] );
    298214            else
    299                 echo '<p>' . __('There are no options for this widget.') . '</p>';
    300             ?>
    301 
    302             <input type="hidden" name="widget-id[]" value="<?php echo $id_format; ?>" />
    303             <input type="hidden" class="widget-width" value="<?php echo $control['width']; ?>" />
     215                echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
     216
     217            <input type="hidden" name="widget-id" class="widget-id" value="<?php echo $id_format; ?>" />
     218            <input type="hidden" name="id_base" class="id_base" value="<?php echo $id_base; ?>" />
     219            <input type="hidden" name="widget-width" value="<?php echo $control['width']; ?>" />
     220            <input type="hidden" name="widget-height" value="<?php echo $control['height']; ?>" />
     221            <input type="hidden" name="widget_number" class="widget_number" value="<?php echo $widget_number; ?>" />
     222            <input type="hidden" name="add_new" class="add_new" value="<?php echo $add_new; ?>" />
     223<?php
     224            if ( isset($multi_number) )
     225                echo "\t\t\t<input type='hidden' name='multi_number' class='multi_number' value='$multi_number' />\n"; ?>
    304226
    305227            <div class="widget-control-actions">
    306 
    307                 <?php if ( $control ) : ?>
    308 
    309                 <a class="button widget-action widget-control-save hide-if-no-js edit alignleft" href="#save:<?php echo $id_format; ?>"><?php _e('Done'); ?></a>
    310 
    311                 <?php endif; ?>
    312 
    313                 <a class="button widget-action widget-control-remove alignright" href="<?php echo clean_url( wp_nonce_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key ) ), "remove-widget_$widget[id]" ) ); ?>"><?php _e('Remove'); ?></a>
     228                <a class="button widget-control-remove alignleft" href="<?php echo $edit ? clean_url( add_query_arg( array( 'remove' => $id_format, 'key' => $key, '_wpnonce' => $nonce ) ) ) : '#remove'; ?>"><?php _e('Remove'); ?></a>
     229<?php       if ( !empty($control) ) { ?>
     230                <a class="button-primary widget-control-save alignright" href="<?php echo $edit ? clean_url( add_query_arg( array( 'save' => $id_format, 'key' => $key, '_wpnonce' => $nonce ) ) ) : '#save'; ?>"><?php _e('Save'); ?></a>
     231<?php       } ?>
    314232                <br class="clear" />
    315233            </div>
    316234        </div>
    317 <?php
    318     if ( empty($sidebar_args['_display']) || 'template' != $sidebar_args['_display'] )
    319         echo $sidebar_args['after_widget'];
     235        </form>
     236        <div class="widget-description">
     237<?php echo ( $widget_description = wp_widget_description($widget_id) ) ? "$widget_description\n" : "$widget_title\n"; ?>
     238        </div>
     239        </div>
     240<?php
     241    echo $sidebar_args['after_widget'];
     242
    320243    return $sidebar_args;
    321244}
    322245
    323 /**
    324  * {@internal Missing Short Description}}
    325  *
    326  * @since unknown
    327  *
    328  * @param string $string
    329  * @return string
    330  */
    331 function wp_widget_control_ob_filter( $string ) {
    332     if ( false === $beg = strpos( $string, '%BEG_OF_TITLE%' ) )
    333         return '';
    334     if ( false === $end = strpos( $string, '%END_OF_TITLE%' ) )
    335         return '';
    336     $string = substr( $string, $beg + 14 , $end - $beg - 14);
    337     $string = str_replace( '&nbsp;', ' ', $string );
    338     return trim( wp_specialchars( strip_tags( $string ) ) );
    339 }
    340 
    341 ?>
Note: See TracChangeset for help on using the changeset viewer.