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