]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/widgets.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-admin / widgets.php
1 <?php
2 /**
3  * Widget administration panel
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /** WordPress Administration Bootstrap */
10 require_once( dirname( __FILE__ ) . '/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(
17                 '<h1>' . __( 'Cheatin&#8217; uh?' ) . '</h1>' .
18                 '<p>' . __( 'You are not allowed to edit theme options on this site.' ) . '</p>',
19                 403
20         );
21 }
22
23 $widgets_access = get_user_setting( 'widgets_access' );
24 if ( isset($_GET['widgets-access']) ) {
25         $widgets_access = 'on' == $_GET['widgets-access'] ? 'on' : 'off';
26         set_user_setting( 'widgets_access', $widgets_access );
27 }
28
29 if ( 'on' == $widgets_access ) {
30         add_filter( 'admin_body_class', 'wp_widgets_access_body_class' );
31 } else {
32         wp_enqueue_script('admin-widgets');
33
34         if ( wp_is_mobile() )
35                 wp_enqueue_script( 'jquery-touch-punch' );
36 }
37
38 /**
39  * Fires early before the Widgets administration screen loads,
40  * after scripts are enqueued.
41  *
42  * @since 2.2.0
43  */
44 do_action( 'sidebar_admin_setup' );
45
46 $title = __( 'Widgets' );
47 $parent_file = 'themes.php';
48
49 get_current_screen()->add_help_tab( array(
50 'id'            => 'overview',
51 'title'         => __('Overview'),
52 'content'       =>
53         '<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>
54         <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>'
55 ) );
56 get_current_screen()->add_help_tab( array(
57 'id'            => 'removing-reusing',
58 'title'         => __('Removing and Reusing'),
59 'content'       =>
60         '<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>
61         <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>
62         <p>' . __('Enabling Accessibility Mode, via Screen Options, allows you to use Add and Edit buttons instead of using drag and drop.') . '</p>'
63 ) );
64 get_current_screen()->add_help_tab( array(
65 'id'            => 'missing-widgets',
66 'title'         => __('Missing Widgets'),
67 'content'       =>
68         '<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>' .
69                 '<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>'
70 ) );
71
72 get_current_screen()->set_help_sidebar(
73         '<p><strong>' . __('For more information:') . '</strong></p>' .
74         '<p>' . __('<a href="https://codex.wordpress.org/Appearance_Widgets_Screen" target="_blank">Documentation on Widgets</a>') . '</p>' .
75         '<p>' . __('<a href="https://wordpress.org/support/" target="_blank">Support Forums</a>') . '</p>'
76 );
77
78 if ( ! current_theme_supports( 'widgets' ) ) {
79         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="https://codex.wordpress.org/Widgetizing_Themes">follow these instructions</a>.' ) );
80 }
81
82 // These are the widgets grouped by sidebar
83 $sidebars_widgets = wp_get_sidebars_widgets();
84
85 if ( empty( $sidebars_widgets ) )
86         $sidebars_widgets = wp_get_widget_defaults();
87
88 foreach ( $sidebars_widgets as $sidebar_id => $widgets ) {
89         if ( 'wp_inactive_widgets' == $sidebar_id )
90                 continue;
91
92         if ( ! is_registered_sidebar( $sidebar_id ) ) {
93                 if ( ! empty( $widgets ) ) { // register the inactive_widgets area as sidebar
94                         register_sidebar(array(
95                                 'name' => __( 'Inactive Sidebar (not used)' ),
96                                 'id' => $sidebar_id,
97                                 'class' => 'inactive-sidebar orphan-sidebar',
98                                 '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.' ),
99                                 'before_widget' => '',
100                                 'after_widget' => '',
101                                 'before_title' => '',
102                                 'after_title' => '',
103                         ));
104                 } else {
105                         unset( $sidebars_widgets[ $sidebar_id ] );
106                 }
107         }
108 }
109
110 // register the inactive_widgets area as sidebar
111 register_sidebar(array(
112         'name' => __('Inactive Widgets'),
113         'id' => 'wp_inactive_widgets',
114         'class' => 'inactive-sidebar',
115         'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
116         'before_widget' => '',
117         'after_widget' => '',
118         'before_title' => '',
119         'after_title' => '',
120 ));
121
122 retrieve_widgets();
123
124 // We're saving a widget without js
125 if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
126         $widget_id = $_POST['widget-id'];
127         check_admin_referer("save-delete-widget-$widget_id");
128
129         $number = isset($_POST['multi_number']) ? (int) $_POST['multi_number'] : '';
130         if ( $number ) {
131                 foreach ( $_POST as $key => $val ) {
132                         if ( is_array($val) && preg_match('/__i__|%i%/', key($val)) ) {
133                                 $_POST[$key] = array( $number => array_shift($val) );
134                                 break;
135                         }
136                 }
137         }
138
139         $sidebar_id = $_POST['sidebar'];
140         $position = isset($_POST[$sidebar_id . '_position']) ? (int) $_POST[$sidebar_id . '_position'] - 1 : 0;
141
142         $id_base = $_POST['id_base'];
143         $sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
144
145         // Delete.
146         if ( isset($_POST['removewidget']) && $_POST['removewidget'] ) {
147
148                 if ( !in_array($widget_id, $sidebar, true) ) {
149                         wp_redirect( admin_url('widgets.php?error=0') );
150                         exit;
151                 }
152
153                 $sidebar = array_diff( $sidebar, array($widget_id) );
154                 $_POST = array('sidebar' => $sidebar_id, 'widget-' . $id_base => array(), 'the-widget-id' => $widget_id, 'delete_widget' => '1');
155
156                 /**
157                  * Fires immediately after a widget has been marked for deletion.
158                  *
159                  * @since 4.4.0
160                  *
161                  * @param string $widget_id  ID of the widget marked for deletion.
162                  * @param string $sidebar_id ID of the sidebar the widget was deleted from.
163                  * @param string $id_base    ID base for the widget.
164                  */
165                 do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base );
166         }
167
168         $_POST['widget-id'] = $sidebar;
169
170         foreach ( (array) $wp_registered_widget_updates as $name => $control ) {
171                 if ( $name != $id_base || !is_callable($control['callback']) )
172                         continue;
173
174                 ob_start();
175                         call_user_func_array( $control['callback'], $control['params'] );
176                 ob_end_clean();
177
178                 break;
179         }
180
181         $sidebars_widgets[$sidebar_id] = $sidebar;
182
183         // Remove old position.
184         if ( !isset($_POST['delete_widget']) ) {
185                 foreach ( $sidebars_widgets as $key => $sb ) {
186                         if ( is_array($sb) )
187                                 $sidebars_widgets[$key] = array_diff( $sb, array($widget_id) );
188                 }
189                 array_splice( $sidebars_widgets[$sidebar_id], $position, 0, $widget_id );
190         }
191
192         wp_set_sidebars_widgets($sidebars_widgets);
193         wp_redirect( admin_url('widgets.php?message=0') );
194         exit;
195 }
196
197 // Remove inactive widgets without js
198 if ( isset( $_POST['removeinactivewidgets'] ) ) {
199         check_admin_referer( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' );
200
201         if ( $_POST['removeinactivewidgets'] ) {
202                 foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) {
203                         $pieces = explode( '-', $widget_id );
204                         $multi_number = array_pop( $pieces );
205                         $id_base = implode( '-', $pieces );
206                         $widget = get_option( 'widget_' . $id_base );
207                         unset( $widget[$multi_number] );
208                         update_option( 'widget_' . $id_base, $widget );
209                         unset( $sidebars_widgets['wp_inactive_widgets'][$key] );
210                 }
211
212                 wp_set_sidebars_widgets( $sidebars_widgets );
213         }
214
215         wp_redirect( admin_url( 'widgets.php?message=0' ) );
216         exit;
217 }
218
219 // Output the widget form without js
220 if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
221         $widget_id = $_GET['editwidget'];
222
223         if ( isset($_GET['addnew']) ) {
224                 // Default to the first sidebar
225                 $keys = array_keys( $wp_registered_sidebars );
226                 $sidebar = reset( $keys );
227
228                 if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
229                         // Copy minimal info from an existing instance of this widget to a new instance
230                         foreach ( $wp_registered_widget_controls as $control ) {
231                                 if ( $_GET['base'] === $control['id_base'] ) {
232                                         $control_callback = $control['callback'];
233                                         $multi_number = (int) $_GET['num'];
234                                         $control['params'][0]['number'] = -1;
235                                         $widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
236                                         $wp_registered_widget_controls[$control['id']] = $control;
237                                         break;
238                                 }
239                         }
240                 }
241         }
242
243         if ( isset($wp_registered_widget_controls[$widget_id]) && !isset($control) ) {
244                 $control = $wp_registered_widget_controls[$widget_id];
245                 $control_callback = $control['callback'];
246         } elseif ( !isset($wp_registered_widget_controls[$widget_id]) && isset($wp_registered_widgets[$widget_id]) ) {
247                 $name = esc_html( strip_tags($wp_registered_widgets[$widget_id]['name']) );
248         }
249
250         if ( !isset($name) )
251                 $name = esc_html( strip_tags($control['name']) );
252
253         if ( !isset($sidebar) )
254                 $sidebar = isset($_GET['sidebar']) ? $_GET['sidebar'] : 'wp_inactive_widgets';
255
256         if ( !isset($multi_number) )
257                 $multi_number = isset($control['params'][0]['number']) ? $control['params'][0]['number'] : '';
258
259         $id_base = isset($control['id_base']) ? $control['id_base'] : $control['id'];
260
261         // Show the widget form.
262         $width = ' style="width:' . max($control['width'], 350) . 'px"';
263         $key = isset($_GET['key']) ? (int) $_GET['key'] : 0;
264
265         require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
266         <div class="wrap">
267         <h1><?php echo esc_html( $title ); ?></h1>
268         <div class="editwidget"<?php echo $width; ?>>
269         <h2><?php printf( __( 'Widget %s' ), $name ); ?></h2>
270
271         <form action="widgets.php" method="post">
272         <div class="widget-inside">
273 <?php
274         if ( is_callable( $control_callback ) )
275                 call_user_func_array( $control_callback, $control['params'] );
276         else
277                 echo '<p>' . __('There are no options for this widget.') . "</p>\n"; ?>
278         </div>
279
280         <p class="describe"><?php _e('Select both the sidebar for this widget and the position of the widget in that sidebar.'); ?></p>
281         <div class="widget-position">
282         <table class="widefat"><thead><tr><th><?php _e('Sidebar'); ?></th><th><?php _e('Position'); ?></th></tr></thead><tbody>
283 <?php
284         foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) {
285                 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr($sbname) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>";
286                 if ( 'wp_inactive_widgets' == $sbname || 'orphaned_widgets' == substr( $sbname, 0, 16 ) ) {
287                         echo '&nbsp;';
288                 } else {
289                         if ( !isset($sidebars_widgets[$sbname]) || !is_array($sidebars_widgets[$sbname]) ) {
290                                 $j = 1;
291                                 $sidebars_widgets[$sbname] = array();
292                         } else {
293                                 $j = count($sidebars_widgets[$sbname]);
294                                 if ( isset($_GET['addnew']) || !in_array($widget_id, $sidebars_widgets[$sbname], true) )
295                                         $j++;
296                         }
297                         $selected = '';
298                         echo "\t\t<select name='{$sbname}_position'>\n";
299                         echo "\t\t<option value=''>" . __('&mdash; Select &mdash;') . "</option>\n";
300                         for ( $i = 1; $i <= $j; $i++ ) {
301                                 if ( in_array($widget_id, $sidebars_widgets[$sbname], true) )
302                                         $selected = selected( $i, $key + 1, false );
303                                 echo "\t\t<option value='$i'$selected> $i </option>\n";
304                         }
305                         echo "\t\t</select>\n";
306                 }
307                 echo "</td></tr>\n";
308         } ?>
309         </tbody></table>
310         </div>
311
312         <div class="widget-control-actions">
313 <?php
314         if ( isset($_GET['addnew']) ) { ?>
315         <a href="widgets.php" class="button alignleft"><?php _e('Cancel'); ?></a>
316 <?php
317         } else {
318                 submit_button( __( 'Delete' ), 'button alignleft', 'removewidget', false );
319         }
320         submit_button( __( 'Save Widget' ), 'button-primary alignright', 'savewidget', false ); ?>
321         <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($widget_id); ?>" />
322         <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
323         <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr($multi_number); ?>" />
324 <?php   wp_nonce_field("save-delete-widget-$widget_id"); ?>
325         <br class="clear" />
326         </div>
327         </form>
328         </div>
329         </div>
330 <?php
331         require_once( ABSPATH . 'wp-admin/admin-footer.php' );
332         exit;
333 }
334
335 $messages = array(
336         __('Changes saved.')
337 );
338
339 $errors = array(
340         __('Error while saving.'),
341         __('Error in displaying the widget settings form.')
342 );
343
344 require_once( ABSPATH . 'wp-admin/admin-header.php' ); ?>
345
346 <div class="wrap">
347 <h1>
348 <?php
349         echo esc_html( $title );
350         if ( current_user_can( 'customize' ) ) {
351                 printf(
352                         ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>',
353                         esc_url( add_query_arg(
354                                 array(
355                                         array( 'autofocus' => array( 'panel' => 'widgets' ) ),
356                                         'return' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) )
357                                 ),
358                                 admin_url( 'customize.php' )
359                         ) ),
360                         __( 'Manage in Customizer' )
361                 );
362         }
363 ?>
364 </h1>
365
366 <?php if ( isset($_GET['message']) && isset($messages[$_GET['message']]) ) { ?>
367 <div id="message" class="updated notice is-dismissible"><p><?php echo $messages[$_GET['message']]; ?></p></div>
368 <?php } ?>
369 <?php if ( isset($_GET['error']) && isset($errors[$_GET['error']]) ) { ?>
370 <div id="message" class="error"><p><?php echo $errors[$_GET['error']]; ?></p></div>
371 <?php } ?>
372
373 <?php
374 /**
375  * Fires before the Widgets administration page content loads.
376  *
377  * @since 3.0.0
378  */
379 do_action( 'widgets_admin_page' ); ?>
380
381 <div class="widget-liquid-left">
382 <div id="widgets-left">
383         <div id="available-widgets" class="widgets-holder-wrap">
384                 <div class="sidebar-name">
385                         <div class="sidebar-name-arrow"><br /></div>
386                         <h2><?php _e( 'Available Widgets' ); ?> <span id="removing-widget"><?php _ex( 'Deactivate', 'removing-widget' ); ?> <span></span></span></h2>
387                 </div>
388                 <div class="widget-holder">
389                         <div class="sidebar-description">
390                                 <p class="description"><?php _e('To activate a widget drag it to a sidebar or click on it. To deactivate a widget and delete its settings, drag it back.'); ?></p>
391                         </div>
392                         <div id="widget-list">
393                                 <?php wp_list_widgets(); ?>
394                         </div>
395                         <br class='clear' />
396                 </div>
397                 <br class="clear" />
398         </div>
399
400 <?php
401
402 $theme_sidebars = array();
403 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
404         if ( false !== strpos( $registered_sidebar['class'], 'inactive-sidebar' ) || 'orphaned_widgets' == substr( $sidebar, 0, 16 ) ) {
405                 $wrap_class = 'widgets-holder-wrap';
406                 if ( !empty( $registered_sidebar['class'] ) )
407                         $wrap_class .= ' ' . $registered_sidebar['class'];
408
409                 ?>
410                 <div class="<?php echo esc_attr( $wrap_class ); ?>">
411                         <div class="widget-holder inactive">
412                                 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
413                                 <div class="remove-inactive-widgets">
414                                         <form action="" method="post">
415                                                 <p>
416                                                         <?php
417                                                         $attributes = array( 'id' => 'inactive-widgets-control-remove' );
418
419                                                         if ( empty($sidebars_widgets['wp_inactive_widgets']) ) {
420                                                                 $attributes['disabled'] = '';
421                                                         }
422
423                                                         submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes );
424                                                         ?>
425                                                         <span class="spinner">
426                                                 </p>
427                                                 <?php wp_nonce_field( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); ?>
428                                         </form>
429                                 </div>
430                         </div>
431                         <p class="description"><?php _e( 'This will clear all items from the inactive widgets list. You will not be able to restore any customizations.' ); ?></p>
432                 </div>
433                 <?php
434
435         } else {
436                 $theme_sidebars[$sidebar] = $registered_sidebar;
437         }
438 }
439
440 ?>
441 </div>
442 </div>
443 <?php
444
445 $i = $split = 0;
446 $single_sidebar_class = '';
447 $sidebars_count = count( $theme_sidebars );
448
449 if ( $sidebars_count > 1 ) {
450         $split = ceil( $sidebars_count / 2 );
451 } else {
452         $single_sidebar_class = ' class="single-sidebar"';
453 }
454
455 ?>
456 <div class="widget-liquid-right">
457 <div id="widgets-right"<?php echo $single_sidebar_class; ?>>
458 <div class="sidebars-column-1">
459 <?php
460
461 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) {
462         $wrap_class = 'widgets-holder-wrap';
463         if ( !empty( $registered_sidebar['class'] ) )
464                 $wrap_class .= ' sidebar-' . $registered_sidebar['class'];
465
466         if ( $i > 0 )
467                 $wrap_class .= ' closed';
468
469         if ( $split && $i == $split ) {
470                 ?>
471                 </div><div class="sidebars-column-2">
472                 <?php
473         }
474
475         ?>
476         <div class="<?php echo esc_attr( $wrap_class ); ?>">
477                 <?php wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); // Show the control forms for each of the widgets in this sidebar ?>
478         </div>
479         <?php
480
481         $i++;
482 }
483
484 ?>
485 </div>
486 </div>
487 </div>
488 <form method="post">
489 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
490 </form>
491 <br class="clear" />
492 </div>
493
494 <div class="widgets-chooser">
495         <ul class="widgets-chooser-sidebars"></ul>
496         <div class="widgets-chooser-actions">
497                 <button class="button-secondary"><?php _e( 'Cancel' ); ?></button>
498                 <button class="button-primary"><?php _e( 'Add Widget' ); ?></button>
499         </div>
500 </div>
501
502 <?php
503
504 /**
505  * Fires after the available widgets and sidebars have loaded, before the admin footer.
506  *
507  * @since 2.2.0
508  */
509 do_action( 'sidebar_admin_page' );
510 require_once( ABSPATH . 'wp-admin/admin-footer.php' );