3 require_once 'admin.php';
5 if ( ! current_user_can('switch_themes') )
6 wp_die( __( 'Cheatin’ uh?' ));
8 wp_enqueue_script('interface');
10 function wp_widgets_admin_head() {
11 global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls;
13 <?php wp_admin_css( 'css/widgets' ); ?>
15 <style type="text/css">
16 #palette { float: <?php echo ( get_bloginfo( 'text_direction' ) == 'rtl' ) ? 'right' : 'left'; ?>; }
22 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
23 $cols[] = '\'' . $index . '\'';
25 $cols = implode( ', ', $cols );
28 foreach ( $wp_registered_widgets as $name => $widget ) {
29 $widgets[] = '\'' . $widget['id'] . '\'';
31 $widgets = implode( ', ', $widgets );
33 <script type="text/javascript">
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']; ?>;
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');
51 jQuery('#shadow').css('opacity','0');
52 jQuery(widgets).each(function(o) {o='#widgetprefix-'+o; jQuery(o).css('position','relative');} );
54 function resetDroppableHeights() {
56 jQuery.map(cols, function(o) {
57 var c = jQuery('#' + o + ' li').length;
58 if ( c > max ) max = c;
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);
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);
72 function getViewportDims() {
74 if (self.innerHeight) { // all except Explorer
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;
84 return new Array(x,y);
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' );
99 jQuery(o).css('left', '1px');
101 jQuery(o).css('top', '1px');
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);};
112 function popShadow() {
113 maxHeight('#shadow');
114 jQuery('#shadow').css({zIndex: '999', display: 'block'});
115 jQuery('#shadow').fadeTo('fast', 0.2);
117 function unpopShadow() {
118 jQuery('#shadow').fadeOut('fast', function() {jQuery('#shadow').hide()});
120 function unpopControl(el) {
121 jQuery(el).attr('class','hidden');
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, ''));
131 function updateAll() {
132 jQuery.map(cols, function(o) {
133 if ( jQuery('#' + o + ' li').length )
134 jQuery('#'+o+'placemat span.handle').hide();
136 jQuery('#'+o+'placemat span.handle').show();
138 resetDroppableHeights();
140 jQuery(document).ready( function() {
148 add_action( 'admin_head', 'wp_widgets_admin_head' );
149 do_action( 'sidebar_admin_setup' );
151 function wp_widget_draggable( $name ) {
152 global $wp_registered_widgets, $wp_registered_widget_controls;
154 if ( !isset( $wp_registered_widgets[$name] ) ) {
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 . '">≡</div>'
164 $output = '<li class="module" id="widgetprefix-%1$s"><span class="handle">%2$s</span></li>';
166 printf( $output, $sanitized_name, $wp_registered_widgets[$name]['name'] . $popper );
169 $title = __( 'Widgets' );
170 $parent_file = 'themes.php';
172 require_once 'admin-header.php';
174 if ( count( $wp_registered_sidebars ) < 1 ) {
177 <h2><?php _e( 'No Sidebars Defined' ); ?></h2>
179 <p><?php _e( 'You are seeing this message because the theme you are currently using isn’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>
183 require_once 'admin-footer.php';
187 $sidebars_widgets = wp_get_sidebars_widgets();
189 if ( empty( $sidebars_widgets ) ) {
190 $sidebars_widgets = wp_get_widget_defaults();
193 if ( isset( $_POST['action'] ) ) {
194 check_admin_referer( 'widgets-save-widget-order' );
196 switch ( $_POST['action'] ) {
198 $sidebars_widgets = wp_get_widget_defaults();
199 wp_set_sidebars_widgets( $sidebars_widgets );
202 case 'save_widget_order' :
203 $sidebars_widgets = array();
205 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
206 $postindex = $index . 'order';
208 parse_str( $_POST[$postindex], $order );
210 $new_order = $order[$index];
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;
223 wp_set_sidebars_widgets( $sidebars_widgets );
228 ksort( $wp_registered_widgets );
230 $inactive_widgets = array();
232 foreach ( $wp_registered_widgets as $name => $widget ) {
235 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
236 if ( is_array( $sidebars_widgets[$index] ) && in_array( $name, $sidebars_widgets[$index] ) ) {
243 $inactive_widgets[] = $name;
247 $containers = array( 'palette' );
249 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
250 $containers[] = $index;
255 foreach ( $containers as $container ) {
256 $c_string .= '"' . $container . '",';
259 $c_string = substr( $c_string, 0, -1 );
261 if ( isset( $_POST['action'] ) ) {
263 <div class="fade updated" id="message">
264 <p><?php printf( __( 'Sidebar updated. <a href="%s">View site »</a>' ), get_bloginfo( 'url' ) . '/' ); ?></p>
270 <h2><?php _e( 'Sidebar Arrangement' ); ?></h2>
272 <p><?php _e( 'You can drag and drop widgets onto your sidebar below.' ); ?></p>
274 <form id="sbadmin" method="post" onsubmit="serializeAll();">
276 <input type="submit" value="<?php _e( 'Save Changes »' ); ?>" />
280 foreach ( $wp_registered_sidebars as $index => $sidebar ) {
282 <input type="hidden" id="<?php echo $index; ?>order" name="<?php echo $index; ?>order" value="" />
284 <div class="dropzone">
285 <h3><?php echo $sidebar['name']; ?></h3>
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.' ); ?>
294 <ul id="<?php echo $index; ?>">
296 if ( is_array( $sidebars_widgets[$index] ) ) {
297 foreach ( $sidebars_widgets[$index] as $name ) {
298 wp_widget_draggable( $name );
310 <div id="palettediv">
311 <h3><?php _e( 'Available Widgets' ); ?></h3>
315 foreach ( $inactive_widgets as $name ) {
316 wp_widget_draggable( $name );
322 <script type="text/javascript">
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
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 »' ); ?>" />
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">×</span>
345 <div class="controlform">
346 <?php call_user_func_array( $widget['callback'], $widget['params'] ); ?>
356 <div id="shadow"> </div>
358 <?php do_action( 'sidebar_admin_page' ); ?>
360 <?php require_once 'admin-footer.php'; ?>