]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/widgets.php
WordPress 3.5.1
[autoinstalls/wordpress.git] / wp-admin / widgets.php
1 <?php
2 /**
3  * Widgets administration panel.
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( './admin.php' );
11
12 /** WordPress Administration Widgets API */
13 require_once(ABSPATH . 'wp-admin/includes/widgets.php');
14
15 if ( ! current_user_can('edit_theme_options') )
16         wp_die( __( 'Cheatin&#8217; uh?' ));
17
18 $widgets_access = get_user_setting( 'widgets_access' );
19 if ( isset($_GET['widgets-access']) ) {
20         $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
21         set_user_setting( 'widgets_access', $widgets_access );
22 }
23
24 function wp_widgets_access_body_class($classes) {
25         return "$classes widgets_access ";
26 }
27
28 if ( 'on' == $widgets_access ) {
29         add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
30 } else {
31         wp_enqueue_script('admin-widgets');
32
33         if ( wp_is_mobile() )
34                 wp_enqueue_script( 'jquery-touch-punch' );
35 }
36
37 do_action( 'sidebar_admin_setup' );
38
39 $title = __( 'Widgets' );
40 $parent_file = 'themes.php';
41
42 get_current_screen()->add_help_tab( array(
43 'id'            => 'overview',
44 'title'         => __('Overview'),
45 'content'       =>
46         '<p>' . __('Widgets are independent sections of content that can be placed into any widgetized area provided by your theme (commonly called sidebars). To populate your sidebars/widget areas with individual widgets, drag and drop the title bars into the desired area. By default, only the first widget area is expanded. To populate additional widget areas, click on their title bars to expand them.') . '</p>
47         <p>' . __('The Available Widgets section contains all the widgets you can choose from. Once you drag a widget into a sidebar, it will open to allow you to configure its settings. When you are happy with the widget settings, click the Save button and the widget will go live on your site. If you click Delete, it will remove the widget.') . '</p>'
48 ) );
49 get_current_screen()->add_help_tab( array(
50 'id'            => 'removing-reusing',
51 'title'         => __('Removing and Reusing'),
52 'content'       =>
53         '<p>' . __('If you want to remove the widget but save its setting for possible future use, just drag it into the Inactive Widgets area. You can add them back anytime from there. This is especially helpful when you switch to a theme with fewer or different widget areas.') . '</p>
54         <p>' . __('Widgets may be used multiple times. You can give each widget a title, to display on your site, but it&#8217;s not required.') . '</p>
55         <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
56 ) );
57 get_current_screen()->add_help_tab( array(
58 'id'            => 'missing-widgets',
59 'title'         => __('Missing Widgets'),
60 'content'       =>
61         '<p>' . __('Many themes show some sidebar widgets by default until you edit your sidebars, but they are not automatically displayed in your sidebar management tool. After you make your first widget change, you can re-add the default widgets by adding them from the Available Widgets area.') . '</p>' .
62                 '<p>' . __('When changing themes, there is often some variation in the number and setup of widget areas/sidebars and sometimes these conflicts make the transition a bit less smooth. If you changed themes and seem to be missing widgets, scroll down on this screen to the Inactive Widgets area, where all of your widgets and their settings will have been saved.') . '</p>'
63 ) );
64
65 get_current_screen()->set_help_sidebar(
66         '<p><strong>' . __('For more information:') . '</strong></p>' .
67         '<p>' . __('<a href="http://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
68         '<p>' . __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
69 );
70
71 if ( ! current_theme_supports( 'widgets' ) ) {
72         wp_die( __( '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://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
73 }
74
75 // These are the widgets grouped by sidebar
76 $sidebars_widgets = wp_get_sidebars_widgets();
77
78 if ( empty( $sidebars_widgets ) )
79         $sidebars_widgets = wp_get_widget_defaults();
80
81 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
82         if ( 'wp_inactive_widgets' == $sidebar_id )
83                 continue;
84
85         if ( !isset( $wp_registered_sidebars[ $sidebar_id ] ) ) {
86                 if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
87                         register_sidebar(array(
88                                 'name' => __( 'Inactive Sidebar (not used)' ),
89                                 'id' => $sidebar_id,
90                                 'class' => 'inactive-sidebar orphan-sidebar',
91                                 'description' => __( 'This sidebar is no longer available and does not show anywhere on your site. Remove each of the widgets below to fully remove this inactive sidebar.' ),
92                                 'before_widget' => '',
93                                 'after_widget' => '',
94                                 'before_title' => '',
95                                 'after_title' => '',
96                         ));
97                 } else {
98                         unset( $sidebars_widgets[ $sidebar_id ] );
99                 }
100         }
101 }
102
103 // register the inactive_widgets area as sidebar
104 register_sidebar(array(
105         'name' => __('Inactive Widgets'),
106         'id' => 'wp_inactive_widgets',
107         'class' => 'inactive-sidebar',
108         'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
109         'before_widget' => '',
110         'after_widget' => '',
111         'before_title' => '',
112         'after_title' => '',
113 ));
114
115 retrieve_widgets();
116
117 // We're saving a widget without js
118 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
119         $widget_id = $_POST['widget-id'];
120         check_admin_referer("save-delete-widget-$widget_id");
121
122         $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
123         if ( $number ) {
124                 foreach ( $_POST as $key => $val ) {
125                         if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
126                                 $_POST[$key] = array( $number => array_shift($val) );
127                                 break;
128                         }
129                 }
130         }
131
132         $sidebar_id = $_POST['sidebar'];
133         $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
134
135         $id_base = $_POST['id_base'];
136         $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
137
138         // delete
139         if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
140
141                 if ( !in_array($widget_id, $sidebar, true) ) {
142                         wp_redirect( admin_url('widgets.php?error=0') );
143                         exit;
144                 }
145
146                 $sidebar = array_diff( $sidebar, array($widget_id) );
147                 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
148         }
149
150         $_POST['widget-id'] = $sidebar;
151
152         foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
153                 if ( $name != $id_base || !is_callable($control['callback']) )
154                         continue;
155
156                 ob_start();
157                         call_user_func_array( $control['callback'], $control['params'] );
158                 ob_end_clean();
159
160                 break;
161         }
162
163         $sidebars_widgets[$sidebar_id] = $sidebar;
164
165         // remove old position
166         if ( !isset($_POST['delete_widget']) ) {
167                 foreach ( $sidebars_widgets as $key => $sb ) {
168                         if ( is_array($sb) )
169                                 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
170                 }
171                 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
172         }
173
174         wp_set_sidebars_widgets($sidebars_widgets);
175         wp_redirect( admin_url('widgets.php?message=0') );
176         exit;
177 }
178
179 // Output the widget form without js
180 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
181         $widget_id = $_GET['editwidget'];
182
183         if ( isset($_GET['addnew']) ) {
184                 // Default to the first sidebar
185                 $sidebar = array_shift( $keys = array_keys($wp_registered_sidebars) );
186
187                 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
188                         // Copy minimal info from an existing instance of this widget to a new instance
189                         foreach ( $wp_registered_widget_controls as $control ) {
190                                 if ( $_GET['base'] === $control['id_base'] ) {
191                                         $control_callback = $control['callback'];
192                                         $multi_number = (int) $_GET['num'];
193                                         $control['params'][0]['number'] = -1;
194                                         $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
195                                         $wp_registered_widget_controls[$control['id']] = $control;
196                                         break;
197                                 }
198                         }
199                 }
200         }
201
202         if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
203                 $control = $wp_registered_widget_controls[$widget_id];
204                 $control_callback = $control['callback'];
205         } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
206                 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
207         }
208
209         if ( !isset($name) )
210                 $name = esc_html( strip_tags($control['name']) );
211
212         if ( !isset($sidebar) )
213                 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
214
215         if ( !isset($multi_number) )
216                 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
217
218         $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
219
220         // show the widget form
221         $width = ' style="width:' . max($control['width'], 350) . 'px"';
222         $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
223
224         require_once( './admin-header.php' ); ?>
225         <div class="wrap">
226         <?php screen_icon(); ?>
227         <h2><?php echo esc_html( $title ); ?></h2>
228         <div class="editwidget"<?php echo $width; ?>>
229         <h3><?php printf( __( 'Widget %s' ), $name ); ?></h3>
230
231         <form action="widgets.php" method="post">
232         <div class="widget-inside">
233 <?php
234         if ( is_callable( $control_callback ) )
235                 call_user_func_array( $control_callback, $control['params'] );
236         else
237                 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
238         </div>
239
240         <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
241         <div class="widget-position">
242         <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
243 <?php
244         foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
245                 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
246                 if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
247                         echo '&nbsp;';
248                 } else {
249                         if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
250                                 $j = 1;
251                                 $sidebars_widgets[$sbname] = array();
252                         } else {
253                                 $j = count($sidebars_widgets[$sbname]);
254                                 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
255                                         $j++;
256                         }
257                         $selected = '';
258                         echo "\t\t<select name='{$sbname}_position'>\n";
259                         echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
260                         for ( $i = 1; $i <= $j; $i++ ) {
261                                 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
262                                         $selected = selected( $i, $key + 1, false );
263                                 echo "\t\t<option value='$i'$selected> $i </option>\n";
264                         }
265                         echo "\t\t</select>\n";
266                 }
267                 echo "</td></tr>\n";
268         } ?>
269         </tbody></table>
270         </div>
271
272         <div class="widget-control-actions">
273 <?php
274         if ( isset($_GET['addnew']) ) { ?>
275         <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
276 <?php
277         } else {
278                 submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
279         }
280         submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
281         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
282         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
283         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
284 <?php   wp_nonce_field("save-delete-widget-$widget_id"); ?>
285         <br class="clear" />
286         </div>
287         </form>
288         </div>
289         </div>
290 <?php
291         require_once( './admin-footer.php' );
292         exit;
293 }
294
295 $messages = array(
296         __('Changes saved.')
297 );
298
299 $errors = array(
300         __('Error while saving.'),
301         __('Error in displaying the widget settings form.')
302 );
303
304 require_once( './admin-header.php' ); ?>
305
306 <div class="wrap">
307 <?php screen_icon(); ?>
308 <h2><?php echo esc_html( $title ); ?></h2>
309
310 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
311 <div id="message" class="updated"><p><?php echo $messages[$_GET['message']]; ?></p></div>
312 <?php } ?>
313 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
314 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
315 <?php } ?>
316
317 <?php do_action( 'widgets_admin_page' ); ?>
318
319 <div class="widget-liquid-left">
320 <div id="widgets-left">
321         <div id="available-widgets" class="widgets-holder-wrap">
322                 <div class="sidebar-name">
323                 <div class="sidebar-name-arrow"><br /></div>
324                 <h3><?php _e('Available Widgets'); ?> <span id="removing-widget"><?php _ex('Deactivate', 'removing-widget'); ?> <span></span></span></h3></div>
325                 <div class="widget-holder">
326                 <p class="description"><?php _e('Drag widgets from here to a sidebar on the right to activate them. Drag widgets back here to deactivate them and delete their settings.'); ?></p>
327                 <div id="widget-list">
328                 <?php wp_list_widgets(); ?>
329                 </div>
330                 <br class='clear' />
331                 </div>
332                 <br class="clear" />
333         </div>
334
335 <?php
336 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
337         if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
338                 $wrap_class = 'widgets-holder-wrap';
339                 if ( !empty( $registered_sidebar['class'] ) )
340                         $wrap_class .= ' ' . $registered_sidebar['class'];
341
342 ?>
343
344                 <div class="<?php echo esc_attr( $wrap_class ); ?>">
345                         <div class="sidebar-name">
346                                 <div class="sidebar-name-arrow"><br /></div>
347                                 <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
348                                         <span class="spinner"></span>
349                                 </h3>
350                         </div>
351                         <div class="widget-holder inactive">
352                                 <?php wp_list_widget_controls( $registered_sidebar['id'] ); ?>
353                                 <div class="clear"></div>
354                         </div>
355                 </div>
356 <?php
357         }
358 }
359 ?>
360
361 </div>
362 </div>
363
364 <div class="widget-liquid-right">
365 <div id="widgets-right">
366 <?php
367 $i = 0;
368 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
369         if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) )
370                 continue;
371
372         $wrap_class = 'widgets-holder-wrap';
373         if ( !empty( $registered_sidebar['class'] ) )
374                 $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
375
376         if ( $i )
377                 $wrap_class .= ' closed'; ?>
378
379         <div class="<?php echo esc_attr( $wrap_class ); ?>">
380         <div class="sidebar-name">
381         <div class="sidebar-name-arrow"><br /></div>
382         <h3><?php echo esc_html( $registered_sidebar['name'] ); ?>
383         <span class="spinner"></span></h3></div>
384         <?php wp_list_widget_controls( $sidebar ); // Show the control forms for each of the widgets in this sidebar ?>
385         </div>
386 <?php
387         $i++;
388 } ?>
389 </div>
390 </div>
391 <form action="" method="post">
392 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
393 </form>
394 <br class="clear" />
395 </div>
396
397 <?php
398 do_action( 'sidebar_admin_page' );
399 require_once( './admin-footer.php' );