Make WordPress Core


Ignore:
Timestamp:
03/17/2008 11:25:05 PM (19 years ago)
Author:
ryan
Message:

Fix multi-widget addition with JS disabled. Props mdawaffe. see #6239

File:
1 edited

Legend:

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

    r7345 r7362  
    4747        $sidebars_widgets[$sidebar] = array();
    4848
    49 $http_post = ( 'POST' == $_SERVER['REQUEST_METHOD'] );
     49$http_post = 'post' == strtolower($_SERVER['REQUEST_METHOD']);
    5050
    5151// We're updating a sidebar
    5252if ( $http_post && isset($sidebars_widgets[$_POST['sidebar']]) ) {
     53        check_admin_referer( 'edit-sidebar_' . $_POST['sidebar'] );
    5354
    5455        /* Hack #1
    … …  
    8283                        unset($_POST['widget-id'][$key]);
    8384
    84         // Reset the key numbering and stare
     85        // Reset the key numbering and store
    8586        $new_sidebar = isset( $_POST['widget-id'] ) && is_array( $_POST['widget-id'] ) ? array_values( $_POST['widget-id'] ) : array();
    8687        $sidebars_widgets[$_POST['sidebar']] = $new_sidebar;
    … …  
    9798$edit_widget = -1;
    9899
    99 $query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message' );
     100$query_args = array('add', 'remove', 'key', 'edit', '_wpnonce', 'message', 'base' );
    100101
    101102if ( isset($_GET['add']) && $_GET['add'] ) {
    102103        // Add to the end of the sidebar
     104        $control_callback;
    103105        if ( isset($wp_registered_widgets[$_GET['add']]) ) {
    104106                check_admin_referer( "add-widget_$_GET[add]" );
    105107                $sidebars_widgets[$sidebar][] = $_GET['add'];
    106108                wp_set_sidebars_widgets( $sidebars_widgets );
     109        } elseif ( isset($_GET['base']) && isset($_GET['key']) ) { // It's a multi-widget
     110                check_admin_referer( "add-widget_$_GET[add]" );
     111                // Copy minimal info from an existing instance of this widget to a new instance
     112                foreach ( $wp_registered_widget_controls as $control ) {
     113                        if ( $_GET['base'] === $control['id_base'] ) {
     114                                $control_callback = $control['callback'];
     115                                $num = (int) $_GET['key'];
     116                                $control['params'][0]['number'] = $num;
     117                                $control['id'] = $control['id_base'] . '-' . $num;
     118                                $wp_registered_widget_controls[$control['id']] = $control;
     119                                $sidebars_widgets[$sidebar][] = $control['id'];
     120                                break;
     121                        }
     122                }
     123        }
     124
     125        // it's a multi-widget.  The only way to add multi-widgets without JS is to actually submit POST content...
     126        // so here we go
     127        if ( is_callable( $control_callback ) ) {
     128                require_once( 'admin-header.php' );
     129        ?>
     130                <div class="wrap">
     131                <h2><?php _e( 'Add Widget' ); ?></h2>
     132                <br />
     133                <form action="<?php echo clean_url( remove_query_arg( $query_args ) ); ?>" method="post">
     134               
     135                        <ul class="widget-control-list">
     136                                <li class="widget-list-control-item">
     137                                        <h4 class="widget-title"><?php echo $control['name']; ?></h4>
     138                                        <div class="widget-control" style="display: block;">
     139        <?php
     140                                                call_user_func_array( $control_callback, $control['params'] );
     141        ?>
     142                                                <div class="widget-control-actions">
     143                                                        <input type="submit" class="button" value="<?php _e( 'Add Widget' ); ?>" />
     144                                                        <input type="hidden" id='sidebar' name='sidebar' value="<?php echo $sidebar; ?>" />
     145        <?php   wp_nonce_field ( 'edit-sidebar_' . $sidebar );
     146                foreach ( $sidebars_widgets[$sidebar] as $sidebar_widget_id ) : ?>
     147                                                        <input type="hidden" name='widget-id[]' value="<?php echo $sidebar_widget_id; ?>" />
     148        <?php   endforeach; ?>
     149                                                </div>
     150                                        </div>
     151                                </li>
     152                        </ul>
     153                </form>
     154                </div>
     155        <?php
     156
     157                require_once( 'admin-footer.php' );
     158                exit;
    107159        }
    108160        wp_redirect( remove_query_arg( $query_args ) );
    … …  
    254306                        <input type="hidden" id="generated-time" name="generated-time" value="<?php echo time() - 1199145600; // Jan 1, 2008 ?>" />
    255307                        <input type="submit" name="save-widgets" value="<?php _e( 'Save Changes' ); ?>" />
     308<?php
     309                        wp_nonce_field( 'edit-sidebar_' . $sidebar );
     310?>
    256311                </p>
    257312        </div>
Note: See TracChangeset for help on using the changeset viewer.