Skip to content

Commit 720ded9

Browse files
Ensure inactive widgets sidebar is registered in wp sidebar subcommands
1 parent 1a0e6d6 commit 720ded9

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

src/Sidebar_Command.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ class Sidebar_Command extends WP_CLI_Command {
4343
* - count
4444
* ---
4545
*
46+
* ## AVAILABLE FIELDS
47+
*
48+
* These fields will be displayed by default for each sidebar:
49+
*
50+
* * name
51+
* * id
52+
* * description
53+
*
54+
* These fields are optionally available:
55+
*
56+
* * class
57+
* * before_widget
58+
* * after_widget
59+
* * before_title
60+
* * after_title
61+
*
4662
* ## EXAMPLES
4763
*
4864
* $ wp sidebar list
@@ -107,6 +123,10 @@ public function list_( $args, $assoc_args ) {
107123
public function get( $args, $assoc_args ) {
108124
global $wp_registered_sidebars;
109125

126+
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
127+
Utils\wp_register_unused_sidebar();
128+
}
129+
110130
$id = $args[0];
111131

112132
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
@@ -128,13 +148,17 @@ public function get( $args, $assoc_args ) {
128148
* ## EXAMPLES
129149
*
130150
* $ wp sidebar exists sidebar-1
131-
* $ wp sidebar exists foo && echo "exists"
151+
* $ wp sidebar exists wp_inactive_widgets && echo "exists"
132152
*
133153
* @when after_wp_load
134154
*/
135155
public function exists( $args ) {
136156
global $wp_registered_sidebars;
137157

158+
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
159+
Utils\wp_register_unused_sidebar();
160+
}
161+
138162
if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
139163
WP_CLI::halt( 0 );
140164
}
@@ -172,30 +196,34 @@ public function exists( $args ) {
172196
public function widgets( $args, $assoc_args ) {
173197
global $wp_registered_sidebars;
174198

199+
if ( function_exists( 'wp_register_unused_sidebar' ) ) {
200+
Utils\wp_register_unused_sidebar();
201+
}
202+
175203
$id = $args[0];
176204

177205
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
178206
WP_CLI::error( "Sidebar '{$id}' does not exist." );
179207
}
180208

181209
$sidebars_widgets = wp_get_sidebars_widgets();
182-
$widgets = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : [];
210+
$widget_ids = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : [];
183211

184-
if ( empty( $widgets ) ) {
212+
if ( empty( $widget_ids ) ) {
185213
WP_CLI::warning( "No widgets found in sidebar '{$id}'." );
186214
return;
187215
}
188216

189217
if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
190-
WP_CLI::line( implode( ' ', $widgets ) );
218+
WP_CLI::line( implode( ' ', $widget_ids ) );
191219
return;
192220
}
193221

194222
$items = array_map(
195223
function ( $widget_id ) {
196224
return [ 'id' => $widget_id ];
197225
},
198-
$widgets
226+
$widget_ids
199227
);
200228

201229
$formatter = new Formatter( $assoc_args, [ 'id' ] );

0 commit comments

Comments
 (0)