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