44use WP_CLI \Formatter ;
55
66/**
7- * Manage registered sidebars.
7+ * Lists registered sidebars.
88 *
9- * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/)
10- * is any widgetized area of your theme.
9+ * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) is any widgetized area of your theme.
10+ *
11+ * ## EXAMPLES
12+ *
13+ * # List sidebars
14+ * $ wp sidebar list --fields=name,id --format=csv
15+ * name,id
16+ * "Widget Area",sidebar-1
17+ * "Inactive Widgets",wp_inactive_widgets
1118 */
1219class Sidebar_Command extends WP_CLI_Command {
1320
14- /**
15- * Default fields for sidebar output.
16- *
17- * @var array
18- */
19- private $ default_fields = [
21+ private $ fields = [
2022 'name ' ,
2123 'id ' ,
2224 'description ' ,
@@ -38,9 +40,9 @@ class Sidebar_Command extends WP_CLI_Command {
3840 * - table
3941 * - csv
4042 * - json
41- * - yaml
4243 * - ids
4344 * - count
45+ * - yaml
4446 * ---
4547 *
4648 * ## AVAILABLE FIELDS
@@ -61,40 +63,25 @@ class Sidebar_Command extends WP_CLI_Command {
6163 *
6264 * ## EXAMPLES
6365 *
64- * $ wp sidebar list
65- * $ wp sidebar list --fields= name,id
66- * $ wp sidebar list --format=ids
67- * $ wp sidebar list --format=count
66+ * $ wp sidebar list --fields=name,id --format=csv
67+ * name,id
68+ * "Widget Area", sidebar-1
69+ * "Inactive Widgets",wp_inactive_widgets
6870 *
6971 * @subcommand list
70- * @when after_wp_load
7172 */
7273 public function list_ ( $ args , $ assoc_args ) {
7374 global $ wp_registered_sidebars ;
7475
75- if ( function_exists ( 'wp_register_unused_sidebar ' ) ) {
76- Utils \wp_register_unused_sidebar ();
77- }
78-
79- // Filter out wp_inactive_widgets from the display
80- $ sidebars = array_filter (
81- $ wp_registered_sidebars ,
82- function ( $ sidebar ) {
83- return 'wp_inactive_widgets ' !== $ sidebar ['id ' ];
84- }
85- );
86-
87- if ( isset ( $ assoc_args ['format ' ] ) && 'ids ' === $ assoc_args ['format ' ] ) {
88- WP_CLI ::line ( implode ( ' ' , wp_list_pluck ( $ sidebars , 'id ' ) ) );
89- return ;
90- }
76+ Utils \wp_register_unused_sidebar ();
9177
92- if ( isset ( $ assoc_args ['format ' ] ) && 'count ' === $ assoc_args ['format ' ] ) {
93- WP_CLI ::line ( count ( $ sidebars ) );
94- return ;
78+ if ( ! empty ( $ assoc_args ['format ' ] ) && 'ids ' === $ assoc_args ['format ' ] ) {
79+ $ sidebars = wp_list_pluck ( $ wp_registered_sidebars , 'id ' );
80+ } else {
81+ $ sidebars = $ wp_registered_sidebars ;
9582 }
9683
97- $ formatter = new Formatter ( $ assoc_args , $ this ->default_fields );
84+ $ formatter = new Formatter ( $ assoc_args , $ this ->fields );
9885 $ formatter ->display_items ( $ sidebars );
9986 }
10087
@@ -129,17 +116,15 @@ function( $sidebar ) {
129116 public function get ( $ args , $ assoc_args ) {
130117 global $ wp_registered_sidebars ;
131118
132- if ( function_exists ( 'wp_register_unused_sidebar ' ) ) {
133- Utils \wp_register_unused_sidebar ();
134- }
119+ Utils \wp_register_unused_sidebar ();
135120
136121 $ id = $ args [0 ];
137122
138123 if ( ! isset ( $ wp_registered_sidebars [ $ id ] ) ) {
139124 WP_CLI ::error ( "Sidebar ' {$ id }' does not exist. " );
140125 }
141126
142- $ formatter = new Formatter ( $ assoc_args , $ this ->default_fields );
127+ $ formatter = new Formatter ( $ assoc_args , $ this ->fields );
143128 $ formatter ->display_item ( $ wp_registered_sidebars [ $ id ] );
144129 }
145130
@@ -161,78 +146,12 @@ public function get( $args, $assoc_args ) {
161146 public function exists ( $ args ) {
162147 global $ wp_registered_sidebars ;
163148
164- if ( function_exists ( 'wp_register_unused_sidebar ' ) ) {
165- Utils \wp_register_unused_sidebar ();
166- }
149+ Utils \wp_register_unused_sidebar ();
167150
168151 if ( isset ( $ wp_registered_sidebars [ $ args [0 ] ] ) ) {
169152 WP_CLI ::halt ( 0 );
170153 }
171154
172155 WP_CLI ::halt ( 1 );
173156 }
174-
175- /**
176- * List widgets assigned to a sidebar.
177- *
178- * ## OPTIONS
179- *
180- * <id>
181- * : The sidebar ID.
182- *
183- * [--format=<format>]
184- * : Render output in a particular format.
185- * ---
186- * default: table
187- * options:
188- * - table
189- * - csv
190- * - json
191- * - yaml
192- * - ids
193- * ---
194- *
195- * ## EXAMPLES
196- *
197- * $ wp sidebar widgets sidebar-1
198- * $ wp sidebar widgets wp_inactive_widgets --format=ids
199- *
200- * @when after_wp_load
201- */
202- public function widgets ( $ args , $ assoc_args ) {
203- global $ wp_registered_sidebars ;
204-
205- if ( function_exists ( 'wp_register_unused_sidebar ' ) ) {
206- Utils \wp_register_unused_sidebar ();
207- }
208-
209- $ id = $ args [0 ];
210-
211- if ( ! isset ( $ wp_registered_sidebars [ $ id ] ) ) {
212- WP_CLI ::error ( "Sidebar ' {$ id }' does not exist. " );
213- }
214-
215- $ sidebars_widgets = wp_get_sidebars_widgets ();
216- $ widget_ids = isset ( $ sidebars_widgets [ $ id ] ) ? $ sidebars_widgets [ $ id ] : [];
217-
218- if ( empty ( $ widget_ids ) ) {
219- WP_CLI ::warning ( "No widgets found in sidebar ' {$ id }'. " );
220- return ;
221- }
222-
223- if ( isset ( $ assoc_args ['format ' ] ) && 'ids ' === $ assoc_args ['format ' ] ) {
224- WP_CLI ::line ( implode ( ' ' , $ widget_ids ) );
225- return ;
226- }
227-
228- $ items = array_map (
229- function ( $ widget_id ) {
230- return [ 'id ' => $ widget_id ];
231- },
232- $ widget_ids
233- );
234-
235- $ formatter = new Formatter ( $ assoc_args , [ 'id ' ] );
236- $ formatter ->display_items ( $ items );
237- }
238157}
0 commit comments