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 diff --git a/src/Sidebar_Command.php b/src/Sidebar_Command.php index b126b117..1128d36a 100644 --- a/src/Sidebar_Command.php +++ b/src/Sidebar_Command.php @@ -84,4 +84,74 @@ public function list_( $args, $assoc_args ) { $formatter = new Formatter( $assoc_args, $this->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 + * --- + * + * ## 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; + + Utils\wp_register_unused_sidebar(); + + $id = $args[0]; + + if ( ! isset( $wp_registered_sidebars[ $id ] ) ) { + WP_CLI::error( "Sidebar '{$id}' does not exist." ); + } + + $formatter = new Formatter( $assoc_args, $this->fields ); + $formatter->display_item( $wp_registered_sidebars[ $id ] ); + } + + /** + * Check if a sidebar exists. + * + * ## OPTIONS + * + * + * : The sidebar ID. + * + * ## EXAMPLES + * + * $ wp sidebar exists sidebar-1 + * $ wp sidebar exists wp_inactive_widgets && echo "exists" + * + * @when after_wp_load + */ + public function exists( $args ) { + global $wp_registered_sidebars; + + Utils\wp_register_unused_sidebar(); + + if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) { + WP_CLI::halt( 0 ); + } + + WP_CLI::halt( 1 ); + } }