Skip to content

Commit 805bba3

Browse files
Adds new and subcommands to the sidebar command
1 parent 0ab5159 commit 805bba3

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

features/sidebar.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,24 @@ Feature: Manage WordPress sidebars
2626
"""
2727
4
2828
"""
29+
30+
Scenario: Get sidebar details
31+
Given a WP install
32+
When I run `wp theme install twentytwelve --activate`
33+
And I run `wp sidebar get sidebar-1`
34+
Then STDOUT should contain:
35+
"""
36+
sidebar-1
37+
"""
38+
39+
Scenario: Sidebar exists command returns success
40+
Given a WP install
41+
When I run `wp theme install twentytwelve --activate`
42+
And I run `wp sidebar exists sidebar-1`
43+
Then the return code should be 0
44+
45+
Scenario: Sidebar exists command returns failure
46+
Given a WP install
47+
When I run `wp theme install twentytwelve --activate`
48+
And I try `wp sidebar exists does-not-exist`
49+
Then the return code should be 1

src/Sidebar_Command.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,74 @@ public function list_( $args, $assoc_args ) {
8484
$formatter = new Formatter( $assoc_args, $this->fields );
8585
$formatter->display_items( $sidebars );
8686
}
87+
88+
/**
89+
* Get details about a specific sidebar.
90+
*
91+
* ## OPTIONS
92+
*
93+
* <id>
94+
* : The sidebar ID.
95+
*
96+
* [--fields=<fields>]
97+
* : Limit the output to specific object fields.
98+
*
99+
* [--format=<format>]
100+
* : Render output in a particular format.
101+
* ---
102+
* default: table
103+
* options:
104+
* - table
105+
* - json
106+
* - yaml
107+
* ---
108+
*
109+
* ## EXAMPLES
110+
*
111+
* $ wp sidebar get sidebar-1
112+
* $ wp sidebar get wp_inactive_widgets --format=json
113+
*
114+
* @when after_wp_load
115+
*/
116+
public function get( $args, $assoc_args ) {
117+
global $wp_registered_sidebars;
118+
119+
Utils\wp_register_unused_sidebar();
120+
121+
$id = $args[0];
122+
123+
if ( ! isset( $wp_registered_sidebars[ $id ] ) ) {
124+
WP_CLI::error( "Sidebar '{$id}' does not exist." );
125+
}
126+
127+
$formatter = new Formatter( $assoc_args, $this->fields );
128+
$formatter->display_item( $wp_registered_sidebars[ $id ] );
129+
}
130+
131+
/**
132+
* Check if a sidebar exists.
133+
*
134+
* ## OPTIONS
135+
*
136+
* <id>
137+
* : The sidebar ID.
138+
*
139+
* ## EXAMPLES
140+
*
141+
* $ wp sidebar exists sidebar-1
142+
* $ wp sidebar exists wp_inactive_widgets && echo "exists"
143+
*
144+
* @when after_wp_load
145+
*/
146+
public function exists( $args ) {
147+
global $wp_registered_sidebars;
148+
149+
Utils\wp_register_unused_sidebar();
150+
151+
if ( isset( $wp_registered_sidebars[ $args[0] ] ) ) {
152+
WP_CLI::halt( 0 );
153+
}
154+
155+
WP_CLI::halt( 1 );
156+
}
87157
}

0 commit comments

Comments
 (0)