From 1a0e6d6f6490f00b4ac902e9375c0146da7479b2 Mon Sep 17 00:00:00 2001 From: Sohilbhai Ghanchivahora Date: Wed, 28 Jan 2026 13:41:04 +0530 Subject: [PATCH 1/5] Extend wp sidebar command with get, exists, and widgets subcommands --- src/Sidebar_Command.php | 187 ++++++++++++++++++++++++++++++++-------- 1 file changed, 152 insertions(+), 35 deletions(-) diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index b126b117..c2d86427 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -4,21 +4,19 @@ use WP_CLI\Formatter; /** - * Lists registered sidebars. + * Manage registered sidebars. * - * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) is any widgetized area of your theme. - * - * ## EXAMPLES - * - * # List sidebars - * $ wp sidebar list --fields=name,id --format=csv - * name,id - * "Widget Area",sidebar-1 - * "Inactive Widgets",wp_inactive_widgets + * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) + * is any widgetized area of your theme. */ class Sidebar_Command extends WP_CLI_Command { - private $fields = [ + /** + * Default fields for sidebar output. + * + * @var array + */ + private $default_fields = [ 'name', 'id', 'description', @@ -40,48 +38,167 @@ class Sidebar_Command extends WP_CLI_Command { * - table * - csv * - json + * - yaml * - ids * - count + * --- + * + * ## EXAMPLES + * + * $ wp sidebar list + * $ wp sidebar list --fields=name,id + * $ wp sidebar list --format=ids + * $ wp sidebar list --format=count + * + * @subcommand list + * @when after_wp_load + */ + public function list_( $args, $assoc_args ) { + global $wp_registered_sidebars; + + if ( function_exists( 'wp_register_unused_sidebar' ) ) { + Utils\wp_register_unused_sidebar(); + } + + $sidebars = $wp_registered_sidebars; + + if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { + WP_CLI::line( implode( ' ', wp_list_pluck( $sidebars, 'id' ) ) ); + return; + } + + if ( isset( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) { + WP_CLI::line( count( $sidebars ) ); + return; + } + + $formatter = new Formatter( $assoc_args, $this->default_fields ); + $formatter->display_items( $sidebars ); + } + + /** + * Get details about a specific sidebar. + * + * ## OPTIONS + * + * + * : The sidebar ID. + * + * [--fields=] + * : Limit the output to specific object fields. + * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - json * - yaml * --- * - * ## AVAILABLE FIELDS + * ## EXAMPLES + * + * $ wp sidebar get sidebar-1 + * $ wp sidebar get wp_inactive_widgets --format=json + * + * @when after_wp_load + */ + public function get( $args, $assoc_args ) { + global $wp_registered_sidebars; + + $id = $args[0]; + + if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { + WP_CLI::error( "Sidebar '{$id}' does not exist." ); + } + + $formatter = new Formatter( $assoc_args, $this->default_fields ); + $formatter->display_item( $wp_registered_sidebars[ $id ] ); + } + + /** + * Check if a sidebar exists. + * + * ## OPTIONS * - * These fields will be displayed by default for each sidebar: + * + * : The sidebar ID. + * + * ## EXAMPLES + * + * $ wp sidebar exists sidebar-1 + * $ wp sidebar exists foo && echo "exists" + * + * @when after_wp_load + */ + public function exists( $args ) { + global $wp_registered_sidebars; + + if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) { + WP_CLI::halt( 0 ); + } + + WP_CLI::halt( 1 ); + } + + /** + * List widgets assigned to a sidebar. * - * * name - * * id - * * description + * ## OPTIONS * - * These fields are optionally available: + * + * : The sidebar ID. * - * * class - * * before_widget - * * after_widget - * * before_title - * * after_title + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - csv + * - json + * - yaml + * - ids + * --- * * ## EXAMPLES * - * $ wp sidebar list --fields=name,id --format=csv - * name,id - * "Widget Area",sidebar-1 - * "Inactive Widgets",wp_inactive_widgets + * $ wp sidebar widgets sidebar-1 + * $ wp sidebar widgets wp_inactive_widgets --format=ids * - * @subcommand list + * @when after_wp_load */ - public function list_( $args, $assoc_args ) { + public function widgets( $args, $assoc_args ) { global $wp_registered_sidebars; - Utils\wp_register_unused_sidebar(); + $id = $args[0]; - if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { - $sidebars = wp_list_pluck( $wp_registered_sidebars, 'id' ); - } else { - $sidebars = $wp_registered_sidebars; + if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { + WP_CLI::error( "Sidebar '{$id}' does not exist." ); } - $formatter = new Formatter( $assoc_args, $this->fields ); - $formatter->display_items( $sidebars ); + $sidebars_widgets = wp_get_sidebars_widgets(); + $widgets = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : []; + + if ( empty( $widgets ) ) { + WP_CLI::warning( "No widgets found in sidebar '{$id}'." ); + return; + } + + if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { + WP_CLI::line( implode( ' ', $widgets ) ); + return; + } + + $items = array_map( + function ( $widget_id ) { + return [ 'id' => $widget_id ]; + }, + $widgets + ); + + $formatter = new Formatter( $assoc_args, [ 'id' ] ); + $formatter->display_items( $items ); } } From 720ded9f4686abdd3f8995aa1117196a9aeaa0b0 Mon Sep 17 00:00:00 2001 From: Sohilbhai Ghanchivahora Date: Wed, 28 Jan 2026 13:54:33 +0530 Subject: [PATCH 2/5] Ensure inactive widgets sidebar is registered in wp sidebar subcommands --- src/Sidebar_Command.php | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index c2d86427..f0a4c53b 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -43,6 +43,22 @@ class Sidebar_Command extends WP_CLI_Command { * - count * --- * + * ## AVAILABLE FIELDS + * + * These fields will be displayed by default for each sidebar: + * + * * name + * * id + * * description + * + * These fields are optionally available: + * + * * class + * * before_widget + * * after_widget + * * before_title + * * after_title + * * ## EXAMPLES * * $ wp sidebar list @@ -107,6 +123,10 @@ public function list_( $args, $assoc_args ) { public function get( $args, $assoc_args ) { global $wp_registered_sidebars; + if ( function_exists( 'wp_register_unused_sidebar' ) ) { + Utils\wp_register_unused_sidebar(); + } + $id = $args[0]; if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { @@ -128,13 +148,17 @@ public function get( $args, $assoc_args ) { * ## EXAMPLES * * $ wp sidebar exists sidebar-1 - * $ wp sidebar exists foo && echo "exists" + * $ wp sidebar exists wp_inactive_widgets && echo "exists" * * @when after_wp_load */ public function exists( $args ) { global $wp_registered_sidebars; + if ( function_exists( 'wp_register_unused_sidebar' ) ) { + Utils\wp_register_unused_sidebar(); + } + if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) { WP_CLI::halt( 0 ); } @@ -172,6 +196,10 @@ public function exists( $args ) { public function widgets( $args, $assoc_args ) { global $wp_registered_sidebars; + if ( function_exists( 'wp_register_unused_sidebar' ) ) { + Utils\wp_register_unused_sidebar(); + } + $id = $args[0]; if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { @@ -179,15 +207,15 @@ public function widgets( $args, $assoc_args ) { } $sidebars_widgets = wp_get_sidebars_widgets(); - $widgets = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : []; + $widget_ids = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : []; - if ( empty( $widgets ) ) { + if ( empty( $widget_ids ) ) { WP_CLI::warning( "No widgets found in sidebar '{$id}'." ); return; } if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { - WP_CLI::line( implode( ' ', $widgets ) ); + WP_CLI::line( implode( ' ', $widget_ids ) ); return; } @@ -195,7 +223,7 @@ public function widgets( $args, $assoc_args ) { function ( $widget_id ) { return [ 'id' => $widget_id ]; }, - $widgets + $widget_ids ); $formatter = new Formatter( $assoc_args, [ 'id' ] ); From 36aa3e14bc582ca664a2e9e8c6e14748a37b64ee Mon Sep 17 00:00:00 2001 From: Sohilbhai Ghanchivahora Date: Wed, 28 Jan 2026 18:22:32 +0530 Subject: [PATCH 3/5] fix: exclude wp_inactive_widgets --- src/Sidebar_Command.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index f0a4c53b..23d620ee 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -76,7 +76,13 @@ public function list_( $args, $assoc_args ) { Utils\wp_register_unused_sidebar(); } - $sidebars = $wp_registered_sidebars; + // Filter out wp_inactive_widgets from the display + $sidebars = array_filter( + $wp_registered_sidebars, + function( $sidebar ) { + return 'wp_inactive_widgets' !== $sidebar['id']; + } + ); if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { WP_CLI::line( implode( ' ', wp_list_pluck( $sidebars, 'id' ) ) ); From 0e6d9d170af89e25c843f64f5342a270655a6b4f Mon Sep 17 00:00:00 2001 From: Sohilbhai Ghanchivahora Date: Wed, 28 Jan 2026 19:19:55 +0530 Subject: [PATCH 4/5] Updated only get and exists function --- src/Sidebar_Command.php | 131 ++++++++-------------------------------- 1 file changed, 25 insertions(+), 106 deletions(-) diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index 23d620ee..1128d36a 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -4,19 +4,21 @@ use WP_CLI\Formatter; /** - * Manage registered sidebars. + * Lists registered sidebars. * - * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) - * is any widgetized area of your theme. + * A [sidebar](https://developer.wordpress.org/themes/functionality/sidebars/) is any widgetized area of your theme. + * + * ## EXAMPLES + * + * # List sidebars + * $ wp sidebar list --fields=name,id --format=csv + * name,id + * "Widget Area",sidebar-1 + * "Inactive Widgets",wp_inactive_widgets */ class Sidebar_Command extends WP_CLI_Command { - /** - * Default fields for sidebar output. - * - * @var array - */ - private $default_fields = [ + private $fields = [ 'name', 'id', 'description', @@ -38,9 +40,9 @@ class Sidebar_Command extends WP_CLI_Command { * - table * - csv * - json - * - yaml * - ids * - count + * - yaml * --- * * ## AVAILABLE FIELDS @@ -61,40 +63,25 @@ class Sidebar_Command extends WP_CLI_Command { * * ## EXAMPLES * - * $ wp sidebar list - * $ wp sidebar list --fields=name,id - * $ wp sidebar list --format=ids - * $ wp sidebar list --format=count + * $ wp sidebar list --fields=name,id --format=csv + * name,id + * "Widget Area",sidebar-1 + * "Inactive Widgets",wp_inactive_widgets * * @subcommand list - * @when after_wp_load */ public function list_( $args, $assoc_args ) { global $wp_registered_sidebars; - if ( function_exists( 'wp_register_unused_sidebar' ) ) { - Utils\wp_register_unused_sidebar(); - } - - // Filter out wp_inactive_widgets from the display - $sidebars = array_filter( - $wp_registered_sidebars, - function( $sidebar ) { - return 'wp_inactive_widgets' !== $sidebar['id']; - } - ); - - if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { - WP_CLI::line( implode( ' ', wp_list_pluck( $sidebars, 'id' ) ) ); - return; - } + Utils\wp_register_unused_sidebar(); - if ( isset( $assoc_args['format'] ) && 'count' === $assoc_args['format'] ) { - WP_CLI::line( count( $sidebars ) ); - return; + if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { + $sidebars = wp_list_pluck( $wp_registered_sidebars, 'id' ); + } else { + $sidebars = $wp_registered_sidebars; } - $formatter = new Formatter( $assoc_args, $this->default_fields ); + $formatter = new Formatter( $assoc_args, $this->fields ); $formatter->display_items( $sidebars ); } @@ -129,9 +116,7 @@ function( $sidebar ) { public function get( $args, $assoc_args ) { global $wp_registered_sidebars; - if ( function_exists( 'wp_register_unused_sidebar' ) ) { - Utils\wp_register_unused_sidebar(); - } + Utils\wp_register_unused_sidebar(); $id = $args[0]; @@ -139,7 +124,7 @@ public function get( $args, $assoc_args ) { WP_CLI::error( "Sidebar '{$id}' does not exist." ); } - $formatter = new Formatter( $assoc_args, $this->default_fields ); + $formatter = new Formatter( $assoc_args, $this->fields ); $formatter->display_item( $wp_registered_sidebars[ $id ] ); } @@ -161,9 +146,7 @@ public function get( $args, $assoc_args ) { public function exists( $args ) { global $wp_registered_sidebars; - if ( function_exists( 'wp_register_unused_sidebar' ) ) { - Utils\wp_register_unused_sidebar(); - } + Utils\wp_register_unused_sidebar(); if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) { WP_CLI::halt( 0 ); @@ -171,68 +154,4 @@ public function exists( $args ) { WP_CLI::halt( 1 ); } - - /** - * List widgets assigned to a sidebar. - * - * ## OPTIONS - * - * - * : The sidebar ID. - * - * [--format=] - * : Render output in a particular format. - * --- - * default: table - * options: - * - table - * - csv - * - json - * - yaml - * - ids - * --- - * - * ## EXAMPLES - * - * $ wp sidebar widgets sidebar-1 - * $ wp sidebar widgets wp_inactive_widgets --format=ids - * - * @when after_wp_load - */ - public function widgets( $args, $assoc_args ) { - global $wp_registered_sidebars; - - if ( function_exists( 'wp_register_unused_sidebar' ) ) { - Utils\wp_register_unused_sidebar(); - } - - $id = $args[0]; - - if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { - WP_CLI::error( "Sidebar '{$id}' does not exist." ); - } - - $sidebars_widgets = wp_get_sidebars_widgets(); - $widget_ids = isset( $sidebars_widgets[ $id ] ) ? $sidebars_widgets[ $id ] : []; - - if ( empty( $widget_ids ) ) { - WP_CLI::warning( "No widgets found in sidebar '{$id}'." ); - return; - } - - if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) { - WP_CLI::line( implode( ' ', $widget_ids ) ); - return; - } - - $items = array_map( - function ( $widget_id ) { - return [ 'id' => $widget_id ]; - }, - $widget_ids - ); - - $formatter = new Formatter( $assoc_args, [ 'id' ] ); - $formatter->display_items( $items ); - } } From 31910a947de50fe1e55c4f85f4cdc5fd4d5e4e31 Mon Sep 17 00:00:00 2001 From: Sohilbhai Ghanchivahora Date: Mon, 2 Feb 2026 20:02:14 +0530 Subject: [PATCH 5/5] Added feature for testing of the get and exists function --- features/sidebar.feature | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/features/sidebar.feature b/features/sidebar.feature index 3741e57b..9aa76c68 100644 --- a/features/sidebar.feature +++ b/features/sidebar.feature @@ -26,3 +26,21 @@ Feature: Manage WordPress sidebars """ 4 """ + + Scenario: Get sidebar details + Given a WP install + When I run `wp sidebar get sidebar-1` + Then STDOUT should contain: + """ + sidebar-1 + """ + + Scenario: Sidebar exists command returns success + Given a WP install + When I run `wp sidebar exists sidebar-1` + Then the return code should be 0 + + Scenario: Sidebar exists command returns failure + Given a WP install + When I run `wp sidebar exists does-not-exist` + Then the return code should be 1 \ No newline at end of file