@@ -478,6 +478,9 @@ public function delete( $args, $assoc_args ) {
478478 * [--all]
479479 * : If set, all sidebars will be reset.
480480 *
481+ * [--inactive]
482+ * : If set, only inactive sidebars will be reset.
483+ *
481484 * ## EXAMPLES
482485 *
483486 * # Reset a sidebar
@@ -494,37 +497,60 @@ public function delete( $args, $assoc_args ) {
494497 * Success: Sidebar 'sidebar-1' reset.
495498 * Success: Sidebar 'sidebar-2' reset.
496499 * Success: Sidebar 'sidebar-3' reset.
500+ *
501+ * # Reset all inactive sidebars
502+ * $ wp widget reset --inactive
503+ * Success: Sidebar 'old-sidebar-1' reset.
497504 */
498505 public function reset ( $ args , $ assoc_args ) {
499506
500507 global $ wp_registered_sidebars ;
501508
502- $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
509+ $ all = Utils \get_flag_value ( $ assoc_args , 'all ' , false );
510+ $ inactive = Utils \get_flag_value ( $ assoc_args , 'inactive ' , false );
503511
504- // Bail if no arguments and no all flag.
505- if ( ! $ all && empty ( $ args ) ) {
506- WP_CLI ::error ( 'Please specify one or more sidebars, or use --all. ' );
512+ // Bail if no arguments and no -- all or --inactive flag.
513+ if ( ! $ all && ! $ inactive && empty ( $ args ) ) {
514+ WP_CLI ::error ( 'Please specify one or more sidebars, or use --all or --inactive . ' );
507515 }
508516
509- // Fetch all sidebars if all flag is set.
517+ // Fetch all registered sidebars if -- all flag is set.
510518 if ( $ all ) {
511519 $ args = array_keys ( $ wp_registered_sidebars );
512520 }
513521
514522 // Sidebar ID wp_inactive_widgets is reserved by WP core for inactive widgets.
515- if ( isset ( $ args ['wp_inactive_widgets ' ] ) ) {
516- unset( $ args ['wp_inactive_widgets ' ] );
523+ $ args = array_values (
524+ array_filter (
525+ $ args ,
526+ static function ( $ id ) {
527+ return 'wp_inactive_widgets ' !== $ id ;
528+ }
529+ )
530+ );
531+
532+ // Collect inactive (unregistered) sidebar IDs if --inactive flag is set.
533+ $ inactive_args = [];
534+ if ( $ inactive ) {
535+ $ inactive_args = $ this ->get_inactive_sidebar_ids ();
517536 }
518537
519- // Check if no registered sidebar.
520- if ( empty ( $ args ) ) {
538+ $ all_args = array_merge ( $ args , $ inactive_args );
539+
540+ // Check if there are no sidebars to reset.
541+ if ( empty ( $ all_args ) ) {
542+ if ( $ inactive && empty ( $ inactive_args ) ) {
543+ WP_CLI ::error ( 'No inactive sidebars found. ' );
544+ }
521545 WP_CLI ::error ( 'No sidebar registered. ' );
522546 }
523547
524548 $ count = 0 ;
525549 $ errors = 0 ;
526- foreach ( $ args as $ sidebar_id ) {
527- if ( ! array_key_exists ( $ sidebar_id , $ wp_registered_sidebars ) ) {
550+ foreach ( $ all_args as $ sidebar_id ) {
551+ // Skip registration validation for sidebars resolved via --inactive.
552+ if ( ! in_array ( $ sidebar_id , $ inactive_args , true ) &&
553+ ! array_key_exists ( $ sidebar_id , $ wp_registered_sidebars ) ) {
528554 WP_CLI ::warning ( sprintf ( 'Invalid sidebar: %s ' , $ sidebar_id ) );
529555 ++$ errors ;
530556 continue ;
@@ -550,7 +576,28 @@ public function reset( $args, $assoc_args ) {
550576 }
551577 }
552578
553- Utils \report_batch_operation_results ( 'sidebar ' , 'reset ' , count ( $ args ), $ count , $ errors );
579+ Utils \report_batch_operation_results ( 'sidebar ' , 'reset ' , count ( $ all_args ), $ count , $ errors );
580+ }
581+
582+ /**
583+ * Returns the IDs of sidebars that exist in the database but are not currently registered.
584+ *
585+ * @return string[]
586+ */
587+ private function get_inactive_sidebar_ids () {
588+ global $ wp_registered_sidebars ;
589+
590+ $ all_sidebar_ids = array_keys ( $ this ->wp_get_sidebars_widgets () );
591+ $ registered_ids = array_keys ( $ wp_registered_sidebars );
592+
593+ return array_values (
594+ array_filter (
595+ array_diff ( $ all_sidebar_ids , $ registered_ids ),
596+ static function ( $ id ) {
597+ return 'wp_inactive_widgets ' !== $ id ;
598+ }
599+ )
600+ );
554601 }
555602
556603 /**
0 commit comments