[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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. 10 if ( ! defined( 'ABSPATH' ) ) { 11 die( '-1' ); 12 } 13 14 $widgets_access = get_user_setting( 'widgets_access' ); 15 if ( 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 22 if ( '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 */ 38 do_action( 'sidebar_admin_setup' ); 39 40 get_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 ); 49 get_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 ); 59 get_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 69 get_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 78 if ( empty( $sidebars_widgets ) ) { 79 $sidebars_widgets = wp_get_widget_defaults(); 80 } 81 82 foreach ( $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. 108 register_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 121 retrieve_widgets(); 122 123 // We're saving a widget without JS. 124 if ( 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 $key => $sb ) { 191 if ( is_array( $sb ) ) { 192 $sidebars_widgets[ $key ] = array_diff( $sb, array( $widget_id ) ); 193 } 194 } 195 array_splice( $sidebars_widgets[ $sidebar_id ], $position, 0, $widget_id ); 196 } 197 198 wp_set_sidebars_widgets( $sidebars_widgets ); 199 wp_redirect( admin_url( 'widgets.php?message=0' ) ); 200 exit; 201 } 202 203 // Remove inactive widgets without JS. 204 if ( isset( $_POST['removeinactivewidgets'] ) ) { 205 check_admin_referer( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); 206 207 if ( $_POST['removeinactivewidgets'] ) { 208 foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) { 209 $pieces = explode( '-', $widget_id ); 210 $multi_number = array_pop( $pieces ); 211 $id_base = implode( '-', $pieces ); 212 $widget = get_option( 'widget_' . $id_base ); 213 unset( $widget[ $multi_number ] ); 214 update_option( 'widget_' . $id_base, $widget ); 215 unset( $sidebars_widgets['wp_inactive_widgets'][ $key ] ); 216 } 217 218 wp_set_sidebars_widgets( $sidebars_widgets ); 219 } 220 221 wp_redirect( admin_url( 'widgets.php?message=0' ) ); 222 exit; 223 } 224 225 // Output the widget form without JS. 226 if ( isset( $_GET['editwidget'] ) && $_GET['editwidget'] ) { 227 $widget_id = $_GET['editwidget']; 228 229 if ( isset( $_GET['addnew'] ) ) { 230 // Default to the first sidebar. 231 $keys = array_keys( $wp_registered_sidebars ); 232 $sidebar = reset( $keys ); 233 234 if ( isset( $_GET['base'] ) && isset( $_GET['num'] ) ) { // Multi-widget. 235 // Copy minimal info from an existing instance of this widget to a new instance. 236 foreach ( $wp_registered_widget_controls as $control ) { 237 if ( $_GET['base'] === $control['id_base'] ) { 238 $control_callback = $control['callback']; 239 $multi_number = (int) $_GET['num']; 240 $control['params'][0]['number'] = -1; 241 $control['id'] = $control['id_base'] . '-' . $multi_number; 242 $widget_id = $control['id']; 243 $wp_registered_widget_controls[ $control['id'] ] = $control; 244 break; 245 } 246 } 247 } 248 } 249 250 if ( isset( $wp_registered_widget_controls[ $widget_id ] ) && ! isset( $control ) ) { 251 $control = $wp_registered_widget_controls[ $widget_id ]; 252 $control_callback = $control['callback']; 253 } elseif ( ! isset( $wp_registered_widget_controls[ $widget_id ] ) && isset( $wp_registered_widgets[ $widget_id ] ) ) { 254 $name = esc_html( strip_tags( $wp_registered_widgets[ $widget_id ]['name'] ) ); 255 } 256 257 if ( ! isset( $name ) ) { 258 $name = esc_html( strip_tags( $control['name'] ) ); 259 } 260 261 if ( ! isset( $sidebar ) ) { 262 $sidebar = isset( $_GET['sidebar'] ) ? $_GET['sidebar'] : 'wp_inactive_widgets'; 263 } 264 265 if ( ! isset( $multi_number ) ) { 266 $multi_number = isset( $control['params'][0]['number'] ) ? $control['params'][0]['number'] : ''; 267 } 268 269 $id_base = isset( $control['id_base'] ) ? $control['id_base'] : $control['id']; 270 271 // Show the widget form. 272 $width = ' style="width:' . max( $control['width'], 350 ) . 'px"'; 273 $key = isset( $_GET['key'] ) ? (int) $_GET['key'] : 0; 274 275 require_once ABSPATH . 'wp-admin/admin-header.php'; 276 ?> 277 <div class="wrap"> 278 <h1><?php echo esc_html( $title ); ?></h1> 279 <div class="editwidget"<?php echo $width; ?>> 280 <h2> 281 <?php 282 /* translators: %s: Widget name. */ 283 printf( __( 'Widget %s' ), $name ); 284 ?> 285 </h2> 286 287 <form action="widgets.php" method="post"> 288 <div class="widget-inside"> 289 <?php 290 if ( is_callable( $control_callback ) ) { 291 call_user_func_array( $control_callback, $control['params'] ); 292 } else { 293 echo '<p>' . __( 'There are no options for this widget.' ) . "</p>\n"; 294 } 295 ?> 296 </div> 297 298 <p class="describe"><?php _e( 'Select both the sidebar for this widget and the position of the widget in that sidebar.' ); ?></p> 299 <div class="widget-position"> 300 <table class="widefat"><thead><tr><th><?php _e( 'Sidebar' ); ?></th><th><?php _e( 'Position' ); ?></th></tr></thead><tbody> 301 <?php 302 foreach ( $wp_registered_sidebars as $sbname => $sbvalue ) { 303 echo "\t\t<tr><td><label><input type='radio' name='sidebar' value='" . esc_attr( $sbname ) . "'" . checked( $sbname, $sidebar, false ) . " /> $sbvalue[name]</label></td><td>"; 304 if ( 'wp_inactive_widgets' === $sbname || str_starts_with( $sbname, 'orphaned_widgets' ) ) { 305 echo ' '; 306 } else { 307 if ( ! isset( $sidebars_widgets[ $sbname ] ) || ! is_array( $sidebars_widgets[ $sbname ] ) ) { 308 $j = 1; 309 $sidebars_widgets[ $sbname ] = array(); 310 } else { 311 $j = count( $sidebars_widgets[ $sbname ] ); 312 if ( isset( $_GET['addnew'] ) || ! in_array( $widget_id, $sidebars_widgets[ $sbname ], true ) ) { 313 ++$j; 314 } 315 } 316 $selected = ''; 317 echo "\t\t<select name='{$sbname}_position'>\n"; 318 echo "\t\t<option value=''>" . __( '— Select —' ) . "</option>\n"; 319 for ( $i = 1; $i <= $j; $i++ ) { 320 if ( in_array( $widget_id, $sidebars_widgets[ $sbname ], true ) ) { 321 $selected = selected( $i, $key + 1, false ); 322 } 323 echo "\t\t<option value='$i'$selected> $i </option>\n"; 324 } 325 echo "\t\t</select>\n"; 326 } 327 echo "</td></tr>\n"; 328 } 329 ?> 330 </tbody></table> 331 </div> 332 333 <div class="widget-control-actions"> 334 <div class="alignleft"> 335 <?php if ( ! isset( $_GET['addnew'] ) ) : ?> 336 <input type="submit" name="removewidget" id="removewidget" class="button-link button-link-delete widget-control-remove" value="<?php esc_attr_e( 'Delete' ); ?>" /> 337 <span class="widget-control-close-wrapper"> 338 | <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 339 </span> 340 <?php else : ?> 341 <a href="widgets.php" class="button-link widget-control-close"><?php _e( 'Cancel' ); ?></a> 342 <?php endif; ?> 343 </div> 344 <div class="alignright"> 345 <?php submit_button( __( 'Save Widget' ), 'primary alignright', 'savewidget', false ); ?> 346 <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr( $widget_id ); ?>" /> 347 <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr( $id_base ); ?>" /> 348 <input type="hidden" name="multi_number" class="multi_number" value="<?php echo esc_attr( $multi_number ); ?>" /> 349 <?php wp_nonce_field( "save-delete-widget-$widget_id" ); ?> 350 </div> 351 <br class="clear" /> 352 </div> 353 354 </form> 355 </div> 356 </div> 357 <?php 358 require_once ABSPATH . 'wp-admin/admin-footer.php'; 359 exit; 360 } 361 362 $messages = array( 363 __( 'Changes saved.' ), 364 ); 365 366 $errors = array( 367 __( 'Error while saving.' ), 368 __( 'Error in displaying the widget settings form.' ), 369 ); 370 371 require_once ABSPATH . 'wp-admin/admin-header.php'; 372 ?> 373 374 <div class="wrap"> 375 <h1 class="wp-heading-inline"> 376 <?php 377 echo esc_html( $title ); 378 ?> 379 </h1> 380 381 <?php 382 if ( current_user_can( 'customize' ) ) { 383 printf( 384 ' <a class="page-title-action hide-if-no-customize" href="%1$s">%2$s</a>', 385 esc_url( 386 add_query_arg( 387 array( 388 array( 'autofocus' => array( 'panel' => 'widgets' ) ), 389 'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 390 ), 391 admin_url( 'customize.php' ) 392 ) 393 ), 394 __( 'Manage with Live Preview' ) 395 ); 396 } 397 398 $nonce = wp_create_nonce( 'widgets-access' ); 399 ?> 400 <div class="widget-access-link"> 401 <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> 402 </div> 403 404 <hr class="wp-header-end"> 405 406 <?php 407 if ( isset( $_GET['message'] ) && isset( $messages[ $_GET['message'] ] ) ) { 408 wp_admin_notice( 409 $messages[ $_GET['message'] ], 410 array( 411 'id' => 'message', 412 'additional_classes' => array( 'updated' ), 413 'dismissible' => true, 414 ) 415 ); 416 } 417 if ( isset( $_GET['error'] ) && isset( $errors[ $_GET['error'] ] ) ) { 418 wp_admin_notice( 419 $errors[ $_GET['error'] ], 420 array( 421 'id' => 'message', 422 'additional_classes' => array( 'error' ), 423 'dismissible' => true, 424 ) 425 ); 426 } 427 428 /** 429 * Fires before the Widgets administration page content loads. 430 * 431 * @since 3.0.0 432 */ 433 do_action( 'widgets_admin_page' ); 434 ?> 435 436 <div class="widget-liquid-left"> 437 <div id="widgets-left"> 438 <div id="available-widgets" class="widgets-holder-wrap"> 439 <div class="sidebar-name"> 440 <button type="button" class="handlediv hide-if-no-js" aria-expanded="true"> 441 <span class="screen-reader-text"> 442 <?php 443 /* translators: Hidden accessibility text. */ 444 _e( 'Available Widgets' ); 445 ?> 446 </span> 447 <span class="toggle-indicator" aria-hidden="true"></span> 448 </button> 449 <h2><?php _e( 'Available Widgets' ); ?> <span id="removing-widget"><?php _ex( 'Deactivate', 'removing-widget' ); ?> <span></span></span></h2> 450 </div> 451 <div class="widget-holder"> 452 <div class="sidebar-description"> 453 <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> 454 </div> 455 <div id="widget-list"> 456 <?php wp_list_widgets(); ?> 457 </div> 458 <br class='clear' /> 459 </div> 460 <br class="clear" /> 461 </div> 462 463 <?php 464 465 $theme_sidebars = array(); 466 foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) { 467 if ( str_contains( $registered_sidebar['class'], 'inactive-sidebar' ) || str_starts_with( $sidebar, 'orphaned_widgets' ) ) { 468 $wrap_class = 'widgets-holder-wrap'; 469 if ( ! empty( $registered_sidebar['class'] ) ) { 470 $wrap_class .= ' ' . $registered_sidebar['class']; 471 } 472 473 $is_inactive_widgets = 'wp_inactive_widgets' === $registered_sidebar['id']; 474 ?> 475 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 476 <div class="widget-holder inactive"> 477 <?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?> 478 479 <?php if ( $is_inactive_widgets ) { ?> 480 <div class="remove-inactive-widgets"> 481 <form method="post"> 482 <p> 483 <?php 484 $attributes = array( 'id' => 'inactive-widgets-control-remove' ); 485 486 if ( empty( $sidebars_widgets['wp_inactive_widgets'] ) ) { 487 $attributes['disabled'] = ''; 488 } 489 490 submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes ); 491 ?> 492 <span class="spinner"></span> 493 </p> 494 <?php wp_nonce_field( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); ?> 495 </form> 496 </div> 497 <?php } ?> 498 </div> 499 <?php if ( $is_inactive_widgets ) { ?> 500 <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> 501 <?php } ?> 502 </div> 503 <?php 504 505 } else { 506 $theme_sidebars[ $sidebar ] = $registered_sidebar; 507 } 508 } 509 510 ?> 511 </div> 512 </div> 513 <?php 514 515 $i = 0; 516 $split = 0; 517 $single_sidebar_class = ''; 518 $sidebars_count = count( $theme_sidebars ); 519 520 if ( $sidebars_count > 1 ) { 521 $split = (int) ceil( $sidebars_count / 2 ); 522 } else { 523 $single_sidebar_class = ' single-sidebar'; 524 } 525 526 ?> 527 <div class="widget-liquid-right"> 528 <div id="widgets-right" class="wp-clearfix<?php echo $single_sidebar_class; ?>"> 529 <div class="sidebars-column-1"> 530 <?php 531 532 foreach ( $theme_sidebars as $sidebar => $registered_sidebar ) { 533 $wrap_class = 'widgets-holder-wrap'; 534 if ( ! empty( $registered_sidebar['class'] ) ) { 535 $wrap_class .= ' sidebar-' . $registered_sidebar['class']; 536 } 537 538 if ( $i > 0 ) { 539 $wrap_class .= ' closed'; 540 } 541 542 if ( $split && $i === $split ) { 543 ?> 544 </div><div class="sidebars-column-2"> 545 <?php 546 } 547 548 ?> 549 <div class="<?php echo esc_attr( $wrap_class ); ?>"> 550 <?php 551 // Show the control forms for each of the widgets in this sidebar. 552 wp_list_widget_controls( $sidebar, $registered_sidebar['name'] ); 553 ?> 554 </div> 555 <?php 556 557 ++$i; 558 } 559 560 ?> 561 </div> 562 </div> 563 </div> 564 <form method="post"> 565 <?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?> 566 </form> 567 <br class="clear" /> 568 </div> 569 570 <div class="widgets-chooser"> 571 <ul class="widgets-chooser-sidebars"></ul> 572 <div class="widgets-chooser-actions"> 573 <button class="button widgets-chooser-cancel"><?php _e( 'Cancel' ); ?></button> 574 <button class="button button-primary widgets-chooser-add"><?php _e( 'Add Widget' ); ?></button> 575 </div> 576 </div> 577 578 <?php 579 580 /** 581 * Fires after the available widgets and sidebars have loaded, before the admin footer. 582 * 583 * @since 2.2.0 584 */ 585 do_action( 'sidebar_admin_page' ); 586 require_once ABSPATH . 'wp-admin/admin-footer.php';
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |