diff --git a/src/wp-admin/admin-ajax.php b/src/wp-admin/admin-ajax.php index 238809227c..64915aa7e4 100644 --- a/src/wp-admin/admin-ajax.php +++ b/src/wp-admin/admin-ajax.php @@ -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', diff --git a/src/wp-admin/css/widgets.css b/src/wp-admin/css/widgets.css index 2c10fbe64e..85cdf4ce6c 100644 --- a/src/wp-admin/css/widgets.css +++ b/src/wp-admin/css/widgets.css @@ -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; } diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 9955a421ed..0170cd9d70 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -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 * diff --git a/src/wp-admin/js/widgets.js b/src/wp-admin/js/widgets.js index e18157fa57..de64533bdf 100644 --- a/src/wp-admin/js/widgets.js +++ b/src/wp-admin/js/widgets.js @@ -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() || ''; diff --git a/src/wp-admin/widgets.php b/src/wp-admin/widgets.php index 6dc662afc0..3b43ac06d2 100644 --- a/src/wp-admin/widgets.php +++ b/src/wp-admin/widgets.php @@ -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,8 +410,25 @@ foreach ( $wp_registered_sidebars as $sidebar => $registered_sidebar ) {
-
+
+
+

+ 'inactive-widgets-control-remove' ); + + if ( empty($sidebars_widgets['wp_inactive_widgets']) ) { + $attributes['disabled'] = ''; + } + + submit_button( __( 'Clear Inactive Widgets' ), 'delete', 'removeinactivewidgets', false, $attributes ); + ?> + +

+ +
+
+