WordPress.org

Make WordPress Core


Ignore:
Timestamp:
04/20/2009 10:58:50 AM (11 years ago)
Author:
azaozz
Message:

Add and edit widget when js is disabled, see #9511

File:
1 edited

Legend:

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

    r10927 r11009  
    2626// register the inactive_widgets area as sidebar
    2727register_sidebar(array(
    28     'name' => '',
     28    'name' => __('Inactive Widgets'),
    2929    'id' => 'wp_inactive_widgets',
    3030    'before_widget' => '',
     
    3939    $sidebars_widgets = wp_get_widget_defaults();
    4040
    41 // look for "lost" widgets, this has to run at each theme change
     41// look for "lost" widgets, this has to run at least on each theme change
    4242function retrieve_widgets() {
    4343    global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets;
     
    112112
    113113if ( count($wp_registered_sidebars) == 1 ) {
    114     // If only the "wp_inactive_widgets" is defined the theme has no sidebars, die.
     114    // If only "wp_inactive_widgets" is defined the theme has no sidebars, die.
    115115    require_once( 'admin-header.php' );
    116116?>
     
    130130}
    131131
     132// We're saving a widget without js
     133if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
     134    $widget_id = $_POST['widget-id'];
     135    check_admin_referer("save-delete-widget-$widget_id");
     136
     137    $sidebar_id = $_POST['insidebar'];
     138    $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
     139    $_POST['sidebar'] = $sidebar_id;
     140
     141    $id_base = $_POST['id_base'];
     142    $number = isset($_POST['multi_number']) ? $_POST['multi_number'] : '';
     143    $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
     144
     145    // delete
     146    if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
     147        $widget = isset($wp_registered_widgets[$widget_id]) ? $wp_registered_widgets[$widget_id] : false;
     148
     149        if ( !in_array($widget_id, $sidebar, true) || !$widget ) {
     150            wp_redirect('widgets.php?error=0');
     151            exit;
     152        }
     153
     154        $option = str_replace( '-', '_', 'widget_' . $id_base );
     155        $data = get_option($option);
     156
     157        if ( isset($widget['params'][0]['number']) ) {
     158            $number = $widget['params'][0]['number'];
     159            if ( is_array($data) && isset($data[$number]) ) {
     160                unset( $data[$number] );
     161                update_option($option, $data);
     162            }
     163        } else {
     164            if ( $data ) {
     165                $data = array();
     166                update_option($option, $data);
     167            }
     168        }
     169
     170        $sidebar = array_diff( $sidebar, array($widget_id) );
     171    } else {
     172        // save
     173        foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
     174            if ( $name != $id_base || !is_callable($control['callback']) )
     175                continue;
     176
     177            if ( $number ) {
     178                // don't delete other instances of the same multi-widget
     179                foreach ( $sidebar as $_widget_id ) {
     180                    if ( isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) )
     181                        unset($wp_registered_widgets[$_widget_id]['params'][0]['number']);
     182                }
     183                $widget_id = "$id_base-$number";
     184            }
     185
     186            ob_start();
     187                call_user_func_array( $control['callback'], $control['params'] );
     188            ob_end_clean();
     189
     190            // remove old position
     191            $sidebar = array_diff( $sidebar, array($widget_id) );
     192            foreach ( $sidebars_widgets as $key => $sb ) {
     193                if ( is_array($sb) && in_array($widget_id, $sb, true) )
     194                    $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
     195            }
     196
     197            array_splice( $sidebar, $position, 0, $widget_id );
     198            break;
     199        }
     200    }
     201
     202    $sidebars_widgets[$sidebar_id] = $sidebar;
     203    wp_set_sidebars_widgets($sidebars_widgets);
     204
     205    wp_redirect('widgets.php?message=0');
     206    exit;
     207}
     208
     209// Output the widget form without js
     210if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
     211    $widget_id = $_GET['editwidget'];
     212
     213    if ( isset($_GET['addnew']) ) {
     214        // Default to the first sidebar
     215        $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
     216
     217        if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
     218            // Copy minimal info from an existing instance of this widget to a new instance
     219            foreach ( $wp_registered_widget_controls as $control ) {
     220                if ( $_GET['base'] === $control['id_base'] ) {
     221                    $control_callback = $control['callback'];
     222                    $multi_number = (int) $_GET['num'];
     223                    $control['params'][0]['number'] = $multi_number;
     224                    $control['id'] = $control['id_base'] . '-' . $multi_number;
     225                    $wp_registered_widget_controls[$control['id']] = $control;
     226                    break;
     227                }
     228            }
     229        }
     230    }
     231
     232    if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
     233        $control = $wp_registered_widget_controls[$widget_id];
     234        $control_callback = $control['callback'];
     235    }
     236
     237    if ( !isset($sidebar) )
     238        $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
     239
     240    if ( !isset($multi_number) )
     241        $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
     242
     243    $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
     244
     245    // show the widget form
     246    if ( is_callable( $control_callback ) ) {
     247        $width = ' style="width:' . max($control['width'], 350) . 'px"';
     248        $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
     249
     250        require_once( 'admin-header.php' ); ?>
     251        <div class="wrap">
     252        <?php screen_icon(); ?>
     253        <h2><?php echo wp_specialchars( $title ); ?></h2>
     254        <div class="editwidget"<?php echo $width; ?>>
     255        <h3><?php printf( __( 'Widget %s' ), wp_specialchars( strip_tags($control['name']) ) ); ?></h3>
     256
     257        <form action="widgets.php" method="post">
     258        <div class="widget-control">
     259<?php   call_user_func_array( $control_callback, $control['params'] ); ?>
     260        </div>
     261
     262        <div class="widget-position">
     263        <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
     264<?php   foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
     265            echo "\t\t<tr><td><label><input type='radio' name='insidebar' value='$sbname'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
     266            if ( 'wp_inactive_widgets' == $sbname ) {
     267                echo '&nbsp;';
     268            } else {
     269                if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
     270                    $j = 1;
     271                } else {
     272                    $j = count($sidebars_widgets[$sbname]);
     273                    if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
     274                        $j++;
     275                }
     276                $selected = '';
     277                echo "\t\t<select name='{$sbname}_position'>\n";
     278                echo "\t\t<option value=''>" . __('-- select --') . "</option>\n";
     279                for ( $i = 1; $i <= $j; $i++ ) {
     280                    if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
     281                        $selected = selected( $i, $key + 1, false );
     282                    echo "\t\t<option value='$i'$selected> $i </option>\n";
     283                }
     284                echo "\t\t</select>\n";
     285            }
     286            echo "</td></tr>\n";
     287        } ?>
     288        </tbody></table>
     289        </div>
     290
     291        <div class="widget-control-actions">
     292<?php   if ( isset($_GET['addnew']) ) { ?>
     293        <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
     294<?php   } else { ?>
     295        <input type="submit" name="removewidget" class="button alignleft" value="<?php _e('Remove'); ?>" />
     296<?php   } ?>
     297        <input type="submit" name="savewidget" class="button-primary alignright" value="<?php _e('Save Widget'); ?>" />
     298        <input type="hidden" name="widget-id" class="widget-id" value="<?php echo $widget_id; ?>" />
     299        <input type="hidden" name="id_base" class="id_base" value="<?php echo $id_base; ?>" />
     300        <input type="hidden" name="multi_number" class="multi_number" value="<?php echo $multi_number; ?>" />
     301<?php   wp_nonce_field("save-delete-widget-$widget_id"); ?>
     302        </div>
     303        </form>
     304        </div>
     305        </div>
     306<?php
     307        require_once( 'admin-footer.php' );
     308        exit;
     309    }
     310    wp_redirect('widgets.php?error=1');
     311    exit;
     312}
     313
    132314/*
    133315// Unsanitized!
     
    136318// Not entirely sure what all should be here
    137319$show_values = array(
    138     ''       => $widget_search ? __( 'Show any widgets' ) : __( 'Show all widgets' ),
     320    ''      => $widget_search ? __( 'Show any widgets' ) : __( 'Show all widgets' ),
    139321    'unused' => __( 'Show unused widgets' ),
    140     'used'   => __( 'Show used widgets' )
     322    'used'  => __( 'Show used widgets' )
    141323);
     324
     325$show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
    142326*/
    143327
    144 $show = isset($_GET['show']) && isset($show_values[$_GET['show']]) ? attribute_escape( $_GET['show'] ) : false;
    145 
    146328$messages = array(
    147     'updated' => __('Changes saved.')
     329    __('Changes saved.')
     330);
     331
     332$errors = array(
     333    __('Error while saving.'),
     334    __('Error in displaying the widget settings form.')
    148335);
    149336
     
    154341<h2><?php echo wp_specialchars( $title ); ?></h2>
    155342
    156 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) : ?>
     343<?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
    157344<div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
    158 <?php endif; ?>
    159 
    160 <!--
    161     <form id="widgets-filter" action="" method="get">
    162 
    163     <div class="widget-liquid-left-holder">
    164     <div id="available-widgets-filter" class="widget-liquid-left">
    165         <h3><label for="show"><?php _e('Available Widgets'); ?></label></h3>
    166         <div class="nav">
    167             <select name="show" id="show">
    168 <?php //foreach ( $show_values as $show_value => $show_text ) : $show_value = attribute_escape( $show_value ); ?>
    169                 <option value='<?php //echo $show_value; ?>'<?php //selected( $show_value, $show ); ?>><?php //echo wp_specialchars( $show_text ); ?></option>
    170 <?php //endforeach; ?>
    171             </select>
    172             <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
    173             <p class="pagenav">
    174                 <?php // echo $page_links; ?>
    175             </p>
    176         </div>
     345<?php } ?>
     346<?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
     347<div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
     348<?php } ?>
     349
     350<div class="widget-liquid-left">
     351<div id="widgets-left">
     352    <div id="available-widgets" class="widgets-holder-wrap">
     353        <h3 class="sidebar-name"><?php _e('Available Widgets'); ?></h3>
     354        <?php wp_list_widgets(); ?>
     355        <br class="clear" />
    177356    </div>
     357
     358    <div id="wp_inactive_widgets" class="widgets-holder-wrap">
     359        <h3 class="sidebar-name"><?php _e('Inactive Widgets'); ?>
     360        <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
     361        <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
     362        <br class="clear" />
    178363    </div>
    179 
    180     <div id="available-sidebars" class="widget-liquid-right">
    181         <h3><label for="sidebar-selector"><?php _e('Current Widgets'); ?></label></h3>
    182 
    183         <div class="nav">
    184             <select id="sidebar-selector" name="sidebar">
    185 <?php //foreach ( $wp_registered_sidebars as $sidebar_id => $registered_sidebar ) : $sidebar_id = attribute_escape( $sidebar_id ); ?>
    186                 <option value='<?php //echo $sidebar_id; ?>'<?php selected( $sidebar_id, $open_sidebar ); ?>><?php //echo wp_specialchars( $registered_sidebar['name'] ); ?></option>
    187 <?php //endforeach; ?>
    188             </select>
    189             <input type="submit" value="<?php _e('Show' ); ?>" class="button-secondary" />
    190         </div>
    191 
     364</div>
     365</div>
     366
     367<div class="widget-liquid-right">
     368<?php
     369$i = 0;
     370foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
     371    if ( 'wp_inactive_widgets' == $sidebar )
     372        continue; ?>
     373    <div id="<?php echo attribute_escape( $sidebar ); ?>" class="widgets-holder-wrap">
     374    <h3 class="sidebar-name"><?php echo wp_specialchars( $registered_sidebar['name'] ); ?>
     375    <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
     376    <?php wp_list_widget_controls( $sidebar, $i ); // Show the control forms for each of the widgets in this sidebar ?>
    192377    </div>
    193 
    194     </form>
    195 -->
    196 
    197     <div class="widget-liquid-left">
    198     <div id="widgets-left">
    199         <div id="available-widgets" class="widgets-holder-wrap">
    200             <h3 class="sidebar-name"><?php _e('Available Widgets'); ?></h3>
    201             <?php wp_list_widgets(); ?>
    202             <br class="clear" />
    203         </div>
    204 
    205         <div id="wp_inactive_widgets" class="widgets-holder-wrap">
    206             <h3 class="sidebar-name"><?php _e('Inactive Widgets'); ?>
    207             <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
    208             <?php wp_list_widget_controls('wp_inactive_widgets'); ?>
    209             <br class="clear" />
    210         </div>
    211     </div>
    212     </div>
    213 
    214 <!--
    215     <div id="current-widgets-head" class="widget-liquid-right">
    216 
    217         <div id="sidebar-info">
    218             <p><?php //echo $sidebar_info_text; ?></p>
    219             <p><?php _e( 'Add more from the Available Widgets section.' ); ?></p>
    220         </div>
    221 
    222     </div>
    223 -->
    224 
    225     <div class="widget-liquid-right">
    226 <?php
    227     $i = 0;
    228     foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
    229         if ( 'wp_inactive_widgets' == $sidebar )
    230             continue;
    231         ?>
    232         <div id="<?php echo attribute_escape( $sidebar ); ?>" class="widgets-holder-wrap">
    233         <h3 class="sidebar-name"><?php echo wp_specialchars( $registered_sidebar['name'] ); ?>
    234         <span><img src="images/loading-publish.gif" class="ajax-feedback" title="" alt="" /></span></h3>
    235         <?php wp_list_widget_controls( $sidebar, $i ); // Show the control forms for each of the widgets in this sidebar ?>
    236         </div>
    237 <?php
    238        $i++;
    239     } ?>
    240     </div>
    241     <form action="" method="post">
    242     <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
    243     </form>
    244     <br class="clear" />
     378<?php
     379    $i++;
     380} ?>
     381</div>
     382<form action="" method="post">
     383<?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
     384</form>
     385<br class="clear" />
    245386</div>
    246387
Note: See TracChangeset for help on using the changeset viewer.