Skip to content

Commit 07a0508

Browse files
Copilotswissspidy
andcommitted
feat: add --inactive flag to wp sidebar list and wp widget reset
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 088c048 commit 07a0508

4 files changed

Lines changed: 155 additions & 15 deletions

File tree

features/sidebar.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@ Feature: Manage WordPress sidebars
2323
4
2424
"""
2525

26+
Scenario: List inactive sidebars
27+
When I run `wp sidebar list --inactive --format=count`
28+
Then STDOUT should be:
29+
"""
30+
0
31+
"""
32+
33+
When I run `wp eval 'update_option( "sidebars_widgets", array_merge( wp_get_sidebars_widgets(), [ "orphaned-sidebar-1" => [] ] ) );'`
34+
And I run `wp sidebar list --inactive --fields=id --format=csv`
35+
Then STDOUT should be:
36+
"""
37+
id
38+
orphaned-sidebar-1
39+
"""
40+
41+
When I run `wp sidebar list --fields=id --format=ids`
42+
Then STDOUT should not contain:
43+
"""
44+
orphaned-sidebar-1
45+
"""
46+
2647
Scenario: Get sidebar details
2748
When I run `wp sidebar get sidebar-1`
2849
Then STDOUT should contain:

features/widget-reset.feature

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Feature: Reset WordPress sidebars
2424
When I try `wp widget reset`
2525
Then STDERR should be:
2626
"""
27-
Error: Please specify one or more sidebars, or use --all.
27+
Error: Please specify one or more sidebars, or use --all or --inactive.
2828
"""
2929

3030
When I try `wp widget reset sidebar-1`
@@ -153,3 +153,37 @@ Feature: Reset WordPress sidebars
153153
"""
154154
calendar-1 search-1
155155
"""
156+
157+
Scenario: Reset inactive sidebars
158+
When I try `wp widget reset --inactive`
159+
Then STDERR should be:
160+
"""
161+
Error: No inactive sidebars found.
162+
"""
163+
And the return code should be 1
164+
165+
When I run `wp widget add calendar sidebar-1 --title="Calendar"`
166+
Then STDOUT should not be empty
167+
168+
# Simulate an inactive (unregistered) sidebar by moving widget data to an orphaned key
169+
When I run `wp eval '$w = wp_get_sidebars_widgets(); $w["orphaned-sidebar-1"] = $w["sidebar-1"]; $w["sidebar-1"] = []; update_option( "sidebars_widgets", $w );'`
170+
171+
When I run `wp sidebar list --inactive --fields=id --format=ids`
172+
Then STDOUT should be:
173+
"""
174+
orphaned-sidebar-1
175+
"""
176+
177+
When I run `wp widget reset --inactive`
178+
Then STDOUT should be:
179+
"""
180+
Sidebar 'orphaned-sidebar-1' reset.
181+
Success: Reset 1 of 1 sidebars.
182+
"""
183+
And the return code should be 0
184+
185+
When I run `wp widget list wp_inactive_widgets --format=ids`
186+
Then STDOUT should contain:
187+
"""
188+
calendar-1
189+
"""

src/Sidebar_Command.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Sidebar_Command extends WP_CLI_Command {
2929
*
3030
* ## OPTIONS
3131
*
32+
* [--inactive]
33+
* : If set, only inactive sidebars will be listed.
34+
*
3235
* [--fields=<fields>]
3336
* : Limit the output to specific object fields.
3437
*
@@ -68,19 +71,54 @@ class Sidebar_Command extends WP_CLI_Command {
6871
* "Widget Area",sidebar-1
6972
* "Inactive Widgets",wp_inactive_widgets
7073
*
74+
* $ wp sidebar list --inactive --fields=id --format=csv
75+
* id
76+
* old-sidebar-1
77+
*
7178
* @subcommand list
7279
*/
7380
public function list_( $args, $assoc_args ) {
7481
global $wp_registered_sidebars;
7582

7683
Utils\wp_register_unused_sidebar();
7784

78-
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
79-
$sidebars = wp_list_pluck( $wp_registered_sidebars, 'id' );
85+
$inactive = Utils\get_flag_value( $assoc_args, 'inactive', false );
86+
87+
if ( $inactive ) {
88+
$sidebars_widgets = get_option( 'sidebars_widgets', [] );
89+
if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) {
90+
unset( $sidebars_widgets['array_version'] );
91+
}
92+
$registered_ids = array_keys( $wp_registered_sidebars );
93+
$inactive_sidebar_ids = array_values(
94+
array_filter(
95+
array_diff( array_keys( $sidebars_widgets ), $registered_ids ),
96+
static function ( $id ) {
97+
return 'wp_inactive_widgets' !== $id;
98+
}
99+
)
100+
);
101+
$sidebars = [];
102+
foreach ( $inactive_sidebar_ids as $sidebar_id ) {
103+
$sidebars[ $sidebar_id ] = [
104+
'name' => $sidebar_id,
105+
'id' => $sidebar_id,
106+
'description' => '',
107+
'class' => '',
108+
'before_widget' => '',
109+
'after_widget' => '',
110+
'before_title' => '',
111+
'after_title' => '',
112+
];
113+
}
80114
} else {
81115
$sidebars = $wp_registered_sidebars;
82116
}
83117

118+
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
119+
$sidebars = wp_list_pluck( $sidebars, 'id' );
120+
}
121+
84122
$formatter = new Formatter( $assoc_args, $this->fields );
85123
$formatter->display_items( $sidebars );
86124
}

src/Widget_Command.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)