Widgets: add a button to delete all inactive widgets simultaneously for better UX.

Props cdog.
Fixes #19159.

git-svn-id: https://develop.svn.wordpress.org/trunk@35317 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2015-10-21 05:22:52 +00:00
parent ba1177bf4c
commit 7ba408c93e
5 changed files with 111 additions and 3 deletions

View File

@ -56,7 +56,7 @@ $core_actions_post = array(
'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
'save-widget', 'set-post-thumbnail', 'date_format', 'time_format',
'save-widget', 'delete-inactive-widgets', 'set-post-thumbnail', 'date_format', 'time_format',
'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',

View File

@ -272,7 +272,8 @@ div#widgets-right .closed .widgets-sortables {
margin-bottom: 0;
}
.sidebar-name .spinner {
.sidebar-name .spinner,
.remove-inactive-widgets .spinner {
float: none;
position: relative;
top: -2px;
@ -348,6 +349,7 @@ div#widgets-right .closed .widgets-sortables {
.js .widgets-holder-wrap.closed .widget,
.js .widgets-holder-wrap.closed .sidebar-description,
.js .widgets-holder-wrap.closed .remove-inactive-widgets,
.js .closed br.clear {
display: none;
}

View File

@ -1933,6 +1933,41 @@ function wp_ajax_update_widget() {
$wp_customize->widgets->wp_ajax_update_widget();
}
/**
* Ajax handler for removing inactive widgets.
*
* @since 4.4.0
*/
function wp_ajax_delete_inactive_widgets() {
check_ajax_referer( 'remove-inactive-widgets', 'removeinactivewidgets' );
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_die( -1 );
}
unset( $_POST['removeinactivewidgets'], $_POST['action'] );
do_action( 'load-widgets.php' );
do_action( 'widgets.php' );
do_action( 'sidebar_admin_setup' );
$sidebars_widgets = wp_get_sidebars_widgets();
foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) {
$pieces = explode( '-', $widget_id );
$multi_number = array_pop( $pieces );
$id_base = implode( '-', $pieces );
$widget = get_option( 'widget_' . $id_base );
unset( $widget[$multi_number] );
update_option( 'widget_' . $id_base, $widget );
unset( $sidebars_widgets['wp_inactive_widgets'][$key] );
}
wp_set_sidebars_widgets( $sidebars_widgets );
wp_die();
}
/**
* Ajax handler for uploading attachments
*

View File

@ -80,6 +80,9 @@ wpWidgets = {
widget.removeClass( 'open' );
wpWidgets.close( widget );
e.preventDefault();
} else if ( target.attr( 'id' ) === 'inactive-widgets-control-remove' ) {
wpWidgets.removeInactiveWidgets();
e.preventDefault();
}
});
@ -387,6 +390,7 @@ wpWidgets = {
});
$.post( ajaxurl, data, function() {
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
$( '.spinner' ).removeClass( 'is-active' );
});
},
@ -431,6 +435,10 @@ wpWidgets = {
});
} else {
widget.remove();
if ( sidebarId === 'wp_inactive_widgets' ) {
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
}
}
} else {
$( '.spinner' ).removeClass( 'is-active' );
@ -438,14 +446,38 @@ wpWidgets = {
$( 'div.widget-content', widget ).html( r );
wpWidgets.appendTitle( widget );
$document.trigger( 'widget-updated', [ widget ] );
if ( sidebarId === 'wp_inactive_widgets' ) {
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , ! $( '#wp_inactive_widgets .widget' ).length );
}
}
}
if ( order ) {
wpWidgets.saveOrder();
}
});
},
removeInactiveWidgets : function() {
var $element = $( '.remove-inactive-widgets' ), a, data;
$( '.spinner', $element ).addClass( 'is-active' );
a = {
action : 'delete-inactive-widgets',
removeinactivewidgets : $( '#_wpnonce_remove_inactive_widgets' ).val()
};
data = $.param( a );
$.post( ajaxurl, data, function() {
$( '#wp_inactive_widgets .widget' ).remove();
$( '#inactive-widgets-control-remove' ).prop( 'disabled' , true );
$( '.spinner', $element ).removeClass( 'is-active' );
} );
},
appendTitle : function(widget) {
var title = $('input[id*="-title"]', widget).val() || '';

View File

@ -194,6 +194,28 @@ if ( isset($_POST['savewidget']) || isset($_POST['removewidget']) ) {
exit;
}
// Remove inactive widgets without js
if ( isset( $_POST['removeinactivewidgets'] ) ) {
check_admin_referer( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' );
if ( $_POST['removeinactivewidgets'] ) {
foreach ( $sidebars_widgets['wp_inactive_widgets'] as $key => $widget_id ) {
$pieces = explode( '-', $widget_id );
$multi_number = array_pop( $pieces );
$id_base = implode( '-', $pieces );
$widget = get_option( 'widget_' . $id_base );
unset( $widget[$multi_number] );
update_option( 'widget_' . $id_base, $widget );
unset( $sidebars_widgets['wp_inactive_widgets'][$key] );
}
wp_set_sidebars_widgets( $sidebars_widgets );
}
wp_redirect( admin_url( 'widgets.php?message=0' ) );
exit;
}
// Output the widget form without js
if ( isset($_GET['editwidget']) && $_GET['editwidget'] ) {
$widget_id = $_GET['editwidget'];
@ -388,9 +410,26 @@ foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
<div class="<?php echo esc_attr( $wrap_class ); ?>">
<div class="widget-holder inactive">
<?php wp_list_widget_controls( $registered_sidebar['id'], $registered_sidebar['name'] ); ?>
<div class="clear"></div>
<div class="remove-inactive-widgets">
<form action="" method="post">
<p>
<?php
$attributes = array( 'id' => 'inactive-widgets-control-remove' );
if ( empty($sidebars_widgets['wp_inactive_widgets']) ) {
$attributes['disabled'] = '';
}
submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes );
?>
<span class="spinner">
</p>
<?php wp_nonce_field( 'remove-inactive-widgets', '_wpnonce_remove_inactive_widgets' ); ?>
</form>
</div>
</div>
<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>
</div>
<?php
} else {