root/tags/1.3/wp-admin/widgets.php

Revision 1069, 11.0 kB (checked in by donncha, 7 months ago)

Merge with WP 2.3 - testing use only!
Move pluggable functions out of wpmu-functions and into pluggable.php, fixes #439

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 require_once 'admin.php';
4
5 if ( ! current_user_can('switch_themes') )
6     wp_die( __( 'Cheatin&#8217; uh?' ));
7
8 wp_enqueue_script('interface');
9
10 function wp_widgets_admin_head() {
11     global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
12 ?>
13     <?php wp_admin_css( 'css/widgets' ); ?>
14     <!--[if IE 7]>
15     <style type="text/css">
16         #palette { float: <?php echo ( get_bloginfo( 'text_direction' ) == 'rtl' ) ? 'right' : 'left'; ?>; }
17     </style>
18     <![endif]-->
19 <?php
20
21     $cols = array();
22     foreach ( $wp_registered_sidebars as $index => $sidebar ) {
23         $cols[] = '\'' . $index . '\'';
24     }
25     $cols = implode( ', ', $cols );
26
27     $widgets = array();
28     foreach ( $wp_registered_widgets as $name => $widget ) {
29         $widgets[] = '\'' . $widget['id'] . '\'';
30     }
31     $widgets = implode( ', ', $widgets );
32 ?>
33 <script type="text/javascript">
34 // <![CDATA[
35     var cols = [<?php echo $cols; ?>];
36     var widgets = [<?php echo $widgets; ?>];
37     var controldims = new Array;
38     <?php foreach ( $wp_registered_widget_controls as $name => $widget ) : ?>
39         controldims['#<?php echo $widget['id']; ?>control'] = new Array;
40         controldims['#<?php echo $widget['id']; ?>control']['width'] = <?php echo (int) $widget['width']; ?>;
41         controldims['#<?php echo $widget['id']; ?>control']['height'] = <?php echo (int) $widget['height']; ?>;
42     <?php endforeach; ?>
43     function initWidgets() {
44     <?php foreach ( $wp_registered_widget_controls as $name => $widget ) : ?>
45         jQuery('#<?php echo $widget['id']; ?>popper').click(function() {popControl('#<?php echo $widget['id']; ?>control');});
46         jQuery('#<?php echo $widget['id']; ?>closer').click(function() {unpopControl('#<?php echo $widget['id']; ?>control');});
47         jQuery('#<?php echo $widget['id']; ?>control').Draggable({handle: '.controlhandle', zIndex: 1000});
48         if ( true && window.opera )
49             jQuery('#<?php echo $widget['id']; ?>control').css('border','1px solid #bbb');
50     <?php endforeach; ?>
51         jQuery('#shadow').css('opacity','0');
52         jQuery(widgets).each(function(o) {o='#widgetprefix-'+o; jQuery(o).css('position','relative');} );
53     }
54     function resetDroppableHeights() {
55         var max = 6;
56         jQuery.map(cols, function(o) {
57             var c = jQuery('#' + o + ' li').length;
58             if ( c > max ) max = c;
59         });
60         var maxheight = 35 * ( max + 1);
61         jQuery.map(cols, function(o) {
62             height = 0 == jQuery('#' + o + ' li').length ? maxheight - jQuery('#' + o + 'placemat').height() : maxheight;
63             jQuery('#' + o).height(height);
64         });
65     }
66     function maxHeight(elm) {
67         htmlheight = document.body.parentNode.clientHeight;
68         bodyheight = document.body.clientHeight;
69         var height = htmlheight > bodyheight ? htmlheight : bodyheight;
70         jQuery(elm).height(height);
71     }
72     function getViewportDims() {
73         var x,y;
74         if (self.innerHeight) { // all except Explorer
75             x = self.innerWidth;
76             y = self.innerHeight;
77         } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
78             x = document.documentElement.clientWidth;
79             y = document.documentElement.clientHeight;
80         } else if (document.body) { // other Explorers
81             x = document.body.clientWidth;
82             y = document.body.clientHeight;
83         }
84         return new Array(x,y);
85     }
86     function dragChange(o) {
87         var p = getViewportDims();
88         var screenWidth = p[0];
89         var screenHeight = p[1];
90         var elWidth = parseInt( jQuery(o).css('width') );
91         var elHeight = parseInt( jQuery(o).css('height') );
92         var elLeft = parseInt( jQuery(o).css('left') );
93         var elTop = parseInt( jQuery(o).css('top') );
94         if ( screenWidth < ( parseInt(elLeft) + parseInt(elWidth) ) )
95             jQuery(o).css('left', ( screenWidth - elWidth ) + 'px' );
96         if ( screenHeight < ( parseInt(elTop) + parseInt(elHeight) ) )
97             jQuery(o).css('top', ( screenHeight - elHeight ) + 'px' );
98         if ( elLeft < 1 )
99             jQuery(o).css('left', '1px');
100         if ( elTop < 1 )
101             jQuery(o).css('top', '1px');
102     }
103     function popControl(elm) {
104         var x = ( document.body.clientWidth - controldims[elm]['width'] ) / 2;
105         var y = ( document.body.parentNode.clientHeight - controldims[elm]['height'] ) / 2;
106         jQuery(elm).css({display: 'block', width: controldims[elm]['width'] + 'px', height: controldims[elm]['height'] + 'px', position: 'absolute', right: x + 'px', top: y + 'px', zIndex: '1000' });
107         jQuery(elm).attr('class','control');
108         jQuery('#shadow').click(function() {unpopControl(elm);});
109         window.onresize = function(){maxHeight('#shadow');dragChange(elm);};
110         popShadow();
111     }
112     function popShadow() {
113         maxHeight('#shadow');
114         jQuery('#shadow').css({zIndex: '999', display: 'block'});
115         jQuery('#shadow').fadeTo('fast', 0.2);
116     }
117     function unpopShadow() {
118         jQuery('#shadow').fadeOut('fast', function() {jQuery('#shadow').hide()});
119     }
120     function unpopControl(el) {
121         jQuery(el).attr('class','hidden');
122         jQuery(el).hide();
123         unpopShadow();
124     }
125     function serializeAll() {
126     <?php $i = 0; foreach ( $wp_registered_sidebars as $index => $sidebar ) : $i++; ?>
127         var serial<?php echo $i ?> = jQuery.SortSerialize('<?php echo $index ?>');
128         jQuery('#<?php echo $index ?>order').attr('value',serial<?php echo $i ?>.hash.replace(/widgetprefix-/g, ''));
129     <?php endforeach; ?>
130     }
131     function updateAll() {
132         jQuery.map(cols, function(o) {
133             if ( jQuery('#' + o + ' li').length )
134                 jQuery('#'+o+'placemat span.handle').hide();
135             else
136                 jQuery('#'+o+'placemat span.handle').show();
137         });
138         resetDroppableHeights();
139     }
140     jQuery(document).ready( function() {
141         updateAll();
142         initWidgets();
143     });
144 // ]]>
145 </script>
146 <?php
147 }
148 add_action( 'admin_head', 'wp_widgets_admin_head' );
149 do_action( 'sidebar_admin_setup' );
150
151 function wp_widget_draggable( $name ) {
152     global $wp_registered_widgets, $wp_registered_widget_controls;
153
154     if ( !isset( $wp_registered_widgets[$name] ) ) {
155         return;
156     }
157
158     $sanitized_name = sanitize_title( $wp_registered_widgets[$name]['id'] );
159     $link_title = __( 'Configure' );
160     $popper = ( isset( $wp_registered_widget_controls[$name] ) )
161         ? ' <div class="popper" id="' . $sanitized_name . 'popper" title="' . $link_title . '">&#8801;</div>'
162         : '';
163
164     $output = '<li class="module" id="widgetprefix-%1$s"><span class="handle">%2$s</span></li>';
165
166     printf( $output, $sanitized_name, $wp_registered_widgets[$name]['name'] . $popper );
167 }
168
169 $title = __( 'Widgets' );
170 $parent_file = 'themes.php';
171
172 require_once 'admin-header.php';
173
174 if ( count( $wp_registered_sidebars ) < 1 ) {
175 ?>
176     <div class="wrap">
177         <h2><?php _e( 'No Sidebars Defined' ); ?></h2>
178
179         <p><?php _e( 'You are seeing this message because the theme you are currently using isn&#8217;t widget-aware, meaning that it has no sidebars that you are able to change. For information on making your theme widget-aware, please <a href="http://automattic.com/code/widgets/themes/">follow these instructions</a>.' ); /* TODO: article on codex */; ?></p>
180     </div>
181 <?php
182
183     require_once 'admin-footer.php';
184     exit;
185 }
186
187 $sidebars_widgets = wp_get_sidebars_widgets();
188
189 if ( empty( $sidebars_widgets ) ) {
190     $sidebars_widgets = wp_get_widget_defaults();
191 }
192
193 if ( isset( $_POST['action'] ) ) {
194     check_admin_referer( 'widgets-save-widget-order' );
195
196     switch ( $_POST['action'] ) {
197         case 'default' :
198             $sidebars_widgets = wp_get_widget_defaults();
199             wp_set_sidebars_widgets( $sidebars_widgets );
200         break;
201
202         case 'save_widget_order' :
203             $sidebars_widgets = array();
204
205             foreach ( $wp_registered_sidebars as $index => $sidebar ) {
206                 $postindex = $index . 'order';
207
208                 parse_str( $_POST[$postindex], $order );
209
210                 $new_order = $order[$index];
211
212                 if ( is_array( $new_order ) ) {
213                     foreach ( $new_order as $sanitized_name ) {
214                         foreach ( $wp_registered_widgets as $name => $widget ) {
215                             if ( $sanitized_name == $widget['id'] ) {
216                                 $sidebars_widgets[$index][] = $name;
217                             }
218                         }
219                     }
220                 }
221             }
222
223             wp_set_sidebars_widgets( $sidebars_widgets );
224         break;
225     }
226 }
227
228 ksort( $wp_registered_widgets );
229
230 $inactive_widgets = array();
231
232 foreach ( $wp_registered_widgets as $name => $widget ) {
233     $is_active = false;
234
235     foreach ( $wp_registered_sidebars as $index => $sidebar ) {
236         if ( is_array( $sidebars_widgets[$index] ) && in_array( $name, $sidebars_widgets[$index] ) ) {
237             $is_active = true;
238             break;
239         }
240     }
241
242     if ( !$is_active ) {
243         $inactive_widgets[] = $name;
244     }
245 }
246
247 $containers = array( 'palette' );
248
249 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
250     $containers[] = $index;
251 }
252
253 $c_string = '';
254
255 foreach ( $containers as $container ) {
256     $c_string .= '"' . $container . '",';
257 }
258
259 $c_string = substr( $c_string, 0, -1 );
260
261 if ( isset( $_POST['action'] ) ) {
262 ?>
263     <div class="fade updated" id="message">
264         <p><?php printf( __( 'Sidebar updated. <a href="%s">View site &raquo;</a>' ), get_bloginfo( 'url' ) . '/' ); ?></p>
265     </div>
266 <?php
267 }
268 ?>
269     <div class="wrap">
270         <h2><?php _e( 'Sidebar Arrangement' ); ?></h2>
271
272         <p><?php _e( 'You can drag and drop widgets onto your sidebar below.' ); ?></p>
273
274         <form id="sbadmin" method="post" onsubmit="serializeAll();">
275             <p class="submit">
276                 <input type="submit" value="<?php _e( 'Save Changes &raquo;' ); ?>" />
277             </p>
278             <div id="zones">
279             <?php
280                 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
281             ?>
282                 <input type="hidden" id="<?php echo $index; ?>order" name="<?php echo $index; ?>order" value="" />
283
284                 <div class="dropzone">
285                     <h3><?php echo $sidebar['name']; ?></h3>
286
287                     <div id="<?php echo $index; ?>placemat" class="placemat">
288                         <span class="handle">
289                             <h4><?php _e( 'Default Sidebar' ); ?></h4>
290                             <?php _e( 'Your theme will display its usual sidebar when this box is empty. Dragging widgets into this box will replace the usual sidebar with your customized sidebar.' ); ?>
291                         </span>
292                     </div>
293
294                     <ul id="<?php echo $index; ?>">
295                     <?php
296                         if ( is_array( $sidebars_widgets[$index] ) ) {
297                             foreach ( $sidebars_widgets[$index] as $name ) {
298                                 wp_widget_draggable( $name );
299                             }
300                         }
301                     ?>
302                     </ul>
303                 </div>
304             <?php
305                 }
306             ?>
307
308             </div>
309
310             <div id="palettediv">
311                 <h3><?php _e( 'Available Widgets' ); ?></h3>
312
313                 <ul id="palette">
314                 <?php
315                     foreach ( $inactive_widgets as $name ) {
316                         wp_widget_draggable( $name );
317                     }
318                 ?>
319                 </ul>
320             </div>
321
322             <script type="text/javascript">
323             // <![CDATA[
324                 jQuery(document).ready(function(){
325             <?php foreach ( $containers as $container ) { ?>
326                     jQuery('ul#<?php echo $container; ?>').Sortable({
327                         accept: 'module', activeclass: 'activeDraggable', opacity: 0.8, revert: true, onStop: updateAll
328                     });
329             <?php } ?>
330                 });
331             // ]]>
332             </script>
333
334             <p class="submit">
335             <?php wp_nonce_field( 'widgets-save-widget-order' ); ?>
336                 <input type="hidden" name="action" id="action" value="save_widget_order" />
337                 <input type="submit" value="<?php _e( 'Save Changes &raquo;' ); ?>" />
338             </p>
339
340             <div id="controls">
341             <?php foreach ( $wp_registered_widget_controls as $name => $widget ) { ?>
342                 <div class="hidden" id="<?php echo $widget['id']; ?>control">
343                     <span class="controlhandle"><?php echo $widget['name']; ?></span>
344                     <span id="<?php echo $widget['id']; ?>closer" class="controlcloser">&#215;</span>
345                     <div class="controlform">
346                     <?php call_user_func_array( $widget['callback'], $widget['params'] ); ?>
347                     </div>
348                 </div>
349             <?php } ?>
350             </div>
351         </form>
352
353         <br class="clear" />
354     </div>
355
356     <div id="shadow"> </div>
357
358     <?php do_action( 'sidebar_admin_page' ); ?>
359
360 <?php require_once 'admin-footer.php'; ?>
361
Note: See TracBrowser for help on using the browser.