Make WordPress Core


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

Widgets page, first run, see #9511

File:
1 edited

Legend:

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

    r10798 r10912  
    1616    wp_die( __( 'Cheatin’ uh?' ));
    1717
    18 wp_enqueue_script( array( 'wp-lists', 'admin-widgets' ) );
     18wp_enqueue_script('admin-widgets');
    1919wp_admin_css( 'widgets' );
    2020
     
    2424$parent_file = 'themes.php';
    2525
    26 // $sidebar = What sidebar are we editing?
    27 if ( isset($_GET['sidebar']) && isset($wp_registered_sidebars[$_GET['sidebar']]) ) {
    28     $sidebar = attribute_escape( $_GET['sidebar'] );
    29 } elseif ( is_array($wp_registered_sidebars) && !empty($wp_registered_sidebars) ) {
    30     // By default we look at the first defined sidebar
    31     $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
    32 } else {
    33     // If no sidebars, die.
     26// register the inactive_widgets area as sidebar
     27register_sidebar(array(
     28    'name' => '',
     29    'id' => 'wp_inactive_widgets',
     30    'before_widget' => '',
     31    'after_widget' => '',
     32    'before_title' => '',
     33    'after_title' => '',
     34));
     35
     36// These are the widgets grouped by sidebar
     37$sidebars_widgets = wp_get_sidebars_widgets();
     38if ( empty( $sidebars_widgets ) )
     39    $sidebars_widgets = wp_get_widget_defaults();
     40
     41// look for "lost" widgets, perhaps run only after changin themes
     42function retrieve_widgets() {
     43    global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets;
     44
     45    $key_sidebars = array_keys( (array) $wp_registered_sidebars);
     46    $key_widgets = array_keys($sidebars_widgets);
     47    if ( count($key_widgets) > count($key_sidebars) ) {
     48        $changed_sidebars = array_diff( $key_widgets, $key_sidebars );
     49        foreach ( $changed_sidebars as $lost ) {
     50            if ( is_array($sidebars_widgets[$lost]) )
     51                $sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $sidebars_widgets['wp_inactive_widgets'], $sidebars_widgets[$lost] );
     52            unset($sidebars_widgets[$lost]);
     53        }
     54    }
     55
     56    $shown_widgets = array();
     57    foreach ( $sidebars_widgets as $sidebar ) {
     58        if ( is_array($sidebar) )
     59            $shown_widgets = array_merge($shown_widgets, $sidebar);
     60    }
     61
     62    $all_widgets = array();
     63    foreach ( $wp_registered_widget_updates as $key => $val ) {
     64        if ( isset($val['id_base']) )
     65            $all_widgets[] = $val['id_base'];
     66        else
     67            $all_widgets[] = $key;
     68    }
     69
     70    $all_widgets = array_unique($all_widgets);
     71
     72    $lost_widgets = array();
     73    foreach ( $all_widgets as $name ) {
     74        $data = get_option( str_replace('-', '_', "widget_$name") );
     75        if ( is_array($data) ) {
     76            foreach ( $data as $num => $value ) {
     77                if ( !is_numeric($num) ) // skip single widgets, some don't delete their settings
     78                    continue;
     79                if ( is_array($value) && !in_array("$name-$num", $shown_widgets, true) )
     80                    $lost_widgets[] = "$name-$num";
     81            }
     82        }
     83    }
     84
     85    $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
     86    wp_set_sidebars_widgets($sidebars_widgets);
     87}
     88retrieve_widgets();
     89
     90if ( count($wp_registered_sidebars) == 1 ) {
     91    // If only the "wp_inactive_widgets" is defined the theme has no sidebars, die.
    3492    require_once( 'admin-header.php' );
    3593?>
     
    49107}
    50108
    51 // These are the widgets grouped by sidebar
    52 $sidebars_widgets = wp_get_sidebars_widgets();
    53 if ( empty( $sidebars_widgets ) )
    54     $sidebars_widgets = wp_get_widget_defaults();
    55 
    56 // for the sake of PHP warnings
    57 if ( empty( $sidebars_widgets[$sidebar] ) )
    58     $sidebars_widgets[$sidebar] = array();
    59 
    60 $http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
    61 
    62 // We're updating a sidebar
    63 if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
    64     check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
    65 
    66     /* Hack #1
    67      * The widget_control is overloaded.  It updates the widget's options AND echoes out the widget's HTML form.
    68      * Since we want to update before sending out any headers, we have to catch it with an output buffer,
    69      */
    70     ob_start();
    71         /* There can be multiple widgets of the same type, but the widget_control for that
    72          * widget type needs only be called once if it's a multi-widget.
    73          */
    74         $already_done = array();
    75 
    76         foreach ( $wp_registered_widget_updates as $name => $control ) {
    77             if ( in_array( $control['callback'], $already_done ) )
    78                 continue;
    79 
    80             if ( is_callable( $control['callback'] ) ) {
    81                 call_user_func_array( $control['callback'], $control['params'] );
    82                 $control_output = ob_get_contents();
    83                 if ( false !== strpos( $control_output, '%i%' ) ) // if it's a multi-widget, only call control function once.
    84                     $already_done[] = $control['callback'];
    85             }
    86 
    87             ob_clean();
    88         }
    89     ob_end_clean();
    90 
    91     // Prophylactic.  Take out empty ids.
    92     if ( isset($_POST['widget-id']) ) {
    93         foreach ( (array) $_POST['widget-id'] as $key => $val ) {
    94             if ( !$val )
    95                 unset($_POST['widget-id'][$key]);
    96         }
    97     }
    98 
    99     // Reset the key numbering and store
    100     $new_sidebar = isset( $_POST['widget-id'] ) && is_array( $_POST['widget-id'] ) ? array_values( $_POST['widget-id'] ) : array();
    101     $sidebars_widgets[$_POST['sidebar']] = $new_sidebar;
    102     wp_set_sidebars_widgets( $sidebars_widgets );
    103 
    104     wp_redirect( add_query_arg( 'message', 'updated' ) );
    105     exit;
    106 }
    107 
    108 // What widget (if any) are we editing
    109 $edit_widget = -1;
    110 
    111 $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
    112 
    113 if ( isset($_GET['add']) && $_GET['add'] ) {
    114     // Add to the end of the sidebar
    115     $control_callback;
    116     if ( isset($wp_registered_widgets[$_GET['add']]) ) {
    117         check_admin_referer( "add-widget_$_GET[add]" );
    118         $sidebars_widgets[$sidebar][] = $_GET['add'];
    119         wp_set_sidebars_widgets( $sidebars_widgets );
    120     } elseif ( isset($_GET['base']) && isset($_GET['key']) ) { // It's a multi-widget
    121         check_admin_referer( "add-widget_$_GET[add]" );
    122         // Copy minimal info from an existing instance of this widget to a new instance
    123         foreach ( $wp_registered_widget_controls as $control ) {
    124             if ( $_GET['base'] === $control['id_base'] ) {
    125                 $control_callback = $control['callback'];
    126                 $num = (int) $_GET['key'];
    127                 $control['params'][0]['number'] = $num;
    128                 $control['id'] = $control['id_base'] . '-' . $num;
    129                 $wp_registered_widget_controls[$control['id']] = $control;
    130                 $sidebars_widgets[$sidebar][] = $control['id'];
    131                 break;
    132             }
    133         }
    134     }
    135 
    136     // it's a multi-widget.  The only way to add multi-widgets without JS is to actually submit POST content...
    137     // so here we go
    138     if ( is_callable( $control_callback ) ) {
    139         require_once( 'admin-header.php' );
    140     ?>
    141         <div class="wrap">
    142         <h2><?php _e( 'Add Widget' ); ?></h2>
    143         <br />
    144         <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
    145 
    146             <ul class="widget-control-list">
    147                 <li class="widget-list-control-item">
    148                     <div class="widget-top">
    149                     <h4 class="widget-title"><?php echo $control['name']; ?></h4>
    150                     </div>
    151                     <div class="widget-control" style="display: block;">
    152     <?php
    153                         call_user_func_array( $control_callback, $control['params'] );
    154     ?>
    155                         <div class="widget-control-actions">
    156                             <input type="submit" class="button" value="<?php _e( 'Add Widget' ); ?>" />
    157                             <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
    158     <?php   wp_nonce_field ( 'edit-sidebar_' . $sidebar );
    159         foreach ( $sidebars_widgets[$sidebar] as $sidebar_widget_id ) : ?>
    160                             <input type="hidden" name='widget-id[]' value="<?php echo $sidebar_widget_id; ?>" />
    161     <?php   endforeach; ?>
    162                         </div>
    163                     </div>
    164                 </li>
    165             </ul>
    166         </form>
    167         </div>
    168     <?php
    169 
    170         require_once( 'admin-footer.php' );
    171         exit;
    172     }
    173     wp_redirect( remove_query_arg( $query_args ) );
    174     exit;
    175 } elseif ( isset($_GET['remove']) && $_GET['remove'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
    176     // Remove from sidebar the widget of type $_GET['remove'] and in position $_GET['key']
    177     $key = (int) $_GET['key'];
    178     if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['remove']) ) && in_array($key, $keys) ) {
    179         check_admin_referer( "remove-widget_$_GET[remove]" );
    180         unset($sidebars_widgets[$sidebar][$key]);
    181         $sidebars_widgets[$sidebar] = array_values($sidebars_widgets[$sidebar]);
    182         wp_set_sidebars_widgets( $sidebars_widgets );
    183     }
    184     wp_redirect( remove_query_arg( $query_args ) );
    185     exit;
    186 } elseif ( isset($_GET['edit']) && $_GET['edit'] && isset($_GET['key']) && is_numeric($_GET['key']) ) {
    187     // Edit widget of type $_GET['edit'] and position $_GET['key']
    188     $key = (int) $_GET['key'];
    189     if ( -1 < $key && ( $keys = array_keys($sidebars_widgets[$sidebar], $_GET['edit']) ) && in_array($key, $keys) )
    190         $edit_widget = $key;
    191 }
    192 
    193 // Total number of registered sidebars
    194 $sidebar_widget_count = count($sidebars_widgets[$sidebar]);
    195 
    196 // This is sort of lame since "widget" won't be converted to "widgets" in the JS
    197 if ( 1 < $sidebars_count = count($wp_registered_sidebars) )
    198     $sidebar_info_text = _n( 'You are using %1$s widget in the "%2$s" sidebar.', 'You are using %1$s widgets in the "%2$s" sidebar.', $sidebar_widget_count );
    199 else
    200     $sidebar_info_text = _n( 'You are using %1$s widget in the sidebar.', 'You are using %1$s widgets in the sidebar.', $sidebar_widget_count );
    201 
    202 
    203 $sidebar_info_text = sprintf( wp_specialchars( $sidebar_info_text ), "<span id='widget-count'>$sidebar_widget_count</span>", $wp_registered_sidebars[$sidebar]['name'] );
    204 
    205 $page = isset($_GET['apage']) ? abs( (int) $_GET['apage'] ) : 1;
    206 
    207 /* TODO: Paginate widgets list
    208 $page_links = paginate_links( array(
    209     'base'    => add_query_arg( 'apage', '%#%' ),
    210     'format'  => '',
    211     'total'   => ceil(($total = 105 )/ 10),
    212     'current' => $page
    213 ));
    214 */
    215 $page_links = '&nbsp;';
    216 
     109/*
    217110// Unsanitized!
    218111$widget_search = isset($_GET['s']) ? $_GET['s'] : false;
     
    224117    'used'   => __( 'Show used widgets' )
    225118);
     119*/
    226120
    227121$show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
     
    237131<?php endif; ?>
    238132
     133
     134<div id="message" class="updated"><p>This page is not 100% ready, please use only on test installations.</p></div>
     135
     136
    239137<div class="wrap">
    240138<?php screen_icon(); ?>
    241139<h2><?php echo wp_specialchars( $title ); ?></h2>
    242140
     141
     142<!--
    243143    <form id="widgets-filter" action="" method="get">
    244144
     
    248148        <div class="nav">
    249149            <select name="show" id="show">
    250 <?php foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
    251                 <option value='<?php echo $show_value; ?>'<?php selected( $show_value, $show ); ?>><?php echo wp_specialchars( $show_text ); ?></option>
    252 <?php endforeach; ?>
     150<?php //foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
     151                <option value='<?php //echo $show_value; ?>'<?php //selected( $show_value, $show ); ?>><?php //echo wp_specialchars( $show_text ); ?></option>
     152<?php //endforeach; ?>
    253153            </select>
    254154            <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
    255155            <p class="pagenav">
    256                 <?php echo $page_links; ?>
     156                <?php // echo $page_links; ?>
    257157            </p>
    258158        </div>
     
    265165        <div class="nav">
    266166            <select id="sidebar-selector" name="sidebar">
    267 <?php foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
    268                 <option value='<?php echo $sidebar_id; ?>'<?php selected( $sidebar_id, $sidebar ); ?>><?php echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
    269 <?php endforeach; ?>
     167<?php //foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
     168                <option value='<?php //echo $sidebar_id; ?>'<?php selected( $sidebar_id, $open_sidebar ); ?>><?php //echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
     169<?php //endforeach; ?>
    270170            </select>
    271171            <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
     
    275175
    276176    </form>
    277 
    278     <div id="widget-content" class="widget-liquid-left-holder">
    279 
    280         <div id="available-widgets" class="widget-liquid-left">
    281 
    282             <?php wp_list_widgets( $show, $widget_search ); // This lists all the widgets for the query ( $show, $search ) ?>
    283 
    284             <div class="nav">
    285                 <p class="pagenav">
    286                     <?php echo $page_links; ?>
    287                 </p>
    288             </div>
    289         </div>
    290     </div>
    291 
    292     <form id="widget-controls" action="" method="post">
    293 
     177-->
     178
     179    <div class="widget-liquid-left">
     180    <div id="widgets-left">
     181        <div id="available-widgets" class="widgets-holder-wrap">
     182            <h3 class="sidebar-name"><?php _e('Available Widgets'); ?></h3>
     183            <?php wp_list_widgets(); ?>
     184            <br class="clear" />
     185        </div>
     186
     187        <div id="wp_inactive_widgets" class="widgets-holder-wrap">
     188            <h3 class="sidebar-name"><?php _e('Inactive Widgets'); ?>
     189            <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
     190            <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
     191            <br class="clear" />
     192        </div>
     193    </div>
     194    </div>
     195
     196<!--
    294197    <div id="current-widgets-head" class="widget-liquid-right">
    295198
    296199        <div id="sidebar-info">
    297             <p><?php echo $sidebar_info_text; ?></p>
     200            <p><?php //echo $sidebar_info_text; ?></p>
    298201            <p><?php _e( 'Add more from the Available Widgets section.' ); ?></p>
    299202        </div>
    300203
    301204    </div>
    302 
    303     <div id="current-widgets" class="widget-liquid-right">
    304         <div id="current-sidebar">
    305 
    306             <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
    307 
    308         </div>
    309 
    310         <p class="submit">
    311             <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
    312             <input type="hidden" id="generated-time" name="generated-time" value="<?php echo time() - 1199145600; // Jan 1, 2008 ?>" />
    313             <input type="submit" name="save-widgets" class="button-primary" value="<?php _e( 'Save Changes' ); ?>" />
    314 <?php
    315             wp_nonce_field( 'edit-sidebar_' . $sidebar );
    316 ?>
    317         </p>
    318     </div>
    319 
     205-->
     206
     207    <div class="widget-liquid-right">
     208<?php
     209    $i = 0;
     210    foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
     211        if ( 'wp_inactive_widgets' == $sidebar )
     212            continue;
     213        ?>
     214        <div id="<?php echo attribute_escape( $sidebar ); ?>" class="widgets-holder-wrap">
     215        <h3 class="sidebar-name"><?php echo wp_specialchars( $registered_sidebar['name'] ); ?>
     216        <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
     217        <?php wp_list_widget_controls( $sidebar, $i ); // Show the control forms for each of the widgets in this sidebar ?>
     218        </div>
     219<?php
     220       $i++;
     221    } ?>
     222    </div>
     223    <form action="" method="post">
     224    <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
    320225    </form>
    321226    <br class="clear" />
    322 
    323227</div>
    324228
    325 <?php do_action( 'sidebar_admin_page' ); ?>
    326 
    327 <?php require_once( 'admin-footer.php' ); ?>
    328 
    329 
     229<?php
     230do_action( 'sidebar_admin_page' );
     231require_once( 'admin-footer.php' );
Note: See TracChangeset for help on using the changeset viewer.